Skip to content

Commit 7707639

Browse files
Merge pull request #10 from Facets-cloud/role-sync-fix
remove superuser and replication permission check from sync query if not set in privileges
2 parents 48d84e1 + 08e631a commit 7707639

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

controllers/postgresql/role_controller.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,9 +354,29 @@ func (r *RoleReconciler) DeletRole(ctx context.Context, role *v1alpha1.Role) (st
354354
}
355355

356356
func (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

Comments
 (0)