Skip to content

Commit 2a6c7ab

Browse files
authored
fix(misconf): support deprecating for Go checks (#7377)
Signed-off-by: nikpivkin <nikita.pivkin@smartforce.io>
1 parent b65b32d commit 2a6c7ab

File tree

6 files changed

+123
-41
lines changed

6 files changed

+123
-41
lines changed

pkg/iac/scanners/azure/arm/scanner.go

+22-16
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,24 @@ var _ scanners.FSScanner = (*Scanner)(nil)
2525
var _ options.ConfigurableScanner = (*Scanner)(nil)
2626

2727
type Scanner struct {
28-
mu sync.Mutex
29-
scannerOptions []options.ScannerOption
30-
logger *log.Logger
31-
frameworks []framework.Framework
32-
regoOnly bool
33-
loadEmbeddedPolicies bool
34-
loadEmbeddedLibraries bool
35-
policyDirs []string
36-
policyReaders []io.Reader
37-
regoScanner *rego.Scanner
38-
spec string
39-
}
40-
41-
func (s *Scanner) SetIncludeDeprecatedChecks(b bool) {}
28+
mu sync.Mutex
29+
scannerOptions []options.ScannerOption
30+
logger *log.Logger
31+
frameworks []framework.Framework
32+
regoOnly bool
33+
loadEmbeddedPolicies bool
34+
loadEmbeddedLibraries bool
35+
policyDirs []string
36+
policyReaders []io.Reader
37+
regoScanner *rego.Scanner
38+
spec string
39+
includeDeprecatedChecks bool
40+
}
41+
42+
func (s *Scanner) SetIncludeDeprecatedChecks(b bool) {
43+
s.includeDeprecatedChecks = b
44+
}
45+
4246
func (s *Scanner) SetCustomSchemas(map[string][]byte) {}
4347

