3e03a007a33cfe8887125cb893b7dbe03a5ae98d
[mirror/dsa-puppet.git] / 3rdparty / modules / certregen / README.markdown
1 # Certregen
2
3 #### Table of Contents
4
5 1. [Overview](#overview)
6 2. [Module Description - What the module does and why it is useful](#module-description)
7 3. [Installing](#installing)
8 4. [Usage - Configuration options and additional functionality](#usage)
9 5. [Reference - An under-the-hood peek at what the module is doing and how](#reference)
10 6. [Limitations - OS compatibility, etc.](#limitations)
11 7. [Development - Guide for contributing to the module](#development)
12
13 ## Overview
14
15 The certregen module can painlessly refresh a certificate authority (CA) that's about to expire. It can also revive a CA that has already expired.
16
17 ## Module Description
18
19 This module is for regenerating and redistributing Puppet CA certificates and refreshing CRLs, without invalidating certificates signed by the original CA.
20
21 A Puppet deployment's CA certificate is only valid for a limited time (usually five years), after which it expires. When a CA expires, Puppet's services will no longer accept any certificates signed by that CA, and your Puppet infrastructure will immediately stop working.
22
23 If your Puppet infrastructure has been in place for almost five years, you should:
24
25 * Check to see if your CA is expiring soon.
26
27 If your CA is expiring soon (or it's already expired and Puppet has stopped working), you should:
28
29 * Generate a new CA certificate using the existing CA keypair. This will also automatically update the expiration date of the certificate revocation list (CRL).
30 * Distribute the new CA cert and CRL to every node in your Puppet infrastructure.
31
32 The certregen module can help you with all of these tasks.
33
34 > **Note:** This module is NOT currently designed for the following tasks:
35 >
36 > * Re-keying and replacing a CA that has become untrustworthy (by a private key compromise or by a vulnerability like Heartbleed or the old `certdnsnames` bug).
37 > * Refreshing normal node certificates that are nearing expiration.
38 >
39 > For the time being, you're on your own for these.
40
41 ## Installing
42
43 If you manage your code with a Puppetfile, add the following line, replacing `<VERSION>` with the version you want to use:
44
45 ```
46 mod 'puppetlabs-certregen', '<VERSION>'
47 ```
48
49 To install manually, run:
50
51 ```
52 puppet module install puppetlabs-certregen
53 ```
54
55 ## Usage
56
57 The certregen module can help you with four main tasks:
58
59 * Check whether any certificates (including the CA) are expired or nearing their expiration date.
60 * Refresh and redistribute a CA that hasn't yet expired, in a working Puppet deployment.
61 * Refresh and redistribute a CRL that hasn't yet expired, in a working Puppet deployment.
62 * Revive and redistribute an expired CA and CRL, in a Puppet deployment that has stopped working.
63
64 ### Check for nearly-expired (or expired) certificates
65
66 The `healthcheck` action can show you which certificates are expiring soon, as well as any that have already expired. The most important certificate is your CA cert --- if it is almost expired, you must refresh it soon.
67
68 1. **On your Puppet CA server,** run `sudo puppet certregen healthcheck`.
69
70    This finds any certificates with less than 10% of their lifetime remaining (plus any that have already expired), and lists their certname, fingerprint, remaining lifetime, and expiration date. (If no certs are near expiration, the output is blank.)
71
72    ```
73    [root@pe-201621-master vagrant]# puppet certregen healthcheck
74    "foo.com" (SHA256) 07:2F:61:B4:FB:B3:05:75:9D:45:D1:A8:B1:69:0F:D0:EB:C9:27:03:4E:F8:DD:4A:59:AE:DF:EF:8E:11:74:69
75      Status: expiring
76      Expiration date: 2016-11-02 19:52:59 UTC
77      Expires in: 4 minutes, 19 seconds
78    ```
79
80 2. Search the `healthcheck` output for a cert named `"ca"`, which is your CA. **If "ca" is expiring or expired, you must refresh it or revive it as soon as possible.** See the tasks below for more details.
81
82 ### Refresh a CA that's expiring soon
83
84 [ca_ttl]: https://docs.puppet.com/puppet/latest/reference/configuration.html#cattl
85 [main manifest]: https://docs.puppet.com/puppet/latest/reference/dirs_manifest.html
86
87 To refresh an expiring CA, you must:
88
89 * Regenerate the CA certificate with `puppet certregen ca`.
90 * Distribute the new CA with the `certregen::client` class.
91
92 Before you begin, check to make sure your CA really needs to be refreshed (see above).
93
94 1. **On your Puppet CA server,** run `sudo puppet certregen ca`.
95
96    **This will result in an error,** which is normal: since this action replaces your CA cert, it has an extra layer of protection. The error should look something like this:
97
98    ```
99    Error: The serial number of the CA certificate to rotate must be provided. If you are sure that you want to rotate the CA certificate, rerun this command with --ca_serial 03
100    ```
101
102 2. In the error message, find and remember the `--ca_serial <NUMBER>` option.
103 3. Run `sudo puppet certregen ca --ca_serial <NUMBER>`, using the number from the error message.
104
105    By default, this gives the new CA a lifetime of five years. If you wish to set a non-default lifetime, you can add `--ca_ttl <DURATION>`. See [the docs on the ca_ttl setting][ca_ttl] for details.
106
107    When you regenerate the CA certificate, the CRL will be refreshed at the same time with a new expiration of five years as well.
108
109    At this point:
110
111    * The CA certificate _on your CA server_ has been replaced with a new one. The new CA uses the same keypair, subject, issuer, and subject key identifier as the old one; in practical terms, this means it is a seamless replacement for the old CA.
112    * The CRL _on your CA server_ has been updated with a new expiration date, but is otherwise unchanged.
113    * Your Puppet nodes are still using the old CA and CRL.
114 4. **In your [main manifest][] directory,** add a new manifest file called `ca.pp`. In that file, add this line:
115
116    ``` puppet
117    include certregen::client
118    ```
119
120    > **Note:** You must do this in **every** active environment, so that **every** node receives this class in its catalog. You must also ensure that the certregen module is installed in every active environment.
121    >
122    > If you have a prohibitively large number of environments... contact us, because we're still developing this module and soliciting feedback on it.
123 5. If your deployment has multiple compile masters, make sure each one completes a Puppet run against the CA server.
124 6. Ensure that Puppet runs at least once on every other node in your deployment.
125 7. On any Puppet infrastructure nodes (Puppet Server, PuppetDB, PE console), restart all Puppet-related services. The exact services to restart will depend on your version of PE or open source Puppet.
126
127 At this point, the new CA and CRL is fully distributed and you're good for another five years.
128
129 ### Revive a CA that's already expired
130
131 [ssldir]: https://docs.puppet.com/puppet/latest/reference/dirs_ssldir.html
132
133 If your CA has expired, Puppet has already stopped working, and recovering will take some extra work. You must:
134
135 * Regenerate the CA certificate with `puppet certregen ca`.
136 * Distribute the new CA to Linux nodes with `puppet certregen redistribute`.
137 * Manually distribute the new CA to Windows and non-Linux \*nix nodes.
138
139 Before you begin:
140
141 * Check to make sure your CA has really expired (see above).
142 * If you wish to automatically distribute the new CA cert, ensure that:
143     * The Puppet CA server is also a PuppetDB server.
144     * Your Linux nodes have a user account which can log in with an SSH key and run `sudo` commands without a password.
145
146 1. **On your Puppet CA server,** run `sudo puppet certregen ca`.
147
148    **This will result in an error,** which is normal: since this action replaces your CA cert, it has an extra layer of protection. The error should look something like this:
149
150    ```
151    Error: The serial number of the CA certificate to rotate must be provided. If you are sure that you want to rotate the CA certificate, rerun this command with --ca_serial 03
152    ```
153
154 2. In the error message, find and remember the `--ca_serial <NUMBER>` option.
155 3. Run `sudo puppet certregen ca --ca_serial <NUMBER>`, using the number from the error message.
156
157    By default, this gives the new CA a lifetime of five years. If you wish to set a non-default lifetime, you can add `--ca_ttl <DURATION>`. See [the docs on the ca_ttl setting][ca_ttl] for details.
158
159    When you regenerate the CA certificate, the CRL will be refreshed at the same time with a new expiration of five years as well.
160
161    At this point:
162
163    * The CA certificate _on your CA server_ has been replaced with a new one. The new CA uses the same keypair, subject, issuer, and subject key identifier as the old one; in practical terms, this means it is a seamless replacement for the old CA.
164    * The CRL _on your CA server_ has been updated with a new expiration date, but is otherwise unchanged.
165    * Your Puppet nodes still have the old CA, and Puppet is still non-functional.
166
167 4. Use `puppet certregen redistribute` to automatically distribute the CA certificate and CRL to as many Linux nodes as possible. If you don't meet the prerequisites for this (see above), move on to the next step and manually copy the new CA cert to all nodes.
168
169    1. Ensure that the CA server has an SSH private key that can log into your affected nodes. If this key is not usually present on this server, copy it over temporarily.
170    2. Run `/opt/puppetlabs/puppet/bin/gem install chloride`. This SSH helper gem is required by the `certregen redistribute` action.
171    3. Run `puppet certregen redistribute --username <USER> --ssh_key_file <PATH TO KEY>`, replacing the placeholders with your SSH key and username.
172
173    This extracts a list of node hostnames from PuppetDB's database, then copy the new CA cert to each node using SSH. When finished, it lists which nodes were successful and which ones failed.
174
175    ```
176    [root@pe-201640-master vagrant]# puppet certregen redistribute --username vagrant --ssh_key_file /vagrant/id_rsa
177
178    {
179      "succeeded": [
180        "pe-201640-agent0.puppetdebug.vlan",
181        "pe-201640-agent1.puppetdebug.vlan",
182        "pe-201640-agent3.puppetdebug.vlan"
183        "pe-201640-agent2.puppetdebug.vlan",
184        "pe-201640-agent4.puppetdebug.vlan",
185        "pe-201640-master.puppetdebug.vlan"
186      ],
187      "failed": [
188      ]
189    }
190    ```
191 5. Manually copy the new CA certificate to any non-Linux nodes and any nodes where automatic distribution failed.
192
193    * The source file is on your CA server, at `/etc/puppetlabs/puppet/ssl/ca/ca_crt.pem`.
194    * Copy it to `<SSLDIR>/certs/ca.pem` on every node. The default ssldir is `/etc/puppetlabs/puppet/ssl` on \*nix, and `%PROGRAMDATA%\PuppetLabs\puppet\etc` on Windows. See [the ssldir docs][ssldir] for the full details.
195    * If you have any other services that use Puppet's PKI (like MCollective, for example), you must update the CA cert for them as well.
196
197 6. Manually copy the updated CRL to any non-Linux nodes and any nodes where automatic distribution failed.
198
199    * The source file is on your CA server, at `/etc/puppetlabs/puppet/ssl/ca/ca_crl.pem`.
200    * Copy it to `<SSLDIR>/crl.pem` on every node. The default ssldir is `/etc/puppetlabs/puppet/ssl` on \*nix, and `%PROGRAMDATA%\PuppetLabs\puppet\etc` on Windows. See [the ssldir docs][ssldir] for the full details.
201    * If you have any other services that use Puppet's PKI (like MCollective, for example) and uses the Puppet CRL, you must update the CRL for them as well.
202
203 7. On any Puppet infrastructure nodes (Puppet Server, PuppetDB, PE console), restart all Puppet-related services. The exact services to restart will depend on your version of PE or open source Puppet.
204
205 At this point, the new CA and CRL is fully distributed and you're good for another five years.
206
207 ## Reference
208
209 ### Concepts
210
211 Puppet's security is based on a PKI using X.509 certificates. If you're only partially familiar with these concepts, [we've written an introductory primer](https://docs.puppet.com/background/ssl/) about them.
212
213 The certregen module's `puppet certregen ca` action creates a new self-signed CA cert using the same keypair as the prior self-signed CA. The new CA has the same:
214
215 * Keypair.
216 * Subject.
217 * Issuer.
218 * X509v3 Subject Key Identifier (the fingerprint of the public key).
219
220 The new CA has a different:
221
222 * Authority Key Identifier (just the serial number, since it's self-signed).
223 * Validity period (the point of the whole exercise).
224 * Signature (since we changed the serial number and validity period).
225
226 Since Puppet's services (and other services that use Puppet's PKI) validate certs by trusting a self-signed CA and comparing its public key to the signatures and Authority Key Identifiers of the certs it has issued, it's possible to issue a new self-signed CA based on a prior keypair without invalidating any certs issued by the old CA. Once you've done that, it's just a matter of delivering the new CA cert to every participant in the PKI.
227
228 ### Faces
229
230 #### puppet certregen ca
231
232 Regenerate the CA certificate with the subject of the old CA certificate and updated notBefore and notAfter dates, and update the CRL with a nextUpdate field of 5 years from the present date.
233
234 This command combines the behaviors of the `puppet certregean cacert` and `puppet certregen crl` commands and is the preferred method of regenerating your CA.
235
236 #### puppet certregen healthcheck
237
238 Check all signed certificates (including the CA certificate) for certificates that are expired or nearing expiry.
239
240 **Examples**:
241
242 ~~~
243 [root@pe-201621-master vagrant]# puppet certregen healthcheck
244 "foo.com" (SHA256) 07:2F:61:B4:FB:B3:05:75:9D:45:D1:A8:B1:69:0F:D0:EB:C9:27:03:4E:F8:DD:4A:59:AE:DF:EF:8E:11:74:69
245   Status: expiring
246   Expiration date: 2016-11-02 19:52:59 UTC
247   Expires in: 4 minutes, 19 seconds
248 ~~~
249
250 #### puppet certregen cacert
251
252 Regenerate the CA certificate with the subject of the old CA certificate and updated notBefore and notAfter dates.
253
254 #### puppet certregen crl
255
256 Update the CRL with a nextUpdate field of 5 years from the present date.
257
258 CRLs don't usually expire, since their expiration date is normally updated whenever a certificate is revoked. But in rare cases, like when no certificates have been revoked in five years, they can expire and cause problems.
259
260 #### puppet certregen redistribute
261
262 Copy a regenerated Puppet CA certificate to all active nodes in PuppetDB in case the CA cert has already expired. This command must be run on the CA server and requires that the PostgreSQL server that PuppetDB uses is also located on the current node.
263
264 ~~~
265 [root@pe-201640-master vagrant]# puppet certregen redistribute --username vagrant --ssh_key_file /vagrant/id_rsa
266
267 {
268   "succeeded": [
269     "pe-201640-agent0.puppetdebug.vlan",
270     "pe-201640-agent1.puppetdebug.vlan",
271     "pe-201640-agent3.puppetdebug.vlan"
272     "pe-201640-agent2.puppetdebug.vlan",
273     "pe-201640-agent4.puppetdebug.vlan",
274     "pe-201640-master.puppetdebug.vlan"
275   ],
276   "failed": [
277   ]
278 }
279 ~~~
280
281 This subcommand depends on the `chloride` gem, which is not included with this Puppet face. In order to use this subcommand you must manually install it with the `gem` command provided via the Puppet agent installer.
282
283
284 ### Classes
285
286 #### Public Classes
287
288   * `certregen::client`: Rotate the CA certificate and CRL on Puppet agents.
289
290 ## Limitations
291
292 The certregen module is designed to rotate an expiring CA certificate and reuse the CA key pair. Because the keypair is reused this module is unsuitable for rotating a CA certificate when the CA private key has been compromised.
293
294 ## Development
295
296 Puppet Labs modules on the Puppet Forge are open projects, and community contributions are essential for keeping them great. We can’t access the huge number of platforms and myriad of hardware, software, and deployment configurations that Puppet is intended to serve.
297
298 We want to keep it as easy as possible to contribute changes so that our modules work in your environment. There are a few guidelines that we need contributors to follow so that we can have a chance of keeping on top of things.
299
300 For more information, see our [module contribution guide.](https://docs.puppetlabs.com/forge/contributing.html)