Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(parser): fix issue with parser returning panic #4223 #4224

Merged
merged 2 commits into from
Sep 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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