Skip to content

Commit

Permalink
fix(secret): update built-in rule tests (aquasecurity#3855)
Browse files Browse the repository at this point in the history
Co-authored-by: Teppei Fukuda <[email protected]>
  • Loading branch information
afdesk and knqyf263 authored Apr 3, 2023
1 parent 5ab6d25 commit d113b93
Show file tree
Hide file tree
Showing 22 changed files with 64 additions and 19 deletions.
6 changes: 5 additions & 1 deletion pkg/fanal/analyzer/secret/secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,11 @@ func TestSecretRequire(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
a := secret.SecretAnalyzer{}
err := a.Init(analyzer.AnalyzerOptions{})
err := a.Init(analyzer.AnalyzerOptions{
SecretScannerOption: analyzer.SecretScannerOption{
ConfigPath: "testdata/skip-tests-config.yaml",
},
})
require.NoError(t, err)

fi, err := os.Stat(tt.filePath)
Expand Down
3 changes: 2 additions & 1 deletion pkg/fanal/analyzer/secret/testdata/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ rules:
regex: (?i)(?P<key>(secret))(=|:).{0,5}['"](?P<secret>[0-9a-zA-Z\-_=]{8,64})['"]
secret-group-name: secret


disable-allow-rules:
- tests
2 changes: 2 additions & 0 deletions pkg/fanal/analyzer/secret/testdata/skip-tests-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
disable-allow-rules:
- tests
2 changes: 1 addition & 1 deletion pkg/fanal/secret/builtin-allow-rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var builtinAllowRules = []AllowRule{
{
ID: "tests",
Description: "Avoid test files and paths",
Path: MustCompile(`(\/test|-test|_test|\.test)`),
Path: MustCompile(`(^test|\/test|-test|_test|\.test)`),
},
{
ID: "examples",
Expand Down
3 changes: 3 additions & 0 deletions pkg/fanal/secret/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ type Match struct {
func (s *Scanner) Scan(args ScanArgs) types.Secret {
// Global allowed paths
if s.AllowPath(args.FilePath) {
log.Logger.Debugf("Skipped secret scanning on %q matching allowed paths", args.FilePath)
return types.Secret{
FilePath: args.FilePath,
}
Expand All @@ -355,11 +356,13 @@ func (s *Scanner) Scan(args ScanArgs) types.Secret {
for _, rule := range s.Rules {
// Check if the file path should be scanned by this rule
if !rule.MatchPath(args.FilePath) {
log.Logger.Debugf("Skipped secret scanning on %q as non-compliant to the rule %q", args.FilePath, rule.ID)
continue
}

// Check if the file path should be allowed
if rule.AllowPath(args.FilePath) {
log.Logger.Debugf("Skipped secret scanning on %q as allowed", args.FilePath)
continue
}

Expand Down
10 changes: 8 additions & 2 deletions pkg/fanal/secret/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,7 @@ func TestSecretScanner(t *testing.T) {
},
{
name: "find Asymmetric Private Key secrets",
configPath: filepath.Join("testdata", "skip-test.yaml"),
inputFilePath: filepath.Join("testdata", "asymmetric-private-secret.txt"),
want: types.Secret{
FilePath: filepath.Join("testdata", "asymmetric-private-secret.txt"),
Expand All @@ -562,6 +563,7 @@ func TestSecretScanner(t *testing.T) {
},
{
name: "find Alibaba AccessKey ID txt",
configPath: filepath.Join("testdata", "skip-test.yaml"),
inputFilePath: "testdata/alibaba-access-key-id.txt",
want: types.Secret{
FilePath: "testdata/alibaba-access-key-id.txt",
Expand All @@ -570,6 +572,7 @@ func TestSecretScanner(t *testing.T) {
},
{
name: "find Asymmetric Private Key secrets json",
configPath: filepath.Join("testdata", "skip-test.yaml"),
inputFilePath: filepath.Join("testdata", "asymmetric-private-secret.json"),
want: types.Secret{
FilePath: filepath.Join("testdata", "asymmetric-private-secret.json"),
Expand Down Expand Up @@ -610,7 +613,7 @@ func TestSecretScanner(t *testing.T) {
},
{
name: "should find ghp builtin secret",
configPath: "",
configPath: filepath.Join("testdata", "skip-test.yaml"),
inputFilePath: filepath.Join("testdata", "builtin-rule-secret.txt"),
want: types.Secret{
FilePath: filepath.Join("testdata", "builtin-rule-secret.txt"),
Expand Down Expand Up @@ -673,6 +676,7 @@ func TestSecretScanner(t *testing.T) {
},
{
name: "skip examples file",
configPath: filepath.Join("testdata", "skip-test.yaml"),
inputFilePath: filepath.Join("testdata", "example-secret.txt"),
want: types.Secret{
FilePath: filepath.Join("testdata", "example-secret.txt"),
Expand Down Expand Up @@ -716,6 +720,7 @@ func TestSecretScanner(t *testing.T) {
},
{
name: "truncate long line",
configPath: filepath.Join("testdata", "skip-test.yaml"),
inputFilePath: filepath.Join("testdata", "long-line-secret.txt"),
want: types.Secret{
FilePath: filepath.Join("testdata", "long-line-secret.txt"),
Expand All @@ -733,12 +738,13 @@ func TestSecretScanner(t *testing.T) {
},
{
name: "invalid aws secrets",
configPath: filepath.Join("testdata", "skip-test.yaml"),
inputFilePath: filepath.Join("testdata", "invalid-aws-secrets.txt"),
want: types.Secret{},
},
{
name: "asymmetric file",
configPath: "",
configPath: filepath.Join("testdata", "skip-test.yaml"),
inputFilePath: "testdata/asymmetric-private-key.txt",
want: types.Secret{
FilePath: "testdata/asymmetric-private-key.txt",
Expand Down
4 changes: 3 additions & 1 deletion pkg/fanal/secret/testdata/allow-path.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ rules:
secret-group-name: secret
allow-rules:
- description: skip text files
path: .*\.txt
path: .*\.txt
disable-allow-rules:
- tests
4 changes: 3 additions & 1 deletion pkg/fanal/secret/testdata/allow-regex-outside-group.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ rules:
secret-group-name: secret
allow-rules:
- description: skip line
regex: line
regex: line
disable-allow-rules:
- tests
4 changes: 3 additions & 1 deletion pkg/fanal/secret/testdata/allow-regex.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ rules:
secret-group-name: secret
allow-rules:
- description: skip other
regex: other
regex: other
disable-allow-rules:
- tests
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ rules:
secret-group-name: secret
disable-allow-rules:
- markdown
- tests


5 changes: 4 additions & 1 deletion pkg/fanal/secret/testdata/config-disable-ghp.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
disable-rules:
- github-pat
- github-pat

disable-allow-rules:
- tests
5 changes: 4 additions & 1 deletion pkg/fanal/secret/testdata/config-disable-rule1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ rules:
regex: (?i)(?P<key>(secret))(=|:).{0,5}['"](?P<secret>[0-9a-zA-Z\-_=]{8,64})['"]
secret-group-name: secret
disable-rules:
- rule1
- rule1

disable-allow-rules:
- tests
5 changes: 4 additions & 1 deletion pkg/fanal/secret/testdata/config-enable-ghp.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
enable-builtin-rules:
- github-pat
- github-pat

disable-allow-rules:
- tests
3 changes: 2 additions & 1 deletion pkg/fanal/secret/testdata/config-happy-keywords.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ rules:
secret-group-name: secret
keywords: [secret]


disable-allow-rules:
- tests
3 changes: 2 additions & 1 deletion pkg/fanal/secret/testdata/config-sad-keywords.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ rules:
secret-group-name: secret
keywords: [bla]


disable-allow-rules:
- tests
2 changes: 2 additions & 0 deletions pkg/fanal/secret/testdata/config-without-severity.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ rules:
title: Generic Rule
regex: (?i)(?P<key>(secret))(=|:).{0,5}['"](?P<secret>somevalue)['"]
secret-group-name: secret
disable-allow-rules:
- tests
3 changes: 2 additions & 1 deletion pkg/fanal/secret/testdata/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ rules:
regex: (?i)(?P<key>(secret))(=|:).{0,5}['"](?P<secret>[0-9a-zA-Z\-_=]{8,64})['"]
secret-group-name: secret


disable-allow-rules:
- tests
4 changes: 3 additions & 1 deletion pkg/fanal/secret/testdata/exclude-block.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ rules:
exclude-block:
description: exclude blocks
regexes:
- --- ignore block start ---(.|\s)*--- ignore block stop ---
- --- ignore block start ---(.|\s)*--- ignore block stop ---
disable-allow-rules:
- tests
4 changes: 3 additions & 1 deletion pkg/fanal/secret/testdata/global-allow-regex.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ rules:
secret-group-name: secret
allow-rules:
- description: skip other
regex: other
regex: other
disable-allow-rules:
- tests
4 changes: 3 additions & 1 deletion pkg/fanal/secret/testdata/global-exclude-block.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ rules:
exclude-block:
description: exclude blocks
regexes:
- --- ignore block start ---(.|\s)*--- ignore block stop ---
- --- ignore block start ---(.|\s)*--- ignore block stop ---
disable-allow-rules:
- tests
4 changes: 2 additions & 2 deletions pkg/fanal/secret/testdata/multiple-secret-groups.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ rules:
severity: HIGH
regex: (?i)credentials:\s*\{\s*user:\s*['"](?P<secret>[0-9a-zA-Z\-_=]{8,64})['"]\s*password:\s*['"](?P<secret>[0-9a-zA-Z\-_=]{8,64})['"]\s*\}
secret-group-name: secret


disable-allow-rules:
- tests
2 changes: 2 additions & 0 deletions pkg/fanal/secret/testdata/skip-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
disable-allow-rules:
- tests

0 comments on commit d113b93

Please sign in to comment.