Skip to content

Commit 09d4aa2

Browse files
committed
feat: Gratn Additional Access for Sequences and Functions
1 parent a2f2035 commit 09d4aa2

File tree

4 files changed

+83
-40
lines changed

4 files changed

+83
-40
lines changed

cmd/main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,15 @@ func main() {
4747
var secureMetrics bool
4848
var enableHTTP2 bool
4949
var tlsOpts []func(*tls.Config)
50-
flag.StringVar(&metricsAddr, "metrics-bind-address", "0", "The address the metrics endpoint binds to. "+
50+
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metrics endpoint binds to. "+
5151
"Use :8443 for HTTPS or :8080 for HTTP, or leave as 0 to disable the metrics service.")
5252
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
53-
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
53+
flag.BoolVar(&enableLeaderElection, "leader-elect", true,
5454
"Enable leader election for controller manager. "+
5555
"Enabling this will ensure there is only one active controller manager.")
56-
flag.BoolVar(&secureMetrics, "metrics-secure", true,
56+
flag.BoolVar(&secureMetrics, "metrics-secure", false,
5757
"If set, the metrics endpoint is served securely via HTTPS. Use --metrics-secure=false to use HTTP instead.")
58-
flag.BoolVar(&enableHTTP2, "enable-http2", false,
58+
flag.BoolVar(&enableHTTP2, "enable-http2", true,
5959
"If set, HTTP/2 will be enabled for the metrics and webhook servers")
6060
opts := zap.Options{
6161
Development: true,

internal/controller/postgres_controller.go

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,17 @@ func (r *PostgresReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
184184
}
185185
// create schemas
186186
var (
187-
database = instance.Spec.Database
188-
owner = instance.Status.Roles.Owner
189-
reader = instance.Status.Roles.Reader
190-
writer = instance.Status.Roles.Writer
191-
readerPrivs = "SELECT"
192-
writerPrivs = "SELECT,INSERT,DELETE,UPDATE"
187+
database = instance.Spec.Database
188+
owner = instance.Status.Roles.Owner
189+
reader = instance.Status.Roles.Reader
190+
writer = instance.Status.Roles.Writer
191+
readerPrivs = "SELECT"
192+
writerPrivs = "SELECT,INSERT,DELETE,UPDATE"
193+
writerSequencePrivs = "USAGE,SELECT"
194+
writerFunctionPrivs = "EXECUTE"
195+
ownerPrivs = "ALL"
196+
ownerFunctionPrivs = "ALL"
197+
ownerSequencePrivs = "ALL"
193198
)
194199
for _, schema := range instance.Spec.Schemas {
195200
// Schema was previously created
@@ -218,27 +223,31 @@ func (r *PostgresReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
218223
continue
219224
}
220225
schemaPrivilegesWriter := postgres.PostgresSchemaPrivileges{
221-
DB: database,
222-
Role: writer,
223-
Schema: schema,
224-
Privs: writerPrivs,
225-
CreateSchema: true,
226+
DB: database,
227+
Role: writer,
228+
Schema: schema,
229+
Privs: writerPrivs,
230+
SequencePrivs: writerSequencePrivs,
231+
FunctionPrivs: writerFunctionPrivs,
232+
CreateSchema: true,
226233
}
227234
err = r.pg.SetSchemaPrivileges(schemaPrivilegesWriter, reqLogger)
228235
if err != nil {
229-
reqLogger.Error(err, fmt.Sprintf("Could not give %s permissions \"%s\"", writer, writerPrivs))
236+
reqLogger.Error(err, fmt.Sprintf("Could not give %s permissions \"%s\", sequence privileges \"%s\", and function privileges \"%s\"", writer, writerPrivs, writerSequencePrivs, writerFunctionPrivs))
230237
continue
231238
}
232239
schemaPrivilegesOwner := postgres.PostgresSchemaPrivileges{
233-
DB: database,
234-
Role: owner,
235-
Schema: schema,
236-
Privs: writerPrivs,
237-
CreateSchema: true,
240+
DB: database,
241+
Role: owner,
242+
Schema: schema,
243+
Privs: ownerPrivs,
244+
SequencePrivs: ownerSequencePrivs,
245+
FunctionPrivs: ownerFunctionPrivs,
246+
CreateSchema: true,
238247
}
239248
err = r.pg.SetSchemaPrivileges(schemaPrivilegesOwner, reqLogger)
240249
if err != nil {
241-
reqLogger.Error(err, fmt.Sprintf("Could not give %s permissions \"%s\"", writer, writerPrivs))
250+
reqLogger.Error(err, fmt.Sprintf("Could not give %s permissions \"%s\", sequence privileges \"%s\", and function privileges \"%s\"", owner, ownerPrivs, ownerSequencePrivs, ownerFunctionPrivs))
242251
continue
243252
}
244253

pkg/postgres/database.go

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,23 @@ import (
88
)
99

1010
const (
11-
CREATE_DB = `CREATE DATABASE "%s"`
12-
CREATE_SCHEMA = `CREATE SCHEMA IF NOT EXISTS "%s" AUTHORIZATION "%s"`
13-
CREATE_EXTENSION = `CREATE EXTENSION IF NOT EXISTS "%s"`
14-
ALTER_DB_OWNER = `ALTER DATABASE "%s" OWNER TO "%s"`
15-
DROP_DATABASE = `DROP DATABASE "%s"`
16-
GRANT_USAGE_SCHEMA = `GRANT USAGE ON SCHEMA "%s" TO "%s"`
17-
GRANT_CREATE_TABLE = `GRANT CREATE ON SCHEMA "%s" TO "%s"`
18-
GRANT_ALL_TABLES = `GRANT %s ON ALL TABLES IN SCHEMA "%s" TO "%s"`
19-
DEFAULT_PRIVS_SCHEMA = `ALTER DEFAULT PRIVILEGES IN SCHEMA "%s" GRANT %s ON TABLES TO "%s"`
20-
REVOKE_CONNECT = `REVOKE CONNECT ON DATABASE "%s" FROM public`
21-
TERMINATE_BACKEND = `SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = '%s' AND pid <> pg_backend_pid()`
22-
GET_DB_OWNER = `SELECT pg_catalog.pg_get_userbyid(d.datdba) FROM pg_catalog.pg_database d WHERE d.datname = '%s'`
23-
GRANT_CREATE_SCHEMA = `GRANT CREATE ON DATABASE "%s" TO "%s"`
11+
CREATE_DB = `CREATE DATABASE "%s"`
12+
CREATE_SCHEMA = `CREATE SCHEMA IF NOT EXISTS "%s" AUTHORIZATION "%s"`
13+
CREATE_EXTENSION = `CREATE EXTENSION IF NOT EXISTS "%s"`
14+
ALTER_DB_OWNER = `ALTER DATABASE "%s" OWNER TO "%s"`
15+
DROP_DATABASE = `DROP DATABASE "%s"`
16+
GRANT_USAGE_SCHEMA = `GRANT USAGE ON SCHEMA "%s" TO "%s"`
17+
GRANT_CREATE_TABLE = `GRANT CREATE ON SCHEMA "%s" TO "%s"`
18+
GRANT_ALL_TABLES = `GRANT %s ON ALL TABLES IN SCHEMA "%s" TO "%s"`
19+
DEFAULT_PRIVS_SCHEMA = `ALTER DEFAULT PRIVILEGES IN SCHEMA "%s" GRANT %s ON TABLES TO "%s"`
20+
GRANT_ALL_FUNCTIONS = `GRANT %s ON ALL FUNCTIONS IN SCHEMA "%s" TO "%s"`
21+
DEFAULT_PRIVS_FUNCTIONS = `ALTER DEFAULT PRIVILEGES IN SCHEMA "%s" GRANT %s ON FUNCTIONS TO "%s"`
22+
GRANT_ALL_SEQUENCES = `GRANT %s ON ALL SEQUENCES IN SCHEMA "%s" TO "%s"`
23+
DEFAULT_PRIVS_SEQUENCES = `ALTER DEFAULT PRIVILEGES IN SCHEMA "%s" GRANT %s ON SEQUENCES TO "%s"`
24+
REVOKE_CONNECT = `REVOKE CONNECT ON DATABASE "%s" FROM public`
25+
TERMINATE_BACKEND = `SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = '%s' AND pid <> pg_backend_pid()`
26+
GET_DB_OWNER = `SELECT pg_catalog.pg_get_userbyid(d.datdba) FROM pg_catalog.pg_database d WHERE d.datname = '%s'`
27+
GRANT_CREATE_SCHEMA = `GRANT CREATE ON DATABASE "%s" TO "%s"`
2428
)
2529

2630
func (c *pg) CreateDB(dbname, role string) error {
@@ -120,6 +124,34 @@ func (c *pg) SetSchemaPrivileges(schemaPrivileges PostgresSchemaPrivileges, logg
120124
return err
121125
}
122126

127+
if schemaPrivileges.SequencePrivs != "" {
128+
// Grant role privs on existing sequences in schema
129+
_, err = tmpDb.Exec(fmt.Sprintf(GRANT_ALL_SEQUENCES, schemaPrivileges.SequencePrivs, schemaPrivileges.Schema, schemaPrivileges.Role))
130+
if err != nil {
131+
return err
132+
}
133+
134+
// Grant role privs on future sequences in schema
135+
_, err = tmpDb.Exec(fmt.Sprintf(DEFAULT_PRIVS_SEQUENCES, schemaPrivileges.Schema, schemaPrivileges.SequencePrivs, schemaPrivileges.Role))
136+
if err != nil {
137+
return err
138+
}
139+
}
140+
141+
if schemaPrivileges.FunctionPrivs != "" {
142+
// Grant role privs on existing functions in schema
143+
_, err = tmpDb.Exec(fmt.Sprintf(GRANT_ALL_FUNCTIONS, schemaPrivileges.FunctionPrivs, schemaPrivileges.Schema, schemaPrivileges.Role))
144+
if err != nil {
145+
return err
146+
}
147+
148+
// Grant role privs on future functions in schema
149+
_, err = tmpDb.Exec(fmt.Sprintf(DEFAULT_PRIVS_FUNCTIONS, schemaPrivileges.Schema, schemaPrivileges.FunctionPrivs, schemaPrivileges.Role))
150+
if err != nil {
151+
return err
152+
}
153+
}
154+
123155
// Grant role usage on schema if createSchema
124156
if schemaPrivileges.CreateSchema {
125157
_, err = tmpDb.Exec(fmt.Sprintf(GRANT_CREATE_TABLE, schemaPrivileges.Schema, schemaPrivileges.Role))

pkg/postgres/postgres.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@ type pg struct {
3737
}
3838

3939
type PostgresSchemaPrivileges struct {
40-
DB string
41-
Role string
42-
Schema string
43-
Privs string
44-
CreateSchema bool
40+
DB string
41+
Role string
42+
Schema string
43+
Privs string
44+
SequencePrivs string
45+
FunctionPrivs string
46+
CreateSchema bool
4547
}
4648

4749
func NewPG(cfg *config.Cfg, logger logr.Logger) (PG, error) {

0 commit comments

Comments
 (0)