Repository navigation
Expand file tree
/
Copy pathk
More file actions
executable file
·471 lines (429 loc) · 12.5 KB
/
Copy pathk
File metadata and controls
executable file
·471 lines (429 loc) · 12.5 KB
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
#!/usr/bin/env bash
# Commands consume shared globals and are resolved dynamically.
# shellcheck disable=SC1090,SC2034
set -euo pipefail
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
ROOT_DIR="$(cd -- "$SCRIPT_DIR/.." && pwd)"
COMMAND_DIR="$SCRIPT_DIR/k.commands"
PATCH_DIR="$ROOT_DIR/patches"
SECRETS_DIR="$ROOT_DIR/secrets"
STATE_FILE="$ROOT_DIR/state.yaml"
RECIPIENTS_FILE="$SECRETS_DIR/state.yaml"
SOPS_CONFIG_FILE="$ROOT_DIR/.sops.yaml"
TALOS_SECRETS_FILE="$SECRETS_DIR/talos.yaml"
BOOTSTRAP_SECRETS_FILE="$SECRETS_DIR/bootstrap.yaml"
VAULT_SECRETS_FILE="$SECRETS_DIR/vault.yaml"
VAULT_RECOVERY_FILE="$SECRETS_DIR/vault-recovery.yaml"
export TALOSCONFIG="$ROOT_DIR/talosconfig"
export KUBECONFIG="$ROOT_DIR/kubeconfig"
cd "$ROOT_DIR"
state_value() {
local value
if ! value="$(yq -r "$1" "$STATE_FILE" 2>/dev/null)" || [[ -z $value || $value == null ]]; then
echo "Missing or invalid state value: $1" >&2
return 1
fi
printf '%s\n' "$value"
}
STATE_LOADED=0
CLUSTER_NAME=""
TALOS_SCHEMATIC=""
TALOS_VERSION=""
KUBERNETES_VERSION=""
GATEWAY_API_VERSION=""
CILIUM_VERSION=""
ARGOCD_VERSION=""
MAIN_NODE=""
MAIN_IP=""
CLUSTER_ENDPOINT=""
TALOS_INSTALL_IMAGE=""
NODES=()
load_cluster_state() {
((STATE_LOADED == 0)) || return 0
local nodes_state
CLUSTER_NAME="$(state_value '.name')"
TALOS_SCHEMATIC="$(state_value '.talos.schematic')"
TALOS_VERSION="$(state_value '.talos.version')"
[[ $TALOS_VERSION =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]] || {
echo "Invalid Talos version in state.yaml: $TALOS_VERSION" >&2
return 1
}
TALOS_VERSION="v$TALOS_VERSION"
KUBERNETES_VERSION="$(state_value '.kubernetes.version')"
GATEWAY_API_VERSION="$(state_value '."gateway-api".version')"
CILIUM_VERSION="$(state_value '.cilium.version')"
ARGOCD_VERSION="$(state_value '.argocd.version')"
MAIN_NODE="$(state_value '.endpoint')"
MAIN_IP="$(MAIN_NODE="$MAIN_NODE" state_value '.nodes[strenv(MAIN_NODE)].address')"
CLUSTER_ENDPOINT="https://$MAIN_IP:6443"
TALOS_INSTALL_IMAGE="factory.talos.dev/installer/$TALOS_SCHEMATIC:$TALOS_VERSION"
nodes_state="$(state_value '.nodes | to_entries[] | "\(.key):\(.value.address)"')"
mapfile -t NODES <<<"$nodes_state"
STATE_LOADED=1
}
require_file() {
if [[ ! -f $1 ]]; then
echo "Missing required file: ${1#"$ROOT_DIR/"}" >&2
return 1
fi
}
require_no_args() {
local usage="$1"
shift
if (($#)); then
echo "Usage: $usage" >&2
return 2
fi
}
require_optional_yes() {
local usage="$1"
shift
if (($# > 1)) || (($# == 1)) && [[ $1 != --yes ]]; then
echo "Usage: $usage" >&2
return 2
fi
}
confirm_action() {
local usage="$1" prompt="$2" answer
shift 2
require_optional_yes "$usage" "$@"
[[ ${1:-} != --yes ]] || return 0
printf '%s [y/N] ' "$prompt"
if ! read -r answer || [[ ! $answer =~ ^[Yy]([Ee][Ss])?$ ]]; then
echo "Cancelled"
return 1
fi
}
resolve_node() {
local requested="$1" node
for node in "${NODES[@]}"; do
if [[ ${node%%:*} == "$requested" ]]; then
printf '%s\n' "${node#*:}"
return
fi
done
echo "Unknown node: $requested" >&2
return 2
}
age_key_file() {
if [[ -n ${SOPS_AGE_KEY_FILE:-} ]]; then
printf '%s\n' "$SOPS_AGE_KEY_FILE"
else
printf '%s/sops/age/keys.txt\n' "${XDG_CONFIG_HOME:-$HOME/.config}"
fi
}
local_age_recipient() {
local key_file
key_file="$(age_key_file)"
require_file "$key_file"
age-keygen -y "$key_file"
}
secret_files() {
local file
for file in "$SECRETS_DIR"/*.yaml; do
[[ -f $file && $file != "$RECIPIENTS_FILE" ]] && printf '%s\n' "$file"
done
}
secret_file() {
local name="$1" file
if [[ $name == *[!a-z0-9._-]* || $name == .* || -z $name || $name == state ]]; then
echo "Invalid secret name: $name" >&2
return 2
fi
file="$SECRETS_DIR/$name.yaml"
require_file "$file"
printf '%s\n' "$file"
}
write_sops_config() {
local output="$1" file name pattern recipient
local -a names=()
require_file "$RECIPIENTS_FILE"
while IFS= read -r file; do
name="$(basename "$file" .yaml)"
[[ $name =~ ^[a-z0-9][a-z0-9._-]*$ ]] || {
echo "Invalid secret filename: $file" >&2
return 1
}
names+=("${name//./\\.}")
done < <(secret_files)
((${#names[@]})) || {
echo "No encrypted secret files found" >&2
return 1
}
pattern="$(
IFS='|'
echo "${names[*]}"
)"
{
echo "# Generated by k secrets; edit recipients with k secrets recipients."
echo "creation_rules:"
printf ' - path_regex: ^secrets/(%s)\\.yaml$\n' "$pattern"
echo " age:"
while IFS= read -r recipient; do
[[ -n $recipient ]] && printf ' - %s\n' "$recipient"
done < <(yq -r '.recipients | to_entries | sort_by(.key) | .[].value' "$RECIPIENTS_FILE")
} >"$output"
grep -q '^ - age1' "$output" || {
rm -f "$output"
echo "No recipients configured" >&2
return 1
}
}
sync_sops_config() {
local generated
generated="$(mktemp "$ROOT_DIR/.sops.XXXXXX")"
if ! write_sops_config "$generated"; then
rm -f "$generated"
return 1
fi
chmod 644 "$generated"
if cmp -s "$generated" "$SOPS_CONFIG_FILE"; then
rm -f "$generated"
else
mv "$generated" "$SOPS_CONFIG_FILE"
fi
}
rekey_secrets() {
local backup file success=1
backup="$(mktemp -d)"
mkdir "$backup/secrets"
cp "$RECIPIENTS_FILE" "$backup/state.yaml"
cp "$SOPS_CONFIG_FILE" "$backup/.sops.yaml"
while IFS= read -r file; do
cp "$file" "$backup/secrets/"
done < <(secret_files)
if "$@" && sync_sops_config; then
while IFS= read -r file; do
if ! sops updatekeys --yes "$file"; then
success=0
break
fi
done < <(secret_files)
if ((success)); then
rm -rf "$backup"
return
fi
fi
cp "$backup/state.yaml" "$RECIPIENTS_FILE"
cp "$backup/.sops.yaml" "$SOPS_CONFIG_FILE"
cp "$backup/secrets/"*.yaml "$SECRETS_DIR/"
rm -rf "$backup"
echo "Recipient update failed; restored previous files" >&2
return 1
}
decrypt_talos_secrets() {
local output
require_file "$TALOS_SECRETS_FILE"
output="$(mktemp)"
if ! sops decrypt --output "$output" "$TALOS_SECRETS_FILE"; then
rm -f "$output"
return 1
fi
printf '%s\n' "$output"
}
read_bootstrap_secret() {
local name="$1"
case "$name" in
ARGOCD_GITHUB_OAUTH_CLIENT_SECRET | ARGOCD_GITHUB_WEBHOOK_SECRET | CLOUDFLARE_API_TOKEN | GRAFANA_ADMIN_PASSWORD | GRAFANA_OIDC_CLIENT_SECRET | VAULT_OIDC_CLIENT_SECRET | ZEROSSL_EAB_HMAC_KEY) ;;
*)
echo "Unknown bootstrap secret: $name" >&2
return 2
;;
esac
require_file "$BOOTSTRAP_SECRETS_FILE"
sops decrypt --extract "[\"$name\"]" "$BOOTSTRAP_SECRETS_FILE"
}
require_bootstrap_secrets() {
local name value
for name in ARGOCD_GITHUB_OAUTH_CLIENT_SECRET ARGOCD_GITHUB_WEBHOOK_SECRET CLOUDFLARE_API_TOKEN GRAFANA_ADMIN_PASSWORD GRAFANA_OIDC_CLIENT_SECRET VAULT_OIDC_CLIENT_SECRET ZEROSSL_EAB_HMAC_KEY; do
value="$(read_bootstrap_secret "$name")"
[[ -n $value && $value != REPLACE_ME ]] || {
echo "Set $name with: k secrets edit bootstrap" >&2
return 1
}
done
}
materialize_grafana_secrets() {
local grafana_admin_password grafana_oidc_client_secret name value
grafana_admin_password="$(read_bootstrap_secret GRAFANA_ADMIN_PASSWORD)"
grafana_oidc_client_secret="$(read_bootstrap_secret GRAFANA_OIDC_CLIENT_SECRET)"
for name in GRAFANA_ADMIN_PASSWORD GRAFANA_OIDC_CLIENT_SECRET; do
if [[ $name == GRAFANA_ADMIN_PASSWORD ]]; then
value="$grafana_admin_password"
else
value="$grafana_oidc_client_secret"
fi
[[ -n $value && $value != REPLACE_ME ]] || {
echo "Set $name with: k secrets edit bootstrap" >&2
return 1
}
done
kubectl create namespace argocd --dry-run=client -o yaml | kubectl apply -f -
kubectl create namespace monitoring --dry-run=client -o yaml | kubectl apply -f -
printf '%s' "$grafana_oidc_client_secret" |
kubectl -n argocd create secret generic argocd-grafana-oidc \
--from-file=oidc.clientSecret=/dev/stdin --dry-run=client -o yaml | kubectl apply -f -
kubectl -n argocd label secret argocd-grafana-oidc app.kubernetes.io/part-of=argocd --overwrite
printf '%s' "$grafana_oidc_client_secret" |
kubectl -n monitoring create secret generic grafana-oidc \
--from-file=oidc.clientSecret=/dev/stdin --dry-run=client -o yaml | kubectl apply -f -
printf '%s' "$grafana_admin_password" |
kubectl -n monitoring create secret generic grafana-admin \
--from-literal=admin-user=admin --from-file=admin-password=/dev/stdin \
--dry-run=client -o yaml | kubectl apply -f -
unset grafana_admin_password grafana_oidc_client_secret
}
read_vault_secret() {
local name="$1"
case "$name" in
VAULT_TRANSIT_SEAL_KEY_V1 | VAULT_TRANSIT_SEAL_TOKEN) ;;
*)
echo "Unknown Vault secret: $name" >&2
return 2
;;
esac
require_file "$VAULT_SECRETS_FILE"
sops decrypt --extract "[\"$name\"]" "$VAULT_SECRETS_FILE"
}
require_vault_secrets() {
local name value
for name in VAULT_TRANSIT_SEAL_KEY_V1 VAULT_TRANSIT_SEAL_TOKEN; do
value="$(read_vault_secret "$name")"
[[ -n $value && $value != REPLACE_ME ]] || {
echo "Set $name with: k secrets edit vault" >&2
return 1
}
done
}
require_vault_worker() {
curl --fail --silent --show-error --max-time 10 \
https://kms.vault.platform.scg.sh/healthz >/dev/null
}
validate_talos_inputs() {
local node node_name node_ip
require_file "$TALOS_SECRETS_FILE"
require_file "$PATCH_DIR/worker.yaml"
require_file "$PATCH_DIR/cilium.yaml"
for node in "${NODES[@]}"; do
node_name="${node%%:*}"
node_ip="${node#*:}"
[[ $node_name =~ ^[a-z0-9][a-z0-9-]*$ && $node_ip != null && $node_ip != REPLACE_* && $node_ip != *[[:space:]]* ]] || {
echo "Invalid node in state.yaml: $node" >&2
return 1
}
require_file "$PATCH_DIR/$node_name.yaml"
done
}
summary_of() {
local file="$1"
[[ -f $file ]] || return 0
awk 'NR > 1 && !/^#/ && NF { print; exit }' "$file"
}
usage_for() {
local directory="$1" prefix="$2" path name summary found=0
printf 'Usage: %s <command> [args...]\n\nCommands:\n' "$prefix"
for path in "$directory"/*.sh; do
[[ -f $path ]] || continue
name="$(basename "$path" .sh)"
summary="$(summary_of "${path%.sh}.md")"
if [[ -n $summary ]]; then
printf ' %-24s %s\n' "$name" "$summary"
else
printf ' %s\n' "$name"
fi
found=1
done
((found)) || echo " (none)"
}
show_doc() {
local doc="$1"
[[ -f $doc ]] || return 0
if [[ ! -t 1 ]]; then
command cat "$doc"
elif command -v glow >/dev/null 2>&1; then
glow "$doc"
else
md_style "$doc"
fi
}
# Minimal ANSI markdown renderer for interactive terminals without glow.
md_style() {
local in_code=0 line
while IFS= read -r line; do
if [[ $line == '```'* ]]; then
in_code=$((1 - in_code))
printf '\n'
continue
fi
if ((in_code)); then
printf '\033[2m%s\033[0m\n' "$line"
elif [[ $line =~ ^#{1,6}[[:space:]]+(.*)$ ]]; then
printf '\033[1;4m%s\033[0m\n' "${BASH_REMATCH[1]}"
elif [[ $line =~ ^[[:space:]]*[-*][[:space:]]+(.*)$ ]]; then
printf ' \033[1m\xE2\x80\xA2\033[0m %s\n' "${BASH_REMATCH[1]}"
else
printf '%s\n' "$line"
fi
done <"$1"
}
run_from() (
local directory="$1" prefix="$2"
shift 2
if (($# == 0)); then
usage_for "$directory" "$prefix"
return 2
fi
local command="$1"
shift
case "$command" in
help)
if (($#)); then
run_from "$directory" "$prefix" "$@" --help
else
usage_for "$directory" "$prefix"
fi
return
;;
-h | --help)
if [[ -f "$directory.md" ]]; then
show_doc "$directory.md"
else
usage_for "$directory" "$prefix"
fi
return
;;
*[!a-z0-9-]* | '')
echo "Invalid command: $command" >&2
return 2
;;
esac
# Keep the old singular spelling working while presenting the natural plural command.
[[ $directory != "$COMMAND_DIR/secrets" || $command != recipient ]] || command=recipients
local script="$directory/$command.sh"
if [[ ! -f $script ]]; then
echo "Unknown command: $prefix $command" >&2
usage_for "$directory" "$prefix" >&2
return 2
fi
if [[ ${1:-} == -h || ${1:-} == --help ]] && [[ ! -d "$directory/$command" ]]; then
if [[ -f "${script%.sh}.md" ]]; then
show_doc "${script%.sh}.md"
else
echo "Usage: $prefix $command [args...]"
fi
return
fi
if [[ ! -d "$directory/$command" && $directory != "$COMMAND_DIR/secrets"* ]]; then
load_cluster_state
fi
source "$script" "$@"
)
run() {
run_from "$COMMAND_DIR" k "$@"
}
run_group() {
local group="$1"
shift
run_from "$COMMAND_DIR/$group" "k $group" "$@"
}
run "$@"