Skip to content

Commit

Permalink
incorporate review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
patilpankaj212 committed Feb 18, 2021
1 parent 3e5170b commit 8f0b45c
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 33 deletions.
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,7 @@ github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3
github.com/spf13/cobra v0.0.5/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tLCHU=
github.com/spf13/cobra v1.0.0 h1:6m/oheQuQ13N9ks4hubMG6BnvwOeaJrqSPLahSnczz8=
github.com/spf13/cobra v1.0.0/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE=
github.com/spf13/cobra v1.1.1 h1:KfztREH0tPxJJ+geloSLaAkaPkr4ki2Er5quFV1TDo4=
github.com/spf13/cobra v1.1.1/go.mod h1:WnodtKOvamDL/PwE2M4iKs8aMDBZ5Q5klgD3qfVJQMI=
github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
github.com/spf13/pflag v0.0.0-20170130214245-9ff6c6923cff/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
Expand Down
8 changes: 4 additions & 4 deletions pkg/iac-providers/helm/v3/load-dir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestLoadIacDir(t *testing.T) {
dirPath string
helmv3 HelmV3
want output.AllResourceConfigs
wantErr bool
wantErr error
resourceCount int
}{
{
Expand All @@ -54,22 +54,22 @@ func TestLoadIacDir(t *testing.T) {
name: "bad directory",
dirPath: "./testdata/bad-dir",
helmv3: HelmV3{},
wantErr: true,
wantErr: &os.PathError{Err: syscall.ENOENT, Op: "lstat", Path: "./testdata/bad-dir"},
resourceCount: 0,
},
{
name: "no helm charts in directory",
dirPath: "./testdata/no-helm-charts",
helmv3: HelmV3{},
wantErr: true,
wantErr: fmt.Errorf("no helm charts found in directory ./testdata/no-helm-charts"),
resourceCount: 0,
},
}

for _, tt := range table {
t.Run(tt.name, func(t *testing.T) {
resources, gotErr := tt.helmv3.LoadIacDir(tt.dirPath)
if tt.wantErr && gotErr == nil {
if !reflect.DeepEqual(gotErr, tt.wantErr) {
t.Errorf("unexpected error; gotErr: '%v', wantErr: '%v'", gotErr, tt.wantErr)
}

Expand Down
16 changes: 8 additions & 8 deletions pkg/iac-providers/kubernetes/v1/load-file.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,38 @@ import (

// LoadIacFile loads the k8s file specified
// Note that a single k8s yaml file may contain multiple resource definitions
func (k *K8sV1) LoadIacFile(filePath string) (allResourcesConfig output.AllResourceConfigs, err error) {
func (k *K8sV1) LoadIacFile(absFilePath string) (allResourcesConfig output.AllResourceConfigs, err error) {
allResourcesConfig = make(map[string][]output.ResourceConfig)

var iacDocuments []*utils.IacDocument

fileExt := k.getFileType(filePath)
fileExt := k.getFileType(absFilePath)
switch fileExt {
case YAMLExtension:
fallthrough
case YAMLExtension2:
iacDocuments, err = utils.LoadYAML(filePath)
iacDocuments, err = utils.LoadYAML(absFilePath)
case JSONExtension:
iacDocuments, err = utils.LoadJSON(filePath)
iacDocuments, err = utils.LoadJSON(absFilePath)
default:
zap.S().Debug("unknown extension found", zap.String("extension", fileExt))
return allResourcesConfig, fmt.Errorf("unknown file extension for file %s", filePath)
return allResourcesConfig, fmt.Errorf("unknown file extension for file %s", absFilePath)
}
if err != nil {
zap.S().Debug("failed to load file", zap.String("file", filePath))
zap.S().Debug("failed to load file", zap.String("file", absFilePath))
return allResourcesConfig, err
}

for _, doc := range iacDocuments {
var config *output.ResourceConfig
config, err = k.Normalize(doc)
if err != nil {
zap.S().Debug("unable to normalize data", zap.Error(err), zap.String("file", filePath))
zap.S().Debug("unable to normalize data", zap.Error(err), zap.String("file", absFilePath))
continue
}

config.Line = doc.StartLine
config.Source = filePath
config.Source = absFilePath

allResourcesConfig[config.Type] = append(allResourcesConfig[config.Type], *config)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/iac-providers/kustomize/v3/load-dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (k *KustomizeV3) LoadIacDir(absRootDir string) (output.AllResourceConfigs,
yamlkustomizeobj, err := utils.ReadYamlFile(filepath.Join(absRootDir, kustomizeFileName))

if err != nil {
err = fmt.Errorf("unable to read the kustomization file in the directory : %v", err)
err = fmt.Errorf("unable to read the kustomization file in the directory %s, error: %v", absRootDir, err)
zap.S().Error("error while reading the file", kustomizeFileName, zap.Error(err))
return allResourcesConfig, err
}
Expand Down
13 changes: 8 additions & 5 deletions pkg/iac-providers/kustomize/v3/load-dir_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package kustomizev3

import (
"fmt"
"os"
"reflect"
"syscall"
"testing"

"github.com/accurics/terrascan/pkg/iac-providers/output"
Expand All @@ -15,14 +18,14 @@ func TestLoadIacDir(t *testing.T) {
dirPath string
kustomize KustomizeV3
want output.AllResourceConfigs
wantErr bool
wantErr error
resourceCount int
}{
{
name: "invalid dirPath",
dirPath: "not-there",
kustomize: KustomizeV3{},
wantErr: true,
wantErr: &os.PathError{Err: syscall.ENOENT, Op: "open", Path: "not-there"},
resourceCount: 0,
},
{
Expand Down Expand Up @@ -66,22 +69,22 @@ func TestLoadIacDir(t *testing.T) {
name: "no-kustomize-directory",
dirPath: "./testdata/no-kustomizefile",
kustomize: KustomizeV3{},
wantErr: true,
wantErr: fmt.Errorf("kustomization.y(a)ml file not found in the directory ./testdata/no-kustomizefile"),
resourceCount: 0,
},
{
name: "kustomize-file-empty",
dirPath: "./testdata/kustomize-file-empty",
kustomize: KustomizeV3{},
wantErr: true,
wantErr: fmt.Errorf("unable to read the kustomization file in the directory ./testdata/kustomize-file-empty, error: yaml file is empty"),
resourceCount: 0,
},
}

for _, tt := range table {
t.Run(tt.name, func(t *testing.T) {
resourceMap, gotErr := tt.kustomize.LoadIacDir(tt.dirPath)
if tt.wantErr && gotErr == nil {
if !reflect.DeepEqual(gotErr, tt.wantErr) {
t.Errorf("unexpected error; gotErr: '%v', wantErr: '%v'", gotErr, tt.wantErr)
}

Expand Down
6 changes: 5 additions & 1 deletion pkg/iac-providers/terraform/commons/load-dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ func LoadIacDir(absRootDir string) (allResourcesConfig output.AllResourceConfigs
// load root config directory
rootMod, diags := parser.LoadConfigDir(absRootDir)
if diags.HasErrors() {
errMessage := fmt.Sprintf("failed to load terraform config dir '%s'. error:\n%+v\n", absRootDir, diags)
var errMsgs []string
for _, v := range diags.Errs() {
errMsgs = append(errMsgs, v.Error())
}
errMessage := fmt.Sprintf("failed to load terraform config dir '%s'. error from terraform:\n%+v\n", absRootDir, strings.Join(errMsgs, "\n"))
zap.S().Debug(errMessage)
return allResourcesConfig, fmt.Errorf(errMessage)
}
Expand Down
23 changes: 15 additions & 8 deletions pkg/iac-providers/terraform/v12/load-dir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package tfv12

import (
"encoding/json"
"fmt"
"io/ioutil"
"reflect"
"testing"
Expand All @@ -27,50 +28,56 @@ import (
)

func TestLoadIacDir(t *testing.T) {

testErrorString1 := `failed to load terraform config dir './testdata'. error from terraform:
testdata/empty.tf:1,21-2,1: Invalid block definition; A block definition must have block content delimited by "{" and "}", starting on the same line as the block header.
testdata/empty.tf:1,1-5: Unsupported block type; Blocks of type "some" are not expected here.
`
testErrorString2 := `failed to load terraform config dir './testdata/multiple-required-providers'. error from terraform:
testdata/multiple-required-providers/b.tf:2,3-21: Duplicate required providers configuration; A module may have only one required providers configuration. The required providers were previously configured at testdata/multiple-required-providers/a.tf:2,3-21.
`
table := []struct {
name string
dirPath string
tfv12 TfV12
want output.AllResourceConfigs
wantErr bool
wantErr error
}{
{
name: "invalid dirPath",
dirPath: "not-there",
tfv12: TfV12{},
wantErr: true,
wantErr: fmt.Errorf("directory 'not-there' has no terraform config files"),
},
{
name: "empty config",
dirPath: "./testdata/testfile",
tfv12: TfV12{},
wantErr: true,
wantErr: fmt.Errorf("directory './testdata/testfile' has no terraform config files"),
},
{
name: "incorrect module structure",
dirPath: "./testdata/invalid-moduleconfigs",
tfv12: TfV12{},
wantErr: true,
wantErr: fmt.Errorf("failed to build terraform allResourcesConfig"),
},
{
name: "load invalid config dir",
dirPath: "./testdata",
tfv12: TfV12{},
wantErr: true,
wantErr: fmt.Errorf(testErrorString1),
},
{
name: "load invalid config dir",
dirPath: "./testdata/multiple-required-providers",
tfv12: TfV12{},
wantErr: true,
wantErr: fmt.Errorf(testErrorString2),
},
}

for _, tt := range table {
t.Run(tt.name, func(t *testing.T) {
_, gotErr := tt.tfv12.LoadIacDir(tt.dirPath)
if tt.wantErr && gotErr == nil {
if !reflect.DeepEqual(gotErr, tt.wantErr) {
t.Errorf("unexpected error; gotErr: '%v', wantErr: '%v'", gotErr, tt.wantErr)
}
})
Expand Down
17 changes: 11 additions & 6 deletions pkg/iac-providers/terraform/v14/load-dir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package tfv14

import (
"encoding/json"
"fmt"
"io/ioutil"
"reflect"
"testing"
Expand All @@ -28,43 +29,47 @@ import (

func TestLoadIacDir(t *testing.T) {

testErrorMessage := `failed to load terraform config dir './testdata'. error from terraform:
testdata/empty.tf:1,21-2,1: Invalid block definition; A block definition must have block content delimited by "{" and "}", starting on the same line as the block header.
testdata/empty.tf:1,1-5: Unsupported block type; Blocks of type "some" are not expected here.
`
table := []struct {
name string
dirPath string
tfv14 TfV14
want output.AllResourceConfigs
wantErr bool
wantErr error
}{
{
name: "invalid dirPath",
dirPath: "not-there",
tfv14: TfV14{},
wantErr: true,
wantErr: fmt.Errorf("directory 'not-there' has no terraform config files"),
},
{
name: "empty config",
dirPath: "./testdata/testfile",
tfv14: TfV14{},
wantErr: true,
wantErr: fmt.Errorf("directory './testdata/testfile' has no terraform config files"),
},
{
name: "incorrect module structure",
dirPath: "./testdata/invalid-moduleconfigs",
tfv14: TfV14{},
wantErr: true,
wantErr: fmt.Errorf("failed to build terraform allResourcesConfig"),
},
{
name: "load invalid config dir",
dirPath: "./testdata",
tfv14: TfV14{},
wantErr: true,
wantErr: fmt.Errorf(testErrorMessage),
},
}

for _, tt := range table {
t.Run(tt.name, func(t *testing.T) {
_, gotErr := tt.tfv14.LoadIacDir(tt.dirPath)
if tt.wantErr && gotErr == nil {
if !reflect.DeepEqual(gotErr, tt.wantErr) {
t.Errorf("unexpected error; gotErr: '%v', wantErr: '%v'", gotErr, tt.wantErr)
}
})
Expand Down

0 comments on commit 8f0b45c

Please sign in to comment.