| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2024, Akini Ross (@akinross) <[email protected]> |
| 5 |
# GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
|
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "certified"} |
| 12 |
|
| 13 |
DOCUMENTATION = r""" |
| 14 |
--- |
| 15 |
module: aci_aaa_key_ring |
| 16 |
short_description: Manage AAA Key Rings (pki:KeyRing) |
| 17 |
description: |
| 18 |
- Manage AAA Key Rings on Cisco ACI fabrics. |
| 19 |
options: |
| 20 |
name: |
| 21 |
description: |
| 22 |
- The name of the Key Ring. |
| 23 |
type: str |
| 24 |
aliases: [ key_ring, key_ring_name ] |
| 25 |
cloud_tenant: |
| 26 |
description: |
| 27 |
- The name of the cloud tenant. |
| 28 |
- This attribute is only applicable for Cloud APIC. |
| 29 |
type: str |
| 30 |
aliases: [ tenant, tenant_name ] |
| 31 |
description: |
| 32 |
description: |
| 33 |
- The description of the Key Ring. |
| 34 |
type: str |
| 35 |
aliases: [ descr ] |
| 36 |
certificate: |
| 37 |
description: |
| 38 |
- The certificate of the Key Ring. |
| 39 |
- The certificate contains a public key and signed information for verifying the identity. |
| 40 |
type: str |
| 41 |
aliases: [ cert_data, certificate_data, cert ] |
| 42 |
modulus: |
| 43 |
description: |
| 44 |
- The modulus is the length of the key in bits. |
| 45 |
type: int |
| 46 |
choices: [ 512, 1024, 1536, 2048 ] |
| 47 |
certificate_authority: |
| 48 |
description: |
| 49 |
- The name of the Certificate Authority. |
| 50 |
type: str |
| 51 |
aliases: [ cert_authority, cert_authority_name, certificate_authority_name ] |
| 52 |
key: |
| 53 |
description: |
| 54 |
- The private key for the certificate. |
| 55 |
- Providing this option will always result in a change because it is a secure property that cannot be retrieved from APIC. |
| 56 |
type: str |
| 57 |
key_type: |
| 58 |
description: |
| 59 |
- The type of the private key. |
| 60 |
- This attribute is only configurable in ACI versions 6.0(2h) and above. |
| 61 |
type: str |
| 62 |
choices: [ rsa, ecc ] |
| 63 |
ecc_curve: |
| 64 |
description: |
| 65 |
- The curve of the private key. |
| 66 |
- This attribute requires O(key_type=ecc). |
| 67 |
- This attribute is only configurable in ACI versions 6.0(2h) and above. |
| 68 |
type: str |
| 69 |
choices: [ P256, P384, P521, none ] |
| 70 |
aliases: [ curve ] |
| 71 |
state: |
| 72 |
description: |
| 73 |
- Use C(present) or C(absent) for adding or removing. |
| 74 |
- Use C(query) for listing an object or multiple objects. |
| 75 |
type: str |
| 76 |
choices: [ absent, present, query ] |
| 77 |
default: present |
| 78 |
name_alias: |
| 79 |
description: |
| 80 |
- The alias for the current object. This relates to the nameAlias field in ACI. |
| 81 |
type: str |
| 82 |
extends_documentation_fragment: |
| 83 |
- cisco.aci.aci |
| 84 |
- cisco.aci.annotation |
| 85 |
- cisco.aci.owner |
| 86 |
|
| 87 |
notes: |
| 88 |
- The C(cloud_tenant) used must exist before using this module in your playbook. |
| 89 |
The M(cisco.aci.aci_tenant) module can be used for this. |
| 90 |
seealso: |
| 91 |
- name: APIC Management Information Model reference |
| 92 |
description: More information about the internal APIC class B(pki:KeyRing). |
| 93 |
link: https://developer.cisco.com/docs/apic-mim-ref/ |
| 94 |
author: |
| 95 |
- Akini Ross (@akinross) |
| 96 |
""" |
| 97 |
|
| 98 |
EXAMPLES = r""" |
| 99 |
- name: Create a Key Ring |
| 100 |
cisco.aci.aci_aaa_key_ring: |
| 101 |
host: apic |
| 102 |
username: admin |
| 103 |
password: SomeSecretPassword |
| 104 |
name: example_key_ring |
| 105 |
certificate: '{{ lookup("file", "pki/example_certificate.crt") }}' |
| 106 |
modulus: 2048 |
| 107 |
certificate_authority: example_certificate_authority |
| 108 |
key: '{{ lookup("file", "pki/example_key.key") }}' |
| 109 |
state: present |
| 110 |
delegate_to: localhost |
| 111 |
|
| 112 |
- name: Query a Key Ring |
| 113 |
cisco.aci.aci_aaa_key_ring: |
| 114 |
host: apic |
| 115 |
username: admin |
| 116 |
password: SomeSecretPassword |
| 117 |
name: example_key_ring |
| 118 |
state: query |
| 119 |
delegate_to: localhost |
| 120 |
register: query_result |
| 121 |
|
| 122 |
- name: Query all Certificate Authorities |
| 123 |
cisco.aci.aci_aaa_key_ring: |
| 124 |
host: apic |
| 125 |
username: admin |
| 126 |
password: SomeSecretPassword |
| 127 |
state: query |
| 128 |
delegate_to: localhost |
| 129 |
register: query_result |
| 130 |
|
| 131 |
- name: Delete a Key Ring |
| 132 |
cisco.aci.aci_aaa_key_ring: |
| 133 |
host: apic |
| 134 |
username: admin |
| 135 |
password: SomeSecretPassword |
| 136 |
name: example_key_ring |
| 137 |
state: absent |
| 138 |
delegate_to: localhost |
| 139 |
""" |
| 140 |
|
| 141 |
RETURN = r""" |
| 142 |
current: |
| 143 |
description: The existing configuration from the APIC after the module has finished |
| 144 |
returned: success |
| 145 |
type: list |
| 146 |
sample: |
| 147 |
[ |
| 148 |
{ |
| 149 |
"fvTenant": { |
| 150 |
"attributes": { |
| 151 |
"descr": "Production environment", |
| 152 |
"dn": "uni/tn-production", |
| 153 |
"name": "production", |
| 154 |
"nameAlias": "", |
| 155 |
"ownerKey": "", |
| 156 |
"ownerTag": "" |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
] |
| 161 |
error: |
| 162 |
description: The error information as returned from the APIC |
| 163 |
returned: failure |
| 164 |
type: dict |
| 165 |
sample: |
| 166 |
{ |
| 167 |
"code": "122", |
| 168 |
"text": "unknown managed object class foo" |
| 169 |
} |
| 170 |
raw: |
| 171 |
description: The raw output returned by the APIC REST API (xml or json) |
| 172 |
returned: parse error |
| 173 |
type: str |
| 174 |
sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>' |
| 175 |
sent: |
| 176 |
description: The actual/minimal configuration pushed to the APIC |
| 177 |
returned: info |
| 178 |
type: list |
| 179 |
sample: |
| 180 |
{ |
| 181 |
"fvTenant": { |
| 182 |
"attributes": { |
| 183 |
"descr": "Production environment" |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
previous: |
| 188 |
description: The original configuration from the APIC before the module has started |
| 189 |
returned: info |
| 190 |
type: list |
| 191 |
sample: |
| 192 |
[ |
| 193 |
{ |
| 194 |
"fvTenant": { |
| 195 |
"attributes": { |
| 196 |
"descr": "Production", |
| 197 |
"dn": "uni/tn-production", |
| 198 |
"name": "production", |
| 199 |
"nameAlias": "", |
| 200 |
"ownerKey": "", |
| 201 |
"ownerTag": "" |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
] |
| 206 |
proposed: |
| 207 |
description: The assembled configuration from the user-provided parameters |
| 208 |
returned: info |
| 209 |
type: dict |
| 210 |
sample: |
| 211 |
{ |
| 212 |
"fvTenant": { |
| 213 |
"attributes": { |
| 214 |
"descr": "Production environment", |
| 215 |
"name": "production" |
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
filter_string: |
| 220 |
description: The filter string used for the request |
| 221 |
returned: failure or debug |
| 222 |
type: str |
| 223 |
sample: ?rsp-prop-include=config-only |
| 224 |
method: |
| 225 |
description: The HTTP method used for the request to the APIC |
| 226 |
returned: failure or debug |
| 227 |
type: str |
| 228 |
sample: POST |
| 229 |
response: |
| 230 |
description: The HTTP response from the APIC |
| 231 |
returned: failure or debug |
| 232 |
type: str |
| 233 |
sample: OK (30 bytes) |
| 234 |
status: |
| 235 |
description: The HTTP status from the APIC |
| 236 |
returned: failure or debug |
| 237 |
type: int |
| 238 |
sample: 200 |
| 239 |
url: |
| 240 |
description: The HTTP url used for the request to the APIC |
| 241 |
returned: failure or debug |
| 242 |
type: str |
| 243 |
sample: https://10.11.12.13/api/mo/uni/tn-production.json |
| 244 |
""" |
| 245 |
|
| 246 |
from ansible.module_utils.basic import AnsibleModule |
| 247 |
from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec, aci_owner_spec |
| 248 |
from ansible_collections.cisco.aci.plugins.module_utils.constants import ECC_CURVE |
| 249 |
|
| 250 |
|
| 251 |
def main(): |
| 252 |
argument_spec = aci_argument_spec() |
| 253 |
argument_spec.update(aci_annotation_spec()) |
| 254 |
argument_spec.update(aci_owner_spec()) |
| 255 |
argument_spec.update( |
| 256 |
name=dict(type="str", aliases=["key_ring", "key_ring_name"]), # Not required for querying all objects |
| 257 |
cloud_tenant=dict(type="str", aliases=["tenant_name", "tenant"]), |
| 258 |
description=dict(type="str", aliases=["descr"]), |
| 259 |
certificate=dict(type="str", aliases=["cert_data", "certificate_data", "cert"]), |
| 260 |
modulus=dict(type="int", choices=[512, 1024, 1536, 2048]), |
| 261 |
certificate_authority=dict(type="str", aliases=["cert_authority", "cert_authority_name", "certificate_authority_name"]), |
| 262 |
key=dict(type="str", no_log=True), |
| 263 |
key_type=dict(type="str", choices=["rsa", "ecc"]), |
| 264 |
ecc_curve=dict(type="str", choices=["P256", "P384", "P521", "none"], aliases=["curve"]), |
| 265 |
state=dict(type="str", default="present", choices=["absent", "present", "query"]), |
| 266 |
name_alias=dict(type="str"), |
| 267 |
) |
| 268 |
|
| 269 |
module = AnsibleModule( |
| 270 |
argument_spec=argument_spec, |
| 271 |
supports_check_mode=True, |
| 272 |
required_if=[ |
| 273 |
["state", "absent", ["name"]], |
| 274 |
["state", "present", ["name"]], |
| 275 |
], |
| 276 |
) |
| 277 |
|
| 278 |
name = module.params.get("name") |
| 279 |
cloud_tenant = module.params.get("cloud_tenant") |
| 280 |
description = module.params.get("description") |
| 281 |
certificate = module.params.get("certificate") |
| 282 |
modulus = "mod{0}".format(module.params.get("modulus")) if module.params.get("modulus") else None |
| 283 |
certificate_authority = module.params.get("certificate_authority") |
| 284 |
key = module.params.get("key") |
| 285 |
key_type = module.params.get("key_type") |
| 286 |
ecc_curve = ECC_CURVE[module.params.get("ecc_curve")] if module.params.get("ecc_curve") else None |
| 287 |
state = module.params.get("state") |
| 288 |
name_alias = module.params.get("name_alias") |
| 289 |
|
| 290 |
aci = ACIModule(module) |
| 291 |
aci_class = "pkiKeyRing" |
| 292 |
parent_class = "cloudCertStore" if cloud_tenant else "pkiEp" |
| 293 |
parent_rn = "tn-{0}/certstore".format(cloud_tenant) if cloud_tenant else "userext/pkiext" |
| 294 |
|
| 295 |
aci.construct_url( |
| 296 |
root_class=dict( |
| 297 |
aci_class=parent_class, |
| 298 |
aci_rn=parent_rn, |
| 299 |
), |
| 300 |
subclass_1=dict( |
| 301 |
aci_class=aci_class, |
| 302 |
aci_rn="keyring-{0}".format(name), |
| 303 |
module_object=name, |
| 304 |
target_filter={"name": name}, |
| 305 |
), |
| 306 |
) |
| 307 |
aci.get_existing() |
| 308 |
|
| 309 |
if state == "present": |
| 310 |
class_config = dict( |
| 311 |
name=name, |
| 312 |
descr=description, |
| 313 |
cert=certificate, |
| 314 |
modulus=modulus, |
| 315 |
tp=certificate_authority, |
| 316 |
nameAlias=name_alias, |
| 317 |
key=key, |
| 318 |
) |
| 319 |
|
| 320 |
if key_type: |
| 321 |
class_config["keyType"] = key_type.upper() |
| 322 |
|
| 323 |
if ecc_curve: |
| 324 |
class_config["eccCurve"] = ecc_curve |
| 325 |
|
| 326 |
aci.payload(aci_class=aci_class, class_config=class_config) |
| 327 |
|
| 328 |
aci.get_diff(aci_class=aci_class, required_properties=dict(name=name) if cloud_tenant else None) |
| 329 |
|
| 330 |
# Only wrap the payload in parent class if cloud_tenant is set to avoid apic error |
| 331 |
aci.post_config(parent_class=parent_class if cloud_tenant else None) |
| 332 |
|
| 333 |
elif state == "absent": |
| 334 |
aci.delete_config() |
| 335 |
|
| 336 |
aci.exit_json() |
| 337 |
|
| 338 |
|
| 339 |
if __name__ == "__main__": |
| 340 |
main() |