-
-
Notifications
You must be signed in to change notification settings - Fork 496
/
report.go
145 lines (119 loc) · 2.59 KB
/
report.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package audit
import (
"fmt"
"sync"
"time"
"github.com/gopasspw/gopass/internal/hashsum"
"github.com/gopasspw/gopass/internal/set"
)
type Finding struct {
Severity string
Message string
}
type SecretReport struct {
Name string
Findings map[string]Finding
Age time.Duration
}
func errors(e []error) []string {
s := make([]string, 0, len(e))
for _, es := range e {
s = append(s, es.Error())
}
return s
}
type Report struct {
Secrets map[string]SecretReport
Template string
Duration time.Duration
}
type ReportBuilder struct {
sync.Mutex
secrets map[string]SecretReport
// SHA512(password) -> secret names
duplicates map[string]set.Set[string]
// HIBP
// SHA1(password) -> secret names
sha1sums map[string]set.Set[string]
t0 time.Time
}
func (r *ReportBuilder) AddPassword(name, pw string) {
if name == "" || pw == "" {
return
}
r.Lock()
defer r.Unlock()
s256 := hashsum.SHA256Hex(pw)
d := r.duplicates[s256]
d.Add(name)
r.duplicates[s256] = d
s1 := hashsum.SHA1Hex(pw)
s := r.sha1sums[s1]
s.Add(name)
r.sha1sums[s1] = s
}
func (r *ReportBuilder) AddFinding(secret, finding, message, severity string) {
if secret == "" || finding == "" || message == "" || severity == "" {
return
}
r.Lock()
defer r.Unlock()
s := r.secrets[secret]
s.Name = secret
if s.Findings == nil {
s.Findings = make(map[string]Finding, 4)
}
f := s.Findings[finding]
f.Message = message
f.Severity = severity
s.Findings[finding] = f
r.secrets[secret] = s
}
func (r *ReportBuilder) SetAge(name string, age time.Duration) {
if name == "" {
return
}
r.Lock()
defer r.Unlock()
s := r.secrets[name]
s.Name = name
s.Age = age
r.secrets[name] = s
}
func newReport() *ReportBuilder {
return &ReportBuilder{
secrets: make(map[string]SecretReport, 512),
duplicates: make(map[string]set.Set[string], 512),
sha1sums: make(map[string]set.Set[string], 512),
t0: time.Now().UTC(),
}
}
// Finalize computes the duplicates.
func (r *ReportBuilder) Finalize() *Report {
for k, s := range r.secrets {
for _, secs := range r.duplicates {
if secs.Len() < 2 {
continue
}
if !secs.Contains(k) {
continue
}
if s.Findings == nil {
s.Findings = make(map[string]Finding, 1)
}
s.Findings["duplicates"] = Finding{
Severity: "warning",
Message: fmt.Sprintf("Duplicates detected. Shared with: %+v", secs.Difference(set.New(k))),
}
}
r.secrets[k] = s
}
ret := &Report{
Secrets: make(map[string]SecretReport, len(r.secrets)),
Duration: time.Since(r.t0),
}
for k := range r.secrets {
ret.Secrets[k] = r.secrets[k]
}
return ret
}