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

changes in log level and messages for load iac functions #541

Merged
merged 2 commits into from
Feb 18, 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
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
36 changes: 17 additions & 19 deletions pkg/iac-providers/helm/v3/load-dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@ import (
)

var (
errSkipTestDir = fmt.Errorf("skipping test directory")
errNoHelmChartsFound = fmt.Errorf("no helm charts found")
errBadChartName = fmt.Errorf("bad chart name in Chart.yaml")
errBadChartVersion = fmt.Errorf("bad chart version in Chart.yaml")
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 @@ -48,14 +47,13 @@ func (h *HelmV3) LoadIacDir(absRootDir string) (output.AllResourceConfigs, error
// find all Chart.yaml files within the specified directory structure
fileMap, err := utils.FindFilesBySuffix(absRootDir, h.getHelmChartFilenames())
if err != nil {
zap.S().Error("error while searching for helm charts", zap.String("root dir", absRootDir), zap.Error(err))
zap.S().Debug("error while searching for helm charts", zap.String("root dir", absRootDir), zap.Error(err))
return allResourcesConfig, err
}

if len(fileMap) == 0 {
err = errNoHelmChartsFound
zap.S().Error("", zap.String("root dir", absRootDir), zap.Error(err))
return allResourcesConfig, err
zap.S().Debug(zap.String("root dir", absRootDir), zap.Error(err))
return allResourcesConfig, fmt.Errorf("no helm charts found in directory %s", absRootDir)
}

// fileDir now contains the chart path
Expand All @@ -70,7 +68,7 @@ func (h *HelmV3) LoadIacDir(absRootDir string) (output.AllResourceConfigs, error
var chartMap helmChartData
iacDocuments, chartMap, err = h.loadChart(chartPath)
if err != nil && err != errSkipTestDir {
logger.Error("error occurred while loading chart", zap.Error(err))
logger.Debug("error occurred while loading chart", zap.Error(err))
continue
}

Expand Down Expand Up @@ -165,7 +163,7 @@ func (h *HelmV3) renderChart(chartPath string, chartMap helmChartData, templateD
var fileData []byte
fileData, err := ioutil.ReadFile(filepath.Join(templateDir, *templateFile))
if err != nil {
logger.Error("unable to read template file", zap.String("file", *templateFile), zap.Error(err))
logger.Debug("error while reading template file", zap.String("file", *templateFile), zap.Error(err))
return iacDocuments, err
}

Expand All @@ -178,14 +176,14 @@ func (h *HelmV3) renderChart(chartPath string, chartMap helmChartData, templateD
// chart name and version are required parameters
chartName, ok := chartMap["name"].(string)
if !ok {
logger.Error("chart name was invalid")
logger.Debug("chart name is invalid")
return iacDocuments, errBadChartName
}

var chartVersion string
chartVersion, ok = chartMap["version"].(string)
if !ok {
logger.Error("chart version was invalid")
logger.Debug("chart version is invalid")
return iacDocuments, errBadChartVersion
}

Expand All @@ -203,7 +201,7 @@ func (h *HelmV3) renderChart(chartPath string, chartMap helmChartData, templateD

v, err := chartutil.ToRenderValues(c, valueMap, options, nil)
if err != nil {
logger.Error("value rendering failed", zap.Any("values", v), zap.Error(err))
logger.Debug("value rendering failed", zap.Any("values", v), zap.Error(err))
return iacDocuments, err
}

Expand All @@ -215,7 +213,7 @@ func (h *HelmV3) renderChart(chartPath string, chartMap helmChartData, templateD
e.LintMode = true
renderData, err = e.Render(c, v)
if err != nil {
logger.Error("error encountered while rendering chart", zap.String("template dir", templateDir), zap.Error(err))
logger.Debug("error encountered while rendering chart", zap.String("template dir", templateDir), zap.Error(err))
return iacDocuments, err
}

Expand All @@ -241,12 +239,12 @@ func (h *HelmV3) loadChart(chartPath string) ([]*utils.IacDocument, helmChartDat
// load the chart file and values file from the specified chart path
chartFileBytes, err := ioutil.ReadFile(chartPath)
if err != nil {
logger.Error("unable to read", zap.Error(err))
logger.Debug("unable to read", zap.Error(err))
return iacDocuments, chartMap, err
}

if err = yaml.Unmarshal(chartFileBytes, &chartMap); err != nil {
logger.Error("unable to unmarshal values", zap.Error(err))
logger.Debug("unable to unmarshal values", zap.Error(err))
return iacDocuments, chartMap, err
}

Expand All @@ -255,21 +253,21 @@ func (h *HelmV3) loadChart(chartPath string) ([]*utils.IacDocument, helmChartDat
valuesFile := filepath.Join(chartDir, helmValuesFilename)
fileInfo, err = os.Stat(valuesFile)
if err != nil {
logger.Error("unable to stat values.yaml", zap.Error(err))
logger.Debug("unable to stat values.yaml", zap.Error(err))
return iacDocuments, chartMap, err
}

logger.With("file name", fileInfo.Name())
var valueFileBytes []byte
valueFileBytes, err = ioutil.ReadFile(valuesFile)
if err != nil {
logger.Error("unable to read values.yaml", zap.Error(err))
logger.Debug("unable to read values.yaml", zap.Error(err))
return iacDocuments, chartMap, err
}

var valueMap map[string]interface{}
if err = yaml.Unmarshal(valueFileBytes, &valueMap); err != nil {
logger.Error("unable to unmarshal values.yaml", zap.Error(err))
logger.Debug("unable to unmarshal values.yaml", zap.Error(err))
return iacDocuments, chartMap, err
}

Expand Down
4 changes: 1 addition & 3 deletions pkg/iac-providers/helm/v3/load-dir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,12 @@ func TestLoadIacDir(t *testing.T) {
name: "happy path (credit to madhuakula/kubernetes-goat)",
dirPath: "./testdata/happy-path",
helmv3: HelmV3{},
wantErr: nil,
resourceCount: 3,
},
{
name: "happy path with subchart (credit to madhuakula/kubernetes-goat)",
dirPath: "./testdata/happy-path-with-subchart",
helmv3: HelmV3{},
wantErr: nil,
resourceCount: 5,
},
{
Expand All @@ -63,7 +61,7 @@ func TestLoadIacDir(t *testing.T) {
name: "no helm charts in directory",
dirPath: "./testdata/no-helm-charts",
helmv3: HelmV3{},
wantErr: errNoHelmChartsFound,
wantErr: fmt.Errorf("no helm charts found in directory ./testdata/no-helm-charts"),
resourceCount: 0,
},
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/iac-providers/kubernetes/v1/load-dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (k *K8sV1) LoadIacDir(absRootDir string) (output.AllResourceConfigs, error)

fileMap, err := utils.FindFilesBySuffix(absRootDir, K8sFileExtensions())
if err != nil {
zap.S().Warn("error while searching for iac files", zap.String("root dir", absRootDir), zap.Error(err))
zap.S().Debug("error while searching for iac files", zap.String("root dir", absRootDir), zap.Error(err))
return allResourcesConfig, err
}

Expand Down
20 changes: 11 additions & 9 deletions pkg/iac-providers/kubernetes/v1/load-file.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package k8sv1

import (
"fmt"

"github.com/accurics/terrascan/pkg/utils"

"github.com/accurics/terrascan/pkg/iac-providers/output"
Expand All @@ -9,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(absRootPath 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(absRootPath)
fileExt := k.getFileType(absFilePath)
switch fileExt {
case YAMLExtension:
fallthrough
case YAMLExtension2:
iacDocuments, err = utils.LoadYAML(absRootPath)
iacDocuments, err = utils.LoadYAML(absFilePath)
case JSONExtension:
iacDocuments, err = utils.LoadJSON(absRootPath)
iacDocuments, err = utils.LoadJSON(absFilePath)
default:
zap.S().Error("unknown extension found", zap.String("extension", fileExt))
return allResourcesConfig, err
zap.S().Debug("unknown extension found", zap.String("extension", fileExt))
return allResourcesConfig, fmt.Errorf("unknown file extension for file %s", absFilePath)
}
if err != nil {
zap.S().Info("failed to load file", zap.String("file", absRootPath))
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", absRootPath))
zap.S().Debug("unable to normalize data", zap.Error(err), zap.String("file", absFilePath))
continue
}

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

allResourcesConfig[config.Type] = append(allResourcesConfig[config.Type], *config)
}
Expand Down
18 changes: 7 additions & 11 deletions pkg/iac-providers/kustomize/v3/load-dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ const (
)

var (
errorKustomizeNotFound = fmt.Errorf("kustomization.y(a)ml file not found in the directory")
errorMultipleKustomizeFile = fmt.Errorf("multiple kustomization.y(a)ml found in the directory")
errorFromKustomize = fmt.Errorf("error from kustomization")
errorFromKustomize = fmt.Errorf("error from kustomization")
)

// LoadIacDir loads the kustomize directory and returns the ResourceConfig mapping which is evaluated by the policy engine
Expand All @@ -29,27 +27,25 @@ func (k *KustomizeV3) LoadIacDir(absRootDir string) (output.AllResourceConfigs,

files, err := utils.FindFilesBySuffixInDir(absRootDir, KustomizeFileNames())
if err != nil {
zap.S().Error("error while searching for iac files", zap.String("root dir", absRootDir), zap.Error(err))
zap.S().Debug("error while searching for iac files", zap.String("root dir", absRootDir), zap.Error(err))
return allResourcesConfig, err
}

if len(files) == 0 {
err = errorKustomizeNotFound
zap.S().Error("error while searching for iac files", zap.String("root dir", absRootDir), zap.Error(err))
return allResourcesConfig, err
zap.S().Debug("error while searching for iac files", zap.String("root dir", absRootDir), zap.Error(err))
return allResourcesConfig, fmt.Errorf("kustomization.y(a)ml file not found in the directory %s", absRootDir)
}

if len(files) > 1 {
err = errorMultipleKustomizeFile
zap.S().Error("error while searching for iac files", zap.String("root dir", absRootDir), zap.Error(err))
return allResourcesConfig, err
zap.S().Debug("error while searching for iac files", zap.String("root dir", absRootDir), zap.Error(err))
return allResourcesConfig, fmt.Errorf("multiple kustomization.y(a)ml found in the directory %s", absRootDir)
}

kustomizeFileName := *files[0]
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
12 changes: 2 additions & 10 deletions pkg/iac-providers/kustomize/v3/load-dir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
"github.com/accurics/terrascan/pkg/utils"
)

var errorReadKustomize = fmt.Errorf("unable to read the kustomization file in the directory : %s", utils.ErrYamlFileEmpty.Error())

func TestLoadIacDir(t *testing.T) {

table := []struct {
Expand All @@ -34,57 +32,51 @@ func TestLoadIacDir(t *testing.T) {
name: "simple-deployment",
dirPath: "./testdata/simple-deployment",
kustomize: KustomizeV3{},
wantErr: nil,
resourceCount: 4,
},
{
name: "multibases",
dirPath: "./testdata/multibases/base",
kustomize: KustomizeV3{},
wantErr: nil,
resourceCount: 2,
},
{
name: "multibases",
dirPath: "./testdata/multibases/dev",
kustomize: KustomizeV3{},
wantErr: nil,
resourceCount: 2,
},
{
name: "multibases",
dirPath: "./testdata/multibases/prod",
kustomize: KustomizeV3{},
wantErr: nil,
resourceCount: 2,
},

{
name: "multibases",
dirPath: "./testdata/multibases/stage",
kustomize: KustomizeV3{},
wantErr: nil,
resourceCount: 2,
},
{
name: "multibases",
dirPath: "./testdata/multibases",
kustomize: KustomizeV3{},
wantErr: nil,
resourceCount: 4,
},
{
name: "no-kustomize-directory",
dirPath: "./testdata/no-kustomizefile",
kustomize: KustomizeV3{},
wantErr: errorKustomizeNotFound,
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: errorReadKustomize,
wantErr: fmt.Errorf("unable to read the kustomization file in the directory ./testdata/kustomize-file-empty, error: yaml file is empty"),
resourceCount: 0,
},
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/iac-providers/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const (
terraformV12 supportedIacVersion = "v12"
terraformV13 supportedIacVersion = "v13"
terraformV14 supportedIacVersion = "v14"
terraformDefaultVersion = terraformV12
terraformDefaultVersion = terraformV14
)

// register terraform as an IaC provider with terrascan
Expand Down
14 changes: 6 additions & 8 deletions pkg/iac-providers/terraform/commons/load-dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ import (
)

var (
// ErrEmptyTFConfigDir error
ErrEmptyTFConfigDir = fmt.Errorf("directory has no terraform files")
kanchwala-yusuf marked this conversation as resolved.
Show resolved Hide resolved
// ErrLoadConfigDir error
ErrLoadConfigDir = fmt.Errorf("failed to load terraform allResourcesConfig dir")
// ErrBuildTFConfigDir error
ErrBuildTFConfigDir = fmt.Errorf("failed to build terraform allResourcesConfig")
)
Expand All @@ -64,15 +60,17 @@ func LoadIacDir(absRootDir string) (allResourcesConfig output.AllResourceConfigs

// check if the directory has any tf config files (.tf or .tf.json)
if !parser.IsConfigDir(absRootDir) {
zap.S().Errorf("directory '%s' has no terraform config files", absRootDir)
return allResourcesConfig, ErrEmptyTFConfigDir
errMessage := fmt.Sprintf("directory '%s' has no terraform config files", absRootDir)
zap.S().Debug(errMessage)
return allResourcesConfig, fmt.Errorf(errMessage)
}

// load root config directory
rootMod, diags := parser.LoadConfigDir(absRootDir)
if diags.HasErrors() {
zap.S().Errorf("failed to load terraform config dir '%s'. error:\n%+v\n", absRootDir, diags)
return allResourcesConfig, ErrLoadConfigDir
errMessage := fmt.Sprintf("failed to load terraform config dir '%s'. error from terraform:\n%+v\n", absRootDir, getErrorMessagesFromDiagnostics(diags))
zap.S().Debug(errMessage)
return allResourcesConfig, fmt.Errorf(errMessage)
}

// create a new downloader to install remote modules
Expand Down
Loading