Skip to content

feat(esa-1866): add CustomRole CRD and controller#351

Merged
tmablunar merged 14 commits intomasterfrom
esa-1866-add-customrole-crd
Apr 10, 2026
Merged

feat(esa-1866): add CustomRole CRD and controller#351
tmablunar merged 14 commits intomasterfrom
esa-1866-add-customrole-crd

Conversation

@tmablunar
Copy link
Copy Markdown
Contributor

@tmablunar tmablunar commented Apr 8, 2026

We want a capability to define custom db roles that are added to all instances. This could (and should) replace something like the hardcoded management_role.

Summary

  • Adds CustomRole CRD (postgresql.lunar.tech/v1alpha1) for declaratively defining a PostgreSQL role with specific permissions
  • Controller creates the role (NOLOGIN), grants server-level roles (e.g. pg_monitor), and applies schema/table privilege grants across all user databases
  • Reconciles automatically when a new PostgreSQLDatabase CR appears (via Watches)
  • Empty or "*" for schema/table expands to all user schemas/tables via pg_namespace/pg_database queries

Stacked on #350.

Test plan

  • Integration tests: POSTGRESQL_CONTROLLER_INTEGRATION_HOST=<host> go test ./pkg/postgres/... -run TestEnsureCustomRole -v
  • Integration tests: POSTGRESQL_CONTROLLER_INTEGRATION_HOST=<host> go test ./pkg/postgres/... -run TestApplyDatabaseGrants -v
  • Integration tests: POSTGRESQL_CONTROLLER_INTEGRATION_HOST=<host> go test ./pkg/postgres/... -run TestUserDatabases -v
  • Apply a CustomRole CR and verify the role + grants exist on the target Postgres instance

🤖 Generated with Claude Code


Note

High Risk
Introduces new reconciliation logic that creates/drops PostgreSQL roles and grants/revokes privileges across all user databases, which is security-sensitive and could impact access if misconfigured or buggy.

Overview
Adds a new namespaced CustomRole CRD for declaring Postgres roles with server-level memberships (grantRoles) and per-database table privileges (grants), plus status phases (Running/Failed/Invalid) and finalizer-based cleanup on deletion.

Implements a CustomRoleReconciler that reconciles the role across all configured hosts, watches PostgreSQLDatabase transitions to Running to re-apply grants for newly provisioned DBs, and updates CR status with retry/drop behavior for temporary vs invalid errors.

Adds Postgres helpers to create NOLOGIN roles, diff-and-sync role memberships and table/schema grants (including wildcard schema/table expansion and skipping missing objects), revoke all grants on deletion, and includes integration tests covering grant combinations and revocation behavior; updates RBAC, CRD manifests, sample YAML, and README documentation accordingly.

Reviewed by Cursor Bugbot for commit 9aca465. Bugbot is set up for automated code reviews on this repo. Configure here.

@tmablunar tmablunar requested a review from a team as a code owner April 8, 2026 09:15
@tmablunar tmablunar force-pushed the chore/upgrade-controller-gen branch from 49a6717 to 090526f Compare April 8, 2026 09:20
@tmablunar tmablunar force-pushed the esa-1866-add-customrole-crd branch from 086ae64 to e516c3c Compare April 8, 2026 09:20
@tmablunar tmablunar force-pushed the chore/upgrade-controller-gen branch from 090526f to d343efa Compare April 8, 2026 09:29
@tmablunar tmablunar force-pushed the esa-1866-add-customrole-crd branch from e516c3c to f69c96e Compare April 8, 2026 09:29
@tmablunar tmablunar force-pushed the esa-1866-add-customrole-crd branch from f69c96e to d19a56a Compare April 8, 2026 11:50
Base automatically changed from chore/upgrade-controller-gen to master April 8, 2026 11:57
tmablunar and others added 3 commits April 8, 2026 13:58
Adds a CustomRole CRD that declaratively defines a PostgreSQL role with
specific permissions. The controller:

- Creates the role (NOLOGIN) on the target PostgreSQL instance
- Grants server-level roles (e.g. pg_monitor, pg_read_all_data)
- Applies schema/table privilege grants across all user databases,
  with empty or "*" schema/table expanding to all user schemas/tables
- Reconciles automatically when a new PostgreSQLDatabase CR appears

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- Quote role/schema/table identifiers with pq.QuoteIdentifier
- Validate privilege keywords against an allowlist to prevent injection
  via unquotable SQL keywords
