From e0c6af616eda4e9507aef65d91d559d67cbef059 Mon Sep 17 00:00:00 2001 From: Devang Date: Thu, 18 Mar 2021 05:22:43 +0530 Subject: [PATCH] updated warning message when policy base path and rego sub path aren't specified together --- go.mod | 3 ++- go.sum | 2 ++ pkg/cli/color-console_windows.go | 3 ++- pkg/cli/output_writer.go | 3 ++- pkg/cli/register.go | 17 ++++------------- pkg/config/global.go | 6 ++++-- pkg/initialize/run.go | 8 +------- pkg/notifications/notifiers.go | 3 +-- pkg/runtime/executor_test.go | 5 +++-- pkg/termcolor/termcolor.go | 3 ++- test/e2e/init/init_test.go | 8 +++----- 11 files changed, 26 insertions(+), 35 deletions(-) diff --git a/go.mod b/go.mod index 783d9afa8..a0fa774f0 100644 --- a/go.mod +++ b/go.mod @@ -31,7 +31,8 @@ require ( github.com/zclconf/go-cty v1.7.1 go.uber.org/zap v1.16.0 golang.org/x/mod v0.4.2 // indirect - golang.org/x/sys v0.0.0-20210317091845-390168757d9c + golang.org/x/sys v0.0.0-20210317225723-c4fcb01b228e + golang.org/x/tools v0.1.0 // indirect gopkg.in/src-d/go-git.v4 v4.13.1 gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b helm.sh/helm/v3 v3.4.0 diff --git a/go.sum b/go.sum index c5f986e31..1641c7e27 100644 --- a/go.sum +++ b/go.sum @@ -1224,6 +1224,8 @@ golang.org/x/sys v0.0.0-20210316164454-77fc1eacc6aa h1:ZYxPR6aca/uhfRJyaOAtflSHj golang.org/x/sys v0.0.0-20210316164454-77fc1eacc6aa/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210317091845-390168757d9c h1:WGyvPg8lhdtSkb8BiYWdtPlLSommHOmJHFvzWODI7BQ= golang.org/x/sys v0.0.0-20210317091845-390168757d9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210317225723-c4fcb01b228e h1:XNp2Flc/1eWQGk5BLzqTAN7fQIwIbfyVTuVxXxZh73M= +golang.org/x/sys v0.0.0-20210317225723-c4fcb01b228e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.0.0-20160726164857-2910a502d2bf/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= diff --git a/pkg/cli/color-console_windows.go b/pkg/cli/color-console_windows.go index 68973d6ec..07f4feb1a 100644 --- a/pkg/cli/color-console_windows.go +++ b/pkg/cli/color-console_windows.go @@ -19,8 +19,9 @@ package cli import ( - "golang.org/x/sys/windows" "os" + + "golang.org/x/sys/windows" ) // In order for colored output to work on Windows, we need to explicitly diff --git a/pkg/cli/output_writer.go b/pkg/cli/output_writer.go index 80bbbc5f1..5bf82a3ed 100644 --- a/pkg/cli/output_writer.go +++ b/pkg/cli/output_writer.go @@ -1,9 +1,10 @@ package cli import ( - "github.com/accurics/terrascan/pkg/termcolor" "io" "os" + + "github.com/accurics/terrascan/pkg/termcolor" ) // NewOutputWriter gets a new io.Writer based on os.Stdout. diff --git a/pkg/cli/register.go b/pkg/cli/register.go index 8eb088d5c..e3b791dee 100644 --- a/pkg/cli/register.go +++ b/pkg/cli/register.go @@ -28,8 +28,6 @@ import ( "go.uber.org/zap" ) -const configEnvvarName = "TERRASCAN_CONFIG" - // RegisterCommand Registers a new command under the base command func RegisterCommand(baseCommand *cobra.Command, command *cobra.Command) { baseCommand.AddCommand(command) @@ -47,23 +45,16 @@ func Execute() { // Set up the logger logging.Init(LogType, LogLevel) - var configfile string - if len(ConfigFile) > 0 { - configfile = ConfigFile - } - - if len(configfile) == 0 { - configfile = os.Getenv(configEnvvarName) + if len(ConfigFile) == 0 { + ConfigFile = os.Getenv(config.ConfigEnvvarName) + zap.S().Debugf("%s:%s", config.ConfigEnvvarName, os.Getenv(config.ConfigEnvvarName)) } - zap.S().Debugf("%s=%s", configEnvvarName, os.Getenv(configEnvvarName)) - // Make sure we load the global config from the specified config file - if err := config.LoadGlobalConfig(configfile); err != nil { + if err := config.LoadGlobalConfig(ConfigFile); err != nil { zap.S().Error("error while loading global config", zap.Error(err)) os.Exit(1) } - }) // parse the flags but hack around to avoid exiting with error code 2 on help diff --git a/pkg/config/global.go b/pkg/config/global.go index 5a0a117f2..ff2da9e98 100644 --- a/pkg/config/global.go +++ b/pkg/config/global.go @@ -28,6 +28,8 @@ const ( defaultPolicyBranch = "master" ) +const ConfigEnvvarName = "TERRASCAN_CONFIG" + var ( defaultPolicyRepoPath = filepath.Join("pkg", "policies", "opa", "rego") defaultBasePolicyPath = filepath.Join(utils.GetHomeDir(), ".terrascan") @@ -57,11 +59,11 @@ func LoadGlobalConfig(configFile string) error { } if len(configReader.getPolicyConfig().BasePath) > 0 && len(configReader.getPolicyConfig().RepoPath) == 0 { - zap.S().Warnf("policy base path specified in configfile %s, but rego_subdir path not specified.", configFile) + zap.S().Warnf("policy base path specified in configfile '%s', but rego_subdir path not specified. applying default rego_subdir value '%s'", configFile, GetPolicyRepoPath()) } if len(configReader.getPolicyConfig().RepoPath) > 0 && len(configReader.getPolicyConfig().BasePath) == 0 { - zap.S().Warnf("policy rego_subdir specified in configfile %s, but base path not specified.", configFile) + zap.S().Warnf("policy rego_subdir specified in configfile '%s', but base path not specified. applying default base path value '%s'", configFile, GetPolicyBasePath()) } if len(configReader.getPolicyConfig().BasePath) > 0 { diff --git a/pkg/initialize/run.go b/pkg/initialize/run.go index b60d25332..6f610f66a 100644 --- a/pkg/initialize/run.go +++ b/pkg/initialize/run.go @@ -38,14 +38,9 @@ const terrascanReadmeURL string = "https://raw.githubusercontent.com/accurics/te func Run(isNonInitCmd bool) error { zap.S().Debug("initializing terrascan") - zap.S().Debugf("rego subdir path : %s", config.GetPolicyRepoPath()) // check if policy paths exist if path, err := os.Stat(config.GetPolicyRepoPath()); err == nil && path.IsDir() { - - zap.S().Debug("EXISTS AND IS A DIR") if isNonInitCmd { - zap.S().Debug("IS NON INIT") - return nil } } @@ -67,14 +62,13 @@ func Run(isNonInitCmd bool) error { func DownloadPolicies() error { policyBasePath := config.GetPolicyBasePath() - policyRepoPath := config.GetPolicyRepoPath() repoURL := config.GetPolicyRepoURL() branch := config.GetPolicyBranch() zap.S().Debug("downloading policies") zap.S().Debugf("base directory path : %s", policyBasePath) - zap.S().Debugf("policy directory path : %s", policyRepoPath) + zap.S().Debugf("policy directory path : %s", config.GetPolicyRepoPath()) zap.S().Debugf("policy repo url : %s", repoURL) zap.S().Debugf("policy repo git branch : %s", branch) diff --git a/pkg/notifications/notifiers.go b/pkg/notifications/notifiers.go index d25938e52..f3aec807c 100644 --- a/pkg/notifications/notifiers.go +++ b/pkg/notifications/notifiers.go @@ -29,7 +29,7 @@ var ( errNotifierNotSupported = fmt.Errorf("notifier not supported") errNotifierTypeNotPresent = fmt.Errorf("notifier type not present in toml config") // ErrNotificationNotPresent error is caused when there isn't any notification present in the config - ErrNotificationNotPresent = fmt.Errorf("no notification present") + ErrNotificationNotPresent = fmt.Errorf("no notification specified in the config") ) // NewNotifier returns a new notifier @@ -54,7 +54,6 @@ func NewNotifiers() ([]Notifier, error) { // get config for 'notifications' notifications := config.GetNotifications() if len(notifications) == 0 { - zap.S().Debug("no notification detected from config") return notifiers, ErrNotificationNotPresent } diff --git a/pkg/runtime/executor_test.go b/pkg/runtime/executor_test.go index ca63be4d6..1effb0ea2 100644 --- a/pkg/runtime/executor_test.go +++ b/pkg/runtime/executor_test.go @@ -18,12 +18,13 @@ package runtime import ( "fmt" + "reflect" + "testing" + iacProvider "github.com/accurics/terrascan/pkg/iac-providers" tfv12 "github.com/accurics/terrascan/pkg/iac-providers/terraform/v12" tfv14 "github.com/accurics/terrascan/pkg/iac-providers/terraform/v14" "github.com/accurics/terrascan/pkg/notifications/webhook" - "reflect" - "testing" "github.com/accurics/terrascan/pkg/config" "github.com/accurics/terrascan/pkg/iac-providers/output" diff --git a/pkg/termcolor/termcolor.go b/pkg/termcolor/termcolor.go index f1659b1ec..66e47e208 100644 --- a/pkg/termcolor/termcolor.go +++ b/pkg/termcolor/termcolor.go @@ -1,10 +1,11 @@ package termcolor import ( - "go.uber.org/zap" "math" "strconv" "strings" + + "go.uber.org/zap" ) var ( diff --git a/test/e2e/init/init_test.go b/test/e2e/init/init_test.go index 3aea8c250..a98d25ba8 100644 --- a/test/e2e/init/init_test.go +++ b/test/e2e/init/init_test.go @@ -41,8 +41,8 @@ var ( testPolicyRepoPath = filepath.Join(utils.GetHomeDir(), ".terrascan-test") testRegoSubDirPath = filepath.Join(testPolicyRepoPath, "pkg", "policies", "opa", "rego") - warnNoBasePath = "policy rego_subdir specified in configfile config/relative_rego_subdir.toml, but base path not specified." - warnNoSubDirPath = "policy base path specified in configfile config/home_prefixed_path.toml, but rego_subdir path not specified." + warnNoBasePath = "policy rego_subdir specified in configfile 'config/relative_rego_subdir.toml', but base path not specified. applying default base path value" + warnNoSubDirPath = "policy base path specified in configfile 'config/home_prefixed_path.toml', but rego_subdir path not specified. applying default rego_subdir value" ) var _ = Describe("Init", func() { @@ -126,7 +126,7 @@ var _ = Describe("Init", func() { Describe("terrascan init is run with -c flag", func() { Context("config file has valid policy repo and branch data", func() { - +// TODO : dekh isse It("should download policies as per the policy config in the config file", func() { configFile := filepath.Join("config", "valid_repo.toml") session = helper.RunCommand(terrascanBinaryPath, outWriter, errWriter, "init", "-c", configFile) @@ -253,8 +253,6 @@ var _ = Describe("Init", func() { }) JustAfterEach(func() { os.Setenv(terrascanConfigEnvName, "") - //remove the cloned repo at "invalid/path", (refer to 'path' in "config/invalid_path.toml") - os.RemoveAll("invalid") }) // The current behavior of terrascan is that, when init command is being run with an invalid/