Skip to content

Commit

Permalink
integrate schema create permission into exisiting method
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Fuhrmeister committed Feb 13, 2024
1 parent d7d3a70 commit b4d514f
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 50 deletions.
9 changes: 2 additions & 7 deletions pkg/controller/postgres/postgres_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,17 +220,12 @@ func (r *ReconcilePostgres) Reconcile(request reconcile.Request) (_ reconcile.Re
}

// Set privileges on schema
err = r.pg.SetSchemaPrivileges(database, owner, reader, schema, readerPrivs, reqLogger)
err = r.pg.SetSchemaPrivileges(database, owner, reader, schema, readerPrivs, false, reqLogger)
if err != nil {
reqLogger.Error(err, fmt.Sprintf("Could not give %s permissions \"%s\"", reader, readerPrivs))
continue
}
err = r.pg.SetSchemaPrivileges(database, owner, writer, schema, writerPrivs, reqLogger)
if err != nil {
reqLogger.Error(err, fmt.Sprintf("Could not give %s permissions \"%s\"", writer, writerPrivs))
continue
}
err = r.pg.SetSchemaPrivilegesCreate(database, owner, writer, schema, writerPrivs, reqLogger)
err = r.pg.SetSchemaPrivileges(database, owner, writer, schema, writerPrivs, true, reqLogger)
if err != nil {
reqLogger.Error(err, fmt.Sprintf("Could not give %s permissions \"%s\"", writer, writerPrivs))
continue
Expand Down
16 changes: 6 additions & 10 deletions pkg/controller/postgres/postgres_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -682,12 +682,10 @@ var _ = Describe("ReconcilePostgres", func() {
// Expected method calls
// 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)
pg.EXPECT().SetSchemaPrivileges(name, name+"-group", gomock.Any(), "customers", gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(2)
// 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)
pg.EXPECT().SetSchemaPrivileges(name, name+"-group", gomock.Any(), "stores", gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(2)
})

It("should update status", func() {
Expand All @@ -709,12 +707,11 @@ var _ = Describe("ReconcilePostgres", func() {
// Expected method calls
// 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)
pg.EXPECT().SetSchemaPrivileges(name, name+"-group", gomock.Any(), "customers", gomock.Any(), 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)
pg.EXPECT().SetSchemaPrivileges(name, name+"-group", gomock.Any(), "stores", gomock.Any(), false, gomock.Any()).Return(nil).Times(1)
pg.EXPECT().SetSchemaPrivileges(name, name+"-group", gomock.Any(), "stores", gomock.Any(), true, gomock.Any()).Return(nil).Times(1)
})

It("should update status", func() {
Expand Down Expand Up @@ -755,8 +752,7 @@ var _ = Describe("ReconcilePostgres", func() {
// Expected method calls
// 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)
pg.EXPECT().SetSchemaPrivileges(name, name+"-group", gomock.Any(), "customers", gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(2)
// stores schema already exists
pg.EXPECT().CreateSchema(name, name+"-group", "stores", gomock.Any()).Times(0)
// Call reconcile
Expand Down
20 changes: 7 additions & 13 deletions pkg/postgres/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (c *pg) CreateExtension(db, extension string, logger logr.Logger) error {
return nil
}

func (c *pg) SetSchemaPrivileges(db, creator, role, schema, privs string, logger logr.Logger) error {
func (c *pg) SetSchemaPrivileges(db, creator, role, schema, privs string, createSchema bool, logger logr.Logger) error {
tmpDb, err := GetConnection(c.user, c.pass, c.host, db, c.args, logger)
if err != nil {
return err
Expand All @@ -119,20 +119,14 @@ func (c *pg) SetSchemaPrivileges(db, creator, role, schema, privs string, logger
if err != nil {
return err
}
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
// Grant role usage on schema if createSchema
if createSchema {
_, err = tmpDb.Exec(fmt.Sprintf(GRANT_CREATE_TABLE, schema, role))
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
}
22 changes: 4 additions & 18 deletions pkg/postgres/mock/postgres.go

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

3 changes: 1 addition & 2 deletions pkg/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ type PG interface {
CreateUserRole(role, password string) (string, error)
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
SetSchemaPrivileges(db, creator, role, schema, privs string, createSchema bool, 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 b4d514f

Please sign in to comment.