Skip to content

Commit

Permalink
fix(roles): properly quote inRoles in SQL statements (cloudnative-pg#…
Browse files Browse the repository at this point in the history
…6346)

This patch fixes an issue where the `inRoles` parameter was not properly
quoted in SQL statements, which could cause syntax errors if the role
name contains special characters.

Closes cloudnative-pg#6337

Signed-off-by: wolfox <[email protected]>
  • Loading branch information
gabriele-wolfox authored Dec 19, 2024
1 parent c59451a commit bc55791
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
8 changes: 7 additions & 1 deletion internal/management/controller/roles/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,13 @@ func GetParentRoles(ctx context.Context, db *sql.DB, role DatabaseRole) ([]strin

func appendInRoleOptions(role DatabaseRole, query *strings.Builder) {
if len(role.InRoles) > 0 {
query.WriteString(fmt.Sprintf(" IN ROLE %s ", strings.Join(role.InRoles, ",")))
quotedInRoles := make([]string, len(role.InRoles))

for i, inRole := range role.InRoles {
quotedInRoles[i] = pgx.Identifier{inRole}.Sanitize()
}

query.WriteString(fmt.Sprintf(" IN ROLE %s ", strings.Join(quotedInRoles, ",")))
}
}

Expand Down
8 changes: 4 additions & 4 deletions internal/management/controller/roles/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,22 +104,22 @@ var _ = Describe("Postgres RoleManager implementation test", func() {
}
wantedRoleExpectedCrtStmt := fmt.Sprintf(
"CREATE ROLE \"%s\" BYPASSRLS NOCREATEDB CREATEROLE NOINHERIT LOGIN NOREPLICATION "+
"NOSUPERUSER CONNECTION LIMIT 2 IN ROLE pg_monitoring VALID UNTIL '2100-01-01 00:00:00Z'",
"NOSUPERUSER CONNECTION LIMIT 2 IN ROLE \"pg_monitoring\" VALID UNTIL '2100-01-01 00:00:00Z'",
wantedRole.Name)

wantedRoleWithPassExpectedCrtStmt := fmt.Sprintf(
"CREATE ROLE \"%s\" BYPASSRLS NOCREATEDB CREATEROLE NOINHERIT LOGIN NOREPLICATION "+
"NOSUPERUSER CONNECTION LIMIT 2 IN ROLE pg_monitoring PASSWORD 'myPassword' VALID UNTIL '2100-01-01 00:00:00Z'",
"NOSUPERUSER CONNECTION LIMIT 2 IN ROLE \"pg_monitoring\" PASSWORD 'myPassword' VALID UNTIL '2100-01-01 00:00:00Z'",
wantedRole.Name)

wantedRoleWithoutValidUntilExpectedCrtStmt := fmt.Sprintf(
"CREATE ROLE \"%s\" BYPASSRLS NOCREATEDB CREATEROLE NOINHERIT LOGIN NOREPLICATION "+
"NOSUPERUSER CONNECTION LIMIT 2 IN ROLE pg_monitoring PASSWORD 'myPassword'",
"NOSUPERUSER CONNECTION LIMIT 2 IN ROLE \"pg_monitoring\" PASSWORD 'myPassword'",
wantedRole.Name)

wantedRoleWithPassDeletionExpectedCrtStmt := fmt.Sprintf(
"CREATE ROLE \"%s\" BYPASSRLS NOCREATEDB CREATEROLE NOINHERIT LOGIN NOREPLICATION "+
"NOSUPERUSER CONNECTION LIMIT 2 IN ROLE pg_monitoring PASSWORD NULL VALID UNTIL '2100-01-01 00:00:00Z'",
"NOSUPERUSER CONNECTION LIMIT 2 IN ROLE \"pg_monitoring\" PASSWORD NULL VALID UNTIL '2100-01-01 00:00:00Z'",
wantedRole.Name)
wantedRoleWithDefaultConnectionLimitExpectedCrtStmt := fmt.Sprintf(
"CREATE ROLE \"%s\" NOBYPASSRLS NOCREATEDB NOCREATEROLE INHERIT NOLOGIN NOREPLICATION "+
Expand Down

0 comments on commit bc55791

Please sign in to comment.