Skip to content

Commit dc0d9ab

Browse files
fix: move ScanConfig to public package (#370)
- Moved ScanConfig from internal/resources to pkg/scanner (public API) - Replaced nested EngineConfig.ScanConfig.WithValidation with direct EngineConfig.WithValidation field
1 parent 68f0215 commit dc0d9ab

9 files changed

Lines changed: 72 additions & 85 deletions

File tree

cmd/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func processFlags(rootCmd *cobra.Command) error {
3737
}
3838

3939
// Apply all flag mappings immediately
40-
engineConfigVar.ScanConfig.WithValidation = validateVar
40+
engineConfigVar.WithValidation = validateVar
4141
if len(customRegexRuleVar) > 0 {
4242
engineConfigVar.CustomRegexPatterns = customRegexRuleVar
4343
}

cmd/config_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func TestProcessFlags(t *testing.T) {
126126
t.Run("ValidateVarMapping", func(t *testing.T) {
127127
// Reset global variables
128128
validateVar = false
129-
engineConfigVar.ScanConfig.WithValidation = false
129+
engineConfigVar.WithValidation = false
130130

131131
// Set test value
132132
validateVar = true
@@ -137,7 +137,7 @@ func TestProcessFlags(t *testing.T) {
137137
processFlags(rootCmd)
138138

139139
// Verify mapping
140-
assert.Equal(t, validateVar, engineConfigVar.ScanConfig.WithValidation, "validateVar should be mapped to engineConfigVar.ScanConfig.WithValidation")
140+
assert.Equal(t, validateVar, engineConfigVar.WithValidation, "validateVar should be mapped to engineConfigVar.WithValidation")
141141
})
142142

143143
t.Run("IgnoreListProcessing", func(t *testing.T) {
@@ -170,7 +170,7 @@ func TestProcessFlags(t *testing.T) {
170170

171171
// Verify all mappings
172172
assert.Equal(t, customRegexRuleVar, engineConfigVar.CustomRegexPatterns, "Custom regex patterns should be mapped")
173-
assert.Equal(t, validateVar, engineConfigVar.ScanConfig.WithValidation, "Validation flag should be mapped")
173+
assert.Equal(t, validateVar, engineConfigVar.WithValidation, "Validation flag should be mapped")
174174
assert.Equal(t, []string{"ignored-rule"}, engineConfigVar.IgnoreList, "IgnoreList should be preserved")
175175
assert.Equal(t, 50, engineConfigVar.MaxTargetMegabytes, "MaxTargetMegabytes should be preserved")
176176
})
@@ -181,7 +181,7 @@ func TestProcessFlags(t *testing.T) {
181181
validateVar = false
182182
engineConfigVar.IgnoreList = []string{}
183183
engineConfigVar.CustomRegexPatterns = []string{}
184-
engineConfigVar.ScanConfig.WithValidation = false
184+
engineConfigVar.WithValidation = false
185185

186186
// Process flags
187187
rootCmd := &cobra.Command{Use: "test"}
@@ -190,7 +190,7 @@ func TestProcessFlags(t *testing.T) {
190190

191191
// Verify empty values are handled correctly
192192
assert.Empty(t, engineConfigVar.CustomRegexPatterns, "Empty custom regex patterns should remain empty")
193-
assert.False(t, engineConfigVar.ScanConfig.WithValidation, "Validation should be false by default")
193+
assert.False(t, engineConfigVar.WithValidation, "Validation should be false by default")
194194
assert.Empty(t, engineConfigVar.IgnoreList, "Empty ignore list should remain empty")
195195
})
196196
}
@@ -220,7 +220,7 @@ max-target-megabytes: 10`
220220
processFlags(rootCmd)
221221

222222
// Verify CLI values take precedence
223-
assert.True(t, engineConfigVar.ScanConfig.WithValidation, "CLI validate flag should override config file")
223+
assert.True(t, engineConfigVar.WithValidation, "CLI validate flag should override config file")
224224
assert.Equal(t, zerolog.DebugLevel, log.Logger.GetLevel(), "CLI log level should override config file")
225225
})
226226
}

cmd/main_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88

99
"github.com/checkmarx/2ms/v5/engine"
1010
"github.com/checkmarx/2ms/v5/engine/rules/ruledefine"
11-
"github.com/checkmarx/2ms/v5/internal/resources"
1211
"github.com/spf13/cobra"
1312
"github.com/stretchr/testify/assert"
1413
)
@@ -44,9 +43,7 @@ func TestPreRun(t *testing.T) {
4443
stdoutFormatVar: "json",
4544
reportPath: []string{"mock.json"},
4645
engineConfigVar: engine.EngineConfig{
47-
ScanConfig: resources.ScanConfig{
48-
WithValidation: true,
49-
},
46+
WithValidation: true,
5047
},
5148
expectedPreRunErr: nil,
5249
},

engine/engine.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"github.com/checkmarx/2ms/v5/engine/score"
2828
"github.com/checkmarx/2ms/v5/engine/semaphore"
2929
"github.com/checkmarx/2ms/v5/engine/validation"
30-
"github.com/checkmarx/2ms/v5/internal/resources"
3130
"github.com/checkmarx/2ms/v5/internal/workerpool"
3231
"github.com/checkmarx/2ms/v5/lib/reporting"
3332
"github.com/checkmarx/2ms/v5/lib/secrets"
@@ -88,7 +87,7 @@ type Engine struct {
8887

8988
Report reporting.IReport
9089

91-
ScanConfig resources.ScanConfig
90+
WithValidation bool
9291

9392
wg conc.WaitGroup
9493

@@ -151,7 +150,7 @@ type EngineConfig struct {
151150

152151
CustomRules []*ruledefine.Rule
153152

154-
ScanConfig resources.ScanConfig
153+
WithValidation bool
155154
}
156155

157156
type EngineOption func(*Engine)
@@ -185,7 +184,7 @@ func initEngine(engineConfig *EngineConfig, opts ...EngineOption) (*Engine, erro
185184
return nil, ErrNoRulesSelected
186185
}
187186

188-
scorer := score.NewScorer(finalRules, engineConfig.ScanConfig.WithValidation)
187+
scorer := score.NewScorer(finalRules, engineConfig.WithValidation)
189188

190189
fileWalkerWorkerPoolSize := defaultDetectorWorkerPoolSize
191190
if engineConfig.DetectorWorkerPoolSize > 0 {
@@ -217,7 +216,7 @@ func initEngine(engineConfig *EngineConfig, opts ...EngineOption) (*Engine, erro
217216
ignoredIds: &engineConfig.IgnoredIds,
218217
allowedValues: &engineConfig.AllowedValues,
219218

220-
ScanConfig: engineConfig.ScanConfig,
219+
WithValidation: engineConfig.WithValidation,
221220

222221
secretsChan: make(chan *secrets.Secret, runtime.GOMAXPROCS(0)),
223222
secretsExtrasChan: make(chan *secrets.Secret, runtime.GOMAXPROCS(0)),
@@ -703,7 +702,7 @@ func (e *Engine) consumeItems(pluginName string) {
703702
}
704703

705704
func (e *Engine) processSecrets() {
706-
if e.ScanConfig.WithValidation {
705+
if e.WithValidation {
707706
e.processSecretsWithValidation()
708707
} else {
709708
e.processSecretsWithoutValidation()
@@ -756,7 +755,7 @@ func (e *Engine) processEvaluationWithoutValidation() {
756755

757756
// processSecretsEvaluation evaluates the secret's validationStatus, Severity and CVSS score
758757
func (e *Engine) processSecretsEvaluation() {
759-
if e.ScanConfig.WithValidation {
758+
if e.WithValidation {
760759
e.processEvaluationWithValidation()
761760
} else {
762761
e.processEvaluationWithoutValidation()

engine/engine_test.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"github.com/checkmarx/2ms/v5/engine/chunk"
2222
"github.com/checkmarx/2ms/v5/engine/rules"
2323
"github.com/checkmarx/2ms/v5/engine/semaphore"
24-
"github.com/checkmarx/2ms/v5/internal/resources"
2524
"github.com/checkmarx/2ms/v5/lib/secrets"
2625
"github.com/checkmarx/2ms/v5/plugins"
2726
"github.com/rs/zerolog"
@@ -939,9 +938,7 @@ func TestProcessItems(t *testing.T) {
939938
func TestProcessSecrets(t *testing.T) {
940939
t.Run("Validate flag is enabled", func(t *testing.T) {
941940
instance, err := initEngine(&EngineConfig{
942-
ScanConfig: resources.ScanConfig{
943-
WithValidation: true,
944-
},
941+
WithValidation: true,
945942
})
946943
assert.NoError(t, err)
947944
secretsChan := instance.secretsChan
@@ -993,9 +990,7 @@ func TestProcessSecrets(t *testing.T) {
993990
})
994991
t.Run("Validate flag is disabled", func(t *testing.T) {
995992
instance, err := initEngine(&EngineConfig{
996-
ScanConfig: resources.ScanConfig{
997-
WithValidation: false,
998-
},
993+
WithValidation: false,
999994
})
1000995
assert.NoError(t, err)
1001996
secretsChan := instance.secretsChan
@@ -1207,8 +1202,8 @@ func TestProcessEvaluationWithValidation(t *testing.T) {
12071202
t.Run(tt.name, func(t *testing.T) {
12081203
instance, err := initEngine(
12091204
&EngineConfig{
1210-
ScanConfig: resources.ScanConfig{WithValidation: true},
1211-
CustomRules: tt.customRules},
1205+
WithValidation: true,
1206+
CustomRules: tt.customRules},
12121207
)
12131208
assert.NoError(t, err)
12141209
validationChan := instance.GetValidationCh()

internal/resources/scanner.go

Lines changed: 0 additions & 17 deletions
This file was deleted.

pkg/scan.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"sync"
77

8-
"github.com/checkmarx/2ms/v5/internal/resources"
98
"github.com/checkmarx/2ms/v5/plugins"
109
"github.com/rs/zerolog/log"
1110
"github.com/sourcegraph/conc"
@@ -17,7 +16,7 @@ import (
1716

1817
type scanner struct {
1918
engineInstance engine.IEngine
20-
scanConfig resources.ScanConfig
19+
scanConfig ScanConfig
2120
mu sync.RWMutex
2221
}
2322

@@ -33,7 +32,7 @@ func NewScanner() Scanner {
3332
return &scanner{}
3433
}
3534

36-
func (s *scanner) Reset(scanConfig *resources.ScanConfig, opts ...engine.EngineOption) error {
35+
func (s *scanner) Reset(scanConfig *ScanConfig, opts ...engine.EngineOption) error {
3736
s.mu.Lock()
3837
defer s.mu.Unlock()
3938

@@ -45,7 +44,7 @@ func (s *scanner) Reset(scanConfig *resources.ScanConfig, opts ...engine.EngineO
4544
MaxFindings: scanConfig.MaxFindings,
4645
MaxRuleMatchesPerFragment: scanConfig.MaxRuleMatchesPerFragment,
4746
MaxSecretSize: scanConfig.MaxSecretSize,
48-
ScanConfig: *scanConfig,
47+
WithValidation: scanConfig.WithValidation,
4948
}, opts...)
5049
if err != nil {
5150
return fmt.Errorf("error initializing engine: %w", err)
@@ -57,7 +56,7 @@ func (s *scanner) Reset(scanConfig *resources.ScanConfig, opts ...engine.EngineO
5756
return nil
5857
}
5958

60-
func (s *scanner) Scan(scanItems []ScanItem, scanConfig *resources.ScanConfig, opts ...engine.EngineOption) (reporting.IReport, error) {
59+
func (s *scanner) Scan(scanItems []ScanItem, scanConfig *ScanConfig, opts ...engine.EngineOption) (reporting.IReport, error) {
6160
var wg conc.WaitGroup
6261
err := s.Reset(scanConfig, opts...)
6362
if err != nil {
@@ -109,7 +108,7 @@ func (s *scanner) Scan(scanItems []ScanItem, scanConfig *resources.ScanConfig, o
109108

110109
func (s *scanner) ScanDynamic(
111110
itemsIn <-chan ScanItem,
112-
scanConfig *resources.ScanConfig,
111+
scanConfig *ScanConfig,
113112
opts ...engine.EngineOption,
114113
) (reporting.IReport, error) {
115114
var wg conc.WaitGroup

0 commit comments

Comments
 (0)