Files
cozystack/packages/apps/postgres/tests/preflight_test.yaml
T
a67fa94fdb feat(postgres)!: issue user passwords through the credentials API
The chart generated every user's password at render time into
<release>-credentials, which tenants could read again at any time and
which Helm history kept for MaxHistory revisions, so a deployment whose
policy requires a credential shown once could not run PostgreSQL here.

The chart now renders a CredentialTemplate per user, and the users move
under CloudNativePG's managed roles, each pointing at its account
Secret, so the operator creates the role with the SCRAM verifier the
Secret holds, applies a changed Secret live, and reverts a password set
inside PostgreSQL. Every role is rendered as the init-script created it,
because the operator reverts an attribute that differs, revokes a parent
role missing from inRoles and clears a comment the spec leaves unset.
The init-script stops creating, passwording and altering accounts, where
it would be a second writer, and keeps the two things the operator has
no field for: the marker membership and the reset of the
createrole_self_grant an earlier chart set. Its marking pass waits a
bounded time for CloudNativePG to create the declared roles before it
marks them. users.app adopts the owner CloudNativePG bootstraps, whose
password the instance manager applies again from the bootstrap secret
on every pod start, so that secret is the app user's account Secret
and both writers apply the one stored verifier. A users.<name>.password
left in values draws an admission warning that points at a mint on the
user's Credential.

createRole is refused on every render path. A role a CREATEROLE user
creates makes that user a member of it, CloudNativePG revokes every
membership a managed role is not declared to hold, and the revoke fails
on the dependent self-grant, so the user would sit in cannotReconcile on
ordinary use; there is no per-role switch to leave memberships alone.
The account-name checks render with the managed roles too, so a restore
cannot bypass them: a user CloudNativePG or PostgreSQL reserves, a
database whose derived <name>_admin or <name>_readonly role starts with
pg_ or cnpg_ (which makes the CloudNativePG webhook reject the whole
Cluster), and a user whose account name is no valid object name are
refused. The chart's account naming is pinned to the API's on one table
of names.

A pre-upgrade hook refuses to let CloudNativePG adopt a role the chart
did not mark, because the operator adopts a managed role within seconds
of the spec reaching it, before any post-upgrade job could look. It
reads the live Cluster first and connects to the database only when the
upgrade adds a name CloudNativePG does not manage yet, so a database
that is down does not block the resize that might fix it. For each
absent declared database role it creates and marks the role as the
superuser before the Cluster applies, so no tenant can plant it in the
adoption window. The refusal and the create run in one psql session,
keyed on a snapshot of the role's existence taken at the top, so a role
planted in between collides with the CREATE and stops the hook; the
statements are built with format() and \gexec, since psql does not
substitute :'role' inside a DO $$ block. Verified on postgres:18: a
fresh role is created NOINHERIT, commented and marked, a role planted
after the snapshot fails the hook, and an existing unmarked role is
refused. The hook reads the Cluster at run time with a ServiceAccount
that may get only that Cluster, rather than through a template lookup,
which nothing re-renders when the object changes. The flag an earlier
chart set when a user held createRole is still read and never set, so
such a release keeps refusing to adopt a role it did not mark.

The EndpointAttachment e2e suite read testuser's password from that
Secret to probe the attached addresses. It now mints one per probe and
waits until the application's own Service accepts it, so the attached
address is still queried exactly once.

BREAKING CHANGE: PostgreSQL passwords are no longer readable from
<release>-credentials. A password is issued once, by a mint on the
account's Credential in core.cozystack.io, and replaced by the next
mint. users.<name>.createRole is refused; a release that sets it keeps
its previous chart version and passwords until it is false. Deleting a
Postgres application whose release is suspended is refused. The
conversion needs a platform variant with migrations enabled.

Co-authored-by: IvanHunters <xorokhotnikov@gmail.com>
Co-authored-by: Andrei Kvapil <kvapss@gmail.com>
Assisted-by: LLM
Signed-off-by: Timofei Larkin <lllamnyp@gmail.com>
Signed-off-by: Andrei Kvapil <kvapss@gmail.com>
2026-11-30 18:21:27 +01:00

263 lines
8.6 KiB
YAML

