git.iko.soy/typeable/nixpkgs
on master help sign in
buildkite-agent: secrecy improvements: non-store, non-Nix provisioning of secrets
3fa4e1eKosyrev Serge
nixos/modules/services/continuous-integration/buildkite-agent.nix+41−23
@@ -4,14 +4,6 @@ with lib;
4
4
5let
5let
6 cfg = config.services.buildkite-agent;
6 cfg = config.services.buildkite-agent;
7− configFile = pkgs.writeText "buildkite-agent.cfg"
8− ''
9− token="${cfg.token}"
10− name="${cfg.name}"
11− meta-data="${cfg.meta-data}"
12− hooks-path="${cfg.package}/share/hooks"
13− build-path="${cfg.dataDir}"
14− '';
15in
7in
16
8
17{
9{
@@ -39,10 +31,13 @@ in
39 type = types.listOf types.package;
31 type = types.listOf types.package;
40 };
32 };
41
33
42− token = mkOption {
34+ tokenPath = mkOption {
43− type = types.str;
35+ type = types.path;
44 description = ''
36 description = ''
45 The token from your Buildkite "Agents" page.
37 The token from your Buildkite "Agents" page.
38+
39+ A run-time path to the token file, which is supposed to be provisioned
40+ outside of Nix store.
46 '';
41 '';
47 };
42 };
48
43
@@ -62,16 +57,22 @@ in
62 };
57 };
63
58
64 openssh =
59 openssh =
65− { privateKey = mkOption {
60+ { privateKeyPath = mkOption {
66− type = types.str;
61+ type = types.path;
67 description = ''
62 description = ''
68 Private agent key.
63 Private agent key.
64+
65+ A run-time path to the key file, which is supposed to be provisioned
66+ outside of Nix store.
69 '';
67 '';
70 };
68 };
71− publicKey = mkOption {
69+ publicKeyPath = mkOption {
72− type = types.str;
70+ type = types.path;
73 description = ''
71 description = ''
74 Public agent key.
72 Public agent key.
73+
74+ A run-time path to the key file, which is supposed to be provisioned
75+ outside of Nix store.
75 '';
76 '';
76 };
77 };
77 };
78 };
@@ -84,11 +85,15 @@ in
84 home = cfg.dataDir;
85 home = cfg.dataDir;
85 createHome = true;
86 createHome = true;
86 description = "Buildkite agent user";
87 description = "Buildkite agent user";
88+ extraGroups = [ "keys" ];
87 };
89 };
88
90
89 environment.systemPackages = [ cfg.package ];
91 environment.systemPackages = [ cfg.package ];
90
92
91 systemd.services.buildkite-agent =
93 systemd.services.buildkite-agent =
94+ let copy = x: target: perms:
95+ "cp -f ${x} ${target}; ${pkgs.coreutils}/bin/chmod ${toString perms} ${target}; ";
96+ in
92 { description = "Buildkite Agent";
97 { description = "Buildkite Agent";
93 wantedBy = [ "multi-user.target" ];
98 wantedBy = [ "multi-user.target" ];
94 after = [ "network.target" ];
99 after = [ "network.target" ];
@@ -97,18 +102,26 @@ in
97 HOME = cfg.dataDir;
102 HOME = cfg.dataDir;
98 NIX_REMOTE = "daemon";
103 NIX_REMOTE = "daemon";
99 };
104 };
100− preStart = ''
101− ${pkgs.coreutils}/bin/mkdir -m 0700 -p ${cfg.dataDir}/.ssh
102−
103− echo "${cfg.openssh.privateKey}" > ${cfg.dataDir}/.ssh/id_rsa
104− ${pkgs.coreutils}/bin/chmod 600 ${cfg.dataDir}/.ssh/id_rsa
105
105
106− echo "${cfg.openssh.publicKey}" > ${cfg.dataDir}/.ssh/id_rsa.pub
106+ ## NB: maximum care is taken so that secrets (ssh keys and the CI token)
107+ ## don't end up in the Nix store.
108+ preStart = ''
109+ ${pkgs.coreutils}/bin/mkdir -m 0700 -p ${cfg.dataDir}/.ssh
110+ ${copy (toString cfg.openssh.privateKeyPath) "${cfg.dataDir}/.ssh/id_rsa" 600}
107− ${pkgs.coreutils}/bin/chmod 600 ${cfg.dataDir}/.ssh/id_rsa.pub
111+ ${copy (toString cfg.openssh.publicKeyPath) "${cfg.dataDir}/.ssh/id_rsa.pub" 600}
112+
113+ cat > "${cfg.dataDir}/buildkite-agent.cfg" <<EOF
114+ token="$(cat ${toString cfg.tokenPath})"
115+ name="${cfg.name}"
116+ meta-data="${cfg.meta-data}"
117+ hooks-path="${pkgs.buildkite-agent}/share/hooks"
118+ build-path="${cfg.dataDir}/builds"
119+ bootstrap-script="${pkgs.buildkite-agent}/share/bootstrap.sh"
120+ EOF
108− '';
121+ '';
109
122
110 serviceConfig =
123 serviceConfig =
111− { ExecStart = "${pkgs.buildkite-agent}/bin/buildkite-agent start --config ${configFile}";
124+ { ExecStart = "${pkgs.buildkite-agent}/bin/buildkite-agent start --config /var/lib/buildkite-agent/buildkite-agent.cfg";
112 User = "buildkite-agent";
125 User = "buildkite-agent";
113 RestartSec = 5;
126 RestartSec = 5;
114 Restart = "on-failure";
127 Restart = "on-failure";
@@ -116,4 +129,9 @@ in
116 };
129 };
117 };
130 };
118 };
131 };
132+ imports = [
133+ (mkRenamedOptionModule [ "services" "buildkite-agent" "token" ] [ "services" "buildkite-agent" "tokenPath" ])
134+ (mkRenamedOptionModule [ "services" "buildkite-agent" "openssh" "privateKey" ] [ "services" "buildkite-agent" "openssh" "privateKeyPath" ])
135+ (mkRenamedOptionModule [ "services" "buildkite-agent" "openssh" "publicKey" ] [ "services" "buildkite-agent" "openssh" "publicKeyPath" ])
136+ ];
119}
137}