feat(platform): convert MongoDB users to minted credentials

Migration 67 seeds the account Secret of every user of every MongoDB
release before the chart switches, from the per-user Secret the old
chart rendered, so clients keep logging in through the upgrade and
the controller adopts the Secrets once the templates exist. The
finish Job then strips users.*.password from the values and removes
any per-user Secret Helm left behind; <release>-credentials holds
only the databaseAdmin platform account and is left alone.

The Job's ServiceAccount, ClusterRole and binding carry the migration
number: one upgrade can run several conversion migrations in a row,
and a shared ServiceAccount deleted by a later hook would cut off an
earlier finish Job still running as it.

Assisted-by: LLM
Signed-off-by: Andrei Kvapil <kvapss@gmail.com>
This commit is contained in:
Andrei Kvapil committed 2026-12-01 14:30:33 +01:00
1 parent 0eb96ac872
commit 79883fed36
5 files changed
+513 -1

No files matched your search

+22
View File
@@ -0,0 +1,22 @@
#!/bin/sh
# Fake credentials-migrate for hack/migration-67-mongodb-credentials.bats.
# Migration 67 runs `credentials-migrate seed` in the hook; the real binary is
# exercised by cmd/credentials-migrate's Go tests, so this fake only records the
# call and fails on demand to pin the hook's fail-closed behaviour.
#
# FAKE_CMDLOG file to append a one-line record of each call to
# FAKE_SEED_FAIL non-empty => `seed` exits 1 with this stderr
set -u
[ -n "${FAKE_CMDLOG:-}" ] && printf 'CREDMIGRATE %s\n' "$*" >> "$FAKE_CMDLOG"
case "${1:-}" in
seed)
if [ -n "${FAKE_SEED_FAIL:-}" ]; then
printf '%s\n' "$FAKE_SEED_FAIL" >&2
exit 1
fi
exit 0
;;
esac
exit 0
+112
View File
@@ -0,0 +1,112 @@
#!/bin/sh
# Fake kubectl for hack/migration-67-mongodb-credentials.bats, modelled on
# hack/testdata/migration-58-http-cache/kubectl. It serves only the calls
# migration 67 and lib/cozystack-version.sh make, and logs each one to
# $FAKE_CMDLOG.
#
# Migration 67 reads its own pod's image, applies the finish Job (with its
# ServiceAccount and ClusterRoleBinding), and stamps the version. The Job
# manifest and the version stamp both arrive as `apply --filename -`, so this
# fake reads stdin and branches on the manifest's kind.
#
# FAKE_CMDLOG file to append a one-line record of each call to
# FAKE_IMAGE image the `get pod` call returns (default a test ref)
# FAKE_IMAGE_EMPTY non-empty => `get pod` returns an empty image
# FAKE_GETPOD_FAIL non-empty => `get pod` exits 1 with this stderr
# FAKE_APPLY_FAIL non-empty => applying the Job exits 1 with this stderr
set -u
log() { [ -n "${FAKE_CMDLOG:-}" ] && printf '%s\n' "$*" >> "$FAKE_CMDLOG"; }
log "KUBECTL $*"
args="$*"
IMAGE="${FAKE_IMAGE:-registry.example.org/cozystack/cozystack:v1.7.0}"
case "$args" in
*"get pod"*jsonpath*)
if [ -n "${FAKE_GETPOD_FAIL:-}" ]; then
printf '%s\n' "$FAKE_GETPOD_FAIL" >&2
exit 1
fi
[ -n "${FAKE_IMAGE_EMPTY:-}" ] || printf '%s' "$IMAGE"
exit 0
;;
*"delete job cozystack-credentials-migration-67"*)
# The hook deletes a Job left by an earlier run before recreating it.
log "DELETE-JOB"
exit 0
;;
*"delete serviceaccount cozystack-credentials-migration-67"*)
# The hook deletes a ServiceAccount left owned by the Job it just deleted,
# so the apply recreates a fresh, owner-less one.
log "DELETE-SA"
exit 0
;;
*"get job cozystack-credentials-migration-67"*jsonpath*)
# The hook reads the Job's uid to own its ServiceAccount.
if [ -n "${FAKE_GETJOB_FAIL:-}" ]; then
printf '%s\n' "$FAKE_GETJOB_FAIL" >&2
exit 1
fi
[ -n "${FAKE_JOBUID_EMPTY:-}" ] || printf '%s' "11111111-2222-3333-4444-555555555555"
exit 0
;;
*"patch serviceaccount cozystack-credentials-migration-67"*)
# The ownerReference tying the ServiceAccount to the Job.
if [ -n "${FAKE_PATCH_FAIL:-}" ]; then
printf '%s\n' "$FAKE_PATCH_FAIL" >&2
exit 1
fi
# The ServiceAccount shares the Job's name, so match the owner reference
# itself rather than the bare name.
owner=$(printf '%s' "$args" | grep -c '"kind":"Job","name":"cozystack-credentials-migration-67"')
log "PATCH-SA-OWNER owner=$owner"
exit 0
;;
*"apply --filename -"*)
manifest=$(cat)
case "$manifest" in
*"kind: ConfigMap"*)
v=$(printf '%s\n' "$manifest" | sed -n 's/^ version: "\{0,1\}\([0-9]\{1,\}\)"\{0,1\}$/\1/p' | head -1)
log "STAMP ${v:-unknown}"
exit 0
;;
*"kind: Job"*)
if [ -n "${FAKE_APPLY_FAIL:-}" ]; then
printf '%s\n' "$FAKE_APPLY_FAIL" >&2
exit 1
fi
# Record the fields the Job must carry, so a copy that drops the
# ServiceAccount, names the wrong migration, or renders the wrong image
# is caught.
role=$(printf '%s\n' "$manifest" | grep -c '^kind: ClusterRole$')
sa=$(printf '%s\n' "$manifest" | grep -c '^kind: ServiceAccount$')
crb=$(printf '%s\n' "$manifest" | grep -c '^kind: ClusterRoleBinding$')
# The binding must grant the least-privilege role, never cluster-admin.
admin=$(printf '%s\n' "$manifest" | grep -c 'cluster-admin')
# The ServiceAccount, ClusterRole and binding share the Job's name, so
# count the name only in the Job's own metadata.
jobname=$(printf '%s\n' "$manifest" | awk '/^kind: Job$/ { j = 1 } j && /^ name: cozystack-credentials-migration-67$/ { c++; j = 0 } END { print c + 0 }')
mig=$(printf '%s\n' "$manifest" | grep -c -- '--migration=67')
img=$(printf '%s\n' "$manifest" | grep -c "^ image: ${IMAGE}$")
sacc=$(printf '%s\n' "$manifest" | grep -c '^ serviceAccountName: cozystack-credentials-migration-67$')
log "APPLY-FINISH role=$role sa=$sa crb=$crb admin=$admin jobname=$jobname migration=$mig image=$img serviceaccount=$sacc"
exit 0
;;
*)
log "APPLY-UNKNOWN"
printf 'fake kubectl: unmodelled apply\n' >&2
exit 1
;;
esac
;;
esac
log "UNHANDLED $args"
printf 'fake kubectl: unmodelled call: %s\n' "$args" >&2
exit 1