Skip to content

Commit efefe2f

Browse files
Merge pull request #3 from Facets-cloud/fix-seq-privileges
filter valid permissions for sequences and use them in grant query
2 parents a9c3f75 + 167910e commit efefe2f

File tree

4 files changed

+79
-7
lines changed

4 files changed

+79
-7
lines changed

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ RUN go mod download
1515
COPY main.go main.go
1616
COPY apis/ apis/
1717
COPY controllers/ controllers/
18+
COPY utility/ utility/
1819

1920
# Build
2021
# the GOARCH has not a default value to allow the binary be built according to the host where the command

controllers/postgresql/grant_controller.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import (
4242

4343
"github.com/Facets-cloud/postgresql-operator/apis/common"
4444
postgresql "github.com/Facets-cloud/postgresql-operator/apis/postgresql/v1alpha1"
45+
"github.com/Facets-cloud/postgresql-operator/utility"
4546
"github.com/google/go-cmp/cmp"
4647
"github.com/lib/pq"
4748
)
@@ -438,10 +439,14 @@ func (r *GrantReconciler) CreateGrant(ctx context.Context, grantType string, gra
438439
// https://www.postgresql.org/docs/current/sql-alterdefaultprivileges.html
439440
createGrantQueryForFutureTables := fmt.Sprintf("ALTER DEFAULT PRIVILEGES IN SCHEMA %s GRANT %s ON TABLES TO \"%s\"", schema, privileges, roleName)
440441
createGrantQueryForExistingTables := fmt.Sprintf("GRANT %s ON ALL TABLES IN SCHEMA %s TO \"%s\"", privileges, schema, roleName)
441-
createGrantSeqQueryForFutureTables := fmt.Sprintf("ALTER DEFAULT PRIVILEGES IN SCHEMA %s GRANT %s ON SEQUENCES TO \"%s\"", schema, privileges, roleName)
442-
createGrantSeqQueryForExistingTables := fmt.Sprintf("GRANT %s ON ALL SEQUENCES IN SCHEMA %s TO \"%s\"", privileges, schema, roleName)
443-
444-
createGrantQuery = strings.Join([]string{createGrantQueryForFutureTables, createGrantQueryForExistingTables, createGrantSeqQueryForFutureTables, createGrantSeqQueryForExistingTables}, "; ")
442+
sequencePrivileges := utility.GenerateSequencePrivileges(privileges)
443+
createGrantSeqQueryForFutureTables := fmt.Sprintf("ALTER DEFAULT PRIVILEGES IN SCHEMA %s GRANT %s ON SEQUENCES TO \"%s\"", schema, sequencePrivileges, roleName)
444+
createGrantSeqQueryForExistingTables := fmt.Sprintf("GRANT %s ON ALL SEQUENCES IN SCHEMA %s TO \"%s\"", sequencePrivileges, schema, roleName)
445+
if strings.Compare(sequencePrivileges, "") == 0 {
446+
createGrantQuery = strings.Join([]string{createGrantQueryForFutureTables, createGrantQueryForExistingTables}, "; ")
447+
} else {
448+
createGrantQuery = strings.Join([]string{createGrantQueryForFutureTables, createGrantQueryForExistingTables, createGrantSeqQueryForFutureTables, createGrantSeqQueryForExistingTables}, "; ")
449+
}
445450
} else {
446451
createGrantQuery = fmt.Sprintf("GRANT %s ON %s.%s TO \"%s\"", privileges, schema, table, roleName)
447452
}
@@ -532,9 +537,14 @@ func (r *GrantReconciler) SyncGrant(ctx context.Context, grantType string, grant
532537
// https://www.postgresql.org/docs/current/sql-alterdefaultprivileges.html
533538
syncGrantQueryForFutureTables := fmt.Sprintf("ALTER DEFAULT PRIVILEGES IN SCHEMA %s GRANT %s ON TABLES TO \"%s\"", schema, privileges, roleName)
534539
syncGrantQueryForExistingTables := fmt.Sprintf("GRANT %s ON ALL TABLES IN SCHEMA %s TO \"%s\"", privileges, schema, roleName)
535-
syncGrantSeqQueryForFutureTables := fmt.Sprintf("ALTER DEFAULT PRIVILEGES IN SCHEMA %s GRANT %s ON SEQUENCES TO \"%s\"", schema, privileges, roleName)
536-
syncGrantSeqQueryForExistingTables := fmt.Sprintf("GRANT %s ON ALL SEQUENCES IN SCHEMA %s TO \"%s\"", privileges, schema, roleName)
537-
syncGrantQuery = strings.Join([]string{syncGrantQueryForFutureTables, syncGrantQueryForExistingTables, syncGrantSeqQueryForFutureTables, syncGrantSeqQueryForExistingTables}, "; ")
540+
sequencePrivileges := utility.GenerateSequencePrivileges(privileges)
541+
syncGrantSeqQueryForFutureTables := fmt.Sprintf("ALTER DEFAULT PRIVILEGES IN SCHEMA %s GRANT %s ON SEQUENCES TO \"%s\"", schema, sequencePrivileges, roleName)
542+
syncGrantSeqQueryForExistingTables := fmt.Sprintf("GRANT %s ON ALL SEQUENCES IN SCHEMA %s TO \"%s\"", sequencePrivileges, schema, roleName)
543+
if strings.Compare(sequencePrivileges, "") == 0 {
544+
syncGrantQuery = strings.Join([]string{syncGrantQueryForFutureTables, syncGrantQueryForExistingTables}, "; ")
545+
} else {
546+
syncGrantQuery = strings.Join([]string{syncGrantQueryForFutureTables, syncGrantQueryForExistingTables, syncGrantSeqQueryForFutureTables, syncGrantSeqQueryForExistingTables}, "; ")
547+
}
538548
} else {
539549
syncGrantQuery = fmt.Sprintf("GRANT %s ON %s.%s TO \"%s\"", privileges, schema, table, roleName)
540550
}

