Files
mtusi2026lab6/scripts/init-secrets.sh
T

43 lines
3.6 KiB
Bash
Executable File

#!/bin/sh
# Create lab credentials once, outside Helm values and release history.
set -eu
: "${KUBE_CONTEXT:=dev}" "${NAMESPACE:=highload-applications}"
: "${REGISTRY:=asisti.registry.twcstorage.ru}" "${REGISTRY_USER:=token}"
: "${REGISTRY_TOKEN_FILE:=$HOME/.secrets/twc-registry-token}"
test -s "$REGISTRY_TOKEN_FILE" || { echo 'Registry token file is missing or empty' >&2; exit 1; }
umask 077
work=$(mktemp -d)
trap 'rm -rf "$work"' EXIT
kubectl --context "$KUBE_CONTEXT" create namespace "$NAMESPACE" --dry-run=client -o yaml | kubectl --context "$KUBE_CONTEXT" apply -f -
if ! kubectl --context "$KUBE_CONTEXT" -n "$NAMESPACE" get secret messenger-secrets >/dev/null 2>&1; then
openssl rand -hex 32 | tr -d '\n' > "$work/postgres-password"
printf 'postgres://messenger:%s@postgres:5432/messenger?sslmode=disable' "$(cat "$work/postgres-password")" > "$work/database-url"
openssl rand -base64 32 | tr -d '\n' > "$work/cursor-key"
kubectl --context "$KUBE_CONTEXT" -n "$NAMESPACE" create secret generic messenger-secrets --from-file="$work/postgres-password" --from-file="$work/database-url" --from-file="$work/cursor-key"
fi
# Add the shared cursor key to an existing lab2 secret without replacing database credentials.
if ! kubectl --context "$KUBE_CONTEXT" -n "$NAMESPACE" get secret messenger-secrets -o json | jq -e '.data["cursor-key"] | length > 0' >/dev/null; then
openssl rand -base64 32 | tr -d '\n' > "$work/cursor-key"
jq -n --rawfile key "$work/cursor-key" '{stringData:{"cursor-key":$key}}' > "$work/patch.json"
kubectl --context "$KUBE_CONTEXT" -n "$NAMESPACE" patch secret messenger-secrets --type merge --patch-file "$work/patch.json"
fi
# Build a portable Docker config; macOS credential-helper references cannot be used by kubelet.
jq -n --arg registry "$REGISTRY" --arg user "$REGISTRY_USER" --rawfile token "$REGISTRY_TOKEN_FILE" \
'{auths:{($registry):{auth:(($user+":"+($token | sub("[\\r\\n]+$"; "")))|@base64)}}}' > "$work/config.json"
kubectl --context "$KUBE_CONTEXT" -n "$NAMESPACE" create secret generic registry-pull \
--type=kubernetes.io/dockerconfigjson --from-file=".dockerconfigjson=$work/config.json" \
--dry-run=client -o yaml | kubectl --context "$KUBE_CONTEXT" -n "$NAMESPACE" apply -f -
if ! kubectl --context "$KUBE_CONTEXT" -n "$NAMESPACE" get secret messenger-s3 >/dev/null 2>&1; then
printf 'operator' > "$work/root-user"
printf 'messenger' > "$work/access-key"
openssl rand -hex 32 | tr -d '\n' > "$work/root-password"
openssl rand -hex 32 | tr -d '\n' > "$work/secret-key"
jq -n --rawfile admin "$work/root-user" --rawfile adminSecret "$work/root-password" --rawfile app "$work/access-key" --rawfile appSecret "$work/secret-key" '{identities:[{name:"operator",credentials:[{accessKey:$admin,secretKey:$adminSecret}],actions:["Admin","Read","Write","List"]},{name:"messenger",credentials:[{accessKey:$app,secretKey:$appSecret}],actions:["Read:attachments","Write:attachments","List:attachments"]}]}' > "$work/identities.json"
kubectl --context "$KUBE_CONTEXT" -n "$NAMESPACE" create secret generic messenger-s3 --from-file="$work/root-user" --from-file="$work/root-password" --from-file="$work/access-key" --from-file="$work/secret-key" --from-file="$work/identities.json"
fi
if ! kubectl --context "$KUBE_CONTEXT" -n "$NAMESPACE" get secret messenger-grafana >/dev/null 2>&1; then
printf 'admin' > "$work/username"
openssl rand -hex 24 | tr -d '\n' > "$work/password"
kubectl --context "$KUBE_CONTEXT" -n "$NAMESPACE" create secret generic messenger-grafana --from-file="$work/username" --from-file="$work/password"
fi