Repository navigation
feat(database): manage the privileges on a database declaratively - #11906
Conversation
|
❗ By default, the pull request is configured to backport to all release branches.
|
e6d32c8 to
7993de4
Compare
|
/test |
|
@leonardoce, here's the link to the E2E on CNPG workflow run: https://github.com/cloudnative-pg/cloudnative-pg/actions/runs/43185690999 |
7993de4 to
629258e
Compare
d368e2b to
3d2d7ea
Compare
|
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:
Let me know what you think. I checked it locally and it works really well. |
|
/test |
|
@gbartolini, here's the link to the E2E on CNPG workflow run: https://github.com/cloudnative-pg/cloudnative-pg/actions/runs/43318456503 |
0e07d2e to
8a68caa
Compare
|
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:
What do you think? |
8a68caa to
aaa2cf8
Compare
aaa2cf8 to
45b5bd6
Compare
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]>
Signed-off-by: Gabriele Bartolini <[email protected]>
45b5bd6 to
16c6adb
Compare
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]>
2c4858b to
03e8b16
Compare
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]>
|
/test |
|
@mnencia, here's the link to the E2E on CNPG workflow run: https://github.com/cloudnative-pg/cloudnative-pg/actions/runs/44371602171 |
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]>
|
/test d=push ft=declarative-databases |
|
@mnencia, here's the link to the E2E on CNPG workflow run: https://github.com/cloudnative-pg/cloudnative-pg/actions/runs/44381520467 |
…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)
Users can now grant and revoke the
CONNECT,CREATEandTEMPORARYprivileges on a database from theDatabaseresource, with the newspec.permissionsstanza, instead of running SQL scripts after the database is created. The most common use is hardening: PostgreSQL grantsCONNECTandTEMPORARYtoPUBLICon every new database, so a production setup revokesCONNECTfrompublicand grants it back to specific roles.Each entry names a role and a
typeofgrant(the default) orrevoke, as the usage entries of schemas and foreign data wrappers do. Thealllist is a shortcut for the three privileges. Entries are applied in the order they are listed, grouped by privilege, with theallentries last. The namepublic, in any letter case, is thePUBLICpseudo-role, as for the usages of schemas and foreign data wrappers, so a role namedPublicorPUBLICcannot be targeted (see #11955).The operator reads the privileges currently held on the database and runs a
GRANTorREVOKEonly when it is needed. The outcome of every privilege and role is reported instatus.permissions, withallexpanded. A role that does not exist is reported on its own entry without stopping the others, and theDatabaseis 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
revokeremoves only the privilege granted to the named role itself, not the ones it holds throughPUBLICor 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