Skip to content

Commit 00405d8

Browse files
committed
Enable YAML configuration
This patch allows configuring nginx-certbot with a config.yml file. In particular this allows to directly declare the certificates that should be requested by certbot with finer granularity compared to the automatic discovery based on the nginx config files. Main motivations: - Currently, since automatic discovery is implemented on a per file basis, all domain names in a file are attached ot all certificates in that file. This means that for e.g. ```nginx server { server_name example.com *.example.com; ssl_certificate /etc/letsencrypt/live/example-com/fullchain.pem; # [...] } server { server_name a.example.com; ssl_certificate /etc/letsencrypt/live/a-example-com/fullchain.pem; # [...] } ``` both `example-com` and `a-example-com` will contain the domain names `example.com`, `*.example.com`, and `a.example.com`. With this patch it is possible to instead do ```yaml certificates: - name: example-com domains: [example.com, *.example.com] - name: a-example-com domains: [a.example.com] ``` - Currently the authenticator credentials can't be specified on a per certificate basis (see e.g. #315). With this patch that is possible: ```yaml certbot: authenticator: dns-cloudflare certificates: - name: example-com domains: [example.com] credentials: /etc/letsencrypt/example-com-cloudflare.ini - name: example-se domains: [example.se] credentials: /etc/letsencrypt/example-se-cloudflare.ini ``` - Authenticator and key type can currently be specified on a per-certificate basis by naming them appropriately. This works okay, but it becomes a bit clunky to support more such per-certificate configurations (such as e.g. the elliptic curve or the authenticator credentials). This patch allows to directly specify everything for each certificate: ```yaml certbot: authenticator: dns-cloudflare key-type: ecdsa certificates: - name: example-com-rsa domains: [example.com] key-type: rsa - name: example-com domains: [example.com] ``` The file examples/config.yml is documented with all the various options that are enabled.
1 parent 763d267 commit 00405d8

File tree

9 files changed

+429
-154
lines changed

9 files changed

+429
-154
lines changed

README.md

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,25 +62,41 @@ instructions, from `@staticfloat`'s image, can be found
6262
function.
6363

6464

65-
## Available Environment Variables
65+
## Configuration
66+
67+
The container can be configured using a YAML config file or with environment
68+
variables. The config file takes precendence whenever a setting is specified
69+
twice. The default location of the config file is
70+
`/etc/nginx-certbot/config.yml` but can be customized with the environment
71+
variable `NGINX_CERTBOT_CONFIG_FILE`. A documented example config file can be
72+
found in the [`examples/`](./examples) folder.
6673

6774
### Required
68-
- `CERTBOT_EMAIL`: Your e-mail address. Used by Let's Encrypt to contact you in case of security issues.
75+
76+
| YAML key | Environment variable | Description |
77+
| --------------- | -------------------- | ----------- |
78+
| `certbot.email` | `CERTBOT_EMAIL` | Your e-mail address. Used by Let's Encrypt to contact you in case of security issues. |
6979

