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

150 lines
6.8 KiB
YAML

suite: init-script rejects a username outside the safe charset
# The username is interpolated into the init-script shell and role SQL and into
# the Cluster's managed roles. A name with a shell metacharacter is a
# command-injection shape. The chart must reject it at render time, naming the
# offending username, so the metacharacter never reaches the script. Reverting
# the guard in init-script.yaml lets the render succeed, so the first test fails.
templates:
- templates/init-script.yaml
- templates/db.yaml
release:
name: pg-test
namespace: tenant-test
set:
_cluster:
scheduling: {}
tests:
- it: fails the render for a username carrying a shell metacharacter
template: templates/init-script.yaml
set:
users:
'evil";id;echo "x': {}
asserts:
- failedTemplate:
errorMessage: 'invalid username "evil\";id;echo \"x": a username may contain only letters, digits and the characters -._ , must not be "." or start with "..", and must be at most 63 characters (it is a PostgreSQL role name, which CREATE ROLE silently truncates past 63 bytes, and the account name derived from it is a Kubernetes object name)'
- it: fails the render for a username with a space
template: templates/init-script.yaml
set:
users:
'a b': {}
asserts:
- failedTemplate:
errorMessage: 'invalid username "a b": a username may contain only letters, digits and the characters -._ , must not be "." or start with "..", and must be at most 63 characters (it is a PostgreSQL role name, which CREATE ROLE silently truncates past 63 bytes, and the account name derived from it is a Kubernetes object name)'
# "." and ".." match the charset regex but are rejected by k8s IsConfigMapKey as
# Secret keys, so the guard rejects them explicitly (the length cap is a separate,
# PostgreSQL-driven concern pinned by the 63/64 boundary tests below). These pin
# the guard to the full IsConfigMapKey contract.
- it: fails the render for the reserved name "."
template: templates/init-script.yaml
set:
users:
'.': {}
asserts:
- failedTemplate:
errorMessage: 'invalid username ".": a username may contain only letters, digits and the characters -._ , must not be "." or start with "..", and must be at most 63 characters (it is a PostgreSQL role name, which CREATE ROLE silently truncates past 63 bytes, and the account name derived from it is a Kubernetes object name)'
- it: fails the render for the reserved name ".."
template: templates/init-script.yaml
set:
users:
'..': {}
asserts:
- failedTemplate:
errorMessage: 'invalid username "..": a username may contain only letters, digits and the characters -._ , must not be "." or start with "..", and must be at most 63 characters (it is a PostgreSQL role name, which CREATE ROLE silently truncates past 63 bytes, and the account name derived from it is a Kubernetes object name)'
# IsConfigMapKey rejects ANY name starting with ".." (hasChDirPrefix), not just
# the literal "..", so "..foo" is charset-legal yet cannot install.
- it: fails the render for a name starting with ".."
template: templates/init-script.yaml
set:
users:
'..foo': {}
asserts:
- failedTemplate:
errorMessage: 'invalid username "..foo": a username may contain only letters, digits and the characters -._ , must not be "." or start with "..", and must be at most 63 characters (it is a PostgreSQL role name, which CREATE ROLE silently truncates past 63 bytes, and the account name derived from it is a Kubernetes object name)'
# A username is a PostgreSQL role name: CREATE ROLE silently truncates past 63
# bytes (NAMEDATALEN-1) with a NOTICE, not an error, so a 64-char name would be
# created as its 63-byte prefix and then dropped by the delete pass (catalog
# truncated != declared full name) in the same hook. The cap is 63, not the
# 253-char Secret-key limit. 64 chars fails; 63 renders. Reverting the cap to 253
# lets the 64-char case through.
- it: fails the render for a username of 64 characters (past the 63-byte role limit)
template: templates/init-script.yaml
set:
users:
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa': {}
asserts:
- failedTemplate:
errorMessage: 'invalid username "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa": a username may contain only letters, digits and the characters -._ , must not be "." or start with "..", and must be at most 63 characters (it is a PostgreSQL role name, which CREATE ROLE silently truncates past 63 bytes, and the account name derived from it is a Kubernetes object name)'
- it: renders normally for a username at the 63-character boundary
set:
users:
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa': {}
template: templates/db.yaml
documentSelector:
path: kind
value: Cluster
asserts:
- contains:
path: spec.managed.roles
content:
name: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
any: true
- it: renders normally for names using the full safe charset
set:
users:
a-b.c_1: {}
template: templates/db.yaml
documentSelector:
path: kind
value: Cluster
asserts:
- contains:
path: spec.managed.roles
content:
name: a-b.c_1
passwordSecret:
name: pg-test.a-b-c-1.account
any: true
# `postgres` is the CNPG-managed superuser and is forbidden as an app role. The
# rejection must not depend on bootstrap.enabled: while it is true the
# create-users block is gated off, so a restore target carrying users.postgres
# installs, and the restore controller's automatic bootstrap clear
# (disablePostgresAppBootstrap) then wedges the next reconcile against this fail
# — a state the controller records as Succeeded. Rejecting it regardless of
# bootstrap keeps the controller from converging a release into an unrenderable
# one. Moving the guard back inside the `not .Values.bootstrap.enabled` block
# lets the bootstrap case render, so this test fails.
- it: rejects users.postgres even while bootstrap.enabled is true
template: templates/init-script.yaml
set:
bootstrap:
enabled: true
users:
postgres:
replication: false
asserts:
- failedTemplate:
errorMessage: '`users.postgres` is forbidden by policy. Use a different username.'
- it: rejects users.postgres on the normal (non-bootstrap) path too
template: templates/init-script.yaml
set:
users:
postgres:
replication: false
asserts:
- failedTemplate:
errorMessage: '`users.postgres` is forbidden by policy. Use a different username.'