| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# (c) 2020-2026, NetApp, Inc |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
''' |
| 7 |
na_ontap_security_certificates |
| 8 |
''' |
| 9 |
|
| 10 |
from __future__ import absolute_import, division, print_function |
| 11 |
__metaclass__ = type |
| 12 |
|
| 13 |
DOCUMENTATION = ''' |
| 14 |
|
| 15 |
module: na_ontap_security_certificates |
| 16 |
short_description: NetApp ONTAP manage security certificates. |
| 17 |
extends_documentation_fragment: |
| 18 |
- netapp.ontap.netapp.na_ontap |
| 19 |
version_added: '20.7.0' |
| 20 |
author: NetApp Ansible Team (@carchi8py) <[email protected]> |
| 21 |
|
| 22 |
description: |
| 23 |
- Install or delete security certificates on ONTAP. (Create and sign will come in a second iteration) |
| 24 |
|
| 25 |
options: |
| 26 |
|
| 27 |
state: |
| 28 |
description: |
| 29 |
- Whether the specified security certificate should exist or not. |
| 30 |
choices: ['present', 'absent'] |
| 31 |
default: 'present' |
| 32 |
type: str |
| 33 |
|
| 34 |
common_name: |
| 35 |
description: |
| 36 |
- Common name of the certificate. |
| 37 |
- Required for create and install. |
| 38 |
- If name is present, ignored for sign and delete. |
| 39 |
- If name is absent or ignored, required for sign and delete. |
| 40 |
type: str |
| 41 |
|
| 42 |
name: |
| 43 |
description: |
| 44 |
- The unique name of the security certificate per SVM. |
| 45 |
- This parameter is not supported for ONTAP 9.6 or 9.7, as the REST API does not support it. |
| 46 |
- If present with ONTAP 9.6 or 9.7, it is ignored by default, see I(ignore_name_if_not_supported). |
| 47 |
- It is strongly recommended to use name for newer releases of ONTAP. |
| 48 |
type: str |
| 49 |
|
| 50 |
svm: |
| 51 |
description: |
| 52 |
- The name of the SVM (vserver). |
| 53 |
- If present, the certificate is installed in the SVM. |
| 54 |
- If absent, the certificate is installed in the cluster. |
| 55 |
type: str |
| 56 |
aliases: |
| 57 |
- vserver |
| 58 |
|
| 59 |
type: |
| 60 |
description: |
| 61 |
- Type of certificate |
| 62 |
- Required for create and install. |
| 63 |
- If name is present, ignored for sign and delete. |
| 64 |
- If name is absent or ignored, required for sign and delete. |
| 65 |
choices: ['client', 'server', 'client_ca', 'server_ca', 'root_ca'] |
| 66 |
type: str |
| 67 |
|
| 68 |
public_certificate: |
| 69 |
description: |
| 70 |
- Public key certificate in PEM format. |
| 71 |
- Required when installing a certificate. Ignored otherwise. |
| 72 |
type: str |
| 73 |
|
| 74 |
private_key: |
| 75 |
description: |
| 76 |
- Private key certificate in PEM format. |
| 77 |
- Required when installing a CA-signed certificate. Ignored otherwise. |
| 78 |
type: str |
| 79 |
|
| 80 |
signing_request: |
| 81 |
description: |
| 82 |
- If present, the certificate identified by name and svm is used to sign the request. |
| 83 |
- A signed certificate is returned. |
| 84 |
type: str |
| 85 |
|
| 86 |
expiry_time: |
| 87 |
description: |
| 88 |
- Certificate expiration time. Specifying an expiration time is recommended when creating a certificate. |
| 89 |
- Can be provided when signing a certificate. |
| 90 |
type: str |
| 91 |
|
| 92 |
key_size: |
| 93 |
description: |
| 94 |
- Key size of the certificate in bits. Specifying a strong key size is recommended when creating a certificate. |
| 95 |
- Ignored for sign and delete. |
| 96 |
type: int |
| 97 |
|
| 98 |
hash_function: |
| 99 |
description: |
| 100 |
- Hashing function. Can be provided when creating a self-signed certificate, when signing a certificate, |
| 101 |
or when generating a certificate signing request. |
| 102 |
- Allowed values are sha1, sha224, sha256, sha384, and sha512. |
| 103 |
type: str |
| 104 |
default: sha256 |
| 105 |
choices: ['sha1', 'sha224', 'sha256', 'sha384', 'sha512'] |
| 106 |
|
| 107 |
intermediate_certificates: |
| 108 |
description: |
| 109 |
- Chain of intermediate Certificates in PEM format. |
| 110 |
- Only valid when installing a certificate. |
| 111 |
type: list |
| 112 |
elements: str |
| 113 |
|
| 114 |
ignore_name_if_not_supported: |
| 115 |
description: |
| 116 |
- ONTAP 9.6 and 9.7 REST API does not support I(name). |
| 117 |
- If set to true, no error is reported if I(name) is present, and I(name) is not used. |
| 118 |
type: bool |
| 119 |
default: true |
| 120 |
version_added: '20.8.0' |
| 121 |
|
| 122 |
algorithm: |
| 123 |
description: |
| 124 |
- Asymmetric Encryption Algorithm. |
| 125 |
type: str |
| 126 |
default: rsa |
| 127 |
choices: ['rsa','ec'] |
| 128 |
version_added: '23.5.0' |
| 129 |
|
| 130 |
security_strength: |
| 131 |
description: |
| 132 |
- Security strength of the certificate in bits. |
| 133 |
type: int |
| 134 |
default: 112 |
| 135 |
choices: [112, 128, 192] |
| 136 |
version_added: '23.5.0' |
| 137 |
|
| 138 |
subject_alternatives: |
| 139 |
description: |
| 140 |
- Subject alternative names for the certificate. |
| 141 |
type: dict |
| 142 |
suboptions: |
| 143 |
email: |
| 144 |
description: |
| 145 |
- List of email subject alternative names. |
| 146 |
type: list |
| 147 |
elements: str |
| 148 |
dns: |
| 149 |
description: |
| 150 |
- List of DNS subject alternative names. |
| 151 |
type: list |
| 152 |
elements: str |
| 153 |
ip: |
| 154 |
description: |
| 155 |
- List of IP subject alternative names. |
| 156 |
type: list |
| 157 |
elements: str |
| 158 |
uri: |
| 159 |
description: |
| 160 |
- List of URI subject alternative names. |
| 161 |
type: list |
| 162 |
elements: str |
| 163 |
version_added: '23.5.0' |
| 164 |
|
| 165 |
subject_name: |
| 166 |
description: |
| 167 |
- Subject name details of the certificate. The format is a list of comma separated key=value pairs. |
| 168 |
type: str |
| 169 |
version_added: '23.5.0' |
| 170 |
|
| 171 |
extended_key_usages: |
| 172 |
description: |
| 173 |
- A list of extended key usage extensions. |
| 174 |
type: list |
| 175 |
elements: str |
| 176 |
choices: ['serverauth', 'clientauth', 'timestamping', 'dvcs', 'ocspsigning', 'codesigning', 'emailprotection', 'anyextendedkeyusage', 'critical'] |
| 177 |
version_added: '23.5.0' |
| 178 |
|
| 179 |
generate_csr: |
| 180 |
description: |
| 181 |
- Generate a Certificate Signing Request (CSR) instead of creating or installing a certificate. |
| 182 |
- When true, the CSR is generated based on the provided parameters and returned in the output. |
| 183 |
type: bool |
| 184 |
default: false |
| 185 |
version_added: '23.5.0' |
| 186 |
|
| 187 |
key_usages: |
| 188 |
description: |
| 189 |
- A list of key usage extensions. |
| 190 |
type: list |
| 191 |
elements: str |
| 192 |
choices: ['digitalsignature', 'nonrepudiation', 'keyencipherment', 'dataencipherment', |
| 193 |
'keyagreement', 'keycertsign', 'crlsign', 'encipheronly', 'decipheronly', 'critical'] |
| 194 |
version_added: '23.5.0' |
| 195 |
|
| 196 |
notes: |
| 197 |
- supports check mode. |
| 198 |
- only supports REST. Requires ONTAP 9.6 or later, ONTAP 9.8 or later is recommended. |
| 199 |
- Module is not idempotent when generating CSR. |
| 200 |
''' |
| 201 |
|
| 202 |
EXAMPLES = """ |
| 203 |
- name: install certificate |
| 204 |
netapp.ontap.na_ontap_security_certificates: |
| 205 |
# <<: *cert_login |
| 206 |
common_name: "{{ ontap_cert_common_name }}" |
| 207 |
name: "{{ ontap_cert_name }}" |
| 208 |
public_certificate: "{{ ssl_certificate }}" |
| 209 |
type: client_ca |
| 210 |
svm: "{{ vserver }}" |
| 211 |
|
| 212 |
# ignore svm option for cluster/admin vserver. |
| 213 |
- name: install certificate in cluster vserver. |
| 214 |
netapp.ontap.na_ontap_security_certificates: |
| 215 |
# <<: *cert_login |
| 216 |
common_name: "{{ ontap_cert_common_name }}" |
| 217 |
name: "{{ ontap_cert_name }}" |
| 218 |
public_certificate: "{{ ssl_certificate }}" |
| 219 |
type: client_ca |
| 220 |
|
| 221 |
- name: create certificate |
| 222 |
netapp.ontap.na_ontap_security_certificates: |
| 223 |
# <<: *cert_login |
| 224 |
common_name: "{{ ontap_cert_root_common_name }}" |
| 225 |
name: "{{ ontap_cert_name }}" |
| 226 |
type: root_ca |
| 227 |
svm: "{{ vserver }}" |
| 228 |
expiry_time: P365DT # one year |
| 229 |
|
| 230 |
- name: Create Certificate Signing Request |
| 231 |
tags: sign_request |
| 232 |
netapp.ontap.na_ontap_security_certificates: |
| 233 |
# <<: *login |
| 234 |
generate_csr: true |
| 235 |
subject_name: "C=US,O=NTAP,CN=test.domain.com" |
| 236 |
security_strength: 128 |
| 237 |
hash_function: "sha256" |
| 238 |
algorithm: "rsa" |
| 239 |
name: "test_cert" |
| 240 |
key_usages: "digitalsignature,keyencipherment" |
| 241 |
extended_key_usages: "serverauth,clientauth" |
| 242 |
subject_alternatives: |
| 243 |
dns: |
| 244 |
- main.example.com |
| 245 |
- www.example.com |
| 246 |
- api.example.com |
| 247 |
email: |
| 248 |
- [email protected] |
| 249 |
- [email protected] |
| 250 |
ip: |
| 251 |
- 192.168.1.100 |
| 252 |
- 10.0.0.50 |
| 253 |
uri: |
| 254 |
- https://example.com |
| 255 |
- https://api.example.com |
| 256 |
|
| 257 |
- name: sign certificate using newly create certificate |
| 258 |
tags: sign_request |
| 259 |
netapp.ontap.na_ontap_security_certificates: |
| 260 |
# <<: *login |
| 261 |
name: "{{ ontap_cert_name }}" |
| 262 |
svm: "{{ vserver }}" |
| 263 |
signing_request: | |
| 264 |
-----BEGIN CERTIFICATE REQUEST----- |
| 265 |
MIIChDCCAWwCAQAwPzELMAkGA1UEBhMCVVMxCzAJBgNVBAgMAkNBMRIwEAYDVQQH |
| 266 |
DAlTdW5ueXZhbGUxDzANBgNVBAoMBk5ldEFwcDCCASIwDQYJKoZIhvcNAQEBBQAD |
| 267 |
ggEPADCCAQoCggEBALgXCj6Si/I4xLdV7wjWYTbt8jY20fQOjk/4E7yBT1vFBflE |
| 268 |
ks6YDc6dhC2G18cnoj9E3DiR8lIHPoAlFB/VmBNDev3GZkbFlrbV7qYmf8OEx2H2 |
| 269 |
tAefgSP0jLmCHCN1yyhJoCG6FsAiD3tf6yoyFF6qS9ureGL0tCJJ/osx64WzUz+Q |
| 270 |
EN8lx7VSxriEFMSjreXZDhUFaCdIYKKRENuEWyYvdy5cbBmczhuM8EP6peOVv5Hm |
| 271 |
BJzPUDkq7oTtEHmttpATq2Y92qzNzETO0bXN5X/93AWri8/yEXdX+HEw1C/omtsE |
| 272 |
jGsCXrCrIJ+DgUdT/GHNdBWlXl/cWGtEgEQ4vrUCAwEAAaAAMA0GCSqGSIb3DQEB |
| 273 |
CwUAA4IBAQBjZNoQgr/JDm1T8zyRhLkl3zw4a16qKNu/MS7prqZHLVQgrptHRegU |
| 274 |
Hbz11XoHfVOdbyuvtzEe95QsDd6FYCZ4qzZRF3se4IjMeqwdQZ5WP0/GFiwM8Uln |
| 275 |
/0TCWjt759XMeUX7+wgOg5NRjJ660eWMXzu/UJf+vZO0Q2FiPIr13JvvY3TjT+9J |
| 276 |
UUtK4r9PaUuOPN2YL9IQqSD3goh8302Qr3nBXUgjeUGLkgfUM5S39apund2hyTX2 |
| 277 |
JCLQsKr88pwU9iDho2tHLv/2QgLwNZLPu8V+7IGu6G4vB28lN4Uy7xbhxFOKtyWu |
| 278 |
fK4sEdTw3B/aDN0tB8MHFdPYycNZsEac |
| 279 |
-----END CERTIFICATE REQUEST----- |
| 280 |
expiry_time: P180DT |
| 281 |
|
| 282 |
- name: delete certificate |
| 283 |
netapp.ontap.na_ontap_security_certificates: |
| 284 |
# <<: *cert_login |
| 285 |
state: absent |
| 286 |
name: "{{ ontap_cert_name }}" |
| 287 |
svm: "{{ vserver }}" |
| 288 |
|
| 289 |
# For ONTAP 9.6 or 9.7, use common_name and type, in addition to, or in lieu of name |
| 290 |
- name: install certificate |
| 291 |
netapp.ontap.na_ontap_security_certificates: |
| 292 |
# <<: *cert_login |
| 293 |
common_name: "{{ ontap_cert_common_name }}" |
| 294 |
public_certificate: "{{ ssl_certificate }}" |
| 295 |
type: client_ca |
| 296 |
svm: "{{ vserver }}" |
| 297 |
|
| 298 |
- name: create certificate |
| 299 |
netapp.ontap.na_ontap_security_certificates: |
| 300 |
# <<: *cert_login |
| 301 |
common_name: "{{ ontap_cert_root_common_name }}" |
| 302 |
type: root_ca |
| 303 |
svm: "{{ vserver }}" |
| 304 |
expiry_time: P365DT # one year |
| 305 |
|
| 306 |
- name: sign certificate using newly create certificate |
| 307 |
tags: sign_request |
| 308 |
netapp.ontap.na_ontap_security_certificates: |
| 309 |
# <<: *login |
| 310 |
common_name: "{{ ontap_cert_root_common_name }}" |
| 311 |
type: root_ca |
| 312 |
svm: "{{ vserver }}" |
| 313 |
signing_request: | |
| 314 |
-----BEGIN CERTIFICATE REQUEST----- |
| 315 |
MIIChDCCAWwCAQAwPzELMAkGA1UEBhMCVVMxCzAJBgNVBAgMAkNBMRIwEAYDVQQH |
| 316 |
DAlTdW5ueXZhbGUxDzANBgNVBAoMBk5ldEFwcDCCASIwDQYJKoZIhvcNAQEBBQAD |
| 317 |
ggEPADCCAQoCggEBALgXCj6Si/I4xLdV7wjWYTbt8jY20fQOjk/4E7yBT1vFBflE |
| 318 |
ks6YDc6dhC2G18cnoj9E3DiR8lIHPoAlFB/VmBNDev3GZkbFlrbV7qYmf8OEx2H2 |
| 319 |
tAefgSP0jLmCHCN1yyhJoCG6FsAiD3tf6yoyFF6qS9ureGL0tCJJ/osx64WzUz+Q |
| 320 |
EN8lx7VSxriEFMSjreXZDhUFaCdIYKKRENuEWyYvdy5cbBmczhuM8EP6peOVv5Hm |
| 321 |
BJzPUDkq7oTtEHmttpATq2Y92qzNzETO0bXN5X/93AWri8/yEXdX+HEw1C/omtsE |
| 322 |
jGsCXrCrIJ+DgUdT/GHNdBWlXl/cWGtEgEQ4vrUCAwEAAaAAMA0GCSqGSIb3DQEB |
| 323 |
CwUAA4IBAQBjZNoQgr/JDm1T8zyRhLkl3zw4a16qKNu/MS7prqZHLVQgrptHRegU |
| 324 |
Hbz11XoHfVOdbyuvtzEe95QsDd6FYCZ4qzZRF3se4IjMeqwdQZ5WP0/GFiwM8Uln |
| 325 |
/0TCWjt759XMeUX7+wgOg5NRjJ660eWMXzu/UJf+vZO0Q2FiPIr13JvvY3TjT+9J |
| 326 |
UUtK4r9PaUuOPN2YL9IQqSD3goh8302Qr3nBXUgjeUGLkgfUM5S39apund2hyTX2 |
| 327 |
JCLQsKr88pwU9iDho2tHLv/2QgLwNZLPu8V+7IGu6G4vB28lN4Uy7xbhxFOKtyWu |
| 328 |
fK4sEdTw3B/aDN0tB8MHFdPYycNZsEac |
| 329 |
-----END CERTIFICATE REQUEST----- |
| 330 |
expiry_time: P180DT |
| 331 |
|
| 332 |
- name: delete certificate |
| 333 |
netapp.ontap.na_ontap_security_certificates: |
| 334 |
# <<: *cert_login |
| 335 |
state: absent |
| 336 |
common_name: "{{ ontap_cert_root_common_name }}" |
| 337 |
type: root_ca |
| 338 |
name: "{{ ontap_cert_name }}" |
| 339 |
svm: "{{ vserver }}" |
| 340 |
|
| 341 |
- name: install certificate - server certificate with chain of intermediate certificates |
| 342 |
netapp.ontap.na_ontap_security_certificates: |
| 343 |
# <<: *cert_login |
| 344 |
common_name: "{{ ontap_cert_common_name }}" |
| 345 |
public_certificate: "{{ ssl_certificate }}" |
| 346 |
type: server |
| 347 |
svm: "{{ vserver }}" |
| 348 |
private_key: "-----BEGIN CERTIFICATE-----\nPrivate Key\n-----END CERTIFICATE-----" |
| 349 |
intermediat_certificates: ["-----BEGIN CERTIFICATE-----\nIntermediate certificate1\n-----END CERTIFICATE-----", |
| 350 |
"-----BEGIN CERTIFICATE-----\nIntermediate certificate2\n-----END CERTIFICATE-----", |
| 351 |
"-----BEGIN CERTIFICATE-----\nRoot certificate\n-----END CERTIFICATE-----"] |
| 352 |
""" |
| 353 |
|
| 354 |
RETURN = """ |
| 355 |
ontap_info: |
| 356 |
description: Returns public_certificate when signing, empty for create, install, and delete. |
| 357 |
returned: always |
| 358 |
type: dict |
| 359 |
sample: '{ |
| 360 |
"ontap_info": { |
| 361 |
"public_certificate": "-----BEGIN CERTIFICATE-----\n........-----END CERTIFICATE-----\n" |
| 362 |
} |
| 363 |
}' |
| 364 |
""" |
| 365 |
|
| 366 |
from ansible.module_utils.basic import AnsibleModule |
| 367 |
import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils |
| 368 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule |
| 369 |
from ansible_collections.netapp.ontap.plugins.module_utils import rest_vserver |
| 370 |
|
| 371 |
|
| 372 |
class NetAppOntapSecurityCertificates: |
| 373 |
''' object initialize and class methods ''' |
| 374 |
|
| 375 |
def __init__(self): |
| 376 |
self.use_rest = False |
| 377 |
self.argument_spec = netapp_utils.na_ontap_host_argument_spec() |
| 378 |
self.argument_spec.update(dict( |
| 379 |
common_name=dict(required=False, type='str'), |
| 380 |
name=dict(required=False, type='str'), |
| 381 |
state=dict(required=False, choices=['present', 'absent'], default='present'), |
| 382 |
type=dict(required=False, choices=['client', 'server', 'client_ca', 'server_ca', 'root_ca']), |
| 383 |
svm=dict(required=False, type='str', aliases=['vserver']), |
| 384 |
public_certificate=dict(required=False, type='str'), |
| 385 |
private_key=dict(required=False, type='str', no_log=True), |
| 386 |
signing_request=dict(required=False, type='str'), |
| 387 |
expiry_time=dict(required=False, type='str'), |
| 388 |
key_size=dict(required=False, type='int'), |
| 389 |
intermediate_certificates=dict(required=False, type='list', elements='str'), |
| 390 |
ignore_name_if_not_supported=dict(required=False, type='bool', default=True), |
| 391 |
|
| 392 |
generate_csr=dict(required=False, type='bool', default=False), |
| 393 |
subject_name=dict(required=False, type='str'), |
| 394 |
subject_alternatives=dict(required=False, type='dict', options=dict( |
| 395 |
email=dict(required=False, type='list', elements='str'), |
| 396 |
dns=dict(required=False, type='list', elements='str'), |
| 397 |
ip=dict(required=False, type='list', elements='str'), |
| 398 |
uri=dict(required=False, type='list', elements='str') |
| 399 |
)), |
| 400 |
security_strength=dict(required=False, type='int', default=112, choices=[112, 128, 192]), |
| 401 |
hash_function=dict(required=False, type='str', default='sha256', |
| 402 |
choices=['sha1', 'sha224', 'sha256', 'sha384', 'sha512']), |
| 403 |
algorithm=dict(required=False, type='str', default='rsa', choices=['rsa', 'ec']), |
| 404 |
key_usages=dict(required=False, type='list', elements='str', |
| 405 |
choices=['digitalsignature', 'nonrepudiation', 'keyencipherment', 'dataencipherment', |
| 406 |
'keyagreement', 'keycertsign', 'crlsign', 'encipheronly', 'decipheronly', 'critical']), |
| 407 |
extended_key_usages=dict(required=False, type='list', elements='str', |
| 408 |
choices=['serverauth', 'clientauth', 'timestamping', 'dvcs', 'ocspsigning', 'codesigning', |
| 409 |
'emailprotection', 'anyextendedkeyusage', 'critical']), |
| 410 |
)) |
| 411 |
|
| 412 |
self.module = AnsibleModule( |
| 413 |
argument_spec=self.argument_spec, |
| 414 |
supports_check_mode=True |
| 415 |
) |
| 416 |
|
| 417 |
self.na_helper = NetAppModule() |
| 418 |
self.parameters = self.na_helper.set_parameters(self.module.params) |
| 419 |
|
| 420 |
if self.parameters.get('name') is None and (self.parameters.get('common_name') is None or self.parameters.get('type') is None): |
| 421 |
error = "Error: 'name' or ('common_name' and 'type') are required parameters." |
| 422 |
self.module.fail_json(msg=error) |
| 423 |
# ONTAP 9.6 and 9.7 do not support name. We'll change this to True if we detect an issue. |
| 424 |
self.ignore_name_param = False |
| 425 |
self.rest_api = netapp_utils.OntapRestAPI(self.module) |
| 426 |
self.use_rest = self.rest_api.is_rest() |
| 427 |
# API should be used for ONTAP 9.6 or higher |
| 428 |
self.rest_api.fail_if_not_rest_minimum_version('na_ontap_security_certificates', 9, 6) |
| 429 |
|
| 430 |
if not self.use_rest: |
| 431 |
self.module.fail_json(msg="This module require REST with ONTAP 9.6 or higher") |
| 432 |
if self.parameters.get('generate_csr') and not self.rest_api.meets_rest_minimum_version(self.use_rest, 9, 8, 0): |
| 433 |
self.module.fail_json(msg=self.rest_api.options_require_ontap_version('generate_csr', '9.8', use_rest=self.use_rest)) |
| 434 |
|
| 435 |
def get_certificate(self): |
| 436 |
""" |
| 437 |
Fetch uuid if certificate exists. |
| 438 |
NOTE: because of a bug in ONTAP 9.6 and 9.7, name is not supported. We are |
| 439 |
falling back to using common_name and type, but unicity is not guaranteed. |
| 440 |
:return: |
| 441 |
Dictionary if certificate with same name is found |
| 442 |
None if not found |
| 443 |
""" |
| 444 |
# REST allows setting cluster/admin svm in create certificate, but no records returned in GET. |
| 445 |
# error if data svm not found |
| 446 |
if 'svm' in self.parameters: |
| 447 |
rest_vserver.get_vserver_uuid(self.rest_api, self.parameters['svm'], self.module, True) |
| 448 |
|
| 449 |
error = "'name' or ('common_name', 'type') are required." |
| 450 |
for key in ('name', 'common_name'): |
| 451 |
if self.parameters.get(key) is None: |
| 452 |
continue |
| 453 |
data = {'fields': 'uuid', |
| 454 |
key: self.parameters[key], |
| 455 |
} |
| 456 |
if self.parameters.get('svm') is not None: |
| 457 |
data['svm.name'] = self.parameters['svm'] |
| 458 |
else: |
| 459 |
data['scope'] = 'cluster' |
| 460 |
if key == 'common_name': |
| 461 |
if self.parameters.get('type') is not None: |
| 462 |
data['type'] = self.parameters['type'] |
| 463 |
else: |
| 464 |
error = "When using 'common_name', 'type' is required." |
| 465 |
break |
| 466 |
|
| 467 |
api = "security/certificates" |
| 468 |
message, error = self.rest_api.get(api, data) |
| 469 |
if error: |
| 470 |
try: |
| 471 |
name_not_supported_error = (key == 'name') and (error['message'] == 'Unexpected argument "name".') |
| 472 |
except (KeyError, TypeError): |
| 473 |
name_not_supported_error = False |
| 474 |
if name_not_supported_error: |
| 475 |
if self.parameters['ignore_name_if_not_supported'] and self.parameters.get('common_name') is not None: |
| 476 |
# let's attempt a retry using common_name |
| 477 |
self.ignore_name_param = True |
| 478 |
continue |
| 479 |
error = "ONTAP 9.6 and 9.7 do not support 'name'. Use 'common_name' and 'type' as a work-around." |
| 480 |
# report success, or any other error as is |
| 481 |
break |
| 482 |
|
| 483 |
if error: |
| 484 |
self.module.fail_json(msg='Error calling API: %s - %s' % (api, error)) |
| 485 |
|
| 486 |
if len(message['records']) == 1: |
| 487 |
return message['records'][0] |
| 488 |
if len(message['records']) > 1: |
| 489 |
error = 'Duplicate records with same common_name are preventing safe operations: %s' % repr(message) |
| 490 |
self.module.fail_json(msg=error) |
| 491 |
return None |
| 492 |
|
| 493 |
def create_or_install_certificate(self, validate_only=False): |
| 494 |
""" |
| 495 |
Create or install certificate |
| 496 |
:return: message (should be empty dict) |
| 497 |
""" |
| 498 |
required_keys = ['type', 'common_name'] |
| 499 |
if validate_only: |
| 500 |
if not set(required_keys).issubset(set(self.parameters.keys())): |
| 501 |
self.module.fail_json(msg='Error creating or installing certificate: one or more of the following options are missing: %s' |
| 502 |
% (', '.join(required_keys))) |
| 503 |
return |
| 504 |
|
| 505 |
optional_keys = ['public_certificate', 'private_key', 'expiry_time', 'key_size', 'hash_function', 'intermediate_certificates'] |
| 506 |
if not self.ignore_name_param: |
| 507 |
optional_keys.append('name') |
| 508 |
# special key: svm |
| 509 |
|
| 510 |
body = {} |
| 511 |
if self.parameters.get('svm') is not None: |
| 512 |
body['svm'] = {'name': self.parameters['svm']} |
| 513 |
for key in required_keys + optional_keys: |
| 514 |
if self.parameters.get(key) is not None: |
| 515 |
body[key] = self.parameters[key] |
| 516 |
params = { |
| 517 |
"return_records": "true" |
| 518 |
} |
| 519 |
api = "security/certificates" |
| 520 |
message, error = self.rest_api.post(api, body, params) |
| 521 |
if error: |
| 522 |
if self.parameters.get('svm') is None and error.get('target') == 'uuid': |
| 523 |
error['target'] = 'cluster' |
| 524 |
if error.get('message') == 'duplicate entry': |
| 525 |
error['message'] += '. Same certificate may already exist under a different name.' |
| 526 |
self.module.fail_json(msg="Error creating or installing certificate: %s" % error) |
| 527 |
return message |
| 528 |
|
| 529 |
def generate_csr_rest(self): |
| 530 |
""" |
| 531 |
Generate Certificate Signing Request using REST API |
| 532 |
""" |
| 533 |
api = 'security/certificate-signing-request' |
| 534 |
|
| 535 |
body = {} |
| 536 |
if self.parameters.get('subject_name'): |
| 537 |
body['subject_name'] = self.parameters['subject_name'] |
| 538 |
if self.parameters.get('subject_alternatives'): |
| 539 |
body['subject_alternatives'] = self.parameters['subject_alternatives'] |
| 540 |
# Validate required parameters |
| 541 |
if not body.get('subject_name') and not body.get('subject_alternatives'): |
| 542 |
self.module.fail_json(msg="Either subject_name or subject_alternatives must be specified for CSR generation") |
| 543 |
for key in ('security_strength', 'hash_function', 'algorithm', 'key_usages', 'extended_key_usages'): |
| 544 |
if key in self.parameters: |
| 545 |
body[key] = self.parameters[key] |
| 546 |
csr_response, error = self.rest_api.post(api, body) |
| 547 |
if error: |
| 548 |
self.module.fail_json(msg="Error generating CSR: %s" % error) |
| 549 |
return csr_response |
| 550 |
|
| 551 |
def validate_csr_parameters(self): |
| 552 |
""" |
| 553 |
Validate CSR-specific parameters |
| 554 |
""" |
| 555 |
if not self.parameters.get('subject_name') and not self.parameters.get('subject_alternatives'): |
| 556 |
self.module.fail_json( |
| 557 |
msg="Either subject_name or subject_alternatives must be specified when generate_csr=true" |
| 558 |
) |
| 559 |
|
| 560 |
if self.parameters.get('algorithm') == 'ec' and self.parameters.get('security_strength') not in [128, 192]: |
| 561 |
self.module.fail_json( |
| 562 |
msg="Value of at least 128 is necessary when using EC algorithm if the certificate is to be used in the context of TLSv1.3." |
| 563 |
) |
| 564 |
|
| 565 |
def sign_certificate(self, uuid): |
| 566 |
""" |
| 567 |
sign certificate |
| 568 |
:return: a dictionary with key "public_certificate" |
| 569 |
""" |
| 570 |
api = "security/certificates/%s/sign" % uuid |
| 571 |
body = {'signing_request': self.parameters['signing_request']} |
| 572 |
optional_keys = ['expiry_time', 'hash_function'] |
| 573 |
for key in optional_keys: |
| 574 |
if self.parameters.get(key) is not None: |
| 575 |
body[key] = self.parameters[key] |
| 576 |
params = { |
| 577 |
"return_records": "true" |
| 578 |
} |
| 579 |
message, error = self.rest_api.post(api, body, params) |
| 580 |
if error: |
| 581 |
self.module.fail_json(msg="Error signing certificate: %s" % error) |
| 582 |
return message |
| 583 |
|
| 584 |
def delete_certificate(self, uuid): |
| 585 |
""" |
| 586 |
Delete certificate |
| 587 |
:return: message (should be empty dict) |
| 588 |
""" |
| 589 |
api = "security/certificates/%s" % uuid |
| 590 |
message, error = self.rest_api.delete(api) |
| 591 |
if error: |
| 592 |
self.module.fail_json(msg="Error deleting certificate: %s" % error) |
| 593 |
return message |
| 594 |
|
| 595 |
def apply(self): |
| 596 |
""" |
| 597 |
Apply action to create/install/sign/delete certificate |
| 598 |
:return: None |
| 599 |
""" |
| 600 |
# TODO: add telemetry for REST |
| 601 |
|
| 602 |
if self.parameters.get('generate_csr'): |
| 603 |
self.validate_csr_parameters() |
| 604 |
generated_csr = self.generate_csr_rest() |
| 605 |
self.module.exit_json( |
| 606 |
changed=True, |
| 607 |
msg="CSR generated successfully", |
| 608 |
csr=generated_csr.get('csr', ''), |
| 609 |
private_key=generated_csr.get('generated_private_key', '') |
| 610 |
) |
| 611 |
|
| 612 |
current = self.get_certificate() |
| 613 |
cd_action = self.na_helper.get_cd_action(current, self.parameters) |
| 614 |
message = None |
| 615 |
if self.parameters.get('signing_request') is not None: |
| 616 |
error = None |
| 617 |
if self.parameters['state'] == 'absent': |
| 618 |
error = "'signing_request' is not supported with 'state' set to 'absent'" |
| 619 |
elif current is None: |
| 620 |
scope = 'cluster' if self.parameters.get('svm') is None else "svm: %s" % self.parameters.get('svm') |
| 621 |
error = "signing certificate with name '%s' not found on %s" % (self.parameters.get('name'), scope) |
| 622 |
elif cd_action is not None: |
| 623 |
error = "'signing_request' is exclusive with other actions: create, install, delete" |
| 624 |
if error is not None: |
| 625 |
self.module.fail_json(msg=error) |
| 626 |
cd_action = 'sign' |
| 627 |
self.na_helper.changed = True |
| 628 |
|
| 629 |
if self.na_helper.changed and cd_action == 'create': |
| 630 |
# validate as much as we can in check_mode or not |
| 631 |
self.create_or_install_certificate(validate_only=True) |
| 632 |
|
| 633 |
if self.na_helper.changed and not self.module.check_mode: |
| 634 |
if cd_action == 'create': |
| 635 |
message = self.create_or_install_certificate() |
| 636 |
elif cd_action == 'sign': |
| 637 |
message = self.sign_certificate(current['uuid']) |
| 638 |
elif cd_action == 'delete': |
| 639 |
message = self.delete_certificate(current['uuid']) |
| 640 |
|
| 641 |
results = netapp_utils.generate_result(self.na_helper.changed, cd_action, extra_responses={'ontap_info': message}) |
| 642 |
self.module.exit_json(**results) |
| 643 |
|
| 644 |
|
| 645 |
def main(): |
| 646 |
""" |
| 647 |
Create instance and invoke apply |
| 648 |
:return: None |
| 649 |
""" |
| 650 |
sec_cert = NetAppOntapSecurityCertificates() |
| 651 |
sec_cert.apply() |
| 652 |
|
| 653 |
|
| 654 |
if __name__ == '__main__': |
| 655 |
main() |