-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.go
48 lines (36 loc) · 991 Bytes
/
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
package main
import (
"bufio"
"io"
"regexp"
"github.com/sirupsen/logrus"
)
var (
emailRegexp = regexp.MustCompile(`[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}`)
detectPasswordInLOGIN = regexp.MustCompile(`^(.*LOGIN\s+\S+\s+)"[^"]+"(.*)$`)
)
func censorCredentials(in io.Reader, out io.Writer) {
scanner := bufio.NewScanner(in)
for scanner.Scan() {
line := scanner.Text()
censoredLine := censorEmailAddress(censorPasswordInLogin(line))
_, err := out.Write([]byte(censoredLine + "\n"))
if err != nil {
logrus.Errorf("unable to write censored lines: %v", err)
}
}
}
func censorPasswordInLogin(in string) string {
matches := detectPasswordInLOGIN.FindStringSubmatch(in)
if len(matches) == 0 {
return in
}
return matches[1] + `"****"` + matches[2]
}
func censorEmailAddress(in string) string {
matches := emailRegexp.FindAllString(in, -1)
if len(matches) == 0 {
return in
}
return emailRegexp.ReplaceAllString(in, "*******@*****.***")
}