| 1 |
#!/usr/bin/python |
| 2 |
# Copyright (c) 2016-2017, Yanis Guenane <[email protected]> |
| 3 |
# Copyright (c) 2017, Markus Teufelberger <[email protected]> |
| 4 |
# Copyright (2) 2020, Felix Fontein <[email protected]> |
| 5 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 7 |
|
| 8 |
from __future__ import annotations |
| 9 |
|
| 10 |
DOCUMENTATION = r""" |
| 11 |
module: x509_certificate_pipe |
| 12 |
short_description: Generate and/or check OpenSSL certificates |
| 13 |
version_added: 1.3.0 |
| 14 |
description: |
| 15 |
- It implements a notion of provider (one of V(selfsigned) and V(ownca)) for your certificate. |
| 16 |
author: |
| 17 |
- Yanis Guenane (@Spredzy) |
| 18 |
- Markus Teufelberger (@MarkusTeufelberger) |
| 19 |
- Felix Fontein (@felixfontein) |
| 20 |
extends_documentation_fragment: |
| 21 |
- community.crypto._attributes |
| 22 |
- community.crypto._module_certificate |
| 23 |
- community.crypto._module_certificate.backend_ownca_documentation |
| 24 |
- community.crypto._module_certificate.backend_selfsigned_documentation |
| 25 |
attributes: |
| 26 |
check_mode: |
| 27 |
support: full |
| 28 |
details: |
| 29 |
- Since community.crypto 3.0.0 the module ignores check mode and always behaves as if check mode is not active. |
| 30 |
options: |
| 31 |
provider: |
| 32 |
description: |
| 33 |
- Name of the provider to use to generate/retrieve the OpenSSL certificate. |
| 34 |
- The V(entrust) provider has been removed from community.crypto 3.0.0 due to sunsetting of the ECS API. |
| 35 |
type: str |
| 36 |
choices: [ownca, selfsigned] |
| 37 |
required: true |
| 38 |
|
| 39 |
content: |
| 40 |
description: |
| 41 |
- The existing certificate. |
| 42 |
type: str |
| 43 |
|
| 44 |
seealso: |
| 45 |
- module: community.crypto.x509_certificate |
| 46 |
""" |
| 47 |
|
| 48 |
EXAMPLES = r""" |
| 49 |
--- |
| 50 |
- name: Generate a Self Signed OpenSSL certificate |
| 51 |
community.crypto.x509_certificate_pipe: |
| 52 |
provider: selfsigned |
| 53 |
privatekey_path: /etc/ssl/private/ansible.com.pem |
| 54 |
csr_path: /etc/ssl/csr/ansible.com.csr |
| 55 |
register: result |
| 56 |
- name: Print the certificate |
| 57 |
ansible.builtin.debug: |
| 58 |
var: result.certificate |
| 59 |
|
| 60 |
# In the following example, both CSR and certificate file are stored on the |
| 61 |
# machine where ansible-playbook is executed, while the OwnCA data (certificate, |
| 62 |
# private key) are stored on the remote machine. |
| 63 |
|
| 64 |
- name: (1/2) Generate an OpenSSL Certificate with the CSR provided inline |
| 65 |
community.crypto.x509_certificate_pipe: |
| 66 |
provider: ownca |
| 67 |
content: "{{ lookup('ansible.builtin.file', '/etc/ssl/csr/www.ansible.com.crt') }}" |
| 68 |
csr_content: "{{ lookup('ansible.builtin.file', '/etc/ssl/csr/www.ansible.com.csr') }}" |
| 69 |
ownca_cert: /path/to/ca_cert.crt |
| 70 |
ownca_privatekey: /path/to/ca_cert.key |
| 71 |
ownca_privatekey_passphrase: hunter2 |
| 72 |
register: result |
| 73 |
|
| 74 |
- name: (2/2) Store certificate |
| 75 |
ansible.builtin.copy: |
| 76 |
dest: /etc/ssl/csr/www.ansible.com.crt |
| 77 |
content: "{{ result.certificate }}" |
| 78 |
delegate_to: localhost |
| 79 |
when: result is changed |
| 80 |
|
| 81 |
# In the following example, the certificate from another machine is signed by |
| 82 |
# our OwnCA whose private key and certificate are only available on this |
| 83 |
# machine (where ansible-playbook is executed), without having to write |
| 84 |
# the certificate file to disk on localhost. The CSR could have been |
| 85 |
# provided by community.crypto.openssl_csr_pipe earlier, or also have been |
| 86 |
# read from the remote machine. |
| 87 |
|
| 88 |
- name: (1/3) Read certificate's contents from remote machine |
| 89 |
ansible.builtin.slurp: |
| 90 |
src: /etc/ssl/csr/www.ansible.com.crt |
| 91 |
register: certificate_content |
| 92 |
|
| 93 |
- name: (2/3) Generate an OpenSSL Certificate with the CSR provided inline |
| 94 |
community.crypto.x509_certificate_pipe: |
| 95 |
provider: ownca |
| 96 |
content: "{{ certificate_content.content | b64decode }}" |
| 97 |
csr_content: "{{ the_csr }}" |
| 98 |
ownca_cert: /path/to/ca_cert.crt |
| 99 |
ownca_privatekey: /path/to/ca_cert.key |
| 100 |
ownca_privatekey_passphrase: hunter2 |
| 101 |
delegate_to: localhost |
| 102 |
register: result |
| 103 |
|
| 104 |
- name: (3/3) Store certificate |
| 105 |
ansible.builtin.copy: |
| 106 |
dest: /etc/ssl/csr/www.ansible.com.crt |
| 107 |
content: "{{ result.certificate }}" |
| 108 |
when: result is changed |
| 109 |
""" |
| 110 |
|
| 111 |
RETURN = r""" |
| 112 |
certificate: |
| 113 |
description: The (current or generated) certificate's content. |
| 114 |
returned: changed or success |
| 115 |
type: str |
| 116 |
""" |
| 117 |
|
| 118 |
import typing as t |
| 119 |
|
| 120 |
from ansible_collections.community.crypto.plugins.module_utils._crypto.basic import ( |
| 121 |
OpenSSLObjectError, |
| 122 |
) |
| 123 |
from ansible_collections.community.crypto.plugins.module_utils._crypto.module_backends.certificate import ( |
| 124 |
get_certificate_argument_spec, |
| 125 |
select_backend, |
| 126 |
) |
| 127 |
from ansible_collections.community.crypto.plugins.module_utils._crypto.module_backends.certificate_ownca import ( |
| 128 |
OwnCACertificateProvider, |
| 129 |
add_ownca_provider_to_argument_spec, |
| 130 |
) |
| 131 |
from ansible_collections.community.crypto.plugins.module_utils._crypto.module_backends.certificate_selfsigned import ( |
| 132 |
SelfSignedCertificateProvider, |
| 133 |
add_selfsigned_provider_to_argument_spec, |
| 134 |
) |
| 135 |
|
| 136 |
if t.TYPE_CHECKING: |
| 137 |
from ansible.module_utils.basic import AnsibleModule # pragma: no cover |
| 138 |
|
| 139 |
from ansible_collections.community.crypto.plugins.module_utils._crypto.module_backends.certificate import ( # pragma: no cover |
| 140 |
CertificateBackend, |
| 141 |
) |
| 142 |
|
| 143 |
|
| 144 |
class GenericCertificate: |
| 145 |
"""Retrieve a certificate using the given module backend.""" |
| 146 |
|
| 147 |
def __init__(self, module: AnsibleModule, module_backend: CertificateBackend): |
| 148 |
self.check_mode = module.check_mode |
| 149 |
self.module = module |
| 150 |
self.module_backend = module_backend |
| 151 |
self.changed = False |
| 152 |
content: str | None = module.params["content"] |
| 153 |
if content is not None: |
| 154 |
self.module_backend.set_existing(content.encode("utf-8")) |
| 155 |
|
| 156 |
def generate(self, module: AnsibleModule) -> None: |
| 157 |
if self.module_backend.needs_regeneration(): |
| 158 |
self.module_backend.generate_certificate() |
| 159 |
self.changed = True |
| 160 |
|
| 161 |
def dump(self, check_mode: bool = False) -> dict[str, t.Any]: |
| 162 |
result = self.module_backend.dump(include_certificate=True) |
| 163 |
result.update( |
| 164 |
{ |
| 165 |
"changed": self.changed, |
| 166 |
} |
| 167 |
) |
| 168 |
return result |
| 169 |
|
| 170 |
|
| 171 |
def main() -> t.NoReturn: |
| 172 |
argument_spec = get_certificate_argument_spec() |
| 173 |
argument_spec.argument_spec["provider"]["required"] = True |
| 174 |
add_ownca_provider_to_argument_spec(argument_spec) |
| 175 |
add_selfsigned_provider_to_argument_spec(argument_spec) |
| 176 |
argument_spec.argument_spec.update( |
| 177 |
{ |
| 178 |
"content": {"type": "str"}, |
| 179 |
} |
| 180 |
) |
| 181 |
module = argument_spec.create_ansible_module( |
| 182 |
supports_check_mode=True, |
| 183 |
) |
| 184 |
|
| 185 |
try: |
| 186 |
provider = module.params["provider"] |
| 187 |
provider_map: dict[ |
| 188 |
str, |
| 189 |
type[OwnCACertificateProvider] | type[SelfSignedCertificateProvider], |
| 190 |
] = { |
| 191 |
"ownca": OwnCACertificateProvider, |
| 192 |
"selfsigned": SelfSignedCertificateProvider, |
| 193 |
} |
| 194 |
|
| 195 |
module_backend = select_backend( |
| 196 |
module=module, provider=provider_map[provider]() |
| 197 |
) |
| 198 |
certificate = GenericCertificate(module, module_backend) |
| 199 |
certificate.generate(module) |
| 200 |
result = certificate.dump() |
| 201 |
module.exit_json(**result) |
| 202 |
except OpenSSLObjectError as exc: |
| 203 |
module.fail_json(msg=str(exc)) |
| 204 |
|
| 205 |
|
| 206 |
if __name__ == "__main__": |
| 207 |
main() |