]>
Commit | Line | Data |
---|---|---|
1 | { | |
2 | description = "Secrets handling"; | |
3 | ||
4 | outputs = { self }: { | |
5 | nixosModule = { config, lib, pkgs, ... }: { | |
6 | options.secrets = with lib; { | |
7 | keys = mkOption { | |
8 | type = types.attrsOf (types.submodule { | |
9 | options = { | |
10 | isTemplated = mkOption { | |
11 | type = types.bool; | |
12 | default = true; | |
13 | description = "If the file is a gucci template that needs to be resolved"; | |
14 | }; | |
15 | isDir = mkOption { | |
16 | type = types.bool; | |
17 | default = false; | |
18 | description = "If the entry is a directory"; | |
19 | }; | |
20 | group = mkOption { | |
21 | type = types.str; | |
22 | default = "root"; | |
23 | description = "Group to associate to the entry"; | |
24 | }; | |
25 | user = mkOption { | |
26 | type = types.str; | |
27 | default = "root"; | |
28 | description = "User to associate to the entry"; | |
29 | }; | |
30 | permissions = mkOption { | |
31 | type = types.str; | |
32 | default = "0600"; | |
33 | description = "Permissions to associate to the entry"; | |
34 | }; | |
35 | text = mkOption { | |
36 | type = types.str; | |
37 | description = "Content of the entry"; | |
38 | }; | |
39 | }; | |
40 | }); | |
41 | default = {}; | |
42 | description = "Keys attrs to upload to the server"; | |
43 | apply = lib.mapAttrsToList (dest: v: v // { inherit dest; }); | |
44 | }; | |
45 | gpgKeys = mkOption { | |
46 | type = types.listOf types.path; | |
47 | default = []; | |
48 | description = "GPG public keys files to encrypt to"; | |
49 | }; | |
50 | ageKeys = mkOption { | |
51 | type = types.listOf types.str; | |
52 | default = []; | |
53 | description = "AGE keys to encrypt to"; | |
54 | }; | |
55 | decryptKey = mkOption { | |
56 | type = types.str; | |
57 | default = "/etc/ssh/ssh_host_ed25519_key"; | |
58 | description = "ed25519 key used to decrypt with AGE"; | |
59 | }; | |
60 | location = mkOption { | |
61 | type = types.path; | |
62 | default = "/var/secrets"; | |
63 | description = "Location where to put the keys"; | |
64 | }; | |
65 | secretsVars = mkOption { | |
66 | type = types.path; | |
67 | description = "Location where the secrets variables are defined, to be used to fill the templates in secrets"; | |
68 | }; | |
69 | deleteSecretsVars = mkOption { | |
70 | type = types.bool; | |
71 | default = false; | |
72 | description = "Delete secrets file after deployment"; | |
73 | }; | |
74 | # Read-only variables | |
75 | fullPaths = mkOption { | |
76 | type = types.attrsOf types.path; | |
77 | default = builtins.listToAttrs | |
78 | (map (v: { name = v.dest; value = "${config.secrets.location}/${v.dest}"; }) config.secrets.keys); | |
79 | readOnly = true; | |
80 | description = "set of full paths to secrets"; | |
81 | }; | |
82 | }; | |
83 | ||
84 | config = let | |
85 | location = config.secrets.location; | |
86 | keys = config.secrets.keys; | |
87 | empty = pkgs.runCommand "empty" { preferLocalBuild = true; } "mkdir -p $out && touch $out/done"; | |
88 | fpath = v: "secrets/${v.dest}${lib.optionalString v.isTemplated ".gucci.tpl"}"; | |
89 | dumpKey = v: | |
90 | if v.isDir then | |
91 | '' | |
92 | mkdir -p secrets/${v.dest} | |
93 | cat >> mods <<EOF | |
94 | ${v.user} ${v.group} ${v.permissions} secrets/${v.dest} | |
95 | EOF | |
96 | '' | |
97 | else '' | |
98 | mkdir -p secrets/$(dirname ${v.dest}) | |
99 | echo -n ${lib.strings.escapeShellArg v.text} > ${fpath v} | |
100 | cat >> mods <<EOF | |
101 | ${v.user} ${v.group} ${v.permissions} ${fpath v} | |
102 | EOF | |
103 | ''; | |
104 | toOutputs = n: v: if v.path or false then n else pkgs.lib.flatten (map (v': (import n)."${v'}") v.outputs); | |
105 | inputs = pkgs.lib.unique (pkgs.lib.flatten (map (v: | |
106 | if v.isDir then [] | |
107 | else pkgs.lib.mapAttrsToList toOutputs (builtins.getContext v.text) | |
108 | ) keys)); | |
109 | secrets = pkgs.runCommand "secrets.tar.enc" { | |
110 | buildInputs = [ pkgs.gnupg pkgs.sops ]; | |
111 | } '' | |
112 | touch mods | |
113 | tar --format=ustar --mtime='1970-01-01' -P --transform="s@${empty}@secrets@" -cf $out ${empty}/done | |
114 | ${builtins.concatStringsSep "\n" (map dumpKey keys)} | |
115 | cat mods | while read u g p k; do | |
116 | tar --no-recursion --format=ustar --mtime='1970-01-01' --owner="$u" --group="$g" --mode="$p" --append -f $out "$k" | |
117 | done | |
118 | export HOME=$(pwd) | |
119 | fingerprints= | |
120 | for key in ${builtins.concatStringsSep " " config.secrets.gpgKeys}; do | |
121 | gpg --import $key 2>/dev/null | |
122 | fingerprints=$fingerprints,$(cat $key | gpg --with-colons --import-options show-only --import 2>/dev/null | grep ^fpr | cut -d: -f10 | head -n1) | |
123 | done | |
124 | ||
125 | sops --age ${builtins.concatStringsSep "," config.secrets.ageKeys} --pgp ''${fingerprints#,} --input-type binary -i -e $out 2>/dev/null | |
126 | cat $out | ${pkgs.jq}/bin/jq --argjson inputs '${builtins.toJSON inputs}' '.sops.nixInputs = $inputs' | ${pkgs.moreutils}/bin/sponge $out | |
127 | ''; | |
128 | pathChmodExcl = | |
129 | let | |
130 | dirs = builtins.filter (v: v.isDir) keys; | |
131 | exclPath = builtins.concatStringsSep " -o " (map (d: " -path $TMP/${d.dest}") dirs); | |
132 | in | |
133 | lib.optionalString (builtins.length dirs > 0) " -not \\( ${exclPath} \\) "; | |
134 | in lib.mkIf (builtins.length keys > 0) { | |
135 | system.activationScripts.secrets = { | |
136 | deps = [ "users" "wrappers" ]; | |
137 | text = '' | |
138 | install -m0750 -o root -g keys -d ${location} | |
139 | TMP=$(${pkgs.coreutils}/bin/mktemp -d) | |
140 | TMPWORK=$(${pkgs.coreutils}/bin/mktemp -d) | |
141 | chmod go-rwx $TMPWORK | |
142 | if [ -n "$TMP" -a -n "$TMPWORK" -a -f ${config.secrets.secretsVars} ]; then | |
143 | install -m0750 -o root -g keys -d $TMP | |
144 | ${pkgs.ssh-to-age}/bin/ssh-to-age -private-key -i ${config.secrets.decryptKey} -o $TMPWORK/keys.txt | |
145 | SOPS_AGE_KEY_FILE=$TMPWORK/keys.txt ${pkgs.sops}/bin/sops -d ${secrets} | ${pkgs.gnutar}/bin/tar --strip-components 1 -C $TMP -x | |
146 | SOPS_AGE_KEY_FILE=$TMPWORK/keys.txt ${pkgs.sops}/bin/sops -d ${config.secrets.secretsVars} > $TMPWORK/vars.yml | |
147 | if [ -f $TMPWORK/vars.yml ]; then | |
148 | find $TMP -name "*.gucci.tpl" -exec \ | |
149 | /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}' \; | |
150 | fi | |
151 | find $TMP -type d ${pathChmodExcl}-exec chown root:keys {} \; -exec chmod o-rx {} \; | |
152 | ${pkgs.rsync}/bin/rsync --exclude="*.gucci.tpl" -O -c -av --delete $TMP/ ${location} | |
153 | rm -rf $TMP $TMPWORK ${lib.optionalString config.secrets.deleteSecretsVars config.secrets.secretsVars} | |
154 | fi | |
155 | ''; | |
156 | }; | |
157 | ||
158 | }; | |
159 | }; | |
160 | }; | |
161 | } |