| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
DOCUMENTATION = r''' |
| 11 |
--- |
| 12 |
module: bigip_remote_role |
| 13 |
short_description: Manage remote roles on a BIG-IP |
| 14 |
description: |
| 15 |
- Manages remote roles on a BIG-IP system. Remote roles are used in situations where |
| 16 |
user authentication is handled off-box. Local access control to the BIG-IP |
| 17 |
is controlled by the defined remote role, and authentication (and by |
| 18 |
extension, assignment to the role) is handled off-box. |
| 19 |
version_added: "1.0.0" |
| 20 |
options: |
| 21 |
name: |
| 22 |
description: |
| 23 |
- Specifies the name of the remote role. |
| 24 |
type: str |
| 25 |
required: True |
| 26 |
line_order: |
| 27 |
description: |
| 28 |
- Specifies the order of the line in the file C(/config/bigip/auth/remoterole). |
| 29 |
- The LDAP and Active Directory servers read this file line by line. |
| 30 |
- The order of the information is important; therefore, F5 recommends |
| 31 |
you set the first line at 1000. This allows you to insert |
| 32 |
lines before the first line in the future. |
| 33 |
- When creating a new remote role, this parameter is required. |
| 34 |
type: int |
| 35 |
attribute_string: |
| 36 |
description: |
| 37 |
- Specifies the user account attributes saved in the group, in the format |
| 38 |
C(cn=, ou=, dc=). |
| 39 |
- When creating a new remote role, this parameter is required. |
| 40 |
type: str |
| 41 |
remote_access: |
| 42 |
description: |
| 43 |
- Enables or disables remote access for the specified group of remotely |
| 44 |
authenticated users. |
| 45 |
- When creating a new remote role, if this parameter is not specified, the default |
| 46 |
is C(true). |
| 47 |
type: bool |
| 48 |
assigned_role: |
| 49 |
description: |
| 50 |
- Specifies the authorization (level of access) for the account. |
| 51 |
- When creating a new remote role, if this parameter is not provided, the |
| 52 |
default is C(none). |
| 53 |
- The C(partition_access) parameter controls which partitions the account can |
| 54 |
access. |
| 55 |
- The role you choose may affect the partitions that one is allowed to specify. |
| 56 |
Specifically, roles such as C(administrator), C(auditor) and C(resource-administrator) |
| 57 |
require a C(partition_access) of C(all). |
| 58 |
- A set of pre-existing roles ship with the system. They are C(none), C(guest), |
| 59 |
C(operator), C(application-editor), C(manager), C(certificate-manager), |
| 60 |
C(irule-manager), C(user-manager), C(resource-administrator), C(auditor), |
| 61 |
C(administrator), and C(firewall-manager). |
| 62 |
type: str |
| 63 |
partition_access: |
| 64 |
description: |
| 65 |
- Specifies the accessible partitions for the account. |
| 66 |
- This parameter supports the reserved names C(all) and C(Common), as well as |
| 67 |
specific partitions a user may access. |
| 68 |
- Users who have access to a partition can operate on objects in that partition, |
| 69 |
as determined by the permissions conferred by the user's C(assigned_role). |
| 70 |
- When creating a new remote role, if this parameter is not specified, the default |
| 71 |
is C(all). |
| 72 |
type: str |
| 73 |
terminal_access: |
| 74 |
description: |
| 75 |
- Specifies terminal-based accessibility for remote accounts not already |
| 76 |
explicitly assigned a user role. |
| 77 |
- Common values for this include C(tmsh) and C(none), but you can also |
| 78 |
specify custom values. |
| 79 |
- When creating a new remote role, if this parameter is not specified, the default |
| 80 |
is C(none). |
| 81 |
type: str |
| 82 |
state: |
| 83 |
description: |
| 84 |
- When C(present), guarantees the remote role exists. |
| 85 |
- When C(absent), removes the remote role from the system. |
| 86 |
type: str |
| 87 |
choices: |
| 88 |
- absent |
| 89 |
- present |
| 90 |
default: present |
| 91 |
extends_documentation_fragment: f5networks.f5_modules.f5 |
| 92 |
author: |
| 93 |
- Tim Rupp (@caphrim007) |
| 94 |
''' |
| 95 |
|
| 96 |
EXAMPLES = r''' |
| 97 |
- name: Create a remote role |
| 98 |
bigip_remote_role: |
| 99 |
name: ldap_group |
| 100 |
line_order: 1 |
| 101 |
attribute_string: memberOf=cn=ldap_group,cn=ldap.group,ou=ldap |
| 102 |
remote_access: true |
| 103 |
assigned_role: administrator |
| 104 |
partition_access: all |
| 105 |
terminal_access: none |
| 106 |
state: present |
| 107 |
provider: |
| 108 |
password: secret |
| 109 |
server: lb.mydomain.com |
| 110 |
user: admin |
| 111 |
delegate_to: localhost |
| 112 |
''' |
| 113 |
|
| 114 |
RETURN = r''' |
| 115 |
attribute_string: |
| 116 |
description: The new attribute string of the resource. |
| 117 |
returned: changed |
| 118 |
type: str |
| 119 |
sample: "memberOf=cn=ldap_group,cn=ldap.group,ou=ldap" |
| 120 |
terminal_access: |
| 121 |
description: The terminal setting of the remote role. |
| 122 |
returned: changed |
| 123 |
type: str |
| 124 |
sample: tmsh |
| 125 |
line_order: |
| 126 |
description: Order of the remote role for LDAP and Active Directory servers. |
| 127 |
returned: changed |
| 128 |
type: int |
| 129 |
sample: 1000 |
| 130 |
assigned_role: |
| 131 |
description: System role this remote role is associated with. |
| 132 |
returned: changed |
| 133 |
type: str |
| 134 |
sample: administrator |
| 135 |
partition_access: |
| 136 |
description: Partition the role has access to. |
| 137 |
returned: changed |
| 138 |
type: str |
| 139 |
sample: all |
| 140 |
remote_access: |
| 141 |
description: Whether remote access is allowed or not. |
| 142 |
returned: changed |
| 143 |
type: bool |
| 144 |
sample: false |
| 145 |
''' |
| 146 |
from datetime import datetime |
| 147 |
|
| 148 |
from ansible.module_utils.basic import AnsibleModule |
| 149 |
from ansible.module_utils.six import iteritems |
| 150 |
|
| 151 |
from ..module_utils.bigip import F5RestClient |
| 152 |
from ..module_utils.common import ( |
| 153 |
F5ModuleError, AnsibleF5Parameters, transform_name, f5_argument_spec, flatten_boolean |
| 154 |
) |
| 155 |
from ..module_utils.icontrol import tmos_version |
| 156 |
from ..module_utils.teem import send_teem |
| 157 |
|
| 158 |
|
| 159 |
class Parameters(AnsibleF5Parameters): |
| 160 |
api_map = { |
| 161 |
'attribute': 'attribute_string', |
| 162 |
'console': 'terminal_access', |
| 163 |
'lineOrder': 'line_order', |
| 164 |
'role': 'assigned_role', |
| 165 |
'userPartition': 'partition_access', |
| 166 |
'deny': 'remote_access' |
| 167 |
} |
| 168 |
|
| 169 |
api_attributes = [ |
| 170 |
'attribute', |
| 171 |
'console', |
| 172 |
'lineOrder', |
| 173 |
'role', |
| 174 |
'deny', |
| 175 |
'userPartition', |
| 176 |
] |
| 177 |
|
| 178 |
returnables = [ |
| 179 |
'attribute_string', |
| 180 |
'terminal_access', |
| 181 |
'line_order', |
| 182 |
'assigned_role', |
| 183 |
'partition_access', |
| 184 |
'remote_access', |
| 185 |
] |
| 186 |
|
| 187 |
updatables = [ |
| 188 |
'attribute_string', |
| 189 |
'terminal_access', |
| 190 |
'line_order', |
| 191 |
'assigned_role', |
| 192 |
'partition_access', |
| 193 |
'remote_access', |
| 194 |
] |
| 195 |
|
| 196 |
role_map = { |
| 197 |
'application-editor': 'applicationeditor', |
| 198 |
'none': 'noaccess', |
| 199 |
'certificate-manager': 'certificatemanager', |
| 200 |
'irule-manager': 'irulemanager', |
| 201 |
'user-manager': 'usermanager', |
| 202 |
'resource-administrator': 'resourceadmin', |
| 203 |
'firewall-manager': 'firewallmanager' |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
class ApiParameters(Parameters): |
| 208 |
pass |
| 209 |
|
| 210 |
|
| 211 |
class ModuleParameters(Parameters): |
| 212 |
@property |
| 213 |
def partition(self): |
| 214 |
return 'Common' |
| 215 |
|
| 216 |
@property |
| 217 |
def assigned_role(self): |
| 218 |
if self._values['assigned_role'] is None: |
| 219 |
return None |
| 220 |
return self.role_map.get(self._values['assigned_role'], self._values['assigned_role']) |
| 221 |
|
| 222 |
@property |
| 223 |
def terminal_access(self): |
| 224 |
if self._values['terminal_access'] in [None, 'tmsh']: |
| 225 |
return self._values['terminal_access'] |
| 226 |
elif self._values['terminal_access'] == 'none': |
| 227 |
return 'disable' |
| 228 |
return self._values['terminal_access'] |
| 229 |
|
| 230 |
@property |
| 231 |
def partition_access(self): |
| 232 |
if self._values['partition_access'] is None: |
| 233 |
return None |
| 234 |
if self._values['partition_access'] == 'all': |
| 235 |
return 'All' |
| 236 |
return self._values['partition_access'] |
| 237 |
|
| 238 |
@property |
| 239 |
def remote_access(self): |
| 240 |
result = flatten_boolean(self._values['remote_access']) |
| 241 |
if result == 'yes': |
| 242 |
return 'disabled' |
| 243 |
elif result == 'no': |
| 244 |
return 'enabled' |
| 245 |
|
| 246 |
|
| 247 |
class Changes(Parameters): |
| 248 |
def to_return(self): |
| 249 |
result = {} |
| 250 |
try: |
| 251 |
for returnable in self.returnables: |
| 252 |
result[returnable] = getattr(self, returnable) |
| 253 |
result = self._filter_params(result) |
| 254 |
except Exception: |
| 255 |
pass |
| 256 |
return result |
| 257 |
|
| 258 |
|
| 259 |
class UsableChanges(Changes): |
| 260 |
pass |
| 261 |
|
| 262 |
|
| 263 |
class ReportableChanges(Changes): |
| 264 |
@property |
| 265 |
def assigned_role(self): |
| 266 |
if self._values['assigned_role'] is None: |
| 267 |
return None |
| 268 |
rmap = dict((v, k) for k, v in iteritems(self.role_map)) |
| 269 |
return rmap.get(self._values['assigned_role'], self._values['assigned_role']) |
| 270 |
|
| 271 |
@property |
| 272 |
def terminal_access(self): |
| 273 |
if self._values['terminal_access'] in [None, 'tmsh']: |
| 274 |
return self._values['terminal_access'] |
| 275 |
elif self._values['terminal_access'] == 'disabled': |
| 276 |
return 'none' |
| 277 |
return self._values['terminal_access'] |
| 278 |
|
| 279 |
|
| 280 |
class Difference(object): |
| 281 |
def __init__(self, want, have=None): |
| 282 |
self.want = want |
| 283 |
self.have = have |
| 284 |
|
| 285 |
def compare(self, param): |
| 286 |
try: |
| 287 |
result = getattr(self, param) |
| 288 |
return result |
| 289 |
except AttributeError: |
| 290 |
return self.__default(param) |
| 291 |
|
| 292 |
def __default(self, param): |
| 293 |
attr1 = getattr(self.want, param) |
| 294 |
try: |
| 295 |
attr2 = getattr(self.have, param) |
| 296 |
if attr1 != attr2: |
| 297 |
return attr1 |
| 298 |
except AttributeError: |
| 299 |
return attr1 |
| 300 |
|
| 301 |
|
| 302 |
class ModuleManager(object): |
| 303 |
def __init__(self, *args, **kwargs): |
| 304 |
self.module = kwargs.get('module', None) |
| 305 |
self.client = F5RestClient(**self.module.params) |
| 306 |
self.want = ModuleParameters(params=self.module.params) |
| 307 |
self.have = ApiParameters() |
| 308 |
self.changes = UsableChanges() |
| 309 |
|
| 310 |
def _set_changed_options(self): |
| 311 |
changed = {} |
| 312 |
for key in Parameters.returnables: |
| 313 |
if getattr(self.want, key) is not None: |
| 314 |
changed[key] = getattr(self.want, key) |
| 315 |
if changed: |
| 316 |
self.changes = UsableChanges(params=changed) |
| 317 |
|
| 318 |
def _update_changed_options(self): |
| 319 |
diff = Difference(self.want, self.have) |
| 320 |
updatables = Parameters.updatables |
| 321 |
changed = dict() |
| 322 |
for k in updatables: |
| 323 |
change = diff.compare(k) |
| 324 |
if change is None: |
| 325 |
continue |
| 326 |
else: |
| 327 |
if isinstance(change, dict): |
| 328 |
changed.update(change) |
| 329 |
else: |
| 330 |
changed[k] = change |
| 331 |
if changed: |
| 332 |
self.changes = UsableChanges(params=changed) |
| 333 |
return True |
| 334 |
return False |
| 335 |
|
| 336 |
def should_update(self): |
| 337 |
result = self._update_changed_options() |
| 338 |
if result: |
| 339 |
return True |
| 340 |
return False |
| 341 |
|
| 342 |
def exec_module(self): |
| 343 |
start = datetime.now().isoformat() |
| 344 |
version = tmos_version(self.client) |
| 345 |
changed = False |
| 346 |
result = dict() |
| 347 |
state = self.want.state |
| 348 |
|
| 349 |
if state == "present": |
| 350 |
changed = self.present() |
| 351 |
elif state == "absent": |
| 352 |
changed = self.absent() |
| 353 |
|
| 354 |
reportable = ReportableChanges(params=self.changes.to_return()) |
| 355 |
changes = reportable.to_return() |
| 356 |
result.update(**changes) |
| 357 |
result.update(dict(changed=changed)) |
| 358 |
self._announce_deprecations(result) |
| 359 |
send_teem(start, self.client, self.module, version) |
| 360 |
return result |
| 361 |
|
| 362 |
def _announce_deprecations(self, result): |
| 363 |
warnings = result.pop('__warnings', []) |
| 364 |
for warning in warnings: |
| 365 |
self.client.module.deprecate( |
| 366 |
msg=warning['msg'], |
| 367 |
version=warning['version'] |
| 368 |
) |
| 369 |
|
| 370 |
def present(self): |
| 371 |
if self.exists(): |
| 372 |
return self.update() |
| 373 |
else: |
| 374 |
return self.create() |
| 375 |
|
| 376 |
def exists(self): |
| 377 |
uri = "https://{0}:{1}/mgmt/tm/auth/remote-role/role-info/{2}".format( |
| 378 |
self.client.provider['server'], |
| 379 |
self.client.provider['server_port'], |
| 380 |
transform_name(self.want.partition, self.want.name) |
| 381 |
) |
| 382 |
resp = self.client.api.get(uri) |
| 383 |
try: |
| 384 |
response = resp.json() |
| 385 |
except ValueError as ex: |
| 386 |
raise F5ModuleError(str(ex)) |
| 387 |
|
| 388 |
if resp.status == 404 or 'code' in response and response['code'] == 404: |
| 389 |
return False |
| 390 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 391 |
return True |
| 392 |
|
| 393 |
errors = [401, 403, 409, 500, 501, 502, 503, 504] |
| 394 |
|
| 395 |
if resp.status in errors or 'code' in response and response['code'] in errors: |
| 396 |
if 'message' in response: |
| 397 |
raise F5ModuleError(response['message']) |
| 398 |
else: |
| 399 |
raise F5ModuleError(resp.content) |
| 400 |
|
| 401 |
def update(self): |
| 402 |
self.have = self.read_current_from_device() |
| 403 |
if not self.should_update(): |
| 404 |
return False |
| 405 |
if self.module.check_mode: |
| 406 |
return True |
| 407 |
self.update_on_device() |
| 408 |
return True |
| 409 |
|
| 410 |
def remove(self): |
| 411 |
if self.module.check_mode: |
| 412 |
return True |
| 413 |
self.remove_from_device() |
| 414 |
if self.exists(): |
| 415 |
raise F5ModuleError("Failed to delete the resource.") |
| 416 |
return True |
| 417 |
|
| 418 |
def create(self): |
| 419 |
if self.want.partition_access is None: |
| 420 |
self.want.update({'partition_access': 'all'}) |
| 421 |
if self.want.remote_access is None: |
| 422 |
self.want.update({'remote_access': True}) |
| 423 |
if self.want.assigned_role is None: |
| 424 |
self.want.update({'assigned_role': 'none'}) |
| 425 |
if self.want.terminal_access is None: |
| 426 |
self.want.update({'terminal_access': 'none'}) |
| 427 |
self._set_changed_options() |
| 428 |
if self.module.check_mode: |
| 429 |
return True |
| 430 |
self.create_on_device() |
| 431 |
return True |
| 432 |
|
| 433 |
def create_on_device(self): |
| 434 |
params = self.changes.api_params() |
| 435 |
params['name'] = self.want.name |
| 436 |
uri = "https://{0}:{1}/mgmt/tm/auth/remote-role/role-info/".format( |
| 437 |
self.client.provider['server'], |
| 438 |
self.client.provider['server_port'] |
| 439 |
) |
| 440 |
resp = self.client.api.post(uri, json=params) |
| 441 |
try: |
| 442 |
response = resp.json() |
| 443 |
except ValueError as ex: |
| 444 |
raise F5ModuleError(str(ex)) |
| 445 |
|
| 446 |
if 'code' in response and response['code'] in [400, 403]: |
| 447 |
if 'message' in response: |
| 448 |
raise F5ModuleError(response['message']) |
| 449 |
else: |
| 450 |
raise F5ModuleError(resp.content) |
| 451 |
|
| 452 |
def update_on_device(self): |
| 453 |
params = self.changes.api_params() |
| 454 |
uri = "https://{0}:{1}/mgmt/tm/auth/remote-role/role-info/{2}".format( |
| 455 |
self.client.provider['server'], |
| 456 |
self.client.provider['server_port'], |
| 457 |
transform_name(self.want.partition, self.want.name) |
| 458 |
) |
| 459 |
resp = self.client.api.patch(uri, json=params) |
| 460 |
try: |
| 461 |
response = resp.json() |
| 462 |
except ValueError as ex: |
| 463 |
raise F5ModuleError(str(ex)) |
| 464 |
|
| 465 |
if 'code' in response and response['code'] == 400: |
| 466 |
if 'message' in response: |
| 467 |
if 'Once configured [All] partition, remote user group cannot' in response['message']: |
| 468 |
raise F5ModuleError( |
| 469 |
"The specified 'attribute_string' is already used in the 'all' partition." |
| 470 |
) |
| 471 |
raise F5ModuleError(response['message']) |
| 472 |
else: |
| 473 |
raise F5ModuleError(resp.content) |
| 474 |
|
| 475 |
def absent(self): |
| 476 |
if self.exists(): |
| 477 |
return self.remove() |
| 478 |
return False |
| 479 |
|
| 480 |
def remove_from_device(self): |
| 481 |
uri = "https://{0}:{1}/mgmt/tm/auth/remote-role/role-info/{2}".format( |
| 482 |
self.client.provider['server'], |
| 483 |
self.client.provider['server_port'], |
| 484 |
transform_name(self.want.partition, self.want.name) |
| 485 |
) |
| 486 |
response = self.client.api.delete(uri) |
| 487 |
if response.status == 200: |
| 488 |
return True |
| 489 |
if 'code' in response and response['code'] == 400: |
| 490 |
if 'message' in response: |
| 491 |
raise F5ModuleError(response['message']) |
| 492 |
else: |
| 493 |
raise F5ModuleError(response.content) |
| 494 |
|
| 495 |
def read_current_from_device(self): |
| 496 |
uri = "https://{0}:{1}/mgmt/tm/auth/remote-role/role-info/{2}".format( |
| 497 |
self.client.provider['server'], |
| 498 |
self.client.provider['server_port'], |
| 499 |
transform_name(self.want.partition, self.want.name) |
| 500 |
) |
| 501 |
resp = self.client.api.get(uri) |
| 502 |
try: |
| 503 |
response = resp.json() |
| 504 |
except ValueError as ex: |
| 505 |
raise F5ModuleError(str(ex)) |
| 506 |
|
| 507 |
if 'code' in response and response['code'] == 400: |
| 508 |
if 'message' in response: |
| 509 |
raise F5ModuleError(response['message']) |
| 510 |
else: |
| 511 |
raise F5ModuleError(resp.content) |
| 512 |
return ApiParameters(params=response) |
| 513 |
|
| 514 |
|
| 515 |
class ArgumentSpec(object): |
| 516 |
def __init__(self): |
| 517 |
self.supports_check_mode = True |
| 518 |
argument_spec = dict( |
| 519 |
name=dict(required=True), |
| 520 |
line_order=dict(type='int'), |
| 521 |
attribute_string=dict(), |
| 522 |
remote_access=dict(type='bool'), |
| 523 |
assigned_role=dict(), |
| 524 |
partition_access=dict(), |
| 525 |
terminal_access=dict(), |
| 526 |
state=dict( |
| 527 |
default='present', |
| 528 |
choices=['present', 'absent'] |
| 529 |
), |
| 530 |
) |
| 531 |
self.argument_spec = {} |
| 532 |
self.argument_spec.update(f5_argument_spec) |
| 533 |
self.argument_spec.update(argument_spec) |
| 534 |
|
| 535 |
|
| 536 |
def main(): |
| 537 |
spec = ArgumentSpec() |
| 538 |
|
| 539 |
module = AnsibleModule( |
| 540 |
argument_spec=spec.argument_spec, |
| 541 |
supports_check_mode=spec.supports_check_mode, |
| 542 |
) |
| 543 |
|
| 544 |
try: |
| 545 |
mm = ModuleManager(module=module) |
| 546 |
results = mm.exec_module() |
| 547 |
module.exit_json(**results) |
| 548 |
except F5ModuleError as ex: |
| 549 |
module.fail_json(msg=str(ex)) |
| 550 |
|
| 551 |
|
| 552 |
if __name__ == '__main__': |
| 553 |
main() |