| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
from __future__ import (absolute_import, division, print_function) |
| 14 |
__metaclass__ = type |
| 15 |
|
| 16 |
DOCUMENTATION = """ |
| 17 |
--- |
| 18 |
module: idrac_user |
| 19 |
short_description: Configure settings for user accounts |
| 20 |
version_added: "2.1.0" |
| 21 |
description: |
| 22 |
- This module allows to perform the following, |
| 23 |
- Add a new user account. |
| 24 |
- Edit a user account. |
| 25 |
- Enable or Disable a user account. |
| 26 |
extends_documentation_fragment: |
| 27 |
- dellemc.openmanage.idrac_x_auth_options |
| 28 |
options: |
| 29 |
state: |
| 30 |
type: str |
| 31 |
description: |
| 32 |
- Select C(present) to create or modify a user account. |
| 33 |
- Select C(absent) to remove a user account. |
| 34 |
choices: [present, absent] |
| 35 |
default: present |
| 36 |
user_name: |
| 37 |
type: str |
| 38 |
required: true |
| 39 |
description: Provide the I(user_name) of the account to be created, deleted or modified. |
| 40 |
user_password: |
| 41 |
type: str |
| 42 |
description: |
| 43 |
- Provide the password for the user account. The password can be changed when the user account is modified. |
| 44 |
- To ensure security, the I(user_password) must be at least eight characters long and must contain |
| 45 |
lowercase and upper-case characters, numbers, and special characters. |
| 46 |
new_user_name: |
| 47 |
type: str |
| 48 |
description: Provide the I(user_name) for the account to be modified. |
| 49 |
privilege: |
| 50 |
type: str |
| 51 |
description: |
| 52 |
- Following are the role-based privileges. |
| 53 |
- A user with C(Administrator) privilege can log in to iDRAC, and then configure iDRAC, configure users, |
| 54 |
clear logs, control and configure system, access virtual console, access virtual media, test alerts, |
| 55 |
and execute debug commands. |
| 56 |
- A user with C(Operator) privilege can log in to iDRAC, and then configure iDRAC, control and configure system, |
| 57 |
access virtual console, access virtual media, and execute debug commands. |
| 58 |
- A user with C(ReadOnly) privilege can only log in to iDRAC. |
| 59 |
- A user with C(None), no privileges assigned. It is not applicable for iDRAC10. |
| 60 |
- Will be ignored, if custom_privilege parameter is provided. |
| 61 |
choices: [Administrator, ReadOnly, Operator, None] |
| 62 |
aliases: ['role'] |
| 63 |
custom_privilege: |
| 64 |
type: int |
| 65 |
description: |
| 66 |
- The privilege level assigned to the user. |
| 67 |
- Minimum value is 1 for iDRAC10. |
| 68 |
version_added: "8.1.0" |
| 69 |
custom_role_name: |
| 70 |
type: str |
| 71 |
description: |
| 72 |
- custom_role_name is required for iDRAC10 when creating a user using custom role for iDRAC10. |
| 73 |
ipmi_lan_privilege: |
| 74 |
type: str |
| 75 |
description: The Intelligent Platform Management Interface LAN privilege level assigned to the user. |
| 76 |
choices: [Administrator, Operator, User, No Access] |
| 77 |
ipmi_serial_privilege: |
| 78 |
type: str |
| 79 |
description: |
| 80 |
- The Intelligent Platform Management Interface Serial Port privilege level assigned to the user. |
| 81 |
- This option is only applicable for rack and tower servers. |
| 82 |
choices: [Administrator, Operator, User, No Access] |
| 83 |
enable: |
| 84 |
type: bool |
| 85 |
description: Provide the option to enable or disable a user from logging in to iDRAC. |
| 86 |
sol_enable: |
| 87 |
type: bool |
| 88 |
description: Enables Serial Over Lan (SOL) for an iDRAC user. |
| 89 |
protocol_enable: |
| 90 |
type: bool |
| 91 |
description: Enables protocol for the iDRAC user. |
| 92 |
authentication_protocol: |
| 93 |
type: str |
| 94 |
description: |
| 95 |
- This option allows to configure one of the following authentication protocol |
| 96 |
types to authenticate the iDRAC user. |
| 97 |
- Secure Hash Algorithm C(SHA). Not applicable for iDRAC10. |
| 98 |
- Message Digest 5 C(MD5). Not applicable for iDRAC10. |
| 99 |
- Secure Hash Algorithm 384-bit C(SHA-384). Only applicable for iDRAC10. |
| 100 |
- Secure Hash Algorithm 512-bit C(SHA-512). Only applicable for iDRAC10. |
| 101 |
- An authentication protocol is not configured if C(None) is selected. |
| 102 |
choices: [None, SHA, MD5, SHA-384, SHA-512] |
| 103 |
privacy_protocol: |
| 104 |
type: str |
| 105 |
description: |
| 106 |
- This option allows to configure one of the following privacy encryption protocols for the iDRAC user. |
| 107 |
- Data Encryption Standard C(DES). Not applicable for iDRAC10. |
| 108 |
- Advanced Encryption Standard C(AES). Not applicable for iDRAC10. |
| 109 |
- Advanced Encryption Standard 256-bit C(AES-256). Only applicable for iDRAC9 and iDRAC10. |
| 110 |
- A privacy protocol is not configured if C(None) is selected. |
| 111 |
choices: [None, DES, AES, AES-256] |
| 112 |
requirements: |
| 113 |
- "python >= 3.9.6" |
| 114 |
author: |
| 115 |
- "Felix Stephen (@felixs88)" |
| 116 |
- "Kritika Bhateja (@Kritika-Bhateja-03)" |
| 117 |
notes: |
| 118 |
- Run this module from a system that has direct access to Dell iDRAC. |
| 119 |
- This module supports C(check_mode). |
| 120 |
""" |
| 121 |
|
| 122 |
EXAMPLES = """ |
| 123 |
--- |
| 124 |
- name: Configure a new iDRAC user |
| 125 |
dellemc.openmanage.idrac_user: |
| 126 |
idrac_ip: 198.162.0.1 |
| 127 |
idrac_user: idrac_user |
| 128 |
idrac_password: idrac_password |
| 129 |
ca_path: "/path/to/ca_cert.pem" |
| 130 |
state: present |
| 131 |
user_name: user_name |
| 132 |
user_password: user_password |
| 133 |
privilege: Administrator |
| 134 |
ipmi_lan_privilege: Administrator |
| 135 |
ipmi_serial_privilege: Administrator |
| 136 |
enable: true |
| 137 |
sol_enable: true |
| 138 |
protocol_enable: true |
| 139 |
authentication_protocol: SHA |
| 140 |
privacy_protocol: AES |
| 141 |
|
| 142 |
- name: Modify existing iDRAC user username and password |
| 143 |
dellemc.openmanage.idrac_user: |
| 144 |
idrac_ip: 198.162.0.1 |
| 145 |
idrac_user: idrac_user |
| 146 |
idrac_password: idrac_password |
| 147 |
ca_path: "/path/to/ca_cert.pem" |
| 148 |
state: present |
| 149 |
user_name: user_name |
| 150 |
new_user_name: new_user_name |
| 151 |
user_password: user_password |
| 152 |
|
| 153 |
- name: Delete existing iDRAC user account |
| 154 |
dellemc.openmanage.idrac_user: |
| 155 |
idrac_ip: 198.162.0.1 |
| 156 |
idrac_user: idrac_user |
| 157 |
idrac_password: idrac_password |
| 158 |
ca_path: "/path/to/ca_cert.pem" |
| 159 |
state: absent |
| 160 |
user_name: user_name |
| 161 |
""" |
| 162 |
|
| 163 |
RETURN = r''' |
| 164 |
--- |
| 165 |
msg: |
| 166 |
description: Status of the iDRAC user configuration. |
| 167 |
returned: always |
| 168 |
type: str |
| 169 |
sample: "Successfully created user account details." |
| 170 |
status: |
| 171 |
description: Configures the iDRAC users attributes. |
| 172 |
returned: success |
| 173 |
type: dict |
| 174 |
sample: { |
| 175 |
"@Message.ExtendedInfo": [{ |
| 176 |
"Message": "Successfully Completed Request", |
| 177 |
"MessageArgs": [], |
| 178 |
"[email protected]": 0, |
| 179 |
"MessageId": "Base.1.5.Success", |
| 180 |
"RelatedProperties": [], |
| 181 |
"[email protected]": 0, |
| 182 |
"Resolution": "None", |
| 183 |
"Severity": "OK" |
| 184 |
}, { |
| 185 |
"Message": "The operation successfully completed.", |
| 186 |
"MessageArgs": [], |
| 187 |
"[email protected]": 0, |
| 188 |
"MessageId": "IDRAC.2.1.SYS413", |
| 189 |
"RelatedProperties": [], |
| 190 |
"[email protected]": 0, |
| 191 |
"Resolution": "No response action is required.", |
| 192 |
"Severity": "Informational"} |
| 193 |
]} |
| 194 |
error_info: |
| 195 |
description: Details of the HTTP Error. |
| 196 |
returned: on HTTP error |
| 197 |
type: dict |
| 198 |
sample: { |
| 199 |
"error": { |
| 200 |
"code": "Base.1.0.GeneralError", |
| 201 |
"message": "A general error has occurred. See ExtendedInfo for more information.", |
| 202 |
"@Message.ExtendedInfo": [ |
| 203 |
{ |
| 204 |
"MessageId": "GEN1234", |
| 205 |
"RelatedProperties": [], |
| 206 |
"Message": "Unable to process the request because an error occurred.", |
| 207 |
"MessageArgs": [], |
| 208 |
"Severity": "Critical", |
| 209 |
"Resolution": "Retry the operation. If the issue persists, contact your system administrator." |
| 210 |
} |
| 211 |
] |
| 212 |
} |
| 213 |
} |
| 214 |
''' |
| 215 |
|
| 216 |
|
| 217 |
import json |
| 218 |
import re |
| 219 |
import time |
| 220 |
from ssl import SSLError |
| 221 |
from ansible.module_utils.six.moves.urllib.error import URLError, HTTPError |
| 222 |
from ansible.module_utils.urls import ConnectionError, SSLValidationError |
| 223 |
from ansible_collections.dellemc.openmanage.plugins.module_utils.idrac_redfish\ |
| 224 |
import iDRACRedfishAPI, IdracAnsibleModule |
| 225 |
from ansible_collections.dellemc.openmanage.plugins.module_utils.utils import ( |
| 226 |
remove_key) |
| 227 |
|
| 228 |
ACCOUNT_URI = "/redfish/v1/Managers/iDRAC.Embedded.1/Accounts/" |
| 229 |
ATTRIBUTE_URI = "/redfish/v1/Managers/iDRAC.Embedded.1/Attributes/" |
| 230 |
ATTRIBUTE_URI_10 = "/redfish/v1/Managers/iDRAC.Embedded.1/Oem/Dell/DellAttributes/iDRAC.Embedded.1/" |
| 231 |
CREATE_CUSTOM_ROLE_URI = "/redfish/v1/AccountService/Roles" |
| 232 |
GET_ROLES_URI = "/redfish/v1/AccountService/Roles?$expand=*($levels=1)" |
| 233 |
MANAGERS_ATTRIBUTES_REGISTRY = "/redfish/v1/Registries/\ |
| 234 |
ManagerAttributeRegistry/ManagerAttributeRegistry.v1_0_0.json" |
| 235 |
USER_ROLES = {"Administrator": 511, "Operator": 499, "ReadOnly": 1, "None": 0} |
| 236 |
ACCESS = {0: "Disabled", 1: "Enabled"} |
| 237 |
INVALID_PRIVILEGE_MSG = "custom_privilege value must range from {0} to 511." |
| 238 |
INVALID_PRIVILEGE_MSG_NONE = "None is not an applicable value for privilege in iDRAC 17G and later." |
| 239 |
INVALID_PRIVILEGE_MIN_iDRAC9 = 0 |
| 240 |
INVALID_PRIVILEGE_MIN_iDRAC10 = 1 |
| 241 |
INVALID_PRIVILEGE_MAX = 511 |
| 242 |
CHANGES_FOUND_MSG = "Changes found to commit!" |
| 243 |
ODATA_ID = "(.*?)@odata" |
| 244 |
UNSUPPORTED_APPLY_TIME = "Apply time {0} is not supported." |
| 245 |
INVALID_AUTHENTICATION_PROTOCOL_MSG = "Authentication protocol {protocol} is\ |
| 246 |
not supported. The supported authentication protocols are\ |
| 247 |
{supported_auth_protocol}." |
| 248 |
INVALID_PRIVACY_PROTOCOL_MSG = "Privacy protocol {protocol} is\ |
| 249 |
not supported. The supported privacy protocols are\ |
| 250 |
{supported_privacy_protocol}." |
| 251 |
PRIVILEGE_KEY = "Users.{0}.Privilege" |
| 252 |
USERNAME_KEY = "Users.{0}.UserName" |
| 253 |
ROLE_KEY = "Users.{0}.Role" |
| 254 |
INVALID_USERNAME_FORMAT = "{username} is not a valid username." |
| 255 |
CUSTOM_ROLE_NAME_REQUIRED_MSG = "custom_role_name is required parameter when using custom_privilege on iDRAC 10." |
| 256 |
|
| 257 |
|
| 258 |
def _get_server_version(idrac: iDRACRedfishAPI) -> int: |
| 259 |
""" |
| 260 |
Function wrapping idrac.get_server_generation. Helps with mocked testing and linting. |
| 261 |
|
| 262 |
Args: |
| 263 |
idrac (iDRACRedfishAPI): iDRACRedfishAPI object. |
| 264 |
|
| 265 |
Returns: |
| 266 |
int: The server generation. |
| 267 |
""" |
| 268 |
t = idrac.get_server_generation |
| 269 |
return t[0] |
| 270 |
|
| 271 |
|
| 272 |
def compare_payload(json_payload, idrac_attr): |
| 273 |
""" |
| 274 |
:param json_payload: json payload created for update operation |
| 275 |
:param idrac_attr: idrac user attributes |
| 276 |
case1: always skip password for difference |
| 277 |
case2: as idrac_attr returns privilege in the format of string so |
| 278 |
convert payload to string only for comparision |
| 279 |
:return: bool |
| 280 |
""" |
| 281 |
copy_json = json_payload.copy() |
| 282 |
for key, val in dict(copy_json).items(): |
| 283 |
split_key = key.split("#")[1] |
| 284 |
if split_key == "Password": |
| 285 |
is_change_required = True |
| 286 |
break |
| 287 |
if split_key == "Privilege": |
| 288 |
copy_json[key] = str(val) |
| 289 |
else: |
| 290 |
is_change_required = bool(list(set(copy_json.items()) - set(idrac_attr.items()))) |
| 291 |
return is_change_required |
| 292 |
|
| 293 |
|
| 294 |
def get_user_account(module, idrac): |
| 295 |
""" |
| 296 |
This function gets the slot id and slot uri for create and modify. |
| 297 |
:param module: ansible module arguments |
| 298 |
:param idrac: idrac objects |
| 299 |
:return: user_attr, slot_uri, slot_id, empty_slot, empty_slot_uri |
| 300 |
""" |
| 301 |
slot_uri, slot_id, empty_slot, empty_slot_uri = None, None, None, None |
| 302 |
if not module.params["user_name"]: |
| 303 |
module.exit_json(msg="User name is not valid.", failed=True) |
| 304 |
response = idrac.export_scp(export_format="JSON", export_use="Default", target=["IDRAC"], job_wait=True) |
| 305 |
user_attributes = idrac.get_idrac_local_account_attr(response.json_data, fqdd="iDRAC.Embedded.1") |
| 306 |
slot_num = tuple(range(2, 17)) |
| 307 |
for num in slot_num: |
| 308 |
user_name = "Users.{0}#UserName".format(num) |
| 309 |
if user_attributes.get(user_name) == module.params["user_name"]: |
| 310 |
slot_id = num |
| 311 |
slot_uri = ACCOUNT_URI + str(num) |
| 312 |
break |
| 313 |
if not user_attributes.get(user_name) and (empty_slot_uri and empty_slot) is None: |
| 314 |
empty_slot = num |
| 315 |
empty_slot_uri = ACCOUNT_URI + str(num) |
| 316 |
return user_attributes, slot_uri, slot_id, empty_slot, empty_slot_uri |
| 317 |
|
| 318 |
|
| 319 |
def get_role(role_value): |
| 320 |
if isinstance(role_value, str): |
| 321 |
return role_value |
| 322 |
user_role_rev = {value: key for key, value in USER_ROLES.items()} |
| 323 |
sorted_user_role_rev = dict(sorted(user_role_rev.items())) |
| 324 |
for key, value in sorted_user_role_rev.items(): |
| 325 |
if key >= role_value: |
| 326 |
return value |
| 327 |
|
| 328 |
|
| 329 |
PRIVILEGE_BIT_MAP = { |
| 330 |
1: ("AssignedPrivileges", "Login"), |
| 331 |
2: ("AssignedPrivileges", "ConfigureManager"), |
| 332 |
4: ("AssignedPrivileges", "ConfigureUsers"), |
| 333 |
8: ("OemPrivileges", "ClearLogs"), |
| 334 |
16: ("AssignedPrivileges", "ConfigureComponents"), |
| 335 |
32: ("OemPrivileges", "AccessVirtualConsole"), |
| 336 |
64: ("OemPrivileges", "AccessVirtualMedia"), |
| 337 |
128: ("OemPrivileges", "TestAlerts"), |
| 338 |
256: ("OemPrivileges", "ExecuteDebugCommands"), |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
def build_role_body(custom_priv): |
| 343 |
assigned = [] |
| 344 |
oem = [] |
| 345 |
|
| 346 |
for bit, (ptype, pname) in PRIVILEGE_BIT_MAP.items(): |
| 347 |
if custom_priv & bit: |
| 348 |
if ptype == "AssignedPrivileges": |
| 349 |
assigned.append(pname) |
| 350 |
else: |
| 351 |
oem.append(pname) |
| 352 |
|
| 353 |
return { |
| 354 |
"AssignedPrivileges": assigned, |
| 355 |
"OemPrivileges": oem |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
def create_custom_role(module, idrac, role_name, custom_priv): |
| 360 |
|
| 361 |
payload = { |
| 362 |
"RoleId": role_name, |
| 363 |
**build_role_body(custom_priv) |
| 364 |
} |
| 365 |
|
| 366 |
response = idrac.invoke_request(CREATE_CUSTOM_ROLE_URI, "POST", data=payload) |
| 367 |
|
| 368 |
if (response.status_code not in [200, 201, 204]): |
| 369 |
module.exit_json(msg="Failed to create custom role.", failed=True, error_info=response.json_data) |
| 370 |
return role_name |
| 371 |
|
| 372 |
|
| 373 |
def rolename_exists(idrac, role_name): |
| 374 |
""" |
| 375 |
Check if a role already exists in iDRAC. |
| 376 |
|
| 377 |
:param idrac: iDRAC connection object |
| 378 |
:param role_name: Role name to check |
| 379 |
:return: True if role exists, else False |
| 380 |
""" |
| 381 |
|
| 382 |
response = idrac.invoke_request(GET_ROLES_URI, "GET") |
| 383 |
|
| 384 |
members = response.json_data.get("Members", []) |
| 385 |
|
| 386 |
for role in members: |
| 387 |
role_id = role.get("Id") |
| 388 |
if not role_id: |
| 389 |
odata_id = role.get("@odata.id", "") |
| 390 |
role_id = odata_id.rstrip("/").split("/")[-1] |
| 391 |
|
| 392 |
if role_id == role_name: |
| 393 |
return True |
| 394 |
|
| 395 |
return False |
| 396 |
|
| 397 |
|
| 398 |
def _get_user_privilege(module, generation): |
| 399 |
"""Get user privilege value based on module parameters.""" |
| 400 |
if generation >= 17 and module.params['privilege'] == 'None': |
| 401 |
module.exit_json(msg=INVALID_PRIVILEGE_MSG_NONE, failed=True) |
| 402 |
|
| 403 |
if "custom_privilege" in module.params and module.params["custom_privilege"] is not None: |
| 404 |
return module.params["custom_privilege"] |
| 405 |
return USER_ROLES.get(module.params["privilege"]) |
| 406 |
|
| 407 |
|
| 408 |
def _build_base_payload(module, user_privilege): |
| 409 |
"""Build the base slot payload with common fields.""" |
| 410 |
return { |
| 411 |
USERNAME_KEY: module.params["user_name"], |
| 412 |
"Users.{0}.Password": module.params["user_password"], |
| 413 |
"Users.{0}.Enable": ACCESS.get(module.params["enable"]), |
| 414 |
PRIVILEGE_KEY: user_privilege, |
| 415 |
"Users.{0}.IpmiLanPrivilege": module.params["ipmi_lan_privilege"], |
| 416 |
"Users.{0}.IpmiSerialPrivilege": module.params["ipmi_serial_privilege"], |
| 417 |
"Users.{0}.SolEnable": ACCESS.get(module.params["sol_enable"]), |
| 418 |
"Users.{0}.ProtocolEnable": ACCESS.get(module.params["protocol_enable"]), |
| 419 |
"Users.{0}.AuthenticationProtocol": module.params["authentication_protocol"], |
| 420 |
"Users.{0}.PrivacyProtocol": module.params["privacy_protocol"], |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
def _handle_absent_state_payload(): |
| 425 |
"""Build payload for absent state (user deletion).""" |
| 426 |
return { |
| 427 |
USERNAME_KEY: "", |
| 428 |
"Users.{0}.Enable": "Disabled", |
| 429 |
PRIVILEGE_KEY: 0, |
| 430 |
"Users.{0}.IpmiLanPrivilege": "No Access", |
| 431 |
"Users.{0}.IpmiSerialPrivilege": "No Access", |
| 432 |
"Users.{0}.SolEnable": "Disabled", |
| 433 |
"Users.{0}.ProtocolEnable": "Disabled", |
| 434 |
"Users.{0}.AuthenticationProtocol": "SHA", |
| 435 |
"Users.{0}.PrivacyProtocol": "AES" |
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
def _handle_role_assignment(module, idrac, slot_payload, user_privilege): |
| 440 |
"""Handle role assignment for generation >= 17.""" |
| 441 |
if user_privilege is None: |
| 442 |
return |
| 443 |
|
| 444 |
if user_privilege in USER_ROLES.values(): |
| 445 |
slot_payload[ROLE_KEY] = get_role(slot_payload[PRIVILEGE_KEY]) |
| 446 |
else: |
| 447 |
rolename = module.params.get("custom_role_name") |
| 448 |
if not rolename_exists(idrac, rolename): |
| 449 |
slot_payload[ROLE_KEY] = create_custom_role(module, idrac, rolename, slot_payload[PRIVILEGE_KEY]) |
| 450 |
else: |
| 451 |
slot_payload[ROLE_KEY] = rolename |
| 452 |
|
| 453 |
|
| 454 |
def get_payload(module, idrac, slot_id, generation, action=None): |
| 455 |
""" |
| 456 |
This function creates the payload with slot id. |
| 457 |
:param module: ansible module arguments |
| 458 |
:param action: new user name is only applicable in case of update user name. |
| 459 |
:param slot_id: slot id for user slot |
| 460 |
:return: json data with slot id |
| 461 |
""" |
| 462 |
user_privilege = _get_user_privilege(module, generation) |
| 463 |
|
| 464 |
if module.params["state"] == "absent": |
| 465 |
slot_payload = _handle_absent_state_payload() |
| 466 |
else: |
| 467 |
slot_payload = _build_base_payload(module, user_privilege) |
| 468 |
|
| 469 |
if module.params["new_user_name"] is not None and action == "update": |
| 470 |
user_name = USERNAME_KEY.format(slot_id) |
| 471 |
slot_payload[user_name] = module.params["new_user_name"] |
| 472 |
|
| 473 |
if generation >= 17: |
| 474 |
_handle_role_assignment(module, idrac, slot_payload, user_privilege) |
| 475 |
del slot_payload[PRIVILEGE_KEY] |
| 476 |
|
| 477 |
payload = dict([(k.format(slot_id), v) for k, v in slot_payload.items() if v is not None]) |
| 478 |
return payload |
| 479 |
|
| 480 |
|
| 481 |
def convert_payload_xml(payload): |
| 482 |
""" |
| 483 |
this function converts payload to xml and json data. |
| 484 |
:param payload: user input for payload |
| 485 |
:return: returns xml and json data |
| 486 |
""" |
| 487 |
root = """<SystemConfiguration><Component FQDD="iDRAC.Embedded.1">{0}</Component></SystemConfiguration>""" |
| 488 |
attr = "" |
| 489 |
json_payload = {} |
| 490 |
for k, v in payload.items(): |
| 491 |
key = re.sub(r"(?<=\d)\.", "#", k) |
| 492 |
attr += '<Attribute Name="{0}">{1}</Attribute>'.format(key, v) |
| 493 |
json_payload[key] = v |
| 494 |
root = root.format(attr) |
| 495 |
return root, json_payload |
| 496 |
|
| 497 |
|
| 498 |
def set_attribute_uri(generation): |
| 499 |
global ATTRIBUTE_URI |
| 500 |
if generation >= 17: |
| 501 |
ATTRIBUTE_URI = ATTRIBUTE_URI_10 |
| 502 |
|
| 503 |
|
| 504 |
def handle_create(module, idrac, generation, payload): |
| 505 |
if module.check_mode: |
| 506 |
module.exit_json(msg=CHANGES_FOUND_MSG, changed=True) |
| 507 |
if generation >= 17: |
| 508 |
response = idrac.invoke_request(ATTRIBUTE_URI, "PATCH", data={"Attributes": payload}) |
| 509 |
if generation >= 14: |
| 510 |
response = idrac.invoke_request(ATTRIBUTE_URI, "PATCH", data={"Attributes": payload}) |
| 511 |
elif generation < 14: |
| 512 |
payloads = convert_payload_xml(payload) |
| 513 |
xml_payload = payloads[0] |
| 514 |
time.sleep(10) |
| 515 |
response = idrac.import_scp(import_buffer=xml_payload, target=["ALL"], job_wait=True) |
| 516 |
return response |
| 517 |
|
| 518 |
|
| 519 |
def handle_update(module, idrac, generation, payload, value, xml_payload): |
| 520 |
if module.check_mode: |
| 521 |
if value: |
| 522 |
module.exit_json(msg=CHANGES_FOUND_MSG, changed=True) |
| 523 |
module.exit_json(msg="No changes found to commit!") |
| 524 |
if not value: |
| 525 |
module.exit_json(msg="Requested changes are already present in the user slot.") |
| 526 |
if generation >= 14: |
| 527 |
response = idrac.invoke_request(ATTRIBUTE_URI, "PATCH", data={"Attributes": payload}) |
| 528 |
elif generation < 14: |
| 529 |
time.sleep(10) |
| 530 |
response = idrac.import_scp(import_buffer=xml_payload, target=["ALL"], job_wait=True) |
| 531 |
return response |
| 532 |
|
| 533 |
|
| 534 |
def validate_username(module, username): |
| 535 |
if username is not None: |
| 536 |
allowed_specials = r'+%)>$[|!&=*,.-{}#(?<;_}I^' |
| 537 |
|
| 538 |
escaped_specials = re.escape(allowed_specials) |
| 539 |
|
| 540 |
pattern = rf'^[a-zA-Z0-9 {escaped_specials}]+$' |
| 541 |
if ( |
| 542 |
username != username.strip() or |
| 543 |
len(username) > 16 or |
| 544 |
not re.fullmatch(pattern, username) |
| 545 |
): |
| 546 |
module.exit_json(msg=INVALID_USERNAME_FORMAT.format(username=username), |
| 547 |
failed=True) |
| 548 |
|
| 549 |
|
| 550 |
def create_or_modify_account(module, idrac, slot_uri, slot_id, empty_slot_id, empty_slot_uri, user_attr, generation): |
| 551 |
""" |
| 552 |
This function create user account in case not exists else update it. |
| 553 |
:param module: user account module arguments |
| 554 |
:param idrac: idrac object |
| 555 |
:param slot_uri: slot uri for update |
| 556 |
:param slot_id: slot id for update |
| 557 |
:param empty_slot_id: empty slot id for create |
| 558 |
:param empty_slot_uri: empty slot uri for create |
| 559 |
:return: json |
| 560 |
""" |
| 561 |
msg, response = "Unable to retrieve the user details.", {} |
| 562 |
validate_username(module, module.params.get("user_name")) |
| 563 |
validate_username(module, module.params.get("new_user_name")) |
| 564 |
if (slot_id and slot_uri) is None and (empty_slot_id and empty_slot_uri) is not None: |
| 565 |
msg = "Successfully created user account." |
| 566 |
payload = get_payload(module, idrac, empty_slot_id, generation, action="create") |
| 567 |
response = handle_create(module, idrac, generation, payload) |
| 568 |
elif (slot_id and slot_uri) is not None: |
| 569 |
msg = "Successfully updated user account." |
| 570 |
payload = get_payload(module, idrac, slot_id, generation, action="update") |
| 571 |
xml_payload, json_payload = convert_payload_xml(payload) |
| 572 |
value = compare_payload(json_payload, user_attr) |
| 573 |
response = handle_update(module, idrac, generation, payload, value, xml_payload) |
| 574 |
elif (slot_id and slot_uri and empty_slot_id and empty_slot_uri) is None: |
| 575 |
module.exit_json( |
| 576 |
msg="Maximum number of users reached. Delete a \ |
| 577 |
user account and retry the operation.", |
| 578 |
failed=True |
| 579 |
) |
| 580 |
return response, msg |
| 581 |
|
| 582 |
|
| 583 |
def remove_user_account(module, idrac, slot_uri, slot_id, generation): |
| 584 |
""" |
| 585 |
remove user user account by passing empty payload details. |
| 586 |
:param module: user account module arguments. |
| 587 |
:param idrac: idrac object. |
| 588 |
:param slot_uri: user slot uri. |
| 589 |
:param slot_id: user slot id. |
| 590 |
:return: json. |
| 591 |
""" |
| 592 |
response, msg = {}, "Successfully deleted user account." |
| 593 |
payload = get_payload(module, idrac, slot_id, generation, action="delete") |
| 594 |
payload = convert_payload_xml(payload) |
| 595 |
xml_payload = payload[0] |
| 596 |
if module.check_mode and (slot_id and slot_uri) is not None: |
| 597 |
module.exit_json(msg=CHANGES_FOUND_MSG, changed=True) |
| 598 |
elif module.check_mode and (slot_uri and slot_id) is None: |
| 599 |
module.exit_json(msg="No changes found to commit!") |
| 600 |
elif not module.check_mode and (slot_uri and slot_id) is not None: |
| 601 |
time.sleep(10) |
| 602 |
response = idrac.import_scp(import_buffer=xml_payload, target=["ALL"], job_wait=True) |
| 603 |
else: |
| 604 |
module.exit_json(msg="The user account is absent.") |
| 605 |
return response, msg |
| 606 |
|
| 607 |
|
| 608 |
def validate_choices_for_protocol(idrac): |
| 609 |
try: |
| 610 |
resp = idrac.invoke_request(MANAGERS_ATTRIBUTES_REGISTRY, "GET") |
| 611 |
reg_entries = resp.json_data.get("RegistryEntries", {}) |
| 612 |
attributes = reg_entries.get("Attributes", [[]]) |
| 613 |
auth_dict = None |
| 614 |
privacy_dict = None |
| 615 |
for sub_dict in attributes: |
| 616 |
attr_name = sub_dict.get("AttributeName") |
| 617 |
if attr_name == "Users.1.AuthenticationProtocol": |
| 618 |
auth_dict = sub_dict |
| 619 |
elif attr_name == "Users.1.PrivacyProtocol": |
| 620 |
privacy_dict = sub_dict |
| 621 |
if auth_dict and privacy_dict: |
| 622 |
break |
| 623 |
auth_choices = [ |
| 624 |
item.get("ValueDisplayName") |
| 625 |
for item in auth_dict.get("Value", []) |
| 626 |
] if auth_dict else [] |
| 627 |
|
| 628 |
privacy_choices = [ |
| 629 |
item.get("ValueDisplayName") |
| 630 |
for item in privacy_dict.get("Value", []) |
| 631 |
] if privacy_dict else [] |
| 632 |
return privacy_choices, auth_choices |
| 633 |
except Exception: |
| 634 |
return [], [] |
| 635 |
|
| 636 |
|
| 637 |
def validate_authentication_and_privacy_protocols(module, |
| 638 |
idrac, |
| 639 |
authentication_protocol, |
| 640 |
privacy_protocol): |
| 641 |
privacy_list, auth_list = validate_choices_for_protocol(idrac) |
| 642 |
if authentication_protocol and authentication_protocol not in auth_list: |
| 643 |
auth_invalid_msg = INVALID_AUTHENTICATION_PROTOCOL_MSG.format( |
| 644 |
protocol=authentication_protocol, |
| 645 |
supported_auth_protocol=auth_list |
| 646 |
) |
| 647 |
module.exit_json(msg=auth_invalid_msg, |
| 648 |
failed=True) |
| 649 |
if privacy_protocol and privacy_protocol not in privacy_list: |
| 650 |
privacy_invalid_msg = INVALID_PRIVACY_PROTOCOL_MSG.format( |
| 651 |
protocol=privacy_protocol, |
| 652 |
supported_privacy_protocol=privacy_list |
| 653 |
) |
| 654 |
module.exit_json(msg=privacy_invalid_msg, |
| 655 |
failed=True) |
| 656 |
|
| 657 |
|
| 658 |
def _get_privilege_min_value(generation): |
| 659 |
"""Get minimum privilege value based on generation.""" |
| 660 |
if isinstance(generation, int) and generation >= 17: |
| 661 |
return INVALID_PRIVILEGE_MIN_iDRAC10 |
| 662 |
return INVALID_PRIVILEGE_MIN_iDRAC9 |
| 663 |
|
| 664 |
|
| 665 |
def _validate_privilege_range(module, user_privilege, min_value): |
| 666 |
"""Validate that user privilege is within allowed range.""" |
| 667 |
if min_value > user_privilege or user_privilege > INVALID_PRIVILEGE_MAX: |
| 668 |
module.exit_json(msg=INVALID_PRIVILEGE_MSG.format(min_value), failed=True) |
| 669 |
|
| 670 |
|
| 671 |
def _validate_custom_role_requirements(module, generation, custom_privilege, custom_role_name): |
| 672 |
"""Validate custom role requirements for iDRAC 17+.""" |
| 673 |
if generation >= 17 and custom_privilege is not None and not custom_role_name: |
| 674 |
module.exit_json(msg=CUSTOM_ROLE_NAME_REQUIRED_MSG, failed=True) |
| 675 |
|
| 676 |
|
| 677 |
def validate_input(module, idrac, generation): |
| 678 |
if module.params["state"] != "present": |
| 679 |
return |
| 680 |
|
| 681 |
min_value = _get_privilege_min_value(generation) |
| 682 |
user_privilege = module.params["custom_privilege"] if "custom_privilege" in module.params and \ |
| 683 |
module.params["custom_privilege"] is not None else USER_ROLES.get(module.params["privilege"], min_value) |
| 684 |
|
| 685 |
_validate_privilege_range(module, user_privilege, min_value) |
| 686 |
|
| 687 |
custom_privilege = module.params.get("custom_privilege") |
| 688 |
custom_role_name = module.params.get("custom_role_name") |
| 689 |
_validate_custom_role_requirements(module, generation, custom_privilege, custom_role_name) |
| 690 |
|
| 691 |
authentication_protocol = module.params.get("authentication_protocol") |
| 692 |
privacy_protocol = module.params.get("privacy_protocol") |
| 693 |
validate_authentication_and_privacy_protocols(module, idrac, authentication_protocol, privacy_protocol) |
| 694 |
|
| 695 |
|
| 696 |
def main(): |
| 697 |
specs = { |
| 698 |
"state": {"required": False, "choices": ['present', 'absent'], "default": "present"}, |
| 699 |
"new_user_name": {"required": False}, |
| 700 |
"user_name": {"required": True}, |
| 701 |
"user_password": {"required": False, "no_log": True}, |
| 702 |
"custom_role_name": {"required": False}, |
| 703 |
"privilege": {"required": False, |
| 704 |
"choices": ['Administrator', 'ReadOnly', 'Operator', 'None'], |
| 705 |
"aliases": ['role']}, |
| 706 |
"custom_privilege": {"required": False, "type": "int"}, |
| 707 |
"ipmi_lan_privilege": {"required": False, "choices": ['Administrator', 'Operator', 'User', 'No Access']}, |
| 708 |
"ipmi_serial_privilege": {"required": False, "choices": ['Administrator', 'Operator', 'User', 'No Access']}, |
| 709 |
"enable": {"required": False, "type": "bool"}, |
| 710 |
"sol_enable": {"required": False, "type": "bool"}, |
| 711 |
"protocol_enable": {"required": False, "type": "bool"}, |
| 712 |
"authentication_protocol": {"required": False, "choices": ['SHA', 'MD5', 'None', 'SHA-384', 'SHA-512']}, |
| 713 |
"privacy_protocol": {"required": False, "choices": ['AES', 'DES', 'None', 'AES-256']}, |
| 714 |
} |
| 715 |
module = IdracAnsibleModule( |
| 716 |
argument_spec=specs, |
| 717 |
supports_check_mode=True) |
| 718 |
try: |
| 719 |
with iDRACRedfishAPI(module.params, req_session=True) as idrac: |
| 720 |
generation = _get_server_version(idrac) |
| 721 |
validate_input(module, idrac, generation) |
| 722 |
set_attribute_uri(generation) |
| 723 |
user_attr, slot_uri, slot_id, empty_slot_id, empty_slot_uri = get_user_account(module, idrac) |
| 724 |
if module.params["state"] == "present": |
| 725 |
response, message = create_or_modify_account(module, idrac, slot_uri, slot_id, empty_slot_id, |
| 726 |
empty_slot_uri, user_attr, generation) |
| 727 |
elif module.params["state"] == "absent": |
| 728 |
response, message = remove_user_account(module, idrac, slot_uri, slot_id, generation) |
| 729 |
error = response.json_data.get("error") |
| 730 |
oem = response.json_data.get("Oem") |
| 731 |
out_data = remove_key(response.json_data, regex_pattern=ODATA_ID) |
| 732 |
if oem: |
| 733 |
oem_msg = oem.get("Dell").get("Message") |
| 734 |
error_msg = ["Unable to complete application of configuration profile values.", |
| 735 |
"Import of Server Configuration Profile operation completed with errors."] |
| 736 |
if oem_msg in error_msg: |
| 737 |
module.exit_json(msg=oem_msg, |
| 738 |
error_info=out_data, |
| 739 |
failed=True) |
| 740 |
if error: |
| 741 |
module.exit_json(msg=error.get("message"), |
| 742 |
error_info=out_data, |
| 743 |
failed=True) |
| 744 |
module.exit_json(msg=message, status=out_data, changed=True) |
| 745 |
except HTTPError as err: |
| 746 |
module.exit_json(msg=str(err), |
| 747 |
error_info=json.load(err), |
| 748 |
failed=True) |
| 749 |
except URLError as err: |
| 750 |
module.exit_json(msg=str(err), unreachable=True) |
| 751 |
except (RuntimeError, SSLValidationError, ConnectionError, KeyError, |
| 752 |
ImportError, ValueError, TypeError, SSLError) as e: |
| 753 |
module.exit_json(msg=str(e), failed=True) |
| 754 |
|
| 755 |
|
| 756 |
if __name__ == '__main__': |
| 757 |
main() |