- Loop over all HostCredentials instead of a single spec-referenced host
- Add customRoleRequeueStrategy with correct log messages

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@tmablunar tmablunar force-pushed the esa-1866-add-customrole-crd branch from d19a56a to 53c2817 Compare April 8, 2026 11:58
- Remove unused `spec.hostCredentials` field from CustomRoleSpec and
  regenerate CRD manifest. The controller intentionally applies custom
  roles to all configured hosts, so a per-host field was misleading and
  never read.
- Wrap invalid-privilege errors with ctlerrors.NewInvalid so the requeue
  strategy correctly drops the CR from the queue (Invalid status) rather
  than retrying every 10 s forever.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- Use metadata.name as PostgreSQL role name (removes spec.roleName field)
- Validate that privileges slice is non-empty in validatePrivileges
- Restrict PostgreSQLDatabase watch to create events only via predicate
- SyncDatabaseGrants: revoke-and-reapply on every reconcile so removed
  privileges and grants converge to desired state
- EnsureCustomRole: revoke role grants no longer in the desired list
- Add finalizer to clean up PostgreSQL role and grants on CR deletion
- Add tests for revoke behaviour

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
… privilege

Replace revoke-all-then-reapply with a proper diff: query current
(schema, table, privilege) tuples from pg_catalog, expand desired grants
against the live database, then issue only the targeted GRANT/REVOKE
statements. This avoids any access outage window during reconciliation.

Also removes ALL from the allowed privilege set so the stored
representation in pg_catalog always matches the spec exactly.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
tmablunar and others added 2 commits April 8, 2026 16:09
- Assert USAGE is revoked when all grants for a schema are removed
- TestSyncDatabaseGrants_partialRevokePreservesSchemaUsage: removing one
  table's grant leaves USAGE intact while the other table still has grants
- TestSyncDatabaseGrants_picksUpNewTable: tables added after the initial
  sync are picked up on the next reconcile
- Add schemaUsageGranted helper querying pg_catalog directly

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
aclexplode() returns full text privilege names (SELECT, UPDATE, INSERT,
etc.) not single-character ACL abbreviations. The CASE conversion was
producing NULL for every row, so the diff always saw an empty current
state and re-granted everything on each reconcile.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
tmablunar and others added 2 commits April 8, 2026 16:45
…baseGrants

Covers all schema/table/privilege permutations in a single test with a
shared 3-schema × 2-table database setup and a fresh role per case:

- specific schema + specific table
- specific schema + all tables (wildcard)
- wildcard schema + specific table
- wildcard schema + all tables (wildcard)
- multiple explicit schemas + specific table
- multiple explicit schemas + all tables
- single schema + multiple specific tables
- multiple schemas + multiple specific tables
- mixed specific and wildcard tables across schemas
- multiple privileges on a specific table
- different privileges per schema

Each case asserts both that expected grants are present and that
out-of-scope (schema, table, privilege) tuples are not granted.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
When a CustomRoleGrant names a concrete schema or table that does not
exist in a given database, expandGrants now logs a warning and skips
the object rather than propagating an error. This prevents a missing
object in one database from blocking grant reconciliation on all
subsequent databases on the same host.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
tmablunar and others added 2 commits April 8, 2026 19:36
- Watch UpdateFunc now fires when a PostgreSQLDatabase transitions to
  Running, ensuring CustomRole grants are applied even when the database
  was not yet provisioned at CreateFunc time.
- Revoke path now filters privilege strings through allowedTablePrivileges,
  consistent with the grant path's validation.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…y skipping

Privilege strings from pg_catalog.aclexplode are trustworthy, so pass them
through to REVOKE instead of filtering against the hardcoded allowlist.
Logs a warning for unrecognized types to aid debugging.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 9aca465. Configure here.

…ML tags from README

currentGrantedSchemas used COALESCE with acldefault which reported
implicit owner USAGE as explicit grants, causing spurious REVOKE USAGE
on every reconcile. Query nspacl directly instead.

Replace <details>/<summary> HTML tags in README examples with markdown
headings.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@tmablunar tmablunar requested a review from mahlunar April 10, 2026 10:23
@tmablunar tmablunar merged commit 00fe598 into master Apr 10, 2026
5 of 6 checks passed
@tmablunar tmablunar deleted the esa-1866-add-customrole-crd branch April 10, 2026 10:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants