git.iko.soy/typeable/nixpkgs
on master help sign in
buildkite-agent: secrecy improvements: non-store, non-Nix provisioning of secrets
commit3fa4e1e3ee4faa16a6ee20ece6a786939f58fd69 authorKosyrev Serge <serge.kosyrev@iohk.io> dateThu 23 Nov 2017, 19:09 (9 years ago) parent3385c85
1 file changed+41−23
nixos/modules/services/continuous-integration/buildkite-agent.nix+41−23
nixos/modules/services/continuous-integration/buildkite-agent.nix+41−23
@@ -4,14 +4,6 @@ with lib;
44
55let
66 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− '';
157in
168
179{
@@ -39,10 +31,13 @@ in
3931 type = types.listOf types.package;
4032 };
4133
42− token = mkOption {
43− type = types.str;
34+ tokenPath = mkOption {
35+ type = types.path;
4436 description = ''
4537 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.
4641 '';
4742 };
4843
@@ -62,16 +57,22 @@ in
6257 };
6358
6459 openssh =
65− { privateKey = mkOption {
66− type = types.str;
60+ { privateKeyPath = mkOption {
61+ type = types.path;
6762 description = ''
6863 Private agent key.
64+
65+ A run-time path to the key file, which is supposed to be provisioned
66+ outside of Nix store.
6967 '';
7068 };
71− publicKey = mkOption {
72− type = types.str;
69+ publicKeyPath = mkOption {
70+ type = types.path;
7371 description = ''
7472 Public agent key.
73+
74+ A run-time path to the key file, which is supposed to be provisioned
75+ outside of Nix store.
7576 '';
7677 };
7778 };
@@ -84,11 +85,15 @@ in
8485 home = cfg.dataDir;
8586 createHome = true;
8687 description = "Buildkite agent user";
88+ extraGroups = [ "keys" ];
8789 };
8890
8991 environment.systemPackages = [ cfg.package ];
9092
9193 systemd.services.buildkite-agent =
94+ let copy = x: target: perms:
95+ "cp -f ${x} ${target}; ${pkgs.coreutils}/bin/chmod ${toString perms} ${target}; ";
96+ in
9297 { description = "Buildkite Agent";
9398 wantedBy = [ "multi-user.target" ];
9499 after = [ "network.target" ];
@@ -97,18 +102,26 @@ in
97102 HOME = cfg.dataDir;
98103 NIX_REMOTE = "daemon";
99104 };
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
105105
106− echo "${cfg.openssh.publicKey}" > ${cfg.dataDir}/.ssh/id_rsa.pub
107− ${pkgs.coreutils}/bin/chmod 600 ${cfg.dataDir}/.ssh/id_rsa.pub
108− '';
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}
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
121+ '';
109122
110123 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";
112125 User = "buildkite-agent";
113126 RestartSec = 5;
114127 Restart = "on-failure";
@@ -116,4 +129,9 @@ in
116129 };
117130 };
118131 };
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+ ];
119137}