Skip to content

Commit 4f7532d

Browse files
Add ability to change validity time of the local CA
A request to be able to more easily change this validity time was raised when it came to deploying this container behind a CDN and making it easy to set up an encrypted connection to the origin server. Through LOCAL_CA_ROOT_CERT_VALIDITY it should now be possible to set it to any number of days desired, but we hide this information inside the advanced documentation so we do falunt with it and force users to read before blindly applying this in production.
1 parent c799f6c commit 4f7532d

File tree

2 files changed

+41
-16
lines changed

2 files changed

+41
-16
lines changed

docs/advanced_usage.md

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ example, are running your own custom ACME server.
134134

135135

136136
## Local CA
137-
During the development phase of a website you might be testing stuff out on a
137+
During the development phase of a website you might be testing stuff on a
138138
computer that either does not have a DNS record pointing to itself or perhaps
139139
it does not have internet access at all. Since certbot has both of these as
140140
requirements to function properly it was previously impossible to use this
@@ -148,13 +148,18 @@ It also enables us to issue certificates that are valid for `localhost` and/or
148148
IP addresses like `::1`, which are otherwise [impossible][7] for certbot to
149149
create.
150150

151+
> You can also use this solution if your intend to deploy behind a CDN and you
152+
> do not need a "real" certificate in order to encrypt the communication
153+
> between the origin server and the CDN's infrastructure. But please read
154+
> this entire section before going forward with that kind of setup.
155+
151156
To enable the usage of this local CA you just set
152157
[`USE_LOCAL_CA=1`](../README.md#advanced), and this will then trigger the
153158
execution of the [`run_local_ca.sh`](../src/scripts/run_local_ca.sh) script
154159
instead of the [`run_certbot.sh`](../src/scripts/run_certbot.sh) one when it is
155160
time to renew the certificates. This script, when run, will always overwrite
156161
any previous keys and certificates, so alternating between the use of a local
157-
CA and certbot without first emptying the `/etc/letsencrypt` folder is not
162+
CA and certbot without first emptying the `/etc/letsencrypt` folder is **not**
158163
supported.
159164

160165
The script is designed to mimic certbot as closely as reasonable, so the
@@ -198,25 +203,25 @@ these files. By then taking the `caCert.pem` and [importing][9] it in your
198203
browser you will be able to visit these sites without the error stating that
199204
the certificate is signed by an unknown authority.
200205

201-
> The validity period for the automatically created CA is only 30 days, and the
202-
reason for this is to deter people from using this solution in production.
203-
204206
An important thing to know is that these files are only created if they do
205207
not exist. What this enables is an even more advanced usecase where you might
206-
already have a private key and certificate that you trust on your devices, so
208+
already have a private key and certificate that you trust on your devices, and
207209
you would like to continue using it for the websites you host as well. Read
208210
more about this in the [next section](#creating-a-custom-ca).
209211

210212

211213
### Creating a Custom CA
212-
As was mentioned in the previous section it is possible to supply the
213-
[`run_local_ca.sh`](../src/scripts/run_local_ca.sh) script with a local
214-
certificate authority that has been created manually by you, perhaps with a
215-
validity period longer than 30 days and that you want to trust on multiple
216-
other devices. This is a solution that can be used in case you want to set up
217-
HTTPS on services that will never be reachable over the open internet, but you
218-
still want their communication to be secure.
219-
214+
The validity period for the automatically created CA is only 30 days by
215+
default, and the reason for this is to deter people from using this solution
216+
blindly in production. While it is possible to quickly change this through the
217+
`LOCAL_CA_ROOT_CERT_VALIDITY` variable, there are security concerns regarding
218+
managing your own certificate authority which should be thought through before
219+
doing this in a production environment.
220+
221+
Nevertheless, as was mentioned in the previous section it is possible to supply
222+
the [`run_local_ca.sh`](../src/scripts/run_local_ca.sh) script with a local
223+
certificate authority that has been created manually by you that you want to
224+
trust on multiple other devices.
220225
Basically all you need to do is to host mount your custom private key and
221226
certificate to the `LOCAL_CA_DIR`, and the script will use these instead of
222227
the short lived automatically created ones. Just make sure the files are named
@@ -248,6 +253,14 @@ and you may of course take a look at the commands used in the
248253
[`generate_ca()`](../src/scripts/run_local_ca.sh) function to help you on your
249254
way of creating your own files.
250255

256+
When the long term CA is in place you can probably tune the `RENEWAL_INTERVAL`
257+
variable to equal something slightly less than the hard-coded `90d` expiry
258+
time of the leaf certificates created. This is the expiry time recommended by
259+
Let's Encrypt (and perhaps [soon mandated][19]), and since the renewal script
260+
_should_ always be successful you only need to run it just before the expiry
261+
time. You can, of course, run it more often than that, but it will not yield
262+
and special benefits.
263+
251264

252265

253266

@@ -271,3 +284,4 @@ way of creating your own files.
271284
[16]: https://superuser.com/questions/738612/openssl-ca-keyusage-extension/1248085#1248085
272285
[17]: https://www.feistyduck.com/library/openssl-cookbook/online/ch-openssl.html
273286
[18]: https://nginx.org/en/docs/http/server_names.html
287+
[19]: https://www.globalsign.com/en-ae/blog/google-90-day-certificate-validity-requires-automation

src/scripts/run_local_ca.sh

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ certificate = ${LOCAL_CA_CRT}
4444
database = ${LOCAL_CA_DB}
4545
serial = ${LOCAL_CA_SRL}
4646
new_certs_dir = ${LOCAL_CA_CRT_DIR}
47-
default_days = 30
47+
default_days = 90
4848
default_md = sha256
4949
email_in_dn = yes
5050
unique_subject = no
@@ -117,6 +117,17 @@ generate_ca() {
117117
# Make sure there exists a self-signed certificate for the CA.
118118
if [ ! -f "${LOCAL_CA_CRT}" ]; then
119119
info "Creating new self-signed certificate for local CA"
120+
121+
# We do allow changing the validity time of the locally generated CA,
122+
# but please read the advanced_usage.md#local-ca documentation before
123+
# doing this.
124+
if [ -z "${LOCAL_CA_ROOT_CERT_VALIDITY}" ]; then
125+
debug "LOCAL_CA_ROOT_CERT_VALIDITY unset, defaulting to 30 days"
126+
LOCAL_CA_ROOT_CERT_VALIDITY=30
127+
else
128+
info "LOCAL_CA_ROOT_CERT_VALIDITY set to custom value '${LOCAL_CA_ROOT_CERT_VALIDITY}'"
129+
fi
130+
120131
openssl req -x509 -new -nodes \
121132
-config <(printf "%s\n" \
122133
"${openssl_cnf}" \
@@ -128,7 +139,7 @@ generate_ca() {
128139
"emailAddress = ${CERTBOT_EMAIL}" \
129140
) \
130141
-extensions ca_cert \
131-
-days 30 \
142+
-days "${LOCAL_CA_ROOT_CERT_VALIDITY}" \
132143
-key "${LOCAL_CA_KEY}" \
133144
-out "${LOCAL_CA_CRT}"
134145
fi

0 commit comments

Comments
 (0)