Skip to content

Commit

Permalink
refactor: move validatePoliciesYaml to a package (datreeio#609)
Browse files Browse the repository at this point in the history
* refactor: move validatePoliciesYaml to a folder

* fix: add defaultRulesSchema.json back

* refactor: move validatePoliciesYaml into a separate package

* refactor: fix goimports
  • Loading branch information
royhadad authored May 11, 2022
1 parent 871aad9 commit 748d706
Show file tree
Hide file tree
Showing 16 changed files with 135 additions and 126 deletions.
31 changes: 3 additions & 28 deletions pkg/policy/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,20 @@ package policy

import (
_ "embed"
"fmt"
"io/ioutil"
"os"
"path/filepath"

"github.com/datreeio/datree/pkg/validatePoliciesYaml"

"github.com/datreeio/datree/pkg/cliClient"
"github.com/datreeio/datree/pkg/fileReader"
"github.com/datreeio/datree/pkg/jsonSchemaValidator"
"github.com/ghodss/yaml"
)

//go:embed defaultRules.yaml
var embeddedDefaultRulesYamlContent string

//go:embed policiesSchema.json
var policiesSchemaContent string

type DefaultRulesDefinitions struct {
ApiVersion string `yaml:"apiVersion"`
Rules []*DefaultRuleDefinition `yaml:"rules"`
Expand Down Expand Up @@ -53,7 +50,7 @@ func GetPoliciesFileFromPath(path string) (*cliClient.EvaluationPrerunPolicies,

policiesStrBytes := []byte(policiesStr)

err = validatePoliciesYaml(policiesStrBytes, path)
err = validatePoliciesYaml.ValidatePoliciesYaml(policiesStrBytes, path)
if err != nil {
return nil, err
}
Expand All @@ -72,28 +69,6 @@ func GetPoliciesFileFromPath(path string) (*cliClient.EvaluationPrerunPolicies,
return policies, nil
}

func validatePoliciesYaml(content []byte, policyYamlPath string) error {
jsonSchemaValidator := jsonSchemaValidator.New()
jsonContent, _ := yaml.YAMLToJSON(content)
errorsResult, err := jsonSchemaValidator.Validate(policiesSchemaContent, jsonContent)

if err != nil {
return err
}

if errorsResult != nil {
validationErrors := fmt.Errorf("found errors in policies file %s:", policyYamlPath)

for _, validationError := range errorsResult {
validationErrors = fmt.Errorf("%s\n(root)%s: %s", validationErrors, validationError.InstanceLocation, validationError.Error)
}

return validationErrors
}

return nil
}

func yamlToStruct(content string) (*DefaultRulesDefinitions, error) {
var defaultRulesDefinitions DefaultRulesDefinitions
err := yaml.Unmarshal([]byte(content), &defaultRulesDefinitions)
Expand Down
98 changes: 0 additions & 98 deletions pkg/policy/validatePoliciesYaml_test.go

This file was deleted.

File renamed without changes.
34 changes: 34 additions & 0 deletions pkg/validatePoliciesYaml/validatePoliciesYaml.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package validatePoliciesYaml

import (
_ "embed"
"fmt"

"github.com/datreeio/datree/pkg/jsonSchemaValidator"
"github.com/ghodss/yaml"
)

//go:embed policiesSchema.json
var policiesSchemaContent string

func ValidatePoliciesYaml(content []byte, policyYamlPath string) error {
jsonSchemaValidator := jsonSchemaValidator.New()
jsonContent, _ := yaml.YAMLToJSON(content)
errorsResult, err := jsonSchemaValidator.Validate(policiesSchemaContent, jsonContent)

if err != nil {
return err
}

if errorsResult != nil {
validationErrors := fmt.Errorf("found errors in policies file %s:", policyYamlPath)

for _, validationError := range errorsResult {
validationErrors = fmt.Errorf("%s\n(root)%s: %s", validationErrors, validationError.InstanceLocation, validationError.Error)
}

return validationErrors
}

return nil
}
98 changes: 98 additions & 0 deletions pkg/validatePoliciesYaml/validatePoliciesYaml_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package validatePoliciesYaml

import (
_ "embed"
"testing"

"github.com/stretchr/testify/assert"
)

//go:embed test_fixtures/customRulesNull.yaml
var customRulesNull string

func Test_customRulesNull(t *testing.T) {
err := ValidatePoliciesYaml([]byte(customRulesNull), "./test_fixtures/customRulesNull.yaml")
assert.Nil(t, err)
}

//go:embed test_fixtures/policyRulesNull.yaml
var policyRulesNull string

func Test_policyRulesNull(t *testing.T) {
err := ValidatePoliciesYaml([]byte(policyRulesNull), "./test_fixtures/policyRulesNull.yaml")
assert.Nil(t, err)
}

//go:embed test_fixtures/missingCustomRules.yaml
var missingCustomRules string

func Test_missingCustomRules(t *testing.T) {
err := ValidatePoliciesYaml([]byte(missingCustomRules), "./test_fixtures/missingCustomRules.yaml")
assert.Nil(t, err)
}

//go:embed test_fixtures/missingPolicyRules.yaml
var missingPolicyRules string

func Test_missingPolicyRules(t *testing.T) {
err := ValidatePoliciesYaml([]byte(missingPolicyRules), "./test_fixtures/missingPolicyRules.yaml")
assert.Nil(t, err)
}

//go:embed test_fixtures/missingApiVersion.yaml
var missingApiVersion string

func Test_missingApiVersion(t *testing.T) {
err := ValidatePoliciesYaml([]byte(missingApiVersion), "./test_fixtures/missingApiVersion.yaml")
assert.EqualError(t, err, "found errors in policies file ./test_fixtures/missingApiVersion.yaml:\n(root): missing properties: 'apiVersion'")
}

//go:embed test_fixtures/missingPolicyName.yaml
var missingPolicyName string

func Test_missingPolicyName(t *testing.T) {
err := ValidatePoliciesYaml([]byte(missingPolicyName), "./test_fixtures/missingPolicyName.yaml")
assert.EqualError(t, err, "found errors in policies file ./test_fixtures/missingPolicyName.yaml:\n(root)/policies/0: missing properties: 'name'")
}

//go:embed test_fixtures/wrongApiVersion.yaml
var wrongApiVersion string

func Test_wrongApiVersion(t *testing.T) {
err := ValidatePoliciesYaml([]byte(wrongApiVersion), "./test_fixtures/wrongApiVersion.yaml")
assert.EqualError(t, err, "found errors in policies file ./test_fixtures/wrongApiVersion.yaml:\n(root)/apiVersion: value must be \"v1\"")
}

// customRule

//go:embed test_fixtures/customRuleMissingIdentifier.yaml
var customRuleMissingIdentifier string

func Test_customRuleMissingIdentifier(t *testing.T) {
err := ValidatePoliciesYaml([]byte(customRuleMissingIdentifier), "./test_fixtures/customRuleMissingIdentifier.yaml")
assert.EqualError(t, err, "found errors in policies file ./test_fixtures/customRuleMissingIdentifier.yaml:\n(root)/customRules/0: missing properties: 'identifier'")
}

//go:embed test_fixtures/customRuleMissingName.yaml
var customRuleMissingName string

func Test_customRuleMissingName(t *testing.T) {
err := ValidatePoliciesYaml([]byte(customRuleMissingName), "./test_fixtures/customRuleMissingName.yaml")
assert.EqualError(t, err, "found errors in policies file ./test_fixtures/customRuleMissingName.yaml:\n(root)/customRules/0: missing properties: 'name'")
}

//go:embed test_fixtures/customRuleMissingDefaultMessageOnFailure.yaml
var customRuleMissingDefaultMessageOnFailure string

func Test_customRuleMissingDefaultMessageOnFailure(t *testing.T) {
err := ValidatePoliciesYaml([]byte(customRuleMissingDefaultMessageOnFailure), "./test_fixtures/customRuleMissingDefaultMessageOnFailure.yaml")
assert.EqualError(t, err, "found errors in policies file ./test_fixtures/customRuleMissingDefaultMessageOnFailure.yaml:\n(root)/customRules/0: missing properties: 'defaultMessageOnFailure'")
}

//go:embed test_fixtures/customRuleValidSchema.yaml
var customRuleValidSchema string

func Test_customRuleValidSchema(t *testing.T) {
err := ValidatePoliciesYaml([]byte(customRuleValidSchema), "./test_fixtures/customRuleValidSchema.yaml")
assert.Nil(t, err)
}

0 comments on commit 748d706

Please sign in to comment.