Files
mumble-kubernetes/bootstrap-homelab-secrets.sh
feedc0de ce91969304
Validate, publish and promote Mumble chart / chart (push) Successful in 47s
Support CNPG secret bootstrap
2026-10-25 07:49:51 +01:00

118 lines
5.0 KiB
Bash
Executable File

#!/bin/sh
set -eu
umask 077
NAMESPACE=${MUMBLE_NAMESPACE:-default}
POSTGRES_MODE=${MUMBLE_POSTGRES_MODE:-bitnami}
SECRET_NAME=mumble-secrets
for command in kubectl openssl jq; do
if ! command -v "$command" >/dev/null 2>&1; then
echo "Required command not found: $command" >&2
exit 1
fi
done
if [ "$(kubectl config current-context)" != kubernetes-admin@kubernetes ]; then
echo 'Refusing to deploy outside the homelab context' >&2
exit 1
fi
case "$POSTGRES_MODE" in
bitnami|external|cnpg) ;;
*) echo 'MUMBLE_POSTGRES_MODE must be bitnami, external, or cnpg' >&2; exit 1 ;;
esac
for dependency in secret/brunner-ninja-tls; do
if ! kubectl get "$dependency" --namespace "$NAMESPACE" >/dev/null 2>&1; then
echo "Required Kubernetes object not found: $NAMESPACE/$dependency" >&2
exit 1
fi
done
if ! kubectl get secret "$SECRET_NAME" --namespace "$NAMESPACE" >/dev/null 2>&1; then
if [ "$NAMESPACE" != default ] && kubectl get secret "$SECRET_NAME" --namespace default >/dev/null 2>&1; then
kubectl get secret "$SECRET_NAME" --namespace default -o json \
| jq --arg namespace "$NAMESPACE" '
.metadata = {name: .metadata.name, namespace: $namespace}
| .type = "kubernetes.io/basic-auth"
| .data.username = "bXVtYmxl"
| .data.password = .data["database-password"]
' \
| kubectl apply -f - >/dev/null
echo "Copied existing $SECRET_NAME into $NAMESPACE without changing its credentials."
elif [ "$NAMESPACE" != default ]; then
echo "Restore $NAMESPACE/$SECRET_NAME from backup; do not generate a new database password" >&2
exit 1
elif kubectl get secret mumble-postgres --namespace "$NAMESPACE" >/dev/null 2>&1; then
echo "Restore $NAMESPACE/$SECRET_NAME from backup; do not replace its database password" >&2
exit 1
else
database_password=$(openssl rand -hex 32)
superuser_password=$(openssl rand -hex 32)
registration_password=$(openssl rand -hex 32)
kubectl create secret generic "$SECRET_NAME" \
--namespace "$NAMESPACE" \
--from-literal="database-password=$database_password" \
--from-literal="superuser-password=$superuser_password" \
--from-literal="registration-password=$registration_password"
unset database_password superuser_password registration_password
echo "Created $NAMESPACE/$SECRET_NAME with random database, SuperUser, and public-registry passwords."
fi
else
echo "Keeping existing $NAMESPACE/$SECRET_NAME."
registration_password_present=$(kubectl get secret "$SECRET_NAME" \
--namespace "$NAMESPACE" \
-o go-template='{{if index .data "registration-password"}}yes{{end}}')
if [ -z "$registration_password_present" ] || [ "${ROTATE_REGISTRATION_PASSWORD:-false}" = true ]; then
registration_password=$(openssl rand -hex 32)
kubectl patch secret "$SECRET_NAME" \
--namespace "$NAMESPACE" \
--type=merge \
--patch "{\"stringData\":{\"registration-password\":\"$registration_password\"}}" >/dev/null
unset registration_password
echo "Set a new random public-registry password in $NAMESPACE/$SECRET_NAME."
fi
unset registration_password_present
fi
if [ "$POSTGRES_MODE" = cnpg ]; then
kubectl get secret "$SECRET_NAME" --namespace "$NAMESPACE" -o json \
| jq '
.type = "kubernetes.io/basic-auth"
| .data.username = "bXVtYmxl"
| .data.password = .data["database-password"]
' \
| kubectl apply -f - >/dev/null
echo "Prepared $NAMESPACE/$SECRET_NAME as CloudNativePG bootstrap and Mumble credentials."
elif ! kubectl get secret mumble-postgres --namespace "$NAMESPACE" >/dev/null 2>&1; then
admin_password=$(openssl rand -hex 32)
database_password=$(kubectl get secret "$SECRET_NAME" --namespace "$NAMESPACE" \
-o go-template='{{index .data "database-password"}}' | base64 -d)
kubectl create secret generic mumble-postgres --namespace "$NAMESPACE" \
--from-literal="postgres-password=$admin_password" \
--from-literal="database-password=$database_password"
unset admin_password database_password
echo "Created $NAMESPACE/mumble-postgres for the dedicated PostgreSQL chart."
else
echo "Keeping existing $NAMESPACE/mumble-postgres."
app_password=$(kubectl get secret "$SECRET_NAME" --namespace "$NAMESPACE" \
-o go-template='{{index .data "database-password"}}')
database_password=$(kubectl get secret mumble-postgres --namespace "$NAMESPACE" \
-o go-template='{{index .data "database-password"}}')
if [ "$app_password" != "$database_password" ]; then
unset app_password database_password
echo 'Mumble and PostgreSQL database passwords differ; restore from backup' >&2
exit 1
fi
unset app_password database_password
fi
echo "Secrets are ready for the Argo CD-managed Mumble release."