Debian Codegraph @

ansible_collections/community/crypto/docs/docsite/rst/guide_ownca.rst

raw
1 ..
2 Copyright (c) Ansible Project
3 GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
4 SPDX-License-Identifier: GPL-3.0-or-later
5
6 .. _ansible_collections.community.crypto.docsite.guide_ownca:
7
8 How to create a small CA
9 ========================
10
11 The `community.crypto collection <https://galaxy.ansible.com/ui/repo/published/community/crypto/>`_ offers multiple modules that create private keys, certificate signing requests, and certificates. This guide shows how to create your own small CA and how to use it to sign certificates.
12
13 In all examples, we assume that the CA's private key is password protected, where the password is provided in the ``secret_ca_passphrase`` variable.
14
15 Set up the CA
16 -------------
17
18 Any certificate can be used as a CA certificate. You can create a self-signed certificate (see :ref:`ansible_collections.community.crypto.docsite.guide_selfsigned`), use another CA certificate to sign a new certificate (using the instructions below for signing a certificate), ask (and pay) a commercial CA to sign your CA certificate, etc.
19
20 The following instructions show how to set up a simple self-signed CA certificate.
21
22 .. code-block:: yaml+jinja
23
24 - name: Create private key with password protection
25 community.crypto.openssl_privatekey:
26 path: /path/to/ca-certificate.key
27 passphrase: "{{ secret_ca_passphrase }}"
28
29 - name: Create certificate signing request (CSR) for CA certificate
30 community.crypto.openssl_csr_pipe:
31 privatekey_path: /path/to/ca-certificate.key
32 privatekey_passphrase: "{{ secret_ca_passphrase }}"
33 common_name: Ansible CA
34 use_common_name_for_san: false # since we do not specify SANs, don't use CN as a SAN
35 basic_constraints:
36 - 'CA:TRUE'
37 basic_constraints_critical: true
38 key_usage:
39 - keyCertSign
40 key_usage_critical: true
41 register: ca_csr
42
43 - name: Create self-signed CA certificate from CSR
44 community.crypto.x509_certificate:
45 path: /path/to/ca-certificate.pem
46 csr_content: "{{ ca_csr.csr }}"
47 privatekey_path: /path/to/ca-certificate.key
48 privatekey_passphrase: "{{ secret_ca_passphrase }}"
49 provider: selfsigned
50
51 Use the CA to sign a certificate
52 --------------------------------
53
54 To sign a certificate, you must pass a CSR to the :ansplugin:`community.crypto.x509_certificate module <community.crypto.x509_certificate#module>` or :ansplugin:`community.crypto.x509_certificate_pipe module <community.crypto.x509_certificate_pipe#module>`.
55
56 In the following example, we assume that the certificate to sign (including its private key) are on ``server_1``, while our CA certificate is on ``server_2``. We do not want any key material to leave each respective server.
57
58 .. code-block:: yaml+jinja
59
60 - name: Create private key for new certificate on server_1
61 community.crypto.openssl_privatekey:
62 path: /path/to/certificate.key
63 delegate_to: server_1
64 run_once: true
65
66 - name: Create certificate signing request (CSR) for new certificate
67 community.crypto.openssl_csr_pipe:
68 privatekey_path: /path/to/certificate.key
69 subject_alt_name:
70 - "DNS:ansible.com"
71 - "DNS:www.ansible.com"
72 - "DNS:docs.ansible.com"
73 delegate_to: server_1
74 run_once: true
75 register: csr
76
77 - name: Sign certificate with our CA
78 community.crypto.x509_certificate_pipe:
79 csr_content: "{{ csr.csr }}"
80 provider: ownca
81 ownca_path: /path/to/ca-certificate.pem
82 ownca_privatekey_path: /path/to/ca-certificate.key
83 ownca_privatekey_passphrase: "{{ secret_ca_passphrase }}"
84 ownca_not_after: +365d # valid for one year
85 ownca_not_before: "-1d" # valid since yesterday
86 delegate_to: server_2
87 run_once: true
88 register: certificate
89
90 - name: Write certificate file on server_1
91 copy:
92 dest: /path/to/certificate.pem
93 content: "{{ certificate.certificate }}"
94 delegate_to: server_1
95 run_once: true
96
97 Please note that the above procedure is **not idempotent**. The following extended example reads the existing certificate from ``server_1`` (if exists) and provides it to the :ansplugin:`community.crypto.x509_certificate_pipe module <community.crypto.x509_certificate_pipe#module>`, and only writes the result back if it was changed:
98
99 .. code-block:: yaml+jinja
100
101 - name: Create private key for new certificate on server_1
102 community.crypto.openssl_privatekey:
103 path: /path/to/certificate.key
104 delegate_to: server_1
105 run_once: true
106
107 - name: Create certificate signing request (CSR) for new certificate
108 community.crypto.openssl_csr_pipe:
109 privatekey_path: /path/to/certificate.key
110 subject_alt_name:
111 - "DNS:ansible.com"
112 - "DNS:www.ansible.com"
113 - "DNS:docs.ansible.com"
114 delegate_to: server_1
115 run_once: true
116 register: csr
117
118 - name: Check whether certificate exists
119 stat:
120 path: /path/to/certificate.pem
121 delegate_to: server_1
122 run_once: true
123 register: certificate_exists
124
125 - name: Read existing certificate if exists
126 slurp:
127 src: /path/to/certificate.pem
128 when: certificate_exists.stat.exists
129 delegate_to: server_1
130 run_once: true
131 register: certificate
132
133 - name: Sign certificate with our CA
134 community.crypto.x509_certificate_pipe:
135 content: "{{ (certificate.content | b64decode) if certificate_exists.stat.exists else omit }}"
136 csr_content: "{{ csr.csr }}"
137 provider: ownca
138 ownca_path: /path/to/ca-certificate.pem
139 ownca_privatekey_path: /path/to/ca-certificate.key
140 ownca_privatekey_passphrase: "{{ secret_ca_passphrase }}"
141 ownca_not_after: +365d # valid for one year
142 ownca_not_before: "-1d" # valid since yesterday
143 delegate_to: server_2
144 run_once: true
145 register: certificate
146
147 - name: Write certificate file on server_1
148 copy:
149 dest: /path/to/certificate.pem
150 content: "{{ certificate.certificate }}"
151 delegate_to: server_1
152 run_once: true
153 when: certificate is changed