summary refs log tree commit diff
path: root/nixos/tests/certmgr.nix
blob: 8f5b89487793ba3aeb857de8770c31716192a799 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
{ system ? builtins.currentSystem,
  config ? {},
  pkgs ? import ../.. { inherit system config; }
}:

with import ../lib/testing-python.nix { inherit system pkgs; };
let
  mkSpec = { host, service ? null, action }: {
    inherit action;
    authority = {
      file = {
        group = "nginx";
        owner = "nginx";
        path = "/var/ssl/${host}-ca.pem";
      };
      label = "www_ca";
      profile = "three-month";
      remote = "localhost:8888";
    };
    certificate = {
      group = "nginx";
      owner = "nginx";
      path = "/var/ssl/${host}-cert.pem";
    };
    private_key = {
      group = "nginx";
      mode = "0600";
      owner = "nginx";
      path = "/var/ssl/${host}-key.pem";
    };
    request = {
      CN = host;
      hosts = [ host "www.${host}" ];
      key = {
        algo = "rsa";
        size = 2048;
      };
      names = [
        {
          C = "US";
          L = "San Francisco";
          O = "Example, LLC";
          ST = "CA";
        }
      ];
    };
    inherit service;
  };

  mkCertmgrTest = { svcManager, specs, testScript }: makeTest {
    name = "certmgr-" + svcManager;
    nodes = {
      machine = { config, lib, pkgs, ... }: {
        networking.firewall.allowedTCPPorts = with config.services; [ cfssl.port certmgr.metricsPort ];
        networking.extraHosts = "127.0.0.1 imp.example.org decl.example.org";

        services.cfssl.enable = true;
        systemd.services.cfssl.after = [ "cfssl-init.service" "networking.target" ];

        systemd.tmpfiles.rules = [ "d /var/ssl 777 root root" ];

        systemd.services.cfssl-init = {
          description = "Initialize the cfssl CA";
          wantedBy    = [ "multi-user.target" ];
          serviceConfig = {
            User             = "cfssl";
            Type             = "oneshot";
            WorkingDirectory = config.services.cfssl.dataDir;
          };
          script = ''
            ${pkgs.cfssl}/bin/cfssl genkey -initca ${pkgs.writeText "ca.json" (builtins.toJSON {
              hosts = [ "ca.example.com" ];
              key = {
                algo = "rsa"; size = 4096; };
                names = [
                  {
                    C = "US";
                    L = "San Francisco";
                    O = "Internet Widgets, LLC";
                    OU = "Certificate Authority";
                    ST = "California";
                  }
                ];
            })} | ${pkgs.cfssl}/bin/cfssljson -bare ca
          '';
        };

        services.nginx = {
          enable = true;
          virtualHosts = lib.mkMerge (map (host: {
            ${host} = {
              sslCertificate = "/var/ssl/${host}-cert.pem";
              sslCertificateKey = "/var/ssl/${host}-key.pem";
              extraConfig = ''
                ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
              '';
              onlySSL = true;
              serverName = host;
              root = pkgs.writeTextDir "index.html" "It works!";
            };
          }) [ "imp.example.org" "decl.example.org" ]);
        };

        systemd.services.nginx.wantedBy = lib.mkForce [];

        systemd.services.certmgr.after = [ "cfssl.service" ];
        services.certmgr = {
          enable = true;
          inherit svcManager;
          inherit specs;
        };

      };
    };
    inherit testScript;
  };
in
{
  systemd = mkCertmgrTest {
    svcManager = "systemd";
    specs = {
      decl = mkSpec { host = "decl.example.org"; service = "nginx"; action ="restart"; };
      imp = toString (pkgs.writeText "test.json" (builtins.toJSON (
        mkSpec { host = "imp.example.org"; service = "nginx"; action = "restart"; }
      )));
    };
    testScript = ''
      machine.wait_for_unit("cfssl.service")
      machine.wait_until_succeeds("ls /var/ssl/decl.example.org-ca.pem")
      machine.wait_until_succeeds("ls /var/ssl/decl.example.org-key.pem")
      machine.wait_until_succeeds("ls /var/ssl/decl.example.org-cert.pem")
      machine.wait_until_succeeds("ls /var/ssl/imp.example.org-ca.pem")
      machine.wait_until_succeeds("ls /var/ssl/imp.example.org-key.pem")
      machine.wait_until_succeeds("ls /var/ssl/imp.example.org-cert.pem")
      machine.wait_for_unit("nginx.service")
      assert 1 < int(machine.succeed('journalctl -u nginx | grep "Starting Nginx" | wc -l'))
      machine.succeed("curl --cacert /var/ssl/imp.example.org-ca.pem https://imp.example.org")
      machine.succeed(
          "curl --cacert /var/ssl/decl.example.org-ca.pem https://decl.example.org"
      )
    '';
  };

  command = mkCertmgrTest {
    svcManager = "command";
    specs = {
      test = mkSpec { host = "command.example.org"; action = "touch /tmp/command.executed"; };
    };
    testScript = ''
      machine.wait_for_unit("cfssl.service")
      machine.wait_until_succeeds("stat /tmp/command.executed")
    '';
  };

}