4448
func (s *Scanner) SetSpec(spec string) {
@@ -150,9 +154,11 @@ func (s *Scanner) scanDeployment(ctx context.Context, deployment azure.Deploymen
150154
return nil, ctx.Err()
151155
default:
152156
}
153-
if rule.GetRule().RegoPackage != "" {
154-
continue
157+
158+
if !s.includeDeprecatedChecks && rule.Deprecated {
159+
continue // skip deprecated checks
155160
}
161+
156162
ruleResults := rule.Evaluate(deploymentState)
157163
if len(ruleResults) > 0 {
158164
results = append(results, ruleResults...)

pkg/iac/scanners/cloudformation/scanner.go

+22-16
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,26 @@ var _ scanners.FSScanner = (*Scanner)(nil)
4848
var _ options.ConfigurableScanner = (*Scanner)(nil)
4949

5050
type Scanner struct {
51-
mu sync.Mutex
52-
logger *log.Logger
53-
policyDirs []string
54-
policyReaders []io.Reader
55-
parser *parser.Parser
56-
regoScanner *rego.Scanner
57-
regoOnly bool
58-
loadEmbeddedPolicies bool
59-
loadEmbeddedLibraries bool
60-
options []options.ScannerOption
61-
parserOptions []parser.Option
62-
frameworks []framework.Framework
63-
spec string
51+
mu sync.Mutex
52+
logger *log.Logger
53+
policyDirs []string
54+
policyReaders []io.Reader
55+
parser *parser.Parser
56+
regoScanner *rego.Scanner
57+
regoOnly bool
58+
loadEmbeddedPolicies bool
59+
loadEmbeddedLibraries bool
60+
options []options.ScannerOption
61+
parserOptions []parser.Option
62+
frameworks []framework.Framework
63+
spec string
64+
includeDeprecatedChecks bool
65+
}
66+
67+
func (s *Scanner) SetIncludeDeprecatedChecks(bool) {
68+
s.includeDeprecatedChecks = true
6469
}
6570

66-
func (s *Scanner) SetIncludeDeprecatedChecks(bool) {}
6771
func (s *Scanner) SetCustomSchemas(map[string][]byte) {}
6872

6973
func (s *Scanner) addParserOption(opt parser.Option) {
@@ -211,9 +215,11 @@ func (s *Scanner) scanFileContext(ctx context.Context, regoScanner *rego.Scanner
211215
return nil, ctx.Err()
212216
default:
213217
}
214-
if rule.GetRule().RegoPackage != "" {
215-
continue
218+
219+
if !s.includeDeprecatedChecks && rule.Deprecated {
220+
continue // skip deprecated checks
216221
}
222+
217223
evalResult := rule.Evaluate(state)
218224
if len(evalResult) > 0 {
219225
for _, scanResult := range evalResult {

pkg/iac/scanners/terraform/executor/executor.go

+17-8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"runtime"
66
"sort"
77

8+
"github.com/samber/lo"
89
"github.com/zclconf/go-cty/cty"
910

1011
adapter "github.com/aquasecurity/trivy/pkg/iac/adapters/terraform"
@@ -15,17 +16,19 @@ import (
1516
"github.com/aquasecurity/trivy/pkg/iac/scan"
1617
"github.com/aquasecurity/trivy/pkg/iac/terraform"
1718
"github.com/aquasecurity/trivy/pkg/iac/types"
19+
ruleTypes "github.com/aquasecurity/trivy/pkg/iac/types/rules"
1820
"github.com/aquasecurity/trivy/pkg/log"
1921
)
2022

2123
// Executor scans HCL blocks by running all registered rules against them
2224
type Executor struct {
23-
workspaceName string
24-
logger *log.Logger
25-
resultsFilters []func(scan.Results) scan.Results
26-
regoScanner *rego.Scanner
27-
regoOnly bool
28-
frameworks []framework.Framework
25+
workspaceName string
26+
logger *log.Logger
27+
resultsFilters []func(scan.Results) scan.Results
28+
regoScanner *rego.Scanner
29+
regoOnly bool
30+
includeDeprecatedChecks bool
31+
frameworks []framework.Framework
2932
}
3033

3134
// New creates a new Executor
@@ -53,8 +56,14 @@ func (e *Executor) Execute(modules terraform.Modules) (scan.Results, error) {
5356

5457
e.logger.Debug("Using max routines", log.Int("count", threads))
5558

56-
registeredRules := rules.GetRegistered(e.frameworks...)
57-
e.logger.Debug("Initialized rule(s).", log.Int("count", len(registeredRules)))
59+
registeredRules := lo.Filter(rules.GetRegistered(e.frameworks...), func(r ruleTypes.RegisteredRule, _ int) bool {
60+
if !e.includeDeprecatedChecks && r.Deprecated {
61+
return false // skip deprecated checks
62+
}
63+
64+
return true
65+
})
66+
e.logger.Debug("Initialized Go check(s).", log.Int("count", len(registeredRules)))
5867

5968
pool := NewPool(threads, registeredRules, modules, infra, e.regoScanner, e.regoOnly)
6069

pkg/iac/scanners/terraform/executor/option.go

+6
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,9 @@ func OptionWithRegoOnly(regoOnly bool) Option {
3737
e.regoOnly = regoOnly
3838
}
3939
}
40+
41+
func OptionWithIncludeDeprecatedChecks(b bool) Option {
42+
return func(e *Executor) {
43+
e.includeDeprecatedChecks = b
44+
}
45+
}

pkg/iac/scanners/terraform/scanner.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ type Scanner struct {
4646
loadEmbeddedPolicies bool
4747
}
4848

49-
func (s *Scanner) SetIncludeDeprecatedChecks(b bool) {}
49+
func (s *Scanner) SetIncludeDeprecatedChecks(b bool) {
50+
s.executorOpt = append(s.executorOpt, executor.OptionWithIncludeDeprecatedChecks(b))
51+
}
52+
5053
func (s *Scanner) SetCustomSchemas(map[string][]byte) {}
5154

5255
func (s *Scanner) SetSpec(spec string) {

pkg/iac/scanners/terraform/scanner_test.go

+52
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,13 @@ import (
1010
"github.com/stretchr/testify/require"
1111

1212
"github.com/aquasecurity/trivy/internal/testutil"
13+
"github.com/aquasecurity/trivy/pkg/iac/providers"
14+
"github.com/aquasecurity/trivy/pkg/iac/rules"
1315
"github.com/aquasecurity/trivy/pkg/iac/scan"
1416
"github.com/aquasecurity/trivy/pkg/iac/scanners/options"
17+
"github.com/aquasecurity/trivy/pkg/iac/severity"
18+
"github.com/aquasecurity/trivy/pkg/iac/state"
19+
"github.com/aquasecurity/trivy/pkg/iac/types"
1520
)
1621

1722
const emptyBucketRule = `
@@ -1065,3 +1070,50 @@ deny[res] {
10651070
occurrences := failed[0].Occurrences()
10661071
assert.Equal(t, "code/example/main.tf", occurrences[0].Filename)
10671072
}
1073+
1074+
func TestSkipDeprecatedGoChecks(t *testing.T) {
1075+
1076+
check := scan.Rule{
1077+
Provider: providers.AWSProvider,
1078+
Service: "service",
1079+
ShortCode: "abc",
1080+
Severity: severity.High,
1081+
Check: func(s *state.State) (results scan.Results) {
1082+
results.Add("Deny", types.NewTestMetadata())
1083+
return
1084+
},
1085+
}
1086+
1087+
fsys := testutil.CreateFS(t, map[string]string{
1088+
"main.tf": `resource "foo" "bar" {}`,
1089+
})
1090+
1091+
scanner := New(
1092+
options.ScannerWithPolicyFilesystem(fsys),
1093+
options.ScannerWithEmbeddedLibraries(false),
1094+
options.ScannerWithEmbeddedPolicies(false),
1095+
ScannerWithAllDirectories(true),
1096+
)
1097+
1098+
t.Run("deprecated", func(t *testing.T) {
1099+
check.Deprecated = true
1100+
reg := rules.Register(check)
1101+
defer rules.Deregister(reg)
1102+
1103+
results, err := scanner.ScanFS(context.TODO(), fsys, ".")
1104+
require.NoError(t, err)
1105+
1106+
require.Empty(t, results)
1107+
})
1108+
1109+
t.Run("not deprecated", func(t *testing.T) {
1110+
check.Deprecated = false
1111+
reg := rules.Register(check)
1112+
defer rules.Deregister(reg)
1113+
1114+
results, err := scanner.ScanFS(context.TODO(), fsys, ".")
1115+
require.NoError(t, err)
1116+
1117+
require.Len(t, results, 1)
1118+
})
1119+
}

0 commit comments

Comments
 (0)