-
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathobfuscation.go
More file actions
271 lines (242 loc) · 7.24 KB
/
Copy pathobfuscation.go
File metadata and controls
271 lines (242 loc) · 7.24 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
* Copyright 2022-present Kuei-chun Chen. All rights reserved.
* obfuscation.go
*/
package hatchet
import (
"bufio"
"encoding/json"
"fmt"
"io"
"os"
"strings"
"github.com/simagix/gox"
"go.mongodb.org/mongo-driver/bson"
)
// Obfuscation wraps gox.Obfuscator with hatchet-specific functionality
type Obfuscation struct {
*gox.Obfuscator
}
// NewObfuscation creates a new Obfuscation instance
func NewObfuscation() *Obfuscation {
return &Obfuscation{
Obfuscator: gox.NewObfuscator(),
}
}
// PrintMappings outputs all obfuscation mappings to stderr in JSON format
func (ptr *Obfuscation) PrintMappings() {
mappings := ptr.Obfuscator.GetMappings()
data, _ := json.MarshalIndent(mappings, "", " ")
fmt.Fprintln(os.Stderr, string(data))
}
// ObfuscateFile reads a log file and writes obfuscated output to stdout
func (ptr *Obfuscation) ObfuscateFile(filename string) error {
return ptr.ObfuscateFileToWriter(filename, os.Stdout)
}
// ObfuscateFileToWriter reads a log file and writes obfuscated output to the provided writer
func (ptr *Obfuscation) ObfuscateFileToWriter(filename string, w io.Writer) error {
var err error
var buf []byte
var isPrefix bool
var reader *bufio.Reader
var scanner *bufio.Scanner
if filename == "-" {
scanner = bufio.NewScanner(os.Stdin)
} else {
file, err := os.Open(filename)
if err != nil {
return err
}
defer file.Close()
if reader, err = gox.NewReader(file); err != nil {
return err
}
scanner = bufio.NewScanner(reader)
}
for scanner.Scan() {
str := scanner.Text()
if str == "" {
continue
}
for isPrefix {
var bbuf []byte
if bbuf, isPrefix, err = reader.ReadLine(); err != nil {
break
}
str += string(bbuf)
}
var doc bson.D
if err = bson.UnmarshalExtJSON([]byte(str), false, &doc); err != nil {
continue
}
// Find the attr element directly from bson.D to preserve type
var attr bson.D
for _, elem := range doc {
if elem.Key == "attr" {
if a, ok := elem.Value.(bson.D); ok {
attr = a
}
break
}
}
if attr == nil {
continue
}
obfuscated := ptr.ObfuscateBsonD(attr)
document := bson.D{}
for _, elem := range doc {
if elem.Key != "attr" {
document = append(document, elem)
} else if len(obfuscated) > 0 {
document = append(document, bson.E{Key: "attr", Value: obfuscated})
}
}
buf, err = bson.MarshalExtJSON(document, false, false)
if err == nil {
fmt.Fprintln(w, string(buf))
}
}
return nil
}
// ObfuscateBsonD recursively obfuscates a bson.D document while preserving key order
func (ptr *Obfuscation) ObfuscateBsonD(d bson.D) bson.D {
var obfuscated bson.D
for _, elem := range d {
var obfuscatedValue interface{}
switch value := elem.Value.(type) {
case bson.D:
obfuscatedValue = ptr.ObfuscateBsonD(value)
case bson.A:
obfuscatedValue = ptr.ObfuscateBsonA(value)
case string:
obfuscatedValue = ptr.Obfuscator.ObfuscateString(value)
case int:
obfuscatedValue = ptr.Obfuscator.ObfuscateInt(value)
case int32:
obfuscatedValue = int32(ptr.Obfuscator.ObfuscateInt(int(value)))
case int64:
obfuscatedValue = int64(ptr.Obfuscator.ObfuscateInt(int(value)))
case float32:
obfuscatedValue = float32(ptr.Obfuscator.ObfuscateNumber(float64(value)))
case float64:
obfuscatedValue = ptr.Obfuscator.ObfuscateNumber(value)
default:
obfuscatedValue = value
}
obfuscated = append(obfuscated, bson.E{Key: elem.Key, Value: obfuscatedValue})
}
return obfuscated
}
// ObfuscateBsonA recursively obfuscates a bson.A array
func (ptr *Obfuscation) ObfuscateBsonA(a bson.A) bson.A {
var obfuscated bson.A
for _, elem := range a {
var obfuscatedValue interface{}
switch value := elem.(type) {
case bson.D:
obfuscatedValue = ptr.ObfuscateBsonD(value)
case bson.A:
obfuscatedValue = ptr.ObfuscateBsonA(value)
case string:
obfuscatedValue = ptr.Obfuscator.ObfuscateString(value)
case int:
obfuscatedValue = ptr.Obfuscator.ObfuscateInt(value)
case int32:
obfuscatedValue = int32(ptr.Obfuscator.ObfuscateInt(int(value)))
case int64:
obfuscatedValue = int64(ptr.Obfuscator.ObfuscateInt(int(value)))
case float32:
obfuscatedValue = float32(ptr.Obfuscator.ObfuscateNumber(float64(value)))
case float64:
obfuscatedValue = ptr.Obfuscator.ObfuscateNumber(value)
default:
obfuscatedValue = value
}
obfuscated = append(obfuscated, obfuscatedValue)
}
return obfuscated
}
// --- Wrapper methods for backward compatibility ---
// ObfuscateInt obfuscates an integer value
func (ptr *Obfuscation) ObfuscateInt(data interface{}) int {
return ptr.Obfuscator.ObfuscateInt(gox.ToInt(data))
}
// ObfuscateNumber obfuscates a numeric value
func (ptr *Obfuscation) ObfuscateNumber(data interface{}) float64 {
f, _ := gox.ToFloat64(data)
return ptr.Obfuscator.ObfuscateNumber(f)
}
// ObfuscateCreditCardNo obfuscates a credit card number
func (ptr *Obfuscation) ObfuscateCreditCardNo(cardNo string) string {
if !ContainsCreditCardNo(cardNo) {
return cardNo
}
return ptr.Obfuscator.ObfuscateCreditCardNo(cardNo)
}
// ObfuscateEmail obfuscates an email address
func (ptr *Obfuscation) ObfuscateEmail(email string) string {
return ptr.Obfuscator.ObfuscateEmail(email)
}
// ObfuscateIP obfuscates an IP address
func (ptr *Obfuscation) ObfuscateIP(ip string) string {
return ptr.Obfuscator.ObfuscateIP(ip)
}
// ObfuscateFQDN obfuscates a fully qualified domain name
func (ptr *Obfuscation) ObfuscateFQDN(fqdn string) string {
return ptr.Obfuscator.ObfuscateFQDN(fqdn)
}
// ObfuscateNS obfuscates a MongoDB namespace (db.collection)
// Uses hatchet's stricter IsNamespace check which excludes email addresses
func (ptr *Obfuscation) ObfuscateNS(ns string) string {
// Use hatchet's IsNamespace which excludes emails (has @ check)
if !IsNamespace(ns) {
return ns
}
return ptr.Obfuscator.ObfuscateNamespace(ns)
}
// ObfuscateSSN obfuscates a Social Security Number
func (ptr *Obfuscation) ObfuscateSSN(ssn string) string {
return ptr.Obfuscator.ObfuscateSSN(ssn)
}
// ObfuscatePhoneNo obfuscates a phone number
func (ptr *Obfuscation) ObfuscatePhoneNo(phoneNo string) string {
return ptr.Obfuscator.ObfuscatePhoneNo(phoneNo)
}
// ObfuscateMAC obfuscates a MAC address
func (ptr *Obfuscation) ObfuscateMAC(mac string) string {
return ptr.Obfuscator.ObfuscateMAC(mac)
}
// ObfuscateDate obfuscates dates in a string
func (ptr *Obfuscation) ObfuscateDate(value string) string {
return ptr.Obfuscator.ObfuscateDate(value)
}
// ObfuscateID obfuscates MRN, account numbers, and other IDs
func (ptr *Obfuscation) ObfuscateID(value string) string {
// Use gox regex and obfuscation logic
matches := gox.ReMRN.FindStringSubmatch(value)
if len(matches) == 0 {
return value
}
matched := matches[0]
if cached, exists := ptr.IDMap[matched]; exists {
return strings.Replace(value, matched, cached, -1)
}
// Find where the number starts
numStart := 0
for i, c := range matched {
if c >= '0' && c <= '9' {
numStart = i
break
}
}
prefix := matched[:numStart]
numPart := matched[numStart:]
// Generate deterministic replacement digits
newDigits := make([]byte, len(numPart))
for i := range numPart {
newDigits[i] = byte(gox.HashIndex(matched+fmt.Sprintf("%d", i), 10) + '0')
}
newValue := prefix + string(newDigits)
ptr.IDMap[matched] = newValue
return strings.Replace(value, matched, newValue, -1)
}