Skip to content

feat(roles): introduce roleGrants field with role membership options - #11031

Closed
jonasbadstuebner wants to merge 4 commits into
cloudnative-pg:mainfrom
jonasbadstuebner:dev/10007
Closed

jonasbadstuebner wants to merge 4 commits into
cloudnative-pg:mainfrom
jonasbadstuebner:dev/10007

Conversation

@jonasbadstuebner

@jonasbadstuebner jonasbadstuebner commented Jun 24, 2026 •

Copy link
Copy Markdown
Contributor

Complements the limited inRoles string array with a structured roleGrants object array in both the DatabaseRole CRD and managed roles definition to support advanced PostgreSQL membership options, ADMIN, INHERIT and SET.

Why this change?
The inRoles option only allows basic role membership. roleGrants provides precise control for environments requiring specific PostgreSQL role behaviors, allowing explicit toggling of ADMIN, INHERIT, and SET membership options per granted role.

Behavior & Compatibility:

  • inRoles stays supported.
  • If both are provided, roleGrants options takes precedence, but new roles are still created with the IN ROLE option
  • If only roleGrants is provided, the role is created first without any memberships and the GRANT statements with granular options are run right after creation.
  • Default behavior for roleGrants matches PostgreSQL defaults if flags are omitted/only the name is given.

Why not a Grant CRD?
Choosing to embed roleGrants into existing definitions rather than building a broad Grant CRD enforces a strict architectural boundary: the CNPG operator manages cluster-level role membership down to the database, while fine-grained permissions of objects inside databases remain the responsibility of application migrations.

Closes #10007

@jonasbadstuebner
jonasbadstuebner requested a review from a team June 24, 2026 16:11
@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. enhancement 🪄 New feature or request labels Jun 24, 2026
@cnpg-bot cnpg-bot added backport-requested ◀️ This pull request should be backported to all supported releases release-1.25 release-1.28 labels Jun 24, 2026
@github-actions

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

@marvin-roesch marvin-roesch left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some minor code improvements to point out and one larger question about optional field semantics.

