Skip to content

Commit

Permalink
temp3
Browse files Browse the repository at this point in the history
  • Loading branch information
devang-gaur committed Mar 13, 2021
1 parent 7368503 commit 51d8817
Show file tree
Hide file tree
Showing 13 changed files with 264 additions and 166 deletions.
2 changes: 2 additions & 0 deletions pkg/cli/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ func Execute() {
configfile = os.Getenv(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 {
zap.S().Error("error while loading global config", zap.Error(err))
Expand Down
4 changes: 2 additions & 2 deletions pkg/config/config-reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (

var (
// ErrTomlLoadConfig indicates error: Failed to load toml config
errTomlLoadConfig = fmt.Errorf("failed to load toml config")
ErrTomlLoadConfig = fmt.Errorf("failed to load toml config")
// ErrNotPresent indicates error: Config file not present
ErrNotPresent = fmt.Errorf("config file not present")
)
Expand Down Expand Up @@ -59,7 +59,7 @@ func NewTerrascanConfigReader(fileName string) (*TerrascanConfigReader, error) {
data, err := ioutil.ReadFile(fileName)
if err != nil {
zap.S().Error("error loading config file", zap.Error(err))
return configReader, errTomlLoadConfig
return configReader, ErrTomlLoadConfig
}

if err = toml.Unmarshal(data, &configReader.config); err != nil {
Expand Down
3 changes: 0 additions & 3 deletions pkg/config/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,9 @@ func LoadGlobalConfig(configFile string) error {
var err error

if configReader, err = NewTerrascanConfigReader(configFile); err != nil {

absolutePolicyBasePath, absolutePolicyRepoPath, _ := utils.GetAbsPolicyConfigPaths(GetPolicyBasePath(), GetPolicyRepoPath())

global.Policy.BasePath = absolutePolicyBasePath
global.Policy.RepoPath = absolutePolicyRepoPath

return err
}

Expand Down
5 changes: 2 additions & 3 deletions pkg/initialize/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@ func DownloadPolicies() error {
repoURL := config.GetPolicyRepoURL()
branch := config.GetPolicyBranch()

fmt.Printf("policyBasePath %s\n", policyBasePath)
fmt.Printf("policyRepoPath %s\n", policyRepoPath)

zap.S().Debug("downloading policies")

tempPath, err := ioutil.TempDir("", "terrascan-")
Expand Down Expand Up @@ -109,6 +106,8 @@ func DownloadPolicies() error {

zap.S().Debugf("base directory path : %s", policyBasePath)
zap.S().Debugf("policy directory path : %s", policyRepoPath)
zap.S().Debugf("policy repo url : %s", repoURL)
zap.S().Debugf("policy repo git branch : %s", branch)

// move the freshly cloned repo from tempPath to basePath
if err = os.Rename(tempPath, policyBasePath); err != nil {
Expand Down
7 changes: 1 addition & 6 deletions pkg/notifications/notifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ import (
"go.uber.org/zap"
)

const (
notificationsConfigKey = "notifications"
)

var (
errNotifierNotSupported = fmt.Errorf("notifier not supported")
errNotifierTypeNotPresent = fmt.Errorf("notifier type not present in toml config")
Expand Down Expand Up @@ -57,8 +53,7 @@ func NewNotifiers() ([]Notifier, error) {

// get config for 'notifications'
notifications := config.GetNotifications()
fmt.Printf("notification map: %v\n", notifications)
if notifications == nil || len(notifications) == 0 {
if len(notifications) == 0 {
zap.S().Debug("no notification detected from config")
return notifiers, ErrNotificationNotPresent
}
Expand Down
270 changes: 140 additions & 130 deletions pkg/runtime/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,16 @@ package runtime

import (
"fmt"
iacProvider "github.com/accurics/terrascan/pkg/iac-providers"
tfv12 "github.com/accurics/terrascan/pkg/iac-providers/terraform/v12"
"github.com/accurics/terrascan/pkg/notifications/webhook"
"reflect"
"testing"

"github.com/accurics/terrascan/pkg/config"
iacProvider "github.com/accurics/terrascan/pkg/iac-providers"
"github.com/accurics/terrascan/pkg/iac-providers/output"
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"
"github.com/accurics/terrascan/pkg/notifications/webhook"
"github.com/accurics/terrascan/pkg/policy"
policyopa "github.com/accurics/terrascan/pkg/policy/opa"
"github.com/accurics/terrascan/pkg/utils"
)

Expand Down Expand Up @@ -175,123 +173,129 @@ func TestExecute(t *testing.T) {
}

func TestInit(t *testing.T) {

table := []struct {
name string
executor Executor
wantErr error
wantIacProvider iacProvider.IacProvider
wantNotifiers []notifications.Notifier
}{
{
name: "valid filePath",
executor: Executor{
filePath: "./testdata/testfile",
dirPath: "",
cloudType: []string{"aws"},
iacType: "terraform",
iacVersion: "v14",
policyPath: []string{"./testdata/testpolicies"},
},
wantErr: nil,
wantIacProvider: &tfv14.TfV14{},
wantNotifiers: []notifications.Notifier{},
},
{
name: "valid notifier",
executor: Executor{
filePath: "./testdata/testfile",
dirPath: "",
cloudType: []string{"aws"},
iacType: "terraform",
iacVersion: "v14",
configFile: "./testdata/webhook.toml",
policyPath: []string{"./testdata/testpolicies"},
},
wantErr: nil,
wantIacProvider: &tfv14.TfV14{},
wantNotifiers: []notifications.Notifier{&webhook.Webhook{}},
},
{
name: "invalid notifier",
executor: Executor{
filePath: "./testdata/testfile",
dirPath: "",
cloudType: []string{"aws"},
iacType: "terraform",
iacVersion: "v14",
configFile: "testdata/invalid-notifier.toml",
},
wantErr: fmt.Errorf("notifier not supported"),
wantIacProvider: &tfv14.TfV14{},
wantNotifiers: []notifications.Notifier{&webhook.Webhook{}},
},
{
name: "config not present",
executor: Executor{
filePath: "./testdata/testfile",
dirPath: "",
cloudType: []string{"aws"},
iacType: "terraform",
iacVersion: "v14",
configFile: "./testdata/does-not-exist",
},
wantErr: nil,
wantIacProvider: &tfv14.TfV14{},
},
{
name: "invalid policy path",
executor: Executor{
filePath: "./testdata/testfile",
dirPath: "",
cloudType: []string{"aws"},
iacType: "terraform",
iacVersion: "v14",
configFile: "./testdata/webhook.toml",
policyPath: []string{"./testdata/notthere"},
},
wantErr: fmt.Errorf("failed to initialize OPA policy engine"),
wantIacProvider: &tfv14.TfV14{},
wantNotifiers: []notifications.Notifier{&webhook.Webhook{}},
},
{
name: "config file with invalid category",
executor: Executor{
filePath: "./testdata/testfile",
dirPath: "",
cloudType: []string{"aws"},
iacType: "terraform",
iacVersion: "v14",
configFile: "./testdata/invalid-category.toml",
policyPath: []string{"./testdata/notthere"},
/*
table := []struct {
name string
executor Executor
wantErr error
wantIacProvider iacProvider.IacProvider
wantNotifiers []notifications.Notifier
}{
{
name: "valid filePath",
executor: Executor{
filePath: "./testdata/testfile",
dirPath: "",
cloudType: []string{"aws"},
iacType: "terraform",
iacVersion: "v14",
policyPath: []string{"./testdata/testpolicies"},
},
wantErr: nil,
wantIacProvider: &tfv14.TfV14{},
wantNotifiers: []notifications.Notifier{},
},
{
name: "valid notifier",
executor: Executor{
filePath: "./testdata/testfile",
dirPath: "",
cloudType: []string{"aws"},
iacType: "terraform",
iacVersion: "v14",
configFile: "./testdata/webhook.toml",
policyPath: []string{"./testdata/testpolicies"},
},
wantErr: nil,
wantIacProvider: &tfv14.TfV14{},
wantNotifiers: []notifications.Notifier{&webhook.Webhook{}},
},
{
name: "invalid notifier",
executor: Executor{
filePath: "./testdata/testfile",
dirPath: "",
cloudType: []string{"aws"},
iacType: "terraform",
iacVersion: "v14",
configFile: "testdata/invalid-notifier.toml",
},
wantErr: fmt.Errorf("notifier not supported"),
wantIacProvider: &tfv14.TfV14{},
wantNotifiers: []notifications.Notifier{&webhook.Webhook{}},
},
{
name: "config not present",
executor: Executor{
filePath: "./testdata/testfile",
dirPath: "",
cloudType: []string{"aws"},
iacType: "terraform",
iacVersion: "v14",
configFile: "./testdata/does-not-exist",
},
wantErr: config.ErrNotPresent,
wantIacProvider: &tfv14.TfV14{},
},
{
name: "invalid policy path",
executor: Executor{
filePath: "./testdata/testfile",
dirPath: "",
cloudType: []string{"aws"},
iacType: "terraform",
iacVersion: "v14",
configFile: "./testdata/webhook.toml",
policyPath: []string{"./testdata/notthere"},
},
wantErr: fmt.Errorf("failed to initialize OPA policy engine"),
wantIacProvider: &tfv14.TfV14{},
wantNotifiers: []notifications.Notifier{&webhook.Webhook{}},
},
{
name: "config file with invalid category",
executor: Executor{
filePath: "./testdata/testfile",
dirPath: "",
cloudType: []string{"aws"},
iacType: "terraform",
iacVersion: "v14",
configFile: "./testdata/invalid-category.toml",
policyPath: []string{"./testdata/notthere"},
},
wantErr: fmt.Errorf("(3, 5): no value can start with c"),
wantIacProvider: &tfv14.TfV14{},
},
wantErr: policyopa.ErrInitFailed,
wantIacProvider: &tfv14.TfV14{},
},
}

for _, tt := range table {
if tt.name == "config not present" {
}
t.Run(tt.name, func(t *testing.T) {
config.LoadGlobalConfig(tt.executor.configFile)
gotErr := tt.executor.Init()
if !reflect.DeepEqual(gotErr, tt.wantErr) {
t.Errorf("unexpected error; gotErr: '%v', wantErr: '%v'", gotErr, tt.wantErr)
}
if !reflect.DeepEqual(tt.executor.iacProvider, tt.wantIacProvider) {
t.Errorf("got: '%v', want: '%v'", tt.executor.iacProvider, tt.wantIacProvider)
}
for i, notifier := range tt.executor.notifiers {
if !reflect.DeepEqual(reflect.TypeOf(notifier), reflect.TypeOf(tt.wantNotifiers[i])) {
t.Errorf("got: '%v', want: '%v'", reflect.TypeOf(notifier), reflect.TypeOf(tt.wantNotifiers[i]))
for _, tt := range table {
//if tt.name == "config not present" {
t.Run(tt.name, func(t *testing.T) {
configErr := config.LoadGlobalConfig(tt.executor.configFile)
if configErr != nil {
if !reflect.DeepEqual(configErr, tt.wantErr) {
t.Errorf("unexpected error; gotErr: '%v', wantErr: '%v'", configErr, tt.wantErr)
}
} else {
gotErr := tt.executor.Init()
if !reflect.DeepEqual(gotErr, tt.wantErr) {
t.Errorf("unexpected error; gotErr: '%v', wantErr: '%v'", gotErr, tt.wantErr)
}
if !reflect.DeepEqual(tt.executor.iacProvider, tt.wantIacProvider) {
t.Errorf("got: '%v', want: '%v'", tt.executor.iacProvider, tt.wantIacProvider)
}
for i, notifier := range tt.executor.notifiers {
if !reflect.DeepEqual(reflect.TypeOf(notifier), reflect.TypeOf(tt.wantNotifiers[i])) {
t.Errorf("got: '%v', want: '%v'", reflect.TypeOf(notifier), reflect.TypeOf(tt.wantNotifiers[i]))
}
}
}
}
})
}
}
})
//}
}*/

table = []struct {
table := []struct {
name string
executor Executor
wantErr error
Expand Down Expand Up @@ -351,7 +355,7 @@ func TestInit(t *testing.T) {
iacVersion: "v12",
configFile: "./testdata/does-not-exist",
},
wantErr: nil,
wantErr: config.ErrNotPresent,
wantIacProvider: &tfv12.TfV12{},
},
{
Expand All @@ -373,17 +377,23 @@ func TestInit(t *testing.T) {

for _, tt := range table {
t.Run(tt.name, func(t *testing.T) {
config.LoadGlobalConfig(tt.executor.configFile)
gotErr := tt.executor.Init()
if !reflect.DeepEqual(gotErr, tt.wantErr) {
t.Errorf("unexpected error; gotErr: '%v', wantErr: '%v'", gotErr, tt.wantErr)
}
if !reflect.DeepEqual(tt.executor.iacProvider, tt.wantIacProvider) {
t.Errorf("got: '%v', want: '%v'", tt.executor.iacProvider, tt.wantIacProvider)
}
for i, notifier := range tt.executor.notifiers {
if !reflect.DeepEqual(reflect.TypeOf(notifier), reflect.TypeOf(tt.wantNotifiers[i])) {
t.Errorf("got: '%v', want: '%v'", reflect.TypeOf(notifier), reflect.TypeOf(tt.wantNotifiers[i]))
configErr := config.LoadGlobalConfig(tt.executor.configFile)
if configErr != nil {
if !reflect.DeepEqual(configErr, tt.wantErr) {
t.Errorf("unexpected error; gotErr: '%v', wantErr: '%v'", configErr, tt.wantErr)
}
} else {
gotErr := tt.executor.Init()
if !reflect.DeepEqual(gotErr, tt.wantErr) {
t.Errorf("unexpected error; gotErr: '%v', wantErr: '%v'", gotErr, tt.wantErr)
}
if !reflect.DeepEqual(tt.executor.iacProvider, tt.wantIacProvider) {
t.Errorf("got: '%v', want: '%v'", tt.executor.iacProvider, tt.wantIacProvider)
}
for i, notifier := range tt.executor.notifiers {
if !reflect.DeepEqual(reflect.TypeOf(notifier), reflect.TypeOf(tt.wantNotifiers[i])) {
t.Errorf("got: '%v', want: '%v'", reflect.TypeOf(notifier), reflect.TypeOf(tt.wantNotifiers[i]))
}
}
}
})
Expand Down
5 changes: 5 additions & 0 deletions test/e2e/init/config/relative_path_config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[policy]
repo_url = "https://github.com/accurics/terrascan.git"
branch = "master"
path = ".terrascan-test"
rego_subdir = "pkg/policies/opa/rego"
Loading

0 comments on commit 51d8817

Please sign in to comment.