| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# (c) 2021-2025, NetApp Inc |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
"""NetApp StorageGRID - Manage Grid Identity Federation""" |
| 7 |
|
| 8 |
from __future__ import absolute_import, division, print_function |
| 9 |
|
| 10 |
__metaclass__ = type |
| 11 |
|
| 12 |
|
| 13 |
DOCUMENTATION = """ |
| 14 |
module: na_sg_grid_identity_federation |
| 15 |
short_description: NetApp StorageGRID manage Grid identity federation. |
| 16 |
extends_documentation_fragment: |
| 17 |
- netapp.storagegrid.netapp.sg |
| 18 |
version_added: '21.6.0' |
| 19 |
author: NetApp Ansible Team (@joshedmonds) <[email protected]> |
| 20 |
description: |
| 21 |
- Configure Grid Identity Federation within NetApp StorageGRID. |
| 22 |
- If module is run with I(check_mode), a connectivity test will be performed using the supplied values without changing the configuration. |
| 23 |
- This module is idempotent if I(password) is not specified. |
| 24 |
options: |
| 25 |
state: |
| 26 |
description: |
| 27 |
- Whether identity federation should be enabled or not. |
| 28 |
type: str |
| 29 |
choices: ['present', 'absent'] |
| 30 |
default: present |
| 31 |
username: |
| 32 |
description: |
| 33 |
- The username to bind to the LDAP server. |
| 34 |
type: str |
| 35 |
password: |
| 36 |
description: |
| 37 |
- The password associated with the username. |
| 38 |
type: str |
| 39 |
hostname: |
| 40 |
description: |
| 41 |
- The hostname or IP address of the LDAP server. |
| 42 |
type: str |
| 43 |
port: |
| 44 |
description: |
| 45 |
- The port used to connect to the LDAP server. Typically 389 for LDAP, or 636 for LDAPS. |
| 46 |
type: int |
| 47 |
base_group_dn: |
| 48 |
description: |
| 49 |
- The Distinguished Name of the LDAP subtree to search for groups. |
| 50 |
type: str |
| 51 |
base_user_dn: |
| 52 |
description: |
| 53 |
- The Distinguished Name of the LDAP subtree to search for users. |
| 54 |
type: str |
| 55 |
ldap_service_type: |
| 56 |
description: |
| 57 |
- The type of LDAP server. |
| 58 |
choices: ['Active Directory', 'OpenLDAP', 'Other'] |
| 59 |
type: str |
| 60 |
type: |
| 61 |
description: |
| 62 |
- The type of identity source. |
| 63 |
- Default is C(ldap). |
| 64 |
type: str |
| 65 |
default: ldap |
| 66 |
ldap_user_id_attribute: |
| 67 |
description: |
| 68 |
- The LDAP attribute which contains the unique user name of a user. |
| 69 |
- Should be configured if I(ldap_service_type=Other). |
| 70 |
type: str |
| 71 |
ldap_user_uuid_attribute: |
| 72 |
description: |
| 73 |
- The LDAP attribute which contains the permanent unique identity of a user. |
| 74 |
- Should be configured if I(ldap_service_type=Other). |
| 75 |
type: str |
| 76 |
ldap_group_id_attribute: |
| 77 |
description: |
| 78 |
- The LDAP attribute which contains the group for a user. |
| 79 |
- Should be configured if I(ldap_service_type=Other). |
| 80 |
type: str |
| 81 |
ldap_group_uuid_attribute: |
| 82 |
description: |
| 83 |
- The LDAP attribute which contains the group's permanent unique identity. |
| 84 |
- Should be configured if I(ldap_service_type=Other). |
| 85 |
type: str |
| 86 |
tls: |
| 87 |
description: |
| 88 |
- Whether Transport Layer Security is used to connect to the LDAP server. |
| 89 |
choices: ['STARTTLS', 'LDAPS', 'Disabled'] |
| 90 |
type: str |
| 91 |
default: STARTTLS |
| 92 |
ca_cert: |
| 93 |
description: |
| 94 |
- Custom certificate used to connect to the LDAP server. |
| 95 |
- If a custom certificate is not supplied, the operating system CA certificate will be used. |
| 96 |
type: str |
| 97 |
""" |
| 98 |
|
| 99 |
EXAMPLES = """ |
| 100 |
- name: test identity federation configuration |
| 101 |
netapp.storagegrid.na_sg_grid_identity_federation: |
| 102 |
api_url: "https://<storagegrid-endpoint-url>" |
| 103 |
auth_token: "storagegrid-auth-token" |
| 104 |
validate_certs: false |
| 105 |
state: present |
| 106 |
ldap_service_type: "Active Directory" |
| 107 |
hostname: "ad.example.com" |
| 108 |
port: 389 |
| 109 |
username: "binduser" |
| 110 |
password: "bindpass" |
| 111 |
base_group_dn: "DC=example,DC=com" |
| 112 |
base_user_dn: "DC=example,DC=com" |
| 113 |
tls: "Disabled" |
| 114 |
check_mode: true |
| 115 |
|
| 116 |
- name: configure identity federation with AD and TLS |
| 117 |
netapp.storagegrid.na_sg_grid_identity_federation: |
| 118 |
api_url: "https://<storagegrid-endpoint-url>" |
| 119 |
auth_token: "storagegrid-auth-token" |
| 120 |
validate_certs: false |
| 121 |
state: present |
| 122 |
ldap_service_type: "Active Directory" |
| 123 |
hostname: "ad.example.com" |
| 124 |
port: 636 |
| 125 |
username: "binduser" |
| 126 |
password: "bindpass" |
| 127 |
base_group_dn: "DC=example,DC=com" |
| 128 |
base_user_dn: "DC=example,DC=com" |
| 129 |
tls: "LDAPS" |
| 130 |
ca_cert: | |
| 131 |
-----BEGIN CERTIFICATE----- |
| 132 |
MIIC+jCCAeICCQDmn9Gow08LTzANBgkqhkiG9w0BAQsFADA/..swCQYDVQQGEwJV |
| 133 |
bXBsZTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB..JFzNIXQEGnsgjV |
| 134 |
JGU4giuvOLOZ8Q3gyuUbkSUQDjmjpMR8PliwJ6iW2Ity89Dv..dl1TaIYI/ansyZ |
| 135 |
Uxk4YXeN6kUkrDtNxCg1McALzXVAfxMTtj2SFlLxne4Z6rX2..UyftQrfM13F1vY |
| 136 |
gK8dBPz+l+X/Uozo/xNm7gxe68p9le9/pcULst1CQn5/sPqq..kgWcSvlKUItu82 |
| 137 |
lq3B2169rovdIaNdcvaQjMPhrDGo5rvLfMN35U3Hgbz41PL5..x2BcUE6/0ab5T4 |
| 138 |
qKBxKa3t9twj+zpUqOzyL0PFfCE+SK5fEXAS1ow4eAcLN+eB..gR/PuvGAyIPCtE |
| 139 |
1+X4GrECAwEAATANBgkqhkiG9w0BAQsFAAOCAQEAFpO+04Ra..FMJPH6dBmzfb7l |
| 140 |
k04BWTvSlur6HiQdXY+oFQMJZzyI7MQ8v9HBIzS0ZAzYWLp4..VZhHmRxnrWyxVs |
| 141 |
u783V5YfQH2L4QnBDoiDefgxyfDs2PcoF5C+X9CGXmPqzst2..y/6tdOVJzdiA== |
| 142 |
-----END CERTIFICATE----- |
| 143 |
""" |
| 144 |
|
| 145 |
RETURN = """ |
| 146 |
resp: |
| 147 |
description: Returns information about the StorageGRID management identity source configuration. |
| 148 |
returned: success |
| 149 |
type: dict |
| 150 |
sample: { |
| 151 |
"id": "00000000-0000-0000-0000-000000000000", |
| 152 |
"disable": false, |
| 153 |
"hostname": "10.1.2.3", |
| 154 |
"port": 389, |
| 155 |
"username": "MYDOMAIN\\\\Administrator", |
| 156 |
"password": "********", |
| 157 |
"baseGroupDn": "DC=example,DC=com", |
| 158 |
"baseUserDn": "DC=example,DC=com", |
| 159 |
"ldapServiceType": "Active Directory", |
| 160 |
"type": "ldap", |
| 161 |
"disableTLS": false, |
| 162 |
"enableLDAPS": false, |
| 163 |
"caCert": "-----BEGIN CERTIFICATE----- abcdefghijkl123456780ABCDEFGHIJKL 123456/7890ABCDEFabcdefghijklABCD -----END CERTIFICATE-----\n" |
| 164 |
} |
| 165 |
""" |
| 166 |
|
| 167 |
import ansible_collections.netapp.storagegrid.plugins.module_utils.netapp as netapp_utils |
| 168 |
from ansible.module_utils.basic import AnsibleModule |
| 169 |
from ansible_collections.netapp.storagegrid.plugins.module_utils.netapp_module import NetAppModule |
| 170 |
from ansible_collections.netapp.storagegrid.plugins.module_utils.netapp import SGRestAPI |
| 171 |
|
| 172 |
|
| 173 |
class SgGridIdentityFederation: |
| 174 |
""" |
| 175 |
Configure and modify StorageGRID Grid Identity Federation |
| 176 |
""" |
| 177 |
|
| 178 |
def __init__(self): |
| 179 |
""" |
| 180 |
Parse arguments, setup state variables, |
| 181 |
check parameters and ensure request module is installed |
| 182 |
""" |
| 183 |
self.argument_spec = netapp_utils.na_storagegrid_host_argument_spec() |
| 184 |
self.argument_spec.update( |
| 185 |
dict( |
| 186 |
state=dict(required=False, type="str", choices=["present", "absent"], default="present"), |
| 187 |
username=dict(required=False, type="str"), |
| 188 |
password=dict(required=False, type="str", no_log=True), |
| 189 |
hostname=dict(required=False, type="str"), |
| 190 |
port=dict(required=False, type="int"), |
| 191 |
base_group_dn=dict(required=False, type="str"), |
| 192 |
base_user_dn=dict(required=False, type="str"), |
| 193 |
ldap_service_type=dict(required=False, type="str", choices=["OpenLDAP", "Active Directory", "Other"]), |
| 194 |
type=dict(required=False, type="str", default="ldap"), |
| 195 |
ldap_user_id_attribute=dict(required=False, type="str"), |
| 196 |
ldap_user_uuid_attribute=dict(required=False, type="str"), |
| 197 |
ldap_group_id_attribute=dict(required=False, type="str"), |
| 198 |
ldap_group_uuid_attribute=dict(required=False, type="str"), |
| 199 |
tls=dict(required=False, type="str", choices=["STARTTLS", "LDAPS", "Disabled"], default="STARTTLS"), |
| 200 |
ca_cert=dict(required=False, type="str"), |
| 201 |
), |
| 202 |
) |
| 203 |
|
| 204 |
parameter_map = { |
| 205 |
"username": "username", |
| 206 |
"password": "password", |
| 207 |
"hostname": "hostname", |
| 208 |
"port": "port", |
| 209 |
"base_group_dn": "baseGroupDn", |
| 210 |
"base_user_dn": "baseUserDn", |
| 211 |
"ldap_service_type": "ldapServiceType", |
| 212 |
"ldap_user_id_attribute": "ldapUserIdAttribute", |
| 213 |
"ldap_user_uuid_attribute": "ldapUserUUIDAttribute", |
| 214 |
"ldap_group_id_attribute": "ldapGroupIdAttribute", |
| 215 |
"ldap_group_uuid_attribute": "ldapGroupUUIDAttribute", |
| 216 |
"ca_cert": "caCert", |
| 217 |
} |
| 218 |
self.module = AnsibleModule( |
| 219 |
argument_spec=self.argument_spec, |
| 220 |
supports_check_mode=True, |
| 221 |
) |
| 222 |
|
| 223 |
self.na_helper = NetAppModule() |
| 224 |
|
| 225 |
# set up state variables |
| 226 |
self.parameters = self.na_helper.set_parameters(self.module.params) |
| 227 |
# Calling generic SG rest_api class |
| 228 |
self.rest_api = SGRestAPI(self.module) |
| 229 |
# Get API version |
| 230 |
self.rest_api.get_sg_product_version() |
| 231 |
self.api_version = self.rest_api.get_api_version() |
| 232 |
|
| 233 |
# Checking for the parameters passed and create new parameters list |
| 234 |
self.data = {} |
| 235 |
|
| 236 |
if self.parameters["state"] == "present": |
| 237 |
self.data["disable"] = False |
| 238 |
|
| 239 |
for k in parameter_map.keys(): |
| 240 |
if self.parameters.get(k) is not None: |
| 241 |
self.data[parameter_map[k]] = self.parameters[k] |
| 242 |
|
| 243 |
if self.parameters.get("tls") == "STARTTLS": |
| 244 |
self.data["disableTLS"] = False |
| 245 |
self.data["enableLDAPS"] = False |
| 246 |
elif self.parameters.get("tls") == "LDAPS": |
| 247 |
self.data["disableTLS"] = False |
| 248 |
self.data["enableLDAPS"] = True |
| 249 |
else: |
| 250 |
self.data["disableTLS"] = True |
| 251 |
self.data["enableLDAPS"] = False |
| 252 |
|
| 253 |
def get_grid_identity_source(self): |
| 254 |
api = "api/%s/grid/identity-source" % self.api_version |
| 255 |
response, error = self.rest_api.get(api) |
| 256 |
|
| 257 |
if error: |
| 258 |
self.module.fail_json(msg=error) |
| 259 |
else: |
| 260 |
return response["data"] |
| 261 |
return None |
| 262 |
|
| 263 |
def update_identity_federation(self, test=False): |
| 264 |
api = "api/%s/grid/identity-source" % self.api_version |
| 265 |
|
| 266 |
params = {} |
| 267 |
|
| 268 |
if test: |
| 269 |
params["test"] = True |
| 270 |
|
| 271 |
response, error = self.rest_api.put(api, self.data, params=params) |
| 272 |
if error: |
| 273 |
self.module.fail_json(msg=error, payload=self.data) |
| 274 |
|
| 275 |
if response: |
| 276 |
if response.get("status_code") == 204: |
| 277 |
return response |
| 278 |
if response.get("status_code") == 200: |
| 279 |
return response["data"] |
| 280 |
else: |
| 281 |
return None |
| 282 |
|
| 283 |
def apply(self): |
| 284 |
""" |
| 285 |
Perform pre-checks, call functions and exit |
| 286 |
""" |
| 287 |
grid_identity_source = self.get_grid_identity_source() |
| 288 |
|
| 289 |
cd_action = self.na_helper.get_cd_action(grid_identity_source, self.parameters) |
| 290 |
|
| 291 |
if cd_action is None and self.parameters["state"] == "present": |
| 292 |
# let's see if we need to update parameters |
| 293 |
update = False |
| 294 |
|
| 295 |
for k in (i for i in self.data.keys() if i != "password"): |
| 296 |
if self.data[k] != grid_identity_source.get(k): |
| 297 |
update = True |
| 298 |
break |
| 299 |
|
| 300 |
# if a password has been specified we need to update it |
| 301 |
if self.data.get("password") and self.parameters["state"] == "present": |
| 302 |
update = True |
| 303 |
self.module.warn("Password attribute has been specified. Task is not idempotent.") |
| 304 |
|
| 305 |
if update: |
| 306 |
self.na_helper.changed = True |
| 307 |
|
| 308 |
if cd_action == "delete": |
| 309 |
# if identity federation is already in a disable state |
| 310 |
if grid_identity_source.get("disable"): |
| 311 |
self.na_helper.changed = False |
| 312 |
|
| 313 |
result_message = "" |
| 314 |
resp_data = grid_identity_source |
| 315 |
|
| 316 |
if self.na_helper.changed and not self.module.check_mode: |
| 317 |
if cd_action == "delete": |
| 318 |
self.data = dict(disable=True) |
| 319 |
resp_data = self.update_identity_federation() |
| 320 |
result_message = "Grid identity federation disabled" |
| 321 |
else: |
| 322 |
resp_data = self.update_identity_federation() |
| 323 |
result_message = "Grid identity federation updated" |
| 324 |
|
| 325 |
if self.module.check_mode: |
| 326 |
self.update_identity_federation(test=True) |
| 327 |
# if no error, connection test successful |
| 328 |
self.module.exit_json(changed=self.na_helper.changed, msg="Connection test successful") |
| 329 |
|
| 330 |
self.module.exit_json(changed=self.na_helper.changed, msg=result_message, resp=resp_data) |
| 331 |
|
| 332 |
|
| 333 |
def main(): |
| 334 |
""" |
| 335 |
Main function |
| 336 |
""" |
| 337 |
na_sg_grid_identity_federation = SgGridIdentityFederation() |
| 338 |
na_sg_grid_identity_federation.apply() |
| 339 |
|
| 340 |
|
| 341 |
if __name__ == "__main__": |
| 342 |
main() |