Skip to content

Commit 30b28ee

Browse files
LeonardoLordelloFontescx-celso-silvacx-leonardo-fontes
authored
feat: expose scan functionality as an importable package (#279)
Co-authored-by: arturfalcao <[email protected]> Co-authored-by: LeonardoLordelloFontes <[email protected]>
1 parent 0520009 commit 30b28ee

21 files changed

+926
-134
lines changed

.2ms.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,23 @@ ignore-result:
2929
- 33a14f1d1e4a1201a3e0062ebf09079fe8c84714 # value used for testing, found at https://github.com/Checkmarx/2ms/commits/d093b7ca36fdacd2f895dd9afd088fad05d77600/cmd/workers_test.go
3030
- da610673906f695e3e85bda6fc0a916762f01a70 # value used for testing, found at https://github.com/Checkmarx/2ms/commits/d093b7ca36fdacd2f895dd9afd088fad05d77600/cmd/workers_test.go
3131
- f8da5c56428cf708773be38269932c46aaf44cd4 # value used for testing, found at https://github.com/Checkmarx/2ms/commits/d093b7ca36fdacd2f895dd9afd088fad05d77600/cmd/workers_test.go
32+
- 0d49f4953e8c5b2e04cca54d40bd2a91c079926b # value used for testing
33+
- 5fb857fa72e8d568e6cfd96119d6b2db87c1e9b2 # value used for testing
34+
- 6b92e79146584c6263671b7bcaac79a9c0852465 # value used for testing
35+
- 22a792422372ef239494839d11c188258d18abc8 # value used for testing
36+
- 29ce1990ca4555a207e77a66ffc26d46575a7911 # value used for testing
37+
- 98a2f843609061bba58b69d4d31b70624de299ee # value used for testing
38+
- 0188f28d26c2ae3f87df20092ab39c4465d6bbba # value used for testing
39+
- 468bdfec08e1660b6ec73d78d15f03c320c68078 # value used for testing
40+
- 5586d6fb77d9fa54224604ab158c2ceda4ab0995 # value used for testing
41+
- 6403ca0ffb2abf3f1c9f70202474fb8f6564c4d7 # value used for testing
42+
- a6fe66dfd9531c5415c1d1fed28b71f13a855a46 # value used for testing
43+
- aaf4ba87a3bdbaf9346c0229f404eb86c0e6aabe # value used for testing
44+
- b09e3219bca2cbcc4d7bc34f46e394e1f80d6574 # value used for testing
45+
- c00a0d0af6bac8b20572bbb3b0b2cbea70476a0d # value used for testing
46+
- c94ccae65acb14fdd2b9db7c9119e58875346a3b # value used for testing
47+
- d4ac7947e0a7a4b387bf46279daa74e9dbe7f66f # value used for testing
48+
- dd2e802e4c3205e57e291a89dfd469946531292b # value used for testing
49+
- e475d6cf0a94469ea1717db008936a4e8749fe6a # value used for testing
50+
- eed7c634d36422d7276cd8623c149e4c8d874f95 # value used for testing
51+
- ff933778f18c92254c15369564b7d359f44018b5 # value used for testing

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# and "Missing User Instruction" since 2ms container is stopped after scan
44

55
# Builder image
6-
FROM cgr.dev/chainguard/go@sha256:2453e92671fb693999e65fde99bbd5744b120b7dd70f3f7c7b220e185ec35050 AS builder
6+
FROM cgr.dev/chainguard/go@sha256:411f37ae52643cf040cfaca740aa78951009f3e7e399eef2ec797c153fe4c892 AS builder
77

88
WORKDIR /app
99

@@ -20,7 +20,7 @@ COPY . .
2020
RUN GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -a -o /app/2ms .
2121

2222
# Runtime image
23-
FROM cgr.dev/chainguard/git@sha256:9e3ec4c4f1465ac810a7e4335d458582c43ad4e8dbaf8ab3a74f8f2a7fdffec2
23+
FROM cgr.dev/chainguard/git@sha256:c893f65bcc5d3de1c327af6db17566139af7663ef89001d536e8370226dcf881
2424

2525
WORKDIR /app
2626

cmd/main.go

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,17 @@ var allPlugins = []plugins.IPlugin{
6666
&plugins.GitPlugin{},
6767
}
6868

69-
var channels = plugins.Channels{
69+
var Channels = plugins.Channels{
7070
Items: make(chan plugins.ISourceItem),
7171
Errors: make(chan error),
7272
WaitGroup: &sync.WaitGroup{},
7373
}
7474

75-
var report = reporting.Init()
76-
var secretsChan = make(chan *secrets.Secret)
77-
var secretsExtrasChan = make(chan *secrets.Secret)
78-
var validationChan = make(chan *secrets.Secret)
79-
var cvssScoreWithoutValidationChan = make(chan *secrets.Secret)
75+
var Report = reporting.Init()
76+
var SecretsChan = make(chan *secrets.Secret)
77+
var SecretsExtrasChan = make(chan *secrets.Secret)
78+
var ValidationChan = make(chan *secrets.Secret)
79+
var CvssScoreWithoutValidationChan = make(chan *secrets.Secret)
8080

8181
func Execute() (int, error) {
8282
vConfig.SetEnvPrefix(envPrefix)
@@ -104,7 +104,7 @@ func Execute() (int, error) {
104104
rootCmd.AddGroup(&cobra.Group{Title: group, ID: group})
105105

106106
for _, plugin := range allPlugins {
107-
subCommand, err := plugin.DefineCommand(channels.Items, channels.Errors)
107+
subCommand, err := plugin.DefineCommand(Channels.Items, Channels.Errors)
108108
if err != nil {
109109
return 0, fmt.Errorf("error while defining command for plugin %s: %s", plugin.GetName(), err.Error())
110110
}
@@ -116,13 +116,13 @@ func Execute() (int, error) {
116116
rootCmd.AddCommand(subCommand)
117117
}
118118

119-
listenForErrors(channels.Errors)
119+
listenForErrors(Channels.Errors)
120120

121121
if err := rootCmd.Execute(); err != nil {
122122
return 0, err
123123
}
124124

125-
return report.TotalSecretsFound, nil
125+
return Report.TotalSecretsFound, nil
126126
}
127127

128128
func preRun(pluginName string, cmd *cobra.Command, args []string) error {
@@ -139,38 +139,38 @@ func preRun(pluginName string, cmd *cobra.Command, args []string) error {
139139
return err
140140
}
141141

142-
channels.WaitGroup.Add(1)
143-
go processItems(engine, pluginName)
142+
Channels.WaitGroup.Add(1)
143+
go ProcessItems(engine, pluginName)
144144

145-
channels.WaitGroup.Add(1)
146-
go processSecrets()
145+
Channels.WaitGroup.Add(1)
146+
go ProcessSecrets()
147147

148-
channels.WaitGroup.Add(1)
149-
go processSecretsExtras()
148+
Channels.WaitGroup.Add(1)
149+
go ProcessSecretsExtras()
150150

151151
if validateVar {
152-
channels.WaitGroup.Add(1)
153-
go processValidationAndScoreWithValidation(engine)
152+
Channels.WaitGroup.Add(1)
153+
go ProcessValidationAndScoreWithValidation(engine)
154154
} else {
155-
channels.WaitGroup.Add(1)
156-
go processScoreWithoutValidation(engine)
155+
Channels.WaitGroup.Add(1)
156+
go ProcessScoreWithoutValidation(engine)
157157
}
158158

159159
return nil
160160
}
161161

162162
func postRun(cmd *cobra.Command, args []string) error {
163-
channels.WaitGroup.Wait()
163+
Channels.WaitGroup.Wait()
164164

165165
cfg := config.LoadConfig("2ms", Version)
166166

167-
if report.TotalItemsScanned > 0 {
168-
if err := report.ShowReport(stdoutFormatVar, cfg); err != nil {
167+
if Report.TotalItemsScanned > 0 {
168+
if err := Report.ShowReport(stdoutFormatVar, cfg); err != nil {
169169
return err
170170
}
171171

172172
if len(reportPathVar) > 0 {
173-
err := report.WriteFile(reportPathVar, cfg)
173+
err := Report.WriteFile(reportPathVar, cfg)
174174
if err != nil {
175175
return fmt.Errorf("failed to create report file with error: %s", err)
176176
}

cmd/main_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,23 +76,23 @@ func TestPreRun(t *testing.T) {
7676
engineConfigVar = tt.engineConfigVar
7777
customRegexRuleVar = tt.customRegexRuleVar
7878
validateVar = tt.validateVar
79-
channels.Items = make(chan plugins.ISourceItem)
80-
channels.Errors = make(chan error)
81-
channels.WaitGroup = &sync.WaitGroup{}
82-
secretsChan = make(chan *secrets.Secret)
83-
secretsExtrasChan = make(chan *secrets.Secret)
84-
validationChan = make(chan *secrets.Secret)
85-
cvssScoreWithoutValidationChan = make(chan *secrets.Secret)
79+
Channels.Items = make(chan plugins.ISourceItem)
80+
Channels.Errors = make(chan error)
81+
Channels.WaitGroup = &sync.WaitGroup{}
82+
SecretsChan = make(chan *secrets.Secret)
83+
SecretsExtrasChan = make(chan *secrets.Secret)
84+
ValidationChan = make(chan *secrets.Secret)
85+
CvssScoreWithoutValidationChan = make(chan *secrets.Secret)
8686
err := preRun("mock", nil, nil)
87-
close(channels.Items)
88-
close(channels.Errors)
89-
channels.WaitGroup.Wait()
87+
close(Channels.Items)
88+
close(Channels.Errors)
89+
Channels.WaitGroup.Wait()
9090
if tt.expectedErr != nil {
9191
assert.Error(t, err)
9292
assert.EqualError(t, err, tt.expectedErr.Error())
9393
} else {
9494
assert.NoError(t, err)
95-
assert.Empty(t, channels.Errors)
95+
assert.Empty(t, Channels.Errors)
9696
}
9797
})
9898
}

cmd/workers.go

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,58 @@
11
package cmd
22

33
import (
4-
"github.com/checkmarx/2ms/lib/secrets"
5-
"sync"
6-
74
"github.com/checkmarx/2ms/engine"
85
"github.com/checkmarx/2ms/engine/extra"
6+
"github.com/checkmarx/2ms/lib/secrets"
7+
"sync"
98
)
109

11-
func processItems(engine *engine.Engine, pluginName string) {
12-
defer channels.WaitGroup.Done()
13-
10+
func ProcessItems(engine *engine.Engine, pluginName string) {
11+
defer Channels.WaitGroup.Done()
1412
wgItems := &sync.WaitGroup{}
15-
for item := range channels.Items {
16-
report.TotalItemsScanned++
13+
for item := range Channels.Items {
14+
Report.TotalItemsScanned++
1715
wgItems.Add(1)
18-
go engine.Detect(item, secretsChan, wgItems, pluginName, channels.Errors)
16+
go engine.Detect(item, SecretsChan, wgItems, pluginName, Channels.Errors)
1917
}
2018
wgItems.Wait()
21-
close(secretsChan)
19+
close(SecretsChan)
2220
}
2321

24-
func processSecrets() {
25-
defer channels.WaitGroup.Done()
22+
func ProcessSecrets() {
23+
defer Channels.WaitGroup.Done()
2624

27-
for secret := range secretsChan {
28-
report.TotalSecretsFound++
29-
secretsExtrasChan <- secret
25+
for secret := range SecretsChan {
26+
Report.TotalSecretsFound++
27+
SecretsExtrasChan <- secret
3028
if validateVar {
31-
validationChan <- secret
29+
ValidationChan <- secret
3230
} else {
33-
cvssScoreWithoutValidationChan <- secret
31+
CvssScoreWithoutValidationChan <- secret
3432
}
35-
report.Results[secret.ID] = append(report.Results[secret.ID], secret)
33+
Report.Results[secret.ID] = append(Report.Results[secret.ID], secret)
3634
}
37-
close(secretsExtrasChan)
38-
close(validationChan)
39-
close(cvssScoreWithoutValidationChan)
35+
close(SecretsExtrasChan)
36+
close(ValidationChan)
37+
close(CvssScoreWithoutValidationChan)
4038
}
4139

42-
func processSecretsExtras() {
43-
defer channels.WaitGroup.Done()
40+
func ProcessSecretsExtras() {
41+
defer Channels.WaitGroup.Done()
4442

4543
wgExtras := &sync.WaitGroup{}
46-
for secret := range secretsExtrasChan {
44+
for secret := range SecretsExtrasChan {
4745
wgExtras.Add(1)
4846
go extra.AddExtraToSecret(secret, wgExtras)
4947
}
5048
wgExtras.Wait()
5149
}
5250

53-
func processValidationAndScoreWithValidation(engine *engine.Engine) {
54-
defer channels.WaitGroup.Done()
51+
func ProcessValidationAndScoreWithValidation(engine *engine.Engine) {
52+
defer Channels.WaitGroup.Done()
5553

5654
wgValidation := &sync.WaitGroup{}
57-
for secret := range validationChan {
55+
for secret := range ValidationChan {
5856
wgValidation.Add(2)
5957
go func(secret *secrets.Secret, wg *sync.WaitGroup) {
6058
engine.RegisterForValidation(secret, wg)
@@ -66,11 +64,11 @@ func processValidationAndScoreWithValidation(engine *engine.Engine) {
6664
engine.Validate()
6765
}
6866

69-
func processScoreWithoutValidation(engine *engine.Engine) {
70-
defer channels.WaitGroup.Done()
67+
func ProcessScoreWithoutValidation(engine *engine.Engine) {
68+
defer Channels.WaitGroup.Done()
7169

7270
wgScore := &sync.WaitGroup{}
73-
for secret := range cvssScoreWithoutValidationChan {
71+
for secret := range CvssScoreWithoutValidationChan {
7472
wgScore.Add(1)
7573
go engine.Score(secret, false, wgScore)
7674
}

0 commit comments

Comments
 (0)