mirror of
https://github.com/cozystack/cozystack.git
synced 2026-12-05 22:53:43 +09:00
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>
138 lines
5.4 KiB
YAML
138 lines
5.4 KiB
YAML
suite: init-script trusts only roles it marked itself
|
|
|
|
# On PostgreSQL 16 a CREATEROLE holder may COMMENT ON ROLE every role it created,
|
|
# and by creating a role through an intermediate role it then drops, it leaves no
|
|
# pg_auth_members row that tells who did. Two passes trusted what it can forge:
|
|
# the delete passes picked roles by their comment, so a forged comment had the
|
|
# chart drop a role it never created; and the create passes adopted any role that
|
|
# already existed under a declared name, so a role squatted before an admin
|
|
# declared it stayed under the squatter's control. Membership in
|
|
# "cozystack:managed", which only the superuser can grant, replaces the comment as
|
|
# the record. The behaviour is proven against live PostgreSQL 13, 15, 16 and 17;
|
|
# these tests pin the render.
|
|
|
|
templates:
|
|
- templates/init-script.yaml
|
|
|
|
release:
|
|
name: pg-test
|
|
namespace: tenant-test
|
|
|
|
set:
|
|
_cluster:
|
|
scheduling: {}
|
|
|
|
tests:
|
|
- it: creates the marker once and marks the roles the chart had commented before it existed
|
|
set:
|
|
users:
|
|
app: {}
|
|
documentSelector:
|
|
path: metadata.name
|
|
value: pg-test-init-script
|
|
asserts:
|
|
- matchRegex:
|
|
path: stringData["init.sh"]
|
|
pattern: "IF NOT EXISTS \\(SELECT FROM pg_catalog.pg_roles WHERE rolname = 'cozystack:managed'\\) THEN\\n\\s+CREATE ROLE \"cozystack:managed\" NOLOGIN;\\n\\s+FOR managed IN SELECT r.rolname FROM pg_roles r JOIN pg_shdescription s ON s.objoid = r.oid AND s.classoid = 'pg_authid'::regclass WHERE s.description IN \\('user managed by helm', 'role managed by helm'\\) LOOP\\n\\s+EXECUTE format\\('GRANT %I TO %I WITH INHERIT FALSE, SET FALSE', 'cozystack:managed', managed.rolname\\);"
|
|
|
|
- it: selects the users and roles to delete by the marker, not by the comment alone
|
|
set:
|
|
users:
|
|
app: {}
|
|
documentSelector:
|
|
path: metadata.name
|
|
value: pg-test-init-script
|
|
asserts:
|
|
- matchRegex:
|
|
path: stringData["init.sh"]
|
|
pattern: "WHERE s.description = 'user managed by helm' AND r.oid IN \\(SELECT m.member FROM pg_auth_members m JOIN pg_roles g ON g.oid = m.roleid WHERE g.rolname = 'cozystack:managed'\\)"
|
|
- matchRegex:
|
|
path: stringData["init.sh"]
|
|
pattern: "WHERE s.description = 'role managed by helm' AND r.oid IN \\(SELECT m.member FROM pg_auth_members m JOIN pg_roles g ON g.oid = m.roleid WHERE g.rolname = 'cozystack:managed'\\)"
|
|
|
|
- it: marks every user and database role it creates, as a record that carries no privilege
|
|
set:
|
|
users:
|
|
app: {}
|
|
databases:
|
|
shop: {}
|
|
documentSelector:
|
|
path: metadata.name
|
|
value: pg-test-init-script
|
|
asserts:
|
|
- matchRegex:
|
|
path: stringData["init.sh"]
|
|
pattern: 'GRANT "cozystack:managed" TO "app" WITH INHERIT FALSE, SET FALSE;'
|
|
- matchRegex:
|
|
path: stringData["init.sh"]
|
|
pattern: 'GRANT "cozystack:managed" TO "shop_admin" WITH INHERIT FALSE, SET FALSE;'
|
|
- matchRegex:
|
|
path: stringData["init.sh"]
|
|
pattern: 'GRANT "cozystack:managed" TO "shop_readonly" WITH INHERIT FALSE, SET FALSE;'
|
|
|
|
# INHERIT and SET membership options arrived in PostgreSQL 16.
|
|
- it: marks with a plain GRANT before PostgreSQL 16
|
|
set:
|
|
version: v15
|
|
users:
|
|
app: {}
|
|
documentSelector:
|
|
path: metadata.name
|
|
value: pg-test-init-script
|
|
asserts:
|
|
- matchRegex:
|
|
path: stringData["init.sh"]
|
|
pattern: 'GRANT "cozystack:managed" TO "app";'
|
|
- notMatchRegex:
|
|
path: stringData["init.sh"]
|
|
pattern: 'INHERIT FALSE'
|
|
|
|
# The refusal to adopt an unmarked role lives in the pre-upgrade hook
|
|
# (preflight_test.yaml); the init-script creates no account and marks only a
|
|
# role that exists.
|
|
- it: no longer refuses or creates user roles itself
|
|
set:
|
|
users:
|
|
debezium:
|
|
replication: true
|
|
documentSelector:
|
|
path: metadata.name
|
|
value: pg-test-init-script
|
|
asserts:
|
|
- notMatchRegex:
|
|
path: stringData["init.sh"]
|
|
pattern: 'already exists and this chart did not create it'
|
|
- notMatchRegex:
|
|
path: stringData["init.sh"]
|
|
pattern: 'CREATE ROLE "debezium"'
|
|
- matchRegex:
|
|
path: stringData["init.sh"]
|
|
pattern: "SELECT 'GRANT \"cozystack:managed\" TO \"debezium\" WITH INHERIT FALSE, SET FALSE;'\nWHERE EXISTS \\(SELECT FROM pg_catalog.pg_roles WHERE rolname = 'debezium'\\)\\\\gexec"
|
|
|
|
- it: never records createRole
|
|
set:
|
|
users:
|
|
app: {}
|
|
documentSelector:
|
|
path: metadata.name
|
|
value: pg-test-init-script
|
|
asserts:
|
|
- notMatchRegex:
|
|
path: stringData["init.sh"]
|
|
pattern: 'SET cozystack.createrole_granted'
|
|
|
|
# CNPG creates the roles from the spec, possibly after this hook starts; the
|
|
# marking pass waits for every declared one, bounded, before it runs.
|
|
- it: waits a bounded time for the declared roles before marking them
|
|
set:
|
|
users:
|
|
web: {}
|
|
Bob.Smith: {}
|
|
documentSelector:
|
|
path: metadata.name
|
|
value: pg-test-init-script
|
|
asserts:
|
|
- matchRegex:
|
|
path: stringData["init.sh"]
|
|
pattern: "roles_from=\\$SECONDS\\nuntil \\[ \"\\$\\(psql -v ON_ERROR_STOP=1 -t -A -c \"SELECT count\\(\\*\\) FROM pg_catalog.pg_roles WHERE rolname IN \\('Bob.Smith', 'web'\\)\"\\)\" = \"2\" \\]; do\\n if \\[ \"\\$SECONDS\" -ge \\$\\(\\(roles_from \\+ 60\\)\\) \\]; then"
|