@@ -354,9 +354,29 @@ func (r *RoleReconciler) DeletRole(ctx context.Context, role *v1alpha1.Role) (st
354354}
355355
356356func (r * RoleReconciler ) SyncRole (ctx context.Context , role * postgresql.Role , rolePassword string , isPasswordSync bool ) (string , metav1.ConditionStatus , string , string ) {
357- privileges := strings .Join (PrivilegesToClauses (role .Spec .Privileges ), " " )
357+ privileges := PrivilegesToClauses (role .Spec .Privileges )
358+
359+ // Remove SUPERUSER and REPLICATION clauses if not explicitly required
360+ privilegesToCheck := []struct {
361+ enabled * bool
362+ keyword string
363+ }{
364+ {role .Spec .Privileges .SuperUser , "SUPERUSER" },
365+ {role .Spec .Privileges .Replication , "REPLICATION" },
366+ }
367+
368+ for _ , check := range privilegesToCheck {
369+ if check .enabled == nil || ! * check .enabled {
370+ for i , clause := range privileges {
371+ if strings .Contains (clause , check .keyword ) {
372+ privileges = append (privileges [:i ], privileges [i + 1 :]... )
373+ break
374+ }
375+ }
376+ }
377+ }
358378
359- alterRoleQuery := fmt .Sprintf ("ALTER ROLE \" %s\" WITH %s PASSWORD '%s' CONNECTION LIMIT %d" , role .Name , privileges , rolePassword , * role .Spec .ConnectionLimit )
379+ alterRoleQuery := fmt .Sprintf ("ALTER ROLE \" %s\" WITH %s PASSWORD '%s' CONNECTION LIMIT %d" , role .Name , strings . Join ( privileges , " " ) , rolePassword , * role .Spec .ConnectionLimit )
360380 _ , err := roleDB .Exec (alterRoleQuery )
361381 if err != nil {
362382 if strings .Contains (err .Error (), fmt .Sprintf ("pq: role \" %s\" does not exist" , role .Name )) {
0 commit comments