Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
build: validate deployment schedule payload on writes (#116)
spinningbot0fe7c850ce7a
diff --git a/api/deploy.go b/api/deploy.goindex 6045938295..d31e856298 100644--- a/api/deploy.go+++ b/api/deploy.go@@ -2,6 +2,10 @@ package apiimport ("context"+ "encoding/json"+ "fmt"++ "github.com/bytebase/bytebase/common")// DeploymentConfig is the API message for deployment configurations.@@ -44,7 +48,7 @@ type DeploymentSpec struct {// LabelSelector is the API message for label selector.type LabelSelector struct {// MatchExpressions is a list of label selector requirements. The requirements are ANDed.- MatchExpressions *LabelSelectorRequirement `json:"matchExpressions"`+ MatchExpressions []*LabelSelectorRequirement `json:"matchExpressions"`}// OperatorType is the type of label selector requirement operator.@@ -102,3 +106,30 @@ type DeploymentConfigService interface {// UpsertDeploymentConfig upserts a deployment configuration to a project.UpsertDeploymentConfig(ctx context.Context, upsert *DeploymentConfigUpsert) (*DeploymentConfig, error)}++// ValidateAndGetDeploymentSchedule validates and returns the deployment schedule.+// Note: this validation only checks whether the payloads is a valid json, however, invalid field name errors are ignored.+func ValidateAndGetDeploymentSchedule(payload string) (*DeploymentSchedule, error) {+ schedule := &DeploymentSchedule{}+ if err := json.Unmarshal([]byte(payload), schedule); err != nil {+ return nil, err+ }++ for _, d := range schedule.Deployments {+ for _, e := range d.Spec.Selector.MatchExpressions {+ switch e.Operator {+ case InOperatorType:+ if len(e.Values) <= 0 {+ return nil, common.Errorf(common.Invalid, fmt.Errorf("expression key %q with %q operator should have at least one value", e.Key, e.Operator))+ }+ case ExistsOperatorType:+ if len(e.Values) > 0 {+ return nil, common.Errorf(common.Invalid, fmt.Errorf("expression key %q with %q operator shouldn't have values", e.Key, e.Operator))+ }+ default:+ return nil, common.Errorf(common.Invalid, fmt.Errorf("expression key %q has invalid operator %q", e.Key, e.Operator))+ }+ }+ }+ return schedule, nil+}diff --git a/api/deploy_test.go b/api/deploy_test.gonew file mode 100644index 0000000000..dbae93be5e--- /dev/null+++ b/api/deploy_test.go@@ -0,0 +1,94 @@+package api++import (+ "testing"++ "github.com/kr/pretty"+)++func TestGetDeploymentSchedule(t *testing.T) {+ tests := []struct {+ name string+ payload string+ wantCfg *DeploymentSchedule+ wantErr bool+ }{+ {+ "complexDeployments",+ `{"deployments":[{"spec":{"selector":{"matchExpressions":[{"key":"location","operator":"In","values":["us-central1","europe-west1"]}]}}},{"spec":{"selector":{"matchExpressions":[{"key":"location","operator":"Exists"}]}}}]}`,+ &DeploymentSchedule{+ Deployments: []*Deployment{+ {+ Spec: &DeploymentSpec{+ Selector: &LabelSelector{+ MatchExpressions: []*LabelSelectorRequirement{+ {+ Key: "location",+ Operator: "In",+ Values: []string{"us-central1", "europe-west1"},+ },+ },+ },+ },+ },+ {+ Spec: &DeploymentSpec{+ Selector: &LabelSelector{+ MatchExpressions: []*LabelSelectorRequirement{+ {+ Key: "location",+ Operator: "Exists",+ Values: nil,+ },+ },+ },+ },+ },+ },+ },+ false,+ },+ {+ "invalidPayload",+ `{"unmatchdeployments":[{"spec":{"selector":{"matchExpressions":[{"key":"location","operator":"In","values":["us-central1","europe-west1"]}]}}},{"spec":{"selector":{"matchExpressions":[{"key":"location","operator":"Exists"}]}}}]}`,+ &DeploymentSchedule{},+ false,+ },+ {+ "json",+ `{`,+ nil,+ true,+ },+ {+ "inOperatorWithNoValue",+ `{"deployments":[{"spec":{"selector":{"matchExpressions":[{"key":"location","operator":"In"}]}}}]}`,+ nil,+ true,+ },+ {+ "existsOperatorWithValues",+ `{"deployments":[{"spec":{"selector":{"matchExpressions":[{"key":"location","operator":"Exists","values":["us-central1","europe-west1"]}]}}}]}`,+ nil,+ true,+ },+ {+ "invalidOperator",+ `{"deployments":[{"spec":{"selector":{"matchExpressions":[{"key":"location","operator":"invalid"}]}}}]}`,+ nil,+ true,+ },+ }++ for _, test := range tests {+ cfg, err := ValidateAndGetDeploymentSchedule(test.payload)+ if err != nil != test.wantErr {+ t.Errorf("%q: GetDeploymentSchedule(%q) got error %v, wantErr %v.", test.name, test.payload, err, test.wantErr)+ }++ diff := pretty.Diff(cfg, test.wantCfg)+ if len(diff) > 0 {+ t.Errorf("%q: GetDeploymentSchedule(%q) got cfg %+v, want %+v, diff %+v.", test.name, test.payload, cfg, test.wantCfg, diff)+ }+ }+}diff --git a/go.mod b/go.modindex ed2be4a751..bb3383d78c 100644--- a/go.mod+++ b/go.mod@@ -13,6 +13,7 @@ require (github.com/google/jsonapi v1.0.0github.com/google/uuid v1.3.0github.com/gosimple/slug v1.10.0+ github.com/kr/pretty v0.2.1github.com/labstack/echo/v4 v4.6.1github.com/lib/pq v1.10.2github.com/mattn/go-sqlite3 v2.0.1+incompatiblediff --git a/go.sum b/go.sumindex 6e95ffec40..f6bcbe454d 100644--- a/go.sum+++ b/go.sum@@ -586,6 +586,7 @@ github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515 h1:T+h1c/A9Gawja4Y9mFVWjgithub.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=+github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=github.com/kr/pty v1.1.5 h1:hyz3dwM5QLc1Rfoz4FuWJQG5BN7tc6K1MndAUnGpQr4=diff --git a/store/deploy.go b/store/deploy.goindex 9e1c2789fd..5412f5ec05 100644--- a/store/deploy.go+++ b/store/deploy.go@@ -96,6 +96,11 @@ func (s *DeploymentConfigService) FindDeploymentConfig(ctx context.Context, find// UpsertDeploymentConfig upserts a deployment configuration to a project.func (s *DeploymentConfigService) UpsertDeploymentConfig(ctx context.Context, upsert *api.DeploymentConfigUpsert) (*api.DeploymentConfig, error) {+ // Validate the deployment configuration.+ if _, err := api.ValidateAndGetDeploymentSchedule(upsert.Payload); err != nil {+ return nil, err+ }+tx, err := s.db.BeginTx(ctx, nil)if err != nil {return nil, FormatError(err)diff --git a/store/seed/test/10210__deployment_config.sql b/store/seed/test/10210__deployment_config.sqlindex b20df26f93..8e65064e05 100644--- a/store/seed/test/10210__deployment_config.sql+++ b/store/seed/test/10210__deployment_config.sql@@ -15,5 +15,5 @@ VALUES101,3005,'regional',- '{"deployment_config":{"deployments":[{"spec":{"selector":{"matchExpressions":[{"key":"location","operator":"IN","values":["us-central1","europe-west1"]}]}}},{"spec":{"selector":{"matchExpressions":[{"key":"location","operator":"EXISTS"}]}}}]}}'+ '{"deployments":[{"spec":{"selector":{"matchExpressions":[{"key":"location","operator":"IN","values":["us-central1","europe-west1"]}]}}},{"spec":{"selector":{"matchExpressions":[{"key":"location","operator":"EXISTS"}]}}}]}');