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

370 lines
19 KiB
YAML

suite: init-script rejects database and extension names that break out of the SQL
# A database name is interpolated into CREATE DATABASE "<db>", the "<db>_admin"
# role, a single-quoted literal in the superuser DO block, and a shell role list;
# an extension name into CREATE EXTENSION in the superuser session. The username
# guard already hardens the same class for users. These guards mirror it for
# databases and extensions, so a "'"/'"'/";"/space cannot escape into arbitrary
# superuser SQL or the shell. The extension is quoted at the call site (real names
# carry "-", e.g. uuid-ossp). Reverting either guard lets the render succeed and
# fails a reject test; unquoting CREATE EXTENSION fails the quoting test.
templates:
- templates/init-script.yaml
release:
name: pg-test
namespace: tenant-test
set:
_cluster:
scheduling: {}
tests:
- it: fails the render for a database name with a single quote (DO-block breakout)
set:
databases:
"x' OR true": {}
asserts:
- failedTemplate:
errorMessage: 'invalid database name "x'' OR true": a database name may contain only letters, digits and the characters -._ and must be at most 54 characters (it is interpolated into superuser SQL and the init-script shell, and it seeds the "<db>_admin" and "<db>_readonly" PostgreSQL roles, which CREATE ROLE silently truncates past 63 bytes; the next init-job run then cannot drop the truncated role and fails). Changing only its key in values makes the init-job drop the database; to keep the data, a platform administrator first renames it as the CNPG superuser (ALTER DATABASE, and ALTER ROLE for its <db>_admin and <db>_readonly roles), then the key is changed'
- it: fails the render for a database name with a semicolon and space
set:
databases:
"a; DROP": {}
asserts:
- failedTemplate:
errorMessage: 'invalid database name "a; DROP": a database name may contain only letters, digits and the characters -._ and must be at most 54 characters (it is interpolated into superuser SQL and the init-script shell, and it seeds the "<db>_admin" and "<db>_readonly" PostgreSQL roles, which CREATE ROLE silently truncates past 63 bytes; the next init-job run then cannot drop the truncated role and fails). Changing only its key in values makes the init-job drop the database; to keep the data, a platform administrator first renames it as the CNPG superuser (ALTER DATABASE, and ALTER ROLE for its <db>_admin and <db>_readonly roles), then the key is changed'
- it: fails the render for an extension name carrying injection
set:
databases:
mydb:
extensions:
- "x; ALTER ROLE app SUPERUSER; --"
asserts:
- failedTemplate:
errorMessage: 'invalid extension name "x; ALTER ROLE app SUPERUSER; --" for database "mydb": an extension name 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 identifier interpolated into CREATE EXTENSION in a superuser session; real names carry "-", e.g. uuid-ossp, so it is quoted at the call site). A clause after the name, such as CASCADE or SCHEMA, is not accepted: remove it from the value, which leaves an extension that is already installed as it is, and list the extensions it depends on before it, e.g. [cube, earthdistance]'
- it: quotes the extension name so a hyphenated one (uuid-ossp) is valid SQL
set:
databases:
mydb:
extensions:
- uuid-ossp
documentSelector:
path: metadata.name
value: pg-test-init-script
asserts:
- matchRegex:
path: stringData["init.sh"]
pattern: 'CREATE EXTENSION IF NOT EXISTS "uuid-ossp";'
- notMatchRegex:
path: stringData["init.sh"]
pattern: 'CREATE EXTENSION IF NOT EXISTS uuid-ossp;'
# PostgreSQL folds an unquoted identifier to lowercase, so an extension written
# as PostGIS resolves to the postgis control file when unquoted. Quoting keeps the case
# and looks for PostGIS.control instead, which does not exist: CREATE EXTENSION
# fails under ON_ERROR_STOP and the post-upgrade init-job wedges the release.
# Fold the name before quoting so it resolves as it did unquoted.
- it: folds a mixed-case extension name to lowercase before quoting it
set:
databases:
mydb:
extensions:
- PostGIS
- UUID-OSSP
documentSelector:
path: metadata.name
value: pg-test-init-script
asserts:
- matchRegex:
path: stringData["init.sh"]
pattern: 'CREATE EXTENSION IF NOT EXISTS "postgis";'
- matchRegex:
path: stringData["init.sh"]
pattern: 'CREATE EXTENSION IF NOT EXISTS "uuid-ossp";'
- notMatchRegex:
path: stringData["init.sh"]
pattern: 'CREATE EXTENSION IF NOT EXISTS "PostGIS";'
# When CREATE EXTENSION rendered the value unquoted, writing the quotes into the
# value was the only way to get a hyphenated extension, so existing releases
# carry '"uuid-ossp"'. One surrounding pair is the identifier's own quoting:
# the name inside it is what the charset guard checks and what gets quoted.
- it: strips one pair of surrounding quotes from an extension name
set:
databases:
mydb:
extensions:
- '"uuid-ossp"'
documentSelector:
path: metadata.name
value: pg-test-init-script
asserts:
- matchRegex:
path: stringData["init.sh"]
pattern: 'CREATE EXTENSION IF NOT EXISTS "uuid-ossp";'
- notMatchRegex:
path: stringData["init.sh"]
pattern: 'CREATE EXTENSION IF NOT EXISTS ""'
- it: renders on upgrade for an extension name written with its own quotes
release:
upgrade: true
set:
databases:
mydb:
extensions:
- '"uuid-ossp"'
documentSelector:
path: metadata.name
value: pg-test-init-script
asserts:
- matchRegex:
path: stringData["init.sh"]
pattern: 'CREATE EXTENSION IF NOT EXISTS "uuid-ossp";'
# A quoted identifier kept its case when it was rendered unquoted, so it must
# resolve to the same control file now; only an unquoted name is folded.
- it: keeps the case of an extension name written with its own quotes
set:
databases:
mydb:
extensions:
- '"PostGIS"'
documentSelector:
path: metadata.name
value: pg-test-init-script
asserts:
- matchRegex:
path: stringData["init.sh"]
pattern: 'CREATE EXTENSION IF NOT EXISTS "PostGIS";'
- it: fails the render for an injection inside an extension name's own quotes
set:
databases:
mydb:
extensions:
- '"x"; ALTER ROLE app SUPERUSER; --"'
asserts:
- failedTemplate:
errorPattern: 'invalid extension name .* for database "mydb": an extension name may contain only letters'
- it: fails the render for an extension name with an unbalanced quote
set:
databases:
mydb:
extensions:
- '"uuid-ossp'
asserts:
- failedTemplate:
errorPattern: 'invalid extension name .* for database "mydb": an extension name may contain only letters'
# The value was once spliced into CREATE EXTENSION whole, so a release can carry
# a clause after the name. The guard still refuses it; the message has to say
# how to get past it without losing the dependency the clause pulled in.
- it: tells how to replace a CASCADE clause when it refuses one on upgrade
release:
upgrade: true
set:
databases:
mydb:
extensions:
- earthdistance CASCADE
asserts:
- failedTemplate:
errorPattern: 'invalid extension name "earthdistance CASCADE" for database "mydb": .*A clause after the name, such as CASCADE or SCHEMA, is not accepted: remove it from the value, which leaves an extension that is already installed as it is, and list the extensions it depends on before it, e\.g\. \[cube, earthdistance\]$'
- it: creates extensions in the order they are listed
set:
databases:
mydb:
extensions:
- cube
- earthdistance
documentSelector:
path: metadata.name
value: pg-test-init-script
asserts:
- matchRegex:
path: stringData["init.sh"]
pattern: '(?s)CREATE EXTENSION IF NOT EXISTS "cube";\s*CREATE EXTENSION IF NOT EXISTS "earthdistance";'
- it: renders normally for a valid database and extension
set:
databases:
mydb:
extensions:
- pg_trgm
documentSelector:
path: metadata.name
value: pg-test-init-script
asserts:
- matchRegex:
path: stringData["init.sh"]
pattern: 'CREATE DATABASE "mydb"'
- matchRegex:
path: stringData["init.sh"]
pattern: 'CREATE EXTENSION IF NOT EXISTS "pg_trgm";'
# A database name seeds "<db>_readonly" (a PostgreSQL role); "_readonly" is 9
# bytes, so the cap is 54 (not the username's 63). 55 chars fails; 54 renders
# (its readonly role is exactly 63 bytes). Reverting the cap to 253 lets 55
# through into a silently-truncated, orphan-dropped role.
- it: fails the render for a database name of 55 characters (readonly role past 63)
set:
databases:
"daaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa": {}
asserts:
- failedTemplate:
errorMessage: 'invalid database name "daaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa": a database name may contain only letters, digits and the characters -._ and must be at most 54 characters (it is interpolated into superuser SQL and the init-script shell, and it seeds the "<db>_admin" and "<db>_readonly" PostgreSQL roles, which CREATE ROLE silently truncates past 63 bytes; the next init-job run then cannot drop the truncated role and fails). Changing only its key in values makes the init-job drop the database; to keep the data, a platform administrator first renames it as the CNPG superuser (ALTER DATABASE, and ALTER ROLE for its <db>_admin and <db>_readonly roles), then the key is changed'
- it: renders normally for a database name at the 54-character boundary
set:
databases:
"daaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa": {}
documentSelector:
path: metadata.name
value: pg-test-init-script
asserts:
- matchRegex:
path: stringData["init.sh"]
pattern: 'CREATE DATABASE "daaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"'
# The extension length cap (63, a PostgreSQL identifier) needs its own pin — the
# sibling username/database caps have boundary tests, this one did not. 64 fails,
# 63 renders. Reverting the extension cap to 253 lets 64 through.
- it: fails the render for an extension name of 64 characters
set:
databases:
mydb:
extensions:
- "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
asserts:
- failedTemplate:
errorMessage: 'invalid extension name "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" for database "mydb": an extension name 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 identifier interpolated into CREATE EXTENSION in a superuser session; real names carry "-", e.g. uuid-ossp, so it is quoted at the call site). A clause after the name, such as CASCADE or SCHEMA, is not accepted: remove it from the value, which leaves an extension that is already installed as it is, and list the extensions it depends on before it, e.g. [cube, earthdistance]'
- it: renders normally for an extension name at the 63-character boundary
set:
databases:
mydb:
extensions:
- "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
documentSelector:
path: metadata.name
value: pg-test-init-script
asserts:
- matchRegex:
path: stringData["init.sh"]
pattern: 'CREATE EXTENSION IF NOT EXISTS "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";'
# A database may declare extensions and no roles at all (roles is optional in
# the schema). The grant-assignment loop chained $d.roles.admin/readonly, and a
# chained field access off a nil .roles nil-pointers the render when a user
# exists and the database has no roles key. Binding $roles := $d.roles | default
# dict and dereferencing the local avoids it (the user is in neither role, so it
# gets REVOKEs), mirroring the mariadb db.yaml guard. Reverting to the inline
# $d.roles.admin chain turns this red with a nil-pointer render error.
- it: renders when a database has extensions but no roles while users exist
set:
users:
app: {}
databases:
mydb:
extensions:
- pg_trgm
documentSelector:
path: metadata.name
value: pg-test-init-script
asserts:
- matchRegex:
path: stringData["init.sh"]
pattern: 'CREATE EXTENSION IF NOT EXISTS "pg_trgm"'
# Past 54 bytes the name's <db>_admin/<db>_readonly roles truncate past 63
# bytes. The next init-job run orphan-drops the truncated role, DROP OWNED only
# reaches the database it runs in, and DROP ROLE then fails on the grants inside
# the long-named database, so such a release cannot survive a second run anyway.
# The cap therefore holds on upgrade as well: failing the
# render names the problem instead of crash-looping the init-job. Gating the cap
# to install turns this red.
- it: still fails on upgrade for a 55-character database name
release:
name: pg-test
namespace: tenant-test
upgrade: true
set:
databases:
"daaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa": {}
asserts:
- failedTemplate:
errorMessage: 'invalid database name "daaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa": a database name may contain only letters, digits and the characters -._ and must be at most 54 characters (it is interpolated into superuser SQL and the init-script shell, and it seeds the "<db>_admin" and "<db>_readonly" PostgreSQL roles, which CREATE ROLE silently truncates past 63 bytes; the next init-job run then cannot drop the truncated role and fails). Changing only its key in values makes the init-job drop the database; to keep the data, a platform administrator first renames it as the CNPG superuser (ALTER DATABASE, and ALTER ROLE for its <db>_admin and <db>_readonly roles), then the key is changed'
# Past 63 bytes CREATE DATABASE itself truncates the name as well, so the next
# run re-runs CREATE under the full name and fails on "already exists".
- it: still fails on upgrade for a 64-character database name
release:
name: pg-test
namespace: tenant-test
upgrade: true
set:
databases:
"daaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa": {}
asserts:
- failedTemplate:
errorMessage: 'invalid database name "daaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa": a database name may contain only letters, digits and the characters -._ and must be at most 54 characters (it is interpolated into superuser SQL and the init-script shell, and it seeds the "<db>_admin" and "<db>_readonly" PostgreSQL roles, which CREATE ROLE silently truncates past 63 bytes; the next init-job run then cannot drop the truncated role and fails). Changing only its key in values makes the init-job drop the database; to keep the data, a platform administrator first renames it as the CNPG superuser (ALTER DATABASE, and ALTER ROLE for its <db>_admin and <db>_readonly roles), then the key is changed'
# The charset is the injection guard: a new database added on upgrade is still a
# new name, and a quote or ";" in it would break out of superuser SQL, so it is
# never gated to install.
- it: still fails on upgrade for a database name outside the charset
release:
name: pg-test
namespace: tenant-test
upgrade: true
set:
databases:
"a; DROP": {}
asserts:
- failedTemplate:
errorMessage: 'invalid database name "a; DROP": a database name may contain only letters, digits and the characters -._ and must be at most 54 characters (it is interpolated into superuser SQL and the init-script shell, and it seeds the "<db>_admin" and "<db>_readonly" PostgreSQL roles, which CREATE ROLE silently truncates past 63 bytes; the next init-job run then cannot drop the truncated role and fails). Changing only its key in values makes the init-job drop the database; to keep the data, a platform administrator first renames it as the CNPG superuser (ALTER DATABASE, and ALTER ROLE for its <db>_admin and <db>_readonly roles), then the key is changed'
# "." and a ".." prefix are rejected for usernames because a username is a
# Secret key; a database name is not, and both are valid quoted identifiers.
- it: renders a database named "." and one starting with ".."
set:
databases:
".": {}
"..x": {}
documentSelector:
path: metadata.name
value: pg-test-init-script
asserts:
- matchRegex:
path: stringData["init.sh"]
pattern: 'CREATE DATABASE "\.\.x"'
# "." and a ".." prefix are not extension names; the guard rejects them with the
# same message. Dropping either conjunct lets the render succeed, so the matching
# test fails.
- it: fails the render for an extension named "."
set:
databases:
mydb:
extensions: ["."]
asserts:
- failedTemplate:
errorMessage: 'invalid extension name "." for database "mydb": an extension name 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 identifier interpolated into CREATE EXTENSION in a superuser session; real names carry "-", e.g. uuid-ossp, so it is quoted at the call site). A clause after the name, such as CASCADE or SCHEMA, is not accepted: remove it from the value, which leaves an extension that is already installed as it is, and list the extensions it depends on before it, e.g. [cube, earthdistance]'
- it: fails the render for an extension name starting with ".."
set:
databases:
mydb:
extensions: ["..x"]
asserts:
- failedTemplate:
errorMessage: 'invalid extension name "..x" for database "mydb": an extension name 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 identifier interpolated into CREATE EXTENSION in a superuser session; real names carry "-", e.g. uuid-ossp, so it is quoted at the call site). A clause after the name, such as CASCADE or SCHEMA, is not accepted: remove it from the value, which leaves an extension that is already installed as it is, and list the extensions it depends on before it, e.g. [cube, earthdistance]'