RoleGrants: []DatabaseRoleGrant{
{
Name: "role1",
Admin: ptr.To(true),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Go 1.26 allows new(true), so using the ptr package no longer is required.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A quick grep says there are over 400 occurrences of ptr.To(...) in the code base and only a handful of new(...).
I wanted to keep the general style of dealing with bool pointers, but if the maintainers agree with you, @marvin-roesch, I can easily replace ptr.To with new for this newly added code.

Comment thread api/v1/cluster_types.go Resolved
Complements the limited `inRoles` string array with a structured
`roleGrants` object array in both the DatabaseRole CRD and managed roles
definition to support advanced PostgreSQL membership options, `ADMIN`,
`INHERIT` and `SET`.

**Why this change?**
The `inRoles` option only allows basic role membership.
`roleGrants` provides precise control for environments requiring
specific PostgreSQL role behaviors, allowing explicit toggling of
`ADMIN`, `INHERIT`, and `SET` membership options per granted role.

Behavior & Compatibility:
- `inRoles` stays supported.
- If both are provided, `roleGrants` options takes precedence, but new roles are
  still created with the `IN ROLE` option
- If only `roleGrants` is provided, the role is created first without
  any memberships and the `GRANT` statements with granular options are run
  right after creation.
- Default behavior for `roleGrants` matches PostgreSQL defaults if flags
  are omitted/only the name is given.

**Why not a Grant CRD?**
Choosing to embed `roleGrants` into existing definitions rather than
building a broad Grant CRD enforces a strict architectural boundary: the
CNPG operator manages cluster-level role membership down to the
database, while fine-grained permissions of objects inside databases
remain the responsibility of application migrations.

Related to cloudnative-pg#10007

Signed-off-by: Jonas Badstübner <[email protected]>
It was suggested that because the function is not only looking up _if_ a
user has a role membership granted but also checks that it has the
correct membership options set, having the function be named
hasMatchingRoleGrants seems to more accurately reflect what is happening
inside it.

cloudnative-pg#11031 (comment)

Signed-off-by: Jonas Badstübner <[email protected]>
This commit changes the way the currently configured membership options
are read back from the database.
Where previously string concatenation was used, json array aggregation
is now utilized instead, making a bloated function obsolete and
improving readability overall.

Signed-off-by: Jonas Badstübner <[email protected]>
This commit adds documentation especially on how the membership options
of `RoleGrant`s behave.

Signed-off-by: Jonas Badstübner <[email protected]>
@gbartolini

Copy link
Copy Markdown
Contributor

Thanks for this, @jonasbadstuebner. Embedding roleGrants in the existing role definitions, instead of adding a Grant CRD, is the right boundary, and I'm happy with the API shape, including admin, inherit and set.

I've written up how I think the interface should look in #10007 (comment). It is mostly what you have here, so the points below are about the implementation. There is one blocker and a few smaller items.

Scope: DatabaseRole only

In the issue comment, I suggest limiting roleGrants to the DatabaseRole CRD and not adding it to .spec.managed.roles in the Cluster resource. The main reason is error reporting: a failure for one role is visible on that DatabaseRole (status.applied, status.message, conditions), while managed roles are reported together in the Cluster status. This is also how the TLS client certificates (#10896) work: they exist only on DatabaseRole. DatabaseRole currently inlines RoleConfiguration, which is shared with the managed roles, so roleGrants would need to move out of RoleConfiguration and into the DatabaseRole spec. The role reconciler would then receive the grants from the DatabaseRole side, and the managed roles keep working with inRoles only.

Blocker: PostgreSQL 14 and 15

We support PostgreSQL 14–18, but this PR assumes PG 16+. I checked the SQL on PostgreSQL 14.24 and 15.19, and the reconciler needs two paths:

PG ≥ 16 PG 14–15
Catalog read admin_option, inherit_option, set_option admin_option only
admin: true GRANT r TO m WITH ADMIN true GRANT r TO m WITH ADMIN OPTION
admin back to false GRANT r TO m WITH ADMIN false REVOKE ADMIN OPTION FOR r FROM m
Per-grant inherit / set supported not supported

Today:

  • ListRoles and GetParentRoles (roles/postgres.go) select inherit_option and set_option unconditionally. On 14 and 15 that fails with column am.inherit_option does not exist and breaks the whole role reconciler, even for clusters that don't use roleGrants.
  • ToWithOptionQuery (roles/contract.go) emits WITH ADMIN true, which is a syntax error before PG 16.

Please:

  • Pick the catalog query and the SQL builder from the server major version.
  • On PG < 16, reject inherit and set in roleGrants and report it on the DatabaseRole status (applied: false with a clear message), since the version is only known at reconcile time. Please don't ignore them silently.
  • Add unit tests for both SQL paths and an e2e run on PG 15.

Validation

  • Reject a role that appears in both inRoles and roleGrants. Today the entries are collapsed by name, so one silently wins.
  • Reject duplicate names within roleGrants.
  • Add MinLength=1 on RoleGrant.name.
  • Apply these rules on the DatabaseRole CRD, where roleGrants lives.

Reconciliation semantics

  • Create the role with no memberships and reconcile everything (inRoles and roleGrants) through the same GRANT/REVOKE path. That drops IN ROLE at creation, so create and update behave the same, and it matches the existing inRoles docs.
  • The "omitted options are not managed" behaviour discussed earlier in this review is fine with me, and the RoleGrant comment documents it well. Please repeat it in the user docs.
  • Please add tests for option transitions, including admin on to off on both paths.

Docs

Nothing under docs/src mentions roleGrants yet, so please add user documentation covering the following:

  • Explain how role-level inherit relates to roleGrants[].inherit and set.
  • Document that admin lets the member grant and revoke the role to others, so users should treat it as privileged.
  • Document that inRoles is the simple form and roleGrants is the form with options.
  • Document the per-version table above.

This is my own feedback, and I'd like the other maintainers, who are more aligned with the code than I am, to weigh in as well, particularly on limiting roleGrants to DatabaseRole and on the interface proposed in the issue.

Thanks again, and let me know if any of this is unclear.

@gbartolini

Copy link
Copy Markdown
Contributor

Hi @jonasbadstuebner, do you have time to update this PR? If not, some of us may take over the issue and decide whether to use your work as a base or start a new one. Please let us know. Thank you!

@jonasbadstuebner

Copy link
Copy Markdown
Contributor Author

Hey @gbartolini,
time is a very precious resource right now, please feel free to take over.

I really like your concept and idea, would implement the feedback as-is, if I had the time.
I think even if I would code it, thoroughly testing it would take more time than I can offer right now.

@gbartolini

Copy link
Copy Markdown
Contributor

Closing this PR.

@gbartolini gbartolini closed this Nov 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-requested ◀️ This pull request should be backported to all supported releases enhancement 🪄 New feature or request release-1.28 release-1.30 size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

5 participants