summaryrefslogtreecommitdiffstats
path: root/nixos/tests/cfssl.nix
blob: fc407ba67f0b6ee5c251c149ac3618d1ad27f065 (plain)
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
{ pkgs, ... }:
{
  name = "cfssl";

  nodes.machine =
    {
      config,
      lib,
      pkgs,
      ...
    }:
    {
      networking.firewall.allowedTCPPorts = [ config.services.cfssl.port ];

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

      systemd.services.cfssl-init = {
        description = "Initialize the cfssl CA";
        wantedBy = [ "multi-user.target" ];
        serviceConfig = {
          User = "cfssl";
          Type = "oneshot";
          WorkingDirectory = config.services.cfssl.dataDir;
        };
        script = with 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";
                  }
                ];
              }
            )
          } | ${cfssl}/bin/cfssljson -bare ca
        '';
      };
    };

  testScript =
    let
      cfsslrequest =
        with pkgs;
        writeScript "cfsslrequest" ''
          curl -f -X POST -H "Content-Type: application/json" -d @${csr} \
            http://localhost:8888/api/v1/cfssl/newkey | ${cfssl}/bin/cfssljson /tmp/certificate
        '';
      csr = pkgs.writeText "csr.json" (
        builtins.toJSON {
          CN = "www.example.com";
          hosts = [
            "example.com"
            "www.example.com"
          ];
          key = {
            algo = "rsa";
            size = 2048;
          };
          names = [
            {
              C = "US";
              L = "San Francisco";
              O = "Example Company, LLC";
              OU = "Operations";
              ST = "California";
            }
          ];
        }
      );
    in
    ''
      machine.wait_for_unit("cfssl.service")
      machine.wait_until_succeeds("${cfsslrequest}")
      machine.succeed("ls /tmp/certificate-key.pem")
    '';
}