-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmodule.nix
479 lines (471 loc) · 19.1 KB
/
module.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
{ pkgs, config, ... }:
let
inherit (builtins) isFunction readFile;
inherit (pkgs) writeText copyPathToStore writeShellScript;
inherit (pkgs.lib.lists) foldl';
inherit (pkgs.lib.strings) concatStringsSep optionalString;
inherit (pkgs.lib.trivial) id;
inherit (pkgs.lib.modules) mkForce;
inherit (pkgs.lib.attrsets) mapAttrsToList attrValues concatMapAttrs attrNames;
foldlAttrs = pkgs.lib.attrsets.foldlAttrs or (f: i: s: foldl' (a: n: f a n s.${n}) i (attrNames s));
d = x: builtins.trace x x;
sysConfig = config;
cfg = config.secrix;
in
{
options = with pkgs.lib; {
secrix = {
ageBin = mkOption {
type = types.str;
default = "${pkgs.age}/bin/age";
defaultText = literalExpression ''
"''${pkgs.age}/bin/age"
'';
description = ''
The age bin to use for encryption and decryption.
'';
};
system = {
secretsDir = {
name = mkOption {
type = types.str;
default = "system-keys";
description = ''
The name of the directory for system secrets. It will end up as `/run/<name>`.
'';
};
permissions = mkOption {
type = types.str;
default = "111";
description = ''
Permissions for the directory containing system secrets.
'';
};
user = mkOption {
type = types.str;
default = "0";
description = ''
The user that owns the directory for system secrets.
'';
};
group = mkOption {
type = types.str;
default = "0";
description = ''
The group that owns the directory for system secrets.
'';
};
};
secretsServiceName = mkOption {
type = types.str;
default = "system-keys";
description = ''
The name of the service that manages system secrets.
'';
};
secrets = mkOption {
type = types.attrsOf (types.submodule ({ name, config, ... }: {
options = {
name = mkOption {
type = types.str;
default = name;
description = ''
The name of the secret. This defaults to the attribute name. This is simply a
referential token to the secret, however if no name is set for the decrypted file,
this will be used.
'';
};
encryptKeys = mkOption {
type = types.attrsOf (types.listOf types.str);
default = cfg.defaultEncryptKeys;
defaultText = literalExpression ''
config.secrix.defaultEncryptKeys
'';
description = ''
Public keys with which to encrypt the secret.
'';
};
encrypted.file = mkOption {
type = types.path;
description = ''
Local location of the secret.
'';
};
decrypted = {
name = mkOption {
type = types.str;
default = config.name;
defaultText = literalExpression ''
config.secrix.system.secrets.<name>.name
'';
description = ''
The name of the decrypted file on disk. This defaults to the secret name.
'';
};
mode = mkOption {
type = types.str;
default = "0400";
description = ''
Permissions of the secret when decrypted.
'';
};
user = mkOption {
type = types.str;
default = "0";
description = ''
Secret user.
'';
};
group = mkOption {
type = types.str;
default = "0";
description = ''
Secret group.
'';
};
path = mkOption {
type = types.str;
default = "/run/${cfg.system.secretsDir.name}/${config.name}";
defaultText = literalExpression ''
"/run/''${config.secrix.system.secretsDir.name}/''${config.secrix.system.secrets.<name>.name}"
'';
readOnly = true;
description = ''
The path to the secret when decrypted on disk. This is automatically set by
secrix and is available only for reference.
'';
};
builder = mkOption {
type = types.nullOr (types.either types.lines (types.functionTo types.lines));
default = null;
description = ''
A builder script (if needed) to perform additional actions on the secret before
it ends up in its final location.
If this is a function that yields a string, it will be passed a single argument
which is the final location of the built file.
If this is a string, a special bash variable $inFile can be used to reference
the secret as it is, however there will be no reference available to its final
destination as that will be up to the builder. Use a string only if you know
what you're doing.
'';
};
};
};
}));
default = {};
description = ''
An attribute set of secrets that will be decrypted on the system. System secrets will
be decrypted at system start and will exist decrypted for the uptime of the system but
will not remain on disk if the system is shut down sanely.
'';
};
};
hostIdentityFile = mkOption {
type = types.str;
default = "/etc/ssh/ssh_host_ed25519_key";
description = ''
The private key of the host that will be decrypting the secret.
'';
};
defaultEncryptKeys = mkOption {
type = types.attrsOf (types.listOf types.str);
default = {};
description = ''
The default encryption keys for all secrets. This will be overriden for any given secret
if `encryptKeys` is specified for that secret.
'';
};
hostPubKey = mkOption {
type = types.nullOr types.str;
default = null;
description = ''
The public key for the host that the secrets will exist on.
'';
};
services = mkOption {
type = types.attrsOf (types.submodule (outer@{ name, config, ... }: {
options = {
secretsDirName = mkOption {
type = types.str;
default = "${name}-keys";
description = ''
The directory name for the service secrets.
'';
};
systemdService = mkOption {
type = types.str;
default = name;
description = ''
The name fo the systemd service that the secrets contained within will be bound
to. This defaults to the attribute name.
'';
};
additionalRuntimeDirNames = mkOption {
type = types.listOf types.str;
default = [ ];
description = ''
In the case that the systemd service these secrets will be bound to has a
RuntimeDirectory specified as a string, secrix will be unable to add an additional
RuntimeDirectory, so add that here.
'';
};
forceRuntimeDirs = mkOption {
type = types.bool;
default = false;
description = ''
Set this to true of the service these secrets will be bound to has a
RuntimeDirectory already specified as a string.
'';
};
secretsServiceName = mkOption {
type = types.str;
default = "${name}-keys";
description = ''
The name of the service that will decrypt the keys.
'';
};
secrets = mkOption {
type = types.attrsOf (types.submodule ({ name, config, ... }: {
options = {
name = mkOption {
type = types.str;
default = name;
description = ''
The name of the secret. This defaults to the attribute name. This is simply
a referential token to the secret, however if no name is set for the
decrypted file, this will be used.
'';
};
encryptKeys = mkOption {
type = types.attrsOf (types.listOf types.str);
default = cfg.defaultEncryptKeys;
defaultText = literalExpression ''
config.secrix.defaultEncryptKeys
'';
description = ''
Public keys with which to encrypt the secret.
'';
};
encrypted.file = mkOption {
type = types.path;
description = ''
Local location of the secret.
'';
};
decrypted = {
name = mkOption {
type = types.str;
default = config.name;
defaultText = literalExpression ''
config.secrix.services.<name>.secrets.<name>.name
'';
description = ''
The name of the decrypted file on disk. This defaults to the secret name.
'';
};
mode = mkOption {
type = types.str;
default = "0400";
description = ''
Permissions of the secret when decrypted.
'';
};
user = mkOption {
type = types.nullOr types.str;
default = let
servUser = hasAttrByPath [ "serviceConfig" "User" ] sysConfig.systemd.services.${outer.config.systemdService};
servDynUser = attrByPath [ "serviceConfig" "DynamicUser" ] false sysConfig.systemd.services.${outer.config.systemdService};
user = attrByPath [ "serviceConfig" "User" ] "0" sysConfig.systemd.services.${outer.config.systemdService};
userSearch = if user == "0" then "root" else user;
sysUser = length (filter (x: x.name == userSearch) (attrValues (sysConfig.users.users or {}))) > 0;
in if (servUser && sysUser) then
user
else if servDynUser then
null
else "0";
defaultText = literalExpression ''
with config.systemd.services.<name>.serviceConfig; User or (if DynamicUser then null else "root")
'';
# in "0";
description = ''
Secret user.
'';
};
group = mkOption {
type = types.str;
default = sysConfig.users.users.${config.decrypted.user}.group or "0";
defaultText = literalExpression ''
config.users.users.''${config.secrix.services.<name>.decrypted.group}.group
'';
description = ''
Secret group.
'';
};
path = mkOption {
type = types.str;
default = "/run/${outer.config.secretsDirName}/${config.name}";
defaultText = literalExpression ''
let cfg = config.secrix.services.<name>; in
"/run/''${cfg.secretsDirName}/''${cfg.name}"
'';
readOnly = true;
description = ''
The path to the secret when decrypted on disk. This is automatically set
by secrix and is available only for reference.
'';
};
builder = mkOption {
type = types.nullOr (types.either types.lines (types.functionTo types.lines));
default = null;
description = ''
A builder script (if needed) to perform additional actions on the secret
before it ends up in its final location.
If this is a function that yields a string, it will be passed a single
argument which is the final location of the built file.
If this is a string, a special bash variable $inFile can be used to
reference the secret as it is, however there will be no reference
available to its final destination as that will be up to the builder. Use
a string only if you know what you're doing.
'';
};
};
};
}));
description = ''
An attribute set of secrets that will be decrypted on the system. Service secrets
will be decrypted at the start of and will exist for the lifetime of the service
they are bound to.
'';
};
};
}));
default = { };
description = ''
An attribute set of systemd service names to which to bind secrets. All secrets bound to
a service will exist only for the lifetime of the service.
'';
};
};
};
config =
let
c = s: "${pkgs.coreutils}/bin/${s}";
runKeyDir = "/run/${cfg.system.secretsDir.name}";
allSecrets = (foldlAttrs (a: _: v: a // foldlAttrs (a': _: v': a' // { ${v'.decrypted.name} = copyPathToStore v'.encrypted.file; }) { } v.secrets) { } cfg.services) //
foldlAttrs (a: _: v: a // { ${v.decrypted.name} = copyPathToStore v.encrypted.file; }) { } cfg.system.secrets;
systemKeysServices = concatMapAttrs (n: v: let
runKeyPath = "${runKeyDir}/${v.decrypted.name}";
in { "secrix-system-secret-${n}" = {
wantedBy = [ "secrix-system-secrets.service" ];
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
ExecStop = writeShellScript "secrix-rm-${n}" ''
${c "rm"} -f ${runKeyPath}
'';
};
script = let
decrypt = p: ''
${cfg.ageBin} -d -i "${cfg.hostIdentityFile}" "${allSecrets.${v.decrypted.name}}" > "${p}"
'';
mkBuilder = s: ''
inFile="$(${c "mktemp"})"
${decrypt "$inFile"}
${s}
${c "rm"} $inFile
'';
chPerms = ''
${c "chown"} ${v.decrypted.user}:${v.decrypted.group} "${runKeyPath}"
${c "chmod"} ${v.decrypted.mode} "${runKeyPath}"
'';
scr = if v.decrypted.builder == null then
"${decrypt runKeyPath}"
else if isFunction v.decrypted.builder then
mkBuilder "${v.decrypted.builder runKeyPath}"
else
mkBuilder "${v.decrypted.builder}";
in ''
${c "mkdir"} -p ${runKeyDir}
${scr}
${chPerms}
'';
}; }) cfg.system.secrets;
systemKeysMainService = {
secrix-system-secrets = {
script = ''
${c "mkdir"} -p ${runKeyDir}
'';
wantedBy = [ "multi-user.target" ];
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
RuntimeDirectory = cfg.system.secretsDir.name;
RuntimeDirectoryMode = cfg.system.secretsDir.permissions;
User = cfg.system.secretsDir.user;
Group = cfg.system.secretsDir.group;
PropagatesStopTo = map (x: "secrix-system-secret-${x}.service") (attrNames cfg.system.secrets);
};
};
};
in
{
# x - systemd service
systemd.services = systemKeysServices // systemKeysMainService // foldl'
(a: x: a // {
${x.secretsServiceName} = {
before = [ "${x.systemdService}.service" ];
bindsTo = [ "${x.systemdService}.service" ];
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
};
script =
let
runKeyDir = "/run/${x.secretsDirName}";
# v - secret
cpKeys = mapAttrsToList
(_: v:
let
runKeyPath = "${runKeyDir}/${v.decrypted.name}";
decrypt = p: ''
${cfg.ageBin} -d -i "${cfg.hostIdentityFile}" "${allSecrets.${v.decrypted.name}}" > "${p}"
'';
mkBuilder = s: ''
inFile="$(${c "mktemp"})"
${decrypt "$inFile"}
${s}
${c "rm"} $inFile
'';
chPerms = optionalString (v.decrypted.user != null) ''
${c "chown"} ${v.decrypted.user}:${v.decrypted.group} "${runKeyPath}"
${c "chmod"} ${v.decrypted.mode} "${runKeyPath}"
'';
scr =
if v.decrypted.builder == null then
"${decrypt runKeyPath}"
else if isFunction v.decrypted.builder then
mkBuilder "${v.decrypted.builder runKeyPath}"
else
mkBuilder "${v.decrypted.builder}";
in
''
${c "mkdir"} -p "${runKeyDir}"
${scr}
${chPerms}
'')
x.secrets;
in
''
${concatStringsSep "\n" cpKeys}
'';
};
${x.systemdService} = {
after = [ "${x.secretsServiceName}.service" ];
bindsTo = [ "${x.secretsServiceName}.service" ];
serviceConfig.RuntimeDirectory = (if x.forceRuntimeDirs then mkForce else id) ([ x.secretsServiceName ] ++ x.additionalRuntimeDirNames);
};
})
{ }
(attrValues cfg.services);
};
}