Repository navigation
Expand file tree
/
Copy pathgen_secrets.sh
More file actions
executable file
·275 lines (249 loc) · 8.84 KB
/
Copy pathgen_secrets.sh
File metadata and controls
executable file
·275 lines (249 loc) · 8.84 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
#!/usr/bin/env bash
# gen_secrets.sh — create/rotate per-federation secrets without leaking to logs
# - Per federation: keycloak-credentials (client-id/client-secret), mip-secret (autogenerated),
# jupyterhub-crypt (autogenerated)
# - Common (mip-athena): codex-llm (Jupyter AI server URL, from CODEX_VLLM_BASE_URL
# or a hidden prompt; skipped when empty)
# - Common (datacatalog): datacatalog-secrets (keycloak + DB creds)
# - Skips existing secrets; use --force to rotate
# - Prompts show the target federation; client-secret input is hidden
# - No secrets printed to stdout/stderr; no inline --from-literal
set -euo pipefail
# ----------------------------- flags / config ---------------------------------
FORCE_UPDATE="${FORCE_UPDATE:-0}"
if [[ "${1:-}" == "-f" || "${1:-}" == "--force" ]]; then
FORCE_UPDATE=1
shift || true
fi
# Hide client-id prompt as well (default shows it so you can see typos)
KC_HIDE_ID="${KC_HIDE_ID:-0}"
# kubectl options (safe defaults); adjust if needed
KUBECTL_OPTS="${KUBECTL_OPTS:---request-timeout=20s}"
# Per-apply timeout (if coreutils `timeout` exists)
APPLY_TIMEOUT="${APPLY_TIMEOUT:-25s}"
if command -v timeout >/dev/null 2>&1; then
TIMEOUT_CMD=(timeout -k 3s "${APPLY_TIMEOUT}")
else
TIMEOUT_CMD=() # no timeout available; kubectl will run normally
fi
# ------------------------------ safety settings -------------------------------
# Remember xtrace state and disable tracing while handling secrets
XTRACE_STATE="$(set +o | grep xtrace || true)"
set +x
# Default perms for any files we create: files 600, dirs 700
umask 077
# -------------------------------- helpers -------------------------------------
gen_password() {
# Generate a 24-char alphanumeric string.
# Temporarily disable pipefail so SIGPIPE from upstream is ignored.
local pw rc
set +o pipefail
pw="$(LC_ALL=C tr -dc 'A-Za-z0-9' </dev/urandom | head -c24)"
rc=$?
set -o pipefail
if [[ $rc -ne 0 || -z "$pw" ]]; then
echo "failed to generate password" >&2
exit 1
fi
printf '%s' "$pw"
}
secret_exists() { # $1=ns, $2=name
kubectl ${KUBECTL_OPTS} -n "$1" get secret "$2" >/dev/null 2>&1
}
prompt_visible() { # $1=var, $2=prompt
local __var="$1" __msg="$2" __val=""
if [[ "${KC_HIDE_ID}" == "1" ]]; then
read -r -s -p "$__msg" __val </dev/tty; echo >/dev/tty
else
read -r -p "$__msg" __val </dev/tty
fi
printf -v "$__var" '%s' "$__val"
}
prompt_secret() { # $1=var, $2=prompt
local __var="$1" __msg="$2" __val=""
read -r -s -p "$__msg" __val </dev/tty; echo >/dev/tty
printf -v "$__var" '%s' "$__val"
}
mkpriv() {
local base="/dev/shm"
[[ -d "$base" && -w "$base" ]] || base="${TMPDIR:-/tmp}"
mktemp "$base/mipsec.XXXXXX"
}
secure_rm() {
command -v shred >/dev/null 2>&1 && shred -u "$1" || rm -f "$1"
}
apply_yaml_file() { # $1=ns, $2=path
local ns="$1" f="$2" rc=0
set +e
"${TIMEOUT_CMD[@]}" kubectl ${KUBECTL_OPTS} -n "$ns" apply -o name -f "$f"
rc=$?
set -e
return "$rc"
}
# -------------------------- discover federation namespaces --------------------
readarray -t FED_NS < <(kubectl ${KUBECTL_OPTS} get ns \
-o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}' | grep '^federation-' || true)
echo "[info] Found ${#FED_NS[@]} federation namespaces: ${FED_NS[*]:-<none>}"
# -------------------------- common datacatalog-secrets -------------------------
# Creates/rotates: mip-common-datacatalog/datacatalog-secrets
# stringData:
# keycloak.client-id
# keycloak.client-secret
# db.user
# db.password
if secret_exists "mip-common-datacatalog" "datacatalog-secrets" && [[ "$FORCE_UPDATE" -eq 0 ]]; then
echo "[skip] mip-common-datacatalog/datacatalog-secrets exists."
else
# Allow env to pre-seed; otherwise, prompt (hide secrets; KC_HIDE_ID respected for client-id)
[[ -z "${COMMON_KC_CLIENT_ID:-}" ]] && prompt_visible COMMON_KC_CLIENT_ID "Keycloak client-id for mip-common-datacatalog: "
[[ -z "${COMMON_KC_CLIENT_SECRET:-}" ]] && prompt_secret COMMON_KC_CLIENT_SECRET "Keycloak client-secret for mip-common-datacatalog: "
if [[ -z "${COMMON_DB_USER:-}" ]]; then
COMMON_DB_USER="$(gen_password)"
COMMON_DB_PASSWORD="$(gen_password)"
fi
if [[ -z "${COMMON_KC_CLIENT_ID:-}" || -z "${COMMON_KC_CLIENT_SECRET:-}" || -z "${COMMON_DB_USER:-}" || -z "${COMMON_DB_PASSWORD:-}" ]]; then
echo "[err] missing datacatalog client-id/secret or DB user/password; aborting." >&2
exit 1
fi
f=$(mkpriv)
cat >"$f" <<EOF
apiVersion: v1
kind: Secret
metadata:
name: datacatalog-secrets
type: Opaque
stringData:
keycloak.client-id: ${COMMON_KC_CLIENT_ID}
keycloak.client-secret: ${COMMON_KC_CLIENT_SECRET}
db.user: ${COMMON_DB_USER}
db.password: ${COMMON_DB_PASSWORD}
EOF
if apply_yaml_file "mip-common-datacatalog" "$f"; then
echo "[ok ] mip-common-datacatalog/datacatalog-secrets"
else
echo "[err] mip-common-datacatalog/datacatalog-secrets" >&2
fi
secure_rm "$f"
unset COMMON_KC_CLIENT_ID COMMON_KC_CLIENT_SECRET
fi
# -------------------------- mip-athena/codex-llm -------------------------------
# Jupyter AI (Codex) inference server URL, key base-url, read only by the shared
# athena-proxy (common/athena-proxy). The namespace is created by Argo CD when
# that app syncs. Optional: without it Codex is off.
if ! kubectl ${KUBECTL_OPTS} get ns mip-athena >/dev/null 2>&1; then
echo "[skip] mip-athena/codex-llm: namespace mip-athena does not exist yet."
elif secret_exists "mip-athena" "codex-llm" && [[ "$FORCE_UPDATE" -eq 0 ]]; then
echo "[skip] mip-athena/codex-llm exists."
else
[[ -z "${CODEX_VLLM_BASE_URL:-}" ]] && prompt_secret CODEX_VLLM_BASE_URL "Codex server URL ending in /v1 (empty to skip): "
if [[ -z "${CODEX_VLLM_BASE_URL:-}" ]]; then
echo "[skip] mip-athena/codex-llm (no URL given)."
else
f=$(mkpriv)
cat >"$f" <<EOF
apiVersion: v1
kind: Secret
metadata:
name: codex-llm
type: Opaque
stringData:
base-url: ${CODEX_VLLM_BASE_URL}
EOF
if apply_yaml_file "mip-athena" "$f"; then
echo "[ok ] mip-athena/codex-llm"
else
echo "[err] mip-athena/codex-llm" >&2
fi
secure_rm "$f"
fi
fi
# -------------------------- per-federation secrets ----------------------------
for ns in "${FED_NS[@]}"; do
echo ">> Namespace: ${ns}"
# --- keycloak-credentials per federation (prompt only if creating/forcing) ---
if secret_exists "$ns" "keycloak-credentials" && [[ "$FORCE_UPDATE" -eq 0 ]]; then
echo " [skip] ${ns}/keycloak-credentials exists."
else
local_id=""; local_secret=""
prompt_visible local_id " Keycloak client-id for ${ns}: "
prompt_secret local_secret " Keycloak client-secret for ${ns}: "
if [[ -z "$local_id" || -z "$local_secret" ]]; then
echo " [err] missing client-id/secret for ${ns}; skipping."
else
f=$(mkpriv)
cat >"$f" <<EOF
apiVersion: v1
kind: Secret
metadata:
name: keycloak-credentials
type: Opaque
stringData:
client-id: ${local_id}
client-secret: ${local_secret}
EOF
if apply_yaml_file "$ns" "$f"; then
echo " [ok ] ${ns}/keycloak-credentials"
else
echo " [err] ${ns}/keycloak-credentials" >&2
fi
secure_rm "$f"
fi
fi
# --- mip-secret per federation (generate or rotate) --------------------------
if secret_exists "$ns" "mip-secret" && [[ "$FORCE_UPDATE" -eq 0 ]]; then
echo " [skip] ${ns}/mip-secret exists."
else
echo " [gen] ${ns}/mip-secret …"
# Fixed values for now
PB_ADMIN_USER="postgres"
PB_USER="portal"
# Autogenerated passwords
PB_ADMIN_PASS="$(gen_password)"
PB_PASS="$(gen_password)"
f=$(mkpriv)
cat >"$f" <<EOF
apiVersion: v1
kind: Secret
metadata:
name: mip-secret
type: Opaque
stringData:
platform-backend-db.DB_ADMIN_USER: ${PB_ADMIN_USER}
platform-backend-db.DB_ADMIN_PASSWORD: ${PB_ADMIN_PASS}
platform-backend-db.PLATFORM_DB_USER: ${PB_USER}
platform-backend-db.PLATFORM_DB_PASSWORD: ${PB_PASS}
EOF
if apply_yaml_file "$ns" "$f"; then
echo " [ok ] ${ns}/mip-secret"
else
echo " [err] ${ns}/mip-secret" >&2
fi
secure_rm "$f"
fi
# --- jupyterhub-crypt per federation (generate or rotate) --------------------
# JupyterHub auth_state encryption key (madgik/mip notebook). Kept out of the
# chart values so it stays stable across syncs; rotating it only drops stored
# auth state, users log in again.
if secret_exists "$ns" "jupyterhub-crypt" && [[ "$FORCE_UPDATE" -eq 0 ]]; then
echo " [skip] ${ns}/jupyterhub-crypt exists."
else
f=$(mkpriv)
cat >"$f" <<EOF
apiVersion: v1
kind: Secret
metadata:
name: jupyterhub-crypt
type: Opaque
stringData:
crypt-key: $(openssl rand -hex 32)
EOF
if apply_yaml_file "$ns" "$f"; then
echo " [ok ] ${ns}/jupyterhub-crypt"
else
echo " [err] ${ns}/jupyterhub-crypt" >&2
fi
secure_rm "$f"
fi
done
# ------------------------------ restore xtrace --------------------------------
eval "$XTRACE_STATE"