| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2018, Dag Wieers (dagwieers) <[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_user_certificate |
| 16 |
short_description: Manage AAA user certificates (aaa:UserCert) |
| 17 |
description: |
| 18 |
- Manage AAA user certificates on Cisco ACI fabrics. |
| 19 |
options: |
| 20 |
aaa_user: |
| 21 |
description: |
| 22 |
- The name of the user to add a certificate to. |
| 23 |
type: str |
| 24 |
required: true |
| 25 |
aaa_user_type: |
| 26 |
description: |
| 27 |
- Whether this is a normal user or an appuser. |
| 28 |
type: str |
| 29 |
choices: [ appuser, user ] |
| 30 |
default: user |
| 31 |
certificate: |
| 32 |
description: |
| 33 |
- The PEM format public key extracted from the X.509 certificate. |
| 34 |
type: str |
| 35 |
aliases: [ cert_data, certificate_data ] |
| 36 |
name: |
| 37 |
description: |
| 38 |
- The name of the user certificate entry in ACI. |
| 39 |
type: str |
| 40 |
state: |
| 41 |
description: |
| 42 |
- Use C(present) or C(absent) for adding or removing. |
| 43 |
- Use C(query) for listing an object or multiple objects. |
| 44 |
type: str |
| 45 |
choices: [ absent, present, query ] |
| 46 |
default: present |
| 47 |
name_alias: |
| 48 |
description: |
| 49 |
- The alias for the current object. This relates to the nameAlias field in ACI. |
| 50 |
type: str |
| 51 |
extends_documentation_fragment: |
| 52 |
- cisco.aci.aci |
| 53 |
- cisco.aci.annotation |
| 54 |
- cisco.aci.owner |
| 55 |
|
| 56 |
notes: |
| 57 |
- The C(aaa_user) must exist before using this module in your playbook. |
| 58 |
The M(cisco.aci.aci_aaa_user) module can be used for this. |
| 59 |
seealso: |
| 60 |
- module: cisco.aci.aci_aaa_user |
| 61 |
- name: APIC Management Information Model reference |
| 62 |
description: More information about the internal APIC class B(aaa:UserCert). |
| 63 |
link: https://developer.cisco.com/docs/apic-mim-ref/ |
| 64 |
author: |
| 65 |
- Dag Wieers (@dagwieers) |
| 66 |
""" |
| 67 |
|
| 68 |
EXAMPLES = r""" |
| 69 |
- name: Add a certificate to user |
| 70 |
cisco.aci.aci_aaa_user_certificate: |
| 71 |
host: apic |
| 72 |
username: admin |
| 73 |
password: SomeSecretPassword |
| 74 |
aaa_user: admin |
| 75 |
name: admin |
| 76 |
certificate_data: '{{ lookup("file", "pki/admin.crt") }}' |
| 77 |
state: present |
| 78 |
delegate_to: localhost |
| 79 |
|
| 80 |
- name: Remove a certificate of a user |
| 81 |
cisco.aci.aci_aaa_user_certificate: |
| 82 |
host: apic |
| 83 |
username: admin |
| 84 |
password: SomeSecretPassword |
| 85 |
aaa_user: admin |
| 86 |
name: admin |
| 87 |
state: absent |
| 88 |
delegate_to: localhost |
| 89 |
|
| 90 |
- name: Query a certificate of a user |
| 91 |
cisco.aci.aci_aaa_user_certificate: |
| 92 |
host: apic |
| 93 |
username: admin |
| 94 |
password: SomeSecretPassword |
| 95 |
aaa_user: admin |
| 96 |
name: admin |
| 97 |
state: query |
| 98 |
delegate_to: localhost |
| 99 |
register: query_result |
| 100 |
|
| 101 |
- name: Query all certificates of a user |
| 102 |
cisco.aci.aci_aaa_user_certificate: |
| 103 |
host: apic |
| 104 |
username: admin |
| 105 |
password: SomeSecretPassword |
| 106 |
aaa_user: admin |
| 107 |
state: query |
| 108 |
delegate_to: localhost |
| 109 |
register: query_result |
| 110 |
""" |
| 111 |
|
| 112 |
RETURN = r""" |
| 113 |
current: |
| 114 |
description: The existing configuration from the APIC after the module has finished |
| 115 |
returned: success |
| 116 |
type: list |
| 117 |
sample: |
| 118 |
[ |
| 119 |
{ |
| 120 |
"fvTenant": { |
| 121 |
"attributes": { |
| 122 |
"descr": "Production environment", |
| 123 |
"dn": "uni/tn-production", |
| 124 |
"name": "production", |
| 125 |
"nameAlias": "", |
| 126 |
"ownerKey": "", |
| 127 |
"ownerTag": "" |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
] |
| 132 |
error: |
| 133 |
description: The error information as returned from the APIC |
| 134 |
returned: failure |
| 135 |
type: dict |
| 136 |
sample: |
| 137 |
{ |
| 138 |
"code": "122", |
| 139 |
"text": "unknown managed object class foo" |
| 140 |
} |
| 141 |
raw: |
| 142 |
description: The raw output returned by the APIC REST API (xml or json) |
| 143 |
returned: parse error |
| 144 |
type: str |
| 145 |
sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>' |
| 146 |
sent: |
| 147 |
description: The actual/minimal configuration pushed to the APIC |
| 148 |
returned: info |
| 149 |
type: list |
| 150 |
sample: |
| 151 |
{ |
| 152 |
"fvTenant": { |
| 153 |
"attributes": { |
| 154 |
"descr": "Production environment" |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
previous: |
| 159 |
description: The original configuration from the APIC before the module has started |
| 160 |
returned: info |
| 161 |
type: list |
| 162 |
sample: |
| 163 |
[ |
| 164 |
{ |
| 165 |
"fvTenant": { |
| 166 |
"attributes": { |
| 167 |
"descr": "Production", |
| 168 |
"dn": "uni/tn-production", |
| 169 |
"name": "production", |
| 170 |
"nameAlias": "", |
| 171 |
"ownerKey": "", |
| 172 |
"ownerTag": "" |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
] |
| 177 |
proposed: |
| 178 |
description: The assembled configuration from the user-provided parameters |
| 179 |
returned: info |
| 180 |
type: dict |
| 181 |
sample: |
| 182 |
{ |
| 183 |
"fvTenant": { |
| 184 |
"attributes": { |
| 185 |
"descr": "Production environment", |
| 186 |
"name": "production" |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
filter_string: |
| 191 |
description: The filter string used for the request |
| 192 |
returned: failure or debug |
| 193 |
type: str |
| 194 |
sample: ?rsp-prop-include=config-only |
| 195 |
method: |
| 196 |
description: The HTTP method used for the request to the APIC |
| 197 |
returned: failure or debug |
| 198 |
type: str |
| 199 |
sample: POST |
| 200 |
response: |
| 201 |
description: The HTTP response from the APIC |
| 202 |
returned: failure or debug |
| 203 |
type: str |
| 204 |
sample: OK (30 bytes) |
| 205 |
status: |
| 206 |
description: The HTTP status from the APIC |
| 207 |
returned: failure or debug |
| 208 |
type: int |
| 209 |
sample: 200 |
| 210 |
url: |
| 211 |
description: The HTTP url used for the request to the APIC |
| 212 |
returned: failure or debug |
| 213 |
type: str |
| 214 |
sample: https://10.11.12.13/api/mo/uni/tn-production.json |
| 215 |
""" |
| 216 |
|
| 217 |
from ansible.module_utils.basic import AnsibleModule |
| 218 |
from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec, aci_owner_spec |
| 219 |
|
| 220 |
ACI_MAPPING = dict( |
| 221 |
appuser=dict( |
| 222 |
aci_class="aaaAppUser", |
| 223 |
aci_mo="userext/appuser-", |
| 224 |
), |
| 225 |
user=dict( |
| 226 |
aci_class="aaaUser", |
| 227 |
aci_mo="userext/user-", |
| 228 |
), |
| 229 |
) |
| 230 |
|
| 231 |
|
| 232 |
def main(): |
| 233 |
argument_spec = aci_argument_spec() |
| 234 |
argument_spec.update(aci_annotation_spec()) |
| 235 |
argument_spec.update(aci_owner_spec()) |
| 236 |
argument_spec.update( |
| 237 |
aaa_user=dict(type="str", required=True), |
| 238 |
aaa_user_type=dict(type="str", default="user", choices=["appuser", "user"]), |
| 239 |
certificate=dict(type="str", aliases=["cert_data", "certificate_data"]), |
| 240 |
name=dict(type="str"), # Not required for querying all objects |
| 241 |
state=dict(type="str", default="present", choices=["absent", "present", "query"]), |
| 242 |
name_alias=dict(type="str"), |
| 243 |
) |
| 244 |
|
| 245 |
module = AnsibleModule( |
| 246 |
argument_spec=argument_spec, |
| 247 |
supports_check_mode=True, |
| 248 |
required_if=[ |
| 249 |
["state", "absent", ["aaa_user", "name"]], |
| 250 |
["state", "present", ["aaa_user", "certificate", "name"]], |
| 251 |
], |
| 252 |
) |
| 253 |
|
| 254 |
aaa_user = module.params.get("aaa_user") |
| 255 |
aaa_user_type = module.params.get("aaa_user_type") |
| 256 |
certificate = module.params.get("certificate") |
| 257 |
name = module.params.get("name") |
| 258 |
state = module.params.get("state") |
| 259 |
name_alias = module.params.get("name_alias") |
| 260 |
|
| 261 |
aci = ACIModule(module) |
| 262 |
aci.construct_url( |
| 263 |
root_class=dict( |
| 264 |
aci_class=ACI_MAPPING.get(aaa_user_type).get("aci_class"), |
| 265 |
aci_rn=ACI_MAPPING.get(aaa_user_type).get("aci_mo") + aaa_user, |
| 266 |
module_object=aaa_user, |
| 267 |
target_filter={"name": aaa_user}, |
| 268 |
), |
| 269 |
subclass_1=dict( |
| 270 |
aci_class="aaaUserCert", |
| 271 |
aci_rn="usercert-{0}".format(name), |
| 272 |
module_object=name, |
| 273 |
target_filter={"name": name}, |
| 274 |
), |
| 275 |
) |
| 276 |
aci.get_existing() |
| 277 |
|
| 278 |
if state == "present": |
| 279 |
aci.payload( |
| 280 |
aci_class="aaaUserCert", |
| 281 |
class_config=dict( |
| 282 |
data=certificate, |
| 283 |
name=name, |
| 284 |
nameAlias=name_alias, |
| 285 |
), |
| 286 |
) |
| 287 |
|
| 288 |
aci.get_diff(aci_class="aaaUserCert") |
| 289 |
|
| 290 |
aci.post_config() |
| 291 |
|
| 292 |
elif state == "absent": |
| 293 |
aci.delete_config() |
| 294 |
|
| 295 |
aci.exit_json() |
| 296 |
|
| 297 |
|
| 298 |
if __name__ == "__main__": |
| 299 |
main() |