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

242 lines
7.9 KiB
YAML

suite: the Cluster declares every role as CloudNativePG managed roles
# Pins the managed roles render (see the comment in db.yaml for why each role
# is rendered as the init-script created it) and the account Secret naming the
# API shares.
templates:
- templates/db.yaml
release:
name: pg-test
namespace: tenant-test
set:
_cluster:
scheduling: {}
tests:
- it: declares the marker with no login and no comment
set:
users:
app: {}
documentSelector:
path: kind
value: Cluster
asserts:
- contains:
path: spec.managed.roles
content:
name: "cozystack:managed"
login: false
- it: declares the database roles with no login, no inherit, the comment and the marker
set:
databases:
shop: {}
documentSelector:
path: kind
value: Cluster
asserts:
- contains:
path: spec.managed.roles
content:
name: shop_admin
login: false
inherit: false
comment: role managed by helm
inRoles:
- "cozystack:managed"
- contains:
path: spec.managed.roles
content:
name: shop_readonly
login: false
inherit: false
comment: role managed by helm
inRoles:
- "cozystack:managed"
- it: gives an account its database roles through inRoles and its Secret by the account name
set:
users:
Web.App: {}
reporter: {}
databases:
shop:
roles:
admin: [Web.App]
readonly: [reporter]
logs:
roles:
readonly: [Web.App, reporter]
documentSelector:
path: kind
value: Cluster
asserts:
- contains:
path: spec.managed.roles
content:
name: Web.App
login: true
inherit: true
replication: false
createrole: false
bypassrls: false
comment: user managed by helm
inRoles:
- "cozystack:managed"
- logs_readonly
- shop_admin
passwordSecret:
name: pg-test.web-app.account
- contains:
path: spec.managed.roles
content:
name: reporter
inRoles:
- "cozystack:managed"
- logs_readonly
- shop_readonly
passwordSecret:
name: pg-test.reporter.account
any: true
# The same refusal the API makes, for a HelmRelease written around it.
- it: refuses two users that map to one account name
set:
users:
web_app: {}
web-app: {}
asserts:
- failedTemplate:
errorMessage: 'users "web-app" and "web_app" both map to the account name "web-app"; rename one of them'
- it: renders no managed roles beyond the marker for a release without users or databases
documentSelector:
path: kind
value: Cluster
asserts:
- lengthEqual:
path: spec.managed.roles
count: 1
# The account Secret name must be the one the API derives; the same rows are
# pinned for account.MapName in pkg/credentials/account/account_test.go.
- it: names each account Secret as the API does
set:
users:
web: {}
Bob.Smith: {}
app_user: {}
UPPER: {}
x-1: {}
db.user_2: {}
documentSelector:
path: kind
value: Cluster
asserts:
- contains: {path: spec.managed.roles, content: {name: web, passwordSecret: {name: pg-test.web.account}}, any: true}
- contains: {path: spec.managed.roles, content: {name: Bob.Smith, passwordSecret: {name: pg-test.bob-smith.account}}, any: true}
- contains: {path: spec.managed.roles, content: {name: app_user, passwordSecret: {name: pg-test.app-user.account}}, any: true}
- contains: {path: spec.managed.roles, content: {name: UPPER, passwordSecret: {name: pg-test.upper.account}}, any: true}
- contains: {path: spec.managed.roles, content: {name: x-1, passwordSecret: {name: pg-test.x-1.account}}, any: true}
- contains: {path: spec.managed.roles, content: {name: db.user_2, passwordSecret: {name: pg-test.db-user-2.account}}, any: true}
- it: refuses a user whose account name does not start and end with a letter or digit
set:
users:
app_: {}
asserts:
- failedTemplate:
errorMessage: 'user "app_" maps to the account name "app-", which must start and end with a letter or a digit; rename the user'
- it: refuses a user whose account name starts with a dash
set:
users:
_svc: {}
asserts:
- failedTemplate:
errorMessage: 'user "_svc" maps to the account name "-svc", which must start and end with a letter or a digit; rename the user'
- it: refuses a user whose account name ends with a dash
set:
users:
x.: {}
asserts:
- failedTemplate:
errorMessage: 'user "x." maps to the account name "x-", which must start and end with a letter or a digit; rename the user'
- it: refuses the CloudNativePG replication user
set:
users:
streaming_replica: {}
asserts:
- failedTemplate:
errorMessage: 'user "streaming_replica" is reserved: streaming_replica and names starting with pg_ or cnpg_ belong to PostgreSQL and CloudNativePG; rename the user'
- it: refuses names PostgreSQL reserves
set:
users:
pg_owner: {}
asserts:
- failedTemplate:
errorMessage: 'user "pg_owner" is reserved: streaming_replica and names starting with pg_ or cnpg_ belong to PostgreSQL and CloudNativePG; rename the user'
- it: refuses names CloudNativePG reserves
set:
users:
cnpg_pooler: {}
asserts:
- failedTemplate:
errorMessage: 'user "cnpg_pooler" is reserved: streaming_replica and names starting with pg_ or cnpg_ belong to PostgreSQL and CloudNativePG; rename the user'
- it: refuses a user named as a role of a declared database
set:
users:
shop_admin: {}
databases:
shop: {}
asserts:
- failedTemplate:
errorMessage: 'user "shop_admin" is the name of a role of database "shop"; rename the user'
- it: refuses a database whose derived roles PostgreSQL reserves
set:
databases:
pg_stat: {}
asserts:
- failedTemplate:
errorMessage: 'database "pg_stat" is reserved: its roles pg_stat_admin and pg_stat_readonly would start with pg_ or cnpg_, which PostgreSQL and CloudNativePG reserve and the CloudNativePG webhook rejects; rename the database'
- it: refuses a database whose derived roles CloudNativePG reserves
set:
databases:
cnpg_x: {}
asserts:
- failedTemplate:
errorMessage: 'database "cnpg_x" is reserved: its roles cnpg_x_admin and cnpg_x_readonly would start with pg_ or cnpg_, which PostgreSQL and CloudNativePG reserve and the CloudNativePG webhook rejects; rename the database'
- it: refuses a database named pg because pg_admin is reserved
set:
databases:
pg: {}
asserts:
- failedTemplate:
errorMessage: 'database "pg" is reserved: its roles pg_admin and pg_readonly would start with pg_ or cnpg_, which PostgreSQL and CloudNativePG reserve and the CloudNativePG webhook rejects; rename the database'
- it: refuses a database named cnpg because cnpg_admin is reserved
set:
databases:
cnpg: {}
asserts:
- failedTemplate:
errorMessage: 'database "cnpg" is reserved: its roles cnpg_admin and cnpg_readonly would start with pg_ or cnpg_, which PostgreSQL and CloudNativePG reserve and the CloudNativePG webhook rejects; rename the database'
- it: allows a database whose name merely begins with pg
set:
databases:
pgdata: {}
asserts:
- notFailedTemplate: {}