Use strict unmarshalling of rules (#7767)

* Fix unmarshalling of rules

Fix #7042

Signed-off-by: Julien Pivotto <roidelapluie@inuits.eu>
This commit is contained in:
Julien Pivotto 2020-08-09 15:42:25 +02:00 committed by GitHub
parent 2899773b01
commit fd06416057
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 2 deletions

View file

@ -14,7 +14,9 @@
package rulefmt package rulefmt
import ( import (
"bytes"
"context" "context"
"io"
"io/ioutil" "io/ioutil"
"strings" "strings"
"time" "time"
@ -267,8 +269,11 @@ func Parse(content []byte) (*RuleGroups, []error) {
errs []error errs []error
) )
err := yaml.Unmarshal(content, &groups) decoder := yaml.NewDecoder(bytes.NewReader(content))
if err != nil { decoder.KnownFields(true)
err := decoder.Decode(&groups)
// Ignore io.EOF which happens with empty input.
if err != nil && err != io.EOF {
errs = append(errs, err) errs = append(errs, err)
} }
err = yaml.Unmarshal(content, &node) err = yaml.Unmarshal(content, &node)

View file

@ -67,6 +67,10 @@ func TestParseFileFailure(t *testing.T) {
filename: "invalid_record_name.bad.yaml", filename: "invalid_record_name.bad.yaml",
errMsg: "invalid recording rule name", errMsg: "invalid recording rule name",
}, },
{
filename: "bad_field.bad.yaml",
errMsg: "field annotation not found",
},
} }
for _, c := range table { for _, c := range table {

10
pkg/rulefmt/testdata/bad_field.bad.yaml vendored Normal file
View file

@ -0,0 +1,10 @@
groups:
- name: yolo
rules:
- alert: hola
expr: 1
labels:
instance: localhost
annotation:
summary: annonations is written without s above