forked from Platonic-Systems/secrix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flake.nix
484 lines (473 loc) · 18.1 KB
/
flake.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
{
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
outputs = { self, ... }@inputs:
let
inherit (builtins) toString;
inherit (pkgs) writeShellScript;
inherit (pkgs.lib.lists) foldl' flatten unique map filter elem genList last length range toList;
inherit (pkgs.lib.strings) splitString concatStringsSep stringLength optionalString concatMapStrings;
inherit (pkgs.lib.trivial) warnIf;
inherit (pkgs.lib.attrsets) attrValues mapAttrsToList filterAttrs attrNames mapAttrs foldlAttrs recursiveUpdate;
pkgs = inputs.nixpkgs.legacyPackages."x86_64-linux";
getopts = opts: (
let
cases = mapAttrsToList
(n: v:
let
attrIsLong = stringLength n > 1;
short = "-${v.short or (optionalString (! attrIsLong) n)}";
long = "--${v.long or (optionalString attrIsLong n)}";
match = filter (x: !(elem x [ "-" "--" ])) [ short long ];
takes = v.takes or 1;
inherit (v) run;
shifts = concatStringsSep "\n" (genList (_: "shift") takes);
in
''
${concatStringsSep "|" match})
${run}
${shifts}
;;
'')
opts;
in
''
positionalOpts=()
while [[ "$#" -gt 0 ]]; do
currentOpt="$1"
shift
case $currentOpt in
${concatStringsSep "\n" cases}
*)
positionalOpts+=("$currentOpt")
;;
esac
done
''
);
in {
secrix = fInputs: {
type = "app";
program = let
help = concatStringsSep "\n" (map (x: "echo \"${x}\"") (splitString "\n" ''
ENCRYPTION
nix run .#secrix create OUTPUT-FILE -- [-u USER | --all-users] [-s SYSTEM | --all-systems] [-r RECIPIENT]
-u | --user
Add all keys (for all secrets and systems) for a given user as specified
in any defaultEncryptKeys or encryptKeys option. Can be specified multiple
times.
-s | --system
Add the hostPubKey for the system specified. Can be specified multiple
times.
-r | --recipient
Add an ad hoc public key for encryption. Can be specified multiple times.
REENCRYPTION
nix run .#secrix rekey FILE -- -i IDENTITY-FILE [-u USER | --all-users] [-s SYSTEM | --all-systems] [-r RECIPIENT]
-i | --identity
Required. The path of a private key which can be used to decrypt the file.
-u, -s, and -r operate the same as in ENCRYPTION.
EDITING
nix run .#secrix edit FILE -- -i IDENTITY_FILE [-u USER | --all-users] [-s SYSTEM | --all-systems] [-r RECIPIENT]
All flags operate the same as in REENCRYPTION.
OTHER
nix run .#secrix -- [-h | --help]
Display this message.
nix run .#secrix -- (--list-users | --list-systems | -l)
--list-users
List all users found.
--list-systems
List all systems found.
-l | --list-all
List all users and systems found.
NOTES
* Due to the way nix run works, all flags must be specified after the --.
Positional parameters can be placed wherever, however must be in the same
order as specified in the documentation.
* Due to constraints in nix, existing keys used for encryption will not be
re-used and must be specified again. Hoefully this will be fixed in the
future.
''));
opts = {
u = {
long = "user";
run = ''
if [[ -z "''${userKeys["$1"]}" ]]; then
echo "No user found: $1" 1>&2
exit 1
fi
recips+="''${userKeys["$1"]}"
'';
};
all-users = {
run = ''
recips+="''${userKeys[@]}"
'';
takes = 0;
};
s = {
long = "system";
run = ''
if [[ -z "''${hostKeys["$1"]}" ]]; then
echo "No system found: $1" 1>&2
exit 1
fi
recips+="''${hostKeys["$1"]}"
'';
};
i = {
long = "identity";
run = ''
identityFile="$1"
'';
};
all-systems = {
run = ''
recips+="''${hostKeys[@]}"
'';
takes = 0;
};
list-users.run = ''
for i in "''${!userKeys[@]}"; do
echo "$i"
done
exit 0
'';
list-systems.run = ''
for i in "''${!hostKeys[@]}"; do
echo "$i"
done
exit 0
'';
l = {
long = "list-all";
run = ''
echo "USERS:"
for i in "''${!userKeys[@]}"; do
echo " $i"
done
echo ""
echo "SYSTEMS:"
for i in "''${!hostKeys[@]}"; do
echo " $i"
done
exit 0
'';
};
r = {
long = "recipient";
run = ''
recips+=" -r '$@'"
'';
};
h = {
long = "help";
run = ''
help
exit 0
'';
};
};
edit = "\${VISUAL:-\${EDITOR:-${pkgs.neovim}/bin/nvim}}";
applicableConfs = filterAttrs (_: v: v ? config.secrix) fInputs.nixosConfigurations;
allServices = flatten (mapAttrsToList (_: v: attrValues v.config.secrix.services) applicableConfs);
allSystemSecrets = flatten (mapAttrsToList (_: v: attrValues v.config.secrix.system.secrets) applicableConfs);
allSecrets = allSystemSecrets ++ flatten (map (x: attrValues x.secrets) allServices);
allUsers = flatten (map (x: x.encryptKeys) allSecrets);
allKeys = foldl' (a: x: a // mapAttrs (n: v: unique (v ++ (a.${n} or []))) x) {} (allUsers ++ (foldl' (a: x: a ++ [ x.config.secrix.defaultEncryptKeys ]) [] (attrValues applicableConfs)));
hostKeys = mapAttrs (_: v: " -r '${v.config.secrix.hostPubKey}'") (filterAttrs (_: v': v'.config.secrix.hostPubKey != null) applicableConfs);
ageBin = let
bins = unique (map (x: x.config.secrix.ageBin) (attrValues applicableConfs));
l = last bins;
in warnIf (length bins > 1) "More than one ageBin definition exists, using '${l}'." l;
in (writeShellScript "secrix" ''
function help {
${help}
}
function checkid {
if [[ -z "$identityFile" ]]; then
echo "No given identity file."
exit 1
elif ! [[ -e "$identityFile" ]]; then
echo "Identity file does not exist."
exit 1
fi
}
status=0
recips=""
identityFile=""
declare -A userKeys
declare -A hostKeys
${foldlAttrs (a: n: v: ''
${a}
userKeys['${n}']="${foldl' (a': x: "${a'} -r '${x}'") "" v}"
'') "" allKeys}
${foldlAttrs (a: n: v: ''
${a}
hostKeys['${n}']="${v}"
'') "" hostKeys}
${getopts opts}
if [[ ''${#positionalOpts[@]} == 0 ]]; then
help
exit 1
fi
case "''${positionalOpts[0]}" in
create)
tmpsec="$(${pkgs.coreutils}/bin/mktemp)"
if ! ${edit} "$tmpsec"; then
echo "Editor exited with non-zero status. Abandoning."
status=1
else
tmpfin="$(${pkgs.coreutils}/bin/mktemp)"
if ! eval ${ageBin} -e $recips "$tmpsec" > "$tmpfin"; then
echo "Encrypt failed with non-zero status. Abandoning."
status=1
else
${pkgs.coreutils}/bin/mv "$tmpfin" "''${positionalOpts[1]}"
fi
fi
;;
rekey)
checkid
tmpsec="$(${pkgs.coreutils}/bin/mktemp)"
if ! eval ${ageBin} -d -i "$identityFile" "''${positionalOpts[1]}" > "$tmpsec"; then
echo "Decrypt failed with non-zero status. Abandoning."
status=1
else
tmpfin="$(${pkgs.coreutils}/bin/mktemp)"
if ! eval ${ageBin} -e $recips "$tmpsec" > "$tmpfin"; then
echo "Reencrypt failed with non-zero status. Abandoning."
status=1
else
${pkgs.coreutils}/bin/mv "$tmpfin" "''${positionalOpts[1]}"
fi
fi
;;
edit)
checkid
tmpsec="$(${pkgs.coreutils}/bin/mktemp)"
if ! eval ${ageBin} -d -i "$identityFile" "''${positionalOpts[1]}" > "$tmpsec"; then
echo "Decrypt failed with non-zero status. Abandoning."
status=1
else
if ! ${edit} "$tmpsec"; then
echo "Editor exited with non-zero status. Abandoning."
status=1
else
tmpfin="$(${pkgs.coreutils}/bin/mktemp)"
if ! eval ${ageBin} -e $recips "$tmpsec" > "$tmpfin"; then
echo "Encrypt failed with non-zero status. Abandoning."
status=1
else
${pkgs.coreutils}/bin/mv "$tmpfin" "''${positionalOpts[1]}"
fi
fi
fi
;;
*)
echo "Unknown action '""''${positionalOpts[0]}.'"
;;
esac
${pkgs.coreutils}/bin/rm -f "$tmpfin"
${pkgs.coreutils}/bin/rm -f "$tmpsec"
exit $status
'').outPath;
};
nixosModules = {
secrix = import ./module.nix;
default = import ./module.nix;
};
checks.x86_64-linux = {
e2e-test = pkgs.nixosTest {
name = "secrix-e2e-test";
extraPythonPackages = p: [ p.termcolor ];
nodes = {
machine = { config, ... }: {
imports = [
self.nixosModules.secrix
];
services.openssh.enable = true;
system.activationScripts.replaceHostKey = ''
mkdir -p /etc/ssh
cp ${./keys/test-host-key} /etc/ssh/ssh_host_ed25519_key
chmod 400 !!$
cp ${./keys/test-host-key.pub} /etc/ssh/ssh_host_ed25519_key.pub
chmod 644 !!$
'';
secrix = {
hostPubKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKn43IR9yp8zhEWUhRmiA+rnd05t99ubTMJY7/ljd+yj chloe@freyja";
defaultEncryptKeys.user = [ "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIM64lwxKaEiwWLsV5Y/g0/4ZGPN+Ri2gz15mHVd716pu chloe@freyja" ];
system.secrets.secret1.encrypted.file = ./secrets/system-secret1;
services = {
test1.secrets.service-secret1.encrypted.file = ./secrets/service-secret1;
test3.secrets.service-secret3.encrypted.file = ./secrets/service-secret3;
test4.secrets.service-secret4.encrypted.file = ./secrets/service-secret4;
test5.secrets.service-secret5.encrypted.file = ./secrets/service-secret5;
test6.secrets.service-secret6 = {
encrypted.file = ./secrets/service-secret6;
decrypted.mode = "0271";
};
test7.secrets.service-secret7.encrypted.file = ./secrets/service-secret1;
test8.secrets.service-secret1.encrypted.file = ./secrets/service-secret1;
};
};
systemd.services = {
test1 = {
script = ''
set -x
cat ${config.secrix.services.test1.secrets.service-secret1.decrypted.path}
'';
serviceConfig = {
Type = "oneshot";
User = "test1";
RemainAfterExit = true;
};
};
test2 = {
script = ''
set -x
cat ${config.secrix.services.test1.secrets.service-secret1.decrypted.path}
'';
serviceConfig = {
Type = "oneshot";
User = "test2";
RemainAfterExit = true;
};
};
test3 = {
script = ''
set -x
cat ${config.secrix.services.test1.secrets.service-secret1.decrypted.path}
'';
serviceConfig = {
User = "test1";
Type = "oneshot";
};
};
test4 = {
script = ''
set -x
cat /run/test4-keys/missing-secret
'';
serviceConfig = {
User = "test4";
Type = "oneshot";
};
};
test5 = {
script = ''
set -x
cat ${config.secrix.services.test5.secrets.service-secret5.decrypted.path}
'';
};
test6 = {
script = ''
set -x
[[ $(stat -c '%a' ${config.secrix.services.test6.secrets.service-secret6.decrypted.path}) == $((10#${config.secrix.services.test6.secrets.service-secret6.decrypted.mode})) ]]
'';
serviceConfig.Type = "oneshot";
};
test7 = {
script = ''
set -x
cat ${config.secrix.services.test7.secrets.service-secret7.decrypted.path}
'';
serviceConfig.Type = "oneshot";
};
test8 = {
script = ''
set -x
cat ${config.secrix.services.test8.secrets.service-secret1.decrypted.path}
'';
serviceConfig.Type = "oneshot";
};
any.script = ''
set -x
cat ${config.secrix.system.secrets.secret1.decrypted.path}
'';
};
users = foldl' (a: x: recursiveUpdate a {
users.${x} = {
isSystemUser = true;
group = x;
};
groups.${x} = {};
}) {} [ "test1" "test2" "test4" ];
};
};
testScript = let
SKIP = r: xs: mapAttrs (_: _: ''
from termcolor import colored; print(colored("!!!!!!!!!!SKIPPED: ${r}!!!!!!!!!!", "red", attrs=["bold"]))
'') xs;
printTest = x: ''
print("==========${x}==========")
'';
cleanup = concatMapStrings (x: ''
machine.systemctl("stop test${toString x}.service")
'') (range 1 3);
in ''
start_all()
machine.wait_for_unit("multi-user.target")
machine.wait_for_unit("system-keys.service")
'' + concatMapStrings (x: ''
${foldlAttrs (a: n: v: ''
${a}
${printTest n}
${v}
${cleanup}
'') "" x}
'') [
{
"The system secret exists." = ''
machine.succeed("cat /run/system-keys/secret1")
'';
}
{
"If you define a system secret, it is available in any service." = ''
machine.succeed("systemctl start any.service")
machine.succeed("cat /run/system-keys/secret1")
'';
}
{
"If you define a service secret, it is available in its service." = ''
machine.succeed("systemctl start test1.service")
machine.systemctl("stop test1.service")
'';
}
{
"If you define a service secret, it is not available in another service." = ''
machine.succeed("systemctl start test1.service")
machine.fail("systemctl start test2.service")
'';
}
{
"If you define a service secret, it is not available if the service is not running." = ''
machine.succeed("systemctl start test1.service")
${cleanup}
machine.fail("systemctl start test3.service")
'';
}
{
"If a service fails to start, its keys should not exist." = ''
machine.fail("systemctl start test4.service")
machine.fail("cat /run/test4-keys/service-secret4")
'';
}
{
"A service secret should have the appropriate permissions set." = ''
machine.succeed("systemctl start test6.service")
'';
}
{
"A secret should be able to be reused without issue." = ''
machine.succeed("systemctl start test7.service")
'';
}
{
"Secrets under 2 different services should be able to have the same name." = ''
machine.succeed("systemctl start test8.service")
'';
}
];
};
};
};
}