Skip to content

feat: set the membership options of a declarative role - #11821

Open
armru wants to merge 3 commits into
mainfrom
dev/10007
Open

armru wants to merge 3 commits into
mainfrom
dev/10007

Conversation

@armru

@armru armru commented Nov 2, 2026 •

Copy link
Copy Markdown
Member

A DatabaseRole could only list the roles it is a member of through inRoles, which grants each membership with the PostgreSQL defaults, leaving any other option to a manual GRANT the operator would not track. This adds roleGrants, one entry per membership with the admin, inherit and set options of the GRANT that makes it, following the interface proposed in #10007 (comment):

apiVersion: postgresql.cnpg.io/v1
kind: DatabaseRole
metadata:
  name: cluster-example-dante
spec:
  cluster:
    name: cluster-example
  name: dante
  login: true
  inRoles:
    - pg_read_all_data
  roleGrants:
    - name: app
      inherit: false
    - name: pg_monitor
      admin: true

An entry carrying only a name is the same as an inRoles item, and an option left out is not managed: PostgreSQL applies its default when the membership is granted, and the operator leaves it alone afterwards. The two lists declare the whole membership set of the role, so removing an entry revokes the membership. The field exists only on DatabaseRole, which reports a failure on the object that caused it, while inline managed roles keep inRoles. This supersedes #11031, whose author no longer has time to carry it on: it is a new implementation rather than a continuation, since that PR put the field in the RoleConfiguration shared with the Cluster, and read inherit_option and set_option on every role reconciliation, which fails on PostgreSQL 14 and 15.

Two points depart from the SQL in the issue, both measured on PostgreSQL 14.24, 15.19, 16.15, 17.11, 18.6 and 19rc1. inherit_option and set_option exist in pg_auth_members only from 16, so the columns the memberships are read with are chosen from the major version of the instance, which the reconcilers get from the same function #11826 added for GRANTED BY. The admin option is granted with WITH ADMIN OPTION and removed with REVOKE ADMIN OPTION FOR on every version: from 16 the REVOKE fails with dependent privileges exist when the member used the option to grant the role to others, where WITH ADMIN FALSE drops the option anyway. The version also rejects inherit and set on 14 and 15, which is reported in the status of the DatabaseRole before any statement runs, rather than ignoring options whose absence changes what the member can do.

From PostgreSQL 16 a membership can be granted once per grantor, each grant with its own options. The declared options are set on every grant of the membership, naming its grantor through GRANTED BY, since any of them can give the member what the spec denies: a membership granted by a role holding the admin option, as in the issue, would otherwise keep letting the member inherit when the spec says inherit: false.

A repeated roleGrants entry is rejected by the API server through listType=map. A role listed in both inRoles and roleGrants fails the reconciliation instead: the CEL rule comparing the two lists exceeds the cost budget of the CRD, by 6.7 times even with maxItems: 64 on roleGrants, because inRoles is unbounded.

This builds on #11826 and #11827, and includes their commits until they are merged: only the last commit belongs to this PR.

Closes #10007

@armru
armru requested review from a team, NiccoloFei and litaocdl as code owners November 2, 2026 13:58
@cnpg-bot cnpg-bot added backport-requested ◀️ This pull request should be backported to all supported releases release-1.28 release-1.30 labels Nov 2, 2026
@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

❗ By default, the pull request is configured to backport to all release branches.

  • To stop backporting this pr, remove the label: backport-requested ◀️ or add the label 'do not backport'
  • To stop backporting this pr to a certain release branch, remove the specific branch label: release-x.y

armru added 3 commits November 2, 2026 16:38
From PostgreSQL 16 a membership can be granted once per grantor: every
role holding the `ADMIN` option on a role can grant it again, and a
`REVOKE` acts only on the grant of the grantor it names, which for a
superuser defaults to the bootstrap superuser. Removing a role from
`inRoles` therefore revoked only the grant the operator had made, and a
membership granted by another role survived, both for inline managed
roles and for a `DatabaseRole`.

The memberships are now read with their grantor, and from PostgreSQL 16
the operator names it through `GRANTED BY` on each grant it revokes, so
the role is no longer a member of a role it does not list. Before 16 a
membership has a single grant, which a superuser `REVOKE` removes
whoever granted it, so the statements there are unchanged. A revoke can
now fail with `dependent privileges exist` when the member used its
`ADMIN` option to grant the role to others, where before the membership
was silently left in place.

Closes #11825

Signed-off-by: Armando Ruocco <[email protected]>
When updating an existing role, the `DatabaseRole` reconciler granted
and revoked its memberships before running `ALTER ROLE`. From PostgreSQL
16 a membership granted without an `inherit` option takes it from the
`inherit` attribute the role has at that moment, so a membership added
in the same change that turned the attribute off was granted with
`inherit_option` set, and the role kept inheriting the privileges of
that role. Memberships are now applied after the attributes and the
comment.

Closes #11824

Signed-off-by: Armando Ruocco <[email protected]>
A `DatabaseRole` could only list the roles it is a member of through
`inRoles`, which grants each membership with the PostgreSQL defaults.
This adds `roleGrants`, one entry per membership with the `admin`,
`inherit` and `set` options of the `GRANT` that makes it:

```yaml
  inRoles:
    - pg_read_all_data
  roleGrants:
    - name: app
      inherit: false
    - name: pg_monitor
      admin: true
```

An entry carrying only a `name` is the same as an `inRoles` item, and an
option left out is not managed: PostgreSQL applies its default when the
membership is granted, and the operator leaves it alone afterwards. The
field exists only on `DatabaseRole`, which reports a failure on the
object that caused it, while inline managed roles keep `inRoles`.

The options of each grant are read from `pg_auth_members`, where
`inherit_option` and `set_option` exist only from PostgreSQL 16, so the
columns are chosen from the major version of the instance. The admin
option is granted with `WITH ADMIN OPTION` and removed with `REVOKE ADMIN
OPTION FOR`, which every supported version accepts and which, from
PostgreSQL 16, refuses to drop an option other grants depend on, where
`WITH ADMIN FALSE` drops it anyway. `inherit` and `set` need PostgreSQL
16, and on an older server the `DatabaseRole` is reported as not applied
rather than having them ignored.

From PostgreSQL 16 a membership can be granted once per grantor, each
grant with its own options. The declared options are set on every grant
of the membership, naming its grantor through `GRANTED BY`, since any of
them can give the member what the spec denies.

A repeated `roleGrants` entry is rejected by the API server. A role
listed in both `inRoles` and `roleGrants` fails the reconciliation
instead, since a CEL rule comparing the two lists exceeds the cost
budget of the CRD while `inRoles` is unbounded.

Closes #10007

Signed-off-by: Armando Ruocco <[email protected]>
@armru armru added do not backport This PR must not be backported - it will be in the next minor release and removed backport-requested ◀️ This pull request should be backported to all supported releases release-1.28 release-1.30 labels Nov 2, 2026
@armru

armru commented Nov 2, 2026

Copy link
Copy Markdown
Member Author

/test

@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not backport This PR must not be backported - it will be in the next minor release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: Support role membership options ADMIN, INHERIT, SET

2 participants