-
Notifications
You must be signed in to change notification settings - Fork 18
/
log.go
209 lines (184 loc) · 5.25 KB
/
log.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package robber
import (
"fmt"
"os"
"strings"
"sync"
"time"
"github.com/fatih/color"
)
const (
verbose = iota
secret
info
data
succ
warn
fail
)
const seperator = "--------------------------------------------------------"
var validColors = map[string]*color.Color{
"black": color.New(color.FgBlack),
"blue": color.New(color.FgBlue),
"cyan": color.New(color.FgCyan),
"green": color.New(color.FgGreen),
"magenta": color.New(color.FgMagenta),
"red": color.New(color.FgRed),
"white": color.New(color.FgWhite),
"yellow": color.New(color.FgYellow),
"hiBlack": color.New(color.FgHiBlack),
"hiBlue": color.New(color.FgHiBlue),
"hiCyan": color.New(color.FgHiCyan),
"hiGreen": color.New(color.FgHiGreen),
"hiMagenta": color.New(color.FgHiMagenta),
"hiRed": color.New(color.FgHiRed),
"hiWhite": color.New(color.FgHiWhite),
"hiYellow": color.New(color.FgHiYellow),
}
// Default colors are set
var logColors = map[int]*color.Color{
verbose: color.New(color.FgBlue),
secret: color.New(color.FgHiYellow).Add(color.Bold),
info: color.New(color.FgHiWhite),
data: color.New(color.FgHiBlue),
succ: color.New(color.FgGreen),
warn: color.New(color.FgRed),
fail: color.New(color.FgRed).Add(color.Bold),
}
// Finding struct contains data of a given secret finding, used for later output of a finding.
type Finding struct {
CommitHash string
CommitMessage string
Committer string
DateOfCommit string
Email string
Reason string
Secret []int
Diff string
RepoName string
Filepath string
}
// Logger handles all logging to the output.
type Logger struct {
sync.Mutex
Verbose bool
}
func setColors() {
colors := GetEnvColors()
for colorType := verbose; colorType <= fail; colorType++ {
if empty, _ := colors[colorType]; empty == "" {
continue
}
fields := strings.Fields(colors[colorType])
if val, ok := validColors[fields[0]]; ok {
if len(fields) > 1 && fields[1] == "bold" {
logColors[colorType] = val.Add(color.Bold)
continue
}
logColors[colorType] = val
}
}
}
// NewLogger sets all colors as specified and returns a new logger.
func NewLogger(verbose bool) *Logger {
setColors()
return &Logger{
Verbose: verbose,
}
}
// NewFinding simply returns a new finding struct.
func NewFinding(reason string, secret []int, diffObject *DiffObject) *Finding {
finding := &Finding{
CommitHash: diffObject.Commit.Hash.String(),
CommitMessage: diffObject.Commit.Message,
Committer: diffObject.Commit.Committer.Name,
DateOfCommit: diffObject.Commit.Committer.When.Format(time.RFC1123),
Email: diffObject.Commit.Committer.Email,
Reason: reason,
Secret: secret,
RepoName: *diffObject.Reponame,
Filepath: *diffObject.Filepath,
}
return finding
}
func (l *Logger) log(level int, format string, a ...interface{}) {
l.Lock()
defer l.Unlock()
if level == verbose && l.Verbose == false {
return
}
if c, ok := logColors[level]; ok {
c.Printf(format, a...)
} else {
fmt.Printf(format, a...)
}
if level == fail {
os.Exit(1)
}
}
func (l *Logger) logSecret(diff string, booty []int, contextNum int) {
data, _ := logColors[data]
secret, _ := logColors[secret]
data.Printf("%s", diff[:booty[0]])
secret.Printf("%s", diff[booty[0]:booty[1]])
data.Printf("%s\n\n", diff[booty[1]:])
}
// LogFinding is used to output Findings
func (l *Logger) LogFinding(f *Finding, m *Middleware, contextDiff string) {
l.Lock()
defer l.Unlock()
f.Diff = contextDiff
m.Append(f)
info, _ := logColors[info]
data, _ := logColors[data]
secret, _ := logColors[secret]
repoPath, _ := GetDir(f.RepoName)
info.Println(seperator)
info.Printf("Reason: ")
data.Println(f.Reason)
if f.Filepath != "" {
info.Printf("Filepath: ")
data.Println(f.Filepath)
}
info.Printf("Repo name: ")
data.Println(f.RepoName)
info.Printf("Committer: ")
data.Printf("%s (%s)\n", f.Committer, f.Email)
info.Printf("Commit hash: ")
data.Println(f.CommitHash)
info.Printf("Date of commit: ")
data.Println(f.DateOfCommit)
info.Printf("View command: ")
data.Printf("git --git-dir=%s show %s:%s\n", repoPath, f.CommitHash[:6], f.Filepath)
info.Printf("Commit message: ")
data.Printf("%s\n\n", strings.Trim(f.CommitMessage, "\n"))
if *m.Flags.NoContext {
secret.Printf("%s\n\n", contextDiff[f.Secret[0]:f.Secret[1]])
} else {
l.logSecret(f.Diff, f.Secret, *m.Flags.Context)
}
}
// LogVerbose prints to output using 'verbose' colors
func (l *Logger) LogVerbose(format string, a ...interface{}) {
l.log(verbose, format, a...)
}
// LogSecret prints to output using 'secret' colors
func (l *Logger) LogSecret(format string, a ...interface{}) {
l.log(secret, format, a...)
}
// LogInfo prints to output using 'info' colors
func (l *Logger) LogInfo(format string, a ...interface{}) {
l.log(info, "[+] "+format, a...)
}
// LogSucc prints to output using 'succ' colors
func (l *Logger) LogSucc(format string, a ...interface{}) {
l.log(succ, "[+] "+format, a...)
}
// LogWarn prints to output using 'warn' colors
func (l *Logger) LogWarn(format string, a ...interface{}) {
l.log(warn, "[-] "+format, a...)
}
// LogFail prints to output using 'fail' colors
func (l *Logger) LogFail(format string, a ...interface{}) {
l.log(fail, "[!] "+format, a...)
}