]> git.immae.eu Git - perso/Immae/Config/Nix.git/blame - flakes/secrets/flake.nix
Squash changes containing private information
[perso/Immae/Config/Nix.git] / flakes / secrets / flake.nix
CommitLineData
da30ae4f
IB
1{
2 description = "Secrets handling";
3
4 outputs = { self }: {
5 nixosModule = { config, lib, pkgs, ... }: {
1a64deeb
IB
6 # Necessary for situations where flake gets included multiple times
7 key = builtins.hashString "sha256" (builtins.path { path = self.sourceInfo.outPath; name = "source"; });
da30ae4f
IB
8 options.secrets = with lib; {
9 keys = mkOption {
4c4652aa
IB
10 type = types.attrsOf (types.submodule {
11 options = {
12 isTemplated = mkOption {
13 type = types.bool;
14 default = true;
15 description = "If the file is a gucci template that needs to be resolved";
16 };
17 isDir = mkOption {
18 type = types.bool;
19 default = false;
20 description = "If the entry is a directory";
21 };
22 group = mkOption {
23 type = types.str;
24 default = "root";
25 description = "Group to associate to the entry";
26 };
27 user = mkOption {
28 type = types.str;
29 default = "root";
30 description = "User to associate to the entry";
31 };
32 permissions = mkOption {
33 type = types.str;
34 default = "0600";
35 description = "Permissions to associate to the entry";
36 };
37 text = mkOption {
38 type = types.str;
39 description = "Content of the entry";
40 };
1a64deeb
IB
41 keyDependencies = mkOption {
42 default = [];
43 type = types.listOf (types.either types.path types.package);
44 description = ''
45 (public) system dependencies that needs to be
46 uploaded with the key.
47
48 keyDependencies + ignoredKeyDependencies should
49 contain the exhaustive list of the text context.
50
51 A warning will be thrown if there are remaning
52 dependencies from the text.
53 '';
54 };
55 ignoredKeyDependencies = mkOption {
56 default = [];
57 type = types.listOf (types.either types.path types.package);
58 description = ''
59 dependencies that must not be sent along with the key.
60
61 keyDependencies + ignoredKeyDependencies should
62 contain the exhaustive list of the text context.
63
64 A warning will be thrown if there are remaning
65 dependencies from the text.
66 '';
67 };
4c4652aa
IB
68 };
69 });
70 default = {};
71 description = "Keys attrs to upload to the server";
1a64deeb 72 apply = builtins.mapAttrs (dest: v: v // { inherit dest; });
da30ae4f
IB
73 };
74 gpgKeys = mkOption {
75 type = types.listOf types.path;
76 default = [];
77 description = "GPG public keys files to encrypt to";
78 };
79 ageKeys = mkOption {
80 type = types.listOf types.str;
81 default = [];
82 description = "AGE keys to encrypt to";
83 };
84 decryptKey = mkOption {
85 type = types.str;
86 default = "/etc/ssh/ssh_host_ed25519_key";
87 description = "ed25519 key used to decrypt with AGE";
88 };
89 location = mkOption {
90 type = types.path;
91 default = "/var/secrets";
92 description = "Location where to put the keys";
93 };
94 secretsVars = mkOption {
95 type = types.path;
96 description = "Location where the secrets variables are defined, to be used to fill the templates in secrets";
97 };
98 deleteSecretsVars = mkOption {
99 type = types.bool;
100 default = false;
101 description = "Delete secrets file after deployment";
102 };
103 # Read-only variables
104 fullPaths = mkOption {
105 type = types.attrsOf types.path;
1a64deeb
IB
106 default = builtins.mapAttrs
107 (n: v: "${config.secrets.location}/${n}") config.secrets.keys;
da30ae4f
IB
108 readOnly = true;
109 description = "set of full paths to secrets";
110 };
111 };
112
113 config = let
114 location = config.secrets.location;
1a64deeb 115 keys = builtins.attrValues config.secrets.keys;
da30ae4f 116 empty = pkgs.runCommand "empty" { preferLocalBuild = true; } "mkdir -p $out && touch $out/done";
4c4652aa 117 fpath = v: "secrets/${v.dest}${lib.optionalString v.isTemplated ".gucci.tpl"}";
da30ae4f 118 dumpKey = v:
4c4652aa 119 if v.isDir then
da30ae4f
IB
120 ''
121 mkdir -p secrets/${v.dest}
122 cat >> mods <<EOF
4c4652aa 123 ${v.user} ${v.group} ${v.permissions} secrets/${v.dest}
da30ae4f
IB
124 EOF
125 ''
126 else ''
127 mkdir -p secrets/$(dirname ${v.dest})
128 echo -n ${lib.strings.escapeShellArg v.text} > ${fpath v}
129 cat >> mods <<EOF
4c4652aa 130 ${v.user} ${v.group} ${v.permissions} ${fpath v}
da30ae4f
IB
131 EOF
132 '';
133 secrets = pkgs.runCommand "secrets.tar.enc" {
134 buildInputs = [ pkgs.gnupg pkgs.sops ];
135 } ''
136 touch mods
137 tar --format=ustar --mtime='1970-01-01' -P --transform="s@${empty}@secrets@" -cf $out ${empty}/done
138 ${builtins.concatStringsSep "\n" (map dumpKey keys)}
139 cat mods | while read u g p k; do
140 tar --no-recursion --format=ustar --mtime='1970-01-01' --owner="$u" --group="$g" --mode="$p" --append -f $out "$k"
141 done
142 export HOME=$(pwd)
143 fingerprints=
144 for key in ${builtins.concatStringsSep " " config.secrets.gpgKeys}; do
145 gpg --import $key 2>/dev/null
146 fingerprints=$fingerprints,$(cat $key | gpg --with-colons --import-options show-only --import 2>/dev/null | grep ^fpr | cut -d: -f10 | head -n1)
147 done
148
149 sops --age ${builtins.concatStringsSep "," config.secrets.ageKeys} --pgp ''${fingerprints#,} --input-type binary -i -e $out 2>/dev/null
150 '';
151 pathChmodExcl =
152 let
4c4652aa 153 dirs = builtins.filter (v: v.isDir) keys;
da30ae4f
IB
154 exclPath = builtins.concatStringsSep " -o " (map (d: " -path $TMP/${d.dest}") dirs);
155 in
156 lib.optionalString (builtins.length dirs > 0) " -not \\( ${exclPath} \\) ";
1a64deeb
IB
157
158 checkKeyDependencies = key:
159 let
160 allDeps = builtins.map (n: if builtins.isPath n then "${n}" else n.drvPath) (key.keyDependencies ++ key.ignoredKeyDependencies);
161 context = builtins.attrNames (builtins.getContext key.text);
162 missing = builtins.foldl' (o: n: lib.remove n o) context allDeps;
163 in
164 lib.optional (!key.isDir && builtins.length missing > 0)
165 ''
166 Key ${key.dest} has non declared dependencies in its context: ${builtins.concatStringsSep " " missing}
167 Add them to ignoredKeyDependencies to ignore
168 '';
da30ae4f 169 in lib.mkIf (builtins.length keys > 0) {
1a64deeb
IB
170 warnings = lib.concatMap checkKeyDependencies keys;
171 # FIXME: Use lib.concatMap (k: k.keyDependencies) keys in latest nixpkgs
172 system.extraDependencies = lib.concatMap (k: builtins.map (dep:
173 if builtins.isPath dep then pkgs.writeText "extra-dep" "${dep}" else dep
174 ) k.keyDependencies) keys;
da30ae4f
IB
175 system.activationScripts.secrets = {
176 deps = [ "users" "wrappers" ];
177 text = ''
178 install -m0750 -o root -g keys -d ${location}
179 TMP=$(${pkgs.coreutils}/bin/mktemp -d)
180 TMPWORK=$(${pkgs.coreutils}/bin/mktemp -d)
181 chmod go-rwx $TMPWORK
1a64deeb 182 if [ -n "$TMP" -a -n "$TMPWORK" -a -f ${config.secrets.secretsVars} ]; then
da30ae4f
IB
183 install -m0750 -o root -g keys -d $TMP
184 ${pkgs.ssh-to-age}/bin/ssh-to-age -private-key -i ${config.secrets.decryptKey} -o $TMPWORK/keys.txt
185 SOPS_AGE_KEY_FILE=$TMPWORK/keys.txt ${pkgs.sops}/bin/sops -d ${secrets} | ${pkgs.gnutar}/bin/tar --strip-components 1 -C $TMP -x
1a64deeb 186 SOPS_AGE_KEY_FILE=$TMPWORK/keys.txt ${pkgs.sops}/bin/sops -d ${config.secrets.secretsVars} > $TMPWORK/vars.yml
da30ae4f
IB
187 if [ -f $TMPWORK/vars.yml ]; then
188 find $TMP -name "*.gucci.tpl" -exec \
189 /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}' \;
190 fi
191 find $TMP -type d ${pathChmodExcl}-exec chown root:keys {} \; -exec chmod o-rx {} \;
192 ${pkgs.rsync}/bin/rsync --exclude="*.gucci.tpl" -O -c -av --delete $TMP/ ${location}
193 rm -rf $TMP $TMPWORK ${lib.optionalString config.secrets.deleteSecretsVars config.secrets.secretsVars}
194 fi
195 '';
196 };
197
198 };
199 };
200 };
201}