2017-07-27 12:24:17 +01:00
|
|
|
let
|
2018-09-24 20:06:31 +01:00
|
|
|
commonConfig = ./common/letsencrypt/common.nix;
|
2020-02-09 02:09:34 +00:00
|
|
|
|
|
|
|
dnsScript = {writeScript, dnsAddress, bash, curl}: writeScript "dns-hook.sh" ''
|
|
|
|
#!${bash}/bin/bash
|
|
|
|
set -euo pipefail
|
|
|
|
echo '[INFO]' "[$2]" 'dns-hook.sh' $*
|
|
|
|
if [ "$1" = "present" ]; then
|
|
|
|
${curl}/bin/curl --data '{"host": "'"$2"'", "value": "'"$3"'"}' http://${dnsAddress}:8055/set-txt
|
|
|
|
else
|
|
|
|
${curl}/bin/curl --data '{"host": "'"$2"'"}' http://${dnsAddress}:8055/clear-txt
|
|
|
|
fi
|
|
|
|
'';
|
|
|
|
|
2019-10-27 12:53:55 +00:00
|
|
|
in import ./make-test-python.nix {
|
2017-07-27 12:24:17 +01:00
|
|
|
name = "acme";
|
|
|
|
|
2019-08-29 15:32:59 +01:00
|
|
|
nodes = rec {
|
2020-02-09 02:09:34 +00:00
|
|
|
letsencrypt = { nodes, lib, ... }: {
|
|
|
|
imports = [ ./common/letsencrypt ];
|
|
|
|
networking.nameservers = lib.mkForce [
|
|
|
|
nodes.dnsserver.config.networking.primaryIPAddress
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
|
|
|
dnsserver = { nodes, pkgs, ... }: {
|
|
|
|
networking.firewall.allowedTCPPorts = [ 8055 53 ];
|
|
|
|
networking.firewall.allowedUDPPorts = [ 53 ];
|
|
|
|
systemd.services.pebble-challtestsrv = {
|
|
|
|
enable = true;
|
|
|
|
description = "Pebble ACME challenge test server";
|
|
|
|
wantedBy = [ "network.target" ];
|
|
|
|
serviceConfig = {
|
|
|
|
ExecStart = "${pkgs.pebble}/bin/pebble-challtestsrv -dns01 ':53' -defaultIPv6 '' -defaultIPv4 '${nodes.webserver.config.networking.primaryIPAddress}'";
|
|
|
|
# Required to bind on privileged ports.
|
|
|
|
User = "root";
|
|
|
|
Group = "root";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
2017-07-27 12:24:17 +01:00
|
|
|
|
2020-02-09 02:09:34 +00:00
|
|
|
acmeStandalone = { nodes, lib, config, pkgs, ... }: {
|
2019-08-29 15:32:59 +01:00
|
|
|
imports = [ commonConfig ];
|
2020-02-09 02:09:34 +00:00
|
|
|
networking.nameservers = lib.mkForce [
|
|
|
|
nodes.dnsserver.config.networking.primaryIPAddress
|
|
|
|
];
|
2019-08-29 15:32:59 +01:00
|
|
|
networking.firewall.allowedTCPPorts = [ 80 ];
|
2019-10-25 23:45:19 +01:00
|
|
|
security.acme = {
|
|
|
|
server = "https://acme-v02.api.letsencrypt.org/dir";
|
|
|
|
certs."standalone.com" = {
|
|
|
|
webroot = "/var/lib/acme/acme-challenges";
|
|
|
|
};
|
2019-08-29 15:32:59 +01:00
|
|
|
};
|
|
|
|
systemd.targets."acme-finished-standalone.com" = {};
|
|
|
|
systemd.services."acme-standalone.com" = {
|
|
|
|
wants = [ "acme-finished-standalone.com.target" ];
|
|
|
|
before = [ "acme-finished-standalone.com.target" ];
|
|
|
|
};
|
|
|
|
services.nginx.enable = true;
|
|
|
|
services.nginx.virtualHosts."standalone.com" = {
|
|
|
|
locations."/.well-known/acme-challenge".root = "/var/lib/acme/acme-challenges";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-02-09 11:34:17 +00:00
|
|
|
webserver = { nodes, config, pkgs, lib, ... }: {
|
2017-07-27 12:24:17 +01:00
|
|
|
imports = [ commonConfig ];
|
|
|
|
networking.firewall.allowedTCPPorts = [ 80 443 ];
|
2020-02-09 02:09:34 +00:00
|
|
|
networking.nameservers = lib.mkForce [
|
|
|
|
nodes.dnsserver.config.networking.primaryIPAddress
|
|
|
|
];
|
2017-07-27 12:24:17 +01:00
|
|
|
|
2019-08-29 15:32:59 +01:00
|
|
|
# A target remains active. Use this to probe the fact that
|
|
|
|
# a service fired eventhough it is not RemainAfterExit
|
|
|
|
systemd.targets."acme-finished-a.example.com" = {};
|
|
|
|
systemd.services."acme-a.example.com" = {
|
|
|
|
wants = [ "acme-finished-a.example.com.target" ];
|
|
|
|
before = [ "acme-finished-a.example.com.target" ];
|
|
|
|
};
|
|
|
|
|
2017-07-27 12:24:17 +01:00
|
|
|
services.nginx.enable = true;
|
2019-08-29 15:32:59 +01:00
|
|
|
|
|
|
|
services.nginx.virtualHosts."a.example.com" = {
|
2017-07-27 12:24:17 +01:00
|
|
|
enableACME = true;
|
|
|
|
forceSSL = true;
|
|
|
|
locations."/".root = pkgs.runCommand "docroot" {} ''
|
|
|
|
mkdir -p "$out"
|
|
|
|
echo hello world > "$out/index.html"
|
|
|
|
'';
|
|
|
|
};
|
2019-08-29 15:32:59 +01:00
|
|
|
|
2019-10-25 23:45:19 +01:00
|
|
|
security.acme.server = "https://acme-v02.api.letsencrypt.org/dir";
|
|
|
|
|
2019-08-29 15:32:59 +01:00
|
|
|
nesting.clone = [
|
|
|
|
({pkgs, ...}: {
|
|
|
|
systemd.targets."acme-finished-b.example.com" = {};
|
|
|
|
systemd.services."acme-b.example.com" = {
|
|
|
|
wants = [ "acme-finished-b.example.com.target" ];
|
|
|
|
before = [ "acme-finished-b.example.com.target" ];
|
2020-02-09 11:34:17 +00:00
|
|
|
after = [ "nginx.service" ];
|
2019-08-29 15:32:59 +01:00
|
|
|
};
|
|
|
|
services.nginx.virtualHosts."b.example.com" = {
|
|
|
|
enableACME = true;
|
|
|
|
forceSSL = true;
|
|
|
|
locations."/".root = pkgs.runCommand "docroot" {} ''
|
|
|
|
mkdir -p "$out"
|
|
|
|
echo hello world > "$out/index.html"
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
})
|
2020-02-09 02:09:34 +00:00
|
|
|
({pkgs, config, nodes, lib, ...}: {
|
|
|
|
security.acme.certs."example.com" = {
|
|
|
|
domain = "*.example.com";
|
|
|
|
dnsProvider = "exec";
|
|
|
|
dnsPropagationCheck = false;
|
|
|
|
credentialsFile = with pkgs; writeText "wildcard.env" ''
|
|
|
|
EXEC_PATH=${dnsScript { inherit writeScript bash curl; dnsAddress = nodes.dnsserver.config.networking.primaryIPAddress; }}
|
|
|
|
'';
|
|
|
|
user = config.services.nginx.user;
|
|
|
|
group = config.services.nginx.group;
|
|
|
|
};
|
|
|
|
systemd.targets."acme-finished-example.com" = {};
|
|
|
|
systemd.services."acme-example.com" = {
|
|
|
|
wants = [ "acme-finished-example.com.target" ];
|
|
|
|
before = [ "acme-finished-example.com.target" "nginx.service" ];
|
|
|
|
wantedBy = [ "nginx.service" ];
|
|
|
|
};
|
|
|
|
services.nginx.virtualHosts."c.example.com" = {
|
|
|
|
forceSSL = true;
|
|
|
|
sslCertificate = config.security.acme.certs."example.com".directory + "/cert.pem";
|
|
|
|
sslTrustedCertificate = config.security.acme.certs."example.com".directory + "/full.pem";
|
|
|
|
sslCertificateKey = config.security.acme.certs."example.com".directory + "/key.pem";
|
|
|
|
locations."/".root = pkgs.runCommand "docroot" {} ''
|
|
|
|
mkdir -p "$out"
|
|
|
|
echo hello world > "$out/index.html"
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
})
|
2019-08-29 15:32:59 +01:00
|
|
|
];
|
2017-07-27 12:24:17 +01:00
|
|
|
};
|
|
|
|
|
2020-02-09 02:09:34 +00:00
|
|
|
client = {nodes, lib, ...}: {
|
|
|
|
imports = [ commonConfig ];
|
|
|
|
networking.nameservers = lib.mkForce [
|
|
|
|
nodes.dnsserver.config.networking.primaryIPAddress
|
|
|
|
];
|
|
|
|
};
|
2017-07-27 12:24:17 +01:00
|
|
|
};
|
|
|
|
|
2019-10-18 18:13:04 +01:00
|
|
|
testScript = {nodes, ...}:
|
2019-08-29 15:32:59 +01:00
|
|
|
let
|
2020-02-09 02:09:34 +00:00
|
|
|
newServerSystem = nodes.webserver.config.system.build.toplevel;
|
2019-08-29 15:32:59 +01:00
|
|
|
switchToNewServer = "${newServerSystem}/bin/switch-to-configuration test";
|
|
|
|
in
|
2019-10-27 12:53:55 +00:00
|
|
|
# Note, wait_for_unit does not work for oneshot services that do not have RemainAfterExit=true,
|
2019-08-29 15:32:59 +01:00
|
|
|
# this is because a oneshot goes from inactive => activating => inactive, and never
|
|
|
|
# reaches the active state. To work around this, we create some mock target units which
|
|
|
|
# get pulled in by the oneshot units. The target units linger after activation, and hence we
|
|
|
|
# can use them to probe that a oneshot fired. It is a bit ugly, but it is the best we can do
|
|
|
|
''
|
2019-10-27 12:53:55 +00:00
|
|
|
client.start()
|
2020-02-09 02:09:34 +00:00
|
|
|
dnsserver.start()
|
|
|
|
|
|
|
|
letsencrypt.wait_for_unit("default.target")
|
|
|
|
dnsserver.wait_for_unit("pebble-challtestsrv.service")
|
|
|
|
client.succeed(
|
|
|
|
'curl --data \'{"host": "acme-v02.api.letsencrypt.org", "addresses": ["${nodes.letsencrypt.config.networking.primaryIPAddress}"]}\' http://${nodes.dnsserver.config.networking.primaryIPAddress}:8055/add-a'
|
|
|
|
)
|
|
|
|
client.succeed(
|
|
|
|
'curl --data \'{"host": "standalone.com", "addresses": ["${nodes.acmeStandalone.config.networking.primaryIPAddress}"]}\' http://${nodes.dnsserver.config.networking.primaryIPAddress}:8055/add-a'
|
|
|
|
)
|
|
|
|
|
2019-10-27 12:53:55 +00:00
|
|
|
letsencrypt.start()
|
|
|
|
acmeStandalone.start()
|
|
|
|
|
|
|
|
letsencrypt.wait_for_unit("default.target")
|
|
|
|
letsencrypt.wait_for_unit("pebble.service")
|
|
|
|
|
|
|
|
with subtest("can request certificate with HTTPS-01 challenge"):
|
|
|
|
acmeStandalone.wait_for_unit("default.target")
|
|
|
|
acmeStandalone.succeed("systemctl start acme-standalone.com.service")
|
|
|
|
acmeStandalone.wait_for_unit("acme-finished-standalone.com.target")
|
|
|
|
|
|
|
|
client.wait_for_unit("default.target")
|
|
|
|
|
|
|
|
client.succeed("curl https://acme-v02.api.letsencrypt.org:15000/roots/0 > /tmp/ca.crt")
|
|
|
|
client.succeed(
|
|
|
|
"curl https://acme-v02.api.letsencrypt.org:15000/intermediate-keys/0 >> /tmp/ca.crt"
|
|
|
|
)
|
|
|
|
|
|
|
|
with subtest("Can request certificate for nginx service"):
|
|
|
|
webserver.wait_for_unit("acme-finished-a.example.com.target")
|
|
|
|
client.succeed(
|
|
|
|
"curl --cacert /tmp/ca.crt https://a.example.com/ | grep -qF 'hello world'"
|
|
|
|
)
|
|
|
|
|
|
|
|
with subtest("Can add another certificate for nginx service"):
|
|
|
|
webserver.succeed(
|
|
|
|
"/run/current-system/fine-tune/child-1/bin/switch-to-configuration test"
|
|
|
|
)
|
|
|
|
webserver.wait_for_unit("acme-finished-b.example.com.target")
|
|
|
|
client.succeed(
|
|
|
|
"curl --cacert /tmp/ca.crt https://b.example.com/ | grep -qF 'hello world'"
|
|
|
|
)
|
2020-02-09 02:09:34 +00:00
|
|
|
|
|
|
|
with subtest("Can request wildcard certificates using DNS-01 challenge"):
|
|
|
|
webserver.succeed(
|
|
|
|
"${switchToNewServer}"
|
|
|
|
)
|
|
|
|
webserver.succeed(
|
|
|
|
"/run/current-system/fine-tune/child-2/bin/switch-to-configuration test"
|
|
|
|
)
|
|
|
|
webserver.wait_for_unit("acme-finished-example.com.target")
|
|
|
|
client.succeed(
|
|
|
|
"curl --cacert /tmp/ca.crt https://c.example.com/ | grep -qF 'hello world'"
|
|
|
|
)
|
2019-08-29 15:32:59 +01:00
|
|
|
'';
|
2017-07-27 12:24:17 +01:00
|
|
|
}
|