| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
# (c) 2022, Florent David (@Ripolin) |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import (absolute_import, division, print_function) |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
DOCUMENTATION = """ |
| 11 |
module: vault_pki_generate_certificate |
| 12 |
version_added: 2.3.0 |
| 13 |
author: |
| 14 |
- Florent David (@Ripolin) |
| 15 |
short_description: Generates a new set of credentials (private key and certificate) using HashiCorp Vault PKI |
| 16 |
requirements: |
| 17 |
- C(hvac) (L(Python library,https://hvac.readthedocs.io/en/stable/changelog.html#may-25th-2019)) version C(0.9.1) or higher |
| 18 |
- For detailed requirements, see R(the collection requirements page,ansible_collections.community.hashi_vault.docsite.user_guide.requirements). |
| 19 |
description: |
| 20 |
- Generates a new set of credentials (private key and certificate) based on a Vault PKI role. |
| 21 |
seealso: |
| 22 |
- name: HashiCorp Vault PKI Secrets Engine API |
| 23 |
description: API documentation for the HashiCorp Vault PKI secrets engine. |
| 24 |
link: https://www.vaultproject.io/api/secret/pki#generate-certificate |
| 25 |
- name: HVAC library reference |
| 26 |
description: HVAC library reference about the PKI engine. |
| 27 |
link: https://hvac.readthedocs.io/en/stable/usage/secrets_engines/pki.html#generate-certificate |
| 28 |
extends_documentation_fragment: |
| 29 |
- community.hashi_vault.attributes |
| 30 |
- community.hashi_vault.attributes.action_group |
| 31 |
- community.hashi_vault.connection |
| 32 |
- community.hashi_vault.auth |
| 33 |
- community.hashi_vault.engine_mount |
| 34 |
attributes: |
| 35 |
check_mode: |
| 36 |
support: partial |
| 37 |
details: |
| 38 |
- In check mode, this module will not contact Vault and will return an empty C(data) field and C(changed) status. |
| 39 |
options: |
| 40 |
alt_names: |
| 41 |
description: |
| 42 |
- Specifies requested Subject Alternative Names. |
| 43 |
- These can be host names or email addresses; they will be parsed into their respective fields. |
| 44 |
- If any requested names do not match role policy, the entire request will be denied. |
| 45 |
type: list |
| 46 |
elements: str |
| 47 |
default: [] |
| 48 |
common_name: |
| 49 |
description: |
| 50 |
- Specifies the requested CN for the certificate. |
| 51 |
- If the CN is allowed by role policy, it will be issued. |
| 52 |
type: str |
| 53 |
required: true |
| 54 |
exclude_cn_from_sans: |
| 55 |
description: |
| 56 |
- If true, the given I(common_name) will not be included in DNS or Email Subject Alternate Names (as appropriate). |
| 57 |
- Useful if the CN is not a hostname or email address, but is instead some human-readable identifier. |
| 58 |
type: bool |
| 59 |
default: False |
| 60 |
format: |
| 61 |
description: |
| 62 |
- Specifies the format for returned data. |
| 63 |
- Can be C(pem), C(der), or C(pem_bundle). |
| 64 |
- If C(der), the output is base64 encoded. |
| 65 |
- >- |
| 66 |
If C(pem_bundle), the C(certificate) field will contain the private key and certificate, concatenated. If the issuing CA is not a Vault-derived |
| 67 |
self-signed root, this will be included as well. |
| 68 |
type: str |
| 69 |
choices: [pem, der, pem_bundle] |
| 70 |
default: pem |
| 71 |
ip_sans: |
| 72 |
description: |
| 73 |
- Specifies requested IP Subject Alternative Names. |
| 74 |
- Only valid if the role allows IP SANs (which is the default). |
| 75 |
type: list |
| 76 |
elements: str |
| 77 |
default: [] |
| 78 |
role_name: |
| 79 |
description: |
| 80 |
- Specifies the name of the role to create the certificate against. |
| 81 |
type: str |
| 82 |
required: true |
| 83 |
other_sans: |
| 84 |
description: |
| 85 |
- Specifies custom OID/UTF8-string SANs. |
| 86 |
- These must match values specified on the role in C(allowed_other_sans). |
| 87 |
- "The format is the same as OpenSSL: C(<oid>;<type>:<value>) where the only current valid type is C(UTF8)." |
| 88 |
type: list |
| 89 |
elements: str |
| 90 |
default: [] |
| 91 |
engine_mount_point: |
| 92 |
description: |
| 93 |
- Specify the mount point used by the PKI engine. |
| 94 |
- Defaults to the default used by C(hvac). |
| 95 |
private_key_format: |
| 96 |
description: |
| 97 |
- Specifies the format for marshaling the private key. |
| 98 |
- Defaults to C(der) which will return either base64-encoded DER or PEM-encoded DER, depending on the value of I(format). |
| 99 |
- The other option is C(pkcs8) which will return the key marshalled as PEM-encoded PKCS8. |
| 100 |
type: str |
| 101 |
choices: [der, pkcs8] |
| 102 |
default: der |
| 103 |
ttl: |
| 104 |
description: |
| 105 |
- Specifies requested Time To Live. |
| 106 |
- Cannot be greater than the role's C(max_ttl) value. |
| 107 |
- If not provided, the role's C(ttl) value will be used. |
| 108 |
- Note that the role values default to system values if not explicitly set. |
| 109 |
type: str |
| 110 |
uri_sans: |
| 111 |
description: |
| 112 |
- Specifies the requested URI Subject Alternative Names. |
| 113 |
type: list |
| 114 |
elements: str |
| 115 |
default: [] |
| 116 |
""" |
| 117 |
|
| 118 |
EXAMPLES = """ |
| 119 |
- name: Login and use the resulting token |
| 120 |
community.hashi_vault.vault_login: |
| 121 |
url: https://localhost:8200 |
| 122 |
auth_method: ldap |
| 123 |
username: "john.doe" |
| 124 |
password: "{{ user_passwd }}" |
| 125 |
register: login_data |
| 126 |
|
| 127 |
- name: Generate a certificate with an existing token |
| 128 |
community.hashi_vault.vault_pki_generate_certificate: |
| 129 |
role_name: test.example.org |
| 130 |
common_name: test.example.org |
| 131 |
ttl: 8760h |
| 132 |
alt_names: |
| 133 |
- test2.example.org |
| 134 |
- test3.example.org |
| 135 |
url: https://vault:8201 |
| 136 |
auth_method: token |
| 137 |
token: "{{ login_data.login.auth.client_token }}" |
| 138 |
register: cert_data |
| 139 |
|
| 140 |
- name: Display generated certificate |
| 141 |
debug: |
| 142 |
msg: "{{ cert_data.data.data.certificate }}" |
| 143 |
""" |
| 144 |
|
| 145 |
RETURN = """ |
| 146 |
data: |
| 147 |
description: Information about newly generated certificate |
| 148 |
returned: success |
| 149 |
type: complex |
| 150 |
contains: |
| 151 |
lease_id: |
| 152 |
description: Vault lease attached to certificate. |
| 153 |
returned: success |
| 154 |
type: str |
| 155 |
sample: pki/issue/test/7ad6cfa5-f04f-c62a-d477-f33210475d05 |
| 156 |
renewable: |
| 157 |
description: True if certificate is renewable. |
| 158 |
returned: success |
| 159 |
type: bool |
| 160 |
sample: false |
| 161 |
lease_duration: |
| 162 |
description: Vault lease duration. |
| 163 |
returned: success |
| 164 |
type: int |
| 165 |
sample: 21600 |
| 166 |
data: |
| 167 |
description: Payload |
| 168 |
returned: success |
| 169 |
type: complex |
| 170 |
contains: |
| 171 |
certificate: |
| 172 |
description: Generated certificate. |
| 173 |
returned: success |
| 174 |
type: str |
| 175 |
sample: "-----BEGIN CERTIFICATE-----...-----END CERTIFICATE-----" |
| 176 |
issuing_ca: |
| 177 |
description: CA certificate. |
| 178 |
returned: success |
| 179 |
type: str |
| 180 |
sample: "-----BEGIN CERTIFICATE-----...-----END CERTIFICATE-----" |
| 181 |
ca_chain: |
| 182 |
description: Linked list of CA certificates. |
| 183 |
returned: success |
| 184 |
type: list |
| 185 |
elements: str |
| 186 |
sample: ["-----BEGIN CERTIFICATE-----...-----END CERTIFICATE-----"] |
| 187 |
private_key: |
| 188 |
description: Private key used to generate certificate. |
| 189 |
returned: success |
| 190 |
type: str |
| 191 |
sample: "-----BEGIN RSA PRIVATE KEY-----...-----END RSA PRIVATE KEY-----" |
| 192 |
private_key_type: |
| 193 |
description: Private key algorithm. |
| 194 |
returned: success |
| 195 |
type: str |
| 196 |
sample: rsa |
| 197 |
serial_number: |
| 198 |
description: Certificate's serial number. |
| 199 |
returned: success |
| 200 |
type: str |
| 201 |
sample: 39:dd:2e:90:b7:23:1f:8d:d3:7d:31:c5:1b:da:84:d0:5b:65:31:58 |
| 202 |
warning: |
| 203 |
description: Warnings returned by Vault during generation. |
| 204 |
returned: success |
| 205 |
type: str |
| 206 |
""" |
| 207 |
|
| 208 |
import traceback |
| 209 |
|
| 210 |
from ansible.module_utils.common.text.converters import to_text |
| 211 |
|
| 212 |
from ansible_collections.community.hashi_vault.plugins.module_utils._hashi_vault_module import HashiVaultModule |
| 213 |
from ansible_collections.community.hashi_vault.plugins.module_utils._hashi_vault_common import HashiVaultValueError |
| 214 |
|
| 215 |
|
| 216 |
def run_module(): |
| 217 |
argspec = HashiVaultModule.generate_argspec( |
| 218 |
role_name=dict(type='str', required=True), |
| 219 |
common_name=dict(type='str', required=True), |
| 220 |
alt_names=dict(type='list', elements='str', required=False, default=[]), |
| 221 |
ip_sans=dict(type='list', elements='str', required=False, default=[]), |
| 222 |
uri_sans=dict(type='list', elements='str', required=False, default=[]), |
| 223 |
other_sans=dict(type='list', elements='str', required=False, default=[]), |
| 224 |
ttl=dict(type='str', required=False, default=None), |
| 225 |
format=dict(type='str', required=False, choices=['pem', 'der', 'pem_bundle'], default='pem'), |
| 226 |
private_key_format=dict(type='str', required=False, choices=['der', 'pkcs8'], default='der'), |
| 227 |
exclude_cn_from_sans=dict(type='bool', required=False, default=False), |
| 228 |
engine_mount_point=dict(type='str', required=False) |
| 229 |
) |
| 230 |
|
| 231 |
module = HashiVaultModule( |
| 232 |
argument_spec=argspec, |
| 233 |
supports_check_mode=True |
| 234 |
) |
| 235 |
|
| 236 |
role_name = module.params.get('role_name') |
| 237 |
common_name = module.params.get('common_name') |
| 238 |
engine_mount_point = module.params.get('engine_mount_point') |
| 239 |
|
| 240 |
extra_params = { |
| 241 |
'alt_names': ','.join(module.params.get('alt_names')), |
| 242 |
'ip_sans': ','.join(module.params.get('ip_sans')), |
| 243 |
'uri_sans': ','.join(module.params.get('uri_sans')), |
| 244 |
'other_sans': ','.join(module.params.get('other_sans')), |
| 245 |
'ttl': module.params.get('ttl'), |
| 246 |
'format': module.params.get('format'), |
| 247 |
'private_key_format': module.params.get('private_key_format'), |
| 248 |
'exclude_cn_from_sans': module.params.get('exclude_cn_from_sans') |
| 249 |
} |
| 250 |
|
| 251 |
module.connection_options.process_connection_options() |
| 252 |
client_args = module.connection_options.get_hvac_connection_options() |
| 253 |
client = module.helper.get_vault_client(**client_args) |
| 254 |
hvac_exceptions = module.helper.get_hvac().exceptions |
| 255 |
|
| 256 |
if engine_mount_point is None: |
| 257 |
from hvac.api.secrets_engines.pki import DEFAULT_MOUNT_POINT |
| 258 |
engine_mount_point = DEFAULT_MOUNT_POINT |
| 259 |
|
| 260 |
try: |
| 261 |
module.authenticator.validate() |
| 262 |
module.authenticator.authenticate(client) |
| 263 |
except (NotImplementedError, HashiVaultValueError) as e: |
| 264 |
module.fail_json(msg=to_text(e), exception=traceback.format_exc()) |
| 265 |
|
| 266 |
try: |
| 267 |
if module.check_mode: |
| 268 |
data = {} |
| 269 |
else: |
| 270 |
data = client.secrets.pki.generate_certificate( |
| 271 |
name=role_name, common_name=common_name, |
| 272 |
extra_params=extra_params, mount_point=engine_mount_point |
| 273 |
) |
| 274 |
except hvac_exceptions.VaultError as e: |
| 275 |
module.fail_json(msg=to_text(e), exception=traceback.format_exc()) |
| 276 |
|
| 277 |
# generate_certificate is a write operation which always return a new certificate |
| 278 |
module.exit_json(changed=True, data=data) |
| 279 |
|
| 280 |
|
| 281 |
def main(): |
| 282 |
run_module() |
| 283 |
|
| 284 |
|
| 285 |
if __name__ == '__main__': |
| 286 |
main() |