/
state.go
82 lines (72 loc) · 2.04 KB
/
state.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package session
import (
"sync"
coreapi "github.com/rumenvasilev/rvsecret/internal/core/api"
"github.com/rumenvasilev/rvsecret/internal/core/finding"
"github.com/rumenvasilev/rvsecret/internal/stats"
)
type State struct {
*sync.Mutex
Stats *stats.Stats
Findings map[string]*finding.Finding
Targets []*coreapi.Owner
Repositories []*coreapi.Repository
}
// AddTargets would iterate over the list and call AddTarget to append each one to the state list
func (st *State) AddTargets(targets []*coreapi.Owner) {
for _, v := range targets {
st.AddTarget(v)
}
}
// AddTarget will add a new target to a session to be scanned during that session
func (st *State) AddTarget(target *coreapi.Owner) {
st.Lock()
defer st.Unlock()
for _, t := range st.Targets {
if *target.ID == *t.ID {
return
}
}
st.Targets = append(st.Targets, target)
// Update statistics
switch *target.Kind {
case coreapi.TargetTypeOrganization:
st.Stats.IncrementOrgs()
case coreapi.TargetTypeUser:
st.Stats.IncrementUsers()
}
st.Stats.IncrementTargets()
}
// AddRepository will add a given repository to be scanned to a session. This counts as
// the total number of repos that have been gathered during a session.
func (st *State) AddRepository(repository *coreapi.Repository) {
st.Lock()
defer st.Unlock()
for _, r := range st.Repositories {
if repository.ID == r.ID {
return
}
}
st.Repositories = append(st.Repositories, repository)
}
// AddFinding will add a finding that has been discovered during a session to the list of findings
// for that session
func (st *State) AddFinding(finding *finding.Finding) bool {
st.Lock()
defer st.Unlock()
// No need to append another finding of the same
// TODO perhaps make the rules that matched a list
if _, ok := st.Findings[finding.SecretID]; ok {
return false
}
st.Findings[finding.SecretID] = finding
st.Stats.IncrementFindingsTotal()
return true
}
func (st *State) GetFindings() []*finding.Finding {
var res []*finding.Finding
for _, f := range st.Findings {
res = append(res, f)
}
return res
}