| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
from __future__ import absolute_import, division, print_function |
| 14 |
|
| 15 |
__metaclass__ = type |
| 16 |
|
| 17 |
DOCUMENTATION = """ |
| 18 |
--- |
| 19 |
module: idrac_secure_boot |
| 20 |
short_description: Configure attributes, import, or export secure boot certificate, and reset keys. |
| 21 |
version_added: "9.6.0" |
| 22 |
description: |
| 23 |
- This module allows you to perform the following operations.` |
| 24 |
- Import or Export Secure Boot certificate. |
| 25 |
- Enable or disable Secure Boot mode. |
| 26 |
- Configure Platform Key (PK) and Key Exchange Key (KEK) policies |
| 27 |
- Configure Allow Database (DB) and Disallow Database (DBX) certificates. |
| 28 |
- Reset UEFI Secure Boot keys. |
| 29 |
extends_documentation_fragment: |
| 30 |
- dellemc.openmanage.idrac_x_auth_options |
| 31 |
options: |
| 32 |
boot_mode: |
| 33 |
type: str |
| 34 |
choices: [Uefi, Bios] |
| 35 |
description: |
| 36 |
- Boot mode of the iDRAC. Not supported for 17G iDRAC10 and later. |
| 37 |
- C(Uefi) enables the secure boot in UEFI mode. |
| 38 |
- C(Bios) enables the secure boot in BIOS mode. |
| 39 |
secure_boot: |
| 40 |
type: str |
| 41 |
choices: [Disabled, Enabled] |
| 42 |
description: |
| 43 |
- UEFI Secure Boot. |
| 44 |
- The I(secure_boot) can be C(Enabled) only if I(boot_mode) is C(UEFI) and I(force_int_10) is C(Disabled). |
| 45 |
- C(Disabled) disables the secure boot mode. |
| 46 |
- C(Enabled) enables the secure boot mode. |
| 47 |
secure_boot_mode: |
| 48 |
type: str |
| 49 |
choices: [AuditMode, DeployedMode, UserMode] |
| 50 |
description: |
| 51 |
- The UEFI Secure Boot mode configures how to use the Secure Boot Policy. |
| 52 |
- C(AuditMode) sets the Secure Boot mode to an Audit mode when Platform Key is not installed on the system. The BIOS does not authenticate |
| 53 |
updates to the policy objects and transition between modes. BIOS performs a signature verification on pre-boot images and logs the results in the Image |
| 54 |
Execution Information table, where it processes the images whether the status of verification is pass or fail. |
| 55 |
- C(DeployedMode) sets the Secure Boot mode to a Deployed mode when Platform Key is installed on the system, and then BIOS performs a signature |
| 56 |
verification to update the policy objects. |
| 57 |
- C(UserMode) sets the Secure Boot mode to a User mode when Platform Key is installed on the system, and then BIOS performs signature |
| 58 |
verification to update policy objects. |
| 59 |
secure_boot_policy: |
| 60 |
type: str |
| 61 |
choices: [Custom, Standard] |
| 62 |
description: |
| 63 |
- The following are the types of Secure Boot policy. |
| 64 |
- C(Custom) inherits the standard certificates and image digests that are loaded in the system by default. |
| 65 |
You can modify the certificates and image digests. |
| 66 |
- C(Standard) indicates that the system has default certificates, image digests, or hash loaded from the factory. |
| 67 |
- When the Secure Boot Policy is set to Custom, you can perform following operations such as viewing, exporting, |
| 68 |
importing, deleting, deleting all, and resetting policies. |
| 69 |
force_int_10: |
| 70 |
type: str |
| 71 |
choices: [Disabled, Enabled] |
| 72 |
description: |
| 73 |
- Determines whether the system BIOS loads the legacy video (INT 10h) option ROM from the video controller. |
| 74 |
- This parameter is supported only in UEFI boot mode. If UEFI Secure Boot mode is enabled, you cannot enable this parameter. |
| 75 |
- This parameter is not supported for 17G iDRAC10 and later. |
| 76 |
- C(Disabled) if the operating system supports UEFI video output standards. |
| 77 |
- C(Enabled) if the operating system does not support UEFI video output standards. |
| 78 |
export_certificates: |
| 79 |
type: bool |
| 80 |
description: |
| 81 |
- Export all the available certificates in the specified directory for the given keys. |
| 82 |
- I(export_cetificates) is mutually exclusive with I(import). |
| 83 |
- I(export_cetificates) is C(true) either of I(platform_key) or i(key_exchange_key) or I(database) - or I(disallow_database) is required. |
| 84 |
import_certificates: |
| 85 |
type: bool |
| 86 |
description: |
| 87 |
- Import all the specified key certificates. |
| 88 |
- When I(import_certificates) is C(true), then either I(platform_key), I(KEK), I(database), or I(disallow_database) is required. |
| 89 |
platform_key: |
| 90 |
type: path |
| 91 |
description: |
| 92 |
- The absolute path of the Platform key certificate file for UEFI secure boot. |
| 93 |
- Directory path with write permission when I(export_certificates) is C(true). |
| 94 |
KEK: |
| 95 |
type: list |
| 96 |
elements: path |
| 97 |
description: |
| 98 |
- A list of absolute paths of the Key Exchange Key (KEK) certificate file for UEFI secure boot. |
| 99 |
- Directory path with write permission when I(export_certificates) is C(true). |
| 100 |
database: |
| 101 |
type: list |
| 102 |
elements: path |
| 103 |
description: |
| 104 |
- A list of absolute paths of the Allow Database(DB) certificate file for UEFI secure boot. |
| 105 |
- Directory path with write permission when I(export_certificates) is C(true). |
| 106 |
disallow_database: |
| 107 |
type: list |
| 108 |
elements: path |
| 109 |
description: |
| 110 |
- A list of absolute paths of the Disallow Database(DBX) certificate file for UEFI secure boot. |
| 111 |
- Directory path with write permission when I(export_certificates) is C(true). |
| 112 |
reset_keys: |
| 113 |
type: str |
| 114 |
choices: [DeleteAllKeys, DeletePK, ResetAllKeysToDefault, ResetDB, ResetDBX, ResetKEK, ResetPK] |
| 115 |
description: |
| 116 |
- Resets the UEFI Secure Boot keys. |
| 117 |
- C(DeleteAllKeys) deletes the content of all UEFI Secure Boot key databases (PK, KEK, DB, and DBX). This choice configures the system in Setup Mode. |
| 118 |
- C(DeletePK) deletes the content of the PK UEFI Secure Boot database. This choice configures the system in Setup Mode. |
| 119 |
- C(ResetAllKeysToDefault) resets the content of all UEFI Secure Boot key databases (PK, KEK, DB, and DBX) to their default values. |
| 120 |
- C(ResetDB) resets the content of the DB UEFI Secure Boot database to its default values. |
| 121 |
- C(ResetDBX) resets the content of the DBX UEFI Secure Boot database to its default values. |
| 122 |
- C(ResetKEK) resets the content of the KEK UEFI Secure Boot database to its default values. |
| 123 |
- C(ResetPK) resets the content of the PK UEFI Secure Boot database to its default values. |
| 124 |
restart: |
| 125 |
type: bool |
| 126 |
default: false |
| 127 |
description: |
| 128 |
- Secure boot certificate import operation requires a server restart. This parameter provides an option to restart the server. |
| 129 |
- C(true) restarts the server. |
| 130 |
- C(false) does not restart the server. |
| 131 |
- I(restart) is applicable when I(import_certificates) is C(true). |
| 132 |
- I(restart) will be ignored only when I(export_certificates) is C(true). |
| 133 |
restart_type: |
| 134 |
type: str |
| 135 |
default: GracefulRestart |
| 136 |
choices: [GracefulRestart, ForceRestart] |
| 137 |
description: |
| 138 |
- Restart type of the server. |
| 139 |
- C(ForceRestart) forcefully restarts the server. |
| 140 |
- C(GracefulRestart) gracefully restarts the server. |
| 141 |
- I(restart_type) is applicable when I(restart) is C(true). |
| 142 |
job_wait: |
| 143 |
type: bool |
| 144 |
default: true |
| 145 |
description: |
| 146 |
- Whether to wait till completion of the secure boot certificate operation. This is applicable when I(restart) is C(true). |
| 147 |
job_wait_timeout: |
| 148 |
type: int |
| 149 |
default: 1200 |
| 150 |
description: |
| 151 |
- The maximum wait time of I(job_wait) in seconds. The job is tracked only for this duration. |
| 152 |
- This option is applicable when I(job_wait) is C(true). |
| 153 |
requirements: |
| 154 |
- "python >= 3.9.6" |
| 155 |
author: |
| 156 |
- "Abhishek Sinha(@ABHISHEK-SINHA10)" |
| 157 |
- "Lovepreet Singh (@singh-lovepreet1)" |
| 158 |
- "Rounak Adhikary (@rounak-adhikary)" |
| 159 |
attributes: |
| 160 |
check_mode: |
| 161 |
description: Runs task to validate without performing action on the target machine. |
| 162 |
support: full |
| 163 |
diff_mode: |
| 164 |
description: Runs the task to report the changes made or to be made. |
| 165 |
support: none |
| 166 |
notes: |
| 167 |
- This module will always report changes found to be applied for I(import_certificates) when run in C(check mode). |
| 168 |
- This module does not support idempotency when I(reset_type) or I(export_certificates) |
| 169 |
or I(import_certificates) is provided. |
| 170 |
- To configure the secure boot settings, the idrac_secure_boot module performs the following order of operations |
| 171 |
set attributes, export certificate, reset keys, import certificate, and restart iDRAC. |
| 172 |
- I(export_certificate) will export all the certificates of the key defined in the playbook. |
| 173 |
- This module considers values of I(restart) and I(job_wait) only for the last operation in the sequence. |
| 174 |
- This module supports IPv4 and IPv6 addresses. |
| 175 |
- Only I(reset_keys) is supported on iDRAC8. |
| 176 |
""" |
| 177 |
|
| 178 |
EXAMPLES = """ |
| 179 |
--- |
| 180 |
- name: Enable Secure Boot. |
| 181 |
dellemc.openmanage.idrac_secure_boot: |
| 182 |
idrac_ip: "192.168.1.2" |
| 183 |
idrac_user: "user" |
| 184 |
idrac_password: "password" |
| 185 |
ca_path: "/path/to/ca_cert.pem" |
| 186 |
secure_boot: "Enabled" |
| 187 |
|
| 188 |
- name: Set Secure Boot mode, Secure Boot policy, and restart iDRAC. |
| 189 |
dellemc.openmanage.idrac_secure_boot: |
| 190 |
idrac_ip: "192.168.1.2" |
| 191 |
idrac_user: "user" |
| 192 |
idrac_password: "password" |
| 193 |
ca_path: "/path/to/ca_cert.pem" |
| 194 |
secure_boot: "Enabled" |
| 195 |
secure_boot_mode: "UserMode" |
| 196 |
secure_boot_policy: "Custom" |
| 197 |
restart: true |
| 198 |
restart_type: "GracefulRestart" |
| 199 |
|
| 200 |
- name: Reset Secure Boot certificates. |
| 201 |
dellemc.openmanage.idrac_secure_boot: |
| 202 |
idrac_ip: "192.168.1.2" |
| 203 |
idrac_user: "user" |
| 204 |
idrac_password: "password" |
| 205 |
ca_path: "/path/to/ca_cert.pem" |
| 206 |
reset_keys: "ResetAllKeysToDefault" |
| 207 |
|
| 208 |
- name: Export multiple Secure Boot certificate. |
| 209 |
dellemc.openmanage.idrac_secure_boot: |
| 210 |
idrac_ip: "192.168.1.2" |
| 211 |
idrac_user: "user" |
| 212 |
idrac_password: "password" |
| 213 |
ca_path: "/path/to/ca_cert.pem" |
| 214 |
export_certificates: true |
| 215 |
platform_key: /user/name/export_cert/pk |
| 216 |
KEK: |
| 217 |
- /user/name/export_cert/kek |
| 218 |
database: |
| 219 |
- /user/name/export_cert/db |
| 220 |
disallow_database: |
| 221 |
- /user/name/export_cert/dbx |
| 222 |
|
| 223 |
- name: Import multiple Secure Boot certificate without applying to iDRAC. |
| 224 |
dellemc.openmanage.idrac_secure_boot: |
| 225 |
idrac_ip: "192.168.1.2" |
| 226 |
idrac_user: "user" |
| 227 |
idrac_password: "password" |
| 228 |
ca_path: "/path/to/ca_cert.pem" |
| 229 |
import_certificates: true |
| 230 |
platform_key: /user/name/certificates/pk.pem |
| 231 |
KEK: |
| 232 |
- /user/name/certificates/kek1.pem |
| 233 |
- /user/name/certificates/kek2.pem |
| 234 |
database: |
| 235 |
- /user/name/certificates/db1.pem |
| 236 |
- /user/name/certificates/db2.pem |
| 237 |
disallow_database: |
| 238 |
- /user/name/certificates/dbx1.pem |
| 239 |
- /user/name/certificates/dbx2.pem |
| 240 |
|
| 241 |
- name: Import a Secure Boot certificate and restart the server to apply it. |
| 242 |
dellemc.openmanage.idrac_secure_boot: |
| 243 |
idrac_ip: "192.168.1.2" |
| 244 |
idrac_user: "user" |
| 245 |
idrac_password: "password" |
| 246 |
ca_path: "/path/to/ca_cert.pem" |
| 247 |
import_certificates: true |
| 248 |
platform_key: /user/name/certificates/pk.pem |
| 249 |
restart: true |
| 250 |
job_wait_timeout: 600 |
| 251 |
""" |
| 252 |
|
| 253 |
RETURN = r''' |
| 254 |
--- |
| 255 |
msg: |
| 256 |
description: Status of the secure boot operation. |
| 257 |
returned: always |
| 258 |
type: str |
| 259 |
sample: "Successfully imported the SecureBoot certificate." |
| 260 |
error_info: |
| 261 |
description: Details of the HTTP Error. |
| 262 |
returned: on HTTP error |
| 263 |
type: dict |
| 264 |
sample: { |
| 265 |
"error": { |
| 266 |
"code": "Base.1.0.GeneralError", |
| 267 |
"message": "A general error has occurred. See ExtendedInfo for more information.", |
| 268 |
"@Message.ExtendedInfo": [ |
| 269 |
{ |
| 270 |
"MessageId": "GEN1234", |
| 271 |
"RelatedProperties": [], |
| 272 |
"Message": "Unable to process the request because an error occurred.", |
| 273 |
"MessageArgs": [], |
| 274 |
"Severity": "Critical", |
| 275 |
"Resolution": "Retry the operation. If the issue persists, contact your system administrator." |
| 276 |
} |
| 277 |
] |
| 278 |
} |
| 279 |
} |
| 280 |
''' |
| 281 |
|
| 282 |
import os |
| 283 |
import json |
| 284 |
from ansible.module_utils.common.dict_transformations import recursive_diff |
| 285 |
from urllib.error import HTTPError, URLError |
| 286 |
from ansible.module_utils.compat.version import LooseVersion |
| 287 |
from ansible.module_utils.urls import ConnectionError, SSLValidationError |
| 288 |
from ansible_collections.dellemc.openmanage.plugins.module_utils.idrac_redfish import iDRACRedfishAPI, IdracAnsibleModule |
| 289 |
from ansible_collections.dellemc.openmanage.plugins.module_utils.utils import ( |
| 290 |
get_dynamic_uri, remove_key, validate_and_get_first_resource_id_uri, |
| 291 |
trigger_restart_operation, wait_for_lc_status, get_lc_log_or_current_log_time, |
| 292 |
cert_file_format_string, strip_substr_dict, idrac_redfish_job_tracking, reset_host) |
| 293 |
|
| 294 |
SYSTEMS_URI = "/redfish/v1/Systems" |
| 295 |
IDRAC_MANAGER_URI = "/redfish/v1/Managers/iDRAC.Embedded.1" |
| 296 |
IDRAC_JOBS_URI = IDRAC_MANAGER_URI + "/Oem/Dell/Jobs" |
| 297 |
iDRAC_JOB_URI = IDRAC_JOBS_URI + "/{job_id}" |
| 298 |
iDRAC_JOBS_EXP = IDRAC_JOBS_URI + "?$expand=*($levels=1)" |
| 299 |
BIOS_JOB_EXISTS = "BIOS Configuration job already exists." |
| 300 |
TIME_FORMAT = "%Y%m%d_%H%M%S" |
| 301 |
TIMEOUT_NEGATIVE_OR_ZERO_MSG = "The value for the 'job_wait_timeout' parameter cannot be negative or zero." |
| 302 |
SUCCESS_MSG = "Successfully imported the SecureBoot certificate." |
| 303 |
NO_OPERATION_SKIP = "Task is skipped because no operation is selected." |
| 304 |
PROVIDE_ABSOLUTE_PATH = "Please provide absolute path of the certificate file {path}." |
| 305 |
NO_READ_PERMISSION_PATH = "Unable to read the certificate file {path}." |
| 306 |
NO_FILE_FOUND = "Unable to find the certificate file {path}." |
| 307 |
NO_VALID_PATHS = "No valid absolute path found for certificate(s)." |
| 308 |
HOST_RESTART_FAILED = "Unable to restart the host system. Check the host status and restart the host system manually." |
| 309 |
SUCCESS_COMPLETE = "Successfully updated the {partial} iDRAC Secure Boot settings." |
| 310 |
SCHEDULED_SUCCESS = "Successfully scheduled the job and initiated the restart operation for {partial} iDRAC Secure Boot settings update." |
| 311 |
COMMITTED_SUCCESS = "Successfully committed {partial} changes. The job is in pending state, and changes will be effective at the next reboot." |
| 312 |
RESET_TRIGGERRED = "Reset BIOS action triggered successfully." |
| 313 |
CHANGES_FOUND = 'Changes found to be applied.' |
| 314 |
SCHEDULED_AND_RESTARTED = "Successfully scheduled the boot certificate import operation and restarted the server." |
| 315 |
FAILED_IMPORT = "Failed to import certificate file {path} for {parameter}." |
| 316 |
NO_IMPORT_SUCCESS = "The Secure Boot Certificate Import operation was not successful." |
| 317 |
NO_RESET_KEYS_SUCCESS = "The Secure Boot Reset Certificates operation was not successful." |
| 318 |
NO_DIRECTORY_FOUND = "Unable to find the directory {path}." |
| 319 |
NO_WRITE_PERMISSION_PATH = 'Unable to write to the directory {path}.' |
| 320 |
TOO_MANY_DIRECTORIES = 'More than one directory found for parameter {key}.' |
| 321 |
CERT_IS_EMPTY = 'Certificate string is empty in {file_name} for {parameter}.' |
| 322 |
SUCCESS_EXPORT_MSG = 'Successfully exported the SecureBoot certificate.' |
| 323 |
UNSUCCESSFUL_EXPORT_MSG = 'Failed to export the SecureBoot certificate.' |
| 324 |
NO_CHANGES_FOUND = 'No changes found to be applied.' |
| 325 |
INVALID_RESET_KEY = "The Reset key {reset_key_op} entered is not applicable. The supported values are {supported_values}. " \ |
| 326 |
"Enter a valid Reset key and retry the operation." |
| 327 |
SUCCESS_MSG_RESET = "The {reset_key_op} operation is successfully completed and restarted the host system." |
| 328 |
SUCCESS_RESET_KEYS_RESTARTED = "The {reset_key_op} operation is successfully completed and initiated the restart operation for the host system. " \ |
| 329 |
"Ensure to wait for the host system to be available." |
| 330 |
SCHEDULED_RESET_KEYS = "The {reset_key_op} operation is successfully completed. To apply the updates, restart the host system manually." |
| 331 |
odata = '@odata.id' |
| 332 |
OPERATION_NOT_SUPPORTED = "{op} is not supported on this firmware version of iDRAC." |
| 333 |
SECURE_BOOT_NOT_FOUND = "Secure Boot operations are not supported on this iDRAC." |
| 334 |
NO_KEY_FOUND = "The entered {secure_boot_key} is not supported on this host." |
| 335 |
LEGACY_BIOS_UNSUPPORTED_MSG = "boot mode and Force int 10 are not supported for 17G and later." |
| 336 |
FAILED_RESET_KEYS = "Failed to complete the Reset Certificates operation using {reset_key_op}. Retry the operation." |
| 337 |
MESSAGE_EXTENDED_INFO = "@Message.ExtendedInfo" |
| 338 |
NO_SECURE_BOOT_SUCCESS = "Unable to update the iDRAC Secure Boot settings because the entered parameter is not supported." |
| 339 |
FAILED_BIOS_JOB = "Failed to update the iDRAC Secure Boot settings. Retry the operation." |
| 340 |
NOT_UPDATED_ATTRIBUTES = "Unable to update the following BIOS attributes: {attribute_list}. This may be because the attribute is in " \ |
| 341 |
"read-only mode, its value depends on other attributes, or an incorrect value was provided." |
| 342 |
SYS_CODES = ["SYS410", "SYS409"] |
| 343 |
|
| 344 |
|
| 345 |
class IDRACSecureBoot: |
| 346 |
|
| 347 |
def __init__(self, idrac: iDRACRedfishAPI, module: IdracAnsibleModule): |
| 348 |
self.module: IdracAnsibleModule = module |
| 349 |
self.idrac: iDRACRedfishAPI = idrac |
| 350 |
plt_key = self.module.params.get('platform_key') |
| 351 |
self.platform_key = [plt_key] if plt_key else [] |
| 352 |
self.KEK = self.module.params.get( |
| 353 |
'KEK') or [] |
| 354 |
self.database = self.module.params.get('database') or [] |
| 355 |
self.disallow_database = self.module.params.get( |
| 356 |
'disallow_database') or [] |
| 357 |
self.uri_mapping = {'database': 'db', |
| 358 |
'KEK': 'KEK', |
| 359 |
'disallow_database': 'dbx', |
| 360 |
'platform_key': 'PK'} |
| 361 |
|
| 362 |
def validate_job_wait(self): |
| 363 |
""" |
| 364 |
Validates job_wait and job_wait_timeout parameters. |
| 365 |
""" |
| 366 |
if self.module.params.get('job_wait') and self.module.params.get('job_wait_timeout') <= 0: |
| 367 |
self.module.exit_json( |
| 368 |
msg=TIMEOUT_NEGATIVE_OR_ZERO_MSG, failed=True) |
| 369 |
|
| 370 |
def get_dynamic_secure_boot_database_uri(self): |
| 371 |
secure_boot_database_uri = None |
| 372 |
uri, error_msg = validate_and_get_first_resource_id_uri( |
| 373 |
self.module, self.idrac, SYSTEMS_URI) |
| 374 |
if error_msg: |
| 375 |
self.module.exit_json(msg=error_msg, failed=True) |
| 376 |
resp_secure_boot_uri = get_dynamic_uri( |
| 377 |
self.idrac, uri, 'SecureBoot') |
| 378 |
if not resp_secure_boot_uri: |
| 379 |
self.module.exit_json(msg=SECURE_BOOT_NOT_FOUND, skipped=True) |
| 380 |
self.secure_boot_uri = resp_secure_boot_uri[odata] |
| 381 |
resp_secure_boot = get_dynamic_uri( |
| 382 |
self.idrac, self.secure_boot_uri, 'SecureBootDatabases') |
| 383 |
if resp_secure_boot: |
| 384 |
secure_boot_database_uri = resp_secure_boot[odata] |
| 385 |
return secure_boot_database_uri |
| 386 |
|
| 387 |
def mapping_secure_boot_database_uri(self): |
| 388 |
mapped_value = {} |
| 389 |
secure_boot_database_uri = self.get_dynamic_secure_boot_database_uri() |
| 390 |
if secure_boot_database_uri: |
| 391 |
secure_boot_database_members = get_dynamic_uri( |
| 392 |
self.idrac, secure_boot_database_uri, 'Members') |
| 393 |
for each_member in secure_boot_database_members: |
| 394 |
for label, last_uri_leaf in self.uri_mapping.items(): |
| 395 |
uri = each_member.get(odata) |
| 396 |
if last_uri_leaf == uri.split('/')[-1]: |
| 397 |
mapped_value.update({label: uri}) |
| 398 |
break |
| 399 |
return mapped_value |
| 400 |
|
| 401 |
def get_dynamic_attribute_uri(self): |
| 402 |
self.uri, error_msg = validate_and_get_first_resource_id_uri( |
| 403 |
self.module, self.idrac, SYSTEMS_URI) |
| 404 |
if error_msg: |
| 405 |
self.module.exit_json(msg=error_msg, failed=True) |
| 406 |
self.bios_uri = get_dynamic_uri( |
| 407 |
self.idrac, self.uri, 'Bios')[odata] |
| 408 |
|
| 409 |
def get_current_attributes(self): |
| 410 |
self.get_dynamic_attribute_uri() |
| 411 |
curr_attributes_res = get_dynamic_uri( |
| 412 |
self.idrac, self.bios_uri) |
| 413 |
self.curr_attributes_val = curr_attributes_res.get("Attributes", {}) |
| 414 |
self.bios_setting_uri = curr_attributes_res.get("@Redfish.Settings").get('SettingsObject').get(odata) |
| 415 |
|
| 416 |
def perform_restart(self, success_codes, current_time, op, unsuccessful_codes=None): |
| 417 |
resp, _err_msg = trigger_restart_operation(self.idrac, |
| 418 |
self.module.params.get('restart_type')) |
| 419 |
job_wait = self.module.params.get('job_wait') |
| 420 |
if self.module.params.get('import_certificates') and op != 'import_certificates': |
| 421 |
job_wait = True |
| 422 |
if resp.success: |
| 423 |
if job_wait: |
| 424 |
lc_completed, error_msg = wait_for_lc_status( |
| 425 |
self.idrac, self.module.params.get('job_wait_timeout')) |
| 426 |
self.operation_after_lc_status_check(success_codes, current_time, lc_completed, error_msg, op, unsuccessful_codes) |
| 427 |
else: |
| 428 |
scheduled_msg = { |
| 429 |
'import_certificates': SCHEDULED_AND_RESTARTED, |
| 430 |
'reset_keys': SUCCESS_RESET_KEYS_RESTARTED.format(reset_key_op=self.module.params.get('reset_keys')) |
| 431 |
} |
| 432 |
self.module.exit_json(msg=scheduled_msg.get(op)) |
| 433 |
else: |
| 434 |
self.module.exit_json(msg=HOST_RESTART_FAILED, skipped=True) |
| 435 |
|
| 436 |
def operation_after_lc_status_check(self, success_codes, current_time, lc_completed, error_msg, op, unsuccessful_codes): |
| 437 |
success_msgs = { |
| 438 |
'import_certificates': SUCCESS_MSG, |
| 439 |
'reset_keys': SUCCESS_MSG_RESET.format(reset_key_op=self.module.params.get('reset_keys')) |
| 440 |
} |
| 441 |
no_success = { |
| 442 |
'import_certificates': NO_IMPORT_SUCCESS, |
| 443 |
'reset_keys': NO_RESET_KEYS_SUCCESS |
| 444 |
} |
| 445 |
if lc_completed: |
| 446 |
if unsuccessful_codes: |
| 447 |
lc_log, _msg = get_lc_log_or_current_log_time(self.idrac, current_time, unsuccessful_codes) |
| 448 |
if lc_log: |
| 449 |
self.module.exit_json(msg=NO_RESET_KEYS_SUCCESS, failed=True) |
| 450 |
lc_log, _msg = get_lc_log_or_current_log_time(self.idrac, current_time, success_codes) |
| 451 |
if lc_log: |
| 452 |
if not (op == "reset_keys" and self.module.params.get('import_certificates')) or op == "import_certificates": |
| 453 |
self.module.exit_json(msg=success_msgs.get(op), changed=True) |
| 454 |
else: |
| 455 |
self.module.exit_json(msg=no_success.get(op), |
| 456 |
skipped=True) |
| 457 |
else: |
| 458 |
self.module.exit_json(msg=error_msg, failed=True) |
| 459 |
|
| 460 |
|
| 461 |
class IDRACImportSecureBoot(IDRACSecureBoot): |
| 462 |
|
| 463 |
def __init__(self, idrac, module): |
| 464 |
super().__init__(idrac, module) |
| 465 |
|
| 466 |
def validate_certificate_paths(self, paths): |
| 467 |
""" |
| 468 |
Validate certificate paths |
| 469 |
""" |
| 470 |
invalid_paths = set() |
| 471 |
for path in paths: |
| 472 |
if path and not os.path.isabs(path): |
| 473 |
self.module.warn(PROVIDE_ABSOLUTE_PATH.format(path=path)) |
| 474 |
invalid_paths.add(path) |
| 475 |
if path and not os.access(path=path, mode=os.R_OK): |
| 476 |
self.module.warn(NO_READ_PERMISSION_PATH.format(path=path)) |
| 477 |
invalid_paths.add(path) |
| 478 |
if path and not os.path.isfile(path): |
| 479 |
self.module.warn(NO_FILE_FOUND.format(path=path)) |
| 480 |
invalid_paths.add(path) |
| 481 |
return list(set(paths) - invalid_paths) |
| 482 |
|
| 483 |
def filter_invalid_paths(self): |
| 484 |
""" |
| 485 |
Filter invalid paths |
| 486 |
""" |
| 487 |
self.platform_key = self.validate_certificate_paths(self.platform_key) |
| 488 |
self.KEK = self.validate_certificate_paths( |
| 489 |
self.KEK) |
| 490 |
self.database = self.validate_certificate_paths(self.database) |
| 491 |
self.disallow_database = self.validate_certificate_paths( |
| 492 |
self.disallow_database) |
| 493 |
|
| 494 |
def read_certificate_file(self, path): |
| 495 |
with open(path, 'r') as f: |
| 496 |
return f.read() |
| 497 |
|
| 498 |
def create_dictionary_payload(self, paths): |
| 499 |
result = [] |
| 500 |
for each_path in paths: |
| 501 |
tmp = {'cert_data': self.read_certificate_file(each_path), |
| 502 |
'path': each_path} |
| 503 |
result.append(tmp) |
| 504 |
return result |
| 505 |
|
| 506 |
def construct_payload(self): |
| 507 |
""" |
| 508 |
Construct payload |
| 509 |
""" |
| 510 |
payload = {} |
| 511 |
if self.platform_key: |
| 512 |
payload['platform_key'] = self.create_dictionary_payload( |
| 513 |
self.platform_key) |
| 514 |
if self.KEK: |
| 515 |
payload['KEK'] = self.create_dictionary_payload( |
| 516 |
self.KEK) |
| 517 |
if self.database: |
| 518 |
payload['database'] = self.create_dictionary_payload(self.database) |
| 519 |
if self.disallow_database: |
| 520 |
payload['disallow_database'] = self.create_dictionary_payload( |
| 521 |
self.disallow_database) |
| 522 |
return payload |
| 523 |
|
| 524 |
def looping_over_parameters(self, payload_values, uri): |
| 525 |
for parameter, payload_list in payload_values.items(): |
| 526 |
for each_payload in payload_list: |
| 527 |
payload = {"CertificateString": each_payload['cert_data'], |
| 528 |
"CertificateType": "PEM"} |
| 529 |
cert_uri = get_dynamic_uri( |
| 530 |
self.idrac, uri[parameter], 'Certificates')[odata] |
| 531 |
try: |
| 532 |
self.idrac.invoke_request( |
| 533 |
method='POST', uri=cert_uri, data=payload) |
| 534 |
except HTTPError: |
| 535 |
self.module.warn(FAILED_IMPORT.format( |
| 536 |
parameter=parameter, path=each_payload['path'])) |
| 537 |
|
| 538 |
def perform_operation(self): |
| 539 |
""" |
| 540 |
Perform operation |
| 541 |
""" |
| 542 |
success_codes = ["UEFI0286"] |
| 543 |
scheduled_code = ["SWC9010"] |
| 544 |
self.filter_invalid_paths() |
| 545 |
self.validate_job_wait() |
| 546 |
payload_values = self.construct_payload() |
| 547 |
if not payload_values: |
| 548 |
self.module.exit_json(msg=NO_VALID_PATHS, skipped=True) |
| 549 |
|
| 550 |
if self.module.check_mode: |
| 551 |
self.module.exit_json(msg=CHANGES_FOUND, changed=True) |
| 552 |
|
| 553 |
uri = self.mapping_secure_boot_database_uri() |
| 554 |
if uri: |
| 555 |
current_time = get_lc_log_or_current_log_time(self.idrac) |
| 556 |
self.looping_over_parameters(payload_values, uri) |
| 557 |
lc_log, scheduled_msg = get_lc_log_or_current_log_time( |
| 558 |
self.idrac, current_time, scheduled_code) |
| 559 |
if not lc_log: |
| 560 |
self.module.exit_json(msg=NO_IMPORT_SUCCESS, skipped=True) |
| 561 |
if self.module.params.get('restart'): |
| 562 |
self.perform_restart(success_codes, current_time, 'import_certificates') |
| 563 |
self.module.exit_json(msg=scheduled_msg) |
| 564 |
self.module.exit_json(msg=OPERATION_NOT_SUPPORTED.format(op='import_certificates'), skipped=True) |
| 565 |
|
| 566 |
|
| 567 |
class IDRACResetCertificates(IDRACSecureBoot): |
| 568 |
|
| 569 |
def __init__(self, idrac, module): |
| 570 |
super().__init__(idrac, module) |
| 571 |
self.reset_keys = self.module.params.get('reset_keys') |
| 572 |
self.import_op = self.module.params.get('import_certificates') |
| 573 |
|
| 574 |
def validate_allowed_reset_keys(self, allowed_values): |
| 575 |
if self.reset_keys not in allowed_values: |
| 576 |
self.module.exit_json(msg=INVALID_RESET_KEY.format(reset_key_op=self.reset_keys, |
| 577 |
supported_values=', '.join(map(str, allowed_values))), skipped=True) |
| 578 |
|
| 579 |
def fetch_allowed_values_reset_uri(self): |
| 580 |
self.get_dynamic_secure_boot_database_uri() |
| 581 |
resp = get_dynamic_uri( |
| 582 |
self.idrac, self.secure_boot_uri, 'Actions') |
| 583 |
secure_boot_resp = resp.get("#SecureBoot.ResetKeys") |
| 584 |
reset_allowable_values = secure_boot_resp.get("[email protected]") |
| 585 |
reset_key_uri = secure_boot_resp.get("target") |
| 586 |
self.validate_allowed_reset_keys(reset_allowable_values) |
| 587 |
return reset_key_uri |
| 588 |
|
| 589 |
def perform_operation(self): |
| 590 |
""" |
| 591 |
Perform operation |
| 592 |
""" |
| 593 |
success_codes = ["UEFI0074", "UEFI0286"] |
| 594 |
unsuccessful_codes = ["UEFI0285", "UEFI0423"] |
| 595 |
scheduled_code = ["SWC9007", "SWC9008", "SWC9009", "SWC9012"] |
| 596 |
self.validate_job_wait() |
| 597 |
reset_key_uri = self.fetch_allowed_values_reset_uri() |
| 598 |
payload = {"ResetKeysType": self.reset_keys} |
| 599 |
self.get_current_attributes() |
| 600 |
secure_boot_policy = self.curr_attributes_val.get("SecureBootPolicy") |
| 601 |
if self.module.check_mode: |
| 602 |
if secure_boot_policy == 'Standard': |
| 603 |
self.module.exit_json(msg=NO_CHANGES_FOUND) |
| 604 |
self.module.exit_json(msg=CHANGES_FOUND, changed=True) |
| 605 |
current_time = get_lc_log_or_current_log_time(self.idrac) |
| 606 |
resp = self.idrac.invoke_request(reset_key_uri, "POST", data=payload) |
| 607 |
if resp.success: |
| 608 |
lc_log, _scheduled_msg = get_lc_log_or_current_log_time( |
| 609 |
self.idrac, current_time, unsuccessful_codes) |
| 610 |
if lc_log: |
| 611 |
self.module.exit_json(msg=NO_RESET_KEYS_SUCCESS, skipped=True) |
| 612 |
lc_log, _scheduled_msg = get_lc_log_or_current_log_time( |
| 613 |
self.idrac, current_time, scheduled_code) |
| 614 |
if not lc_log: |
| 615 |
self.module.exit_json(msg=NO_RESET_KEYS_SUCCESS, skipped=True) |
| 616 |
reboot_required = self.module.params.get('restart') |
| 617 |
if self.import_op: |
| 618 |
reboot_required = True |
| 619 |
current_time = get_lc_log_or_current_log_time(self.idrac) |
| 620 |
if reboot_required: |
| 621 |
self.perform_restart(success_codes, current_time, 'reset_keys', unsuccessful_codes) |
| 622 |
else: |
| 623 |
self.module.exit_json(msg=SCHEDULED_RESET_KEYS.format(reset_key_op=self.reset_keys), changed=True) |
| 624 |
else: |
| 625 |
self.module.exit_json(msg=FAILED_RESET_KEYS.format(reset_key_op=self.reset_keys), failed=True) |
| 626 |
|
| 627 |
|
| 628 |
def get_server_generation(idrac: iDRACRedfishAPI): |
| 629 |
""" |
| 630 |
Function wrapping idrac.get_server_generation. Helps with mocked testing. |
| 631 |
|
| 632 |
Args: |
| 633 |
idrac (iDRACRedfishAPI): iDRACRedfishAPI object. |
| 634 |
|
| 635 |
Returns: |
| 636 |
Tuple[int, str, str]: A tuple containing the server generation, firmware version, and hardware model. |
| 637 |
""" |
| 638 |
return idrac.get_server_generation |
| 639 |
|
| 640 |
|
| 641 |
class IDRACAttributes(IDRACSecureBoot): |
| 642 |
|
| 643 |
def __init__(self, idrac: iDRACRedfishAPI, module: IdracAnsibleModule): |
| 644 |
super().__init__(idrac, module) |
| 645 |
self.boot_mode = self.module.params.get('boot_mode') |
| 646 |
self.secure_boot = self.module.params.get('secure_boot') |
| 647 |
self.secure_boot_mode = self.module.params.get('secure_boot_mode') |
| 648 |
self.secure_boot_policy = self.module.params.get('secure_boot_policy') |
| 649 |
self.force_int_10 = self.module.params.get('force_int_10') |
| 650 |
|
| 651 |
server_generation_op = get_server_generation(self.idrac) |
| 652 |
self.generation, self.idrac_firmware_version = server_generation_op[0], server_generation_op[1] |
| 653 |
|
| 654 |
def check_scheduled_bios_job(self): |
| 655 |
job_resp = self.idrac.invoke_request(iDRAC_JOBS_EXP, "GET") |
| 656 |
job_list = job_resp.json_data.get('Members', []) |
| 657 |
sch_jb = None |
| 658 |
for jb in job_list: |
| 659 |
if jb.get("JobType") == "BIOSConfiguration" and jb.get("JobState") in ["Scheduled", "Running", "Starting"]: |
| 660 |
sch_jb = jb['Id'] |
| 661 |
break |
| 662 |
return sch_jb |
| 663 |
|
| 664 |
def get_pending_attributes(self): |
| 665 |
try: |
| 666 |
resp = self.idrac.invoke_request(self.bios_setting_uri, "GET") |
| 667 |
attr = resp.json_data.get("Attributes") |
| 668 |
except Exception: |
| 669 |
attr = {} |
| 670 |
return attr |
| 671 |
|
| 672 |
def trigger_bios_job(self): |
| 673 |
job_id = None |
| 674 |
payload = {"TargetSettingsURI": self.bios_setting_uri} |
| 675 |
resp = self.idrac.invoke_request(IDRAC_JOBS_URI, "POST", data=payload) |
| 676 |
job_id = resp.headers["Location"].split("/")[-1] |
| 677 |
return job_id |
| 678 |
|
| 679 |
def apply_attributes(self, pending): |
| 680 |
payload = {"Attributes": pending} |
| 681 |
reboot_required = False |
| 682 |
partial_update = False |
| 683 |
resp = self.idrac.invoke_request(self.bios_setting_uri, "PATCH", data=payload) |
| 684 |
if resp.success: |
| 685 |
resp_body = resp.json_data.get(MESSAGE_EXTENDED_INFO) |
| 686 |
filtered_data = [item.get("MessageArgs") for item in resp_body |
| 687 |
if any(code in item.get("MessageId", "") for code in SYS_CODES)] |
| 688 |
if filtered_data: |
| 689 |
self.module.warn(NOT_UPDATED_ATTRIBUTES.format(attribute_list=', '.join(map(str, filtered_data)))) |
| 690 |
partial_update = True |
| 691 |
if self.module.params.get('restart'): |
| 692 |
reboot_required = True |
| 693 |
job_id = self.trigger_bios_job() |
| 694 |
return job_id, reboot_required, partial_update |
| 695 |
self.module.exit_json(msg=FAILED_BIOS_JOB, failed=True) |
| 696 |
|
| 697 |
def compare_attr_val(self, curr_attr): |
| 698 |
""" |
| 699 |
Compare the current attributes with the new attributes. |
| 700 |
|
| 701 |
Parameters: |
| 702 |
curr_attr (dict): The current attributes. |
| 703 |
|
| 704 |
Returns: |
| 705 |
dict: A dictionary containing the attributes that are different. |
| 706 |
""" |
| 707 |
new_payload = {"BootMode": self.boot_mode, "SecureBoot": self.secure_boot, |
| 708 |
"SecureBootMode": self.secure_boot_mode, "SecureBootPolicy": self.secure_boot_policy, |
| 709 |
"ForceInt10": self.force_int_10} |
| 710 |
new_attr = {k: v for k, v in new_payload.items() if v is not None} |
| 711 |
fetched_dict = {} |
| 712 |
attributes = list(new_attr.keys()) |
| 713 |
for key in attributes: |
| 714 |
if key in curr_attr: |
| 715 |
fetched_dict[key] = curr_attr[key] |
| 716 |
else: |
| 717 |
new_attr.pop(key) |
| 718 |
self.module.warn(NO_KEY_FOUND.format(secure_boot_key=key)) |
| 719 |
if not fetched_dict: |
| 720 |
self.module.exit_json(msg=NO_SECURE_BOOT_SUCCESS, skipped=True) |
| 721 |
diff_tuple = recursive_diff(new_attr, fetched_dict) |
| 722 |
attr = {} |
| 723 |
if diff_tuple and diff_tuple[0]: |
| 724 |
attr = diff_tuple[0] |
| 725 |
return attr |
| 726 |
|
| 727 |
def attributes_config(self): |
| 728 |
curr_attr = self.curr_attributes_val |
| 729 |
attr = self.compare_attr_val(curr_attr) |
| 730 |
self.import_op = self.module.params.get('import_certificates') |
| 731 |
self.export_op = self.module.params.get('export_certificates') |
| 732 |
self.reset_keys = self.module.params.get('reset_keys') |
| 733 |
if not attr: |
| 734 |
if self.import_op or self.export_op or self.reset_keys: |
| 735 |
return |
| 736 |
elif self.module.check_mode: |
| 737 |
self.module.exit_json(msg=NO_CHANGES_FOUND, changed=False) |
| 738 |
self.module.exit_json(msg=NO_CHANGES_FOUND) |
| 739 |
if self.module.check_mode: |
| 740 |
self.module.exit_json(msg=CHANGES_FOUND, changed=True) |
| 741 |
self.update_pending_attributes(attr) |
| 742 |
|
| 743 |
def update_pending_attributes(self, attr): |
| 744 |
pending = self.get_pending_attributes() |
| 745 |
pending.update(attr) |
| 746 |
if pending: |
| 747 |
self.handle_scheduled_bios_job() |
| 748 |
self.apply_attributes_and_exit_json(attr) |
| 749 |
|
| 750 |
def handle_scheduled_bios_job(self): |
| 751 |
job_id = self.check_scheduled_bios_job() |
| 752 |
if job_id: |
| 753 |
self.module.exit_json(msg=BIOS_JOB_EXISTS, job_id=job_id, |
| 754 |
skipped=True) |
| 755 |
|
| 756 |
def apply_attributes_and_exit_json(self, attr): |
| 757 |
reboot_required = False |
| 758 |
job_id, reboot_required, partial_update = self.apply_attributes(attr) |
| 759 |
job_wait = self.module.params.get("job_wait") |
| 760 |
job_resp = self.idrac.invoke_request(iDRAC_JOB_URI.format(job_id=job_id), 'GET') |
| 761 |
job_dict = job_resp.json_data |
| 762 |
if self.import_op or self.export_op or self.reset_keys: |
| 763 |
reboot_required = True |
| 764 |
job_wait = True |
| 765 |
if reboot_required and job_id: |
| 766 |
restart_type = self.module.params.get('restart_type') |
| 767 |
reset_success = reset_host(self.idrac, restart_type, SYSTEMS_URI, self.uri) |
| 768 |
if not reset_success: |
| 769 |
self.module.exit_json(msg=HOST_RESTART_FAILED, |
| 770 |
failed=True) |
| 771 |
if job_wait: |
| 772 |
self.handle_job_wait(job_id, partial_update) |
| 773 |
return |
| 774 |
else: |
| 775 |
resp_msg = SCHEDULED_SUCCESS.format(partial='partial' if partial_update |
| 776 |
else '').replace(" ", " ") |
| 777 |
self.module.exit_json(msg=resp_msg, job_status=strip_substr_dict(job_dict), changed=True) |
| 778 |
resp_msg = COMMITTED_SUCCESS.format(partial='partial' if partial_update |
| 779 |
else '').replace(" ", " ") |
| 780 |
self.module.exit_json(msg=resp_msg, |
| 781 |
job_status=strip_substr_dict(job_dict), changed=True) |
| 782 |
|
| 783 |
def handle_job_wait(self, job_id, partial_update): |
| 784 |
job_failed, msg, job_dict, _wait_time = idrac_redfish_job_tracking( |
| 785 |
self.idrac, iDRAC_JOB_URI.format(job_id=job_id), |
| 786 |
max_job_wait_sec=self.module.params.get('job_wait_timeout')) |
| 787 |
if job_failed: |
| 788 |
self.module.exit_json(failed=True, msg=msg, job_id=job_id) |
| 789 |
if self.import_op or self.export_op or self.reset_keys: |
| 790 |
return |
| 791 |
resp_msg = SUCCESS_COMPLETE.format(partial='partial' if partial_update |
| 792 |
else '').replace(" ", " ") |
| 793 |
self.module.exit_json(msg=resp_msg, job_status=strip_substr_dict(job_dict), changed=True) |
| 794 |
|
| 795 |
def perform_operation(self): |
| 796 |
""" |
| 797 |
Perform operation |
| 798 |
""" |
| 799 |
self.validate_job_wait() |
| 800 |
if self.generation < 17 and LooseVersion(self.idrac_firmware_version) < '3.0': |
| 801 |
self.module.exit_json(msg=OPERATION_NOT_SUPPORTED.format(op='Secure Boot settings update'), skipped=True) |
| 802 |
if self.generation >= 17: |
| 803 |
|
| 804 |
|
| 805 |
if self.boot_mode is not None or self.force_int_10 is not None: |
| 806 |
self.module.exit_json(msg=LEGACY_BIOS_UNSUPPORTED_MSG, failed=True, changed=False) |
| 807 |
self.get_current_attributes() |
| 808 |
self.attributes_config() |
| 809 |
|
| 810 |
|
| 811 |
class IDRACExportSecureBoot(IDRACSecureBoot): |
| 812 |
|
| 813 |
def __init__(self, idrac, module): |
| 814 |
super().__init__(idrac, module) |
| 815 |
|
| 816 |
def verify_path_is_directory_and_has_write_permission(self, path): |
| 817 |
flag = True |
| 818 |
is_dir_path = os.path.isdir(path) |
| 819 |
if path and not is_dir_path: |
| 820 |
self.module.warn(NO_DIRECTORY_FOUND.format(path=path)) |
| 821 |
flag = False |
| 822 |
if path and is_dir_path and not os.access(path=path, mode=os.W_OK): |
| 823 |
self.module.warn(NO_WRITE_PERMISSION_PATH.format(path=path)) |
| 824 |
flag = False |
| 825 |
return path if flag is True else '' |
| 826 |
|
| 827 |
def validate_key_directory(self, parameter, directories): |
| 828 |
resp = '' |
| 829 |
length_directories = len(directories) |
| 830 |
if length_directories > 1: |
| 831 |
self.module.warn(TOO_MANY_DIRECTORIES.format(key=parameter)) |
| 832 |
if length_directories == 1: |
| 833 |
resp = self.verify_path_is_directory_and_has_write_permission(directories[0]) |
| 834 |
return resp |
| 835 |
|
| 836 |
def filter_invalid_directory(self): |
| 837 |
self.platform_key = self.validate_key_directory('platform_key', self.platform_key) |
| 838 |
self.KEK = self.validate_key_directory('KEK', self.KEK) |
| 839 |
self.database = self.validate_key_directory('database', self.database) |
| 840 |
self.disallow_database = self.validate_key_directory('disallow_database', self.disallow_database) |
| 841 |
return any([self.platform_key, self.KEK, self.database, self.disallow_database]) |
| 842 |
|
| 843 |
def fetch_certificate_file_and_write(self, uri, path, parameter): |
| 844 |
certificates_uri = get_dynamic_uri(self.idrac, uri, 'Certificates')[odata] |
| 845 |
members = get_dynamic_uri(self.idrac, certificates_uri, 'Members') |
| 846 |
for each_member in members: |
| 847 |
tmp_uri = each_member.get(odata) |
| 848 |
file_name = tmp_uri.split('/')[-1] |
| 849 |
resp = get_dynamic_uri(self.idrac, tmp_uri) |
| 850 |
cert_type = resp.get('CertificateType').lower() |
| 851 |
cert_string = resp.get('CertificateString') |
| 852 |
hostname = self.module.params.get('idrac_ip') |
| 853 |
updated_file_name = cert_file_format_string(hostname, postfix=f"_{file_name}") |
| 854 |
file_path = os.path.join(path, f"{updated_file_name}.{cert_type}") |
| 855 |
if cert_string: |
| 856 |
with open(file_path, 'w') as f: |
| 857 |
f.write(cert_string) |
| 858 |
else: |
| 859 |
self.module.warn(CERT_IS_EMPTY.format(file_name=file_name, |
| 860 |
parameter=parameter)) |
| 861 |
|
| 862 |
def perform_operation(self): |
| 863 |
""" |
| 864 |
Perform operation |
| 865 |
""" |
| 866 |
self.validate_job_wait() |
| 867 |
valid_directory = self.filter_invalid_directory() |
| 868 |
if self.module.check_mode: |
| 869 |
if valid_directory: |
| 870 |
self.module.exit_json(msg=CHANGES_FOUND, changed=True) |
| 871 |
elif self.module.params.get('reset_keys') or self.module.params.get('import_certificates'): |
| 872 |
return |
| 873 |
self.module.exit_json(msg=NO_CHANGES_FOUND) |
| 874 |
|
| 875 |
if not valid_directory: |
| 876 |
self.module.exit_json(msg=UNSUCCESSFUL_EXPORT_MSG, failed=True) |
| 877 |
|
| 878 |
uri = self.mapping_secure_boot_database_uri() |
| 879 |
if uri: |
| 880 |
parameter_map = { |
| 881 |
'platform_key': self.platform_key, |
| 882 |
'KEK': self.KEK, |
| 883 |
'database': self.database, |
| 884 |
'disallow_database': self.disallow_database |
| 885 |
} |
| 886 |
for parameter, path in parameter_map.items(): |
| 887 |
if path: |
| 888 |
self.fetch_certificate_file_and_write(uri[parameter], path, |
| 889 |
parameter) |
| 890 |
if self.module.params.get('reset_keys') or self.module.params.get('import_certificates'): |
| 891 |
return |
| 892 |
self.module.exit_json(msg=SUCCESS_EXPORT_MSG) |
| 893 |
self.module.exit_json(msg=OPERATION_NOT_SUPPORTED.format(op='export_certificates'), skipped=True) |
| 894 |
|
| 895 |
|
| 896 |
def main(): |
| 897 |
try: |
| 898 |
specs = { |
| 899 |
"boot_mode": {"type": 'str', "choices": ['Uefi', 'Bios']}, |
| 900 |
"secure_boot": {"type": 'str', "choices": ['Disabled', 'Enabled']}, |
| 901 |
"secure_boot_mode": {"type": 'str', "choices": ['AuditMode', 'DeployedMode', 'UserMode']}, |
| 902 |
"secure_boot_policy": {"type": 'str', "choices": ['Custom', 'Standard']}, |
| 903 |
"force_int_10": {"type": 'str', "choices": ['Disabled', 'Enabled']}, |
| 904 |
"export_certificates": {"type": 'bool'}, |
| 905 |
"import_certificates": {"type": 'bool'}, |
| 906 |
"platform_key": {"type": 'path'}, |
| 907 |
"KEK": {"type": 'list', "elements": 'path'}, |
| 908 |
"database": {"type": 'list', "elements": 'path'}, |
| 909 |
"disallow_database": {"type": 'list', "elements": 'path'}, |
| 910 |
"restart": {"type": 'bool', "default": False}, |
| 911 |
"restart_type": {"type": 'str', "default": "GracefulRestart", |
| 912 |
"choices": ['ForceRestart', 'GracefulRestart']}, |
| 913 |
"reset_keys": {"type": 'str', "choices": ['ResetAllKeysToDefault', 'DeleteAllKeys', 'DeletePK', |
| 914 |
'ResetPK', 'ResetKEK', 'ResetDB', 'ResetDBX']}, |
| 915 |
"job_wait": {"type": 'bool', "default": True}, |
| 916 |
"job_wait_timeout": {"type": 'int', "default": 1200}, |
| 917 |
} |
| 918 |
mutually_exclusive_val = [("import_certificates", "export_certificates")] |
| 919 |
required_if_val = [("import_certificates", True, ("platform_key", "KEK", |
| 920 |
"database", "disallow_database"), True), |
| 921 |
("export_certificates", True, ("platform_key", "KEK", |
| 922 |
"database", "disallow_database"), True)] |
| 923 |
|
| 924 |
module = IdracAnsibleModule(argument_spec=specs, |
| 925 |
required_if=required_if_val, |
| 926 |
mutually_exclusive=mutually_exclusive_val, |
| 927 |
supports_check_mode=True) |
| 928 |
with iDRACRedfishAPI(module.params, req_session=True) as idrac: |
| 929 |
obj = None |
| 930 |
boot_mode = module.params.get('boot_mode') |
| 931 |
secure_boot = module.params.get('secure_boot') |
| 932 |
secure_boot_mode = module.params.get('secure_boot_mode') |
| 933 |
secure_boot_policy = module.params.get('secure_boot_policy') |
| 934 |
force_int_10 = module.params.get('force_int_10') |
| 935 |
class_mapping = { |
| 936 |
'export_certificates': IDRACExportSecureBoot, |
| 937 |
'reset_keys': IDRACResetCertificates, |
| 938 |
'import_certificates': IDRACImportSecureBoot |
| 939 |
} |
| 940 |
if not all(p is None for p in [boot_mode, secure_boot, secure_boot_mode, |
| 941 |
secure_boot_policy, force_int_10]): |
| 942 |
obj = IDRACAttributes(idrac, module) |
| 943 |
obj.perform_operation() |
| 944 |
if any(module.params.get(param) is not None for param in class_mapping.keys()): |
| 945 |
for param, op_class in class_mapping.items(): |
| 946 |
if module.params.get(param): |
| 947 |
obj = op_class(idrac, module) |
| 948 |
obj.perform_operation() |
| 949 |
if obj is None: |
| 950 |
module.exit_json(msg=NO_OPERATION_SKIP, skipped=True) |
| 951 |
except HTTPError as err: |
| 952 |
filter_err = remove_key(json.load(err), regex_pattern='(.*?)@odata') |
| 953 |
message_details = filter_err.get('error').get(MESSAGE_EXTENDED_INFO)[0] |
| 954 |
message_id = message_details.get('MessageId').split(".")[-1] |
| 955 |
skippable_messages = { |
| 956 |
'SYS011': message_details.get('Message'), |
| 957 |
'SYS409': message_details.get('Message'), |
| 958 |
'SYS410': message_details.get('Message'), |
| 959 |
'SYS439': message_details.get('Message') |
| 960 |
} |
| 961 |
if message_id in skippable_messages: |
| 962 |
module.exit_json(msg=skippable_messages[message_id], skipped=True) |
| 963 |
module.exit_json(msg=str(err), error_info=filter_err, failed=True) |
| 964 |
except URLError as err: |
| 965 |
module.exit_json(msg=str(err), unreachable=True) |
| 966 |
except (SSLValidationError, ConnectionError, TypeError, ValueError, OSError) as err: |
| 967 |
module.exit_json(msg=str(err), failed=True) |
| 968 |
|
| 969 |
|
| 970 |
if __name__ == '__main__': |
| 971 |
main() |