git.iko.soy/typeable/nixpkgs
on master help sign in
Merge pull request #77578 from m1cr0man/master

Replace simp-le with lego and support DNS-01 challenge

commit4e0fea3fe294d112ac6eef5adbed2357b295f11d authorFlorian Klink <flokli@flokli.de> dateMon 10 Feb 2020, 11:47 (7 years ago) parentb2abf3675fa802
6 files changed+256−61
maintainers/maintainer-list.nix+6 nixos/doc/manual/release-notes/rl-2003.xml+15 nixos/modules/security/acme.nix+130−43 nixos/modules/security/acme.xml+1−1 nixos/tests/acme.nix+101−17 nixos/tests/common/letsencrypt/common.nix+3
maintainers/maintainer-list.nix+6
@@ -4304,6 +4304,12 @@
43044304 email = "wheatdoge@gmail.com";
43054305 name = "Tim Liou";
43064306 };
4307+ m1cr0man = {
4308+ email = "lucas+nix@m1cr0man.com";
4309+ github = "m1cr0man";
4310+ githubId = 3044438;
4311+ name = "Lucas Savva";
4312+ };
43074313 m3tti = {
43084314 email = "mathaeus.peter.sander@gmail.com";
43094315 name = "Mathaeus Sander";
nixos/doc/manual/release-notes/rl-2003.xml+15
@@ -660,6 +660,21 @@ auth required pam_succeed_if.so uid >= 1000 quiet
660660 <literal>PRETTY_NAME</literal> in <literal>/etc/os-release</literal>
661661 now uses the short rather than full version string.
662662 </para>
663+ </listitem>
664+ <listitem>
665+ <para>
666+ The ACME module has switched from simp-le to <link xlink:href="https://github.com/go-acme/lego">lego</link>
667+ which allows us to support DNS-01 challenges and wildcard certificates. The following options have been added:
668+ <link linkend="opt-security.acme.acceptTerms">security.acme.acceptTerms</link>,
669+ <link linkend="opt-security.acme.certs">security.acme.certs.&lt;name&gt;.dnsProvider</link>,
670+ <link linkend="opt-security.acme.certs">security.acme.certs.&lt;name&gt;.credentialsFile</link>,
671+ <link linkend="opt-security.acme.certs">security.acme.certs.&lt;name&gt;.dnsPropagationCheck</link>.
672+ As well as this, the options <literal>security.acme.acceptTerms</literal> and either
673+ <literal>security.acme.email</literal> or <literal>security.acme.certs.&lt;name&gt;.email</literal>
674+ must be set in order to use the ACME module.
675+ Certificates will be regenerated anew on the next renewal date. The credentials for simp-le are
676+ preserved and thus it is possible to roll back to previous versions without breaking certificate
677+ generation.
663678 </listitem>
664679 <listitem>
665680 <para>
nixos/modules/security/acme.nix+130−43
@@ -1,7 +1,5 @@
11{ config, lib, pkgs, ... }:
2−
32with lib;
4−
53let
64
75 cfg = config.security.acme;
@@ -9,7 +7,8 @@ let
97 certOpts = { name, ... }: {
108 options = {
119 webroot = mkOption {
12− type = types.str;
10+ type = types.nullOr types.str;
11+ default = null;
1312 example = "/var/lib/acme/acme-challenges";
1413 description = ''
1514 Where the webroot of the HTTP vhost is located.
@@ -38,7 +37,7 @@ let
3837
3938 email = mkOption {
4039 type = types.nullOr types.str;
41− default = null;
40+ default = cfg.email;
4241 description = "Contact email address for the CA to be able to reach you.";
4342 };
4443
@@ -76,20 +75,6 @@ let
7675 '';
7776 };
7877
79− plugins = mkOption {
80− type = types.listOf (types.enum [
81− "cert.der" "cert.pem" "chain.pem" "external.sh"
82− "fullchain.pem" "full.pem" "key.der" "key.pem" "account_key.json" "account_reg.json"
83− ]);
84− default = [ "fullchain.pem" "full.pem" "key.pem" "account_key.json" "account_reg.json" ];
85− description = ''
86− Plugins to enable. With default settings simp_le will
87− store public certificate bundle in <filename>fullchain.pem</filename>,
88− private key in <filename>key.pem</filename> and those two previous
89− files combined in <filename>full.pem</filename> in its state directory.
90− '';
91− };
92−
9378 directory = mkOption {
9479 type = types.str;
9580 readOnly = true;
@@ -111,6 +96,46 @@ let
11196 own server roots if needed.
11297 '';
11398 };
99+
100+ keyType = mkOption {
101+ type = types.str;
102+ default = "ec384";
103+ description = ''
104+ Key type to use for private keys.
105+ For an up to date list of supported values check the --key-type option
106+ at https://go-acme.github.io/lego/usage/cli/#usage.
107+ '';
108+ };
109+
110+ dnsProvider = mkOption {
111+ type = types.nullOr types.str;
112+ default = null;
113+ example = "route53";
114+ description = ''
115+ DNS Challenge provider. For a list of supported providers, see the "code"
116+ field of the DNS providers listed at https://go-acme.github.io/lego/dns/.
117+ '';
118+ };
119+
120+ credentialsFile = mkOption {
121+ type = types.path;
122+ description = ''
123+ Path to an EnvironmentFile for the cert's service containing any required and
124+ optional environment variables for your selected dnsProvider.
125+ To find out what values you need to set, consult the documentation at
126+ https://go-acme.github.io/lego/dns/ for the corresponding dnsProvider.
127+ '';
128+ example = "/var/src/secrets/example.org-route53-api-token";
129+ };
130+
131+ dnsPropagationCheck = mkOption {
132+ type = types.bool;
133+ default = true;
134+ description = ''
135+ Toggles lego DNS propagation check, which is used alongside DNS-01
136+ challenge to ensure the DNS entries required are available.
137+ '';
138+ };
114139 };
115140 };
116141
@@ -130,14 +155,21 @@ in
130155 (mkRemovedOptionModule [ "security" "acme" "directory"] "ACME Directory is now hardcoded to /var/lib/acme and its permisisons are managed by systemd. See https://github.com/NixOS/nixpkgs/issues/53852 for more info.")
131156 (mkRemovedOptionModule [ "security" "acme" "preDelay"] "This option has been removed. If you want to make sure that something executes before certificates are provisioned, add a RequiredBy=acme-\${cert}.service to the service you want to execute before the cert renewal")
132157 (mkRemovedOptionModule [ "security" "acme" "activationDelay"] "This option has been removed. If you want to make sure that something executes before certificates are provisioned, add a RequiredBy=acme-\${cert}.service to the service you want to execute before the cert renewal")
158+ (mkChangedOptionModule [ "security" "acme" "validMin"] [ "security" "acme" "validMinDays"] (config: config.security.acme.validMin / (24 * 3600)))
133159 ];
134160 options = {
135161 security.acme = {
136162
137− validMin = mkOption {
163+ validMinDays = mkOption {
138164 type = types.int;
139− default = 30 * 24 * 3600;
140− description = "Minimum remaining validity before renewal in seconds.";
165+ default = 30;
166+ description = "Minimum remaining validity before renewal in days.";
167+ };
168+
169+ email = mkOption {
170+ type = types.nullOr types.str;
171+ default = null;
172+ description = "Contact email address for the CA to be able to reach you.";
141173 };
142174
143175 renewInterval = mkOption {
@@ -173,6 +205,15 @@ in
173205 '';
174206 };
175207
208+ acceptTerms = mkOption {
209+ type = types.bool;
210+ default = false;
211+ description = ''
212+ Accept the CA's terms of service. The default provier is Let's Encrypt,
213+ you can find their ToS at https://letsencrypt.org/repository/
214+ '';
215+ };
216+
176217 certs = mkOption {
177218 default = { };
178219 type = with types; attrsOf (submodule certOpts);
@@ -204,27 +245,55 @@ in
204245 config = mkMerge [
205246 (mkIf (cfg.certs != { }) {
206247
248+ assertions = let
249+ certs = (mapAttrsToList (k: v: v) cfg.certs);
250+ in [
251+ {
252+ assertion = all (certOpts: certOpts.dnsProvider == null || certOpts.webroot == null) certs;
253+ message = ''
254+ Options `security.acme.certs.<name>.dnsProvider` and
255+ `security.acme.certs.<name>.webroot` are mutually exclusive.
256+ '';
257+ }
258+ {
259+ assertion = cfg.email != null || all (certOpts: certOpts.email != null) certs;
260+ message = ''
261+ You must define `security.acme.certs.<name>.email` or
262+ `security.acme.email` to register with the CA.
263+ '';
264+ }
265+ {
266+ assertion = cfg.acceptTerms;
267+ message = ''
268+ You must accept the CA's terms of service before using
269+ the ACME module by setting `security.acme.acceptTerms`
270+ to `true`. For Let's Encrypt's ToS see https://letsencrypt.org/repository/
271+ '';
272+ }
273+ ];
274+
207275 systemd.services = let
208276 services = concatLists servicesLists;
209277 servicesLists = mapAttrsToList certToServices cfg.certs;
210278 certToServices = cert: data:
211279 let
280+ # StateDirectory must be relative, and will be created under /var/lib by systemd
212281 lpath = "acme/${cert}";
282+ apath = "/var/lib/${lpath}";
283+ spath = "/var/lib/acme/.lego";
213284 rights = if data.allowKeysForGroup then "750" else "700";
214− cmdline = [ "-v" "-d" data.domain "--default_root" data.webroot "--valid_min" cfg.validMin ]
215− ++ optionals (data.email != null) [ "--email" data.email ]
216− ++ concatMap (p: [ "-f" p ]) data.plugins
217− ++ concatLists (mapAttrsToList (name: root: [ "-d" (if root == null then name else "${name}:${root}")]) data.extraDomains)
285+ globalOpts = [ "-d" data.domain "--email" data.email "--path" "." "--key-type" data.keyType ]
286+ ++ optionals (cfg.acceptTerms) [ "--accept-tos" ]
287+ ++ optionals (data.dnsProvider != null && !data.dnsPropagationCheck) [ "--dns.disable-cp" ]
288+ ++ concatLists (mapAttrsToList (name: root: [ "-d" name ]) data.extraDomains)
289+ ++ (if data.dnsProvider != null then [ "--dns" data.dnsProvider ] else [ "--http" "--http.webroot" data.webroot ])
218290 ++ optionals (cfg.server != null || data.server != null) ["--server" (if data.server == null then cfg.server else data.server)];
291+ runOpts = escapeShellArgs (globalOpts ++ [ "run" ]);
292+ renewOpts = escapeShellArgs (globalOpts ++ [ "renew" "--days" (toString cfg.validMinDays) ]);
219293 acmeService = {
220294 description = "Renew ACME Certificate for ${cert}";
221295 after = [ "network.target" "network-online.target" ];
222296 wants = [ "network-online.target" ];
223− # simp_le uses requests, which uses certifi under the hood,
224− # which doesn't respect the system trust store.
225− # At least in the acme test, we provision a fake CA, impersonating the LE endpoint.
226− # REQUESTS_CA_BUNDLE is a way to teach python requests to use something else
227− environment.REQUESTS_CA_BUNDLE = "/etc/ssl/certs/ca-certificates.crt";
228297 serviceConfig = {
229298 Type = "oneshot";
230299 # With RemainAfterExit the service is considered active even
@@ -233,18 +302,37 @@ in
233302 # the permissions of the StateDirectory get adjusted
234303 # according to the specified group
235304 RemainAfterExit = true;
236− SuccessExitStatus = [ "0" "1" ];
237305 User = data.user;
238306 Group = data.group;
239307 PrivateTmp = true;
240− StateDirectory = lpath;
308+ StateDirectory = "acme/.lego ${lpath}";
241309 StateDirectoryMode = rights;
242− WorkingDirectory = "/var/lib/${lpath}";
243− ExecStart = "${pkgs.simp_le}/bin/simp_le ${escapeShellArgs cmdline}";
310+ WorkingDirectory = spath;
311+ # Only try loading the credentialsFile if the dns challenge is enabled
312+ EnvironmentFile = if data.dnsProvider != null then data.credentialsFile else null;
313+ ExecStart = pkgs.writeScript "acme-start" ''
314+ #!${pkgs.runtimeShell} -e
315+ ${pkgs.lego}/bin/lego ${renewOpts} || ${pkgs.lego}/bin/lego ${runOpts}
316+ '';
244317 ExecStartPost =
245318 let
319+ keyName = builtins.replaceStrings ["*"] ["_"] data.domain;
246320 script = pkgs.writeScript "acme-post-start" ''
247321 #!${pkgs.runtimeShell} -e
322+ cd ${apath}
323+
324+ # Test that existing cert is older than new cert
325+ KEY=${spath}/certificates/${keyName}.key
326+ if [ -e $KEY -a $KEY -nt key.pem ]; then
327+ cp -p ${spath}/certificates/${keyName}.key key.pem
328+ cp -p ${spath}/certificates/${keyName}.crt cert.pem
329+ cp -p ${spath}/certificates/${keyName}.issuer.crt chain.pem
330+ cat cert.pem chain.pem > fullchain.pem
331+ cat key.pem cert.pem chain.pem > full.pem
332+ chmod ${rights} *.pem
333+ chown '${data.user}:${data.group}' *.pem
334+ fi
335+
248336 ${data.postRun}
249337 '';
250338 in
@@ -276,17 +364,17 @@ in
276364 -out $workdir/server.crt
277365
278366 # Copy key to destination
279− cp $workdir/server.key /var/lib/${lpath}/key.pem
367+ cp $workdir/server.key ${apath}/key.pem
280368
281369 # Create fullchain.pem (same format as "simp_le ... -f fullchain.pem" creates)
282− cat $workdir/{server.crt,ca.crt} > "/var/lib/${lpath}/fullchain.pem"
370+ cat $workdir/{server.crt,ca.crt} > "${apath}/fullchain.pem"
283371
284372 # Create full.pem for e.g. lighttpd
285− cat $workdir/{server.key,server.crt,ca.crt} > "/var/lib/${lpath}/full.pem"
373+ cat $workdir/{server.key,server.crt,ca.crt} > "${apath}/full.pem"
286374
287375 # Give key acme permissions
288− chown '${data.user}:${data.group}' "/var/lib/${lpath}/"{key,fullchain,full}.pem
289− chmod ${rights} "/var/lib/${lpath}/"{key,fullchain,full}.pem
376+ chown '${data.user}:${data.group}' "${apath}/"{key,fullchain,full}.pem
377+ chmod ${rights} "${apath}/"{key,fullchain,full}.pem
290378 '';
291379 serviceConfig = {
292380 Type = "oneshot";
@@ -297,7 +385,7 @@ in
297385 };
298386 unitConfig = {
299387 # Do not create self-signed key when key already exists
300− ConditionPathExists = "!/var/lib/${lpath}/key.pem";
388+ ConditionPathExists = "!${apath}/key.pem";
301389 };
302390 };
303391 in (
@@ -309,8 +397,7 @@ in
309397 servicesAttr;
310398
311399 systemd.tmpfiles.rules =
312− flip mapAttrsToList cfg.certs
313− (cert: data: "d ${data.webroot}/.well-known/acme-challenge - ${data.user} ${data.group}");
400+ map (data: "d ${data.webroot}/.well-known/acme-challenge - ${data.user} ${data.group}") (filter (data: data.webroot != null) (attrValues cfg.certs));
314401
315402 systemd.timers = flip mapAttrs' cfg.certs (cert: data: nameValuePair
316403 ("acme-${cert}")
@@ -334,7 +421,7 @@ in
334421 ];
335422
336423 meta = {
337− maintainers = with lib.maintainers; [ abbradar fpletz globin ];
424+ maintainers = with lib.maintainers; [ abbradar fpletz globin m1cr0man ];
338425 doc = ./acme.xml;
339426 };
340427}
nixos/modules/security/acme.xml+1−1
@@ -7,7 +7,7 @@
77 <para>
88 NixOS supports automatic domain validation &amp; certificate retrieval and
99 renewal using the ACME protocol. This is currently only implemented by and
10− for Let's Encrypt. The alternative ACME client <literal>simp_le</literal> is
10+ for Let's Encrypt. The alternative ACME client <literal>lego</literal> is
1111 used under the hood.
1212 </para>
1313 <section xml:id="module-security-acme-prerequisites">
nixos/tests/acme.nix+101−17
@@ -1,17 +1,50 @@
11let
22 commonConfig = ./common/letsencrypt/common.nix;
3+
4+ dnsScript = {writeScript, dnsAddress, bash, curl}: writeScript "dns-hook.sh" ''
5+ #!${bash}/bin/bash
6+ set -euo pipefail
7+ echo '[INFO]' "[$2]" 'dns-hook.sh' $*
8+ if [ "$1" = "present" ]; then
9+ ${curl}/bin/curl --data '{"host": "'"$2"'", "value": "'"$3"'"}' http://${dnsAddress}:8055/set-txt
10+ else
11+ ${curl}/bin/curl --data '{"host": "'"$2"'"}' http://${dnsAddress}:8055/clear-txt
12+ fi
13+ '';
14+
315in import ./make-test-python.nix {
416 name = "acme";
517
618 nodes = rec {
7− letsencrypt = ./common/letsencrypt;
19+ letsencrypt = { nodes, lib, ... }: {
20+ imports = [ ./common/letsencrypt ];
21+ networking.nameservers = lib.mkForce [
22+ nodes.dnsserver.config.networking.primaryIPAddress
23+ ];
24+ };
25+
26+ dnsserver = { nodes, pkgs, ... }: {
27+ networking.firewall.allowedTCPPorts = [ 8055 53 ];
28+ networking.firewall.allowedUDPPorts = [ 53 ];
29+ systemd.services.pebble-challtestsrv = {
30+ enable = true;
31+ description = "Pebble ACME challenge test server";
32+ wantedBy = [ "network.target" ];
33+ serviceConfig = {
34+ ExecStart = "${pkgs.pebble}/bin/pebble-challtestsrv -dns01 ':53' -defaultIPv6 '' -defaultIPv4 '${nodes.webserver.config.networking.primaryIPAddress}'";
35+ # Required to bind on privileged ports.
36+ User = "root";
37+ Group = "root";
38+ };
39+ };
40+ };
841
9− acmeStandalone = { config, pkgs, ... }: {
42+ acmeStandalone = { nodes, lib, config, pkgs, ... }: {
1043 imports = [ commonConfig ];
44+ networking.nameservers = lib.mkForce [
45+ nodes.dnsserver.config.networking.primaryIPAddress
46+ ];
1147 networking.firewall.allowedTCPPorts = [ 80 ];
12− networking.extraHosts = ''
13− ${config.networking.primaryIPAddress} standalone.com
14− '';
1548 security.acme = {
1649 server = "https://acme-v02.api.letsencrypt.org/dir";
1750 certs."standalone.com" = {
@@ -29,14 +62,12 @@ in import ./make-test-python.nix {
2962 };
3063 };
3164
32− webserver = { config, pkgs, ... }: {
65+ webserver = { nodes, config, pkgs, lib, ... }: {
3366 imports = [ commonConfig ];
3467 networking.firewall.allowedTCPPorts = [ 80 443 ];
35−
36− networking.extraHosts = ''
37− ${config.networking.primaryIPAddress} a.example.com
38− ${config.networking.primaryIPAddress} b.example.com
39− '';
68+ networking.nameservers = lib.mkForce [
69+ nodes.dnsserver.config.networking.primaryIPAddress
70+ ];
4071
4172 # A target remains active. Use this to probe the fact that
4273 # a service fired eventhough it is not RemainAfterExit
@@ -61,14 +92,11 @@ in import ./make-test-python.nix {
6192
6293 nesting.clone = [
6394 ({pkgs, ...}: {
64−
65− networking.extraHosts = ''
66− ${config.networking.primaryIPAddress} b.example.com
67− '';
6895 systemd.targets."acme-finished-b.example.com" = {};
6996 systemd.services."acme-b.example.com" = {
7097 wants = [ "acme-finished-b.example.com.target" ];
7198 before = [ "acme-finished-b.example.com.target" ];
99+ after = [ "nginx.service" ];
72100 };
73101 services.nginx.virtualHosts."b.example.com" = {
74102 enableACME = true;
@@ -79,15 +107,48 @@ in import ./make-test-python.nix {
79107 '';
80108 };
81109 })
110+ ({pkgs, config, nodes, lib, ...}: {
111+ security.acme.certs."example.com" = {
112+ domain = "*.example.com";
113+ dnsProvider = "exec";
114+ dnsPropagationCheck = false;
115+ credentialsFile = with pkgs; writeText "wildcard.env" ''
116+ EXEC_PATH=${dnsScript { inherit writeScript bash curl; dnsAddress = nodes.dnsserver.config.networking.primaryIPAddress; }}
117+ '';
118+ user = config.services.nginx.user;
119+ group = config.services.nginx.group;
120+ };
121+ systemd.targets."acme-finished-example.com" = {};
122+ systemd.services."acme-example.com" = {
123+ wants = [ "acme-finished-example.com.target" ];
124+ before = [ "acme-finished-example.com.target" "nginx.service" ];
125+ wantedBy = [ "nginx.service" ];
126+ };
127+ services.nginx.virtualHosts."c.example.com" = {
128+ forceSSL = true;
129+ sslCertificate = config.security.acme.certs."example.com".directory + "/cert.pem";
130+ sslTrustedCertificate = config.security.acme.certs."example.com".directory + "/full.pem";
131+ sslCertificateKey = config.security.acme.certs."example.com".directory + "/key.pem";
132+ locations."/".root = pkgs.runCommand "docroot" {} ''
133+ mkdir -p "$out"
134+ echo hello world > "$out/index.html"
135+ '';
136+ };
137+ })
82138 ];
83139 };
84140
85− client = commonConfig;
141+ client = {nodes, lib, ...}: {
142+ imports = [ commonConfig ];
143+ networking.nameservers = lib.mkForce [
144+ nodes.dnsserver.config.networking.primaryIPAddress
145+ ];
146+ };
86147 };
87148
88149 testScript = {nodes, ...}:
89150 let
90− newServerSystem = nodes.webserver2.config.system.build.toplevel;
151+ newServerSystem = nodes.webserver.config.system.build.toplevel;
91152 switchToNewServer = "${newServerSystem}/bin/switch-to-configuration test";
92153 in
93154 # Note, wait_for_unit does not work for oneshot services that do not have RemainAfterExit=true,
@@ -97,6 +158,17 @@ in import ./make-test-python.nix {
97158 # can use them to probe that a oneshot fired. It is a bit ugly, but it is the best we can do
98159 ''
99160 client.start()
161+ dnsserver.start()
162+
163+ letsencrypt.wait_for_unit("default.target")
164+ dnsserver.wait_for_unit("pebble-challtestsrv.service")
165+ client.succeed(
166+ 'curl --data \'{"host": "acme-v02.api.letsencrypt.org", "addresses": ["${nodes.letsencrypt.config.networking.primaryIPAddress}"]}\' http://${nodes.dnsserver.config.networking.primaryIPAddress}:8055/add-a'
167+ )
168+ client.succeed(
169+ 'curl --data \'{"host": "standalone.com", "addresses": ["${nodes.acmeStandalone.config.networking.primaryIPAddress}"]}\' http://${nodes.dnsserver.config.networking.primaryIPAddress}:8055/add-a'
170+ )
171+
100172 letsencrypt.start()
101173 acmeStandalone.start()
102174
@@ -129,5 +201,17 @@ in import ./make-test-python.nix {
129201 client.succeed(
130202 "curl --cacert /tmp/ca.crt https://b.example.com/ | grep -qF 'hello world'"
131203 )
204+
205+ with subtest("Can request wildcard certificates using DNS-01 challenge"):
206+ webserver.succeed(
207+ "${switchToNewServer}"
208+ )
209+ webserver.succeed(
210+ "/run/current-system/fine-tune/child-2/bin/switch-to-configuration test"
211+ )
212+ webserver.wait_for_unit("acme-finished-example.com.target")
213+ client.succeed(
214+ "curl --cacert /tmp/ca.crt https://c.example.com/ | grep -qF 'hello world'"
215+ )
132216 '';
133217}
nixos/tests/common/letsencrypt/common.nix+3
@@ -5,5 +5,8 @@ in {
55 nodes.letsencrypt.config.networking.primaryIPAddress
66 ];
77
8+ security.acme.acceptTerms = true;
9+ security.acme.email = "webmaster@example.com";
10+
811 security.pki.certificateFiles = [ letsencrypt-ca ];
912}