-
Notifications
You must be signed in to change notification settings - Fork 509
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #441 from patilpankaj212/scan-and-skip-rules
implement scan and skip rules
- Loading branch information
Showing
37 changed files
with
1,023 additions
and
248 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[rules] | ||
scan-rules = [ | ||
"AWS.ECR.DataSecurity.High.0579" | ||
] | ||
skip-rules = [ | ||
"AWS.SecurityGroup.NetworkPortsSecurity.Low.0561" | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
Copyright (C) 2020 Accurics, Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package config | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
|
||
"github.com/pelletier/go-toml" | ||
"go.uber.org/zap" | ||
) | ||
|
||
var ( | ||
// ErrTomlLoadConfig indicates error: 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") | ||
) | ||
|
||
// TerrascanConfigReader holds the terrascan config file name | ||
type TerrascanConfigReader struct { | ||
config TerrascanConfig | ||
} | ||
|
||
// NewTerrascanConfigReader initialises and returns a config reader | ||
func NewTerrascanConfigReader(fileName string) (*TerrascanConfigReader, error) { | ||
config := TerrascanConfig{} | ||
configReader := new(TerrascanConfigReader) | ||
configReader.config = config | ||
|
||
// empty file name check should be done by the caller, this is a safe check | ||
if fileName == "" { | ||
zap.S().Debug("no config file specified") | ||
return configReader, nil | ||
} | ||
|
||
// return error if file doesn't exist | ||
_, err := os.Stat(fileName) | ||
if err != nil { | ||
zap.S().Error("config file: %s, doesn't exist", fileName) | ||
return configReader, ErrNotPresent | ||
} | ||
|
||
data, err := ioutil.ReadFile(fileName) | ||
if err != nil { | ||
zap.S().Error("error loading config file", zap.Error(err)) | ||
return configReader, errTomlLoadConfig | ||
} | ||
|
||
if err = toml.Unmarshal(data, &configReader.config); err != nil { | ||
return configReader, err | ||
} | ||
return configReader, nil | ||
} | ||
|
||
// GetPolicyConfig will return the policy config from the terrascan config file | ||
func (r TerrascanConfigReader) GetPolicyConfig() Policy { | ||
return r.config.Policy | ||
} | ||
|
||
// GetNotifications will return the notifiers specified in the terrascan config file | ||
func (r TerrascanConfigReader) GetNotifications() map[string]Notifier { | ||
return r.config.Notifications | ||
} | ||
|
||
// GetRules will return the rules specified in the terrascan config file | ||
func (r TerrascanConfigReader) GetRules() Rules { | ||
return r.config.Rules | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/* | ||
Copyright (C) 2020 Accurics, Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package config | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestNewTerrascanConfigReader(t *testing.T) { | ||
testNotifier := Notifier{ | ||
NotifierType: "webhook", | ||
NotifierConfig: map[string]interface{}{ | ||
"url": "testurl1", | ||
}, | ||
} | ||
testPolicy := Policy{ | ||
BasePath: "custom-path", | ||
RepoPath: "rego-subdir", | ||
RepoURL: "https://repository/url", | ||
Branch: "branch-name", | ||
} | ||
testRules := Rules{ | ||
ScanRules: []string{"rule.1", "rule.2", "rule.3", "rule.4", "rule.5"}, | ||
SkipRules: []string{"rule.1"}, | ||
} | ||
|
||
type args struct { | ||
fileName string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want *TerrascanConfigReader | ||
wantErr bool | ||
assertGetters bool | ||
Policy | ||
notifications map[string]Notifier | ||
Rules | ||
}{ | ||
{ | ||
name: "empty config file", | ||
args: args{ | ||
fileName: "", | ||
}, | ||
want: &TerrascanConfigReader{}, | ||
}, | ||
{ | ||
name: "non existent config file", | ||
args: args{ | ||
fileName: "test", | ||
}, | ||
wantErr: true, | ||
want: &TerrascanConfigReader{}, | ||
}, | ||
{ | ||
name: "invalid toml config file", | ||
args: args{ | ||
fileName: "testdata/invalid.toml", | ||
}, | ||
wantErr: true, | ||
want: &TerrascanConfigReader{}, | ||
}, | ||
{ | ||
name: "valid toml config file with partial fields", | ||
args: args{ | ||
fileName: "testdata/terrascan-config.toml", | ||
}, | ||
want: &TerrascanConfigReader{ | ||
config: TerrascanConfig{ | ||
Policy: testPolicy, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "valid toml config file with all fields", | ||
args: args{ | ||
fileName: "testdata/terrascan-config-all-fields.toml", | ||
}, | ||
want: &TerrascanConfigReader{ | ||
config: TerrascanConfig{ | ||
Policy: testPolicy, | ||
Notifications: map[string]Notifier{ | ||
"webhook1": testNotifier, | ||
}, | ||
Rules: testRules, | ||
}, | ||
}, | ||
assertGetters: true, | ||
notifications: map[string]Notifier{ | ||
"webhook1": testNotifier, | ||
}, | ||
Policy: testPolicy, | ||
Rules: testRules, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := NewTerrascanConfigReader(tt.args.fileName) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("NewTerrascanConfigReader() got error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("NewTerrascanConfigReader() = got %v, want %v", got, tt.want) | ||
} | ||
if tt.assertGetters { | ||
if !reflect.DeepEqual(got.GetPolicyConfig(), tt.Policy) || !reflect.DeepEqual(got.GetNotifications(), tt.notifications) || !reflect.DeepEqual(got.GetRules(), tt.Rules) { | ||
t.Errorf("NewTerrascanConfigReader() = got config: %v, notifications: %v, rules: %v want config: %v, notifications: %v, rules: %v", got.GetPolicyConfig(), got.GetNotifications(), got.GetRules(), tt.Policy, tt.notifications, tt.Rules) | ||
} | ||
} | ||
}) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.