Skip to content

Commit

Permalink
updated warning message when policy base path and rego sub path aren'…
Browse files Browse the repository at this point in the history
…t specified together
  • Loading branch information
devang-gaur committed Mar 22, 2021
1 parent cc3b4bb commit e0c6af6
Show file tree
Hide file tree
Showing 11 changed files with 26 additions and 35 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
3 changes: 2 additions & 1 deletion pkg/cli/color-console_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion pkg/cli/output_writer.go
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
17 changes: 4 additions & 13 deletions pkg/cli/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions pkg/config/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 1 addition & 7 deletions pkg/initialize/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand All @@ -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)

Expand Down
3 changes: 1 addition & 2 deletions pkg/notifications/notifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand Down
5 changes: 3 additions & 2 deletions pkg/runtime/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion pkg/termcolor/termcolor.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package termcolor

import (
"go.uber.org/zap"
"math"
"strconv"
"strings"

"go.uber.org/zap"
)

var (
Expand Down
8 changes: 3 additions & 5 deletions test/e2e/init/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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/
Expand Down

0 comments on commit e0c6af6

Please sign in to comment.