]>
Commit | Line | Data |
---|---|---|
da30ae4f IB |
1 | { |
2 | description = "Secrets handling"; | |
3 | ||
4 | outputs = { self }: { | |
5 | nixosModule = { config, lib, pkgs, ... }: { | |
6 | options.secrets = with lib; { | |
7 | keys = mkOption { | |
4c4652aa IB |
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; }); | |
da30ae4f IB |
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"; | |
4c4652aa | 88 | fpath = v: "secrets/${v.dest}${lib.optionalString v.isTemplated ".gucci.tpl"}"; |
da30ae4f | 89 | dumpKey = v: |
4c4652aa | 90 | if v.isDir then |
da30ae4f IB |
91 | '' |
92 | mkdir -p secrets/${v.dest} | |
93 | cat >> mods <<EOF | |
4c4652aa | 94 | ${v.user} ${v.group} ${v.permissions} secrets/${v.dest} |
da30ae4f IB |
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 | |
4c4652aa | 101 | ${v.user} ${v.group} ${v.permissions} ${fpath v} |
da30ae4f IB |
102 | EOF |
103 | ''; | |
104 | secrets = pkgs.runCommand "secrets.tar.enc" { | |
105 | buildInputs = [ pkgs.gnupg pkgs.sops ]; | |
106 | } '' | |
107 | touch mods | |
108 | tar --format=ustar --mtime='1970-01-01' -P --transform="s@${empty}@secrets@" -cf $out ${empty}/done | |
109 | ${builtins.concatStringsSep "\n" (map dumpKey keys)} | |
110 | cat mods | while read u g p k; do | |
111 | tar --no-recursion --format=ustar --mtime='1970-01-01' --owner="$u" --group="$g" --mode="$p" --append -f $out "$k" | |
112 | done | |
113 | export HOME=$(pwd) | |
114 | fingerprints= | |
115 | for key in ${builtins.concatStringsSep " " config.secrets.gpgKeys}; do | |
116 | gpg --import $key 2>/dev/null | |
117 | fingerprints=$fingerprints,$(cat $key | gpg --with-colons --import-options show-only --import 2>/dev/null | grep ^fpr | cut -d: -f10 | head -n1) | |
118 | done | |
119 | ||
120 | sops --age ${builtins.concatStringsSep "," config.secrets.ageKeys} --pgp ''${fingerprints#,} --input-type binary -i -e $out 2>/dev/null | |
121 | ''; | |
122 | pathChmodExcl = | |
123 | let | |
4c4652aa | 124 | dirs = builtins.filter (v: v.isDir) keys; |
da30ae4f IB |
125 | exclPath = builtins.concatStringsSep " -o " (map (d: " -path $TMP/${d.dest}") dirs); |
126 | in | |
127 | lib.optionalString (builtins.length dirs > 0) " -not \\( ${exclPath} \\) "; | |
128 | in lib.mkIf (builtins.length keys > 0) { | |
129 | system.activationScripts.secrets = { | |
130 | deps = [ "users" "wrappers" ]; | |
131 | text = '' | |
132 | install -m0750 -o root -g keys -d ${location} | |
133 | TMP=$(${pkgs.coreutils}/bin/mktemp -d) | |
134 | TMPWORK=$(${pkgs.coreutils}/bin/mktemp -d) | |
135 | chmod go-rwx $TMPWORK | |
5492ca52 | 136 | if [ -n "$TMP" -a -n "$TMPWORK" -a -f ${config.secrets.secretsVars} ]; then |
da30ae4f IB |
137 | install -m0750 -o root -g keys -d $TMP |
138 | ${pkgs.ssh-to-age}/bin/ssh-to-age -private-key -i ${config.secrets.decryptKey} -o $TMPWORK/keys.txt | |
139 | SOPS_AGE_KEY_FILE=$TMPWORK/keys.txt ${pkgs.sops}/bin/sops -d ${secrets} | ${pkgs.gnutar}/bin/tar --strip-components 1 -C $TMP -x | |
5492ca52 | 140 | SOPS_AGE_KEY_FILE=$TMPWORK/keys.txt ${pkgs.sops}/bin/sops -d ${config.secrets.secretsVars} > $TMPWORK/vars.yml |
da30ae4f IB |
141 | if [ -f $TMPWORK/vars.yml ]; then |
142 | find $TMP -name "*.gucci.tpl" -exec \ | |
143 | /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}' \; | |
144 | fi | |
145 | find $TMP -type d ${pathChmodExcl}-exec chown root:keys {} \; -exec chmod o-rx {} \; | |
146 | ${pkgs.rsync}/bin/rsync --exclude="*.gucci.tpl" -O -c -av --delete $TMP/ ${location} | |
147 | rm -rf $TMP $TMPWORK ${lib.optionalString config.secrets.deleteSecretsVars config.secrets.secretsVars} | |
148 | fi | |
149 | ''; | |
150 | }; | |
151 | ||
152 | }; | |
153 | }; | |
154 | }; | |
155 | } |