7080
### Optional
71-
- `DHPARAM_SIZE`: The size of the [Diffie-Hellman parameters](./docs/good_to_know.md#diffie-hellman-parameters) (default: `2048`)
72-
- `ELLIPTIC_CURVE`: The size/[curve][15] of the ECDSA keys (default: `secp256r1`)
73-
- `RENEWAL_INTERVAL`: Time interval between certbot's [renewal checks](./docs/good_to_know.md#renewal-check-interval) (default: `8d`)
74-
- `RSA_KEY_SIZE`: The size of the RSA encryption keys (default: `2048`)
75-
- `STAGING`: Set to `1` to use Let's Encrypt's [staging servers](./docs/good_to_know.md#initial-testing) (default: `0`)
76-
- `USE_ECDSA`: Set to `0` to have certbot use [RSA instead of ECDSA](./docs/good_to_know.md#ecdsa-and-rsa-certificates) (default: `1`)
81+
82+
| YAML key | Environment variable | Description |
83+
| -------------------------------- | -------------------- | ----------- |
84+
| `nginx-certbot.renewal-interval` | `RENEWAL_INTERVAL` | Time interval between certbot's [renewal checks](./docs/good_to_know.md#renewal-check-interval) (default: `8d`) |
85+
| `nginx-certbot.dhparam-size` | `DHPARAM_SIZE` | The size of the [Diffie-Hellman parameters](./docs/good_to_know.md#diffie-hellman-parameters) (default: `2048`) |
86+
| `certbot.elliptic-curve` | `ELLIPTIC_CURVE` | The size/[curve][15] of the ECDSA keys (default: `secp256r1`) |
87+
| `certbot.rsa-key-size` | `RSA_KEY_SIZE` | The size of the RSA encryption keys (default: `2048`) |
88+
| `certbot.staging` | `STAGING` | Set to `1` to use Let's Encrypt's [staging servers](./docs/good_to_know.md#initial-testing) (default: `0`) |
89+
| - | `USE_ECDSA` | Set to `0` to have certbot use [RSA instead of ECDSA](./docs/good_to_know.md#ecdsa-and-rsa-certificates) (default: `1`) |
90+
| `certbot.key-type` | - | Certificate key type (default: `ecdsa` (or, if `USE_ECDSA=0`, `rsa`) |
7791

7892
### Advanced
79-
- `CERTBOT_AUTHENTICATOR`: The [authenticator plugin](./docs/certbot_authenticators.md) to use when responding to challenges (default: `webroot`)
80-
- `CERTBOT_DNS_PROPAGATION_SECONDS`: The number of seconds to wait for the DNS challenge to [propagate](.docs/certbot_authenticators.md#troubleshooting-tips) (default: certbot's default)
81-
- `DEBUG`: Set to `1` to enable debug messages and use the [`nginx-debug`][10] binary (default: `0`)
82-
- `USE_LOCAL_CA`: Set to `1` to enable the use of a [local certificate authority](./docs/advanced_usage.md#local-ca) (default: `0`)
8393

94+
| YAML key | Environment variable | Description |
95+
| --------------------------------- | --------------------------------- | ----------- |
96+
| `certbot.authenticator` | `CERTBOT_AUTHENTICATOR` | The [authenticator plugin](./docs/certbot_authenticators.md) to use when responding to challenges (default: `webroot`) |
97+
| `certbot.dns-propagation-seconds` | `CERTBOT_DNS_PROPAGATION_SECONDS` | The number of seconds to wait for the DNS challenge to [propagate](.docs/certbot_authenticators.md#troubleshooting-tips) (default: certbot's default) |
98+
| `nginx-certbot.debug` | `DEBUG` | Set to `1` to enable debug messages and use the [`nginx-debug`][10] binary (default: `0`) |
99+
| - | `USE_LOCAL_CA` | Set to `1` to enable the use of a [local certificate authority](./docs/advanced_usage.md#local-ca) (default: `0`) |
84100

85101
## Volumes
86102
- `/etc/letsencrypt`: Stores the obtained certificates and the Diffie-Hellman parameters

docs/good_to_know.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ in the old way like how [`@staticfloat`'s image][5] worked.
6565

6666

6767
## How the Script add Domain Names to Certificate Requests
68+
There are two ways to configure the certificates the container should request
69+
and maintain:
70+
- [Automatic discovery](#automatic-certificate-discovery) based on the mounted
71+
Nginx config files
72+
- Explicit specification using the [YAML config file](#yaml-certificate-specification)
73+
74+
### Automatic certificate discovery
6875
The included script will go through all configuration files (`*.conf*`) it
6976
finds inside Nginx's `/etc/nginx/conf.d/` folder, and create requests from the
7077
file's content. In every unique file it will find any line that says:
@@ -119,6 +126,28 @@ Furthermore, we support wildcard domain names, but that requires you to use an
119126
authenticator capable of DNS-01 challenges, and more info about that may be
120127
found in the [certbot_authenticators.md](./certbot_authenticators.md) document.
121128

129+
### YAML certificate specification
130+
To explicitly define certificate requests you can define a list `certificates:`
131+
list in a YAML config file (`/etc/nginx-certbot/config.yml` by default). Note
132+
that when the `certificates` key exist the automatic discovery from nginx
133+
config files is disabled and *only* certificates from the config file are
134+
requested.
135+
136+
The example from the previous section would correspond to the following
137+
specification:
138+
```yaml
139+
certificates:
140+
- name: test-name
141+
domains:
142+
- yourdomain.com
143+
- www.yourdomain.com
144+
- sub.yourdomain.com
145+
```
146+
147+
Refer to the commented example [`config.yml`](../examples/config.yml) file for
148+
more details. It is, for example, possible to specify the
149+
[certbot authenticator](./certbot_authenticators.md), and the certificate
150+
[key type](#ecdsa-and-rsa-certificates).
122151

123152
## ECDSA and RSA Certificates
124153
[ECDSA (or ECC)][16] certificates use a newer encryption algorithm than the well

examples/config.yml

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Configuration for this docker image
2+
nginx-certbot:
3+
# Diffie-Hellman parameter size. Falls back to the DHPARAM_SIZE environment variable or,
4+
# if that is unset, to '2048'.
5+
dhparam-size: 2048
6+
# Certificate renewal interval. Falls back to the RENEWAL_INTERVAL environment variable
7+
# or, if that is unset, to '8d'.
8+
renewal-interval: 8d
9+
# Boolean to enable verbose debug messages and the nginx-debug binary. Falls back to the
10+
# DEBUG environment variable, or, if that is unset, to 'false'.
11+
debug: false
12+
13+
# Configuration for certbot.
14+
# Note that some of these can be overriden on the certificate level.
15+
certbot:
16+
# Default certbot authenticator (see certbots --authenticator flag). Falls back to the
17+
# CERTBOT_AUTHENTICATOR environment variable or, if that is unset, to 'webroot'. The
18+
# authenticator can be overriden on the certificate level.
19+
authenticator: webroot
20+
# Default certbot authenticator credentials (see certbots --<authenticator>-credentials
21+
# flag). This is required for the various DNS authenticators. Falls back to
22+
# '/etc/letsencrypt/<authenticator>.ini'.
23+
credentials: ''
24+
# Number of seconds to wait for the DNS challenge (when using dns authenticators). Falls
25+
# back to the CERTBOT_DNS_PROPAGATION_SECONDS environment variable and if that is unset to
26+
# certbots default.
27+
dns-propagation-seconds: ''
28+
# Default elliptic curve (see certbots --elliptic-curve flag). Falls back to the
29+
# ELLIPTIC_CURVE environment variable or, if that is unset, to 'secp256r1'.
30+
elliptic-curve: secp256r1
31+
# Default key type (see certbots --key-type flag). Falls back to 'ecdsa' (or if
32+
# USE_ECDSA=0 to 'rsa'). The key type can be overriden on the certificate level.
33+
key-type: ecdsa
34+
# Default RSA key size (see certbots --rsa-key-size flag). Falls back to the RSA_KEY_SIZE
35+
# environment variable or, if that is unset, to 2048. The key size can be overriden on the
36+
# certificate level.
37+
rsa-key-size: 2048
38+
# Boolean to enable the Let's Encrypt staging servers. Falls back to the STAGING
39+
# environment variable or, if that is unset, to 'false'.
40+
staging: false
41+
42+
# Array of certificate specifications.
43+
# If the 'certificates' key exist (even if the array is empty) the automatic discovery of
44+
# certificate names and domains is disabled and instead nginx-certbot will request
45+
# certificates based on the specifications in the array.
46+
# A minimum requirement for each certificate is to specifiy 'name' and 'domains'.
47+
certificates:
48+
# Certificate name (see certbots --cert-name flag). Generated certificates will be
49+
# placed in the /etc/letsencrypt/live/<name>/ folder. This is a required parameter.
50+
- name: example-com
51+
# Required list of domains for which the certificate should be valid for (see certbots
52+
# --domain flag). This is a required parameter.
53+
domains: ["a.example.com", "b.example.com", "*.c.example.com"]
54+
# Authenticator to use for this certificate. Falls back to certbot.authenticator.
55+
authenticator: ''
56+
# Credential file for this certificates authenticator. Falls back to
57+
# certbot.credentials.
58+
credentials: ''
59+
# Key type for the certificate. Falls back to certbot.key-type.
60+
key-type: ''
61+
# RSA key size for the certificate. Falls back to certbot.rsa-key-size.
62+
rsa-key-size: ''

src/Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ RUN set -ex && \
7171
pip3 install -r /requirements.txt && \
7272
# And the supported extra authenticators.
7373
pip3 install $(echo $CERTBOT_DNS_AUTHENTICATORS | sed 's/\(^\| \)/\1certbot-dns-/g') && \
74+
# Install shyaml
75+
pip3 install shyaml && \
7476
# Remove everything that is no longer necessary.
7577
apt-get remove --purge -y \
7678
build-essential \

src/scripts/create_dhparams.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ set -e
1111
# The created file should be stored somewhere under /etc/letsencrypt/dhparams/
1212
# to ensure persistence between restarts.
1313
create_dhparam() {
14+
# Read dhparam-size from config file, falling back to DHPARAM_SIZE environment variable.
15+
local CONFIG_FILE="${NGINX_CERTBOT_CONFIG_FILE:-/etc/nginx-certbot/config.yml}"
16+
if [ -f "${CONFIG_FILE}" ]; then
17+
local YAML_DHPARAM_SIZE
18+
YAML_DHPARAM_SIZE=$(shyaml get-value nginx-certbot.dhparam-size '' < "${CONFIG_FILE}")
19+
if [ -n "${YAML_DHPARAM_SIZE}" ]; then
20+
DHPARAM_SIZE=${YAML_DHPARAM_SIZE}
21+
debug "Using nginx-certbot.dhparam-size=${DHPARAM_SIZE} from config file."
22+
fi
23+
fi
1424
if [ -z "${DHPARAM_SIZE}" ]; then
1525
debug "DHPARAM_SIZE unset, using default of 2048 bits"
1626
DHPARAM_SIZE=2048

0 commit comments

Comments
 (0)