|
| 1 | +#!/usr/bin/env bats |
| 2 | + |
| 3 | +# E2E for the platform-managed cozy-default BackupClass. |
| 4 | +# |
| 5 | +# Covers the parts of the design where tenants no longer supply S3 |
| 6 | +# configuration: |
| 7 | +# 1. The cozystack-installed cozy-default BackupClass exists and binds |
| 8 | +# Postgres -> cozy-default-cnpg without any tenant-side bucket/secret |
| 9 | +# setup. |
| 10 | +# 2. A Postgres BackupJob referencing cozy-default succeeds and lands a |
| 11 | +# Ready Backup artefact under the s3://cozy-backups/<ns>/<app>/ prefix |
| 12 | +# enforced by the default strategy template. |
| 13 | +# 3. The platform-projected cozy-backups-creds Secret in the tenant |
| 14 | +# namespace is not readable by the tenant ServiceAccount — the RBAC |
| 15 | +# check anchors the credentials-isolation invariant. |
| 16 | +# |
| 17 | +# Edge cases (projection retry on missing source Secret, format |
| 18 | +# normalisation accessKey -> AWS_ACCESS_KEY_ID) belong in the Go unit |
| 19 | +# tests at internal/backupcontroller/credentials_projector_test.go. |
| 20 | + |
| 21 | +TEST_NAMESPACE='tenant-test' |
| 22 | +TEST_POSTGRES_NAME='pgapp' |
| 23 | +TEST_BACKUPJOB_NAME='pg-default-job' |
| 24 | +TEST_PROJECTED_SECRET='cozy-backups-creds' |
| 25 | +SYSTEM_BUCKET_NS='cozy-backup-controller' |
| 26 | +SYSTEM_BUCKET_NAME='cozy-backups' |
| 27 | + |
| 28 | +setup_file() { |
| 29 | + kubectl -n "$TEST_NAMESPACE" delete backupjob.backups.cozystack.io --all --ignore-not-found --timeout=60s |
| 30 | + kubectl -n "$TEST_NAMESPACE" delete backup.backups.cozystack.io --all --ignore-not-found --timeout=60s |
| 31 | + kubectl -n "$TEST_NAMESPACE" delete postgres.apps.cozystack.io --all --ignore-not-found --timeout=2m |
| 32 | + kubectl -n "$TEST_NAMESPACE" delete secret "$TEST_PROJECTED_SECRET" --ignore-not-found --timeout=60s |
| 33 | +} |
| 34 | + |
| 35 | +teardown_file() { |
| 36 | + setup_file |
| 37 | +} |
| 38 | + |
| 39 | +print_log() { |
| 40 | + echo "# $1" >&3 |
| 41 | +} |
| 42 | + |
| 43 | +dump_diagnostics() { |
| 44 | + echo "# --- diagnostics ---" >&3 |
| 45 | + kubectl get backupclass cozy-default -o yaml >&3 2>&1 || true |
| 46 | + kubectl -n "$SYSTEM_BUCKET_NS" get bucket,secret >&3 2>&1 || true |
| 47 | + kubectl -n "$TEST_NAMESPACE" get postgres,cnpgcluster,backupjob,backup,secret -o wide >&3 2>&1 || true |
| 48 | + kubectl -n "$TEST_NAMESPACE" describe backupjob "$TEST_BACKUPJOB_NAME" >&3 2>&1 || true |
| 49 | + kubectl -n cozy-backupstrategy-controller logs -l app=backupstrategy-controller --tail=200 >&3 2>&1 || true |
| 50 | +} |
| 51 | + |
| 52 | +@test "Platform-managed cozy-default BackupClass and bucket exist" { |
| 53 | + kubectl get backupclass cozy-default >/dev/null |
| 54 | + kubectl -n "$SYSTEM_BUCKET_NS" get bucket "$SYSTEM_BUCKET_NAME" >/dev/null |
| 55 | + KIND_COUNT=$(kubectl get backupclass cozy-default -o jsonpath='{range .spec.strategies[*]}{.application.kind}{"\n"}{end}' | sort -u | wc -l) |
| 56 | + [ "$KIND_COUNT" -ge 3 ] |
| 57 | +} |
| 58 | + |
| 59 | +@test "Postgres BackupJob via cozy-default Succeeds without tenant S3 config" { |
| 60 | + print_log "Apply Postgres app with no chart-level backup.* values" |
| 61 | + kubectl apply -f - <<EOF |
| 62 | +apiVersion: apps.cozystack.io/v1alpha1 |
| 63 | +kind: Postgres |
| 64 | +metadata: |
| 65 | + name: ${TEST_POSTGRES_NAME} |
| 66 | + namespace: ${TEST_NAMESPACE} |
| 67 | +spec: |
| 68 | + replicas: 1 |
| 69 | + size: 1Gi |
| 70 | + resources: |
| 71 | + cpu: 100m |
| 72 | + memory: 256Mi |
| 73 | + backup: |
| 74 | + enabled: true |
| 75 | +EOF |
| 76 | + |
| 77 | + kubectl -n "$TEST_NAMESPACE" wait hr "${TEST_POSTGRES_NAME}" --for=condition=ready --timeout=600s |
| 78 | + |
| 79 | + print_log "Submit BackupJob referencing cozy-default" |
| 80 | + kubectl apply -f - <<EOF |
| 81 | +apiVersion: backups.cozystack.io/v1alpha1 |
| 82 | +kind: BackupJob |
| 83 | +metadata: |
| 84 | + name: ${TEST_BACKUPJOB_NAME} |
| 85 | + namespace: ${TEST_NAMESPACE} |
| 86 | +spec: |
| 87 | + applicationRef: |
| 88 | + apiGroup: apps.cozystack.io |
| 89 | + kind: Postgres |
| 90 | + name: ${TEST_POSTGRES_NAME} |
| 91 | + backupClassName: cozy-default |
| 92 | +EOF |
| 93 | + |
| 94 | + print_log "Wait for BackupJob phase=Succeeded" |
| 95 | + kubectl -n "$TEST_NAMESPACE" wait backupjob.backups.cozystack.io "$TEST_BACKUPJOB_NAME" \ |
| 96 | + --for=jsonpath='{.status.phase}'=Succeeded --timeout=1200s || { dump_diagnostics; false; } |
| 97 | + |
| 98 | + print_log "Inspect the projected credentials Secret" |
| 99 | + kubectl -n "$TEST_NAMESPACE" get secret "$TEST_PROJECTED_SECRET" >/dev/null |
| 100 | + AK=$(kubectl -n "$TEST_NAMESPACE" get secret "$TEST_PROJECTED_SECRET" -o jsonpath='{.data.AWS_ACCESS_KEY_ID}') |
| 101 | + SK=$(kubectl -n "$TEST_NAMESPACE" get secret "$TEST_PROJECTED_SECRET" -o jsonpath='{.data.AWS_SECRET_ACCESS_KEY}') |
| 102 | + [ -n "$AK" ] |
| 103 | + [ -n "$SK" ] |
| 104 | +} |
| 105 | + |
| 106 | +@test "Tenant ServiceAccount cannot read the projected credentials Secret" { |
| 107 | + # The projection deliberately omits the lineage tenantresource label so |
| 108 | + # the Secret is not promoted to a TenantSecret view. The default |
| 109 | + # cozy:tenant:base aggregation has zero verbs on core/v1.Secret, so |
| 110 | + # `kubectl auth can-i` MUST return no. |
| 111 | + RESULT=$(kubectl auth can-i get secret "$TEST_PROJECTED_SECRET" \ |
| 112 | + --namespace "$TEST_NAMESPACE" \ |
| 113 | + --as "system:serviceaccount:${TEST_NAMESPACE}:default" 2>/dev/null || true) |
| 114 | + [ "$RESULT" = "no" ] |
| 115 | +} |
0 commit comments