| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
from __future__ import absolute_import, division, print_function |
| 7 |
__metaclass__ = type |
| 8 |
|
| 9 |
ANSIBLE_METADATA = {'metadata_version': '1.1', |
| 10 |
'status': ['preview'], |
| 11 |
'supported_by': 'community'} |
| 12 |
|
| 13 |
DOCUMENTATION = r''' |
| 14 |
--- |
| 15 |
module: intersight_certificate_management_policy |
| 16 |
short_description: Certificate Management Policy configuration for Cisco Intersight |
| 17 |
description: |
| 18 |
- Manages Certificate Management Policy configuration on Cisco Intersight. |
| 19 |
- A policy to configure certificate management for Cisco Intersight managed servers. |
| 20 |
- This policy allows configuration of Root CA certificates and IMC certificates with private keys. |
| 21 |
- For more information see L(Cisco Intersight,https://intersight.com/apidocs/certificatemanagement/Policies/get/). |
| 22 |
extends_documentation_fragment: intersight |
| 23 |
options: |
| 24 |
state: |
| 25 |
description: |
| 26 |
- If C(present), will verify the resource is present and will create if needed. |
| 27 |
- If C(absent), will verify the resource is absent and will delete if needed. |
| 28 |
type: str |
| 29 |
choices: [present, absent] |
| 30 |
default: present |
| 31 |
organization: |
| 32 |
description: |
| 33 |
- The name of the Organization this resource is assigned to. |
| 34 |
- Profiles and Policies that are created within a Custom Organization are applicable only to devices in the same Organization. |
| 35 |
type: str |
| 36 |
default: default |
| 37 |
name: |
| 38 |
description: |
| 39 |
- The name assigned to the Certificate Management Policy. |
| 40 |
- The name must be between 1 and 62 alphanumeric characters, allowing special characters :-_. |
| 41 |
type: str |
| 42 |
required: true |
| 43 |
description: |
| 44 |
description: |
| 45 |
- The user-defined description for the Certificate Management Policy. |
| 46 |
- Description can contain letters(a-z, A-Z), numbers(0-9), hyphen(-), period(.), colon(:), or an underscore(_). |
| 47 |
type: str |
| 48 |
aliases: [descr] |
| 49 |
tags: |
| 50 |
description: |
| 51 |
- List of tags in Key:<user-defined key> Value:<user-defined value> format. |
| 52 |
type: list |
| 53 |
elements: dict |
| 54 |
certificates: |
| 55 |
description: |
| 56 |
- List of certificates to manage in the policy. |
| 57 |
- Can include Root CA certificates and IMC certificates. |
| 58 |
- Only one IMC certificate can be configured per policy. |
| 59 |
type: list |
| 60 |
elements: dict |
| 61 |
suboptions: |
| 62 |
certificate_type: |
| 63 |
description: |
| 64 |
- Type of certificate to configure. |
| 65 |
- C(rootca) for Root CA certificates. |
| 66 |
- C(imc) for IMC certificates (only one allowed per policy). |
| 67 |
type: str |
| 68 |
choices: [rootca, imc] |
| 69 |
required: true |
| 70 |
certificate_name: |
| 71 |
description: |
| 72 |
- A name that helps identify a certificate. |
| 73 |
- Can be any string that adheres to the following constraints. |
| 74 |
- It should start and end with an alphanumeric character. |
| 75 |
- It can have underscores and hyphens. |
| 76 |
- It cannot be more than 30 characters. |
| 77 |
- Required for Root CA certificates. |
| 78 |
type: str |
| 79 |
certificate: |
| 80 |
description: |
| 81 |
- The PEM-encoded certificate data. |
| 82 |
- Must be base64 encoded. |
| 83 |
- Required for all certificate types. |
| 84 |
type: str |
| 85 |
required: true |
| 86 |
private_key: |
| 87 |
description: |
| 88 |
- The PEM-encoded private key for the certificate. |
| 89 |
- Must be base64 encoded. |
| 90 |
- Required only for IMC certificate type. |
| 91 |
type: str |
| 92 |
enabled: |
| 93 |
description: |
| 94 |
- Whether the certificate is enabled. |
| 95 |
type: bool |
| 96 |
default: true |
| 97 |
author: |
| 98 |
- Ron Gershburg (@rgershbu) |
| 99 |
''' |
| 100 |
|
| 101 |
EXAMPLES = r''' |
| 102 |
- name: Create Certificate Management Policy with Root CA certificate |
| 103 |
cisco.intersight.intersight_certificate_management_policy: |
| 104 |
api_private_key: "{{ api_private_key }}" |
| 105 |
api_key_id: "{{ api_key_id }}" |
| 106 |
organization: "default" |
| 107 |
name: "CertMgmt-Policy-01" |
| 108 |
description: "Certificate management policy with Root CA" |
| 109 |
certificates: |
| 110 |
- certificate_type: rootca |
| 111 |
certificate_name: "RootCA-01" |
| 112 |
certificate: "LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0t..." |
| 113 |
enabled: true |
| 114 |
state: present |
| 115 |
|
| 116 |
- name: Create Certificate Management Policy with IMC certificate |
| 117 |
cisco.intersight.intersight_certificate_management_policy: |
| 118 |
api_private_key: "{{ api_private_key }}" |
| 119 |
api_key_id: "{{ api_key_id }}" |
| 120 |
name: "CertMgmt-Policy-02" |
| 121 |
description: "Certificate management policy with IMC certificate" |
| 122 |
certificates: |
| 123 |
- certificate_type: imc |
| 124 |
certificate: "LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0t..." |
| 125 |
private_key: "LS0tLS1CRUdJTiBSU0EgUFJJVkFURSBLRVktLS0tLQ..." |
| 126 |
enabled: true |
| 127 |
state: present |
| 128 |
|
| 129 |
- name: Create Certificate Management Policy with both types |
| 130 |
cisco.intersight.intersight_certificate_management_policy: |
| 131 |
api_private_key: "{{ api_private_key }}" |
| 132 |
api_key_id: "{{ api_key_id }}" |
| 133 |
name: "CertMgmt-Policy-03" |
| 134 |
description: "Certificate management policy with both certificate types" |
| 135 |
certificates: |
| 136 |
- certificate_type: rootca |
| 137 |
certificate_name: "RootCA-01" |
| 138 |
certificate: "LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0t..." |
| 139 |
enabled: true |
| 140 |
- certificate_type: imc |
| 141 |
certificate: "LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0t..." |
| 142 |
private_key: "LS0tLS1CRUdJTiBSU0EgUFJJVkFURSBLRVktLS0tLQ..." |
| 143 |
enabled: true |
| 144 |
state: present |
| 145 |
|
| 146 |
- name: Update policy - remove all certificates |
| 147 |
cisco.intersight.intersight_certificate_management_policy: |
| 148 |
api_private_key: "{{ api_private_key }}" |
| 149 |
api_key_id: "{{ api_key_id }}" |
| 150 |
name: "CertMgmt-Policy-01" |
| 151 |
certificates: [] |
| 152 |
state: present |
| 153 |
|
| 154 |
- name: Delete Certificate Management Policy |
| 155 |
cisco.intersight.intersight_certificate_management_policy: |
| 156 |
api_private_key: "{{ api_private_key }}" |
| 157 |
api_key_id: "{{ api_key_id }}" |
| 158 |
name: "CertMgmt-Policy-01" |
| 159 |
state: absent |
| 160 |
''' |
| 161 |
|
| 162 |
RETURN = r''' |
| 163 |
api_response: |
| 164 |
description: The API response output returned by the specified resource. |
| 165 |
returned: always |
| 166 |
type: dict |
| 167 |
sample: |
| 168 |
"api_response": { |
| 169 |
"Name": "CertMgmt-Policy-01", |
| 170 |
"ObjectType": "certificatemanagement.Policy", |
| 171 |
"Moid": "1234567890abcdef12345678", |
| 172 |
"Description": "Certificate management policy", |
| 173 |
"Certificates": [ |
| 174 |
{ |
| 175 |
"Certificate": { |
| 176 |
"PemCertificate": "LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0t..." |
| 177 |
}, |
| 178 |
"CertificateName": "RootCA-01", |
| 179 |
"ObjectType": "certificatemanagement.RootCaCertificate", |
| 180 |
"Enabled": true |
| 181 |
} |
| 182 |
], |
| 183 |
"Tags": [ |
| 184 |
{ |
| 185 |
"Key": "Environment", |
| 186 |
"Value": "Production" |
| 187 |
} |
| 188 |
] |
| 189 |
} |
| 190 |
''' |
| 191 |
|
| 192 |
|
| 193 |
from ansible.module_utils.basic import AnsibleModule |
| 194 |
from ansible_collections.cisco.intersight.plugins.module_utils.intersight import IntersightModule, intersight_argument_spec |
| 195 |
import re |
| 196 |
|
| 197 |
|
| 198 |
def validate_certificate_name(certificate_name): |
| 199 |
""" |
| 200 |
Validate certificate name according to Intersight constraints. |
| 201 |
""" |
| 202 |
if not certificate_name: |
| 203 |
return False, "Certificate name is required for Root CA certificates" |
| 204 |
|
| 205 |
if len(certificate_name) > 30: |
| 206 |
return False, "Certificate name cannot be more than 30 characters" |
| 207 |
|
| 208 |
# Check if it starts and ends with alphanumeric |
| 209 |
if not re.match(r'^[a-zA-Z0-9].*[a-zA-Z0-9]$', certificate_name): |
| 210 |
return False, "Certificate name must start and end with an alphanumeric character" |
| 211 |
|
| 212 |
# Check if it only contains allowed characters (alphanumeric, underscore, hyphen) |
| 213 |
if not re.match(r'^[a-zA-Z0-9_-]+$', certificate_name): |
| 214 |
return False, "Certificate name can only contain alphanumeric characters, underscores, and hyphens" |
| 215 |
|
| 216 |
return True, "" |
| 217 |
|
| 218 |
|
| 219 |
def build_certificate_object(cert_config): |
| 220 |
""" |
| 221 |
Build certificate object for the Certificates array in policy API body. |
| 222 |
""" |
| 223 |
cert_type = cert_config['certificate_type'] |
| 224 |
|
| 225 |
cert_obj = { |
| 226 |
'Enabled': cert_config.get('enabled', True) |
| 227 |
} |
| 228 |
|
| 229 |
if cert_type == 'rootca': |
| 230 |
cert_obj['ObjectType'] = 'certificatemanagement.RootCaCertificate' |
| 231 |
cert_obj['CertificateName'] = cert_config['certificate_name'] |
| 232 |
elif cert_type == 'imc': |
| 233 |
cert_obj['ObjectType'] = 'certificatemanagement.Imc' |
| 234 |
if cert_config.get('private_key'): |
| 235 |
cert_obj['Privatekey'] = cert_config['private_key'] |
| 236 |
cert_obj['IsPrivatekeySet'] = True |
| 237 |
|
| 238 |
# Add certificate data |
| 239 |
if cert_config.get('certificate'): |
| 240 |
cert_obj['Certificate'] = { |
| 241 |
'PemCertificate': cert_config['certificate'] |
| 242 |
} |
| 243 |
|
| 244 |
return cert_obj |
| 245 |
|
| 246 |
|
| 247 |
def main(): |
| 248 |
certificate_options = dict( |
| 249 |
certificate_type=dict(type='str', choices=['rootca', 'imc'], required=True), |
| 250 |
certificate_name=dict(type='str'), |
| 251 |
certificate=dict(type='str', required=True), |
| 252 |
private_key=dict(type='str', no_log=True), |
| 253 |
enabled=dict(type='bool', default=True) |
| 254 |
) |
| 255 |
|
| 256 |
argument_spec = intersight_argument_spec.copy() |
| 257 |
argument_spec.update( |
| 258 |
state=dict(type='str', choices=['present', 'absent'], default='present'), |
| 259 |
organization=dict(type='str', default='default'), |
| 260 |
name=dict(type='str', required=True), |
| 261 |
description=dict(type='str', aliases=['descr']), |
| 262 |
tags=dict(type='list', elements='dict'), |
| 263 |
certificates=dict(type='list', elements='dict', options=certificate_options) |
| 264 |
) |
| 265 |
|
| 266 |
module = AnsibleModule( |
| 267 |
argument_spec, |
| 268 |
supports_check_mode=True |
| 269 |
) |
| 270 |
|
| 271 |
intersight = IntersightModule(module) |
| 272 |
intersight.result['api_response'] = {} |
| 273 |
intersight.result['trace_id'] = '' |
| 274 |
|
| 275 |
# Resource path used to configure policy |
| 276 |
resource_path = '/certificatemanagement/Policies' |
| 277 |
|
| 278 |
# Validate certificates configuration |
| 279 |
certificates = module.params.get('certificates') or [] |
| 280 |
imc_count = 0 |
| 281 |
|
| 282 |
for cert_config in certificates: |
| 283 |
cert_type = cert_config['certificate_type'] |
| 284 |
|
| 285 |
# Count IMC certificates |
| 286 |
if cert_type == 'imc': |
| 287 |
imc_count += 1 |
| 288 |
|
| 289 |
# Validate Root CA certificate name |
| 290 |
if cert_type == 'rootca': |
| 291 |
if not cert_config.get('certificate_name'): |
| 292 |
module.fail_json(msg="certificate_name is required for Root CA certificates") |
| 293 |
|
| 294 |
is_valid, error_msg = validate_certificate_name(cert_config['certificate_name']) |
| 295 |
if not is_valid: |
| 296 |
module.fail_json(msg=f"Invalid certificate name: {error_msg}") |
| 297 |
|
| 298 |
# Validate IMC certificate requirements |
| 299 |
if cert_type == 'imc': |
| 300 |
if not cert_config.get('private_key'): |
| 301 |
module.fail_json(msg="private_key is required for IMC certificates") |
| 302 |
|
| 303 |
# Validate only one IMC certificate per policy |
| 304 |
if imc_count > 1: |
| 305 |
module.fail_json(msg="Only one IMC certificate can be configured per policy") |
| 306 |
|
| 307 |
# Define API body used in compares or create |
| 308 |
intersight.api_body = { |
| 309 |
'Organization': { |
| 310 |
'Name': intersight.module.params['organization'], |
| 311 |
}, |
| 312 |
'Name': intersight.module.params['name'], |
| 313 |
} |
| 314 |
|
| 315 |
if intersight.module.params['state'] == 'present': |
| 316 |
intersight.set_tags_and_description() |
| 317 |
|
| 318 |
# Build Certificates array |
| 319 |
if certificates: |
| 320 |
certificates_array = [] |
| 321 |
for cert_config in certificates: |
| 322 |
cert_obj = build_certificate_object(cert_config) |
| 323 |
certificates_array.append(cert_obj) |
| 324 |
intersight.api_body['Certificates'] = certificates_array |
| 325 |
else: |
| 326 |
# Empty array to clear all certificates |
| 327 |
intersight.api_body['Certificates'] = [] |
| 328 |
|
| 329 |
# Configure the policy |
| 330 |
intersight.configure_policy_or_profile(resource_path=resource_path) |
| 331 |
|
| 332 |
module.exit_json(**intersight.result) |
| 333 |
|
| 334 |
|
| 335 |
if __name__ == '__main__': |
| 336 |
main() |