]>
Commit | Line | Data |
---|---|---|
1a718805 | 1 | { lib, pkgs, config, ... }: |
44742a43 | 2 | { |
1a718805 | 3 | options.secrets = { |
44742a43 IB |
4 | keys = lib.mkOption { |
5 | type = lib.types.listOf lib.types.unspecified; | |
1a718805 | 6 | default = []; |
44742a43 IB |
7 | description = "Keys to upload to server"; |
8 | }; | |
282c67a1 IB |
9 | gpgKeys = lib.mkOption { |
10 | type = lib.types.listOf lib.types.path; | |
11 | default = []; | |
12 | description = "GPG public keys files to encrypt to"; | |
13 | }; | |
14 | ageKeys = lib.mkOption { | |
15 | type = lib.types.listOf lib.types.str; | |
16 | default = []; | |
17 | description = "AGE keys to encrypt to"; | |
18 | }; | |
19 | decryptKey = lib.mkOption { | |
20 | type = lib.types.str; | |
21 | default = "/etc/ssh/ssh_host_ed25519_key"; | |
22 | description = "ed25519 key used to decrypt with AGE"; | |
23 | }; | |
1a718805 IB |
24 | location = lib.mkOption { |
25 | type = lib.types.path; | |
26 | default = "/var/secrets"; | |
27 | description = "Location where to put the keys"; | |
28 | }; | |
282c67a1 IB |
29 | secretsVars = lib.mkOption { |
30 | type = lib.types.path; | |
31 | default = "/run/keys/vars.yml"; | |
32 | description = "Location where the secrets variables are defined, to be used to fill the templates in secrets"; | |
33 | }; | |
34 | deleteSecretsVars = lib.mkOption { | |
35 | type = lib.types.bool; | |
36 | default = false; | |
37 | description = "Delete secrets file after deployment"; | |
38 | }; | |
717ccfd9 IB |
39 | # Read-only variables |
40 | fullPaths = lib.mkOption { | |
41 | type = lib.types.attrsOf lib.types.path; | |
42 | default = builtins.listToAttrs | |
43 | (map (v: { name = v.dest; value = "${config.secrets.location}/${v.dest}"; }) config.secrets.keys); | |
44 | readOnly = true; | |
45 | description = "set of full paths to secrets"; | |
46 | }; | |
44742a43 | 47 | }; |
717ccfd9 | 48 | |
44742a43 | 49 | config = let |
1a718805 IB |
50 | location = config.secrets.location; |
51 | keys = config.secrets.keys; | |
44742a43 | 52 | empty = pkgs.runCommand "empty" { preferLocalBuild = true; } "mkdir -p $out && touch $out/done"; |
50abe6fc | 53 | fpath = v: "secrets/${v.dest}${lib.optionalString (v.isTemplated or true) ".gucci.tpl"}"; |
44742a43 IB |
54 | dumpKey = v: '' |
55 | mkdir -p secrets/$(dirname ${v.dest}) | |
50abe6fc | 56 | echo -n ${lib.strings.escapeShellArg v.text} > ${fpath v} |
44742a43 | 57 | cat >> mods <<EOF |
50abe6fc | 58 | ${v.user or "root"} ${v.group or "root"} ${v.permissions or "0600"} ${fpath v} |
44742a43 IB |
59 | EOF |
60 | ''; | |
282c67a1 IB |
61 | secrets = pkgs.runCommand "secrets.tar.enc" { |
62 | buildInputs = [ pkgs.gnupg pkgs.sops ]; | |
63 | } '' | |
44742a43 IB |
64 | touch mods |
65 | tar --format=ustar --mtime='1970-01-01' -P --transform="s@${empty}@secrets@" -cf $out ${empty}/done | |
44742a43 IB |
66 | ${builtins.concatStringsSep "\n" (map dumpKey keys)} |
67 | cat mods | while read u g p k; do | |
68 | tar --format=ustar --mtime='1970-01-01' --owner="$u" --group="$g" --mode="$p" --append -f $out "$k" | |
69 | done | |
282c67a1 IB |
70 | export HOME=$(pwd) |
71 | fingerprints= | |
72 | for key in ${builtins.concatStringsSep " " config.secrets.gpgKeys}; do | |
73 | gpg --import $key 2>/dev/null | |
74 | fingerprints=$fingerprints,$(cat $key | gpg --with-colons --import-options show-only --import 2>/dev/null | grep ^fpr | cut -d: -f10 | head -n1) | |
75 | done | |
76 | ||
77 | sops --age ${builtins.concatStringsSep "," config.secrets.ageKeys} --pgp ''${fingerprints#,} --input-type binary -i -e $out 2>/dev/null | |
44742a43 | 78 | ''; |
1a718805 | 79 | in lib.mkIf (builtins.length keys > 0) { |
44742a43 IB |
80 | system.activationScripts.secrets = { |
81 | deps = [ "users" "wrappers" ]; | |
82 | text = '' | |
1a718805 | 83 | install -m0750 -o root -g keys -d ${location} |
282c67a1 IB |
84 | TMP=$(${pkgs.coreutils}/bin/mktemp -d) |
85 | TMPWORK=$(${pkgs.coreutils}/bin/mktemp -d) | |
86 | chmod go-rwx $TMPWORK | |
87 | if [ -n "$TMP" -a -n "$TMPWORK" ]; then | |
88 | install -m0750 -o root -g keys -d $TMP | |
89 | ${pkgs.ssh-to-age}/bin/ssh-to-age -private-key -i ${config.secrets.decryptKey} -o $TMPWORK/keys.txt | |
90 | SOPS_AGE_KEY_FILE=$TMPWORK/keys.txt ${pkgs.sops}/bin/sops -d ${secrets} | ${pkgs.gnutar}/bin/tar --strip-components 1 -C $TMP -x | |
91 | if [ -f ${config.secrets.secretsVars} ]; then | |
92 | SOPS_AGE_KEY_FILE=$TMPWORK/keys.txt ${pkgs.sops}/bin/sops -d ${config.secrets.secretsVars} > $TMPWORK/vars.yml | |
44742a43 | 93 | fi |
282c67a1 IB |
94 | if [ -f $TMPWORK/vars.yml ]; then |
95 | find $TMP -name "*.gucci.tpl" -exec \ | |
96 | /bin/sh -c 'f="{}"; ${pkgs.gucci}/bin/gucci -f '$TMPWORK'/vars.yml "$f" > "''${f%.gucci.tpl}"; touch --reference "$f" ''${f%.gucci.tpl} ; chmod --reference="$f" ''${f%.gucci.tpl} ; chown --reference="$f" ''${f%.gucci.tpl}' \; | |
97 | fi | |
98 | find $TMP -type d -exec chown root:keys {} \; -exec chmod o-rx {} \; | |
99 | ${pkgs.rsync}/bin/rsync --exclude="*.gucci.tpl" -O -c -av --delete $TMP/ ${location} | |
100 | rm -rf $TMP $TMPWORK ${lib.optionalString config.secrets.deleteSecretsVars config.secrets.secretsVars} | |
44742a43 IB |
101 | fi |
102 | ''; | |
103 | }; | |
50abe6fc | 104 | |
50abe6fc | 105 | deployment.secrets."secret_vars.yml" = { |
282c67a1 IB |
106 | source = builtins.toString ../nixops/secrets/vars.yml; |
107 | destination = config.secrets.secretsVars; | |
50abe6fc IB |
108 | owner.user = "root"; |
109 | owner.group = "root"; | |
110 | permissions = "0400"; | |
111 | }; | |
44742a43 IB |
112 | }; |
113 | } |