diff --git a/hack/migration-71-bucket-credentials.bats b/hack/migration-71-bucket-credentials.bats new file mode 100644 index 000000000..261afcde2 --- /dev/null +++ b/hack/migration-71-bucket-credentials.bats @@ -0,0 +1,252 @@ +#!/usr/bin/env bats +# ----------------------------------------------------------------------------- +# Unit tests for platform migration 71 --> 72 (convert Bucket users to +# platform-minted credentials). +# +# The migration marks every tenant BucketAccess the bucket chart rendered with +# helm.sh/resource-policy=keep and credentials.cozystack.io/adopt=true, so the +# chart upgrade leaves it in place and cozystack-controller adopts it with its +# keys, and marks the backup bucket's system user platform. See +# packages/core/platform/images/migrations/migrations/71. +# +# FAIL CLOSED is pinned: a failed listing or a failed mark must stop the +# migration before it stamps the version, because migrations never re-run. +# +# The harness (docker, the migrations image base, fake kubectl on PATH) is the +# one hack/migration-58-http-cache-freeze.bats describes. +# +# Run with: hack/cozytest.sh hack/migration-71-bucket-credentials.bats +# ----------------------------------------------------------------------------- + +load test_helper + +FAKEBIN="$PWD/hack/testdata/migration-71-bucket-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-migration71-test:$(printf '%s' "$ALPINE" | sed 's/[^a-zA-Z0-9]/-/g')" +WORKROOT="${TMPDIR:-/tmp}/cozy-migration-71-$$" + +# namespace name release class controller-kind. bucket-docs-public-read is a +# tenant user named public-read in a bucket without publicRead, on the base +# class of a SeaweedFS in tenant-public, which ends in -public too; +# bucket-old-public-read is an anonymous access whose class is gone. +ACCESSES='tenant-foo bucket-photos-web bucket-photos tenant-foo +tenant-foo bucket-photos-reader bucket-photos tenant-foo-readonly +tenant-foo bucket-photos-public-read bucket-photos tenant-foo-public +tenant-public bucket-docs-public-read bucket-docs tenant-public +tenant-bar bucket-old-public-read bucket-old tenant-bar-public +tenant-root bucket-cozy-backups-system bucket-cozy-backups tenant-root +tenant-foo bucket-done-app bucket-done tenant-foo CredentialTemplate +tenant-foo unrelated some-release tenant-foo ' + +# name accessPolicy, as the SeaweedFS chart renders them. +CLASSES='tenant-foo readwrite +tenant-foo-readonly readonly +tenant-foo-public public-read +tenant-public readwrite +tenant-public-readonly readonly +tenant-public-public public-read +tenant-root readwrite' + +cmdlog_line() { + grep -nE -- "$1" "$FAKE_CMDLOG" | head -1 | cut -d: -f1 +} + +cozy_cleanup() { + rm -rf "$WORKROOT" + return 0 +} + +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=cozy-system \ + -e FAKE_ACCESSES="${FAKE_ACCESSES-}" \ + -e FAKE_CLASSES="${FAKE_CLASSES-}" \ + -e FAKE_CLASS_FAIL="${FAKE_CLASS_FAIL-}" \ + -e FAKE_BACKUP_NS="${FAKE_BACKUP_NS-}" \ + -e FAKE_BACKUP_BUCKET="${FAKE_BACKUP_BUCKET-}" \ + -e FAKE_NO_SYSTEM="${FAKE_NO_SYSTEM-}" \ + -e FAKE_NO_CRD="${FAKE_NO_CRD-}" \ + -e FAKE_LIST_FAIL="${FAKE_LIST_FAIL-}" \ + -e FAKE_LABEL_FAIL="${FAKE_LABEL_FAIL-}" \ + -e FAKE_KEYLESS="${FAKE_KEYLESS-}" \ + -e FAKE_STRIP_FAIL="${FAKE_STRIP_FAIL-}" \ + -e FAKE_CRD_FAIL="${FAKE_CRD_FAIL-}" \ + -e CREDENTIAL_CRD_FILE=/work/credentialtemplates.yaml \ + "$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 71 inside a build of" >&2 + echo "$ALPINE, the migrations image's base." >&2 + return 1 + } + docker build -q -t "$TESTIMG" - >/dev/null < "$FAKE_CMDLOG" + export FAKE_ACCESSES="$ACCESSES" + export FAKE_CLASSES="$CLASSES" + unset FAKE_CLASS_FAIL FAKE_BACKUP_NS FAKE_BACKUP_BUCKET FAKE_NO_SYSTEM FAKE_NO_CRD FAKE_LIST_FAIL FAKE_LABEL_FAIL FAKE_CRD_FAIL FAKE_KEYLESS FAKE_STRIP_FAIL || true + return 0 +} + +@test "it marks tenant BucketAccesses, the backup user platform, and stamps" { + prep + rc=0 + run_migration 71 >"$WORK/out" 2>&1 || rc=$? + cat "$WORK/out"; cat "$FAKE_CMDLOG" + [ "$rc" -eq 0 ] + + grep -qxF "PATCH-PLATFORM tenant-root/bucket-cozy-backups platform=1" "$FAKE_CMDLOG" + # The CRD with spec.bucketAccess is applied, owned by the + # cozystack-controller release, and established before anything else. + _crd=$(cmdlog_line "^CRD established$") + _platform=$(cmdlog_line "^PATCH-PLATFORM ") + [ -n "$_crd" ] && [ "$_crd" -lt "$_platform" ] + grep -qxF "CRD label" "$FAKE_CMDLOG" + grep -qxF "CRD annotate" "$FAKE_CMDLOG" + for name in tenant-foo/bucket-photos-web tenant-foo/bucket-photos-reader tenant-public/bucket-docs-public-read; do + grep -qxF "KEEP $name keep=1" "$FAKE_CMDLOG" + grep -qxF "ADOPT $name adopt=1" "$FAKE_CMDLOG" + done + grep -qxF "STRIP tenant-public/bucket-docs-public-read-credentials strip=1" "$FAKE_CMDLOG" + # The anonymous public-read access has no keys, whether its class is there + # or gone; the backup user stays a platform account, an adopted + # BucketAccess is left alone, and nothing a bucket chart did not render is + # touched. + for name in bucket-photos-public-read bucket-old-public-read bucket-cozy-backups-system bucket-done-app unrelated; do + if grep -qE "^(KEEP|ADOPT) [^ ]*/$name " "$FAKE_CMDLOG"; then echo "marked $name" >&2; return 1; fi + done + # Each tenant user's readable Secret loses its keys; the platform user's + # keeps them, and nothing reads the key values out. + for name in bucket-photos-web bucket-photos-reader bucket-done-app; do + grep -qxF "STRIP tenant-foo/$name-credentials strip=1" "$FAKE_CMDLOG" + done + if grep -qE "^STRIP [^ ]*/bucket-cozy-backups-system-credentials " "$FAKE_CMDLOG"; then echo "stripped the platform user's keys" >&2; return 1; fi + if grep -qE "^READ-KEYS " "$FAKE_CMDLOG"; then echo "read the key values" >&2; return 1; fi + _strip=$(cmdlog_line "^STRIP ") + _stamp=$(cmdlog_line "^STAMP 72$") + [ -n "$_strip" ] && [ "$_strip" -lt "$_stamp" ] + rm -rf "$WORK" +} + +@test "a Secret that holds no keys is not patched" { + prep + export FAKE_ACCESSES='tenant-foo bucket-photos-web bucket-photos tenant-foo ' + export FAKE_KEYLESS=bucket-photos-web-credentials + rc=0 + run_migration 71 >"$WORK/out" 2>&1 || rc=$? + cat "$WORK/out"; cat "$FAKE_CMDLOG" + [ "$rc" -eq 0 ] + if grep -qE "^STRIP " "$FAKE_CMDLOG"; then echo "patched a keyless Secret" >&2; return 1; fi + grep -qxF "ADOPT tenant-foo/bucket-photos-web adopt=1" "$FAKE_CMDLOG" + rm -rf "$WORK" +} + +@test "a failed strip stops before stamping" { + prep + export FAKE_STRIP_FAIL="secrets is forbidden" + rc=0 + run_migration 71 >"$WORK/out" 2>&1 || rc=$? + cat "$WORK/out"; cat "$FAKE_CMDLOG" + [ "$rc" -ne 0 ] + if grep -qF "STAMP" "$FAKE_CMDLOG"; then echo "stamped after a failed strip" >&2; return 1; fi + rm -rf "$WORK" +} + +@test "it finds the backup bucket where backupstrategy-controller provisions it" { + prep + export FAKE_BACKUP_NS=tenant-ops FAKE_BACKUP_BUCKET=backups + export FAKE_ACCESSES='tenant-ops bucket-backups-system bucket-backups tenant-ops ' + rc=0 + run_migration 71 >"$WORK/out" 2>&1 || rc=$? + cat "$WORK/out"; cat "$FAKE_CMDLOG" + [ "$rc" -eq 0 ] + grep -qxF "PATCH-PLATFORM tenant-ops/bucket-backups platform=1" "$FAKE_CMDLOG" + if grep -qE "^(KEEP|ADOPT) " "$FAKE_CMDLOG"; then echo "marked the backup user" >&2; return 1; fi + rm -rf "$WORK" +} + +@test "a backup release without a system user is not given one" { + prep + export FAKE_NO_SYSTEM=1 + rc=0 + run_migration 71 >"$WORK/out" 2>&1 || rc=$? + cat "$WORK/out"; cat "$FAKE_CMDLOG" + [ "$rc" -eq 0 ] + if grep -qE "^PATCH-PLATFORM" "$FAKE_CMDLOG"; then echo "patched a release without a system user" >&2; return 1; fi + rm -rf "$WORK" +} + +@test "a cluster without the COSI API stamps without listing" { + prep + export FAKE_NO_CRD=1 + rc=0 + run_migration 71 >"$WORK/out" 2>&1 || rc=$? + cat "$WORK/out"; cat "$FAKE_CMDLOG" + [ "$rc" -eq 0 ] + if grep -qF "get bucketaccesses" "$FAKE_CMDLOG"; then echo "listed BucketAccesses without the CRD" >&2; return 1; fi + grep -qxF "STAMP 72" "$FAKE_CMDLOG" + rm -rf "$WORK" +} + +@test "a failed listing stops before stamping" { + prep + export FAKE_LIST_FAIL="the server was unable to return a response" + rc=0 + run_migration 71 >"$WORK/out" 2>&1 || rc=$? + cat "$WORK/out"; cat "$FAKE_CMDLOG" + [ "$rc" -ne 0 ] + if grep -qF "STAMP" "$FAKE_CMDLOG"; then echo "stamped after a failed listing" >&2; return 1; fi + rm -rf "$WORK" +} + +@test "a failed class listing stops before any mark and the stamp" { + prep + export FAKE_CLASS_FAIL="the server was unable to return a response" + rc=0 + run_migration 71 >"$WORK/out" 2>&1 || rc=$? + cat "$WORK/out"; cat "$FAKE_CMDLOG" + [ "$rc" -ne 0 ] + if grep -qE "^(KEEP|ADOPT|STRIP) " "$FAKE_CMDLOG"; then echo "marked without the classes" >&2; return 1; fi + if grep -qF "STAMP" "$FAKE_CMDLOG"; then echo "stamped after a failed class listing" >&2; return 1; fi + rm -rf "$WORK" +} + +@test "a failed mark stops before stamping" { + prep + export FAKE_LABEL_FAIL="admission webhook denied the request" + rc=0 + run_migration 71 >"$WORK/out" 2>&1 || rc=$? + cat "$WORK/out"; cat "$FAKE_CMDLOG" + [ "$rc" -ne 0 ] + if grep -qF "STAMP" "$FAKE_CMDLOG"; then echo "stamped after a failed mark" >&2; return 1; fi + rm -rf "$WORK" +} + +@test "a CRD that fails to apply stops before any mark and the stamp" { + prep + export FAKE_CRD_FAIL=1 + rc=0 + run_migration 71 >"$WORK/out" 2>&1 || rc=$? + cat "$WORK/out"; cat "$FAKE_CMDLOG" + [ "$rc" -ne 0 ] + if grep -qE "^(PATCH-PLATFORM|KEEP|ADOPT) " "$FAKE_CMDLOG"; then echo "marked without the CRD" >&2; return 1; fi + if grep -qF "STAMP" "$FAKE_CMDLOG"; then echo "stamped without the CRD" >&2; return 1; fi + rm -rf "$WORK" +} diff --git a/hack/testdata/migration-71-bucket-credentials/kubectl b/hack/testdata/migration-71-bucket-credentials/kubectl new file mode 100755 index 000000000..ed8717034 --- /dev/null +++ b/hack/testdata/migration-71-bucket-credentials/kubectl @@ -0,0 +1,111 @@ +#!/bin/sh +# Fake kubectl for hack/migration-71-bucket-credentials.bats, modelled on +# hack/testdata/migration-64-postgres-credentials/kubectl. It serves only the +# calls migration 71, lib/credential-crd.sh and lib/cozystack-version.sh +# make, and logs each one to $FAKE_CMDLOG. +# +# FAKE_CMDLOG file to append a one-line record of each call to +# FAKE_BACKUP_NS backupStorage.namespace of backupstrategy-controller +# FAKE_BACKUP_BUCKET backupStorage.bucketName of backupstrategy-controller +# FAKE_NO_SYSTEM non-empty => the backup Bucket release has no system user +# FAKE_NO_CRD non-empty => the BucketAccess CRD is not installed +# FAKE_ACCESSES the BucketAccess listing, one line per object: +# namespace name release class controller-kind +# FAKE_CLASSES the BucketAccessClass listing, one line per object: +# name accessPolicy +# FAKE_CLASS_FAIL non-empty => listing BucketAccessClasses exits 1 +# FAKE_LIST_FAIL non-empty => listing BucketAccesses exits 1 +# FAKE_LABEL_FAIL non-empty => labelling a BucketAccess exits 1 +# FAKE_KEYLESS space-separated Secret names that hold no keys +# FAKE_STRIP_FAIL non-empty => patching a credentials Secret exits 1 +# FAKE_CRD_FAIL non-empty => applying the CredentialTemplate CRD exits 1 +set -u + +log() { [ -n "${FAKE_CMDLOG:-}" ] && printf '%s\n' "$*" >> "$FAKE_CMDLOG"; } +log "KUBECTL $*" + +args="$*" +ns_of() { printf '%s\n' "$args" | sed -n 's/.*--namespace \([^ ]*\).*/\1/p'; } + +case "$args" in + *"apply --server-side --force-conflicts --filename "*credentialtemplates*) + log "CRD apply" + [ -n "${FAKE_CRD_FAIL-}" ] && exit 1 + exit 0 ;; + *"label crd credentialtemplates.cozystack.io app.kubernetes.io/managed-by=Helm"*) + log "CRD label"; exit 0 ;; + *"annotate crd credentialtemplates.cozystack.io"*"meta.helm.sh/release-name=cozystack-controller"*"meta.helm.sh/release-namespace=cozy-system"*) + log "CRD annotate"; exit 0 ;; + *"wait --for=condition=established crd/credentialtemplates.cozystack.io"*) + log "CRD established"; exit 0 ;; + *"get helmreleases.helm.toolkit.fluxcd.io backupstrategy-controller"*"backupStorage.namespace"*) + printf '%s' "${FAKE_BACKUP_NS:-}"; exit 0 ;; + *"get helmreleases.helm.toolkit.fluxcd.io backupstrategy-controller"*"backupStorage.bucketName"*) + printf '%s' "${FAKE_BACKUP_BUCKET:-}"; exit 0 ;; + *"get helmreleases.helm.toolkit.fluxcd.io backupstrategy-controller"*"backupStorage.provisionBucket"*) + exit 0 ;; + *"get helmreleases.helm.toolkit.fluxcd.io bucket-"*"users.system"*) + [ -n "${FAKE_NO_SYSTEM:-}" ] || printf '%s' '{"readonly":false}' + exit 0 ;; + *"patch helmreleases.helm.toolkit.fluxcd.io "*) + name=$(printf '%s\n' "$args" | sed -n 's/.*patch helmreleases.helm.toolkit.fluxcd.io \([^ ]*\).*/\1/p') + platform=$(printf '%s\n' "$args" | grep -c '"system":{"platform":true}') + log "PATCH-PLATFORM $(ns_of)/$name platform=$platform" + exit 0 ;; + *"get customresourcedefinitions.apiextensions.k8s.io bucketaccesses.objectstorage.k8s.io"*) + [ -n "${FAKE_NO_CRD:-}" ] || printf '%s\n' "customresourcedefinition.apiextensions.k8s.io/bucketaccesses.objectstorage.k8s.io" + exit 0 ;; + *"get bucketaccessclasses.objectstorage.k8s.io "*"parameters.accessPolicy"*) + if [ -n "${FAKE_CLASS_FAIL:-}" ]; then + printf '%s\n' "$FAKE_CLASS_FAIL" >&2; exit 1 + fi + printf '%s\n' "${FAKE_CLASSES:-}" + exit 0 ;; + *"get bucketaccesses.objectstorage.k8s.io --all-namespaces"*"spec.bucketAccessClassName"*) + if [ -n "${FAKE_LIST_FAIL:-}" ]; then + printf '%s\n' "$FAKE_LIST_FAIL" >&2; exit 1 + fi + printf '%s\n' "${FAKE_ACCESSES:-}" + exit 0 ;; + *"get secrets "*"go-template="*) + name=$(printf '%s\n' "$args" | sed -n 's/.*get secrets \([^ ]*\).*/\1/p') + # The check must not read the key values out. + case "$args" in *"jsonpath"*) log "READ-KEYS $name"; exit 1 ;; esac + case " ${FAKE_KEYLESS:-} " in *" $name "*) exit 0 ;; esac + printf '%s' held + exit 0 ;; + *"patch secrets "*) + if [ -n "${FAKE_STRIP_FAIL:-}" ]; then + printf '%s\n' "$FAKE_STRIP_FAIL" >&2; exit 1 + fi + name=$(printf '%s\n' "$args" | sed -n 's/.*patch secrets \([^ ]*\).*/\1/p') + strip=$(printf '%s\n' "$args" | grep -c '"accessKey":null,"secretKey":null') + log "STRIP $(ns_of)/$name strip=$strip" + exit 0 ;; + *"annotate bucketaccesses.objectstorage.k8s.io "*) + name=$(printf '%s\n' "$args" | sed -n 's/.*annotate bucketaccesses.objectstorage.k8s.io \([^ ]*\).*/\1/p') + keep=$(printf '%s\n' "$args" | grep -c 'helm.sh/resource-policy=keep') + log "KEEP $(ns_of)/$name keep=$keep" + exit 0 ;; + *"label bucketaccesses.objectstorage.k8s.io "*) + if [ -n "${FAKE_LABEL_FAIL:-}" ]; then + printf '%s\n' "$FAKE_LABEL_FAIL" >&2; exit 1 + fi + name=$(printf '%s\n' "$args" | sed -n 's/.*label bucketaccesses.objectstorage.k8s.io \([^ ]*\).*/\1/p') + adopt=$(printf '%s\n' "$args" | grep -c 'credentials.cozystack.io/adopt=true') + log "ADOPT $(ns_of)/$name adopt=$adopt" + 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 ;; + esac + ;; +esac + +log "UNHANDLED $args" +printf 'fake kubectl: unmodelled call: %s\n' "$args" >&2 +exit 1 diff --git a/packages/core/platform/images/migrations/migrations/71 b/packages/core/platform/images/migrations/migrations/71 new file mode 100755 index 000000000..b7b796d98 --- /dev/null +++ b/packages/core/platform/images/migrations/migrations/71 @@ -0,0 +1,173 @@ +#!/bin/sh +# Migration 71 --> 72 +# Convert Bucket users to platform-minted credentials. +# +# The converted bucket chart stops rendering a tenant user's BucketAccess and +# renders an engine-issued CredentialTemplate in its place; +# cozystack-controller then owns the BucketAccess and re-creates it on a mint. +# A BucketAccess's keys are bound to its UID (the COSI sidecar names the driver +# account after it), so a deleted and re-created BucketAccess is a new key +# pair. This hook runs before any component chart changes version and keeps +# every existing user's keys: +# +# - each tenant BucketAccess the bucket chart rendered gets +# helm.sh/resource-policy=keep, which Helm reads off the live object +# before it deletes a resource the new chart no longer renders, so the +# upgrade leaves it in place; and credentials.cozystack.io/adopt=true, which +# lets the controller take it over and copy the keys it already has into +# the account Secret. Clients keep working until the first mint. +# +# - the CredentialTemplate CRD baked into this image is applied first. +# The converted chart renders spec.bucketAccess, which a cluster that ran +# migration 64 in an earlier release lacks until cozystack-controller +# upgrades, possibly after the bucket releases switch: the API server +# would prune the field, and the controller would skip the template. +# +# - the backup bucket's system user is marked platform in its Bucket +# release, so it keeps its chart-rendered BucketAccess and its readable +# Secret whichever of the bucket chart and backupstrategy-controller is +# upgraded first. Its BucketAccess is not marked. +# +# - each tenant user's --credentials loses its accessKey and +# secretKey, which the tenant can read. The converted bucket-system chart +# renders that Secret without them, but its upgrade cannot remove them: +# the old chart wrote them through stringData, so they are absent from the +# manifest Helm diffs against, and a three-way merge keeps a live field +# that neither manifest names (helm v3.20 pkg/kube/client.go createPatch, +# strategicpatch.CreateThreeWayMergePatch). Done here, before the chart +# switch, the keys are gone however the release is upgraded. A platform +# user's Secret keeps them. +# +# There is no finish Job and so no standing identity: once the keys are +# stripped here, the COSI Secret the sidecar writes is the only other copy, +# and it has to stay for the grant. The keep annotation is +# left on adopted BucketAccesses: Helm no longer tracks them, and the garbage +# collector removes them with their template, which the annotation does not +# affect. +# +# Fail-closed: migrations never re-run, so an error here stops the upgrade +# rather than stamping the version past a BucketAccess that was not marked. + +set -euo pipefail + +# shellcheck source=lib/cozystack-version.sh +. "$(dirname "$0")/lib/cozystack-version.sh" +# shellcheck source=lib/credential-crd.sh +. "$(dirname "$0")/lib/credential-crd.sh" + +apply_credential_crd + +# Where backupstrategy-controller provisions the backup bucket, from its +# release values when they override the chart defaults. +BACKUP_RELEASE_NAMESPACE="${BACKUP_RELEASE_NAMESPACE:-cozy-backup-controller}" +BACKUP_RELEASE_NAME="${BACKUP_RELEASE_NAME:-backupstrategy-controller}" + +backup_value() { + kubectl get helmreleases.helm.toolkit.fluxcd.io "$BACKUP_RELEASE_NAME" \ + --namespace "$BACKUP_RELEASE_NAMESPACE" \ + --ignore-not-found \ + --output "jsonpath={.spec.values.backupStorage.$1}" +} + +platform_namespace=$(backup_value namespace) +platform_bucket=$(backup_value bucketName) +provision=$(backup_value provisionBucket) +platform_namespace="${platform_namespace:-tenant-root}" +platform_bucket="${platform_bucket:-cozy-backups}" +platform_release="bucket-${platform_bucket}" +platform_access="${platform_release}-system" + +# Only a release that has the system user: a merge patch would otherwise add +# one to a bucket that never declared it. +if [ "$provision" != "false" ]; then + system_user=$(kubectl get helmreleases.helm.toolkit.fluxcd.io "$platform_release" \ + --namespace "$platform_namespace" \ + --ignore-not-found \ + --output 'jsonpath={.spec.values.users.system}') + if [ -n "$system_user" ]; then + kubectl patch helmreleases.helm.toolkit.fluxcd.io "$platform_release" \ + --namespace "$platform_namespace" \ + --type merge \ + --patch '{"spec":{"values":{"users":{"system":{"platform":true}}}}}' + fi +fi + +# A cluster without the COSI API has no BucketAccess to keep. +crd=$(kubectl get customresourcedefinitions.apiextensions.k8s.io \ + bucketaccesses.objectstorage.k8s.io \ + --ignore-not-found \ + --output name) +if [ -z "$crd" ]; then + stamp_cozystack_version 72 + exit 0 +fi + +# One line per BucketAccessClass: name and access policy. The anonymous +# public-read access is told by its class, never by its name: with publicRead +# off a tenant user may be called public-read, and its keyed BucketAccess has +# the same name the anonymous one would. +classes=$(kubectl get bucketaccessclasses.objectstorage.k8s.io \ + --output 'jsonpath={range .items[*]}{.metadata.name}{" "}{.parameters.accessPolicy}{"\n"}{end}') + +# Whether a BucketAccess of this class is the keyless public-read access: its +# class grants the public-read policy, or, gone since, carries the -public +# suffix the SeaweedFS chart gives only that class. +public_class() { + _policy=$(printf '%s\n' "$classes" | awk -v class="$1" ' + $1 == class { found = 1; policy = $2 } + END { if (found) print (policy == "" ? "-" : policy) }') + case "$_policy" in + public-read) return 0 ;; + "") case "$1" in *-public) return 0 ;; esac ;; + esac + return 1 +} + +# One line per BucketAccess: namespace, name, the Helm release that rendered +# it, its class, and the kind of its controller, if any. +accesses=$(kubectl get bucketaccesses.objectstorage.k8s.io \ + --all-namespaces \ + --output 'jsonpath={range .items[*]}{.metadata.namespace}{" "}{.metadata.name}{" "}{.metadata.annotations.meta\.helm\.sh/release-name}{" "}{.spec.bucketAccessClassName}{" "}{.metadata.ownerReferences[?(@.controller==true)].kind}{"\n"}{end}') + +printf '%s\n' "$accesses" | while read -r namespace name release class controller; do + [ -n "$name" ] || continue + # Only what a bucket chart rendered for one of its users. + case "$release" in bucket-*) ;; *) continue ;; esac + case "$name" in "${release}"-?*) ;; *) continue ;; esac + # The anonymous public-read access has no keys and stays chart-rendered. + if public_class "$class"; then + continue + fi + # The backup bucket's system user stays a chart-rendered platform account. + if [ "$namespace" = "$platform_namespace" ] && [ "$name" = "$platform_access" ]; then + continue + fi + # Strip the keys from the tenant-readable Secret, checked without reading + # them, and patched only when one is there: a merge patch of null removes a + # key and leaves endpoint and bucketName in place. + secret="${name}-credentials" + held=$(kubectl get secrets "$secret" \ + --namespace "$namespace" \ + --ignore-not-found \ + --output 'go-template={{with .data}}{{if or (index . "accessKey") (index . "secretKey")}}held{{end}}{{end}}') + if [ -n "$held" ]; then + kubectl patch secrets "$secret" \ + --namespace "$namespace" \ + --type merge \ + --patch '{"data":{"accessKey":null,"secretKey":null}}' + echo "removed the keys from Secret $namespace/$secret" + fi + # Already taken over. + [ -z "$controller" ] || continue + kubectl annotate bucketaccesses.objectstorage.k8s.io "$name" \ + --namespace "$namespace" \ + --overwrite \ + helm.sh/resource-policy=keep + kubectl label bucketaccesses.objectstorage.k8s.io "$name" \ + --namespace "$namespace" \ + --overwrite \ + credentials.cozystack.io/adopt=true + echo "marked BucketAccess $namespace/$name for adoption" +done + +stamp_cozystack_version 72 diff --git a/packages/core/platform/values.yaml b/packages/core/platform/values.yaml index 82cefcf1e..9126f5a5e 100644 --- a/packages/core/platform/values.yaml +++ b/packages/core/platform/values.yaml @@ -89,7 +89,7 @@ migrations: # refuses to advance past a migration file missing from the image (exit 1), so # a stale pin fails loudly rather than silently skipping the etcd adoption. image: ghcr.io/cozystack/cozystack/platform-migrations:v1.6.0@sha256:6777042e3e9c6bad76e7d96fb64baa115061975f39061cb3ede84e21d0e2213f - targetVersion: 71 + targetVersion: 72 # Adopt legacy etcd clusters onto the v1alpha2 operator WITHOUT the mandatory # pre-adoption safety snapshot (migration 50). Leave false unless migration 50 # has actually refused to proceed and you accept the risk: adoption rewires