feat(esa-1866): add CustomRole CRD and controller#351
Merged
Conversation
49a6717 to
090526f
Compare
086ae64 to
e516c3c
Compare
090526f to
d343efa
Compare
e516c3c to
f69c96e
Compare
f69c96e to
d19a56a
Compare
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>
d19a56a to
53c2817
Compare
- 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>
tmablunar
commented
Apr 8, 2026
tmablunar
commented
Apr 8, 2026
tmablunar
commented
Apr 8, 2026
tmablunar
commented
Apr 8, 2026
tmablunar
commented
Apr 8, 2026
- 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>
- 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>
…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>
- 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>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ 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.
mahlunar
reviewed
Apr 10, 2026
…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>
mahlunar
approved these changes
Apr 10, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

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
CustomRoleCRD (postgresql.lunar.tech/v1alpha1) for declaratively defining a PostgreSQL role with specific permissionsNOLOGIN), grants server-level roles (e.g.pg_monitor), and applies schema/table privilege grants across all user databasesPostgreSQLDatabaseCR appears (viaWatches)"*"for schema/table expands to all user schemas/tables viapg_namespace/pg_databasequeriesStacked on #350.
Test plan
POSTGRESQL_CONTROLLER_INTEGRATION_HOST=<host> go test ./pkg/postgres/... -run TestEnsureCustomRole -vPOSTGRESQL_CONTROLLER_INTEGRATION_HOST=<host> go test ./pkg/postgres/... -run TestApplyDatabaseGrants -vPOSTGRESQL_CONTROLLER_INTEGRATION_HOST=<host> go test ./pkg/postgres/... -run TestUserDatabases -vCustomRoleCR 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
CustomRoleCRD 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
CustomRoleReconcilerthat reconciles the role across all configured hosts, watchesPostgreSQLDatabasetransitions toRunningto 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
NOLOGINroles, 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.