mirror of
https://github.com/cozystack/cozystack.git
synced 2026-12-05 22:53:43 +09:00
feat(mariadb)!: mint tenant passwords through the credentials API (#5379)
## What this PR does MariaDB passwords are no longer readable from `<release>-credentials`, which every `use` member and every parent tenant can read today. An administrator mints a password on the user's `Credential` and sees it once. The database stores only its `mysql_native_password` hash, and the next mint or a revoke kills it within seconds. `root` stays the platform's account and is hidden from tenants. This is the MariaDB part of the user secrets API design (cozystack/community#137). Stacked on #5378. Review only the top three commits: - `credentials-migrate` gets `--skip-account`, so no account is written for `users.root`, which the chart always ignored. - The chart renders a `mysql-native` CredentialTemplate per user and points each `User` at its account Secret through `passwordHashSecretKeyRef`. - Migration 66 writes the hashed account Secrets before the chart switches. Its finish Job removes the tenant keys from `<release>-credentials` and keeps `root`. Tested on a three-node cluster, fresh install: a minted password logs in after 3 s, the old one gets `Access denied` 3 s after the next mint, revoke works, and removing a user drops the `mysql.user` row too. Full upgrade from 1.7.1 on the same cluster, all engines at once with a client loop each: migrations 64-71 ran in order and every account was adopted with its old password. The MariaDB client loop had no failures. Breaking for users: passwords can't be read any more. A user name longer than 63 characters, one that starts or ends with a character other than a letter or digit (such as `_backup`), or two names that map to one account (`a.b` and `a-b`), now fail the render; such a release is not converted. A password changed inside MariaDB is reverted by the operator. Open questions: 1. `root` is not rotated, although the design says it should be. The operator reads it on every reconcile and the pods take it as `MARIADB_ROOT_PASSWORD` for their probes, so changing it needs a coordinated restart. So the `root` password tenants could read stays valid. Accept that and fix the design, or rotate it before merge? 2. Not from this diff: the backup CronJob reads `root-password` from a Secret `<release>` that doesn't exist. It needs a separate fix. ### Screenshots No UI change. ### Downstream repositories - [ ] No downstream repository is affected by this change - [ ] [cozystack/website](https://github.com/cozystack/website) - follow-up: needed, a MariaDB note for the credentials API docs (cozystack/website#753), an upgrade note, and `backup-classes.md` still points at credentials in `<release>-credentials` - [ ] [cozystack/terraform-provider-cozystack](https://github.com/cozystack/terraform-provider-cozystack) - follow-up: needed, `mariadb_schema.go` tells users the password is in `mariadb-<name>-credentials` - [ ] [cozystack/ansible-cozystack](https://github.com/cozystack/ansible-cozystack) - follow-up: - [ ] [cozystack/ccp](https://github.com/cozystack/ccp) - follow-up: - [ ] [cozystack/talm](https://github.com/cozystack/talm) - follow-up: - [ ] [cozystack/cozyhr](https://github.com/cozystack/cozyhr) - follow-up: - [ ] [cozystack/cozy-proxy](https://github.com/cozystack/cozy-proxy) - follow-up: - [ ] [cozystack/cozystack-telemetry-server](https://github.com/cozystack/cozystack-telemetry-server) - follow-up: - [ ] [cozystack/external-apps-example](https://github.com/cozystack/external-apps-example) - follow-up: - [ ] [cozystack/examples](https://github.com/cozystack/examples) - follow-up: - [ ] [cozystack/community](https://github.com/cozystack/community) - follow-up: needed only if `root` stays unrotated, to fix the design ### Release note ```release-note feat(mariadb)!: MariaDB passwords are issued through the credentials API and are no longer readable from `<release>-credentials`. An administrator mints a password on the user's Credential; it is shown once and applies within seconds. `root` is no longer visible to tenants. Existing passwords keep working through the upgrade. A user name longer than 63 characters, one that starts or ends with a character other than a letter or digit (such as `_backup`), or two names that map to one account name (such as `a.b` and `a-b`), now fail the render and keep the release unconverted until renamed. Switch clients to minted passwords after the upgrade. ``` <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **New Features** * MariaDB tenant accounts now use individually issued credentials. Passwords are returned when minted, while only password hashes are stored; credentials can be rotated or revoked through the credentials API. * Existing MariaDB accounts are migrated to the new credential system. The platform-managed `root` account remains separate from tenant accounts. * **Bug Fixes** * Tenant-supplied `password` values and `root` entries are ignored, with admission warnings for supplied passwords. Password changes made directly in MariaDB are reverted during reconciliation. * Tenant access to MariaDB credentials Secrets is restricted. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
27 files changed
+1086
-227
No files matched your search
@@ -61,12 +61,14 @@ CHART="$REPO_ROOT/packages/system/mariadb-rd"
|
||||
|
||||
NORMALIZE='{exclude: (.exclude // [] | sort), include: (.include // [] | sort)}'
|
||||
|
||||
# The complete tenant-visible Secret surface: the credentials Secret by name,
|
||||
# the trust anchor by label, every key-bearing and credential Secret withheld
|
||||
# by name. mariadb-<name>.ca-tls holds the CA private key and mariadb-<name>.tls
|
||||
# The complete tenant-visible Secret surface: the trust anchor by label, and
|
||||
# nothing by name. Every key-bearing and credential Secret is withheld by name,
|
||||
# <release>-credentials among them: it holds only root, the platform's own
|
||||
# account, and tenant users are Credentials whose account Secrets no definition
|
||||
# selects. mariadb-<name>.ca-tls holds the CA private key and mariadb-<name>.tls
|
||||
# the server key; the operator's own -ca, -server-cert and -client-cert hold
|
||||
# keys too. None of them may reach a tenant, by name or by a label they carry.
|
||||
EXPECTED_SECRETS='{"exclude":[{"resourceNames":["mariadb-{{ .name }}.ca-tls","mariadb-{{ .name }}.tls","mariadb-{{ .name }}-ca","mariadb-{{ .name }}-server-cert","mariadb-{{ .name }}-client-cert","mariadb-{{ .name }}-root","mariadb-{{ .name }}-password","mariadb-{{ .name }}-repl-password","mariadb-{{ .name }}-metrics-password","mariadb-{{ .name }}-metrics-config","mariadb-{{ .name }}-backup","mariadb-{{ .name }}-regsecret"]}],"include":[{"matchLabels":{"internal.cozystack.io/tenant-ca":"true"}},{"resourceNames":["mariadb-{{ .name }}-credentials"]}]}'
|
||||
EXPECTED_SECRETS='{"exclude":[{"resourceNames":["mariadb-{{ .name }}.ca-tls","mariadb-{{ .name }}.tls","mariadb-{{ .name }}-ca","mariadb-{{ .name }}-server-cert","mariadb-{{ .name }}-client-cert","mariadb-{{ .name }}-credentials","mariadb-{{ .name }}-root","mariadb-{{ .name }}-password","mariadb-{{ .name }}-repl-password","mariadb-{{ .name }}-metrics-password","mariadb-{{ .name }}-metrics-config","mariadb-{{ .name }}-backup","mariadb-{{ .name }}-regsecret"]}],"include":[{"matchLabels":{"internal.cozystack.io/tenant-ca":"true"}}]}'
|
||||
|
||||
# The Services an instance exposes, pinned for the same reason: a catch-all
|
||||
# selector reaches this list too, and nothing else in the tree holds it.
|
||||
@@ -133,7 +135,7 @@ EXPECTED_SERVICES='{"exclude":[],"include":[{"resourceNames":["mariadb-{{ .name
|
||||
# cert-manager for are joined with a dot, everything else with a hyphen, so
|
||||
# a loop that supplied the hyphen itself would look for names nothing renders.
|
||||
for n in .ca-tls .tls -ca -server-cert -client-cert \
|
||||
-root -password -repl-password -metrics-password -metrics-config \
|
||||
-credentials -root -password -repl-password -metrics-password -metrics-config \
|
||||
-backup -regsecret; do
|
||||
hits=$(printf '%s\n' "$rendered" | yq eval-all \
|
||||
"[.. | select(has(\"secrets\")) | .secrets.exclude[].resourceNames[]? | select(. == \"mariadb-{{ .name }}$n\")] | length" \
|
||||
@@ -150,7 +152,7 @@ EXPECTED_SERVICES='{"exclude":[],"include":[{"resourceNames":["mariadb-{{ .name
|
||||
# Whole-structure comparison. Anything added to the tenant's Secret surface --
|
||||
# by name, by label, or by an unrestricted entry -- changes this string, and so
|
||||
# does anything removed from the backstop.
|
||||
@test "mariadb-rd grants exactly the credentials Secret and the trust anchor" {
|
||||
@test "mariadb-rd grants exactly the trust anchor" {
|
||||
rendered=$(helm template mariadb-rd "$CHART") || return 1
|
||||
|
||||
if [ -z "$rendered" ]; then
|
||||
|
||||
@@ -128,6 +128,114 @@ spec:
|
||||
status:
|
||||
replicas: 1
|
||||
|
||||
# No second disclosure: the minted password logs in over TCP as the user,
|
||||
# the Credential records the mint, no read path the design names carries
|
||||
# the password (the audit log aside, which the sandbox does not collect),
|
||||
# root's Secret is out of the tenant's reach, and a second mint replaces
|
||||
# the first once mariadb-operator applies it.
|
||||
- name: verify-minted-credential
|
||||
try:
|
||||
- assert:
|
||||
timeout: 2m
|
||||
resource:
|
||||
apiVersion: core.cozystack.io/v1alpha1
|
||||
kind: Credential
|
||||
metadata:
|
||||
name: mariadb-test.testuser
|
||||
spec:
|
||||
application:
|
||||
kind: MariaDB
|
||||
name: test
|
||||
user: testuser
|
||||
status:
|
||||
password:
|
||||
origin: NotIssued
|
||||
# bash for pipefail and process substitution; the sandbox's /bin/sh is dash.
|
||||
- script:
|
||||
timeout: 5m
|
||||
shell: bash
|
||||
content: |
|
||||
set -euo pipefail
|
||||
cred=mariadb-test.testuser
|
||||
as="--as=e2e-mariadb-probe --as-group=system:authenticated --as-group=${NAMESPACE}-super-admin"
|
||||
mint() {
|
||||
kubectl create --raw "/apis/core.cozystack.io/v1alpha1/namespaces/$NAMESPACE/credentials/$cred/mint" \
|
||||
-f <(printf '{"apiVersion":"core.cozystack.io/v1alpha1","kind":"CredentialRequest"}') \
|
||||
| jq -r '.status.password'
|
||||
}
|
||||
# The password goes to the client on stdin: exec arguments travel in
|
||||
# the request URI, which the audit log records at any level.
|
||||
login() {
|
||||
printf '%s\n' "$1" | kubectl -n "$NAMESPACE" exec -i mariadb-test-0 -c mariadb -- \
|
||||
sh -c 'IFS= read -r MYSQL_PWD; export MYSQL_PWD; exec mariadb "$@"' sh \
|
||||
-h mariadb-test -u testuser -D testdb -N -e 'SELECT 1' 2>/dev/null | tr -d '[:space:]' || true
|
||||
}
|
||||
# Fails when any of the named texts carries the password.
|
||||
refuse() {
|
||||
local what=$1 text=$2
|
||||
if printf '%s' "$text" | grep -qF -- "$first"; then
|
||||
echo "$what carries the minted password" >&2
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
first=$(mint)
|
||||
[ "${#first}" -eq 32 ] || { echo "minted password has ${#first} characters, want 32" >&2; exit 1; }
|
||||
# mariadb-operator applies the changed account Secret live through
|
||||
# the watch label; a readiness wait on that eventually consistent
|
||||
# property, not a retry.
|
||||
until [ "$(login "$first")" = "1" ]; do sleep 5; done
|
||||
|
||||
view=$(kubectl -n "$NAMESPACE" get credentials.core.cozystack.io "$cred" -o json)
|
||||
[ "$(printf '%s' "$view" | jq -r .status.password.origin)" = "Minted" ] || { echo "origin is not Minted: $view" >&2; exit 1; }
|
||||
[ "$(printf '%s' "$view" | jq -r '.status.history[0].operation')" = "Mint" ] || { echo "history lacks the mint: $view" >&2; exit 1; }
|
||||
refuse "the Credential" "$view"
|
||||
|
||||
# The account Secret holds the mysql_native_password hash, never the
|
||||
# password.
|
||||
stored=$(kubectl -n "$NAMESPACE" get secret "$cred.account" -o json | jq -r '.data.password' | base64 -d)
|
||||
if ! printf '%s' "$stored" | grep -Eqx '\*[0-9A-F]{40}'; then
|
||||
echo "the account Secret does not hold a mysql_native_password hash" >&2; exit 1
|
||||
fi
|
||||
refuse "the account Secret" "$stored"
|
||||
|
||||
# Capture each read in an assignment first: under set -e a failing
|
||||
# command substitution in an argument does not stop the script.
|
||||
events=$(kubectl -n "$NAMESPACE" get events -o json)
|
||||
refuse "an Event" "$events"
|
||||
application=$(kubectl -n "$NAMESPACE" get mariadbs.apps.cozystack.io test -o json)
|
||||
refuse "the application" "$application"
|
||||
rels=$(kubectl -n "$NAMESPACE" get secret -l owner=helm,name=mariadb-test -o name)
|
||||
[ -n "$rels" ] || { echo "no Helm release Secret for mariadb-test to inspect" >&2; exit 1; }
|
||||
for rel in $rels; do
|
||||
decoded=$(kubectl -n "$NAMESPACE" get "$rel" -o jsonpath='{.data.release}' | base64 -d | base64 -d | gunzip)
|
||||
refuse "Helm release $rel" "$decoded"
|
||||
done
|
||||
# Neither the account Secret nor root's Secret is listed through
|
||||
# tenantsecrets, and neither is granted by name.
|
||||
visible=$(kubectl -n "$NAMESPACE" get tenantsecrets.core.cozystack.io $as -o json)
|
||||
for s in "$cred.account" mariadb-test-credentials; do
|
||||
if printf '%s' "$visible" | jq -r '.items[].metadata.name' | grep -Fqx "$s"; then
|
||||
echo "$s is listed through tenantsecrets" >&2; exit 1
|
||||
fi
|
||||
# can-i exits 1 when it answers no, so the answer is read from its
|
||||
# output; anything but a plain "no" fails, an error included.
|
||||
answer=$(kubectl -n "$NAMESPACE" auth can-i get "secret/$s" $as 2>&1) || true
|
||||
if [ "$answer" != no ]; then
|
||||
echo "the tenant super-admin group can read $s, or can-i did not answer: $answer" >&2
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
decoded_visible=$(printf '%s' "$visible" | jq -r '.items[].data // {} | .[] | @base64d')
|
||||
refuse "tenantsecrets" "$decoded_visible"
|
||||
|
||||
second=$(mint)
|
||||
[ "$second" != "$first" ] || { echo "a second mint returned the same password" >&2; exit 1; }
|
||||
until [ "$(login "$second")" = "1" ]; do sleep 5; done
|
||||
if [ "$(login "$first")" = "1" ]; then echo "the replaced password still logs in" >&2; exit 1; fi
|
||||
check:
|
||||
($error == null): true
|
||||
|
||||
# The trust anchor a client verifies against, read off the live operator
|
||||
# rather than off this repo's beliefs about it. The unit suite pins what the
|
||||
# chart renders — the sentinel, its source name and its key — from the same
|
||||
@@ -148,9 +256,10 @@ spec:
|
||||
# a kind that never reconciled. Without a witness that the mechanism is
|
||||
# live, they would all pass while asserting nothing.
|
||||
#
|
||||
# The credentials Secret is the witness: the chart renders it, its lineage
|
||||
# resolves to this instance, and the ApplicationDefinition grants it by
|
||||
# name, so it MUST be stamped.
|
||||
# The tenant-ca projection below is the witness: it must read "true",
|
||||
# which only the webhook writes. <release>-credentials cannot serve: it
|
||||
# holds only root, the platform's account, and the chart renders it with
|
||||
# "false" itself, which this step pins.
|
||||
- assert:
|
||||
timeout: 2m
|
||||
resource:
|
||||
@@ -159,7 +268,7 @@ spec:
|
||||
metadata:
|
||||
name: mariadb-test-credentials
|
||||
labels:
|
||||
internal.cozystack.io/tenantresource: "true"
|
||||
internal.cozystack.io/tenantresource: "false"
|
||||
# tenant-ca is stamped by the CA-extraction controller; tenantresource is
|
||||
# the LINEAGE WEBHOOK's verdict, computed from the ApplicationDefinition's
|
||||
# secrets.include matchLabels. It reads "true" only if the whole chain held:
|
||||
|
||||
@@ -0,0 +1,201 @@
|
||||
#!/usr/bin/env bats
|
||||
# -----------------------------------------------------------------------------
|
||||
# Unit tests for platform migration 66 --> 67 (convert MariaDB accounts to
|
||||
# platform-minted credentials, first part).
|
||||
#
|
||||
# The migration seeds the account Secrets of every MariaDB release, then
|
||||
# creates the cozystack-credentials-migration-66 finish Job directly (rather
|
||||
# than rendering it from the platform chart) so a changing migrations image
|
||||
# never collides with an immutable pod template and helm-controller waits on no
|
||||
# long-running Job. See packages/core/platform/images/migrations/migrations/66.
|
||||
#
|
||||
# FAIL CLOSED is pinned for each path: a failed seed, a pod whose image cannot
|
||||
# be read, and a failed apply of the Job must stop the migration before it
|
||||
# stamps the version, because migrations never re-run.
|
||||
#
|
||||
# The harness (docker, the migrations image base, fake kubectl and
|
||||
# credentials-migrate on PATH) is the one hack/migration-58-http-cache-freeze.bats
|
||||
# describes; see there for why each piece is load-bearing.
|
||||
#
|
||||
# Run with: hack/cozytest.sh hack/migration-66-mariadb-credentials.bats
|
||||
# -----------------------------------------------------------------------------
|
||||
|
||||
load test_helper
|
||||
|
||||
FAKEBIN="$PWD/hack/testdata/migration-66-mariadb-credentials"
|
||||
MIG_DIR="$PWD/packages/core/platform/images/migrations/migrations"
|
||||
ALPINE=$(sed -n 's/^FROM \(alpine:[^ ]*\).*$/\1/p' \
|
||||
"$PWD/packages/core/platform/images/migrations/Dockerfile" | head -1)
|
||||
TESTIMG="cozystack-migration66-test:$(printf '%s' "$ALPINE" | sed 's/[^a-zA-Z0-9]/-/g')"
|
||||
WORKROOT="${TMPDIR:-/tmp}/cozy-migration-66-$$"
|
||||
IMAGE=registry.example.org/cozystack/cozystack:v1.7.0
|
||||
|
||||
cozy_cleanup() {
|
||||
rm -rf "$WORKROOT"
|
||||
return 0
|
||||
}
|
||||
|
||||
cmdlog_line() {
|
||||
grep -nE -- "$1" "$FAKE_CMDLOG" | head -1 | cut -d: -f1
|
||||
}
|
||||
|
||||
run_migration() {
|
||||
_run_migration_rc=0
|
||||
docker run --rm --network none \
|
||||
--user "$(id -u):$(id -g)" \
|
||||
-v "$MIG_DIR:/migrations:ro" \
|
||||
-v "$FAKEBIN:/fakebin:ro" \
|
||||
-v "$WORK:/work" \
|
||||
-e PATH=/fakebin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin \
|
||||
-e FAKE_CMDLOG=/work/cmdlog \
|
||||
-e NAMESPACE="${NAMESPACE-}" \
|
||||
-e FAKE_IMAGE="${FAKE_IMAGE-}" \
|
||||
-e FAKE_IMAGE_EMPTY="${FAKE_IMAGE_EMPTY-}" \
|
||||
-e FAKE_GETPOD_FAIL="${FAKE_GETPOD_FAIL-}" \
|
||||
-e FAKE_APPLY_FAIL="${FAKE_APPLY_FAIL-}" \
|
||||
-e FAKE_SEED_FAIL="${FAKE_SEED_FAIL-}" \
|
||||
-e FAKE_GETJOB_FAIL="${FAKE_GETJOB_FAIL-}" \
|
||||
-e FAKE_JOBUID_EMPTY="${FAKE_JOBUID_EMPTY-}" \
|
||||
-e FAKE_PATCH_FAIL="${FAKE_PATCH_FAIL-}" \
|
||||
"$TESTIMG" "/migrations/$1" || _run_migration_rc=$?
|
||||
return "$_run_migration_rc"
|
||||
}
|
||||
|
||||
prep() {
|
||||
docker info >/dev/null 2>&1 || {
|
||||
echo "docker is required: these tests run migration 66 inside a build of" >&2
|
||||
echo "$ALPINE, the migrations image's base." >&2
|
||||
return 1
|
||||
}
|
||||
docker build -q -t "$TESTIMG" - >/dev/null <<DOCKERFILE
|
||||
FROM $ALPINE
|
||||
DOCKERFILE
|
||||
chmod +x "$FAKEBIN/kubectl" "$FAKEBIN/credentials-migrate"
|
||||
mkdir -p "$WORKROOT"
|
||||
WORK=$(mktemp -d "$WORKROOT/XXXXXX")
|
||||
export FAKE_CMDLOG="$WORK/cmdlog"
|
||||
: > "$FAKE_CMDLOG"
|
||||
export NAMESPACE=cozy-system
|
||||
export FAKE_IMAGE="$IMAGE"
|
||||
unset FAKE_IMAGE_EMPTY FAKE_GETPOD_FAIL FAKE_APPLY_FAIL FAKE_SEED_FAIL FAKE_GETJOB_FAIL FAKE_JOBUID_EMPTY FAKE_PATCH_FAIL || true
|
||||
return 0
|
||||
}
|
||||
|
||||
@test "it seeds, creates the finish Job on its own image, and stamps" {
|
||||
prep
|
||||
rc=0
|
||||
run_migration 66 >"$WORK/out" 2>&1 || rc=$?
|
||||
cat "$WORK/out"; cat "$FAKE_CMDLOG"
|
||||
[ "$rc" -eq 0 ]
|
||||
|
||||
# The seed runs for migration 66, on MariaDB, storing mysql_native_password
|
||||
# hashes, skipping users.root and asking for the watch label.
|
||||
grep -qE "^CREDMIGRATE seed --migration 66 --kind MariaDB --format mysql-native --accounts-path users --skip-account root --label k8s.mariadb.com/watch=$" "$FAKE_CMDLOG"
|
||||
|
||||
# The finish Job carries its least-privilege ClusterRole (never cluster-admin),
|
||||
# ServiceAccount and binding, the migration number, and the image this hook
|
||||
# runs as, read from its own pod rather than values.
|
||||
grep -qE "^APPLY-FINISH role=1 sa=1 crb=1 admin=0 jobname=1 migration=1 image=1 serviceaccount=1 platformkey=1 skip=1$" "$FAKE_CMDLOG"
|
||||
|
||||
# The Job owns its ServiceAccount, so the least-privilege identity is collected
|
||||
# with the Job and does not stand indefinitely.
|
||||
grep -qE "^PATCH-SA-OWNER owner=1$" "$FAKE_CMDLOG"
|
||||
|
||||
# A stale Job from an earlier run is deleted before the apply, so a rerun on a
|
||||
# newer image does not fault on the immutable pod template; the ownership is
|
||||
# set after the Job exists; and only then, after the Job is created and owned,
|
||||
# the version is stamped. A stamp before the Job would mark the migration done
|
||||
# with no finisher.
|
||||
_del=$(cmdlog_line "^DELETE-JOB$")
|
||||
_apply=$(cmdlog_line "^APPLY-FINISH ")
|
||||
_own=$(cmdlog_line "^PATCH-SA-OWNER ")
|
||||
_stamp=$(cmdlog_line "^STAMP 67$")
|
||||
[ -n "$_del" ] && [ "$_del" -lt "$_apply" ]
|
||||
[ -n "$_own" ] && [ "$_apply" -lt "$_own" ]
|
||||
[ -n "$_stamp" ] && [ "$_own" -lt "$_stamp" ]
|
||||
rm -rf "$WORK"
|
||||
}
|
||||
|
||||
@test "a pod whose image cannot be read, as a failed get, stops before the Job and the stamp" {
|
||||
prep
|
||||
export FAKE_GETPOD_FAIL="the server could not find the requested resource"
|
||||
rc=0
|
||||
run_migration 66 >"$WORK/out" 2>&1 || rc=$?
|
||||
cat "$WORK/out"; cat "$FAKE_CMDLOG"
|
||||
[ "$rc" -ne 0 ]
|
||||
grep -qE "^CREDMIGRATE seed --migration 66 " "$FAKE_CMDLOG"
|
||||
if grep -qE "^APPLY-FINISH" "$FAKE_CMDLOG"; then echo "created the Job after a failed image read" >&2; return 1; fi
|
||||
if grep -qF "STAMP" "$FAKE_CMDLOG"; then echo "stamped after a failed image read" >&2; return 1; fi
|
||||
rm -rf "$WORK"
|
||||
}
|
||||
|
||||
@test "a failed seed stops before the Job and before stamping" {
|
||||
prep
|
||||
export FAKE_SEED_FAIL="apiserver is unavailable"
|
||||
rc=0
|
||||
run_migration 66 >"$WORK/out" 2>&1 || rc=$?
|
||||
cat "$WORK/out"; cat "$FAKE_CMDLOG"
|
||||
[ "$rc" -ne 0 ]
|
||||
if grep -qE "^APPLY-FINISH" "$FAKE_CMDLOG"; then echo "created the Job after a failed seed" >&2; return 1; fi
|
||||
if grep -qF "STAMP" "$FAKE_CMDLOG"; then echo "stamped after a failed seed" >&2; return 1; fi
|
||||
rm -rf "$WORK"
|
||||
}
|
||||
|
||||
@test "a pod whose image cannot be read stops after the seed, before the Job and the stamp" {
|
||||
prep
|
||||
export FAKE_IMAGE_EMPTY=1
|
||||
rc=0
|
||||
run_migration 66 >"$WORK/out" 2>&1 || rc=$?
|
||||
cat "$WORK/out"; cat "$FAKE_CMDLOG"
|
||||
[ "$rc" -ne 0 ]
|
||||
grep -qE "^CREDMIGRATE seed --migration 66 " "$FAKE_CMDLOG"
|
||||
if grep -qE "^APPLY-FINISH" "$FAKE_CMDLOG"; then echo "created the Job with no image to run" >&2; return 1; fi
|
||||
if grep -qF "STAMP" "$FAKE_CMDLOG"; then echo "stamped with no image to run" >&2; return 1; fi
|
||||
rm -rf "$WORK"
|
||||
}
|
||||
|
||||
@test "a failed apply of the Job stops before stamping" {
|
||||
prep
|
||||
export FAKE_APPLY_FAIL="admission webhook denied the request"
|
||||
rc=0
|
||||
run_migration 66 >"$WORK/out" 2>&1 || rc=$?
|
||||
cat "$WORK/out"; cat "$FAKE_CMDLOG"
|
||||
[ "$rc" -ne 0 ]
|
||||
if grep -qF "STAMP" "$FAKE_CMDLOG"; then echo "stamped after the Job failed to apply" >&2; return 1; fi
|
||||
rm -rf "$WORK"
|
||||
}
|
||||
|
||||
@test "a Job whose uid cannot be read stops before owning the SA and before stamping" {
|
||||
prep
|
||||
export FAKE_GETJOB_FAIL="the server was unable to return a response"
|
||||
rc=0
|
||||
run_migration 66 >"$WORK/out" 2>&1 || rc=$?
|
||||
cat "$WORK/out"; cat "$FAKE_CMDLOG"
|
||||
[ "$rc" -ne 0 ]
|
||||
if grep -qE "^PATCH-SA-OWNER" "$FAKE_CMDLOG"; then echo "patched ownership with no uid" >&2; return 1; fi
|
||||
if grep -qF "STAMP" "$FAKE_CMDLOG"; then echo "stamped without owning the SA" >&2; return 1; fi
|
||||
rm -rf "$WORK"
|
||||
}
|
||||
|
||||
@test "an empty Job uid stops before owning the SA and before stamping" {
|
||||
prep
|
||||
export FAKE_JOBUID_EMPTY=1
|
||||
rc=0
|
||||
run_migration 66 >"$WORK/out" 2>&1 || rc=$?
|
||||
cat "$WORK/out"; cat "$FAKE_CMDLOG"
|
||||
[ "$rc" -ne 0 ]
|
||||
if grep -qE "^PATCH-SA-OWNER" "$FAKE_CMDLOG"; then echo "patched ownership with an empty uid" >&2; return 1; fi
|
||||
if grep -qF "STAMP" "$FAKE_CMDLOG"; then echo "stamped with an empty uid" >&2; return 1; fi
|
||||
rm -rf "$WORK"
|
||||
}
|
||||
|
||||
@test "a failed ownership patch stops before stamping" {
|
||||
prep
|
||||
export FAKE_PATCH_FAIL="serviceaccounts \"cozystack-credentials-migration-66\" not found"
|
||||
rc=0
|
||||
run_migration 66 >"$WORK/out" 2>&1 || rc=$?
|
||||
cat "$WORK/out"; cat "$FAKE_CMDLOG"
|
||||
[ "$rc" -ne 0 ]
|
||||
if grep -qF "STAMP" "$FAKE_CMDLOG"; then echo "stamped after the ownership patch failed" >&2; return 1; fi
|
||||
rm -rf "$WORK"
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
#!/bin/sh
|
||||
# Fake credentials-migrate for hack/migration-66-mariadb-credentials.bats.
|
||||
# Migration 66 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
|
||||
+114
@@ -0,0 +1,114 @@
|
||||
#!/bin/sh
|
||||
# Fake kubectl for hack/migration-66-mariadb-credentials.bats, modelled on
|
||||
# hack/testdata/migration-58-http-cache/kubectl. It serves only the calls
|
||||
# migration 66 and lib/cozystack-version.sh make, and logs each one to
|
||||
# $FAKE_CMDLOG.
|
||||
#
|
||||
# Migration 66 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-66"*)
|
||||
# The hook deletes a Job left by an earlier run before recreating it.
|
||||
log "DELETE-JOB"
|
||||
exit 0
|
||||
;;
|
||||
|
||||
*"delete serviceaccount cozystack-credentials-migration-66"*)
|
||||
# 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-66"*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-66"*)
|
||||
# The ownerReference tying the ServiceAccount to the Job.
|
||||
if [ -n "${FAKE_PATCH_FAIL:-}" ]; then
|
||||
printf '%s\n' "$FAKE_PATCH_FAIL" >&2
|
||||
exit 1
|
||||
fi
|
||||
owner=$(printf '%s' "$args" | grep -c 'cozystack-credentials-migration-66')
|
||||
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
|
||||
# the Job's own metadata is read from its document alone.
|
||||
jobname=$(printf '%s\n' "$manifest" | awk '/^kind: Job$/{f=1} f' | grep -c '^ name: cozystack-credentials-migration-66$')
|
||||
mig=$(printf '%s\n' "$manifest" | grep -c -- '--migration=66')
|
||||
img=$(printf '%s\n' "$manifest" | grep -c "^ image: ${IMAGE}$")
|
||||
sacc=$(printf '%s\n' "$manifest" | grep -c '^ serviceAccountName: cozystack-credentials-migration-66$')
|
||||
# The Job keeps root, MariaDB's platform account, in the source
|
||||
# Secret and skips a users.root entry.
|
||||
keep=$(printf '%s\n' "$manifest" | grep -c -- '--platform-key=root')
|
||||
skip=$(printf '%s\n' "$manifest" | grep -c -- '--skip-account=root')
|
||||
log "APPLY-FINISH role=$role sa=$sa crb=$crb admin=$admin jobname=$jobname migration=$mig image=$img serviceaccount=$sacc platformkey=$keep skip=$skip"
|
||||
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
|
||||
Reference in new issue
Block a user