Skip to content

Commit

Permalink
Make superuser for dbo user configurable (#552)
Browse files Browse the repository at this point in the history
  • Loading branch information
eberlep authored Apr 10, 2024
1 parent 1e4f364 commit 53c8e10
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
5 changes: 4 additions & 1 deletion api/v1/postgres_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ func (p *Postgres) ToPeripheralResourceLookupKey() types.NamespacedName {
}
}

func (p *Postgres) ToUnstructuredZalandoPostgresql(z *zalando.Postgresql, c *corev1.ConfigMap, sc string, pgParamBlockList map[string]bool, rbs *BackupConfig, srcDB *Postgres, patroniTTL, patroniLoopWait, patroniRetryTimeout uint32) (*unstructured.Unstructured, error) {
func (p *Postgres) ToUnstructuredZalandoPostgresql(z *zalando.Postgresql, c *corev1.ConfigMap, sc string, pgParamBlockList map[string]bool, rbs *BackupConfig, srcDB *Postgres, patroniTTL, patroniLoopWait, patroniRetryTimeout uint32, dboIsSuperuser bool) (*unstructured.Unstructured, error) {
if z == nil {
z = &zalando.Postgresql{}
}
Expand Down Expand Up @@ -657,6 +657,9 @@ func (p *Postgres) ToUnstructuredZalandoPostgresql(z *zalando.Postgresql, c *cor
// Create database owner
z.Spec.Users = make(map[string]zalando.UserFlags)
z.Spec.Users[ownerName] = zalando.UserFlags{"createdb", "createrole"}
if dboIsSuperuser {
z.Spec.Users[ownerName] = zalando.UserFlags{"createdb", "createrole", "superuser"}
}
// Add auditor user
z.Spec.Users["auditor"] = zalando.UserFlags{"nologin"}

Expand Down
2 changes: 1 addition & 1 deletion api/v1/postgres_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ func TestPostgresRestoreTimestamp_ToUnstructuredZalandoPostgresql(t *testing.T)
p := &Postgres{
Spec: tt.spec,
}
got, _ := p.ToUnstructuredZalandoPostgresql(nil, tt.c, tt.sc, tt.pgParamBlockList, tt.rbs, tt.srcDB, 130, 10, 60)
got, _ := p.ToUnstructuredZalandoPostgresql(nil, tt.c, tt.sc, tt.pgParamBlockList, tt.rbs, tt.srcDB, 130, 10, 60, false)

jsonZ, err := runtime.DefaultUnstructuredConverter.ToUnstructured(got)
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions controllers/postgres_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ type PostgresReconciler struct {
EnableWalGEncryption bool
PostgresletFullname string
EnableBootstrapStandbyFromS3 bool
EnableSuperUserForDBO bool
}

// Reconcile is the entry point for postgres reconciliation.
Expand Down Expand Up @@ -357,7 +358,7 @@ func (r *PostgresReconciler) createOrUpdateZalandoPostgresql(ctx context.Context
return fmt.Errorf("failed to fetch zalando postgresql: %w", err)
}

u, err := instance.ToUnstructuredZalandoPostgresql(nil, sidecarsCM, r.StorageClass, r.PgParamBlockList, restoreBackupConfig, restoreSouceInstance, patroniTTL, patroniLoopWait, patroniRetryTimout)
u, err := instance.ToUnstructuredZalandoPostgresql(nil, sidecarsCM, r.StorageClass, r.PgParamBlockList, restoreBackupConfig, restoreSouceInstance, patroniTTL, patroniLoopWait, patroniRetryTimout, r.EnableSuperUserForDBO)
if err != nil {
return fmt.Errorf("failed to convert to unstructured zalando postgresql: %w", err)
}
Expand All @@ -373,7 +374,7 @@ func (r *PostgresReconciler) createOrUpdateZalandoPostgresql(ctx context.Context
// Update zalando postgresql
mergeFrom := client.MergeFrom(rawZ.DeepCopy())

u, err := instance.ToUnstructuredZalandoPostgresql(rawZ, sidecarsCM, r.StorageClass, r.PgParamBlockList, restoreBackupConfig, restoreSouceInstance, patroniTTL, patroniLoopWait, patroniRetryTimout)
u, err := instance.ToUnstructuredZalandoPostgresql(rawZ, sidecarsCM, r.StorageClass, r.PgParamBlockList, restoreBackupConfig, restoreSouceInstance, patroniTTL, patroniLoopWait, patroniRetryTimout, r.EnableSuperUserForDBO)
if err != nil {
return fmt.Errorf("failed to convert to unstructured zalando postgresql: %w", err)
}
Expand Down
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ const (
enableWalGEncryptionFlg = "enable-walg-encryption"
enableForceSharedIPFlg = "enable-force-shared-ip"
enableBootstrapStandbyFromS3Flg = "enable-bootsrtap-standby-from-s3"
enableSuperUserForDBOFlg = "enable-superuser-for-dbo"
)

var (
Expand Down Expand Up @@ -126,6 +127,7 @@ func main() {
enableWalGEncryption bool
enableForceSharedIP bool
enableBootstrapStandbyFromS3 bool
enableSuperUserForDBO bool

portRangeStart int
portRangeSize int
Expand Down Expand Up @@ -265,6 +267,9 @@ func main() {
viper.SetDefault(enableBootstrapStandbyFromS3Flg, true)
enableBootstrapStandbyFromS3 = viper.GetBool(enableBootstrapStandbyFromS3Flg)

viper.SetDefault(enableSuperUserForDBOFlg, false)
enableSuperUserForDBO = viper.GetBool(enableSuperUserForDBOFlg)

ctrl.SetLogger(zap.New(zap.UseDevMode(true)))

ctrl.Log.Info("flag",
Expand Down Expand Up @@ -305,6 +310,7 @@ func main() {
enableWalGEncryptionFlg, enableWalGEncryption,
enableForceSharedIPFlg, enableForceSharedIP,
enableBootstrapStandbyFromS3Flg, enableBootstrapStandbyFromS3,
enableSuperUserForDBOFlg, enableSuperUserForDBO,
)

svcClusterConf := ctrl.GetConfigOrDie()
Expand Down Expand Up @@ -413,6 +419,7 @@ func main() {
EnableWalGEncryption: enableWalGEncryption,
PostgresletFullname: postgresletFullname,
EnableBootstrapStandbyFromS3: enableBootstrapStandbyFromS3,
EnableSuperUserForDBO: enableSuperUserForDBO,
}).SetupWithManager(ctrlPlaneClusterMgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Postgres")
os.Exit(1)
Expand Down

0 comments on commit 53c8e10

Please sign in to comment.