Skip to content

Commit

Permalink
Allow writer user to create tables in schema
Browse files Browse the repository at this point in the history
In postgres 15 they change the behaviour of the public schema. Now only
the owner can create tables in this schema. And the user is in charge to
configure the permissions.

Grant the writer user to also create tables in a schema.

add the public schema explicitly to the list of schemas to create, to
force the schema privileges to be applied.
  • Loading branch information
Matthias Fuhrmeister committed Oct 26, 2023
1 parent 2f9870f commit ecfc2a7
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/controller/postgres/postgres_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,11 @@ func (r *ReconcilePostgres) Reconcile(request reconcile.Request) (_ reconcile.Re
reqLogger.Error(err, fmt.Sprintf("Could not give %s permissions \"%s\"", writer, writerPrivs))
continue
}
err = r.pg.SetSchemaPrivilegesCreate(database, owner, writer, schema, writerPrivs, reqLogger)
if err != nil {
reqLogger.Error(err, fmt.Sprintf("Could not give %s permissions \"%s\"", writer, writerPrivs))
continue
}

instance.Status.Schemas = append(instance.Status.Schemas, schema)
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/controller/postgres/postgres_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -683,9 +683,11 @@ var _ = Describe("ReconcilePostgres", func() {
// customers schema
pg.EXPECT().CreateSchema(name, name+"-group", "customers", gomock.Any()).Return(nil).Times(1)
pg.EXPECT().SetSchemaPrivileges(name, name+"-group", gomock.Any(), "customers", gomock.Any(), gomock.Any()).Return(nil).Times(2)
pg.EXPECT().SetSchemaPrivilegesCreate(name, name+"-group", name+"-writer", "customers", gomock.Any(), gomock.Any()).Return(nil).Times(1)
// stores schema
pg.EXPECT().CreateSchema(name, name+"-group", "stores", gomock.Any()).Return(nil).Times(1)
pg.EXPECT().SetSchemaPrivileges(name, name+"-group", gomock.Any(), "stores", gomock.Any(), gomock.Any()).Return(nil).Times(2)
pg.EXPECT().SetSchemaPrivilegesCreate(name, name+"-group", name+"-writer", "stores", gomock.Any(), gomock.Any()).Return(nil).Times(1)
})

It("should update status", func() {
Expand All @@ -708,9 +710,11 @@ var _ = Describe("ReconcilePostgres", func() {
// customers schema errors
pg.EXPECT().CreateSchema(name, name+"-group", "customers", gomock.Any()).Return(fmt.Errorf("Could not create schema")).Times(1)
pg.EXPECT().SetSchemaPrivileges(name, name+"-group", gomock.Any(), "customers", gomock.Any(), gomock.Any()).Return(nil).Times(0)
pg.EXPECT().SetSchemaPrivilegesCreate(name, name+"-group", name+"-writer", "customers", gomock.Any(), gomock.Any()).Return(nil).Times(0)
// stores schema
pg.EXPECT().CreateSchema(name, name+"-group", "stores", gomock.Any()).Return(nil).Times(1)
pg.EXPECT().SetSchemaPrivileges(name, name+"-group", gomock.Any(), "stores", gomock.Any(), gomock.Any()).Return(nil).Times(2)
pg.EXPECT().SetSchemaPrivilegesCreate(name, name+"-group", name+"-writer", "stores", gomock.Any(), gomock.Any()).Return(nil).Times(1)
})

It("should update status", func() {
Expand Down Expand Up @@ -752,6 +756,7 @@ var _ = Describe("ReconcilePostgres", func() {
// customers schema
pg.EXPECT().CreateSchema(name, name+"-group", "customers", gomock.Any()).Return(nil).Times(1)
pg.EXPECT().SetSchemaPrivileges(name, name+"-group", gomock.Any(), "customers", gomock.Any(), gomock.Any()).Return(nil).Times(2)
pg.EXPECT().SetSchemaPrivilegesCreate(name, name+"-group", name+"-writer", "customers", gomock.Any(), gomock.Any()).Return(nil).Times(1)
// stores schema already exists
pg.EXPECT().CreateSchema(name, name+"-group", "stores", gomock.Any()).Times(0)
// Call reconcile
Expand Down
16 changes: 16 additions & 0 deletions pkg/postgres/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const (
ALTER_DB_OWNER = `ALTER DATABASE "%s" OWNER TO "%s"`
DROP_DATABASE = `DROP DATABASE "%s"`
GRANT_USAGE_SCHEMA = `GRANT USAGE ON SCHEMA "%s" TO "%s"`
GRANT_CREATE_TABLE = `GRANT CREATE ON SCHEMA "%s" TO "%s"`
GRANT_ALL_TABLES = `GRANT %s ON ALL TABLES IN SCHEMA "%s" TO "%s"`
DEFAULT_PRIVS_SCHEMA = `ALTER DEFAULT PRIVILEGES FOR ROLE "%s" IN SCHEMA "%s" GRANT %s ON TABLES TO "%s"`
REVOKE_CONNECT = `REVOKE CONNECT ON DATABASE "%s" FROM public`
Expand Down Expand Up @@ -109,3 +110,18 @@ func (c *pg) SetSchemaPrivileges(db, creator, role, schema, privs string, logger
}
return nil
}

func (c *pg) SetSchemaPrivilegesCreate(db, creator, role, schema, privs string, logger logr.Logger) error {
tmpDb, err := GetConnection(c.user, c.pass, c.host, db, c.args, logger)
if err != nil {
return err
}
defer tmpDb.Close()

// Grant role usage on schema
_, err = tmpDb.Exec(fmt.Sprintf(GRANT_CREATE_TABLE, schema, role))
if err != nil {
return err
}
return nil
}
14 changes: 14 additions & 0 deletions pkg/postgres/mock/postgres.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pkg/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type PG interface {
UpdatePassword(role, password string) error
GrantRole(role, grantee string) error
SetSchemaPrivileges(db, creator, role, schema, privs string, logger logr.Logger) error
SetSchemaPrivilegesCreate(db, creator, role, schema, privs string, logger logr.Logger) error
RevokeRole(role, revoked string) error
AlterDefaultLoginRole(role, setRole string) error
DropDatabase(db string, logger logr.Logger) error
Expand Down

0 comments on commit ecfc2a7

Please sign in to comment.