Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
Backup plan policy: use structured policy configuration with schedule instead of enum.
spinningbotbe566ce591b2
diff --git a/api/policy.go b/api/policy.goindex 45e6825662..6f62abff00 100644--- a/api/policy.go+++ b/api/policy.go@@ -2,6 +2,7 @@ package apiimport ("context"+ "encoding/json""fmt")@@ -11,8 +12,8 @@ type PolicyType string// ApprovalPolicyValue is value for approval policy.type ApprovalPolicyValue string-// BackupPlanPolicyValue is value for backup plan policy.-type BackupPlanPolicyValue string+// BackupPlanPolicySchedule is value for backup plan policy.+type BackupPlanPolicySchedule stringconst (// PolicyTypeApprovalPolicy is the approval policy type.@@ -25,12 +26,12 @@ const (// ApprovalPolicyValueManualAlways is MANUAL_APPROVAL_ALWAYS approval policy value.ApprovalPolicyValueManualAlways ApprovalPolicyValue = "MANUAL_APPROVAL_ALWAYS"- // BackupPlanPolicyValueNever is NEVER backup plan policy value.- BackupPlanPolicyValueNever BackupPlanPolicyValue = "NEVER"- // BackupPlanPolicyValueDaily is DAILY backup plan policy value.- BackupPlanPolicyValueDaily BackupPlanPolicyValue = "DAILY"- // BackupPlanPolicyValueWeekly is WEEKLY backup plan policy value.- BackupPlanPolicyValueWeekly BackupPlanPolicyValue = "WEEKLY"+ // BackupPlanPolicyScheduleNever is NEVER backup plan policy value.+ BackupPlanPolicyScheduleNever BackupPlanPolicySchedule = "NEVER"+ // BackupPlanPolicyScheduleDaily is DAILY backup plan policy value.+ BackupPlanPolicyScheduleDaily BackupPlanPolicySchedule = "DAILY"+ // BackupPlanPolicyScheduleWeekly is WEEKLY backup plan policy value.+ BackupPlanPolicyScheduleWeekly BackupPlanPolicySchedule = "WEEKLY")var (@@ -87,8 +88,8 @@ type PolicyUpsert struct {EnvironmentId int// Domain specific fields- Type PolicyType `jsonapi:"attr,type"`- Payload string `jsonapi:"attr,payload"`+ Type PolicyType+ Payload string `jsonapi:"attr,payload"`}// PolicyService is the backend for policies.@@ -97,6 +98,28 @@ type PolicyService interface {UpsertPolicy(ctx context.Context, upsert *PolicyUpsert) (*Policy, error)}+// BackupPlanPolicy is the policy configuration for backup plan.+type BackupPlanPolicy struct {+ Schedule BackupPlanPolicySchedule `jsonapi:"attr,schedule"`+}++func (bp BackupPlanPolicy) String() (string, error) {+ s, err := json.Marshal(bp)+ if err != nil {+ return "", err+ }+ return string(s), nil+}++// UnmarshalBackupPlanPolicy will unmarshal payload to backup plan policy.+func UnmarshalBackupPlanPolicy(payload string) (*BackupPlanPolicy, error) {+ var bp BackupPlanPolicy+ if err := json.Unmarshal([]byte(payload), &bp); err != nil {+ return nil, fmt.Errorf("failed to unmarshal backup plan policy %q: %q", payload, err)+ }+ return &bp, nil+}+// ValidatePolicy will validate the policy type and payload values.func ValidatePolicy(pType PolicyType, payload string) error {if !PolicyTypes[pType] {@@ -113,9 +136,12 @@ func ValidatePolicy(pType PolicyType, payload string) error {return fmt.Errorf("invalid approval policy value: %q", payload)}case PolicyTypeBackupPlan:- pv := BackupPlanPolicyValue(payload)- if pv != BackupPlanPolicyValueNever && pv != BackupPlanPolicyValueDaily && pv != BackupPlanPolicyValueWeekly {- return fmt.Errorf("invalid backup plan policy value: %q", payload)+ bp, err := UnmarshalBackupPlanPolicy(payload)+ if err != nil {+ return err+ }+ if bp.Schedule != BackupPlanPolicyScheduleNever && bp.Schedule != BackupPlanPolicyScheduleDaily && bp.Schedule != BackupPlanPolicyScheduleWeekly {+ return fmt.Errorf("invalid backup plan policy schedule: %q", bp.Schedule)}}return nil@@ -123,12 +149,14 @@ func ValidatePolicy(pType PolicyType, payload string) error {// GetDefaultPolicy will return the default value for the given policy type.// The default policy can be empty when we don't have anything to enforce at runtime.-func GetDefaultPolicy(pType PolicyType) string {+func GetDefaultPolicy(pType PolicyType) (string, error) {switch pType {case PolicyTypeApprovalPolicy:- return ""+ return "", nilcase PolicyTypeBackupPlan:- return string(BackupPlanPolicyValueNever)+ return BackupPlanPolicy{+ Schedule: BackupPlanPolicyScheduleNever,+ }.String()}- return ""+ return "", nil}diff --git a/server/policy.go b/server/policy.goindex 88bf81f34b..570a333eee 100644--- a/server/policy.go+++ b/server/policy.go@@ -23,6 +23,9 @@ func (s *Server) registerPolicyRoutes(g *echo.Group) {return echo.NewHTTPError(http.StatusBadRequest, "Malformatted set policy request").SetInternal(err)}pType := api.PolicyType(c.QueryParam("type"))+ if err := api.ValidatePolicy(pType, ""); err != nil {+ return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid policy type: %q", pType)).SetInternal(err)+ }policyUpsert.EnvironmentId = environmentIDpolicyUpsert.Type = pTypepolicyUpsert.UpdaterId = c.Get(GetPrincipalIdContextKey()).(int)@@ -46,6 +49,9 @@ func (s *Server) registerPolicyRoutes(g *echo.Group) {}policyFind := &api.PolicyFind{}pType := api.PolicyType(c.QueryParam("type"))+ if err := api.ValidatePolicy(pType, ""); err != nil {+ return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid policy type: %q", pType)).SetInternal(err)+ }policyFind.Type = &pTypepolicyFind.EnvironmentId = &environmentIDdiff --git a/store/database.go b/store/database.goindex d614a65448..c6549de129 100644--- a/store/database.go+++ b/store/database.go@@ -66,7 +66,10 @@ func (s *DatabaseService) CreateDatabaseTx(ctx context.Context, tx *sql.Tx, creaif err != nil {return nil, err}- backupPlanPolicy := api.BackupPlanPolicyValue(policy.Payload)+ backupPlanPolicy, err := api.UnmarshalBackupPlanPolicy(policy.Payload)+ if err != nil {+ return nil, err+ }database, err := s.createDatabase(ctx, tx, create)if err != nil {@@ -74,17 +77,17 @@ func (s *DatabaseService) CreateDatabaseTx(ctx context.Context, tx *sql.Tx, crea}// Enable automatic backup setting based on backup plan policy.- if backupPlanPolicy != api.BackupPlanPolicyValueNever {+ if backupPlanPolicy.Schedule != api.BackupPlanPolicyScheduleNever {backupSettingUpsert := &api.BackupSettingUpsert{UpdaterId: api.SYSTEM_BOT_ID,DatabaseId: database.ID,Enabled: true,Hour: rand.Intn(24),}- switch backupPlanPolicy {- case api.BackupPlanPolicyValueDaily:+ switch backupPlanPolicy.Schedule {+ case api.BackupPlanPolicyScheduleDaily:backupSettingUpsert.DayOfWeek = -1- case api.BackupPlanPolicyValueWeekly:+ case api.BackupPlanPolicyScheduleWeekly:backupSettingUpsert.DayOfWeek = rand.Intn(7)}if _, err := s.backupService.UpsertBackupSettingTx(ctx, tx, backupSettingUpsert); err != nil {diff --git a/store/policy.go b/store/policy.goindex 401ee5749d..dd534be841 100644--- a/store/policy.go+++ b/store/policy.go@@ -44,20 +44,29 @@ func (s *PolicyService) FindPolicy(ctx context.Context, find *api.PolicyFind) (*defer tx.Rollback()list, err := s.findPolicy(ctx, tx, find)+ var ret *api.Policyif err != nil {return nil, err} else if len(list) == 0 {- // Return the default policy when there is no stored policy.- return &api.Policy{+ ret = &api.Policy{EnvironmentId: *find.EnvironmentId,Type: *find.Type,- Payload: api.GetDefaultPolicy(*find.Type),- }, nil+ }} else if len(list) > 1 {return nil, &common.Error{Code: common.ECONFLICT, Message: fmt.Sprintf("found %d policy with filter %+v, expect 1. ", len(list), find)}+ } else {+ ret = list[0]}- return list[0], nil+ if ret.Payload == "" {+ // Return the default policy when there is no stored policy.+ payload, err := api.GetDefaultPolicy(*find.Type)+ if err != nil {+ return nil, &common.Error{Code: common.EINTERNAL, Message: err.Error()}+ }+ ret.Payload = payload+ }+ return ret, nil}func (s *PolicyService) findPolicy(ctx context.Context, tx *Tx, find *api.PolicyFind) (_ []*api.Policy, err error) {