utility/privileges.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package utility
2+
3+
import "strings"
4+
5+
func GenerateSequencePrivileges(privileges string) string {
6+
7+
// map of valid privilges that can be granted on sequence
8+
allowedPrivileges := map[string]struct{}{"SELECT": {}, "UPDATE": {}, "USAGE": {}}
9+
10+
// filtering the valid privileges
11+
splitStrings := strings.Split(privileges, ",")
12+
13+
sequencePrivileges := make([]string, 0)
14+
for _, str := range splitStrings {
15+
trimmedStr := strings.TrimSpace(str)
16+
_, ok := allowedPrivileges[trimmedStr]
17+
if ok {
18+
sequencePrivileges = append(sequencePrivileges, trimmedStr)
19+
}
20+
}
21+
22+
sequencePrivilegesStr := strings.Join(sequencePrivileges, ", ")
23+
24+
return sequencePrivilegesStr
25+
}

utility/privileges_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package utility
2+
3+
import "testing"
4+
5+
func TestGenerateSequencePrivileges(t *testing.T) {
6+
type args struct {
7+
privileges string
8+
}
9+
tests := []struct {
10+
name string
11+
args args
12+
want string
13+
}{
14+
{
15+
name: "test has sequence privileges",
16+
args: args{
17+
privileges: " INSERT , SELECT,UPDATE, DELETE, TRUNCATE, REFERENCES, TRIGGER, USAGE ",
18+
},
19+
want: "SELECT, UPDATE, USAGE",
20+
},
21+
{
22+
name: "test has no sequence privileges",
23+
args: args{
24+
privileges: " INSERT, DELETE, TRUNCATE, REFERENCES, TRIGGER",
25+
},
26+
want: "",
27+
},
28+
}
29+
for _, tt := range tests {
30+
t.Run(tt.name, func(t *testing.T) {
31+
if got := GenerateSequencePrivileges(tt.args.privileges); got != tt.want {
32+
t.Errorf("GenerateSequencePrivileges() = %v, want %v", got, tt.want)
33+
}
34+
})
35+
}
36+
}

0 commit comments

Comments
 (0)