-
Notifications
You must be signed in to change notification settings - Fork 3
/
masking.go
162 lines (143 loc) · 3.96 KB
/
masking.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
package loggingdrain
import (
"encoding/json"
"regexp"
)
const (
default_masking_prefix = "[:"
default_masking_suffix = ":]"
)
type logMasker struct {
prefix string
suffix string
nameToInstructions map[string]*logInstruction
}
type logInstruction struct {
pattern string
maskWith string
re *regexp.Regexp
}
type logMaskerMarshalStruct struct {
Prefix string
Suffix string
MaskInstructions []*logInstruction
}
type logInstructionMarshalStruct struct {
Pattern string
MaskWith string
}
func (logInstruction *logInstruction) MarshalJSON() ([]byte, error) {
marshalStruct := logInstructionMarshalStruct{
Pattern: logInstruction.pattern,
MaskWith: logInstruction.maskWith,
}
return json.Marshal(marshalStruct)
}
func (logInstruction *logInstruction) UnmarshalJSON(data []byte) error {
var marshalStruct logInstructionMarshalStruct
err := json.Unmarshal(data, &marshalStruct)
if err != nil {
return err
}
re, err := regexp.Compile(marshalStruct.Pattern)
if err != nil {
return errMaskPatternCompile(err)
}
logInstruction.pattern = marshalStruct.Pattern
logInstruction.maskWith = marshalStruct.MaskWith
logInstruction.re = re
return nil
}
func (logMasker *logMasker) MarshalJSON() ([]byte, error) {
marshalStruct := logMaskerMarshalStruct{
Prefix: logMasker.prefix,
Suffix: logMasker.suffix,
MaskInstructions: make([]*logInstruction, 0, len(logMasker.nameToInstructions)),
}
for _, v := range logMasker.nameToInstructions {
marshalStruct.MaskInstructions = append(marshalStruct.MaskInstructions, v)
}
return json.Marshal(marshalStruct)
}
func (logMasker *logMasker) UnmarshalJSON(data []byte) error {
var marshalStruct logMaskerMarshalStruct
err := json.Unmarshal(data, &marshalStruct)
if err != nil {
return err
}
logMasker.prefix = marshalStruct.Prefix
logMasker.suffix = marshalStruct.Suffix
logMasker.nameToInstructions = map[string]*logInstruction{}
for _, v := range marshalStruct.MaskInstructions {
logMasker.nameToInstructions[v.maskWith] = v
}
return nil
}
func newLogInstruction(maskWith, pattern string) (*logInstruction, error) {
re, err := regexp.Compile(pattern)
if err != nil {
return nil, errMaskPatternCompile(err)
}
return &logInstruction{
pattern: pattern,
maskWith: maskWith,
re: re,
}, nil
}
func (ins *logInstruction) mask(content, prefix, suffix string) string {
maskStr := prefix + ins.maskWith + suffix
return ins.re.ReplaceAllString(content, maskStr)
}
func newLogMaskerWithConfig(maskConfig maskConfig) (*logMasker, error) {
nameToInstructions := map[string]*logInstruction{}
for _, ins := range maskConfig.MaskInstructions {
newIns, err := newLogInstruction(ins.MaskWith, ins.Pattern)
if err != nil {
return nil, err
}
nameToInstructions[ins.MaskWith] = newIns
}
masker := &logMasker{
prefix: maskConfig.Prefix,
suffix: maskConfig.Suffix,
nameToInstructions: nameToInstructions,
}
return masker, nil
}
func newLogMasker(prefix, suffix string) (*logMasker, error) {
nameToInstructions := map[string]*logInstruction{}
masker := &logMasker{
prefix: prefix,
suffix: suffix,
nameToInstructions: nameToInstructions,
}
return masker, nil
}
func (mask *logMasker) addInstruction(maskWith, pattern string) error {
ins, err := newLogInstruction(maskWith, pattern)
if err != nil {
return err
}
mask.nameToInstructions[maskWith] = ins
return nil
}
func (mask *logMasker) mask(content string) string {
if len(mask.nameToInstructions) == 0 {
return content
}
res := content
for _, v := range mask.nameToInstructions {
res = v.mask(res, mask.prefix, mask.suffix)
}
return res
}
func (mask *logMasker) maskNames() []string {
names := make([]string, 0, len(mask.nameToInstructions))
for k := range mask.nameToInstructions {
names = append(names, k)
}
return names
}
func (mask *logMasker) instruction(name string) *logInstruction {
return mask.nameToInstructions[name]
}