diff --git a/hack/check-postgres-app-owner-password.bats b/hack/check-postgres-app-owner-password.bats new file mode 100644 index 000000000..724bb714c --- /dev/null +++ b/hack/check-postgres-app-owner-password.bats @@ -0,0 +1,25 @@ +#!/usr/bin/env bats +# Unit test: on a fresh install, packages/apps/postgres hands CNPG the same +# users.app password it stores in -credentials. +# +# The password is generated at render time, so the two Secrets can only be +# compared within one render. helm-unittest asserts each document against a +# fixed value and has no way to compare one document with another, so a chart +# generating two independent passwords would still pass the chart's own suite. + +load test_helper + +REPO_ROOT="$(cd "$(dirname "${BATS_TEST_FILENAME:-$0}")/.." && pwd)" +CHART="$REPO_ROOT/packages/apps/postgres" + +@test "a fresh users.app gets one generated password in both Secrets" { + rendered="$BATS_TEST_TMPDIR/init-script.yaml" + helm template pg "$CHART" --namespace tenant-test \ + --set-json '_cluster={"scheduling":{}}' --set-json 'users={"app":{}}' \ + --show-only templates/init-script.yaml > "$rendered" + + app=$(yq -e 'select(.kind == "Secret" and .metadata.name == "pg-credentials").stringData.app' "$rendered") + owner=$(yq -e 'select(.kind == "Secret" and .metadata.name == "pg-app-owner").stringData.password' "$rendered") + [ -n "$app" ] || { echo "no generated password for app in pg-credentials" >&2; return 1; } + [ "$app" = "$owner" ] || { printf 'pg-credentials app: %s\npg-app-owner password: %s\n' "$app" "$owner" >&2; return 1; } +} diff --git a/packages/apps/postgres/templates/db.yaml b/packages/apps/postgres/templates/db.yaml index 8538b326b..4f9cd00e4 100644 --- a/packages/apps/postgres/templates/db.yaml +++ b/packages/apps/postgres/templates/db.yaml @@ -260,6 +260,29 @@ spec: # cluster's name — which would read the wrong S3 prefix on restore. barmanObjectName: {{ .Release.Name }}-recovery serverName: {{ .Values.bootstrap.serverName | default .Values.bootstrap.oldName }} + {{- else if hasKey (.Values.users | default dict) "app" }} + {{- /* + "app" is the owner CNPG bootstraps, and users.app adopts that role. CNPG's + instance manager re-applies the owner's password from the bootstrap secret + on every reconcile whose secret version it has not seen, so on every pod + restart: left on CNPG's own -app, with its generated password, the + role silently loses the password the init-job set from -credentials. + Point the bootstrap at the chart-rendered -app-owner, which holds that + same password. It goes on the method the live Cluster was bootstrapped with, + because CNPG rejects a second one, and a restored Cluster keeps its recovery + block. Changing bootstrap on an existing Cluster is accepted, and the + remaining fields come from the live object or CNPG's defaults (app/app). + A release turning bootstrap.enabled off still drops recovery.source and + recoveryTarget: they are read only at bootstrap, and CNPG's webhook + rejects a source once its externalClusters entry, rendered only with + bootstrap.enabled, is gone. + */}} + {{- $live := lookup "postgresql.cnpg.io/v1" "Cluster" .Release.Namespace .Release.Name }} + {{- $liveBootstrap := dig "spec" "bootstrap" dict ($live | default dict) }} + bootstrap: + {{ ternary "recovery" "initdb" (hasKey $liveBootstrap "recovery") }}: + secret: + name: {{ .Release.Name }}-app-owner {{- end }} resources: {{- include "cozy-lib.resources.defaultingSanitize" (list .Values.resourcesPreset .Values.resources $) | nindent 4 }} diff --git a/packages/apps/postgres/templates/init-script.yaml b/packages/apps/postgres/templates/init-script.yaml index 6306a5fae..8787f1961 100644 --- a/packages/apps/postgres/templates/init-script.yaml +++ b/packages/apps/postgres/templates/init-script.yaml @@ -599,3 +599,15 @@ stringData: EOT {{- end }} {{- end }} +{{- if hasKey (.Values.users | default dict) "app" }} +--- +{{- /* The bootstrap secret db.yaml gives CNPG for its "app" owner; see there. */}} +apiVersion: v1 +kind: Secret +metadata: + name: {{ .Release.Name }}-app-owner +type: kubernetes.io/basic-auth +stringData: + username: app + password: {{ quote (index $passwords "app") }} +{{- end }} diff --git a/packages/apps/postgres/tests/app_owner_secret_test.yaml b/packages/apps/postgres/tests/app_owner_secret_test.yaml new file mode 100644 index 000000000..65a5fd571 --- /dev/null +++ b/packages/apps/postgres/tests/app_owner_secret_test.yaml @@ -0,0 +1,270 @@ +suite: users.app keeps its password across pod restarts + +# "app" is the owner CNPG bootstraps, and CNPG's instance manager re-applies the +# owner's password from the bootstrap secret on every pod restart. A declared +# users.app must therefore hand CNPG a secret holding the very password stored +# in -credentials, or the role reverts to CNPG's generated one. + +release: + name: pg-test + namespace: tenant-test + +set: + _cluster: + scheduling: {} + +tests: + - it: gives CNPG the password users.app has in the credentials Secret + templates: + - templates/init-script.yaml + kubernetesProvider: + scheme: + "v1/Secret": + gvr: + version: v1 + resource: secrets + namespaced: true + objects: + - apiVersion: v1 + kind: Secret + metadata: + name: pg-test-credentials + namespace: tenant-test + data: + app: Y29uc29sZS1wYXNzd29yZA== + set: + users: + app: {} + asserts: + - equal: + path: stringData.app + value: console-password + documentSelector: + path: metadata.name + value: pg-test-credentials + - equal: + path: stringData.password + value: console-password + documentSelector: + path: metadata.name + value: pg-test-app-owner + - equal: + path: stringData.username + value: app + documentSelector: + path: metadata.name + value: pg-test-app-owner + - equal: + path: type + value: kubernetes.io/basic-auth + documentSelector: + path: metadata.name + value: pg-test-app-owner + + # Both Secrets sharing this password is checked by + # hack/check-postgres-app-owner-password.bats, which compares them in one render. + - it: generates a 16-character password for a fresh app owner + templates: + - templates/init-script.yaml + set: + users: + app: {} + asserts: + - matchRegex: + path: stringData.password + pattern: ^[a-zA-Z0-9]{16}$ + documentSelector: + path: metadata.name + value: pg-test-app-owner + + - it: renders no owner secret when no user is named app + templates: + - templates/init-script.yaml + set: + users: + other: {} + asserts: + # pg-test-credentials and pg-test-init-script only + - hasDocuments: + count: 2 + + - it: points a new Cluster's initdb at the owner secret + templates: + - templates/db.yaml + set: + users: + app: {} + documentSelector: + path: kind + value: Cluster + asserts: + - equal: + path: spec.bootstrap + value: + initdb: + secret: + name: pg-test-app-owner + + - it: points a live initdb Cluster at the owner secret + templates: + - templates/db.yaml + kubernetesProvider: + scheme: + "postgresql.cnpg.io/v1/Cluster": + gvr: + group: postgresql.cnpg.io + version: v1 + resource: clusters + namespaced: true + objects: + - apiVersion: postgresql.cnpg.io/v1 + kind: Cluster + metadata: + name: pg-test + namespace: tenant-test + spec: + bootstrap: + initdb: + database: app + owner: app + set: + users: + app: {} + documentSelector: + path: kind + value: Cluster + asserts: + - equal: + path: spec.bootstrap.initdb.secret.name + value: pg-test-app-owner + - notExists: + path: spec.bootstrap.recovery + + - it: keeps a restored Cluster on its recovery method + # CNPG rejects a Cluster with two bootstrap methods, and a Cluster restored + # from a backup keeps bootstrap.recovery in its live spec. + templates: + - templates/db.yaml + kubernetesProvider: + scheme: + "postgresql.cnpg.io/v1/Cluster": + gvr: + group: postgresql.cnpg.io + version: v1 + resource: clusters + namespaced: true + objects: + - apiVersion: postgresql.cnpg.io/v1 + kind: Cluster + metadata: + name: pg-test + namespace: tenant-test + spec: + bootstrap: + recovery: + database: app + owner: app + source: pg-old + set: + users: + app: {} + documentSelector: + path: kind + value: Cluster + asserts: + - equal: + path: spec.bootstrap.recovery.secret.name + value: pg-test-app-owner + - notExists: + path: spec.bootstrap.initdb + + - it: leaves bootstrap unrendered when no user is named app + templates: + - templates/db.yaml + set: + users: + other: {} + documentSelector: + path: kind + value: Cluster + asserts: + - notExists: + path: spec.bootstrap + + - it: leaves an in-progress restore's recovery block untouched + # While bootstrap.enabled is on the init-job is skipped and the credentials + # are flagged pending, so the owner password is not taken over mid-restore. + templates: + - templates/db.yaml + set: + users: + app: {} + backup: + enabled: true + useSystemBucket: false + s3AccessKey: AKIAEXAMPLE + s3SecretKey: s3cr3t + destinationPath: s3://bucket/pg-test/ + endpointURL: https://s3.example:9000 + bootstrap: + enabled: true + oldName: pg-old + serverName: pg-old + documentSelector: + path: kind + value: Cluster + asserts: + - equal: + path: spec.bootstrap.recovery.source + value: pg-old + - notExists: + path: spec.bootstrap.recovery.secret + + - it: drops recovery.source with its external cluster once bootstrap.enabled is off + # A source naming an absent externalClusters entry is rejected by CNPG's + # webhook, and both are read only at bootstrap. + templates: + - templates/db.yaml + kubernetesProvider: + scheme: + "postgresql.cnpg.io/v1/Cluster": + gvr: + group: postgresql.cnpg.io + version: v1 + resource: clusters + namespaced: true + objects: + - apiVersion: postgresql.cnpg.io/v1 + kind: Cluster + metadata: + name: pg-test + namespace: tenant-test + spec: + bootstrap: + recovery: + database: app + owner: app + source: pg-old + recoveryTarget: + backupID: 20261019T000000 + externalClusters: + - name: pg-old + set: + users: + app: {} + bootstrap: + enabled: false + oldName: pg-old + backupID: 20261019T000000 + documentSelector: + path: kind + value: Cluster + asserts: + - equal: + path: spec.bootstrap + value: + recovery: + secret: + name: pg-test-app-owner + - notExists: + path: spec.externalClusters