| 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 = r""" |
| 18 |
--- |
| 19 |
module: idrac_certificates |
| 20 |
short_description: Configure certificates for iDRAC |
| 21 |
version_added: "5.5.0" |
| 22 |
description: |
| 23 |
- This module allows to generate certificate signing request, import, and export certificates on iDRAC. |
| 24 |
extends_documentation_fragment: |
| 25 |
- dellemc.openmanage.idrac_x_auth_options |
| 26 |
options: |
| 27 |
command: |
| 28 |
description: |
| 29 |
- "C(generate_csr), generate CSR. This requires I(cert_params) and I(certificate_path). |
| 30 |
This is applicable only for C(HTTPS)" |
| 31 |
- C(import), import the certificate file. This requires I(certificate_path). |
| 32 |
- C(export), export the certificate. This requires I(certificate_path). |
| 33 |
- C(reset), reset the certificate to default settings. This is applicable only for C(HTTPS). |
| 34 |
type: str |
| 35 |
choices: [import, export, generate_csr, reset] |
| 36 |
default: 'generate_csr' |
| 37 |
certificate_type: |
| 38 |
description: |
| 39 |
- Type of the iDRAC certificate. |
| 40 |
- C(HTTPS) The Dell self-signed SSL certificate. |
| 41 |
- C(CA) Certificate Authority(CA) signed SSL certificate. |
| 42 |
- C(CUSTOMCERTIFICATE) The custom PKCS12 certificate and private key. Export of custom certificate is supported only on iDRAC firmware version 7.00.00.00 |
| 43 |
and above. |
| 44 |
- C(CSC) The custom signing SSL certificate. |
| 45 |
- C(CLIENT_TRUST_CERTIFICATE) Client trust certificate. |
| 46 |
type: str |
| 47 |
choices: [HTTPS, CA, CUSTOMCERTIFICATE, CSC, CLIENT_TRUST_CERTIFICATE] |
| 48 |
default: 'HTTPS' |
| 49 |
certificate_path: |
| 50 |
description: |
| 51 |
- Absolute path of the certificate file if I(command) is C(import). |
| 52 |
- Directory path with write permissions if I(command) is C(generate_csr) or C(export). |
| 53 |
type: path |
| 54 |
passphrase: |
| 55 |
description: The passphrase string if the certificate to be imported is passphrase protected. |
| 56 |
type: str |
| 57 |
ssl_key: |
| 58 |
description: |
| 59 |
- Absolute path of the private or SSL key file. |
| 60 |
- This is applicable only when I(command) is C(import) and I(certificate_type) is C(HTTPS). |
| 61 |
- Uploading the SSL key to iDRAC is supported on firmware version 6.00.02.00 and above. |
| 62 |
type: path |
| 63 |
version_added: 8.6.0 |
| 64 |
cert_params: |
| 65 |
description: Certificate parameters to generate signing request. |
| 66 |
type: dict |
| 67 |
suboptions: |
| 68 |
common_name: |
| 69 |
description: The common name of the certificate. |
| 70 |
type: str |
| 71 |
required: true |
| 72 |
organization_unit: |
| 73 |
description: The name associated with an organizational unit. For example department name. |
| 74 |
type: str |
| 75 |
required: true |
| 76 |
locality_name: |
| 77 |
description: The city or other location where the entity applying for certification is located. |
| 78 |
type: str |
| 79 |
required: true |
| 80 |
state_name: |
| 81 |
description: The state where the entity applying for certification is located. |
| 82 |
type: str |
| 83 |
required: true |
| 84 |
country_code: |
| 85 |
description: The country code of the country where the entity applying for certification is located. |
| 86 |
type: str |
| 87 |
required: true |
| 88 |
email_address: |
| 89 |
description: The email associated with the CSR. |
| 90 |
type: str |
| 91 |
organization_name: |
| 92 |
description: The name associated with an organization. |
| 93 |
type: str |
| 94 |
required: true |
| 95 |
subject_alt_name: |
| 96 |
description: The alternative domain names associated with the request. |
| 97 |
type: list |
| 98 |
elements: str |
| 99 |
default: [] |
| 100 |
resource_id: |
| 101 |
description: Redfish ID of the resource. |
| 102 |
type: str |
| 103 |
reset: |
| 104 |
description: |
| 105 |
- To reset the iDRAC after the certificate operation. |
| 106 |
- This is applicable when I(command) is C(import) or C(reset). |
| 107 |
type: bool |
| 108 |
default: true |
| 109 |
wait: |
| 110 |
description: |
| 111 |
- Maximum wait time for iDRAC to start after the reset, in seconds. |
| 112 |
- This is applicable when I(command) is C(import) or C(reset) and I(reset) is C(true). |
| 113 |
type: int |
| 114 |
default: 600 |
| 115 |
requirements: |
| 116 |
- "python >= 3.9.6" |
| 117 |
author: |
| 118 |
- "Jagadeesh N V(@jagadeeshnv)" |
| 119 |
- "Rajshekar P(@rajshekarp87)" |
| 120 |
- "Kristian Lamb V(@kristian_lamb)" |
| 121 |
notes: |
| 122 |
- The certificate operations are supported on iDRAC firmware version 6.10.80.00 and above. |
| 123 |
- Run this module from a system that has direct access to Dell iDRAC. |
| 124 |
- This module supports C(check_mode). |
| 125 |
- This module supports IPv4 and IPv6 addresses. |
| 126 |
""" |
| 127 |
|
| 128 |
EXAMPLES = r""" |
| 129 |
--- |
| 130 |
- name: Generate HTTPS certificate signing request |
| 131 |
dellemc.openmanage.idrac_certificates: |
| 132 |
idrac_ip: "192.168.0.1" |
| 133 |
idrac_user: "user_name" |
| 134 |
idrac_password: "user_password" |
| 135 |
ca_path: "/path/to/ca_cert.pem" |
| 136 |
command: "generate_csr" |
| 137 |
certificate_type: "HTTPS" |
| 138 |
certificate_path: "/home/omam/mycerts" |
| 139 |
cert_params: |
| 140 |
common_name: "sample.domain.com" |
| 141 |
organization_unit: "OrgUnit" |
| 142 |
locality_name: "Bangalore" |
| 143 |
state_name: "Karnataka" |
| 144 |
country_code: "IN" |
| 145 |
email_address: "[email protected]" |
| 146 |
organization_name: "OrgName" |
| 147 |
subject_alt_name: |
| 148 |
- 192.198.2.1 |
| 149 |
|
| 150 |
- name: Import a HTTPS certificate. |
| 151 |
dellemc.openmanage.idrac_certificates: |
| 152 |
idrac_ip: "192.168.0.1" |
| 153 |
idrac_user: "user_name" |
| 154 |
idrac_password: "user_password" |
| 155 |
ca_path: "/path/to/ca_cert.pem" |
| 156 |
command: "import" |
| 157 |
certificate_type: "HTTPS" |
| 158 |
certificate_path: "/path/to/cert.pem" |
| 159 |
|
| 160 |
- name: Import an HTTPS certificate along with its private key. |
| 161 |
dellemc.openmanage.idrac_certificates: |
| 162 |
idrac_ip: "192.168.0.1" |
| 163 |
idrac_user: "user_name" |
| 164 |
idrac_password: "user_password" |
| 165 |
ca_path: "/path/to/ca_cert.pem" |
| 166 |
command: "import" |
| 167 |
certificate_type: "HTTPS" |
| 168 |
certificate_path: "/path/to/cert.pem" |
| 169 |
ssl_key: "/path/to/private_key.pem" |
| 170 |
|
| 171 |
- name: Export a HTTPS certificate. |
| 172 |
dellemc.openmanage.idrac_certificates: |
| 173 |
idrac_ip: "192.168.0.1" |
| 174 |
idrac_user: "user_name" |
| 175 |
idrac_password: "user_password" |
| 176 |
ca_path: "/path/to/ca_cert.pem" |
| 177 |
command: "export" |
| 178 |
certificate_type: "HTTPS" |
| 179 |
certificate_path: "/home/omam/mycert_dir" |
| 180 |
|
| 181 |
- name: Import a CSC certificate. |
| 182 |
dellemc.openmanage.idrac_certificates: |
| 183 |
idrac_ip: "192.168.0.1" |
| 184 |
idrac_user: "user_name" |
| 185 |
idrac_password: "user_password" |
| 186 |
ca_path: "/path/to/ca_cert.pem" |
| 187 |
command: "import" |
| 188 |
certificate_type: "CSC" |
| 189 |
certificate_path: "/path/to/cert.pem" |
| 190 |
|
| 191 |
- name: Import a custom certificate with a passphrase. |
| 192 |
dellemc.openmanage.idrac_certificates: |
| 193 |
idrac_ip: "192.168.0.1" |
| 194 |
idrac_user: "user_name" |
| 195 |
idrac_password: "user_password" |
| 196 |
command: "import" |
| 197 |
certificate_type: "CUSTOMCERTIFICATE" |
| 198 |
certificate_path: "/path/to/idrac_cert.p12" |
| 199 |
passphrase: "cert_passphrase" |
| 200 |
reset: false |
| 201 |
|
| 202 |
- name: Export a Client trust certificate. |
| 203 |
dellemc.openmanage.idrac_certificates: |
| 204 |
idrac_ip: "192.168.0.1" |
| 205 |
idrac_user: "user_name" |
| 206 |
idrac_password: "user_password" |
| 207 |
ca_path: "/path/to/ca_cert.pem" |
| 208 |
command: "export" |
| 209 |
certificate_type: "CLIENT_TRUST_CERTIFICATE" |
| 210 |
certificate_path: "/home/omam/mycert_dir" |
| 211 |
""" |
| 212 |
|
| 213 |
RETURN = r''' |
| 214 |
--- |
| 215 |
msg: |
| 216 |
type: str |
| 217 |
description: Status of the certificate configuration operation. |
| 218 |
returned: always |
| 219 |
sample: "Successfully performed the 'generate_csr' certificate operation." |
| 220 |
certificate_path: |
| 221 |
type: str |
| 222 |
description: The csr or exported certificate file path |
| 223 |
returned: when I(command) is C(export) or C(generate_csr) |
| 224 |
sample: "/home/ansible/myfiles/cert.pem" |
| 225 |
error_info: |
| 226 |
description: Details of the HTTP Error. |
| 227 |
returned: on HTTP error |
| 228 |
type: dict |
| 229 |
sample: { |
| 230 |
"error": { |
| 231 |
"code": "Base.1.0.GeneralError", |
| 232 |
"message": "A general error has occurred. See ExtendedInfo for more information.", |
| 233 |
"@Message.ExtendedInfo": [ |
| 234 |
{ |
| 235 |
"MessageId": "GEN1234", |
| 236 |
"RelatedProperties": [], |
| 237 |
"Message": "Unable to process the request because an error occurred.", |
| 238 |
"MessageArgs": [], |
| 239 |
"Severity": "Critical", |
| 240 |
"Resolution": "Retry the operation. If the issue persists, contact your system administrator." |
| 241 |
} |
| 242 |
] |
| 243 |
} |
| 244 |
} |
| 245 |
''' |
| 246 |
|
| 247 |
import json |
| 248 |
import time |
| 249 |
import base64 |
| 250 |
import os |
| 251 |
from datetime import datetime |
| 252 |
from ansible_collections.dellemc.openmanage.plugins.module_utils.idrac_redfish import iDRACRedfishAPI, IdracAnsibleModule |
| 253 |
from ansible.module_utils.six.moves.urllib.error import URLError, HTTPError |
| 254 |
from ansible.module_utils.urls import ConnectionError, SSLValidationError |
| 255 |
from ansible_collections.dellemc.openmanage.plugins.module_utils.utils import reset_idrac |
| 256 |
|
| 257 |
IMPORT_SSL_CERTIFICATE = "#DelliDRACCardService.ImportSSLCertificate" |
| 258 |
EXPORT_SSL_CERTIFICATE = "#DelliDRACCardService.ExportSSLCertificate" |
| 259 |
IDRAC_CARD_SERVICE_ACTION_URI = "/redfish/v1/Managers/{res_id}/Oem/Dell/DelliDRACCardService/Actions" |
| 260 |
|
| 261 |
NOT_SUPPORTED_ACTION = "Certificate '{operation}' not supported for the specified certificate type '{cert_type}'." |
| 262 |
SUCCESS_MSG = "Successfully performed the '{command}' certificate operation." |
| 263 |
SUCCESS_MSG_SSL = "Successfully performed the SSL key upload and '{command}' certificate operation." |
| 264 |
NO_CHANGES_MSG = "No changes found to be applied." |
| 265 |
CHANGES_MSG = "Changes found to be applied." |
| 266 |
WAIT_NEGATIVE_OR_ZERO_MSG = "The value for the `wait` parameter cannot be negative or zero." |
| 267 |
SYSTEM_ID = "System.Embedded.1" |
| 268 |
MANAGER_ID = "iDRAC.Embedded.1" |
| 269 |
ACTIONS_PFIX = f"{IDRAC_CARD_SERVICE_ACTION_URI}/DelliDRACCardService." |
| 270 |
SYSTEMS_URI = "/redfish/v1/Systems" |
| 271 |
MANAGERS_URI = "/redfish/v1/Managers" |
| 272 |
IDRAC_SERVICE = "/redfish/v1/Managers/{res_id}/Oem/Dell/DelliDRACCardService" |
| 273 |
CSR_SSL = "/redfish/v1/CertificateService/Actions/CertificateService.GenerateCSR" |
| 274 |
IMPORT_SSL = f"{IDRAC_CARD_SERVICE_ACTION_URI}/DelliDRACCardService.ImportSSLCertificate" |
| 275 |
UPLOAD_SSL = f"{IDRAC_CARD_SERVICE_ACTION_URI}/DelliDRACCardService.UploadSSLKey" |
| 276 |
EXPORT_SSL = f"{IDRAC_CARD_SERVICE_ACTION_URI}/DelliDRACCardService.ExportSSLCertificate" |
| 277 |
RESET_SSL = f"{IDRAC_CARD_SERVICE_ACTION_URI}/DelliDRACCardService.SSLResetCfg" |
| 278 |
IDRAC_RESET = "/redfish/v1/Managers/{res_id}/Actions/Manager.Reset" |
| 279 |
GET_LAST_GENERATED_CSR = "/redfish/v1/CertificateService/Actions/Oem/DellCertificateService.GetLastGeneratedCSR" |
| 280 |
CERT_STATUS = "/redfish/v1/Managers/iDRAC.Embedded.1/Oem/Dell/DellAttributes/iDRAC.Embedded.1?$select=Security.1.ConfigCertStatus" |
| 281 |
|
| 282 |
idrac_service_actions = { |
| 283 |
"#DelliDRACCardService.DeleteCertificate": f"{IDRAC_CARD_SERVICE_ACTION_URI}/DelliDRACCardService.DeleteCertificate", |
| 284 |
"#DelliDRACCardService.ExportCertificate": f"{IDRAC_CARD_SERVICE_ACTION_URI}/DelliDRACCardService.ExportCertificate", |
| 285 |
EXPORT_SSL_CERTIFICATE: EXPORT_SSL, |
| 286 |
"#DelliDRACCardService.FactoryIdentityCertificateGenerateCSR": |
| 287 |
f"{IDRAC_CARD_SERVICE_ACTION_URI}/DelliDRACCardService.FactoryIdentityCertificateGenerateCSR", |
| 288 |
"#DelliDRACCardService.FactoryIdentityExportCertificate": |
| 289 |
f"{IDRAC_CARD_SERVICE_ACTION_URI}/DelliDRACCardService.FactoryIdentityExportCertificate", |
| 290 |
"#DelliDRACCardService.FactoryIdentityImportCertificate": |
| 291 |
f"{IDRAC_CARD_SERVICE_ACTION_URI}/DelliDRACCardService.FactoryIdentityImportCertificate", |
| 292 |
"#DelliDRACCardService.GenerateSEKMCSR": f"{IDRAC_CARD_SERVICE_ACTION_URI}/DelliDRACCardService.GenerateSEKMCSR", |
| 293 |
"#DelliDRACCardService.ImportCertificate": f"{IDRAC_CARD_SERVICE_ACTION_URI}/DelliDRACCardService.ImportCertificate", |
| 294 |
IMPORT_SSL_CERTIFICATE: IMPORT_SSL, |
| 295 |
"#DelliDRACCardService.UploadSSLKey": UPLOAD_SSL, |
| 296 |
"#DelliDRACCardService.SSLResetCfg": f"{IDRAC_CARD_SERVICE_ACTION_URI}/DelliDRACCardService.SSLResetCfg", |
| 297 |
"#DelliDRACCardService.iDRACReset": f"{IDRAC_CARD_SERVICE_ACTION_URI}/DelliDRACCardService.iDRACReset" |
| 298 |
} |
| 299 |
|
| 300 |
rfish_cert_coll = {'Server': { |
| 301 |
"@odata.id": "/redfish/v1/Managers/iDRAC.Embedded.1/NetworkProtocol/HTTPS/Certificates" |
| 302 |
}} |
| 303 |
out_file_path = {"CSRString": 'certificate_path', |
| 304 |
"CertificateFile": 'certificate_path'} |
| 305 |
changed_map = {"generate_csr": False, "import": True, "export": False, "reset": True} |
| 306 |
|
| 307 |
csr_transform = {"common_name": "CommonName", |
| 308 |
"organization_unit": "OrganizationalUnit", |
| 309 |
"locality_name": 'City', |
| 310 |
"state_name": 'State', |
| 311 |
"country_code": "Country", |
| 312 |
"email_address": 'Email', |
| 313 |
"organization_name": "Organization", |
| 314 |
"subject_alt_name": 'AlternativeNames'} |
| 315 |
action_url_map = {"generate_csr": {}, |
| 316 |
"import": {'Server': IMPORT_SSL_CERTIFICATE, |
| 317 |
'CA': IMPORT_SSL_CERTIFICATE, |
| 318 |
'CustomCertificate': IMPORT_SSL_CERTIFICATE, |
| 319 |
'CSC': IMPORT_SSL_CERTIFICATE, |
| 320 |
'ClientTrustCertificate': IMPORT_SSL_CERTIFICATE}, |
| 321 |
"export": {'Server': EXPORT_SSL_CERTIFICATE, |
| 322 |
'CA': EXPORT_SSL_CERTIFICATE, |
| 323 |
'CustomCertificate': EXPORT_SSL_CERTIFICATE, |
| 324 |
'CSC': EXPORT_SSL_CERTIFICATE, |
| 325 |
'ClientTrustCertificate': EXPORT_SSL_CERTIFICATE}, |
| 326 |
"reset": {'Server': "#DelliDRACCardService.SSLResetCfg"}} |
| 327 |
|
| 328 |
dflt_url_map = {"generate_csr": {'Server': CSR_SSL}, |
| 329 |
"import": {'Server': IMPORT_SSL, |
| 330 |
'CA': IMPORT_SSL, |
| 331 |
'CUSTOMCERTIFICATE': IMPORT_SSL, |
| 332 |
'CSC': IMPORT_SSL, |
| 333 |
'ClientTrustCertificate': IMPORT_SSL}, |
| 334 |
"export": {'Server': EXPORT_SSL, |
| 335 |
'CA': EXPORT_SSL, |
| 336 |
'CUSTOMCERTIFICATE': EXPORT_SSL, |
| 337 |
'CSC': EXPORT_SSL, |
| 338 |
'ClientTrustCertificate': EXPORT_SSL}, |
| 339 |
"reset": {'Server': RESET_SSL}} |
| 340 |
certype_map = {'HTTPS': "Server", 'CA': "CA", 'CUSTOMCERTIFICATE': "CustomCertificate", 'CSC': "CSC", |
| 341 |
'CLIENT_TRUST_CERTIFICATE': "ClientTrustCertificate"} |
| 342 |
|
| 343 |
|
| 344 |
def get_ssl_payload(module, operation, cert_type): |
| 345 |
payload = {} |
| 346 |
method = 'POST' |
| 347 |
|
| 348 |
if operation == 'import': |
| 349 |
payload = _build_import_payload(module, cert_type) |
| 350 |
elif operation == 'export': |
| 351 |
payload = {"SSLCertType": cert_type} |
| 352 |
elif operation == 'generate_csr': |
| 353 |
payload = _build_generate_csr_payload(module, cert_type) |
| 354 |
elif operation == 'reset': |
| 355 |
payload = '{}' |
| 356 |
|
| 357 |
return payload, method |
| 358 |
|
| 359 |
|
| 360 |
def _build_import_payload(module, cert_type): |
| 361 |
payload = {"CertificateType": cert_type} |
| 362 |
|
| 363 |
if module.params.get('passphrase'): |
| 364 |
payload['Passphrase'] = module.params.get('passphrase') |
| 365 |
|
| 366 |
cert_path = module.params.get('certificate_path') |
| 367 |
try: |
| 368 |
if str(cert_path).lower().endswith('.p12') or str(cert_path).lower().endswith('.pfx'): |
| 369 |
with open(cert_path, 'rb') as cert_file: |
| 370 |
cert_content = cert_file.read() |
| 371 |
cert_file_content = base64.encodebytes(cert_content).decode('ascii') |
| 372 |
else: |
| 373 |
with open(cert_path, "r") as cert_file: |
| 374 |
cert_file_content = cert_file.read() |
| 375 |
except OSError as file_error: |
| 376 |
module.exit_json(msg=str(file_error), failed=True) |
| 377 |
|
| 378 |
payload['SSLCertificateFile'] = cert_file_content |
| 379 |
return payload |
| 380 |
|
| 381 |
|
| 382 |
def _build_generate_csr_payload(module, cert_type): |
| 383 |
payload = {} |
| 384 |
cert_params = module.params.get("cert_params") |
| 385 |
|
| 386 |
for key, value in csr_transform.items(): |
| 387 |
if cert_params.get(key) is not None: |
| 388 |
if value == 'AlternativeNames': |
| 389 |
forming_string = ",".join(cert_params.get(key)) |
| 390 |
payload[value] = [forming_string] |
| 391 |
else: |
| 392 |
payload[value] = cert_params.get(key) |
| 393 |
|
| 394 |
if rfish_cert_coll.get(cert_type): |
| 395 |
payload["CertificateCollection"] = rfish_cert_coll.get(cert_type) |
| 396 |
|
| 397 |
return payload |
| 398 |
|
| 399 |
|
| 400 |
payload_map = {"Server": get_ssl_payload, |
| 401 |
"CA": get_ssl_payload, |
| 402 |
"CustomCertificate": get_ssl_payload, |
| 403 |
"CSC": get_ssl_payload, |
| 404 |
"ClientTrustCertificate": get_ssl_payload} |
| 405 |
|
| 406 |
|
| 407 |
def get_res_id(idrac, cert_type): |
| 408 |
resp = None |
| 409 |
cert_map = {"Server": MANAGER_ID} |
| 410 |
try: |
| 411 |
resp = idrac.invoke_request(cert_map.get(cert_type, MANAGERS_URI), "GET") |
| 412 |
membs = resp.json_data.get("Members") |
| 413 |
res_uri = membs[0].get('@odata.id') |
| 414 |
res_id = res_uri.split("/")[-1] |
| 415 |
except Exception: |
| 416 |
res_id = cert_map.get(cert_type, MANAGER_ID) |
| 417 |
return res_id |
| 418 |
|
| 419 |
|
| 420 |
def get_idrac_service(idrac, res_id): |
| 421 |
resp = None |
| 422 |
resp = idrac.invoke_request(f"{MANAGERS_URI}/{res_id}", 'GET') |
| 423 |
srvc_data = resp.json_data |
| 424 |
dell_srvc = srvc_data['Links']['Oem']['Dell']['DelliDRACCardService'] |
| 425 |
srvc = dell_srvc.get("@odata.id", IDRAC_SERVICE.format(res_id=res_id)) |
| 426 |
return srvc |
| 427 |
|
| 428 |
|
| 429 |
def get_actions_map(idrac, idrac_service_uri): |
| 430 |
resp = None |
| 431 |
actions = idrac_service_actions |
| 432 |
try: |
| 433 |
resp = idrac.invoke_request(idrac_service_uri, 'GET') |
| 434 |
srvc_data = resp.json_data |
| 435 |
actions = dict((k, v.get('target')) for k, v in srvc_data.get('Actions').items()) |
| 436 |
except Exception: |
| 437 |
actions = idrac_service_actions |
| 438 |
return actions |
| 439 |
|
| 440 |
|
| 441 |
def get_cert_url(actions, operation, cert_type, res_id): |
| 442 |
idrac_key = action_url_map.get(operation).get(cert_type) |
| 443 |
dynurl = actions.get(idrac_key) |
| 444 |
if not dynurl: |
| 445 |
dynurl = dflt_url_map.get(operation).get(cert_type) |
| 446 |
if dynurl: |
| 447 |
dynurl = dynurl.format(res_id=res_id) |
| 448 |
return dynurl |
| 449 |
|
| 450 |
|
| 451 |
def upload_ssl_key(module, idrac, actions, ssl_key, res_id): |
| 452 |
if not os.path.exists(ssl_key) or os.path.isdir(ssl_key): |
| 453 |
module.exit_json(msg=f"Unable to locate the SSL key file at {ssl_key}.", failed=True) |
| 454 |
|
| 455 |
try: |
| 456 |
with open(ssl_key, "r") as file: |
| 457 |
scert_file = file.read() |
| 458 |
except OSError as err: |
| 459 |
module.exit_json(msg=str(err), failed=True) |
| 460 |
|
| 461 |
if not module.check_mode: |
| 462 |
upload_url = actions.get("#DelliDRACCardService.UploadSSLKey") |
| 463 |
if not upload_url: |
| 464 |
module.exit_json("Upload of SSL key not supported", failed=True) |
| 465 |
|
| 466 |
payload = {} |
| 467 |
payload = {'SSLKeyString': scert_file} |
| 468 |
idrac.invoke_request(upload_url.format(res_id=res_id), "POST", data=payload) |
| 469 |
|
| 470 |
|
| 471 |
def certificate_action(module, idrac, actions, operation, cert_type, res_id): |
| 472 |
cert_url = get_cert_url(actions, operation, cert_type, res_id) |
| 473 |
if not cert_url: |
| 474 |
module.exit_json(msg=NOT_SUPPORTED_ACTION.format(operation=operation, cert_type=module.params.get('certificate_type'))) |
| 475 |
cert_payload, method = payload_map.get(cert_type)(module, operation, cert_type) |
| 476 |
exit_certificates(module, idrac, cert_url, cert_payload, method, cert_type, res_id) |
| 477 |
|
| 478 |
|
| 479 |
def write_to_file(module, cert_data, dkey): |
| 480 |
f_ext = {'HTTPS': ".pem", 'CA': ".pem", "CUSTOMCERTIFICATE": ".crt", 'CSC': ".crt", 'CLIENT_TRUST_CERTIFICATE': ".crt"} |
| 481 |
path = module.params.get('certificate_path') |
| 482 |
if not (os.path.exists(path) or os.path.isdir(path)): |
| 483 |
module.exit_json(msg=f"Provided directory path '{path}' is not valid.", failed=True) |
| 484 |
if not os.access(path, os.W_OK): |
| 485 |
module.exit_json(msg=f"Provided directory path '{path}' is not writable. Please check if you " |
| 486 |
"have appropriate permissions.", failed=True) |
| 487 |
d = datetime.now() |
| 488 |
if module.params.get('command') == 'generate_csr': |
| 489 |
ext = '.txt' |
| 490 |
else: |
| 491 |
ext = f_ext.get(module.params.get('certificate_type')) |
| 492 |
cert_file_name = f"{module.params['idrac_ip']}_{d.strftime('%Y%m%d_%H%M%S')}_{module.params.get('certificate_type')}{ext}" |
| 493 |
file_name = os.path.join(path, cert_file_name) |
| 494 |
write_data = cert_data.pop(dkey, None) |
| 495 |
with open(file_name, "w") as fp: |
| 496 |
fp.writelines(write_data) |
| 497 |
cert_data[out_file_path.get(dkey)] = file_name |
| 498 |
|
| 499 |
|
| 500 |
def format_output(module, cert_data): |
| 501 |
cp = cert_data.copy() |
| 502 |
klist = cp.keys() |
| 503 |
for k in klist: |
| 504 |
if "message" in k.lower(): |
| 505 |
cert_data.pop(k, None) |
| 506 |
if k in out_file_path: |
| 507 |
write_to_file(module, cert_data, k) |
| 508 |
cert_data.pop("CertificateCollection", None) |
| 509 |
return cert_data |
| 510 |
|
| 511 |
|
| 512 |
def get_export_data(idrac, cert_type, res_id): |
| 513 |
try: |
| 514 |
resp = idrac.invoke_request(EXPORT_SSL.format(res_id=res_id), "POST", data={"SSLCertType": cert_type}) |
| 515 |
cert_data = resp.json_data |
| 516 |
except Exception: |
| 517 |
cert_data = {"CertificateFile": ""} |
| 518 |
return cert_data.get("CertificateFile") |
| 519 |
|
| 520 |
|
| 521 |
def check_csr_generated(idrac): |
| 522 |
resp = None |
| 523 |
generated = False |
| 524 |
|
| 525 |
count = 24 |
| 526 |
while not generated and count > 0: |
| 527 |
resp = idrac.invoke_request(CERT_STATUS, "GET") |
| 528 |
generated = True if resp.json_data.get("Attributes").get("Security.1.ConfigCertStatus") == 2 else False |
| 529 |
time.sleep(5) |
| 530 |
count -= 1 |
| 531 |
return generated |
| 532 |
|
| 533 |
|
| 534 |
def perform_operation_and_download_csr(idrac, cert_url, method, cert_payload, module): |
| 535 |
resp = None |
| 536 |
try: |
| 537 |
resp = idrac.invoke_request(cert_url, method, data=cert_payload, api_timeout=60) |
| 538 |
except HTTPError as err: |
| 539 |
json_err = json.load(err) |
| 540 |
msg_id = json_err.get("error").get("@Message.ExtendedInfo")[0].get("MessageId") |
| 541 |
if err.code == 503 and msg_id in ['IDRAC.2.9.SYS537', 'IDRAC.2.8.SYS537']: |
| 542 |
body = {'CertificateCollection': rfish_cert_coll['Server']} |
| 543 |
if check_csr_generated(idrac): |
| 544 |
resp = idrac.invoke_request(GET_LAST_GENERATED_CSR, "POST", data=body) |
| 545 |
else: |
| 546 |
module.exit_json(failed=True, error_info=json_err, msg=str(err)) |
| 547 |
return resp |
| 548 |
|
| 549 |
|
| 550 |
def exit_certificates(module, idrac, cert_url, cert_payload, method, cert_type, res_id): |
| 551 |
resp = None |
| 552 |
cmd = module.params.get('command') |
| 553 |
changed = changed_map.get(cmd) |
| 554 |
reset = changed_map.get(cmd) and module.params.get('reset') |
| 555 |
result = {"changed": changed} |
| 556 |
reset_msg = "" |
| 557 |
if changed: |
| 558 |
reset_msg = "Reset iDRAC to apply the new certificate." \ |
| 559 |
" Until the iDRAC is reset, the old certificate will remain active." |
| 560 |
if module.params.get('command') == 'import': |
| 561 |
export_cert = get_export_data(idrac, cert_type, res_id) |
| 562 |
if cert_payload.get('SSLCertificateFile') in export_cert: |
| 563 |
module.exit_json(msg=NO_CHANGES_MSG) |
| 564 |
if module.check_mode and changed: |
| 565 |
module.exit_json(msg=CHANGES_MSG, changed=changed) |
| 566 |
if module.params.get('command') == 'reset' and cert_type == "Server": |
| 567 |
resp = idrac.invoke_request(cert_url, method, data=cert_payload, dump=False) |
| 568 |
else: |
| 569 |
resp = perform_operation_and_download_csr(idrac, cert_url, method, cert_payload, module) |
| 570 |
cert_data = resp.json_data |
| 571 |
cert_output = format_output(module, cert_data) |
| 572 |
result.update(cert_output) |
| 573 |
if reset: |
| 574 |
reset, track_failed, reset_msg = reset_idrac(idrac, module.params.get('wait'), res_id) |
| 575 |
if cmd == "import" and cert_type == "Server" and module.params.get('ssl_key'): |
| 576 |
result['msg'] = "{0} {1}".format(SUCCESS_MSG_SSL.format(command=cmd), reset_msg) |
| 577 |
else: |
| 578 |
result['msg'] = "{0}{1}".format(SUCCESS_MSG.format(command=cmd), reset_msg) |
| 579 |
module.exit_json(**result) |
| 580 |
|
| 581 |
|
| 582 |
def main(): |
| 583 |
specs = { |
| 584 |
"command": {"type": 'str', "default": 'generate_csr', |
| 585 |
"choices": ['generate_csr', 'export', 'import', 'reset']}, |
| 586 |
"certificate_type": {"type": 'str', "default": 'HTTPS', |
| 587 |
"choices": ['HTTPS', 'CA', 'CUSTOMCERTIFICATE', 'CSC', 'CLIENT_TRUST_CERTIFICATE']}, |
| 588 |
"certificate_path": {"type": 'path'}, |
| 589 |
"ssl_key": {"type": 'path'}, |
| 590 |
"passphrase": {"type": 'str', "no_log": True}, |
| 591 |
"cert_params": {"type": 'dict', "options": { |
| 592 |
"common_name": {"type": 'str', "required": True}, |
| 593 |
"organization_unit": {"type": 'str', "required": True}, |
| 594 |
"locality_name": {"type": 'str', "required": True}, |
| 595 |
"state_name": {"type": 'str', "required": True}, |
| 596 |
"country_code": {"type": 'str', "required": True}, |
| 597 |
"email_address": {"type": 'str'}, |
| 598 |
"organization_name": {"type": 'str', "required": True}, |
| 599 |
"subject_alt_name": {"type": 'list', "elements": 'str', "default": []} |
| 600 |
}}, |
| 601 |
"resource_id": {"type": 'str'}, |
| 602 |
"reset": {"type": 'bool', "default": True}, |
| 603 |
"wait": {"type": 'int', "default": 600} |
| 604 |
} |
| 605 |
|
| 606 |
module = IdracAnsibleModule( |
| 607 |
argument_spec=specs, |
| 608 |
required_if=[ |
| 609 |
['command', 'generate_csr', ('cert_params', 'certificate_path',)], |
| 610 |
['command', 'import', ('certificate_path',)], |
| 611 |
['command', 'export', ('certificate_path',)] |
| 612 |
], |
| 613 |
supports_check_mode=True) |
| 614 |
|
| 615 |
try: |
| 616 |
with iDRACRedfishAPI(module.params) as idrac: |
| 617 |
cert_type = certype_map.get(module.params.get('certificate_type')) |
| 618 |
operation = module.params.get('command') |
| 619 |
res_id = module.params.get('resource_id') |
| 620 |
if not res_id: |
| 621 |
res_id = get_res_id(idrac, cert_type) |
| 622 |
idrac_service_uri = get_idrac_service(idrac, res_id) |
| 623 |
actions_map = get_actions_map(idrac, idrac_service_uri) |
| 624 |
if operation in ["import", "reset"] and module.params.get('reset') and module.params.get('wait') <= 0: |
| 625 |
module.exit_json(msg=WAIT_NEGATIVE_OR_ZERO_MSG, failed=True) |
| 626 |
ssl_key = module.params.get('ssl_key') |
| 627 |
if operation == "import" and ssl_key is not None and cert_type == "Server": |
| 628 |
upload_ssl_key(module, idrac, actions_map, ssl_key, res_id) |
| 629 |
certificate_action(module, idrac, actions_map, operation, cert_type, res_id) |
| 630 |
except HTTPError as err: |
| 631 |
module.exit_json(msg=str(err), error_info=json.load(err), failed=True) |
| 632 |
except URLError as err: |
| 633 |
module.exit_json(msg=str(err), unreachable=True) |
| 634 |
except (ImportError, ValueError, RuntimeError, SSLValidationError, |
| 635 |
ConnectionError, KeyError, TypeError, IndexError) as e: |
| 636 |
module.exit_json(msg=str(e), failed=True) |
| 637 |
|
| 638 |
|
| 639 |
if __name__ == '__main__': |
| 640 |
main() |