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 malformed files not reporting error #2754

Merged
merged 3 commits into from
Apr 9, 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
8 changes: 4 additions & 4 deletions pkg/parser/terraform/converter/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ var inputVarMap = make(InputVariableMap)

// DefaultConverted an hcl File to a toJson serializable object
// This assumes that the body is a hclsyntax.Body
var DefaultConverted = func(file *hcl.File, inputVariables InputVariableMap) (model.Document, int, error) {
var DefaultConverted = func(file *hcl.File, inputVariables InputVariableMap) (model.Document, error) {
inputVarMap = inputVariables
c := converter{bytes: file.Bytes}
body, err := c.convertBody(file.Body.(*hclsyntax.Body))

if err != nil {
sentry.CaptureException(err)
if er, ok := err.(*hcl.Diagnostic); ok && er.Subject != nil {
return nil, er.Subject.Start.Line, err
return nil, err
}

return nil, 0, err
return nil, err
}

return body, 0, nil
return body, nil
}

type converter struct {
Expand Down
10 changes: 5 additions & 5 deletions pkg/parser/terraform/converter/default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ block "label_one" "label_two" {

file, _ := hclsyntax.ParseConfig([]byte(input), "testFileName", hcl.Pos{Byte: 0, Line: 1, Column: 1})

body, _, err := DefaultConverted(file, InputVariableMap{})
body, err := DefaultConverted(file, InputVariableMap{})
if err != nil {
t.Fatal("parse bytes:", err)
}
Expand Down Expand Up @@ -62,7 +62,7 @@ block "label_one" {

file, _ := hclsyntax.ParseConfig([]byte(input), "testFileName", hcl.Pos{Byte: 0, Line: 1, Column: 1})

body, _, err := DefaultConverted(file, InputVariableMap{})
body, err := DefaultConverted(file, InputVariableMap{})
if err != nil {
t.Fatal("parse bytes:", err)
}
Expand Down Expand Up @@ -99,7 +99,7 @@ block "label_one" {

file, _ := hclsyntax.ParseConfig([]byte(input), "testFileName", hcl.Pos{Byte: 0, Line: 1, Column: 1})

body, _, err := DefaultConverted(file, InputVariableMap{})
body, err := DefaultConverted(file, InputVariableMap{})
if err != nil {
t.Fatal("parse bytes:", err)
}
Expand Down Expand Up @@ -128,7 +128,7 @@ block "label_one" {

file, _ := hclsyntax.ParseConfig([]byte(input), "testFileName", hcl.Pos{Byte: 0, Line: 1, Column: 1})

body, _, err := DefaultConverted(file, InputVariableMap{
body, err := DefaultConverted(file, InputVariableMap{
"var": cty.ObjectVal(map[string]cty.Value{
"test": cty.StringVal("my-test"),
}),
Expand Down Expand Up @@ -271,7 +271,7 @@ variable "region" {

file, _ := hclsyntax.ParseConfig([]byte(input), "testFileName", hcl.Pos{Byte: 0, Line: 1, Column: 1})

body, _, err := DefaultConverted(file, InputVariableMap{})
body, err := DefaultConverted(file, InputVariableMap{})
if err != nil {
t.Fatal("parse bytes:", err)
}
Expand Down
48 changes: 7 additions & 41 deletions pkg/parser/terraform/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package terraform

import (
"path/filepath"
"strings"

"github.com/Checkmarx/kics/pkg/model"
"github.com/Checkmarx/kics/pkg/parser/terraform/converter"
Expand All @@ -15,7 +14,7 @@ import (
const RetriesDefaultValue = 50

// Converter returns content json, error line, error
type Converter func(file *hcl.File, inputVariables converter.InputVariableMap) (model.Document, int, error)
type Converter func(file *hcl.File, inputVariables converter.InputVariableMap) (model.Document, error)

// Parser struct that contains the function to parse file and the number of retries if something goes wrong
type Parser struct {
Expand All @@ -39,22 +38,15 @@ func (p *Parser) Resolve(fileContent []byte, filename string) (*[]byte, error) {

// Parse execute parser for the content in a file
func (p *Parser) Parse(path string, content []byte) ([]model.Document, error) {
var (
fc model.Document
lineOfErr int
parseErr error
)
file, diagnostics := hclsyntax.ParseConfig(content, filepath.Base(path), hcl.Pos{Byte: 0, Line: 1, Column: 1})

for try := 0; try < p.numOfRetries; try++ {
fc, lineOfErr, parseErr = p.doParse(content, filepath.Base(path))
if parseErr != nil && lineOfErr != 0 {
content = p.removeProblematicLine(content, lineOfErr)
continue
}

break
if diagnostics != nil && diagnostics.HasErrors() && len(diagnostics.Errs()) > 0 {
err := diagnostics.Errs()[0]
return nil, err
}

fc, parseErr := p.convertFunc(file, inputVariableMap)

return []model.Document{fc}, errors.Wrap(parseErr, "failed terraform parse")
}

Expand All @@ -72,29 +64,3 @@ func (p *Parser) SupportedTypes() []string {
func (p *Parser) GetKind() model.FileKind {
return model.KindTerraform
}

func (p *Parser) removeProblematicLine(content []byte, line int) []byte {
lines := strings.Split(string(content), "\n")
if line > 0 && line <= len(lines) {
lines[line-1] = ""
return []byte(strings.Join(lines, "\n"))
}
return content
}

func (p *Parser) doParse(content []byte, fileName string) (json model.Document, errLine int, err error) {
file, diagnostics := hclsyntax.ParseConfig(content, fileName, hcl.Pos{Byte: 0, Line: 1, Column: 1})

if diagnostics != nil && diagnostics.HasErrors() && len(diagnostics.Errs()) > 0 {
err := diagnostics.Errs()[0]
line := 0

if e, ok := err.(*hcl.Diagnostic); ok {
line = e.Subject.Start.Line
}

return nil, line, errors.Wrap(err, "failed to parse file")
}

return p.convertFunc(file, inputVariableMap)
}
2 changes: 1 addition & 1 deletion pkg/parser/terraform/terraform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var (
have = `
resource "aws_s3_bucket" "b" {
bucket = "S3B_541"
acl = public-read"
acl = "public-read"

tags = {
Name = "My bucket"
Expand Down