-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecrets.nix
289 lines (231 loc) · 9.59 KB
/
secrets.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
{ pkgs, lib, config }:
with lib;
let
# This is not really configurable with systemd,
# but to reduce hardcoding in the fetchers, like
# this for now.
privateMount = "/tmp";
in
rec {
# The makeup of a fetcher is quite simple. The interface is just a list of secrets ids
# and the output are secrets files a la /tmp/{secret-id1, secret-id1} and /tmp/secrets.env
# for consumption from files or the environment.
# A current restriction of secret ids is that they must be valid filenames without spaces etc,
# mostly due to naive implementation in this poc.
# A fetcher is supposed to remain running. When the fetcher terminates, this will cause the
# a service restart, for instance because the secrets have changed.
# This means that in a case with disabled reloading, the fetcher is expected to sleep indefinitely.
fetchers = {
# Basic folder/file based fetcher, copying the secrets to private temp and supporting
# reloading watching inotify events of the files.
folder = backendConfig: pkgs.writeShellScript "file-copy-fetcher" ''
set -e
PRIVATE_MNT="${privateMount}"
SECRETS_STORE_PATH="${backendConfig.secretsDir}"
SECRETS_ENV_FILE="$PRIVATE_MNT/secrets.env"
function fetch_secrets() {
echo "Fetching secrets: $@"
for secret_id in $@; do
secret_file="$PRIVATE_MNT/$secret_id"
${pkgs.coreutils}/bin/cp -a "$SECRETS_STORE_PATH/$secret_id" "$secret_file"
echo "export $secret_id='$(<$secret_file)'" >> $SECRETS_ENV_FILE
for f in $secret_file $SECRETS_ENV_FILE; do
${pkgs.coreutils}/bin/chmod -R 444 "$f"
done
done
}
function watch_changes() {
watch_paths=""
for s in $@; do
watch_paths+=" $SECRETS_STORE_PATH/$s"
done
${pkgs.inotify-tools}/bin/inotifywait -e MODIFY $watch_paths
}
fetch_secrets $@
${pkgs.systemd}/bin/systemd-notify --ready
if [[ ${lib.boolToString backendConfig.reloadOnChange} == "true" ]]; then
watch_changes $@
else
echo "Sleeping..."
sleep infinity
fi
'';
# A fetcher for vault.
#
# Secrets Ids refer to individual KV entries in a particular path/secret.
#
# Right now only a single path is supported for the vault backend,
# which means that all secrets required for an individual service need to be entries
# within one vault secret.
#
# As such, for rotations, the metadata of the secret is watched, but not
# of individual entries within a secret. This also means that no secrets
# from multiple paths can be used in this manner right now.
#
# Changes are detected based on the secret version
vault = backendConfig: pkgs.writeShellScript "vault-fetcher" ''
PRIVATE_MNT="${privateMount}"
SECRETS_PATH="${backendConfig.path}"
VAULT_MOUNT="${backendConfig.mount}"
VAULT_SERVER="${backendConfig.url}"
POLL_INTERVAL="${toString backendConfig.refreshInterval}"
fetched_secrets_version=""
function get_version() {
payload=$(${pkgs.curl}/bin/curl -sf \
-H "X-Vault-Token: $(<${backendConfig.tokenPath})" \
$VAULT_SERVER/v1/$VAULT_MOUNT/data/$SECRETS_PATH)
${pkgs.python}/bin/python -c \
"import sys, json; print(json.load(sys.stdin)['data']['metadata']['version'])" <<<$payload
}
function watch_changes() {
echo "Started watching secrets"
while true; do
echo "polling secrets version..."
current_version=$(get_version)
echo "current version: $current_version"
if [[ "$current_version" != "$fetched_secrets_version" ]]; then
echo "Secrets version changed, exiting"
exit
fi
sleep $POLL_INTERVAL
done
}
function fetch_secrets() {
echo "Fetching secrets: $@"
local secrets_version;
local secret;
local secret_id;
local payload;
SECRETS_ENV_FILE="$PRIVATE_MNT/secrets.env"
for secret_id in $@; do
echo "Fetching secret: $secret_id"
secret_file=$PRIVATE_MNT/$secret_id
echo "DEBUG: ${pkgs.curl}/bin/curl -sf \
--header "X-Vault-Token: <redacted> \
$VAULT_SERVER/v1/$VAULT_MOUNT/data/$SECRETS_PATH"
payload=$(${pkgs.curl}/bin/curl -sf \
--header "X-Vault-Token: $(<${backendConfig.tokenPath})" \
$VAULT_SERVER/v1/$VAULT_MOUNT/data/$SECRETS_PATH)
if [[ $? -ne 0 ]]; then
echo "Failed retrieving secret from vault."
exit 1
fi;
# NOTE: For some reason Im having trouble with JQ core-dumping within the service,
# so falling back to python for now.
#secret=$(${pkgs.jq}/bin/jq -ra .data.data.$secret_id <<<$payload)
secret=$(${pkgs.python}/bin/python -c \
"import sys, json; print(json.load(sys.stdin)['data']['data']['$secret_id'])" <<<$payload)
echo "$secret" > $secret_file
echo "export $secret_id='$secret'" >> $SECRETS_ENV_FILE
for f in $secret_file $SECRET_ENV_FILE; do
${pkgs.coreutils}/bin/chmod -R 444 "$f"
done
done
# This is somewhat naive, but will do for a POC.
# In a proper implementation, the version would be extracted from
# the actual secrets retrieved and not after the fact, as this could
# miss an update that would happen between the last key and the version
# check. Also, tracking only a single version wouldn't work when using secrets
# from different paths.
fetched_secrets_version=$(get_version)
}
function run() {
fetch_secrets $@
${pkgs.systemd}/bin/systemd-notify --ready
if [[ ${lib.boolToString backendConfig.reloadOnChange} == "true" ]]; then
watch_changes $@
else
echo "Sleeping..."
sleep infinity
fi
}
run $@
'';
};
# Attached a sidecar for secrets injection to a systemd service,
# configures lifecycle like reloading and ensures some necessary
# settings like PrivateTmp.
wrapWithSidecart = fetcher: secretIds: serviceName: serviceDef: let
sidecartUnitName = "${serviceName}-secrets.service";
serviceUnitName = "${serviceName}.service";
in
assert serviceDef.serviceConfig.PrivateTmp or true;
assert serviceDef.unitConfig.JoinsNamespaceOf or null == null;
{
# Definition of the side-cart containers responsible for retrieving secrets
# and populating the private tmp
"${serviceName}-secrets" = {
description = "side-cart for ${serviceName}";
before = [ serviceUnitName ];
requiredBy = [ serviceUnitName ];
# Ensures the sidecar is stopped when the consuming service
# terminates.
bindsTo = [ serviceUnitName ];
partOf = [ serviceUnitName ];
serviceConfig = {
Type = "notify";
ExecStart = "${fetcher} ${lib.concatStringsSep " " secretIds}";
PrivateTmp = true;
Restart = "on-success";
};
};
# Hooks up the original service definition with the sidecar secrets
# service and enforces some settings, like private /tmp.
"${serviceName}" = lib.recursiveUpdate serviceDef {
partOf = serviceDef.partOf or [] ++ [ sidecartUnitName ];
requires = serviceDef.requires or [] ++ [ sidecartUnitName ];
# TODO: Maybe injecting into env should be optional
serviceConfig.ExecStart = wrapExec serviceDef.serviceConfig.ExecStart;
serviceConfig.PrivateTmp = true;
unitConfig.JoinsNamespaceOf = sidecartUnitName;
};
};
# creates an accompanying sidecar service loading specified secrets for
# each provided serviceDef
withSecrets = secretIds: fetcher: serviceDefs:
fold (a: b: a // b) { }
(map (k: (wrapWithSidecart fetcher secretIds k serviceDefs."${k}"))
(lib.attrNames serviceDefs));
genSecretsAttrs = secretIds:
lib.fold (a: b: a // b) { }
(map (s: { "${s}" = "${privateMount}/${s}"; }) secretIds);
# Unfortunately EnvironmentFile is not really usable for our purpose, because
# it's not part of PrivateTmp (neither is PreExec), so this is a simple wrapper to
# source a generated secrets.env file for an arbitrary command
wrapExec = cmd: pkgs.writeShellScript "secrets-env-wrapper" ''
echo "Injecting secrets into environment for ${cmd}"
source ${privateMount}/secrets.env
${cmd}
'';
serviceSecretsScope = { backendConfig, loadSecrets }:
let
fetcher = if backendConfig.backend == "folder"
then
fetchers.folder (config.secretsStore.folder or {} // backendConfig.config)
else if backendConfig.backend == "vault" then
fetchers.vault (config.secretsStore.vault or {} // backendConfig.config)
else
throw "Unsupported store type: ${type}";
in serviceDefs:
withSecrets loadSecrets fetcher
# This injects "resolved" secret paths into the service definition
(serviceDefs (genSecretsAttrs loadSecrets));
# TODO: Possibly allow for configuring a mapping of secret-ids used in the module to secret "paths" in the
# secrets store instead of hard convention.
secretsBackendOption = mkOption {
description = "Configuration for secrets backend";
type = types.submodule {
options = {
backend = mkOption {
type = types.str;
description = "Name of the backend to use";
};
# TODO: Add some assertions for the different types
config = mkOption {
type = types.attrs;
description = "backend settings";
};
};
};
};
}