Skip to content

Commit

Permalink
fix(parser): fix issue with parser returning panic #4223 (#4224)
Browse files Browse the repository at this point in the history
Signed-off-by: João Reigota <[email protected]>
  • Loading branch information
joaoReigota1 authored Sep 17, 2021
1 parent aa630a1 commit 799ebf4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
12 changes: 9 additions & 3 deletions pkg/analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var (
k8sRegex = regexp.MustCompile("(\\s*\"apiVersion\":)|(\\s*apiVersion:)")
k8sRegexKind = regexp.MustCompile("(\\s*\"kind\":)|(\\s*kind:)")
k8sRegexMetadata = regexp.MustCompile("(\\s*\"metadata\":)|(\\s*metadata:)")
ansibleVaultRegex = regexp.MustCompile(`^\s*\$ANSIBLE_VAULT.*`)
)

const (
Expand Down Expand Up @@ -183,9 +184,14 @@ func checkContent(path string, results, unwanted chan<- string, ext string) {
// write to channel type of file
results <- returnType
} else if ext == yaml || ext == yml {
// Since Ansible has no defining property
// and no other type matched for YAML file extension, assume the file type is Ansible
results <- "ansible"
// check if it is an ansible vault
if res := ansibleVaultRegex.Match(content); res {
unwanted <- path
} else {
// Since Ansible has no defining property
// and no other type matched for YAML file extension, assume the file type is Ansible
results <- "ansible"
}
} else {
// No type was determined (ignore on parser)
unwanted <- path
Expand Down
20 changes: 12 additions & 8 deletions pkg/model/model_yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package model

import (
json "encoding/json"
"errors"
"strconv"

"github.com/rs/zerolog/log"
Expand All @@ -10,14 +11,17 @@ import (

// UnmarshalYAML is a custom yaml parser that places line information in the payload
func (m *Document) UnmarshalYAML(value *yaml.Node) error {
dpc := unmarshal(value).(map[string]interface{})
// set line information for root level objects
dpc["_kics_lines"] = getLines(value, 0)

// place the payload in the Document struct
tmp, _ := json.Marshal(dpc)
_ = json.Unmarshal(tmp, m)
return nil
dpc := unmarshal(value)
if mapDcp, ok := dpc.(map[string]interface{}); ok {
// set line information for root level objects
mapDcp["_kics_lines"] = getLines(value, 0)

// place the payload in the Document struct
tmp, _ := json.Marshal(mapDcp)
_ = json.Unmarshal(tmp, m)
return nil
}
return errors.New("failed to parse yaml content")
}

/*
Expand Down

0 comments on commit 799ebf4

Please sign in to comment.