Skip to content

refactor: extract a shared GRANT/REVOKE primitive for object privileges - #10829

Merged
leonardoce merged 4 commits into
mainfrom
dev/10828
Jun 5, 2026
Merged

leonardoce merged 4 commits into
mainfrom
dev/10828

Conversation

@gbartolini

@gbartolini gbartolini commented Jun 3, 2026 •

Copy link
Copy Markdown
Contributor

Generalise the database reconciler's privilege handling in preparation for schema-level and database-level grants.

Extract applyObjectPrivilege from applyUsagePermissions, lifting the hardcoded USAGE keyword into a privilege parameter and making the log and error messages privilege-agnostic. applyUsagePermissions becomes a thin wrapper over it, so the FOREIGN DATA WRAPPER and FOREIGN SERVER callers and their tests are unchanged.

Add a sanitizeGrantee helper (and a publicRole constant) that emits the PUBLIC pseudo-role verbatim rather than quoting it as the non-existent role "public". This is a minor, deliberate behaviour change on the existing USAGE path and is required to support REVOKE ... FROM PUBLIC, the most common database-hardening operation.

Closes #10828

Assisted-by: Claude

@gbartolini
gbartolini requested a review from a team June 3, 2026 07:41
@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. chore Intangible work to reduce technical debt labels Jun 3, 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 3, 2026
@github-actions

github-actions Bot commented Jun 3, 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

@gbartolini
gbartolini force-pushed the dev/10828 branch 2 times, most recently from f90555f to 0833459 Compare June 3, 2026 08:36
@armru

armru commented Jun 3, 2026

Copy link
Copy Markdown
Member

/test

@github-actions

github-actions Bot commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label Jun 3, 2026
@cnpg-bot cnpg-bot added the ok to merge 👌 This PR can be merged label Jun 3, 2026
gbartolini and others added 4 commits June 5, 2026 14:02
Generalise the database reconciler's privilege handling in preparation for
schema-level and database-level grants.

Extract applyObjectPrivilege from applyUsagePermissions, lifting the
hardcoded USAGE keyword into a privilege parameter and making the log and
error messages privilege-agnostic. applyUsagePermissions becomes a thin
wrapper over it, so the FOREIGN DATA WRAPPER and FOREIGN SERVER callers and
their tests are unchanged.

Add a sanitizeGrantee helper (and a publicRole constant) that emits the
PUBLIC pseudo-role verbatim rather than quoting it as the non-existent role
"public". This is a minor, deliberate behaviour change on the existing USAGE
path and is required to support REVOKE ... FROM PUBLIC, the most common
database-hardening operation.

Closes #10828

Assisted-by: Claude

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

The new applyObjectPrivilege spec block closed the sqlmock DB in its
AfterEach and asserted success, but go-sqlmock requires an explicit
ExpectClose() for db.Close() to succeed. Without it, db.Close() returns
"call to database Close was not expected" and all seven specs in the
block fail.

Match the convention of the other spec blocks in this file, which only
assert ExpectationsWereMet() and do not close the mock DB.

Signed-off-by: Armando Ruocco <[email protected]>
Fix a grammar slip ("grants or revoke" -> "revokes") and document that
the privilege and objectType arguments are interpolated into the SQL
verbatim, so callers must pass trusted in-code constants and never
user-controlled input.

Signed-off-by: Armando Ruocco <[email protected]>
Signed-off-by: Leonardo Cecchi <[email protected]>
@leonardoce

Copy link
Copy Markdown
Contributor

/test

@github-actions

github-actions Bot commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

@leonardoce
leonardoce merged commit 28eb808 into main Jun 5, 2026
42 checks passed
@leonardoce
leonardoce deleted the dev/10828 branch June 5, 2026 15:04
cnpg-bot pushed a commit that referenced this pull request Jun 5, 2026
…es (#10829)

Generalise the database reconciler's privilege handling in preparation
for schema-level and database-level grants.

Extract applyObjectPrivilege from applyUsagePermissions, lifting the
hardcoded USAGE keyword into a privilege parameter and making the log
and error messages privilege-agnostic. applyUsagePermissions becomes a
thin wrapper over it, so the FOREIGN DATA WRAPPER and FOREIGN SERVER
callers and their tests are unchanged.

Add a sanitizeGrantee helper (and a publicRole constant) that emits the
PUBLIC pseudo-role verbatim rather than quoting it as the non-existent
role "public". This is a minor, deliberate behaviour change on the
existing USAGE path and is required to support REVOKE ... FROM PUBLIC,
the most common database-hardening operation.

Closes #10828

Assisted-by: Claude

Signed-off-by: Gabriele Bartolini <[email protected]>
Signed-off-by: Armando Ruocco <[email protected]>
Signed-off-by: Leonardo Cecchi <[email protected]>
Co-authored-by: Armando Ruocco <[email protected]>
Co-authored-by: Leonardo Cecchi <[email protected]>
(cherry picked from commit 28eb808)
cnpg-bot pushed a commit that referenced this pull request Jun 5, 2026
…es (#10829)

Generalise the database reconciler's privilege handling in preparation
for schema-level and database-level grants.

Extract applyObjectPrivilege from applyUsagePermissions, lifting the
hardcoded USAGE keyword into a privilege parameter and making the log
and error messages privilege-agnostic. applyUsagePermissions becomes a
thin wrapper over it, so the FOREIGN DATA WRAPPER and FOREIGN SERVER
callers and their tests are unchanged.

Add a sanitizeGrantee helper (and a publicRole constant) that emits the
PUBLIC pseudo-role verbatim rather than quoting it as the non-existent
role "public". This is a minor, deliberate behaviour change on the
existing USAGE path and is required to support REVOKE ... FROM PUBLIC,
the most common database-hardening operation.

Closes #10828

Assisted-by: Claude

Signed-off-by: Gabriele Bartolini <[email protected]>
Signed-off-by: Armando Ruocco <[email protected]>
Signed-off-by: Leonardo Cecchi <[email protected]>
Co-authored-by: Armando Ruocco <[email protected]>
Co-authored-by: Leonardo Cecchi <[email protected]>
(cherry picked from commit 28eb808)
leonardoce pushed a commit to BRGOVIND/cloudnative-pg that referenced this pull request Nov 2, 2026
Restructure schema-level privilege management to a permissions stanza
keyed by privilege (usage, create), each entry reusing the existing
UsageSpec {name, type: grant|revoke} model, instead of the flat
usagePrivileges/createPrivileges lists. This aligns the API with the
design proposed in cloudnative-pg#10826 and keeps the surface consistent with the
foreign data wrapper / server usage permissions, building on the shared
applyObjectPrivilege primitive from cloudnative-pg#10829.

Regenerate deepcopy, CRD manifests and the API reference docs, and add
unit tests covering grant/revoke application on schema create and update.

Signed-off-by: B R GOVIND <[email protected]>
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 chore Intangible work to reduce technical debt lgtm This PR has been approved by a maintainer ok to merge 👌 This PR can be merged release-1.28 size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Chore]: refactor object-privilege SQL into a shared GRANT/REVOKE primitive

4 participants