mirror of
https://github.com/cozystack/cozystack.git
synced 2026-11-21 20:23:39 +09:00
Without these, a tenant that needs to manage its own roles has no option but the postgres superuser, which also hands over filesystem access and command execution on the instance. Role attributes are not inherited through role membership, so the generated <db>_admin group role cannot carry them: they have to be set on the login user itself. users.<name>.createRole and users.<name>.bypassRls set CREATEROLE and BYPASSRLS on a managed login user. Both are reconciled on every init-script run, so setting one back to false emits the NO... form and revokes the attribute. createRole is refused on PostgreSQL 15 and older. Before PostgreSQL 16, CREATEROLE allowed granting membership in any non-superuser role, so a user holding it could grant itself pg_execute_server_program and reach command execution on the instance, which is exactly what not exposing the superuser prevents. PostgreSQL 16 restricted CREATEROLE to roles the grantee administers. bypassRls carries no such gate: BYPASSRLS only waives row-level security inside the tenant's own databases, whose owner already reads every row, so it adds no reach. The two attributes are needed together, not separately. PostgreSQL only lets a role create other roles carrying BYPASSRLS if it holds the attribute itself, so provisioning a privileged service role fails with "Only roles with the BYPASSRLS attribute may create roles with the BYPASSRLS attribute" when only createRole is granted. createRole also sets createrole_self_grant as a role-level default. PostgreSQL 16 grants a CREATEROLE holder only ADMIN OPTION on the roles it creates, governed by that parameter, whose default is empty. ADMIN OPTION allows granting the role onward but not SET ROLE to it, and creating an object owned by another role requires SET. The attribute therefore looks functional -- CREATE ROLE succeeds -- while the next statement a tenant writes, CREATE SCHEMA ... AUTHORIZATION <new role>, fails with "must be able to SET ROLE". The default is reset when the attribute is withdrawn. The init job runs as the superuser, and before this change it trusted two things a CREATEROLE holder controls for the roles it creates: role comments and role names. It picked the users and roles to drop by their comment, and a PostgreSQL 16 CREATEROLE holder may comment every role it created, then hand it to the chart for deletion. It adopted any role that already existed under a declared name, so a role created under a name an administrator declares later stayed under its creator's control. Neither can be caught from the catalog: creating a role through an intermediate role and then dropping that role leaves the squatted role with no member at all, while the objects, settings and default privileges planted in it stay. The chart therefore records the roles it manages as members of "cozystack:managed", a role only the superuser can grant, and the delete passes act only on those. Its first run marks every role the chart had commented before, when no role could yet hold CREATEROLE through it. Once any user has held createRole, recorded as a setting of the postgres database, the chart refuses to adopt a role it did not mark and fails with the role's name, rather than hand a squatted role REPLICATION or the ownership of a database. Verified against live PostgreSQL instances. On 17 a migration checklist replayed as the login user, with no superuser: role creation with attributes, CREATE SCHEMA ... AUTHORIZATION with the requested owner, extensions in a named schema, per-role search_path and timeouts, RLS including FORCE ROW LEVEL SECURITY, and SECURITY DEFINER functions all succeed. On 17 the forged-comment and squatting paths are refused or ignored, the upgrade from the previous chart marks the roles it already managed, and a fresh install still adopts the role CNPG bootstraps for users.app. On 13, 15 and 16 the init script runs and re-runs cleanly. Assisted-by: LLM Signed-off-by: Mattia Eleuteri <mattia@hidora.io>
144 lines
6.2 KiB
YAML
144 lines
6.2 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'
|
|
|
|
- it: refuses to adopt an unmarked user role once createRole has been granted, before creating it
|
|
set:
|
|
users:
|
|
debezium:
|
|
replication: true
|
|
documentSelector:
|
|
path: metadata.name
|
|
value: pg-test-init-script
|
|
asserts:
|
|
- matchRegex:
|
|
path: stringData["init.sh"]
|
|
pattern: "SELECT format\\('DO \\$\\$BEGIN RAISE EXCEPTION %L; END\\$\\$', 'role \"debezium\" already exists and this chart did not create it; [^']*'\\)\\nFROM pg_catalog.pg_roles r\\nWHERE r.rolname = 'debezium'\\n AND current_setting\\('cozystack.createrole_granted', true\\) = 'on'\\n AND NOT EXISTS \\(SELECT FROM pg_catalog.pg_auth_members m JOIN pg_catalog.pg_roles g ON g.oid = m.roleid WHERE g.rolname = 'cozystack:managed' AND m.member = r.oid\\)\\\\gexec\\nSELECT 'CREATE ROLE \"debezium\" LOGIN INHERIT;'"
|
|
|
|
- it: refuses to adopt unmarked database roles once createRole has been granted, before creating the database
|
|
set:
|
|
databases:
|
|
newdb: {}
|
|
documentSelector:
|
|
path: metadata.name
|
|
value: pg-test-init-script
|
|
asserts:
|
|
- matchRegex:
|
|
path: stringData["init.sh"]
|
|
pattern: "WHERE r.rolname IN \\('newdb_admin', 'newdb_readonly'\\)\\n AND current_setting\\('cozystack.createrole_granted', true\\) = 'on'\\n AND NOT EXISTS \\(SELECT FROM pg_catalog.pg_auth_members m JOIN pg_catalog.pg_roles g ON g.oid = m.roleid WHERE g.rolname = 'cozystack:managed' AND m.member = r.oid\\)\\\\gexec\\nSELECT 'CREATE DATABASE \"newdb\"'"
|
|
|
|
# The flag is set after every user is created and marked, so the run that first
|
|
# grants createRole still adopts a role that predates it, such as the one CNPG
|
|
# bootstraps for users.app, and before any CREATEROLE is granted, so no holder
|
|
# exists while it is unset.
|
|
- it: records createRole after creating and marking the users and before granting the attribute
|
|
set:
|
|
users:
|
|
app: {}
|
|
migrator:
|
|
createRole: true
|
|
documentSelector:
|
|
path: metadata.name
|
|
value: pg-test-init-script
|
|
asserts:
|
|
- matchRegex:
|
|
path: stringData["init.sh"]
|
|
pattern: "(?s)GRANT \"cozystack:managed\" TO \"migrator\" WITH INHERIT FALSE, SET FALSE;\\nEOT\\npsql -v ON_ERROR_STOP=1 --echo-all -c \"ALTER DATABASE postgres SET cozystack.createrole_granted = 'on';\".*ALTER ROLE \"migrator\" WITH PASSWORD :'pw' LOGIN INHERIT NOREPLICATION CREATEROLE"
|
|
|
|
- it: does not record createRole when no user holds it
|
|
set:
|
|
users:
|
|
app: {}
|
|
documentSelector:
|
|
path: metadata.name
|
|
value: pg-test-init-script
|
|
asserts:
|
|
- notMatchRegex:
|
|
path: stringData["init.sh"]
|
|
pattern: 'SET cozystack.createrole_granted'
|