Skip to content

Commit

Permalink
Fix channel issues
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonardoLordelloFontes committed Nov 21, 2024
1 parent 8c3431d commit d7e0687
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
6 changes: 3 additions & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ var report = reporting.Init()
var secretsChan = make(chan *secrets.Secret)
var secretsExtrasChan = make(chan *secrets.Secret)
var validationChan = make(chan *secrets.Secret)
var cvssScoreChan = make(chan *secrets.Secret)
var cvssScoreWithoutValidationChan = make(chan *secrets.Secret)

func Execute() (int, error) {
vConfig.SetEnvPrefix(envPrefix)
Expand Down Expand Up @@ -150,11 +150,11 @@ func preRun(pluginName string, cmd *cobra.Command, args []string) error {

if validateVar {
channels.WaitGroup.Add(1)
go processValidation(engine)
go processValidationAndScoreWithValidation(engine)
}

channels.WaitGroup.Add(1)
go processScore(engine)
go processScoreWithoutValidation(engine)

return nil
}
Expand Down
24 changes: 12 additions & 12 deletions cmd/workers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"github.com/checkmarx/2ms/lib/secrets"
"sync"

"github.com/checkmarx/2ms/engine"
Expand Down Expand Up @@ -29,13 +30,13 @@ func processSecrets() {
if validateVar {
validationChan <- secret
} else {
cvssScoreChan <- secret
cvssScoreWithoutValidationChan <- secret
}
report.Results[secret.ID] = append(report.Results[secret.ID], secret)
}
close(secretsExtrasChan)
close(validationChan)
close(cvssScoreChan)
close(cvssScoreWithoutValidationChan)
}

func processSecretsExtras() {
Expand All @@ -49,30 +50,29 @@ func processSecretsExtras() {
wgExtras.Wait()
}

func processValidation(engine *engine.Engine) {
func processValidationAndScoreWithValidation(engine *engine.Engine) {
defer channels.WaitGroup.Done()

wgValidation := &sync.WaitGroup{}
for secret := range validationChan {
wgValidation.Add(1)
go func() {
wgValidation.Done()
engine.RegisterForValidation(secret)
cvssScoreChan <- secret
}()
wgValidation.Add(2)
go func(secret *secrets.Secret, wg *sync.WaitGroup) {
engine.RegisterForValidation(secret, wg)
engine.Score(secret, true, wg)
}(secret, wgValidation)
}
wgValidation.Wait()

engine.Validate()
}

func processScore(engine *engine.Engine) {
func processScoreWithoutValidation(engine *engine.Engine) {
defer channels.WaitGroup.Done()

wgScore := &sync.WaitGroup{}
for secret := range cvssScoreChan {
for secret := range cvssScoreWithoutValidationChan {
wgScore.Add(1)
go engine.Score(secret, validateVar, wgScore)
go engine.Score(secret, false, wgScore)
}
wgScore.Wait()
}
3 changes: 2 additions & 1 deletion engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ func (e *Engine) AddRegexRules(patterns []string) error {
return nil
}

func (s *Engine) RegisterForValidation(secret *secrets.Secret) {
func (s *Engine) RegisterForValidation(secret *secrets.Secret, wg *sync.WaitGroup) {
defer wg.Done()
s.validator.RegisterForValidation(secret)
}

Expand Down

0 comments on commit d7e0687

Please sign in to comment.