mirror of
https://github.com/cozystack/cozystack.git
synced 2026-11-30 04:06:34 +09:00
fix(postgres): keep users.app's password across pod restarts (#4984)
## What this PR does A Postgres app that declares a user named `app` loses that user's password every time its pod restarts. Clients keep the password advertised in `<release>-credentials` and start failing with `password authentication failed for user "app"`, with nothing changed on their side. `app` is the owner role CNPG bootstraps, with a generated password in `<release>-app`, and a declared `users.app` adopts that role (the init-script says so). The init-job sets the role's password from `<release>-credentials`, but CNPG's instance manager re-applies the owner's password from the bootstrap secret whenever it meets a secret version it has not applied yet (`refreshCredentialsFromSecret` → `reconcileUser` in `internal/management/controller/instance_controller.go`). Its cache is in memory, so every pod start re-applies `<release>-app` and silently undoes the init-job. Only releases whose two secrets happen to differ break, which is every release where `users.app` was declared at creation. This PR renders a `<release>-app-owner` basic-auth Secret from the same password `<release>-credentials` holds, and sets `spec.bootstrap.<method>.secret.name` to it, so both writers agree on one value. CNPG resolves the owner secret through that field before falling back to `<release>-app`, never rewrites the data of a secret it does not own, and its webhook accepts a bootstrap change on an existing Cluster. The secret goes on the method the live Cluster was bootstrapped with, read through `lookup`, because CNPG rejects two methods and a restored Cluster keeps `bootstrap.recovery` in its live spec. A release with no `users.app` renders exactly what it rendered before, and a restore in progress (`bootstrap.enabled`) is left alone, since the chart flags its credentials as pending until the init-job runs. We hit this in production: the cluster-wide pod restart of a platform upgrade broke two of the four apps that declare `users.app`; the two others survived only because their two secrets held the same value. `tests/app_owner_secret_test.yaml` covers the shared password (preserved and freshly generated), the initdb and recovery methods, a release without `users.app`, and an in-progress restore. Without the template change, 5 of its 8 tests fail. The full chart suite passes (217 tests). ### Screenshots Not a UI change. ### Downstream repositories Walked against the diff: two templates and one test file under `packages/apps/postgres`. No package is added or renamed, `values.yaml`, `values.schema.json` and `README.md` are untouched, and no output Secret or Service is renamed (a new Secret is added; the Terraform provider reads only `-credentials`, `-rw` and `-ro`). - [x] No downstream repository is affected by this change - [ ] [cozystack/website](https://github.com/cozystack/website) - follow-up: - [ ] [cozystack/terraform-provider-cozystack](https://github.com/cozystack/terraform-provider-cozystack) - follow-up: - [ ] [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: ### Release note ```release-note fix(postgres): a user named `app` no longer loses its password when the Postgres pod restarts. CNPG re-applied its own generated password to the `app` owner role on every pod start; the chart now hands CNPG the password stored in `<release>-credentials`. ``` <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit - **New Features** - Added owner credentials when the `app` user is configured. - PostgreSQL clusters now use those credentials during initialization or recovery, while preserving compatible recovery settings for restored and actively recovering clusters. - **Bug Fixes** - Ensured generated or existing `app` passwords are reused for owner credentials. - **Tests** - Added coverage for credential reuse, password generation, omitted-user behavior, and cluster recovery scenarios. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
4 files changed
+330
No files matched your search
@@ -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 <release>-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; }
|
||||
}
|
||||
@@ -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 <release>-app, with its generated password, the
|
||||
role silently loses the password the init-job set from <release>-credentials.
|
||||
Point the bootstrap at the chart-rendered <release>-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 }}
|
||||
|
||||
@@ -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 }}
|
||||
@@ -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 <release>-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
|
||||
Reference in new issue
Block a user