Fix default privileges by adding FOR ROLE clause#319
Open
gcaracuel wants to merge 1 commit intomovetokube:masterfrom
Open
Fix default privileges by adding FOR ROLE clause#319gcaracuel wants to merge 1 commit intomovetokube:masterfrom
gcaracuel wants to merge 1 commit intomovetokube:masterfrom
Conversation
Fixes movetokube#318 Add FOR ROLE clause to ALTER DEFAULT PRIVILEGES commands to ensure that privileges are applied to objects created by owner and writer group roles, not just the operator's connection user. This ensures that when users create tables, sequences, or functions while operating as their group role (via SET ROLE), the reader and writer roles automatically receive appropriate permissions.
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.
Fixes #318
Root Cause
The operator was executing
ALTER DEFAULT PRIVILEGEScommands without theFOR ROLEclause:When
FOR ROLEis not specified, PostgreSQL only applies default privileges to objects created by the current user executing the command (in this case, the postgres superuser that the operator connects with).However, when users are provisioned by the operator:
myapp-abc123) with LOGIN privilegemydb-groupfor owner,mydb-writerfor writer,mydb-readerfor reader)ALTER USER "myapp-abc123" SET ROLE "mydb-group"(see postgresuser_controller.go:158)mydb-group), not by the postgres superuserResult: Default privileges don't apply to these newly created tables, causing writer and reader roles to have no access.
Steps to Reproduce
postgres)Solution
Add
FOR ROLEclause toALTER DEFAULT PRIVILEGEScommands to specify which roles' object creations should have default privileges applied.The fix ensures that when the owner role or writer role creates objects (tables, sequences, functions), the reader and writer roles automatically receive appropriate privileges.
Example SQL Generated After Fix
For a database named
mydb, the operator now executes:Changes Made
SQL Constants (pkg/postgres/database.go:19,21,23)
FOR ROLE "%s"clause to all threeALTER DEFAULT PRIVILEGESconstantsALTER DEFAULT PRIVILEGES FOR ROLE "%s" IN SCHEMA "%s" GRANT %s ON TABLES TO "%s"Schema Privileges Struct (pkg/postgres/postgres.go:41-49)
CreatorRolefield toPostgresSchemaPrivilegesstructFOR ROLEclause)SetSchemaPrivileges Function (pkg/postgres/database.go:152,165,179)
fmt.Sprintfcalls to includeschemaPrivileges.CreatorRoleas the first parameterController Reconciliation Loop (internal/controller/postgres_controller.go:235-283)
SetSchemaPrivileges()per schema (one per role) to 6 calls per schemaownerandwriter) and sets default privileges for each combination of (recipient role, creator role)