Skip to content

Commit 6bca7c3

Browse files
authored
refactor(terraform): remove unused options (#6446)
1 parent 8e4279b commit 6bca7c3

File tree

6 files changed

+56
-447
lines changed

6 files changed

+56
-447
lines changed

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

Lines changed: 23 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -14,52 +14,31 @@ import (
1414
"github.com/aquasecurity/trivy/pkg/iac/rego"
1515
"github.com/aquasecurity/trivy/pkg/iac/rules"
1616
"github.com/aquasecurity/trivy/pkg/iac/scan"
17-
"github.com/aquasecurity/trivy/pkg/iac/severity"
18-
"github.com/aquasecurity/trivy/pkg/iac/state"
1917
"github.com/aquasecurity/trivy/pkg/iac/terraform"
2018
"github.com/aquasecurity/trivy/pkg/iac/types"
2119
)
2220

2321
// Executor scans HCL blocks by running all registered rules against them
2422
type Executor struct {
25-
enableIgnores bool
26-
excludedRuleIDs []string
27-
includedRuleIDs []string
28-
ignoreCheckErrors bool
29-
workspaceName string
30-
useSingleThread bool
31-
debug debug.Logger
32-
resultsFilters []func(scan.Results) scan.Results
33-
severityOverrides map[string]string
34-
regoScanner *rego.Scanner
35-
regoOnly bool
36-
stateFuncs []func(*state.State)
37-
frameworks []framework.Framework
23+
workspaceName string
24+
debug debug.Logger
25+
resultsFilters []func(scan.Results) scan.Results
26+
regoScanner *rego.Scanner
27+
regoOnly bool
28+
frameworks []framework.Framework
3829
}
3930

4031
// New creates a new Executor
4132
func New(options ...Option) *Executor {
4233
s := &Executor{
43-
ignoreCheckErrors: true,
44-
enableIgnores: true,
45-
regoOnly: false,
34+
regoOnly: false,
4635
}
4736
for _, option := range options {
4837
option(s)
4938
}
5039
return s
5140
}
5241

53-
// Find element in list
54-
func checkInList(id string, list []string) bool {
55-
for _, codeIgnored := range list {
56-
if codeIgnored == id {
57-
return true
58-
}
59-
}
60-
return false
61-
}
62-
6342
func (e *Executor) Execute(modules terraform.Modules) (scan.Results, error) {
6443

6544
e.debug.Log("Adapting modules...")
@@ -70,90 +49,46 @@ func (e *Executor) Execute(modules terraform.Modules) (scan.Results, error) {
7049
if threads > 1 {
7150
threads--
7251
}
73-
if e.useSingleThread {
74-
threads = 1
75-
}
76-
e.debug.Log("Using max routines of %d", threads)
7752

78-
e.debug.Log("Applying state modifier functions...")
79-
for _, f := range e.stateFuncs {
80-
f(infra)
81-
}
53+
e.debug.Log("Using max routines of %d", threads)
8254

8355
registeredRules := rules.GetRegistered(e.frameworks...)
8456
e.debug.Log("Initialized %d rule(s).", len(registeredRules))
8557

86-
pool := NewPool(threads, registeredRules, modules, infra, e.ignoreCheckErrors, e.regoScanner, e.regoOnly)
58+
pool := NewPool(threads, registeredRules, modules, infra, e.regoScanner, e.regoOnly)
8759
e.debug.Log("Created pool with %d worker(s) to apply rules.", threads)
60+
8861
results, err := pool.Run()
8962
if err != nil {
9063
return nil, err
9164
}
92-
e.debug.Log("Finished applying rules.")
9365

94-
if e.enableIgnores {
95-
e.debug.Log("Applying ignores...")
96-
var ignores ignore.Rules
97-
for _, module := range modules {
98-
ignores = append(ignores, module.Ignores()...)
99-
}
66+
e.debug.Log("Finished applying rules.")
10067

101-
ignorers := map[string]ignore.Ignorer{
102-
"ws": workspaceIgnorer(e.workspaceName),
103-
"ignore": attributeIgnorer(modules),
104-
}
68+
e.debug.Log("Applying ignores...")
69+
var ignores ignore.Rules
70+
for _, module := range modules {
71+
ignores = append(ignores, module.Ignores()...)
72+
}
10573

106-
results.Ignore(ignores, ignorers)
74+
ignorers := map[string]ignore.Ignorer{
75+
"ws": workspaceIgnorer(e.workspaceName),
76+
"ignore": attributeIgnorer(modules),
77+
}
10778

108-
for _, ignored := range results.GetIgnored() {
109-
e.debug.Log("Ignored '%s' at '%s'.", ignored.Rule().LongID(), ignored.Range())
110-
}
79+
results.Ignore(ignores, ignorers)
11180

112-
} else {
113-
e.debug.Log("Ignores are disabled.")
81+
for _, ignored := range results.GetIgnored() {
82+
e.debug.Log("Ignored '%s' at '%s'.", ignored.Rule().LongID(), ignored.Range())
11483
}
11584

116-
results = e.updateSeverity(results)
11785
results = e.filterResults(results)
11886

11987
e.sortResults(results)
12088
return results, nil
12189
}
12290

123-
func (e *Executor) updateSeverity(results []scan.Result) scan.Results {
124-
if len(e.severityOverrides) == 0 {
125-
return results
126-
}
127-
128-
var overriddenResults scan.Results
129-
for _, res := range results {
130-
for code, sev := range e.severityOverrides {
131-
if res.Rule().LongID() != code {
132-
continue
133-
}
134-
135-
overrides := scan.Results([]scan.Result{res})
136-
override := res.Rule()
137-
override.Severity = severity.Severity(sev)
138-
overrides.SetRule(override)
139-
res = overrides[0]
140-
}
141-
overriddenResults = append(overriddenResults, res)
142-
}
143-
144-
return overriddenResults
145-
}
146-
14791
func (e *Executor) filterResults(results scan.Results) scan.Results {
148-
includedOnly := len(e.includedRuleIDs) > 0
149-
for i, result := range results {
150-
id := result.Rule().LongID()
151-
if (includedOnly && !checkInList(id, e.includedRuleIDs)) || checkInList(id, e.excludedRuleIDs) {
152-
e.debug.Log("Excluding '%s' at '%s'.", result.Rule().LongID(), result.Range())
153-
results[i].OverrideStatus(scan.StatusIgnored)
154-
}
155-
}
156-
15792
if len(e.resultsFilters) > 0 && len(results) > 0 {
15893
before := len(results.GetIgnored())
15994
e.debug.Log("Applying %d results filters to %d results...", len(results), before)

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,7 @@ resource "problem" "this" {
7878

7979
modules, _, err := p.EvaluateAll(context.TODO())
8080
require.NoError(t, err)
81-
82-
_, err = New(OptionStopOnErrors(false)).Execute(modules)
81+
_, err = New().Execute(modules)
8382
assert.Error(t, err)
8483
}
8584

@@ -127,6 +126,6 @@ resource "problem" "this" {
127126
modules, _, err := p.EvaluateAll(context.TODO())
128127
require.NoError(t, err)
129128

130-
_, err = New(OptionStopOnErrors(false)).Execute(modules)
129+
_, err = New().Execute(modules)
131130
assert.Error(t, err)
132131
}

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

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"github.com/aquasecurity/trivy/pkg/iac/framework"
88
"github.com/aquasecurity/trivy/pkg/iac/rego"
99
"github.com/aquasecurity/trivy/pkg/iac/scan"
10-
"github.com/aquasecurity/trivy/pkg/iac/state"
1110
)
1211

1312
type Option func(s *Executor)
@@ -24,66 +23,24 @@ func OptionWithResultsFilter(f func(scan.Results) scan.Results) Option {
2423
}
2524
}
2625

27-
func OptionWithSeverityOverrides(overrides map[string]string) Option {
28-
return func(s *Executor) {
29-
s.severityOverrides = overrides
30-
}
31-
}
32-
3326
func OptionWithDebugWriter(w io.Writer) Option {
3427
return func(s *Executor) {
3528
s.debug = debug.New(w, "terraform", "executor")
3629
}
3730
}
3831

39-
func OptionNoIgnores() Option {
40-
return func(s *Executor) {
41-
s.enableIgnores = false
42-
}
43-
}
44-
45-
func OptionExcludeRules(ruleIDs []string) Option {
46-
return func(s *Executor) {
47-
s.excludedRuleIDs = ruleIDs
48-
}
49-
}
50-
51-
func OptionIncludeRules(ruleIDs []string) Option {
52-
return func(s *Executor) {
53-
s.includedRuleIDs = ruleIDs
54-
}
55-
}
56-
57-
func OptionStopOnErrors(stop bool) Option {
58-
return func(s *Executor) {
59-
s.ignoreCheckErrors = !stop
60-
}
61-
}
62-
6332
func OptionWithWorkspaceName(workspaceName string) Option {
6433
return func(s *Executor) {
6534
s.workspaceName = workspaceName
6635
}
6736
}
6837

69-
func OptionWithSingleThread(single bool) Option {
70-
return func(s *Executor) {
71-
s.useSingleThread = single
72-
}
73-
}
74-
7538
func OptionWithRegoScanner(s *rego.Scanner) Option {
7639
return func(e *Executor) {
7740
e.regoScanner = s
7841
}
7942
}
8043

81-
func OptionWithStateFunc(f ...func(*state.State)) Option {
82-
return func(e *Executor) {
83-
e.stateFuncs = f
84-
}
85-
}
86-
8744
func OptionWithRegoOnly(regoOnly bool) Option {
8845
return func(e *Executor) {
8946
e.regoOnly = regoOnly

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

Lines changed: 30 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,22 @@ import (
1717
)
1818

1919
type Pool struct {
20-
size int
21-
modules terraform.Modules
22-
state *state.State
23-
rules []types.RegisteredRule
24-
ignoreErrors bool
25-
rs *rego.Scanner
26-
regoOnly bool
20+
size int
21+
modules terraform.Modules
22+
state *state.State
23+
rules []types.RegisteredRule
24+
rs *rego.Scanner
25+
regoOnly bool
2726
}
2827

29-
func NewPool(size int, rules []types.RegisteredRule, modules terraform.Modules, st *state.State, ignoreErrors bool, regoScanner *rego.Scanner, regoOnly bool) *Pool {
28+
func NewPool(size int, rules []types.RegisteredRule, modules terraform.Modules, st *state.State, regoScanner *rego.Scanner, regoOnly bool) *Pool {
3029
return &Pool{
31-
size: size,
32-
rules: rules,
33-
state: st,
34-
modules: modules,
35-
ignoreErrors: ignoreErrors,
36-
rs: regoScanner,
37-
regoOnly: regoOnly,
30+
size: size,
31+
rules: rules,
32+
state: st,
33+
modules: modules,
34+
rs: regoScanner,
35+
regoOnly: regoOnly,
3836
}
3937
}
4038

@@ -69,17 +67,15 @@ func (p *Pool) Run() (scan.Results, error) {
6967
for _, module := range p.modules {
7068
mod := *module
7169
outgoing <- &hclModuleRuleJob{
72-
module: &mod,
73-
rule: r,
74-
ignoreErrors: p.ignoreErrors,
70+
module: &mod,
71+
rule: r,
7572
}
7673
}
7774
} else {
7875
// run defsec rule
7976
outgoing <- &infraRuleJob{
80-
state: p.state,
81-
rule: r,
82-
ignoreErrors: p.ignoreErrors,
77+
state: p.state,
78+
rule: r,
8379
}
8480
}
8581
}
@@ -105,14 +101,11 @@ type Job interface {
105101
type infraRuleJob struct {
106102
state *state.State
107103
rule types.RegisteredRule
108-
109-
ignoreErrors bool
110104
}
111105

112106
type hclModuleRuleJob struct {
113-
module *terraform.Module
114-
rule types.RegisteredRule
115-
ignoreErrors bool
107+
module *terraform.Module
108+
rule types.RegisteredRule
116109
}
117110

118111
type regoJob struct {
@@ -122,24 +115,21 @@ type regoJob struct {
122115
}
123116

124117
func (h *infraRuleJob) Run() (_ scan.Results, err error) {
125-
if h.ignoreErrors {
126-
defer func() {
127-
if panicErr := recover(); panicErr != nil {
128-
err = fmt.Errorf("%s\n%s", panicErr, string(runtimeDebug.Stack()))
129-
}
130-
}()
131-
}
118+
defer func() {
119+
if panicErr := recover(); panicErr != nil {
120+
err = fmt.Errorf("%s\n%s", panicErr, string(runtimeDebug.Stack()))
121+
}
122+
}()
123+
132124
return h.rule.Evaluate(h.state), err
133125
}
134126

135127
func (h *hclModuleRuleJob) Run() (results scan.Results, err error) {
136-
if h.ignoreErrors {
137-
defer func() {
138-
if panicErr := recover(); panicErr != nil {
139-
err = fmt.Errorf("%s\n%s", panicErr, string(runtimeDebug.Stack()))
140-
}
141-
}()
142-
}
128+
defer func() {
129+
if panicErr := recover(); panicErr != nil {
130+
err = fmt.Errorf("%s\n%s", panicErr, string(runtimeDebug.Stack()))
131+
}
132+
}()
143133
customCheck := h.rule.GetRule().CustomChecks.Terraform
144134
for _, block := range h.module.GetBlocks() {
145135
if !isCustomCheckRequiredForBlock(customCheck, block) {

0 commit comments

Comments
 (0)