Skip to content

Commit

Permalink
Refactor reporting package to use pointers for Secret struct
Browse files Browse the repository at this point in the history
(cherry picked from commit 753c515)
  • Loading branch information
Baruch Odem committed Feb 14, 2024
1 parent a37deaf commit 9839c0e
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ var channels = plugins.Channels{
}

var report = reporting.Init()
var secretsChan = make(chan reporting.Secret)
var secretsChan = make(chan *reporting.Secret)

func Execute() (int, error) {
vConfig.SetEnvPrefix(envPrefix)
Expand Down
8 changes: 4 additions & 4 deletions reporting/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ const (
)

type Report struct {
TotalItemsScanned int `json:"totalItemsScanned"`
TotalSecretsFound int `json:"totalSecretsFound"`
Results map[string][]Secret `json:"results"`
TotalItemsScanned int `json:"totalItemsScanned"`
TotalSecretsFound int `json:"totalSecretsFound"`
Results map[string][]*Secret `json:"results"`
}

type Secret struct {
Expand All @@ -35,7 +35,7 @@ type Secret struct {

func Init() *Report {
return &Report{
Results: make(map[string][]Secret),
Results: make(map[string][]*Secret),
}
}

Expand Down
6 changes: 3 additions & 3 deletions reporting/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ JPcHeO7M6FohKgcEHX84koQDN98J/L7pFlSoU7WOl6f8BKavIdeSTPS9qQYWdQuT
4Xgur9w/aLZrLM3DSatR+kL+cVTyDTtgCt9Dc8k48Q==
-----END RSA PRIVATE KEY-----`)

results := map[string][]Secret{}
results := map[string][]*Secret{}
report := Report{len(results), 1, results}
secret := Secret{Source: "bla", StartLine: 0, StartColumn: 0, EndLine: 0, EndColumn: 0, Value: secretValue}
secret := &Secret{Source: "bla", StartLine: 0, StartColumn: 0, EndLine: 0, EndColumn: 0, Value: secretValue}
source := "directory\\rawStringAsFile.txt"

report.Results[source] = append(report.Results[source], secret)
Expand All @@ -36,6 +36,6 @@ JPcHeO7M6FohKgcEHX84koQDN98J/L7pFlSoU7WOl6f8BKavIdeSTPS9qQYWdQuT
}

if !reflect.DeepEqual(report.Results, results) {
t.Errorf("got %q want %q", key, results)
t.Errorf("got %+v want %+v", key, results)
}
}
2 changes: 1 addition & 1 deletion reporting/sarif.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func getResults(report Report) []Results {
return results
}

func getLocation(secret Secret) []Locations {
func getLocation(secret *Secret) []Locations {
return []Locations{
{
PhysicalLocation: PhysicalLocation{
Expand Down
6 changes: 3 additions & 3 deletions secrets/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ func Init(secretsConfig SecretsConfig) (*Secrets, error) {
}, nil
}

func (s *Secrets) Detect(item plugins.Item, secretsChannel chan reporting.Secret, wg *sync.WaitGroup, ignoredIds []string) {
func (s *Secrets) Detect(item plugins.Item, secretsChannel chan *reporting.Secret, wg *sync.WaitGroup, ignoredIds []string) {
defer wg.Done()

fragment := detect.Fragment{
Raw: item.Content,
}
for _, value := range s.detector.Detect(fragment) {
itemId := getFindingId(item, value)
secret := reporting.Secret{
secret := &reporting.Secret{
ID: itemId,
Source: item.Source,
RuleID: value.RuleID,
Expand All @@ -76,7 +76,7 @@ func (s *Secrets) Detect(item plugins.Item, secretsChannel chan reporting.Secret
EndColumn: value.EndColumn,
Value: value.Secret,
}
if !isSecretIgnored(&secret, &ignoredIds) {
if !isSecretIgnored(secret, &ignoredIds) {
secretsChannel <- secret
} else {
log.Debug().Msgf("Secret %s was ignored", secret.ID)
Expand Down
2 changes: 1 addition & 1 deletion secrets/secrets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func TestSecrets(t *testing.T) {
}
t.Run(name, func(t *testing.T) {
fmt.Printf("Start test %s", name)
secretsChan := make(chan reporting.Secret, 1)
secretsChan := make(chan *reporting.Secret, 1)
wg := &sync.WaitGroup{}
wg.Add(1)
detector.Detect(plugins.Item{Content: secret.Content}, secretsChan, wg, nil)
Expand Down

0 comments on commit 9839c0e

Please sign in to comment.