-
-
Notifications
You must be signed in to change notification settings - Fork 363
/
Copy pathkeys.nix
267 lines (229 loc) · 8.51 KB
/
keys.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
{ config, pkgs, lib, ... }:
with lib;
let
keyOptionsType = types.submodule ({ config, name, ... }: {
options.name = mkOption {
example = "secret.txt";
default = name;
type = types.str;
description = ''
The name of the key file.
'';
};
options.text = mkOption {
example = "super secret stuff";
default = null;
type = types.nullOr types.str;
description = ''
When non-null, this designates the text that the key should contain. So if
the key name is ``password`` and
``foobar`` is set here, the contents of the file
``destDir``/``password``
will be ``foobar``.
NOTE: Either ``text``, ``keyCommand`` or
``keyFile`` have to be set.
'';
};
options.keyCommand = mkOption {
default = null;
example = [ "pass" "show" "secrettoken" ];
type = types.nullOr (types.listOf types.str);
description = ''
When non-null, output of this command run on local machine will be
deployed to the specified key on the target machine. If the key name
is
``password`` and ``echo secrettoken``
is set here, the contents of the file
``destDir``/``password``
deployed will equal the output of the command ``echo secrettoken``.
This option is especially useful when you don't want to store the secrets
inside of your NixOps deployment but rather in a well-guarded place such as an
encrypted file. Consider using nixpkgs.password-store as storage for
such sensitive secrets.
NOTE: Either ``text``, ``keyCommand`` or
``keyFile`` have to be set.
'';
};
options.keyFile = mkOption {
default = null;
type = types.nullOr types.path;
apply = value: if value == null then null else toString value;
description = ''
When non-null, contents of the specified file will be deployed to the
specified key on the target machine. If the key name is
``password`` and ``/foo/bar`` is set
here, the contents of the file
``destDir``/``password``
deployed will be the same as local file ``/foo/bar``.
Since no serialization/deserialization of key contents is involved, there
are no limits on that content: null bytes, invalid Unicode,
``/dev/random`` output -- anything goes.
NOTE: Either ``text``, ``keyCommand`` or
``keyFile`` have to be set.
'';
};
options.destDir = mkOption {
default = "/run/keys";
type = types.path;
description = ''
When specified, this allows changing the destDir directory of the key
file from its default value of ``/run/keys``.
This directory will be created, its permissions changed to
``0750`` and ownership to ``root:keys``.
'';
};
options.path = mkOption {
type = types.path;
default = "${config.destDir}/${config.name}";
internal = true;
description = ''
Path to the destination of the file, a shortcut to
``destDir`` + / + ``name``
Example: For key named ``foo``,
this option would have the value ``/run/keys/foo``.
'';
};
options.user = mkOption {
default = "root";
type = types.str;
description = ''
The user which will be the owner of the key file.
'';
};
options.group = mkOption {
default = "root";
type = types.str;
description = ''
The group that will be set for the key file.
'';
};
options.permissions = mkOption {
default = "0600";
example = "0640";
type = types.str;
description = ''
The default permissions to set for the key file, needs to be in the
format accepted by ``chmod(1)``.
'';
};
});
convertOldKeyType = key: val: let
warning = "Using plain strings for `deployment.keys' is"
+ " deprecated, please use `deployment.keys.${key}.text ="
+ " \"<value>\"` instead of `deployment.keys.${key} ="
+ " \"<value>\"`.";
in if isString val then builtins.trace warning { text = val; } else val;
keyType = mkOptionType {
name = "string or key options";
check = v: isString v || keyOptionsType.check v;
merge = loc: defs: let
convert = def: def // {
value = convertOldKeyType (last loc) def.value;
};
in keyOptionsType.merge loc (map convert defs);
inherit (keyOptionsType) getSubOptions;
};
in
{
###### interface
options = {
deployment.keys = mkOption {
default = {};
example = { password.text = "foobar"; };
type = types.attrsOf keyType;
apply = mapAttrs convertOldKeyType;
description = ''
The set of keys to be deployed to the machine. Each attribute maps
a key name to a file that can be accessed as
``destDir``/``name``,
where ``destDir`` defaults to
``/run/keys``. Thus, ``{ password.text = "foobar"; }`` causes a file
``destDir``/password`` to be
created with contents ``foobar``. The directory ``destDir`` is only
accessible to root and the ``keys`` group, so keep in mind
to add any users that need to have access to a particular key to this
group.
Each key also gets a systemd service ``<name>-key.service``
which is active while the key is present and inactive while the key
is absent. Thus, ``{ password.text = "foobar"; }`` gets
a ``password-key.service``.
'';
};
};
###### implementation
config = {
assertions = (flip mapAttrsToList config.deployment.keys (key: opts: {
assertion = (opts.text == null && opts.keyFile != null && opts.keyCommand == null) ||
(opts.text != null && opts.keyFile == null && opts.keyCommand == null) ||
(opts.text == null && opts.keyFile == null && opts.keyCommand != null);
message = "Deployment key '${key}' must have either a 'text', 'keyCommand' or a 'keyFile' specified.";
})) ++ (flip mapAttrsToList config.deployment.keys (key: opts: let
dups = lib.attrNames (lib.filterAttrs (n: v: n != key && v.path == opts.path) config.deployment.keys);
in {
assertion = dups == [];
message = "Deployment key '${key}' has non-unique paths, duplicated in: ${lib.concatStringsSep ", " dups}.";
}));
system.activationScripts.nixops-keys =
let
script = ''
mkdir -p /run/keys -m 0750
chown root:keys /run/keys
${concatStringsSep "\n" (flip mapAttrsToList config.deployment.keys (name: value:
# Make sure each key has correct ownership, since the configured owning
# user or group may not have existed when first uploaded.
''
[[ -f "${value.path}" ]] && chown '${value.user}:${value.group}' "${value.path}"
''
))}
'';
in stringAfter [ "users" "groups" ] "source ${pkgs.writeText "setup-keys.sh" script}";
systemd.services = (
{ nixops-keys =
{ enable = any (key: hasPrefix "/run/" key.destDir) (
attrValues config.deployment.keys
);
description = "Waiting for NixOps Keys";
wantedBy = [ "keys.target" ];
before = [ "keys.target" ];
unitConfig.DefaultDependencies = false; # needed to prevent a cycle
serviceConfig.Type = "oneshot";
serviceConfig.RemainAfterExit = true;
script =
''
while ! [ -e /run/keys/done ]; do
sleep 0.1
done
'';
};
}
//
(flip mapAttrs' config.deployment.keys (name: keyCfg:
nameValuePair "${name}-key" {
enable = true;
serviceConfig.TimeoutStartSec = "infinity";
serviceConfig.Restart = "always";
serviceConfig.RestartSec = "100ms";
path = [ pkgs.inotify-tools ];
preStart = ''
(while read f; do if [ "$f" = "${keyCfg.name}" ]; then break; fi; done \
< <(inotifywait -qm --format '%f' -e create,move ${keyCfg.destDir}) ) &
if [[ -e "${keyCfg.path}" ]]; then
echo 'flapped down'
kill %1
exit 0
fi
wait %1
'';
script = ''
inotifywait -qq -e delete_self "${keyCfg.path}" &
if [[ ! -e "${keyCfg.path}" ]]; then
echo 'flapped up'
exit 0
fi
wait %1
'';
}
))
);
};
}