Files
cozystack/packages/apps/postgres/tests/user_createrole_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

198 lines
6.3 KiB
YAML

suite: per-user CREATEROLE attribute
# users.<name>.createRole is refused while users are CloudNativePG managed
# roles. 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, which leaves the user stuck
# in cannotReconcile on ordinary use. CloudNativePG has no per-role switch to
# leave memberships alone. The refusal renders with the Cluster, on every path,
# recovery included, so an existing release that grants it keeps its previous
# chart version until the flag is set to false.
templates:
- templates/init-script.yaml
- templates/db.yaml
release:
name: pg-test
namespace: tenant-test
set:
_cluster:
scheduling: {}
tests:
- it: "user is NOCREATEROLE by default"
set:
users:
app: {}
template: templates/db.yaml
documentSelector:
path: kind
value: Cluster
asserts:
- contains:
path: spec.managed.roles
content:
name: app
login: true
inherit: true
replication: false
createrole: false
bypassrls: false
comment: user managed by helm
inRoles:
- "cozystack:managed"
passwordSecret:
name: pg-test.app.account
- it: "createRole=true is refused while users are managed roles"
set:
version: v18
users:
app:
createRole: true
template: templates/db.yaml
asserts:
- failedTemplate:
errorMessage: "users.app.createRole is not supported: users are CloudNativePG managed roles, which revoke the membership PostgreSQL gives a CREATEROLE user in each role it creates. Set createRole to false; an existing release with it keeps its previous chart version until then"
- it: "createRole=false renders createrole false, so a role that held it loses it"
set:
version: v18
users:
app:
createRole: false
template: templates/db.yaml
documentSelector:
path: kind
value: Cluster
asserts:
- contains:
path: spec.managed.roles
content:
name: app
createrole: false
any: true
- it: "createRole on PostgreSQL 15 is refused"
set:
version: v15
users:
app:
createRole: true
template: templates/db.yaml
asserts:
- failedTemplate:
errorMessage: "users.app.createRole is not supported: users are CloudNativePG managed roles, which revoke the membership PostgreSQL gives a CREATEROLE user in each role it creates. Set createRole to false; an existing release with it keeps its previous chart version until then"
- it: "createRole on PostgreSQL 13 is refused"
set:
version: v13
users:
app:
createRole: true
template: templates/db.yaml
asserts:
- failedTemplate:
errorMessage: "users.app.createRole is not supported: users are CloudNativePG managed roles, which revoke the membership PostgreSQL gives a CREATEROLE user in each role it creates. Set createRole to false; an existing release with it keeps its previous chart version until then"
# The refusal renders with the Cluster, so a restore cannot get past it.
# roles do, so the guard sits with them.
- it: "createRole on PostgreSQL 15 is refused on recovery too"
set:
version: v15
bootstrap:
enabled: true
oldName: pg-old
backup:
s3AccessKey: a
s3SecretKey: b
users:
migrator:
createRole: true
template: templates/db.yaml
asserts:
- failedTemplate:
errorMessage: "users.migrator.createRole is not supported: users are CloudNativePG managed roles, which revoke the membership PostgreSQL gives a CREATEROLE user in each role it creates. Set createRole to false; an existing release with it keeps its previous chart version until then"
- it: "createRole on PostgreSQL 13 is refused on recovery too"
set:
version: v13
bootstrap:
enabled: true
oldName: pg-old
backup:
s3AccessKey: a
s3SecretKey: b
users:
migrator:
createRole: true
template: templates/db.yaml
asserts:
- failedTemplate:
errorMessage: "users.migrator.createRole is not supported: users are CloudNativePG managed roles, which revoke the membership PostgreSQL gives a CREATEROLE user in each role it creates. Set createRole to false; an existing release with it keeps its previous chart version until then"
- it: "PostgreSQL 15 renders fine as long as createRole is not requested"
set:
version: v15
users:
app: {}
template: templates/init-script.yaml
documentSelector:
path: metadata.name
value: pg-test-init-script
asserts:
- matchRegex:
path: stringData["init.sh"]
pattern: 'GRANT "cozystack:managed" TO "app";'
- it: "bypassRls is allowed on PostgreSQL 15, it carries no escalation path of its own"
set:
version: v15
users:
app:
bypassRls: true
template: templates/db.yaml
documentSelector:
path: kind
value: Cluster
asserts:
- contains:
path: spec.managed.roles
content:
name: app
bypassrls: true
any: true
# A role that held createRole under an earlier chart keeps its
# createrole_self_grant until this reset, which runs once the role exists.
- it: "withdrawing createRole resets createrole_self_grant"
set:
version: v18
users:
app:
createRole: false
template: templates/init-script.yaml
documentSelector:
path: metadata.name
value: pg-test-init-script
asserts:
- matchRegex:
path: stringData["init.sh"]
pattern: "SELECT 'ALTER ROLE \"app\" RESET createrole_self_grant;'\\nWHERE EXISTS \\(SELECT FROM pg_catalog.pg_roles WHERE rolname = 'app'\\)\\\\gexec"
- it: "does not fail the wait when no user holds createRole"
set:
version: v18
users:
app: {}
template: templates/init-script.yaml
documentSelector:
path: metadata.name
value: pg-test-init-script
asserts:
- notMatchRegex:
path: stringData["init.sh"]
pattern: "createRole users still absent"