Skip to content

Commit

Permalink
fix: panic when validation unsupported file type and rollbac to previ…
Browse files Browse the repository at this point in the history
…ous behavior with unsupported file types (#95)

fixes #94

Signed-off-by: Martin Chodur <[email protected]>
  • Loading branch information
FUSAKLA authored Oct 31, 2024
1 parent c20d2b5 commit 5e707fc
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added: New validation `expressionDoesNotUseClassicHistogramBucketOperations` to avoid queries fragile because of the classic histogram bucket operations.
See the docs for more info [expressionDoesNotUseClassicHistogramBucketOperations](docs/validations.md#expressiondoesnotuseclassichistogrambucketoperations)
- Changed: :warning: revert the ENV expansion in config file since it was breaking and caused issues (it was a stupid idea)
- Fix: Avoid panic when validating unsupported file type and revert to previous behavior when any file was treated as a rule file regardless of the extension.
The only exception are `.jsonnet` files that are evaluated first.

## [3.4.0] - 2024-10-30
- Fixed: :warning: Ignore white spaces around rule names in the `disabled_validation_rules` annotation CSV format (Thanks @jmichalek13 !)
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ build:

E2E_TESTS_VALIDATIONS_FILE := examples/validation.yaml
E2E_TESTS_ADDITIONAL_VALIDATIONS_FILE := examples/additional-validation.yaml
E2E_TESTS_RULES_FILES := examples/rules/*.yaml
E2E_TESTS_RULES_FILES := examples/rules/*
E2E_TESTS_DOCS_FILE_MD := examples/human_readable.md
E2E_TESTS_DOCS_FILE_HTML := examples/human_readable.html

Expand Down
17 changes: 17 additions & 0 deletions examples/rules/foo.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
groups: [
{
name: 'foo',
limit: 1,
rules: [
{
alert: 'foo',
expr: |||
# ignore_validations: hasLabels,hasAnyOfAnnotations,hasAnnotations,hasAllowedLimit
1
|||,
},
],
},
],
}
6 changes: 6 additions & 0 deletions examples/rules/foo.rules
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# ignore_validations: hasLabels,hasAnyOfAnnotations,hasAnnotations,hasAllowedLimit
groups:
- name: foo
rules:
- alert: foo
expr: 1
19 changes: 10 additions & 9 deletions pkg/validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,26 @@ func Files(fileNames []string, validationRules []*validationrule.ValidationRule,
validationReport.FilesCount++
fileReport := validationReport.NewFileReport(fileName)
var yamlReader io.Reader
if strings.HasSuffix(fileName, ".yaml") || strings.HasSuffix(fileName, ".yml") {
var err error
yamlReader, err = os.Open(fileName)
switch {
case strings.HasSuffix(fileName, ".jsonnet"):
log.Debugf("evaluating jsonnet file %s", fileName)
jsonnetOutput, err := jsonnetVM.EvaluateFile(fileName)
if err != nil {
validationReport.Failed = true
fileReport.Valid = false
fileReport.Errors = []error{fmt.Errorf("cannot read file %s: %w", fileName, err)}
fileReport.Errors = []error{fmt.Errorf("cannot evaluate jsonnet file %s: %w", fileName, err)}
continue
}
} else if strings.HasSuffix(fileName, ".jsonnet") {
log.Debugf("evaluating jsonnet file %s", fileName)
jsonnetOutput, err := jsonnetVM.EvaluateFile(fileName)
yamlReader = strings.NewReader(jsonnetOutput)
default:
var err error
yamlReader, err = os.Open(fileName)
if err != nil {
validationReport.Failed = true
fileReport.Valid = false
fileReport.Errors = []error{fmt.Errorf("cannot evaluate jsonnet file %s: %w", fileName, err)}
fileReport.Errors = []error{fmt.Errorf("cannot read file %s: %w", fileName, err)}
continue
}
yamlReader = strings.NewReader(jsonnetOutput)
}
var rf unmarshaler.RulesFileWithComment
decoder := yaml.NewDecoder(yamlReader)
Expand Down

0 comments on commit 5e707fc

Please sign in to comment.