forked from datreeio/datree
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move validatePoliciesYaml to a package (datreeio#609)
* refactor: move validatePoliciesYaml to a folder * fix: add defaultRulesSchema.json back * refactor: move validatePoliciesYaml into a separate package * refactor: fix goimports
- Loading branch information
Showing
16 changed files
with
135 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |