Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
validate policy type/payload and return default policy if there's no stored policy.
spinningbot3e63ecbd5443
diff --git a/api/policy.go b/api/policy.goindex 5d399f4a87..c56ee3f4b7 100644--- a/api/policy.go+++ b/api/policy.go@@ -2,6 +2,32 @@ package apiimport ("context"+ "fmt"+)++// PolicyType is the type or name of a policy.+type PolicyType string++const (+ // PolicyTypeApprovalPolicy is the approval policy type.+ PolicyTypeApprovalPolicy PolicyType = "approval_policy"+)++var (+ // PolicyTypes is a set of all policy types.+ PolicyTypes = map[PolicyType]bool{+ PolicyTypeApprovalPolicy: true,+ }+)++// ApprovalPolicyValue is value for approval policy.+type ApprovalPolicyValue string++const (+ // ApprovalPolicyValueManualNever is MANUAL_APPROVAL_NEVER approval policy value.+ ApprovalPolicyValueManualNever ApprovalPolicyValue = "MANUAL_APPROVAL_NEVER"+ // ApprovalPolicyValueManualAlways is MANUAL_APPROVAL_ALWAYS approval policy value.+ ApprovalPolicyValueManualAlways ApprovalPolicyValue = "MANUAL_APPROVAL_ALWAYS")type Policy struct {@@ -23,8 +49,8 @@ type Policy struct {Environment *Environment// Domain specific fields- Type string `jsonapi:"attr,type"`- Payload string `jsonapi:"attr,payload"`+ Type PolicyType `jsonapi:"attr,type"`+ Payload string `jsonapi:"attr,payload"`}// PolicyFind is the message to get a policy.@@ -35,7 +61,7 @@ type PolicyFind struct {EnvironmentId *int// Domain specific fields- Type *string `jsonapi:"attr,type"`+ Type *PolicyType `jsonapi:"attr,type"`}// PolicyUpsert is the message to upsert a policy.@@ -50,8 +76,8 @@ type PolicyUpsert struct {EnvironmentId int `jsonapi:"attr,environmentId"`// Domain specific fields- Type string `jsonapi:"attr,type"`- Payload string `jsonapi:"attr,payload"`+ Type PolicyType `jsonapi:"attr,type"`+ Payload string `jsonapi:"attr,payload"`}// PolicyService is the backend for policies.@@ -59,3 +85,32 @@ type PolicyService interface {FindPolicy(ctx context.Context, find *PolicyFind) (*Policy, error)UpsertPolicy(ctx context.Context, upsert *PolicyUpsert) (*Policy, error)}++// ValidatePolicy will validate the policy type and payload values.+func ValidatePolicy(pType PolicyType, payload string) error {+ if !PolicyTypes[pType] {+ return fmt.Errorf("invalid policy type: %s", pType)+ }+ if payload == "" {+ return nil+ }++ switch pType {+ case PolicyTypeApprovalPolicy:+ pv := ApprovalPolicyValue(payload)+ if pv != ApprovalPolicyValueManualNever && pv != ApprovalPolicyValueManualAlways {+ return fmt.Errorf("invalid approval policy value: %q", payload)+ }+ }+ return nil+}++// 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 {+ switch pType {+ case PolicyTypeApprovalPolicy:+ return ""+ }+ return ""+}diff --git a/server/policy.go b/server/policy.goindex 3661faac6b..41f8a564b0 100644--- a/server/policy.go+++ b/server/policy.go@@ -16,7 +16,7 @@ func (s *Server) registerPolicyRoutes(g *echo.Group) {if err := jsonapi.UnmarshalPayload(c.Request().Body, policyUpsert); err != nil {return echo.NewHTTPError(http.StatusBadRequest, "Malformatted set policy request").SetInternal(err)}- pType := c.Param("type")+ pType := api.PolicyType(c.Param("type"))policyUpsert.Type = pTypepolicyUpsert.UpdaterId = c.Get(GetPrincipalIdContextKey()).(int)@@ -37,7 +37,7 @@ func (s *Server) registerPolicyRoutes(g *echo.Group) {if err := jsonapi.UnmarshalPayload(c.Request().Body, policyFind); err != nil {return echo.NewHTTPError(http.StatusBadRequest, "Malformatted get policy request").SetInternal(err)}- pType := c.Param("type")+ pType := api.PolicyType(c.Param("type"))policyFind.Type = &pTypepolicy, err := s.PolicyService.FindPolicy(context.Background(), policyFind)diff --git a/store/policy.go b/store/policy.goindex 60e977165d..401ee5749d 100644--- a/store/policy.go+++ b/store/policy.go@@ -31,6 +31,12 @@ func NewPolicyService(logger *zap.Logger, db *DB, cache api.CacheService) *Polic// Returns ENOTFOUND if no matching record.// Returns ECONFLICT if finding more than 1 matching records.func (s *PolicyService) FindPolicy(ctx context.Context, find *api.PolicyFind) (*api.Policy, error) {+ // Validate policy type existence.+ if find.Type != nil && *find.Type != "" {+ if err := api.ValidatePolicy(*find.Type, ""); err != nil {+ return nil, &common.Error{Code: common.EINVALID, Message: err.Error()}+ }+ }tx, err := s.db.BeginTx(ctx, nil)if err != nil {return nil, FormatError(err)@@ -38,11 +44,15 @@ func (s *PolicyService) FindPolicy(ctx context.Context, find *api.PolicyFind) (*defer tx.Rollback()list, err := s.findPolicy(ctx, tx, find)- // TODO(spinningbot): handle the default.if err != nil {return nil, err} else if len(list) == 0 {- return nil, &common.Error{Code: common.ENOTFOUND, Message: fmt.Sprintf("policy not found: %+v", find)}+ // Return the default policy when there is no stored policy.+ return &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)}}@@ -110,6 +120,12 @@ func (s *PolicyService) findPolicy(ctx context.Context, tx *Tx, find *api.Policy// UpsertPolicy sets a policy for an environment.func (s *PolicyService) UpsertPolicy(ctx context.Context, upsert *api.PolicyUpsert) (*api.Policy, error) {+ // Validate policy.+ if upsert.Type != "" {+ if err := api.ValidatePolicy(upsert.Type, upsert.Payload); err != nil {+ return nil, &common.Error{Code: common.EINVALID, Message: err.Error()}+ }+ }tx, err := s.db.BeginTx(ctx, nil)if err != nil {return nil, FormatError(err)