| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# (c) 2019-2026, NetApp, Inc |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
''' |
| 7 |
na_ontap_iscsi_security |
| 8 |
''' |
| 9 |
|
| 10 |
from __future__ import absolute_import, division, print_function |
| 11 |
__metaclass__ = type |
| 12 |
|
| 13 |
DOCUMENTATION = ''' |
| 14 |
author: NetApp Ansible Team (@carchi8py) <[email protected]> |
| 15 |
description: |
| 16 |
- Create/Delete/Modify iscsi security. |
| 17 |
extends_documentation_fragment: |
| 18 |
- netapp.ontap.netapp.na_ontap_rest |
| 19 |
module: na_ontap_iscsi_security |
| 20 |
options: |
| 21 |
state: |
| 22 |
choices: ['present', 'absent'] |
| 23 |
description: |
| 24 |
- Whether the specified initiator should exist or not. |
| 25 |
default: present |
| 26 |
type: str |
| 27 |
vserver: |
| 28 |
description: |
| 29 |
- Name of the vserver to use. |
| 30 |
required: true |
| 31 |
type: str |
| 32 |
auth_type: |
| 33 |
description: |
| 34 |
- Specifies the authentication type. |
| 35 |
choices: ['chap', 'none', 'deny'] |
| 36 |
type: str |
| 37 |
initiator: |
| 38 |
description: |
| 39 |
- Specifies the name of the initiator. |
| 40 |
required: true |
| 41 |
type: str |
| 42 |
address_ranges: |
| 43 |
description: |
| 44 |
- May be a single IPv4 or IPv6 address or a range containing a startaddress and an end address. |
| 45 |
- The start and end addresses themselves are included in the range. |
| 46 |
- If not present, the initiator is allowed to log in from any IP address. |
| 47 |
type: list |
| 48 |
elements: str |
| 49 |
inbound_username: |
| 50 |
description: |
| 51 |
- Inbound CHAP username. |
| 52 |
- Required for CHAP. A null username is not allowed. |
| 53 |
type: str |
| 54 |
inbound_password: |
| 55 |
description: |
| 56 |
- Inbound CHAP user password. |
| 57 |
- Can not be modified. If want to change password, delete and re-create the initiator. |
| 58 |
type: str |
| 59 |
outbound_username: |
| 60 |
description: |
| 61 |
- Outbound CHAP user name. |
| 62 |
type: str |
| 63 |
outbound_password: |
| 64 |
description: |
| 65 |
- Outbound CHAP user password. |
| 66 |
- Can not be modified. If want to change password, delete and re-create the initiator. |
| 67 |
type: str |
| 68 |
lambda_config: |
| 69 |
description: |
| 70 |
- Configuration parameters for AWS Lambda proxy functionality. |
| 71 |
- These option and suboptions are only supported with REST. |
| 72 |
type: dict |
| 73 |
version_added: 23.5.0 |
| 74 |
suboptions: |
| 75 |
function_name: |
| 76 |
description: |
| 77 |
- The name of the AWS Lambda function to invoke. |
| 78 |
type: str |
| 79 |
required: true |
| 80 |
aws_region: |
| 81 |
description: |
| 82 |
- The name of the AWS region. |
| 83 |
type: str |
| 84 |
required: true |
| 85 |
aws_profile: |
| 86 |
description: |
| 87 |
- The name of the AWS profile to use for authentication. |
| 88 |
type: str |
| 89 |
|
| 90 |
notes: |
| 91 |
- Supports AWS Lambda proxy functionality. See README for example usage. |
| 92 |
short_description: "NetApp ONTAP Manage iscsi security." |
| 93 |
version_added: "19.11.0" |
| 94 |
''' |
| 95 |
|
| 96 |
EXAMPLES = """ |
| 97 |
- name: Create iscsi security |
| 98 |
netapp.ontap.na_ontap_iscsi_security: |
| 99 |
hostname: "{{ netapp_hostname }}" |
| 100 |
username: "{{ netapp_username }}" |
| 101 |
password: "{{ netapp_password }}" |
| 102 |
vserver: test_svm |
| 103 |
state: present |
| 104 |
initiator: eui.9999956789abcdef |
| 105 |
inbound_username: user_1 |
| 106 |
inbound_password: password_1 |
| 107 |
outbound_username: user_2 |
| 108 |
outbound_password: password_2 |
| 109 |
auth_type: chap |
| 110 |
address_ranges: 10.125.10.0-10.125.10.10,10.125.193.78 |
| 111 |
|
| 112 |
- name: Modify outbound username |
| 113 |
netapp.ontap.na_ontap_iscsi_security: |
| 114 |
hostname: "{{ netapp_hostname }}" |
| 115 |
username: "{{ netapp_username }}" |
| 116 |
password: "{{ netapp_password }}" |
| 117 |
vserver: test_svm |
| 118 |
state: present |
| 119 |
initiator: eui.9999956789abcdef |
| 120 |
inbound_username: user_1 |
| 121 |
inbound_password: password_1 |
| 122 |
outbound_username: user_out_3 |
| 123 |
outbound_password: password_3 |
| 124 |
auth_type: chap |
| 125 |
address_ranges: 10.125.10.0-10.125.10.10,10.125.193.78 |
| 126 |
|
| 127 |
- name: Modify address |
| 128 |
netapp.ontap.na_ontap_iscsi_security: |
| 129 |
hostname: "{{ netapp_hostname }}" |
| 130 |
username: "{{ netapp_username }}" |
| 131 |
password: "{{ netapp_password }}" |
| 132 |
vserver: test_svm |
| 133 |
state: present |
| 134 |
initiator: eui.9999956789abcdef |
| 135 |
address_ranges: 10.125.193.90,10.125.10.20-10.125.10.30 |
| 136 |
""" |
| 137 |
|
| 138 |
RETURN = """ |
| 139 |
""" |
| 140 |
|
| 141 |
from ansible.module_utils.basic import AnsibleModule |
| 142 |
import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils |
| 143 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule |
| 144 |
import ansible_collections.netapp.ontap.plugins.module_utils.rest_response_helpers as rrh |
| 145 |
|
| 146 |
|
| 147 |
class NetAppONTAPIscsiSecurity: |
| 148 |
""" |
| 149 |
Class with iscsi security methods |
| 150 |
""" |
| 151 |
def __init__(self): |
| 152 |
self.argument_spec = netapp_utils.na_ontap_rest_only_spec() |
| 153 |
self.argument_spec.update(dict( |
| 154 |
state=dict(required=False, type='str', choices=['present', 'absent'], default='present'), |
| 155 |
vserver=dict(required=True, type='str'), |
| 156 |
auth_type=dict(required=False, type='str', choices=['chap', 'none', 'deny']), |
| 157 |
inbound_password=dict(required=False, type='str', no_log=True), |
| 158 |
inbound_username=dict(required=False, type='str'), |
| 159 |
initiator=dict(required=True, type='str'), |
| 160 |
address_ranges=dict(required=False, type='list', elements='str'), |
| 161 |
outbound_password=dict(required=False, type='str', no_log=True), |
| 162 |
outbound_username=dict(required=False, type='str'), |
| 163 |
)) |
| 164 |
self.argument_spec.update(netapp_utils.na_ontap_lambda_argument_spec()) |
| 165 |
|
| 166 |
self.module = AnsibleModule( |
| 167 |
argument_spec=self.argument_spec, |
| 168 |
supports_check_mode=True, |
| 169 |
required_if=[ |
| 170 |
['auth_type', 'chap', ['inbound_username', 'inbound_password']], |
| 171 |
['use_lambda', True, ('lambda_config',)] |
| 172 |
], |
| 173 |
required_together=[ |
| 174 |
['inbound_username', 'inbound_password'], |
| 175 |
['outbound_username', 'outbound_password'], |
| 176 |
], |
| 177 |
) |
| 178 |
|
| 179 |
self.na_helper = NetAppModule() |
| 180 |
self.parameters = self.na_helper.set_parameters(self.module.params) |
| 181 |
|
| 182 |
self.rest_api = netapp_utils.OntapRestAPI(self.module) |
| 183 |
self.rest_api.fail_if_not_rest_minimum_version('na_ontap_iscsi_security:', 9, 6) |
| 184 |
self.uuid = self.get_svm_uuid() |
| 185 |
|
| 186 |
def get_initiator(self): |
| 187 |
""" |
| 188 |
Get current initiator. |
| 189 |
:return: dict of current initiator details. |
| 190 |
""" |
| 191 |
params = {'fields': '*', 'initiator': self.parameters['initiator']} |
| 192 |
api = 'protocols/san/iscsi/credentials' |
| 193 |
message, error = self.rest_api.get(api, params) |
| 194 |
if error is not None: |
| 195 |
self.module.fail_json(msg="Error on fetching initiator: %s" % error) |
| 196 |
if message['num_records'] > 0: |
| 197 |
record = message['records'][0] |
| 198 |
initiator_details = {'auth_type': record['authentication_type']} |
| 199 |
if initiator_details['auth_type'] == 'chap': |
| 200 |
if record['chap'].get('inbound'): |
| 201 |
initiator_details['inbound_username'] = record['chap']['inbound']['user'] |
| 202 |
else: |
| 203 |
initiator_details['inbound_username'] = None |
| 204 |
if record['chap'].get('outbound'): |
| 205 |
initiator_details['outbound_username'] = record['chap']['outbound']['user'] |
| 206 |
else: |
| 207 |
initiator_details['outbound_username'] = None |
| 208 |
if record.get('initiator_address'): |
| 209 |
if record['initiator_address'].get('ranges'): |
| 210 |
ranges = [] |
| 211 |
for address_range in record['initiator_address']['ranges']: |
| 212 |
if address_range['start'] == address_range['end']: |
| 213 |
ranges.append(address_range['start']) |
| 214 |
else: |
| 215 |
ranges.append(address_range['start'] + '-' + address_range['end']) |
| 216 |
initiator_details['address_ranges'] = ranges |
| 217 |
else: |
| 218 |
initiator_details['address_ranges'] = [] |
| 219 |
else: |
| 220 |
initiator_details['address_ranges'] = [] |
| 221 |
return initiator_details |
| 222 |
|
| 223 |
def create_initiator(self): |
| 224 |
""" |
| 225 |
Create initiator. |
| 226 |
:return: None. |
| 227 |
""" |
| 228 |
body = { |
| 229 |
'authentication_type': self.parameters['auth_type'], |
| 230 |
'initiator': self.parameters['initiator'] |
| 231 |
} |
| 232 |
|
| 233 |
if self.parameters['auth_type'] == 'chap': |
| 234 |
chap_info = {'inbound': {'user': self.parameters['inbound_username'], 'password': self.parameters['inbound_password']}} |
| 235 |
|
| 236 |
if self.parameters.get('outbound_username'): |
| 237 |
chap_info['outbound'] = {'user': self.parameters['outbound_username'], 'password': self.parameters['outbound_password']} |
| 238 |
body['chap'] = chap_info |
| 239 |
address_info = self.get_address_info(self.parameters.get('address_ranges')) |
| 240 |
if address_info is not None: |
| 241 |
body['initiator_address'] = {'ranges': address_info} |
| 242 |
body['svm'] = {'uuid': self.uuid, 'name': self.parameters['vserver']} |
| 243 |
api = 'protocols/san/iscsi/credentials' |
| 244 |
dummy, error = self.rest_api.post(api, body) |
| 245 |
if error is not None: |
| 246 |
self.module.fail_json(msg="Error on creating initiator: %s" % error) |
| 247 |
|
| 248 |
def delete_initiator(self): |
| 249 |
""" |
| 250 |
Delete initiator. |
| 251 |
:return: None. |
| 252 |
""" |
| 253 |
api = 'protocols/san/iscsi/credentials/{0}/{1}'.format(self.uuid, self.parameters['initiator']) |
| 254 |
dummy, error = self.rest_api.delete(api) |
| 255 |
if error is not None: |
| 256 |
self.module.fail_json(msg="Error on deleting initiator: %s" % error) |
| 257 |
|
| 258 |
def modify_initiator(self, modify, current): |
| 259 |
""" |
| 260 |
Modify initiator. |
| 261 |
:param modify: dict of modify attributes. |
| 262 |
:return: None. |
| 263 |
""" |
| 264 |
body = {} |
| 265 |
use_chap = False |
| 266 |
chap_update = False |
| 267 |
chap_update_inbound = False |
| 268 |
chap_update_outbound = False |
| 269 |
|
| 270 |
if modify.get('auth_type'): |
| 271 |
body['authentication_type'] = modify.get('auth_type') |
| 272 |
if modify['auth_type'] == 'chap': |
| 273 |
# change in auth_type |
| 274 |
chap_update = True |
| 275 |
use_chap = True |
| 276 |
elif current.get('auth_type') == 'chap': |
| 277 |
# we're already using chap |
| 278 |
use_chap = True |
| 279 |
|
| 280 |
if use_chap and (modify.get('inbound_username') or modify.get('inbound_password')): |
| 281 |
# change in chap inbound credentials |
| 282 |
chap_update = True |
| 283 |
chap_update_inbound = True |
| 284 |
|
| 285 |
if use_chap and (modify.get('outbound_username') or modify.get('outbound_password')): |
| 286 |
# change in chap outbound credentials |
| 287 |
chap_update = True |
| 288 |
chap_update_outbound = True |
| 289 |
|
| 290 |
if chap_update and not chap_update_inbound and 'inbound_username' in self.parameters: |
| 291 |
# use credentials from input |
| 292 |
chap_update_inbound = True |
| 293 |
|
| 294 |
if chap_update and not chap_update_outbound and 'outbound_username' in self.parameters: |
| 295 |
# use credentials from input |
| 296 |
chap_update_outbound = True |
| 297 |
|
| 298 |
if chap_update: |
| 299 |
chap_info = dict() |
| 300 |
# set values from self.parameters as they may not show as modified |
| 301 |
if chap_update_inbound: |
| 302 |
chap_info['inbound'] = {'user': self.parameters['inbound_username'], 'password': self.parameters['inbound_password']} |
| 303 |
else: |
| 304 |
# use current values as inbound username/password are required |
| 305 |
chap_info['inbound'] = {'user': current.get('inbound_username'), 'password': current.get('inbound_password')} |
| 306 |
if chap_update_outbound: |
| 307 |
chap_info['outbound'] = {'user': self.parameters['outbound_username'], 'password': self.parameters['outbound_password']} |
| 308 |
body['chap'] = chap_info |
| 309 |
# PATCH fails if this is not present, even though there is no change |
| 310 |
body['authentication_type'] = 'chap' |
| 311 |
|
| 312 |
address_info = self.get_address_info(modify.get('address_ranges')) |
| 313 |
if address_info is not None: |
| 314 |
body['initiator_address'] = {'ranges': address_info} |
| 315 |
api = 'protocols/san/iscsi/credentials/{0}/{1}'.format(self.uuid, self.parameters['initiator']) |
| 316 |
dummy, error = self.rest_api.patch(api, body) |
| 317 |
if error is not None: |
| 318 |
self.module.fail_json(msg="Error on modifying initiator: %s - params: %s" % (error, body)) |
| 319 |
|
| 320 |
def get_address_info(self, address_ranges): |
| 321 |
if address_ranges is None: |
| 322 |
return None |
| 323 |
address_info = [] |
| 324 |
for address in address_ranges: |
| 325 |
address_range = {} |
| 326 |
if '-' in address: |
| 327 |
address_range['end'] = address.split('-')[1] |
| 328 |
address_range['start'] = address.split('-')[0] |
| 329 |
else: |
| 330 |
address_range['end'] = address |
| 331 |
address_range['start'] = address |
| 332 |
address_info.append(address_range) |
| 333 |
return address_info |
| 334 |
|
| 335 |
def apply(self): |
| 336 |
""" |
| 337 |
check create/delete/modify operations if needed. |
| 338 |
:return: None. |
| 339 |
""" |
| 340 |
current = self.get_initiator() |
| 341 |
action = self.na_helper.get_cd_action(current, self.parameters) |
| 342 |
modify = self.na_helper.get_modified_attributes(current, self.parameters) |
| 343 |
if self.na_helper.changed and not self.module.check_mode: |
| 344 |
if action == 'create': |
| 345 |
self.create_initiator() |
| 346 |
elif action == 'delete': |
| 347 |
self.delete_initiator() |
| 348 |
elif modify: |
| 349 |
self.modify_initiator(modify, current) |
| 350 |
result = netapp_utils.generate_result(self.na_helper.changed, action, modify) |
| 351 |
self.module.exit_json(**result) |
| 352 |
|
| 353 |
def get_svm_uuid(self): |
| 354 |
""" |
| 355 |
Get a svm's UUID |
| 356 |
:return: uuid of the svm. |
| 357 |
""" |
| 358 |
params = {'fields': 'uuid', 'name': self.parameters['vserver']} |
| 359 |
api = "svm/svms" |
| 360 |
message, error = self.rest_api.get(api, params) |
| 361 |
record, error = rrh.check_for_0_or_1_records(api, message, error) |
| 362 |
if error is not None: |
| 363 |
self.module.fail_json(msg="Error on fetching svm uuid: %s" % error) |
| 364 |
if record is None: |
| 365 |
self.module.fail_json(msg="Error on fetching svm uuid, SVM not found: %s" % self.parameters['vserver']) |
| 366 |
return message['records'][0]['uuid'] |
| 367 |
|
| 368 |
|
| 369 |
def main(): |
| 370 |
"""Execute action""" |
| 371 |
iscsi_obj = NetAppONTAPIscsiSecurity() |
| 372 |
iscsi_obj.apply() |
| 373 |
|
| 374 |
|
| 375 |
if __name__ == '__main__': |
| 376 |
main() |