suite: pre-upgrade hook refuses to adopt a role the chart did not mark
# Pins the pre-upgrade refusal (see preflight-job.yaml for why it runs before
# the upgrade and reads the live Cluster at run time): the declared names the
# run compares, the database check only for what is new, the gates on install
# and bootstrap, and that the createRole flag an earlier chart set is read but
# never set.
templates:
- templates/hooks/preflight-job.yaml
release:
name: pg-test
namespace: tenant-test
set:
_cluster:
scheduling: {}
tests:
- it: renders nothing on install
set:
users:
app: {}
asserts:
- hasDocuments:
count: 0
- it: renders nothing while bootstrap.recovery is in effect
release:
name: pg-test
namespace: tenant-test
upgrade: true
set:
users:
app: {}
bootstrap:
enabled: true
oldName: pg-old
asserts:
- hasDocuments:
count: 0
- it: renders nothing for a release without users or databases
release:
name: pg-test
namespace: tenant-test
upgrade: true
asserts:
- hasDocuments:
count: 0
- it: hands every declared user and database role to the comparison with the live Cluster
release:
name: pg-test
namespace: tenant-test
upgrade: true
set:
version: v18
users:
debezium: {}
migrator: {}
databases:
newdb: {}
documentSelector:
path: metadata.name
value: pg-test-preflight-script
asserts:
- matchRegex:
path: data["new-roles.sh"]
pattern: "kubectl -n tenant-test get clusters.postgresql.cnpg.io pg-test --ignore-not-found -o jsonpath='\\{\"found \"\\}\\{range .spec.managed.roles\\[\\*\\]\\}\\{.name\\}\\{\" \"\\}\\{end\\}'"
- matchRegex:
path: data["new-roles.sh"]
pattern: "\\ncheck user debezium debezium\\ncheck user migrator migrator\\ncheck database newdb newdb_admin\\ncheck database newdb newdb_readonly\\n"
# The point of reading the Cluster first: an upgrade that adds no role never
# waits for the database, so a database that is down does not block it.
- it: leaves before waiting for the database when nothing is new
release:
name: pg-test
namespace: tenant-test
upgrade: true
set:
users:
web: {}
documentSelector:
path: metadata.name
value: pg-test-preflight-script
asserts:
- matchRegex:
path: data["preflight.sh"]
pattern: "(?s)if \\[ ! -s /work/new \\]; then\\n[^\\n]*\\n exit 0\\nfi\\n.*until pg_isready"
# A restore purges the Cluster under an upgrade in flight. Without a Cluster
# there is no database to plant a role in, and once its superuser Secret is
# gone a container that required it would wait out the Job deadline, holding
# the release in this hook.
- it: passes without a Cluster and fails at once when the Cluster goes away mid-check
release:
name: pg-test
namespace: tenant-test
upgrade: true
set:
users:
web: {}
documentSelector:
path: metadata.name
value: pg-test-preflight-script
asserts:
- matchRegex:
path: data["new-roles.sh"]
pattern: "(?s)if \\[ -z \"\\$out\" \\]; then\\n.*: > /work/new\\n exit 0\\n fi\\n live=\" \\$\\{out#found \\} \""
- matchRegex:
path: data["preflight.sh"]
pattern: "(?s)if \\[ ! -s /work/new \\]; then\\n.*exit 0\\nfi\\nif \\[ -z \"\\$\\{PGPASSWORD:-\\}\" \\]; then\\n.*exit 1\\nfi\\nuntil pg_isready"
- it: starts the check container when the superuser Secret is missing
release:
name: pg-test
namespace: tenant-test
upgrade: true
set:
users:
web: {}
documentSelector:
path: kind
value: Job
asserts:
- equal:
path: spec.template.spec.containers[0].env[0].valueFrom.secretKeyRef
value:
name: pg-test-superuser
key: username
optional: true
- equal:
path: spec.template.spec.containers[0].env[1].valueFrom.secretKeyRef
value:
name: pg-test-superuser
key: password
optional: true
- it: refuses an unmarked role only for the names the run found new
release:
name: pg-test
namespace: tenant-test
upgrade: true
set:
users:
debezium: {}
documentSelector:
path: metadata.name
value: pg-test-preflight-script
asserts:
- matchRegex:
path: data["preflight.sh"]
pattern: "while read -r kind key role; do\\n psql -v ON_ERROR_STOP=1 --echo-all -v kind=\"\\$kind\" -v key=\"\\$key\" -v role=\"\\$role\""
- matchRegex:
path: data["preflight.sh"]
pattern: "WHERE r.rolname = :'role'\\n AND current_setting\\('cozystack.createrole_granted', true\\) = 'on'\\n AND NOT EXISTS \\(SELECT FROM pg_catalog.pg_auth_members m JOIN pg_catalog.pg_roles g ON g.oid = m.roleid WHERE g.rolname = 'cozystack:managed' AND m.member = r.oid\\)\\\\gexec"
- notMatchRegex:
path: data["preflight.sh"]
pattern: "cozystack.createrole_granted = 'on'"
- it: may read only its own Cluster
release:
name: pg-test
namespace: tenant-test
upgrade: true
set:
users:
web: {}
documentSelector:
path: kind
value: Role
asserts:
- equal:
path: rules
value:
- apiGroups: ["postgresql.cnpg.io"]
resources: ["clusters"]
resourceNames: ["pg-test"]
verbs: ["get"]
- equal:
path: metadata.annotations["helm.sh/hook"]
value: pre-upgrade
- it: runs as a bounded pre-upgrade Job that can reach the API server
release:
name: pg-test
namespace: tenant-test
upgrade: true
set:
users:
app: {}
documentSelector:
path: kind
value: Job
asserts:
- equal:
path: metadata.annotations["helm.sh/hook"]
value: pre-upgrade
- equal:
path: spec.activeDeadlineSeconds
value: 300
- equal:
path: spec.backoffLimit
value: 0
- equal:
path: spec.template.spec.serviceAccountName
value: pg-test-preflight
- equal:
path: spec.template.metadata.labels["policy.cozystack.io/allow-to-apiserver"]
value: "true"
- equal:
path: spec.template.spec.initContainers[0].command
value: ["/bin/sh", "/scripts/new-roles.sh"]
- equal:
path: spec.template.spec.containers[0].env[2].value
value: pg-test-rw
- it: creates and marks each absent declared database role as the superuser on pg16+
release:
upgrade: true
set:
version: v18
databases:
shop: {}
documentSelector:
path: metadata.name
value: pg-test-preflight-script
asserts:
# The refusal and the create-and-mark run in one psql session, keyed on a
# snapshot of whether the role existed, so a role planted between them
# collides with the unconditional CREATE and stops the hook.
- matchRegex:
path: data["preflight.sh"]
pattern: "SELECT NOT EXISTS \\(SELECT FROM pg_catalog.pg_roles WHERE rolname = :'role'\\) AS role_absent\\\\gset"
# The create and mark run as a top-level SELECT format(...) + \gexec, not a
# DO block: psql does not substitute :'role' inside a dollar-quoted body,
# so it would reach PostgreSQL literally and the hook would fail to parse.
- matchRegex:
path: data["preflight.sh"]
pattern: "SELECT format\\('CREATE ROLE %I NOINHERIT; COMMENT ON ROLE %I IS %L; GRANT %I TO %I WITH INHERIT FALSE, SET FALSE', :'role', :'role', 'role managed by helm', 'cozystack:managed', :'role'\\)"
# The create is gated on the snapshot, not a live NOT EXISTS, so it is
# unconditional and fails closed on a concurrent plant.
- matchRegex:
path: data["preflight.sh"]
pattern: "WHERE :'kind' = 'database' AND :'role_absent'::boolean"
- it: marks a hook-created database role without the pg16 options on older PostgreSQL
release:
upgrade: true
set:
version: v15
databases:
shop: {}
documentSelector:
path: metadata.name
value: pg-test-preflight-script
asserts:
- matchRegex:
path: data["preflight.sh"]
pattern: "SELECT format\\('CREATE ROLE %I NOINHERIT; COMMENT ON ROLE %I IS %L; GRANT %I TO %I', :'role', :'role', 'role managed by helm', 'cozystack:managed', :'role'\\)"