Skip to content

Commit

Permalink
fix multierror variable issue for helm, kustomize and k8s (#818)
Browse files Browse the repository at this point in the history
  • Loading branch information
patilpankaj212 committed May 28, 2021
1 parent 1839b24 commit 23bb0ae
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 29 deletions.
19 changes: 9 additions & 10 deletions pkg/iac-providers/helm/v3/load-dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ import (
)

var (
errSkipTestDir = fmt.Errorf("skipping test directory")
errBadChartName = fmt.Errorf("invalid chart name in Chart.yaml")
errBadChartVersion = fmt.Errorf("invalid chart version in Chart.yaml")
errIacLoadDirs *multierror.Error = nil
errSkipTestDir = fmt.Errorf("skipping test directory")
errBadChartName = fmt.Errorf("invalid chart name in Chart.yaml")
errBadChartVersion = fmt.Errorf("invalid chart version in Chart.yaml")
)

// LoadIacDir loads all helm charts under the specified directory
Expand All @@ -51,13 +50,13 @@ func (h *HelmV3) LoadIacDir(absRootDir string, nonRecursive bool) (output.AllRes
fileMap, err := utils.FindFilesBySuffix(absRootDir, h.getHelmChartFilenames())
if err != nil {
zap.S().Debug("error while searching for helm charts", zap.String("root dir", absRootDir), zap.Error(err))
return allResourcesConfig, multierror.Append(errIacLoadDirs, results.DirScanErr{IacType: "helm", Directory: absRootDir, ErrMessage: err.Error()})
return allResourcesConfig, multierror.Append(h.errIacLoadDirs, results.DirScanErr{IacType: "helm", Directory: absRootDir, ErrMessage: err.Error()})
}

if len(fileMap) == 0 {
errMsg := fmt.Sprintf("no helm charts found in directory %s", absRootDir)
zap.S().Debug(zap.String("root dir", absRootDir), zap.Error(err))
return allResourcesConfig, multierror.Append(errIacLoadDirs, results.DirScanErr{IacType: "helm", Directory: absRootDir, ErrMessage: errMsg})
return allResourcesConfig, multierror.Append(h.errIacLoadDirs, results.DirScanErr{IacType: "helm", Directory: absRootDir, ErrMessage: errMsg})
}

// fileDir now contains the chart path
Expand All @@ -74,7 +73,7 @@ func (h *HelmV3) LoadIacDir(absRootDir string, nonRecursive bool) (output.AllRes
if err != nil && err != errSkipTestDir {
errMsg := fmt.Sprintf("error occurred while loading chart. err: %v", err)
logger.Debug("error occurred while loading chart", zap.Error(err))
errIacLoadDirs = multierror.Append(errIacLoadDirs, results.DirScanErr{IacType: "helm", Directory: fileDir, ErrMessage: errMsg})
h.errIacLoadDirs = multierror.Append(h.errIacLoadDirs, results.DirScanErr{IacType: "helm", Directory: fileDir, ErrMessage: errMsg})
continue
}

Expand All @@ -86,7 +85,7 @@ func (h *HelmV3) LoadIacDir(absRootDir string, nonRecursive bool) (output.AllRes
if err != nil {
errMsg := fmt.Sprintf("failed to create helm chart resource. err: %v", err)
logger.Error("failed to create helm chart resource", zap.Any("config", config), zap.Error(err))
errIacLoadDirs = multierror.Append(errIacLoadDirs, results.DirScanErr{IacType: "helm", Directory: fileDir, ErrMessage: errMsg})
h.errIacLoadDirs = multierror.Append(h.errIacLoadDirs, results.DirScanErr{IacType: "helm", Directory: fileDir, ErrMessage: errMsg})
continue
}

Expand All @@ -105,7 +104,7 @@ func (h *HelmV3) LoadIacDir(absRootDir string, nonRecursive bool) (output.AllRes
// in that case, we should not output an error as it was the user's intention to prevent rendering the resource
if err != k8sv1.ErrNoKind {
zap.S().Error("unable to normalize data", zap.Error(err), zap.String("file", doc.FilePath))
errIacLoadDirs = multierror.Append(errIacLoadDirs, err)
h.errIacLoadDirs = multierror.Append(h.errIacLoadDirs, err)
}
continue
}
Expand All @@ -117,7 +116,7 @@ func (h *HelmV3) LoadIacDir(absRootDir string, nonRecursive bool) (output.AllRes
}
}

return allResourcesConfig, errIacLoadDirs
return allResourcesConfig, h.errIacLoadDirs
}

// createHelmChartResource returns normalized Helm Chart resource data
Expand Down
6 changes: 5 additions & 1 deletion pkg/iac-providers/helm/v3/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@

package helmv3

import "github.com/hashicorp/go-multierror"

// HelmV3 struct implements the IacProvider interface
type HelmV3 struct{}
type HelmV3 struct {
errIacLoadDirs *multierror.Error
}

type helmChartData map[string]interface{}

Expand Down
10 changes: 3 additions & 7 deletions pkg/iac-providers/kubernetes/v1/load-dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ import (
"github.com/hashicorp/go-multierror"
)

var (
errIacLoadDirs *multierror.Error = nil
)

func (*K8sV1) getFileType(file string) string {
if strings.HasSuffix(file, YAMLExtension) {
return YAMLExtension
Expand All @@ -36,7 +32,7 @@ func (k *K8sV1) LoadIacDir(absRootDir string, nonRecursive bool) (output.AllReso
fileMap, err := utils.FindFilesBySuffix(absRootDir, K8sFileExtensions())
if err != nil {
zap.S().Debug("error while searching for iac files", zap.String("root dir", absRootDir), zap.Error(err))
return allResourcesConfig, multierror.Append(errIacLoadDirs, results.DirScanErr{IacType: "k8s", Directory: absRootDir, ErrMessage: err.Error()})
return allResourcesConfig, multierror.Append(k.errIacLoadDirs, results.DirScanErr{IacType: "k8s", Directory: absRootDir, ErrMessage: err.Error()})
}

for fileDir, files := range fileMap {
Expand All @@ -47,7 +43,7 @@ func (k *K8sV1) LoadIacDir(absRootDir string, nonRecursive bool) (output.AllReso
if configData, err = k.LoadIacFile(file); err != nil {
errMsg := fmt.Sprintf("error while loading iac file '%s'. err: %v", file, err)
zap.S().Debug("error while loading iac files", zap.String("IAC file", file), zap.Error(err))
errIacLoadDirs = multierror.Append(errIacLoadDirs, results.DirScanErr{IacType: "k8s", Directory: fileDir, ErrMessage: errMsg})
k.errIacLoadDirs = multierror.Append(k.errIacLoadDirs, results.DirScanErr{IacType: "k8s", Directory: fileDir, ErrMessage: errMsg})
continue
}

Expand All @@ -61,7 +57,7 @@ func (k *K8sV1) LoadIacDir(absRootDir string, nonRecursive bool) (output.AllReso
}
}

return allResourcesConfig, errIacLoadDirs
return allResourcesConfig, k.errIacLoadDirs
}

// makeSourcePathRelative modifies the source path of each resource from absolute to relative path
Expand Down
6 changes: 5 additions & 1 deletion pkg/iac-providers/kubernetes/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@

package k8sv1

import "github.com/hashicorp/go-multierror"

// K8sV1 struct implements the IacProvider interface
type K8sV1 struct{}
type K8sV1 struct {
errIacLoadDirs *multierror.Error
}

const (
// YAMLExtension yaml
Expand Down
15 changes: 7 additions & 8 deletions pkg/iac-providers/kustomize/v3/load-dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ const (
)

var (
kustomizeErrMessage = "error from kustomization. error : %v"
errIacLoadDirs *multierror.Error = nil
kustomizeErrMessage = "error from kustomization. error : %v"
)

// LoadIacDir loads the kustomize directory and returns the ResourceConfig mapping which is evaluated by the policy engine
Expand All @@ -31,19 +30,19 @@ func (k *KustomizeV3) LoadIacDir(absRootDir string, nonRecursive bool) (output.A
files, err := utils.FindFilesBySuffixInDir(absRootDir, KustomizeFileNames())
if err != nil {
zap.S().Debug("error while searching for iac files", zap.String("root dir", absRootDir), zap.Error(err))
return allResourcesConfig, multierror.Append(errIacLoadDirs, results.DirScanErr{IacType: "kustomize", Directory: absRootDir, ErrMessage: err.Error()})
return allResourcesConfig, multierror.Append(k.errIacLoadDirs, results.DirScanErr{IacType: "kustomize", Directory: absRootDir, ErrMessage: err.Error()})
}

if len(files) == 0 {
errMsg := fmt.Sprintf("kustomization.y(a)ml file not found in the directory %s", absRootDir)
zap.S().Debug("error while searching for iac files", zap.String("root dir", absRootDir), zap.Error(err))
return allResourcesConfig, multierror.Append(errIacLoadDirs, results.DirScanErr{IacType: "kustomize", Directory: absRootDir, ErrMessage: errMsg})
return allResourcesConfig, multierror.Append(k.errIacLoadDirs, results.DirScanErr{IacType: "kustomize", Directory: absRootDir, ErrMessage: errMsg})
}

if len(files) > 1 {
errMsg := fmt.Sprintf("multiple kustomization.y(a)ml found in the directory %s", absRootDir)
zap.S().Debug("error while searching for iac files", zap.String("root dir", absRootDir), zap.Error(err))
return allResourcesConfig, multierror.Append(errIacLoadDirs, results.DirScanErr{IacType: "kustomize", Directory: absRootDir, ErrMessage: errMsg})
return allResourcesConfig, multierror.Append(k.errIacLoadDirs, results.DirScanErr{IacType: "kustomize", Directory: absRootDir, ErrMessage: errMsg})
}

kustomizeFileName := *files[0]
Expand All @@ -52,7 +51,7 @@ func (k *KustomizeV3) LoadIacDir(absRootDir string, nonRecursive bool) (output.A
if err != nil {
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, multierror.Append(errIacLoadDirs, results.DirScanErr{IacType: "kustomize", Directory: absRootDir, ErrMessage: err.Error()})
return allResourcesConfig, multierror.Append(k.errIacLoadDirs, results.DirScanErr{IacType: "kustomize", Directory: absRootDir, ErrMessage: err.Error()})
}

// ResourceConfig representing the kustomization.y(a)ml file
Expand All @@ -72,7 +71,7 @@ func (k *KustomizeV3) LoadIacDir(absRootDir string, nonRecursive bool) (output.A
if err != nil {
errMsg := fmt.Sprintf("error occurred while loading kustomize directory '%s'. err: %v", absRootDir, err)
zap.S().Error("error occurred while loading kustomize directory", zap.String("kustomize directory", absRootDir), zap.Error(err))
return nil, multierror.Append(errIacLoadDirs, results.DirScanErr{IacType: "kustomize", Directory: absRootDir, ErrMessage: errMsg})
return nil, multierror.Append(k.errIacLoadDirs, results.DirScanErr{IacType: "kustomize", Directory: absRootDir, ErrMessage: errMsg})
}

for _, doc := range iacDocuments {
Expand All @@ -91,7 +90,7 @@ func (k *KustomizeV3) LoadIacDir(absRootDir string, nonRecursive bool) (output.A
allResourcesConfig[config.Type] = append(allResourcesConfig[config.Type], *config)
}

return allResourcesConfig, errIacLoadDirs
return allResourcesConfig, k.errIacLoadDirs
}

// LoadKustomize loads up a 'kustomized' directory and returns a returns a list of IacDocuments
Expand Down
9 changes: 7 additions & 2 deletions pkg/iac-providers/kustomize/v3/types.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package kustomizev3

import "github.com/accurics/terrascan/pkg/utils"
import (
"github.com/accurics/terrascan/pkg/utils"
"github.com/hashicorp/go-multierror"
)

// KustomizeV3 struct
type KustomizeV3 struct{}
type KustomizeV3 struct {
errIacLoadDirs *multierror.Error
}

const (
// YAMLExtension yaml
Expand Down

0 comments on commit 23bb0ae

Please sign in to comment.