1 { system ? builtins.currentSystem,
3 pkgs ? import ../.. { inherit system config; }
6 with import ../lib/testing-python.nix { inherit system pkgs; };
8 mkSpec = { host, service ? null, action }: {
14 path = "/var/ssl/${host}-ca.pem";
17 profile = "three-month";
18 remote = "localhost:8888";
23 path = "/var/ssl/${host}-cert.pem";
29 path = "/var/ssl/${host}-key.pem";
33 hosts = [ host "www.${host}" ];
50 mkCertmgrTest = { svcManager, specs, testScript }: makeTest {
51 name = "certmgr-" + svcManager;
53 machine = { config, lib, pkgs, ... }: {
54 networking.firewall.allowedTCPPorts = with config.services; [ cfssl.port certmgr.metricsPort ];
55 networking.extraHosts = "127.0.0.1 imp.example.org decl.example.org";
57 services.cfssl.enable = true;
58 systemd.services.cfssl.after = [ "cfssl-init.service" "networking.target" ];
60 systemd.tmpfiles.rules = [ "d /var/ssl 777 root root" ];
62 systemd.services.cfssl-init = {
63 description = "Initialize the cfssl CA";
64 wantedBy = [ "multi-user.target" ];
68 WorkingDirectory = config.services.cfssl.dataDir;
71 ${pkgs.cfssl}/bin/cfssl genkey -initca ${pkgs.writeText "ca.json" (builtins.toJSON {
72 hosts = [ "ca.example.com" ];
74 algo = "rsa"; size = 4096; };
79 O = "Internet Widgets, LLC";
80 OU = "Certificate Authority";
84 })} | ${pkgs.cfssl}/bin/cfssljson -bare ca
90 virtualHosts = lib.mkMerge (map (host: {
92 sslCertificate = "/var/ssl/${host}-cert.pem";
93 sslCertificateKey = "/var/ssl/${host}-key.pem";
95 ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
99 root = pkgs.writeTextDir "index.html" "It works!";
101 }) [ "imp.example.org" "decl.example.org" ]);
104 systemd.services.nginx.wantedBy = lib.mkForce [];
106 systemd.services.certmgr.after = [ "cfssl.service" ];
119 systemd = mkCertmgrTest {
120 svcManager = "systemd";
122 decl = mkSpec { host = "decl.example.org"; service = "nginx"; action ="restart"; };
123 imp = toString (pkgs.writeText "test.json" (builtins.toJSON (
124 mkSpec { host = "imp.example.org"; service = "nginx"; action = "restart"; }
128 machine.wait_for_unit("cfssl.service")
129 machine.wait_until_succeeds("ls /var/ssl/decl.example.org-ca.pem")
130 machine.wait_until_succeeds("ls /var/ssl/decl.example.org-key.pem")
131 machine.wait_until_succeeds("ls /var/ssl/decl.example.org-cert.pem")
132 machine.wait_until_succeeds("ls /var/ssl/imp.example.org-ca.pem")
133 machine.wait_until_succeeds("ls /var/ssl/imp.example.org-key.pem")
134 machine.wait_until_succeeds("ls /var/ssl/imp.example.org-cert.pem")
135 machine.wait_for_unit("nginx.service")
136 assert 1 < int(machine.succeed('journalctl -u nginx | grep "Starting Nginx" | wc -l'))
137 machine.succeed("curl --cacert /var/ssl/imp.example.org-ca.pem https://imp.example.org")
139 "curl --cacert /var/ssl/decl.example.org-ca.pem https://decl.example.org"
144 command = mkCertmgrTest {
145 svcManager = "command";
147 test = mkSpec { host = "command.example.org"; action = "touch /tmp/command.executed"; };
150 machine.wait_for_unit("cfssl.service")
151 machine.wait_until_succeeds("stat /tmp/command.executed")