Skip to content

feat(database): manage the privileges on a database declaratively - #11906

Merged
mnencia merged 18 commits into
cloudnative-pg:mainfrom
leonardoce:cloudnative-pg-10826
Nov 19, 2026
Merged

mnencia merged 18 commits into
cloudnative-pg:mainfrom
leonardoce:cloudnative-pg-10826

Conversation

@leonardoce

@leonardoce leonardoce commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Users can now grant and revoke the CONNECT, CREATE and TEMPORARY privileges on a database from the Database resource, with the new spec.permissions stanza, instead of running SQL scripts after the database is created. The most common use is hardening: PostgreSQL grants CONNECT and TEMPORARY to PUBLIC on every new database, so a production setup revokes CONNECT from public and grants it back to specific roles.

Each entry names a role and a type of grant (the default) or revoke, as the usage entries of schemas and foreign data wrappers do. The all list is a shortcut for the three privileges. Entries are applied in the order they are listed, grouped by privilege, with the all entries last. The name public, in any letter case, is the PUBLIC pseudo-role, as for the usages of schemas and foreign data wrappers, so a role named Public or PUBLIC cannot be targeted (see #11955).

The operator reads the privileges currently held on the database and runs a GRANT or REVOKE only when it is needed. The outcome of every privilege and role is reported in status.permissions, with all expanded. A role that does not exist is reported on its own entry without stopping the others, and the Database is retried every 30 seconds until the role exists. Listing a role twice for a privilege, whether granted and revoked or not, is rejected when the resource is created or updated.

A revoke removes only the privilege granted to the named role itself, not the ones it holds through PUBLIC or another role. As for the other managed objects, privileges are applied when the spec changes, and removing an entry does not undo it. The documentation explains both.

Closes #10826

Assisted-by: Claude

@leonardoce
leonardoce requested review from a team, NiccoloFei and litaocdl as code owners November 12, 2026 08:33
@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 12, 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

@leonardoce leonardoce 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 12, 2026
@leonardoce

Copy link
Copy Markdown
Contributor Author

/test

@github-actions

Copy link
Copy Markdown
Contributor

@cnpg-bot cnpg-bot added the ok to merge 👌 This PR can be merged label Nov 12, 2026
@gbartolini
gbartolini force-pushed the cloudnative-pg-10826 branch 2 times, most recently from d368e2b to 3d2d7ea Compare November 12, 2026 20:02
@gbartolini

Copy link
Copy Markdown
Contributor

I have made a few changes @leonardoce. Please check every single commit I have made and let me know if you don't agree. In particular:

  1. I have made consistent the behaviour of the public pseudo-role with the existing one for FDWs and schemas, purely for practical reasons
  2. I have adopted a deterministic approach based on the order of specification of permissions, rather than a sort

Let me know what you think.

I checked it locally and it works really well.

@gbartolini

Copy link
Copy Markdown
Contributor

/test

@github-actions

Copy link
Copy Markdown
Contributor

@gbartolini

Copy link
Copy Markdown
Contributor

Tests have successully passed. Thanks @leonardoce for this.

A few things are deliberately not part of this first version, and we are postponing them to a future release:

  • Delegated privileges. The operator manages the privileges granted by the database owner. A privilege that a role obtained from another role holding WITH GRANT OPTION is not seen and is not removed by a revoke entry, and the entry is still reported as applied. The documentation states this boundary.
  • WITH GRANT OPTION, GRANTED BY and CASCADE. None of them can be expressed yet. The same applies to the schema, foreign data wrapper and foreign server usages that share UsageSpec, and, with WITH ADMIN OPTION, to role memberships (inRoles), where the grantor-aware revoke is already being handled separately.
  • Status of a revoke that cannot take effect. Checking the effective privilege after a revoke, and saying which grantor still holds it, is also left for later.

What do you think?

@mnencia
mnencia force-pushed the cloudnative-pg-10826 branch from aaa2cf8 to 45b5bd6 Compare November 16, 2026 15:04
leonardoce and others added 4 commits November 18, 2026 15:11
Users can now grant and revoke the `CONNECT`, `CREATE` and `TEMPORARY`
privileges on a database from the `Database` resource, with the new
`spec.permissions` stanza, instead of running SQL scripts after the
database is created. The most common use is hardening: PostgreSQL grants
`CONNECT` and `TEMPORARY` to `PUBLIC` on every new database, so a
production setup revokes `CONNECT` from `public` and grants it back to
specific roles.

Each entry names a role and a `type` of `grant` (the default) or
`revoke`, as the usage entries of schemas and foreign data wrappers do.
The `all` list is a shortcut for the three privileges. The name `public`
is the `PUBLIC` pseudo-role, and only that exact spelling: `PUBLIC` and
`Public` are ordinary role names, which PostgreSQL allows.

The operator reads the privileges currently held on the database and
runs a `GRANT` or `REVOKE` only when it is needed, so a reconciliation
that finds nothing to change executes nothing. The outcome of every
privilege and role is reported in `status.permissions`, with `all`
expanded. A role that does not exist is reported on its own entry
without stopping the others, and the `Database` is retried every 30
seconds until the role exists. Listing a role twice for a privilege,
whether granted and revoked or not, is rejected when the resource is
created or updated.

A `revoke` removes only the privilege granted to the named role itself,
not the ones it holds through `PUBLIC` or another role. As for the other
managed objects, privileges are applied when the spec changes, and
removing an entry does not undo it. The documentation explains both.

Closes cloudnative-pg#10826

Assisted-By: Claude

Signed-off-by: Leonardo Cecchi <[email protected]>
UsageSpec is no longer limited to foreign data wrappers: it is used for
schema, foreign server and database-level privileges. Update the Go doc
comments, and the generated CRD and API reference, accordingly.

Signed-off-by: Gabriele Bartolini <[email protected]>
A privilege delegated through `WITH GRANT OPTION` and granted by another
role is not visible to the operator, so a `revoke` entry cannot remove it.
Document this boundary in the declarative database management page.

Signed-off-by: Gabriele Bartolini <[email protected]>
…ermissions

Database-level permissions had their own rule, where only the exact
lowercase `public` was the PUBLIC pseudo-role. Schema, foreign data wrapper
and foreign server usages instead treat every spelling of `public` as
PUBLIC, through `sanitizeGrantee` and the webhook's folding of the name.

Use the same handling here, so that the whole `Database` resource follows
a single rule: render grantees with `sanitizeGrantee`, and fold every
spelling of `public` when detecting duplicate or conflicting entries, in
both the webhook and the instance manager. Drop the `databaseGrantee`
helper and the `publicGranteeName` constant, and update the documentation
and the tests accordingly.

Assisted-by: Claude

Signed-off-by: Gabriele Bartolini <[email protected]>
The privilege changes were sorted by role name, which made the order in
which GRANT and REVOKE statements run unrelated to what the user wrote:
revoking CONNECT from `public` could run before the grants that were
meant to replace it, and a failure in between left the roles without it.

Apply the changes in the order they are listed, grouped by privilege, as
the usages of schemas and foreign data wrappers already are, so that the
user stays in control of the order. Document it, and advise listing the
grants before the revokes.

Assisted-by: Claude

Signed-off-by: Gabriele Bartolini <[email protected]>
Revoking CONNECT from public also cuts off the metrics exporter, which
connects as the cnpg_metrics_exporter role and not as the superuser. The
user-defined metrics that target the database then fail, or are skipped
when the target is a pattern. Tell users to grant it CONNECT too.

Signed-off-by: Marco Nenciarini <[email protected]>
Listing the grants before a revoke from public gives the roles the
privilege before public loses it. It does not protect them when a grant
fails, because the entries are independent and the revoke still runs.
Say what the order really gives, and that the failed grant is retried.

Signed-off-by: Marco Nenciarini <[email protected]>
The entries of the all list are applied after the entries of each
privilege, whatever their position in the manifest. The documentation
only said that the entries follow the order they are listed.

Signed-off-by: Marco Nenciarini <[email protected]>
The name public is the PUBLIC pseudo-role in any letter case, so a role
named Public or PUBLIC cannot be targeted, and an entry with that name
applies to every role. The applied field only compares the privilege
granted by the database owner, so say so.

Signed-off-by: Marco Nenciarini <[email protected]>
The controller calls reconcileDatabasePermissions and fails the
reconciliation while an entry is not applied, but no test exercised
either step: removing them left every test green. Add a controller test
that checks both. Drop the test that claimed to cover a NULL datacl: it
only fed a hand-built ACL to the planner, and the first planning test
already covers the revoke from PUBLIC.

Signed-off-by: Marco Nenciarini <[email protected]>
Fix the comments that no longer matched the code. A repeated entry is
flagged on the first change, not deduplicated, and the schemas have no
all shortcut to compare with. The err field is also set for repeated
entries and failed statements, and the planning error replaces the one a
change may already have. Drop the stale comment on the privilege
constants, the history of a rejected design and a paragraph that the
granteeKey comment already says.

Signed-off-by: Marco Nenciarini <[email protected]>
@mnencia mnencia changed the title feat: manage the privileges on a database declaratively feat(database): manage the privileges on a database declaratively Nov 18, 2026
@mnencia
mnencia force-pushed the cloudnative-pg-10826 branch from 2c4858b to 03e8b16 Compare November 18, 2026 17:10
PostgreSQL reserves the name public, so no role can have it. Say so next
to the note on the letter case of the name.

Signed-off-by: Marco Nenciarini <[email protected]>
The e2e only checked the privileges of PUBLIC, so the lookup of a role
and the expansion of all were never exercised against PostgreSQL. Grant
all to a managed role, check that it holds each privilege, then revoke
it and check that it holds none.

Signed-off-by: Marco Nenciarini <[email protected]>
The metrics exporter queries the application database by default, so
revoking CONNECT from public on that database stops all the default
metrics, not only the user-defined ones. List the grant before the revoke
in the example, as the text advises, and revoke temporary from public
there: revoking it from the owner has no effect. Say that a failed
reconciliation is retried, and mention the privileges in the feature list.

Signed-off-by: Marco Nenciarini <[email protected]>
Nothing failed when the entries of all were applied before the ones of
the privilege, which the documentation promises. Add a test for it and
use the same order in the webhook test. The e2e query returned false for
a role that does not exist, so a revoke check on a mistyped role passed
without checking anything: return NULL instead.

Signed-off-by: Marco Nenciarini <[email protected]>
@mnencia

mnencia commented Nov 18, 2026

Copy link
Copy Markdown
Member

/test

@github-actions

Copy link
Copy Markdown
Contributor

The hardening example cut the metrics exporter off when it was applied
to the application database, so grant CONNECT to cnpg_metrics_exporter
there. Say that a revoke runs without CASCADE and fails while the role
has granted the privilege to others, and link the default set of
metrics instead of the whole page.

Signed-off-by: Marco Nenciarini <[email protected]>
Two unit assertions checked nothing: the default type was read from an
entry with an explicit type, and a failed change was planned against an
ACL where the grant was already a no-op. The webhook tests did not tell
a conflict from a duplicate. The e2e query returned false for a missing
database and interpolated names without escaping, and the permission
steps passed silently if the fixture lost its permissions.

Signed-off-by: Marco Nenciarini <[email protected]>
@mnencia

mnencia commented Nov 18, 2026

Copy link
Copy Markdown
Member

/test d=push ft=declarative-databases

@github-actions

Copy link
Copy Markdown
Contributor

@mnencia
mnencia merged commit 7f3d6ae into cloudnative-pg:main Nov 19, 2026
1 check passed
cnpg-bot pushed a commit to cloudnative-pg/api that referenced this pull request Nov 19, 2026
…oudnative-pg/cloudnative-pg#11906)

Users can now grant and revoke the `CONNECT`, `CREATE` and `TEMPORARY`
privileges on a database from the `Database` resource, with the new
`spec.permissions` stanza, instead of running SQL scripts after the
database is created. The most common use is hardening: PostgreSQL grants
`CONNECT` and `TEMPORARY` to `PUBLIC` on every new database, so a
production setup revokes `CONNECT` from `public` and grants it back to
specific roles.

Each entry names a role and a `type` of `grant` (the default) or
`revoke`, as the usage entries of schemas and foreign data wrappers do.
The `all` list is a shortcut for the three privileges. Entries are
applied in the order they are listed, grouped by privilege, with the
`all` entries last. The name `public`, in any letter case, is the
`PUBLIC` pseudo-role, as for the usages of schemas and foreign data
wrappers, so a role named `Public` or `PUBLIC` cannot be targeted (see
cloudnative-pg/cloudnative-pg#11955).

The operator reads the privileges currently held on the database and
runs a `GRANT` or `REVOKE` only when it is needed. The outcome of every
privilege and role is reported in `status.permissions`, with `all`
expanded. A role that does not exist is reported on its own entry
without stopping the others, and the `Database` is retried every 30
seconds until the role exists. Listing a role twice for a privilege,
whether granted and revoked or not, is rejected when the resource is
created or updated.

A `revoke` removes only the privilege granted to the named role itself,
not the ones it holds through `PUBLIC` or another role. As for the other
managed objects, privileges are applied when the spec changes, and
removing an entry does not undo it. The documentation explains both.

Closes cloudnative-pg/cloudnative-pg#10826

Assisted-by: Claude
Signed-off-by: Leonardo Cecchi <[email protected]>
Signed-off-by: Gabriele Bartolini <[email protected]>
Signed-off-by: Marco Nenciarini <[email protected]>
Co-authored-by: Gabriele Bartolini <[email protected]>
Co-authored-by: Marco Nenciarini <[email protected]>

(cherry picked from commit cloudnative-pg/cloudnative-pg@7f3d6ae)
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 ok to merge 👌 This PR can be merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: Support for database-level GRANT/REVOKE privileges in the Database CRD

4 participants