| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# (c) 2018, NetApp, Inc |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
from __future__ import absolute_import, division, print_function |
| 7 |
|
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
ANSIBLE_METADATA = {'metadata_version': '1.1', |
| 11 |
'status': ['deprecated'], |
| 12 |
'supported_by': 'community'} |
| 13 |
|
| 14 |
DOCUMENTATION = ''' |
| 15 |
--- |
| 16 |
module: netapp_e_ldap |
| 17 |
short_description: NetApp E-Series manage LDAP integration to use for authentication |
| 18 |
description: |
| 19 |
- Configure an E-Series system to allow authentication via an LDAP server |
| 20 |
version_added: '2.7.0' |
| 21 |
author: Michael Price (@lmprice) |
| 22 |
extends_documentation_fragment: |
| 23 |
- netapp_eseries.santricity.santricity.netapp.eseries |
| 24 |
options: |
| 25 |
state: |
| 26 |
description: |
| 27 |
- Enable/disable LDAP support on the system. Disabling will clear out any existing defined domains. |
| 28 |
choices: |
| 29 |
- present |
| 30 |
- absent |
| 31 |
default: present |
| 32 |
type: str |
| 33 |
identifier: |
| 34 |
description: |
| 35 |
- This is a unique identifier for the configuration (for cases where there are multiple domains configured). |
| 36 |
- If this is not specified, but I(state=present), we will utilize a default value of 'default'. |
| 37 |
type: str |
| 38 |
username: |
| 39 |
description: |
| 40 |
- This is the user account that will be used for querying the LDAP server. |
| 41 |
- "Example: CN=MyBindAcct,OU=ServiceAccounts,DC=example,DC=com" |
| 42 |
required: yes |
| 43 |
type: str |
| 44 |
aliases: |
| 45 |
- bind_username |
| 46 |
password: |
| 47 |
description: |
| 48 |
- This is the password for the bind user account. |
| 49 |
required: yes |
| 50 |
type: str |
| 51 |
aliases: |
| 52 |
- bind_password |
| 53 |
attributes: |
| 54 |
description: |
| 55 |
- The user attributes that should be considered for the group to role mapping. |
| 56 |
- Typically this is used with something like 'memberOf', and a user's access is tested against group |
| 57 |
membership or lack thereof. |
| 58 |
default: memberOf |
| 59 |
type: list |
| 60 |
elements: str |
| 61 |
server: |
| 62 |
description: |
| 63 |
- This is the LDAP server url. |
| 64 |
- The connection string should be specified as using the ldap or ldaps protocol along with the port |
| 65 |
information. |
| 66 |
aliases: |
| 67 |
- server_url |
| 68 |
required: yes |
| 69 |
type: str |
| 70 |
name: |
| 71 |
description: |
| 72 |
- The domain name[s] that will be utilized when authenticating to identify which domain to utilize. |
| 73 |
- Default to use the DNS name of the I(server). |
| 74 |
- The only requirement is that the name[s] be resolvable. |
| 75 |
- "Example: [email protected]" |
| 76 |
required: no |
| 77 |
type: list |
| 78 |
elements: str |
| 79 |
search_base: |
| 80 |
description: |
| 81 |
- The search base is used to find group memberships of the user. |
| 82 |
- "Example: ou=users,dc=example,dc=com" |
| 83 |
required: yes |
| 84 |
type: str |
| 85 |
role_mappings: |
| 86 |
description: |
| 87 |
- This is where you specify which groups should have access to what permissions for the |
| 88 |
storage-system. |
| 89 |
- For example, all users in group A will be assigned all 4 available roles, which will allow access |
| 90 |
to all the management functionality of the system (super-user). Those in group B only have the |
| 91 |
storage.monitor role, which will allow only read-only access. |
| 92 |
- This is specified as a mapping of regular expressions to a list of roles. See the examples. |
| 93 |
- The roles that will be assigned to to the group/groups matching the provided regex. |
| 94 |
- storage.admin allows users full read/write access to storage objects and operations. |
| 95 |
- storage.monitor allows users read-only access to storage objects and operations. |
| 96 |
- support.admin allows users access to hardware, diagnostic information, the Major Event |
| 97 |
Log, and other critical support-related functionality, but not the storage configuration. |
| 98 |
- security.admin allows users access to authentication/authorization configuration, as well |
| 99 |
as the audit log configuration, and certification management. |
| 100 |
type: dict |
| 101 |
required: yes |
| 102 |
user_attribute: |
| 103 |
description: |
| 104 |
- This is the attribute we will use to match the provided username when a user attempts to |
| 105 |
authenticate. |
| 106 |
type: str |
| 107 |
default: sAMAccountName |
| 108 |
log_path: |
| 109 |
description: |
| 110 |
- A local path to a file to be used for debug logging |
| 111 |
required: no |
| 112 |
type: str |
| 113 |
notes: |
| 114 |
- Check mode is supported. |
| 115 |
- This module allows you to define one or more LDAP domains identified uniquely by I(identifier) to use for |
| 116 |
authentication. Authorization is determined by I(role_mappings), in that different groups of users may be given |
| 117 |
different (or no), access to certain aspects of the system and API. |
| 118 |
- The local user accounts will still be available if the LDAP server becomes unavailable/inaccessible. |
| 119 |
- Generally, you'll need to get the details of your organization's LDAP server before you'll be able to configure |
| 120 |
the system for using LDAP authentication; every implementation is likely to be very different. |
| 121 |
- This API is currently only supported with the Embedded Web Services API v2.0 and higher, or the Web Services Proxy |
| 122 |
v3.0 and higher. |
| 123 |
''' |
| 124 |
|
| 125 |
EXAMPLES = ''' |
| 126 |
- name: Disable LDAP authentication |
| 127 |
netapp_e_ldap: |
| 128 |
api_url: "10.1.1.1:8443" |
| 129 |
api_username: "admin" |
| 130 |
api_password: "myPass" |
| 131 |
ssid: "1" |
| 132 |
state: absent |
| 133 |
|
| 134 |
- name: Remove the 'default' LDAP domain configuration |
| 135 |
netapp_e_ldap: |
| 136 |
state: absent |
| 137 |
identifier: default |
| 138 |
|
| 139 |
- name: Define a new LDAP domain, utilizing defaults where possible |
| 140 |
netapp_e_ldap: |
| 141 |
state: present |
| 142 |
bind_username: "CN=MyBindAccount,OU=ServiceAccounts,DC=example,DC=com" |
| 143 |
bind_password: "mySecretPass" |
| 144 |
server: "ldap://example.com:389" |
| 145 |
search_base: 'OU=Users,DC=example,DC=com' |
| 146 |
role_mappings: |
| 147 |
".*dist-dev-storage.*": |
| 148 |
- storage.admin |
| 149 |
- security.admin |
| 150 |
- support.admin |
| 151 |
- storage.monitor |
| 152 |
''' |
| 153 |
|
| 154 |
RETURN = """ |
| 155 |
msg: |
| 156 |
description: Success message |
| 157 |
returned: on success |
| 158 |
type: str |
| 159 |
sample: The ldap settings have been updated. |
| 160 |
""" |
| 161 |
|
| 162 |
import json |
| 163 |
import logging |
| 164 |
|
| 165 |
try: |
| 166 |
import urlparse |
| 167 |
except ImportError: |
| 168 |
import urllib.parse as urlparse |
| 169 |
|
| 170 |
from ansible.module_utils.basic import AnsibleModule |
| 171 |
from ansible_collections.netapp_eseries.santricity.plugins.module_utils.netapp import request, eseries_host_argument_spec |
| 172 |
from ansible.module_utils._text import to_native |
| 173 |
|
| 174 |
|
| 175 |
class Ldap(object): |
| 176 |
NO_CHANGE_MSG = "No changes were necessary." |
| 177 |
|
| 178 |
def __init__(self): |
| 179 |
argument_spec = eseries_host_argument_spec() |
| 180 |
argument_spec.update(dict( |
| 181 |
state=dict(type='str', required=False, default='present', |
| 182 |
choices=['present', 'absent']), |
| 183 |
identifier=dict(type='str', required=False, ), |
| 184 |
username=dict(type='str', required=True, aliases=['bind_username']), |
| 185 |
password=dict(type='str', required=True, aliases=['bind_password'], no_log=True), |
| 186 |
name=dict(type='list', elements="str", required=False, ), |
| 187 |
server=dict(type='str', required=True, aliases=['server_url']), |
| 188 |
search_base=dict(type='str', required=True, ), |
| 189 |
role_mappings=dict(type='dict', required=True, ), |
| 190 |
user_attribute=dict(type='str', required=False, default='sAMAccountName'), |
| 191 |
attributes=dict(type='list', elements="str", default=['memberOf'], required=False, ), |
| 192 |
log_path=dict(type='str', required=False), |
| 193 |
)) |
| 194 |
|
| 195 |
required_if = [ |
| 196 |
["state", "present", ["username", "password", "server", "search_base", "role_mappings", ]] |
| 197 |
] |
| 198 |
|
| 199 |
self.module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True, required_if=required_if) |
| 200 |
args = self.module.params |
| 201 |
self.ldap = args['state'] == 'present' |
| 202 |
self.identifier = args['identifier'] |
| 203 |
self.username = args['username'] |
| 204 |
self.password = args['password'] |
| 205 |
self.names = args['name'] |
| 206 |
self.server = args['server'] |
| 207 |
self.search_base = args['search_base'] |
| 208 |
self.role_mappings = args['role_mappings'] |
| 209 |
self.user_attribute = args['user_attribute'] |
| 210 |
self.attributes = args['attributes'] |
| 211 |
|
| 212 |
self.ssid = args['ssid'] |
| 213 |
self.url = args['api_url'] |
| 214 |
self.creds = dict(url_password=args['api_password'], |
| 215 |
validate_certs=args['validate_certs'], |
| 216 |
url_username=args['api_username'], |
| 217 |
timeout=60) |
| 218 |
|
| 219 |
self.check_mode = self.module.check_mode |
| 220 |
|
| 221 |
log_path = args['log_path'] |
| 222 |
|
| 223 |
# logging setup |
| 224 |
self._logger = logging.getLogger(self.__class__.__name__) |
| 225 |
|
| 226 |
if log_path: |
| 227 |
logging.basicConfig( |
| 228 |
level=logging.DEBUG, filename=log_path, filemode='w', |
| 229 |
format='%(relativeCreated)dms %(levelname)s %(module)s.%(funcName)s:%(lineno)d\n %(message)s') |
| 230 |
|
| 231 |
if not self.url.endswith('/'): |
| 232 |
self.url += '/' |
| 233 |
|
| 234 |
self.embedded = None |
| 235 |
self.base_path = None |
| 236 |
|
| 237 |
def make_configuration(self): |
| 238 |
if not self.identifier: |
| 239 |
self.identifier = 'default' |
| 240 |
|
| 241 |
if not self.names: |
| 242 |
parts = urlparse.urlparse(self.server) |
| 243 |
netloc = parts.netloc |
| 244 |
if ':' in netloc: |
| 245 |
netloc = netloc.split(':')[0] |
| 246 |
self.names = [netloc] |
| 247 |
|
| 248 |
roles = list() |
| 249 |
for regex in self.role_mappings: |
| 250 |
for role in self.role_mappings[regex]: |
| 251 |
roles.append(dict(groupRegex=regex, |
| 252 |
ignoreCase=True, |
| 253 |
name=role)) |
| 254 |
|
| 255 |
domain = dict(id=self.identifier, |
| 256 |
ldapUrl=self.server, |
| 257 |
bindLookupUser=dict(user=self.username, password=self.password), |
| 258 |
roleMapCollection=roles, |
| 259 |
groupAttributes=self.attributes, |
| 260 |
names=self.names, |
| 261 |
searchBase=self.search_base, |
| 262 |
userAttribute=self.user_attribute, |
| 263 |
) |
| 264 |
|
| 265 |
return domain |
| 266 |
|
| 267 |
def is_embedded(self): |
| 268 |
"""Determine whether or not we're using the embedded or proxy implementation of Web Services""" |
| 269 |
if self.embedded is None: |
| 270 |
url = self.url |
| 271 |
try: |
| 272 |
parts = urlparse.urlparse(url) |
| 273 |
parts = parts._replace(path='/devmgr/utils/') |
| 274 |
url = urlparse.urlunparse(parts) |
| 275 |
|
| 276 |
(rc, result) = request(url + 'about', **self.creds) |
| 277 |
self.embedded = not result['runningAsProxy'] |
| 278 |
except Exception as err: |
| 279 |
self._logger.exception("Failed to retrieve the About information.") |
| 280 |
self.module.fail_json(msg="Failed to determine the Web Services implementation type!" |
| 281 |
" Array Id [%s]. Error [%s]." |
| 282 |
% (self.ssid, to_native(err))) |
| 283 |
|
| 284 |
return self.embedded |
| 285 |
|
| 286 |
def get_full_configuration(self): |
| 287 |
try: |
| 288 |
(rc, result) = request(self.url + self.base_path, **self.creds) |
| 289 |
return result |
| 290 |
except Exception as err: |
| 291 |
self._logger.exception("Failed to retrieve the LDAP configuration.") |
| 292 |
self.module.fail_json(msg="Failed to retrieve LDAP configuration! Array Id [%s]. Error [%s]." |
| 293 |
% (self.ssid, to_native(err))) |
| 294 |
|
| 295 |
def get_configuration(self, identifier): |
| 296 |
try: |
| 297 |
(rc, result) = request(self.url + self.base_path + '%s' % (identifier), ignore_errors=True, **self.creds) |
| 298 |
if rc == 200: |
| 299 |
return result |
| 300 |
elif rc == 404: |
| 301 |
return None |
| 302 |
else: |
| 303 |
self.module.fail_json(msg="Failed to retrieve LDAP configuration! Array Id [%s]. Error [%s]." |
| 304 |
% (self.ssid, result)) |
| 305 |
except Exception as err: |
| 306 |
self._logger.exception("Failed to retrieve the LDAP configuration.") |
| 307 |
self.module.fail_json(msg="Failed to retrieve LDAP configuration! Array Id [%s]. Error [%s]." |
| 308 |
% (self.ssid, to_native(err))) |
| 309 |
|
| 310 |
def update_configuration(self): |
| 311 |
# Define a new domain based on the user input |
| 312 |
domain = self.make_configuration() |
| 313 |
|
| 314 |
# This is the current list of configurations |
| 315 |
current = self.get_configuration(self.identifier) |
| 316 |
|
| 317 |
update = current != domain |
| 318 |
msg = "No changes were necessary for [%s]." % self.identifier |
| 319 |
self._logger.info("Is updated: %s", update) |
| 320 |
if update and not self.check_mode: |
| 321 |
msg = "The configuration changes were made for [%s]." % self.identifier |
| 322 |
try: |
| 323 |
if current is None: |
| 324 |
api = self.base_path + 'addDomain' |
| 325 |
else: |
| 326 |
api = self.base_path + '%s' % (domain['id']) |
| 327 |
|
| 328 |
(rc, result) = request(self.url + api, method='POST', data=json.dumps(domain), **self.creds) |
| 329 |
except Exception as err: |
| 330 |
self._logger.exception("Failed to modify the LDAP configuration.") |
| 331 |
self.module.fail_json(msg="Failed to modify LDAP configuration! Array Id [%s]. Error [%s]." |
| 332 |
% (self.ssid, to_native(err))) |
| 333 |
|
| 334 |
return msg, update |
| 335 |
|
| 336 |
def clear_single_configuration(self, identifier=None): |
| 337 |
if identifier is None: |
| 338 |
identifier = self.identifier |
| 339 |
|
| 340 |
configuration = self.get_configuration(identifier) |
| 341 |
updated = False |
| 342 |
msg = self.NO_CHANGE_MSG |
| 343 |
if configuration: |
| 344 |
updated = True |
| 345 |
msg = "The LDAP domain configuration for [%s] was cleared." % identifier |
| 346 |
if not self.check_mode: |
| 347 |
try: |
| 348 |
(rc, result) = request(self.url + self.base_path + '%s' % identifier, method='DELETE', **self.creds) |
| 349 |
except Exception as err: |
| 350 |
self.module.fail_json(msg="Failed to remove LDAP configuration! Array Id [%s]. Error [%s]." |
| 351 |
% (self.ssid, to_native(err))) |
| 352 |
return msg, updated |
| 353 |
|
| 354 |
def clear_configuration(self): |
| 355 |
configuration = self.get_full_configuration() |
| 356 |
updated = False |
| 357 |
msg = self.NO_CHANGE_MSG |
| 358 |
if configuration['ldapDomains']: |
| 359 |
updated = True |
| 360 |
msg = "The LDAP configuration for all domains was cleared." |
| 361 |
if not self.check_mode: |
| 362 |
try: |
| 363 |
(rc, result) = request(self.url + self.base_path, method='DELETE', ignore_errors=True, **self.creds) |
| 364 |
|
| 365 |
# Older versions of NetApp E-Series restAPI does not possess an API to remove all existing configs |
| 366 |
if rc == 405: |
| 367 |
for config in configuration['ldapDomains']: |
| 368 |
self.clear_single_configuration(config['id']) |
| 369 |
|
| 370 |
except Exception as err: |
| 371 |
self.module.fail_json(msg="Failed to clear LDAP configuration! Array Id [%s]. Error [%s]." |
| 372 |
% (self.ssid, to_native(err))) |
| 373 |
return msg, updated |
| 374 |
|
| 375 |
def get_base_path(self): |
| 376 |
embedded = self.is_embedded() |
| 377 |
if embedded: |
| 378 |
return 'storage-systems/%s/ldap/' % self.ssid |
| 379 |
else: |
| 380 |
return '/ldap/' |
| 381 |
|
| 382 |
def update(self): |
| 383 |
self.base_path = self.get_base_path() |
| 384 |
|
| 385 |
if self.ldap: |
| 386 |
msg, update = self.update_configuration() |
| 387 |
elif self.identifier: |
| 388 |
msg, update = self.clear_single_configuration() |
| 389 |
else: |
| 390 |
msg, update = self.clear_configuration() |
| 391 |
self.module.exit_json(msg=msg, changed=update, ) |
| 392 |
|
| 393 |
def __call__(self, *args, **kwargs): |
| 394 |
self.update() |
| 395 |
|
| 396 |
|
| 397 |
def main(): |
| 398 |
settings = Ldap() |
| 399 |
settings() |
| 400 |
|
| 401 |
|
| 402 |
if __name__ == '__main__': |
| 403 |
main() |