-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner.go
458 lines (393 loc) · 13.1 KB
/
scanner.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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
package sds
import (
"bytes"
"encoding/binary"
"encoding/json"
"fmt"
"reflect"
"unsafe"
)
/*
#include <stdlib.h>
#include <sds.h>
*/
import "C"
var (
ErrUnknown error = fmt.Errorf("unknown error")
ErrInvalidRegex error = fmt.Errorf("invalid regex")
ErrInvalidKeywords error = fmt.Errorf("invalid keywords")
ErrInvalidMatchAction error = fmt.Errorf("invalid match action")
)
// Scanner wraps an SDS scanner.
// See `CreateScanner` to create one providing SDS rules.
// See `Scan`, `ScanEventsList` or a `ScanEventsMap` for usage.
type Scanner struct {
// Id of this scanner generated by the SDS library when the scanner is created.
Id int64
// They are stored on creation for read-only usage.
Rules []Rule
}
// ScanResult contains a Scan result.
type ScanResult struct {
// String Event contains the event after the scan.
// In case of map input it contains the mutated string. (The input event is mutated in place)
// If `Mutated` is true:
// * it contains the processed event after redaction.
// If `Mutated` is false:
// * it contains the original event, unchanged.
Event []byte
scanResult
}
type scanResult struct {
// Mutated indicates if the processed event has been
// mutated or not (e.g. redacted).
Mutated bool
// Matches contains all rule matches if any.
Matches []RuleMatch
}
// CreateScanner creates a scanner in the underlying SDS shared library. The library
// only returns an ID to then address what scanner to use on Scan calls. This ID is
// stored in the Scanner Go object for convenience. See `Scan` to process events.
// The rules used to create the Scanner are stored as a read-only information in the
// returned Scanner.
func CreateScanner(rules []Rule) (*Scanner, error) {
if len(rules) == 0 {
return nil, fmt.Errorf("no rules provided")
}
data, err := json.Marshal(rules)
if err != nil {
return nil, err
}
cdata := C.CString(string(data)) // this call adds the 0, memory has to be freed
defer C.free(unsafe.Pointer(cdata))
var errorString *C.char
id := C.create_scanner(cdata, &errorString)
if id < 0 {
switch id {
// see rust/native/create_scanner.rs for the mapping.
case -1: // rust unknown error
return nil, ErrUnknown
case -2: // rust: CreateScannerError::InvalidRegex
return nil, ErrInvalidRegex
case -3: // rust: CreateScannerError::InvalidKeywords
return nil, ErrInvalidKeywords
case -4: // rust: CreateScannerError::InvalidMatchAction
return nil, ErrInvalidMatchAction
case -5: // rust panic
if errorString != nil {
defer C.free_string(errorString)
return nil, fmt.Errorf("internal panic: %v", C.GoString(errorString))
} else {
return nil, fmt.Errorf("internal panic")
}
}
return nil, ErrUnknown
}
return &Scanner{
Id: int64(id),
Rules: rules,
}, nil
}
// Delete deletes the instance of the current Scanner.
// The current Scanner should not be reused.
func (s *Scanner) Delete() {
C.delete_scanner(C.long(s.Id))
s.Id = 0
s.Rules = nil
}
func (s *Scanner) lowLevelScan(encodedEvent []byte) ([]byte, error) {
cdata := C.CBytes(encodedEvent)
defer C.free(cdata)
var retsize int64
var retcap int64
var errorString *C.char
rvdata := C.scan(C.long(s.Id), cdata, C.long(len(encodedEvent)), (*C.long)(unsafe.Pointer(&retsize)), (*C.long)(unsafe.Pointer(&retcap)), &errorString)
if errorString != nil {
defer C.free_string(errorString)
return nil, fmt.Errorf("internal panic: %v", C.GoString(errorString))
}
// nothing has matched, ignore the returned object
if retsize <= 0 || retcap <= 0 {
return nil, nil
}
// otherwise we received data initially owned by rust, once we've used it,
// use `free_vec` to let know rust it can drop this memory.
defer C.free_vec(rvdata, C.long(retsize), C.long(retcap))
// Note that in the Go 1.21 documentation, GoBytes is part of:
// > A few special functions convert between Go and C types by making copies of the data.
// Meaning that the data in `rv` is a copy owned by Go of what's in rvdata.
response := C.GoBytes(unsafe.Pointer(rvdata), C.int(retsize))
return response, nil
}
func (s *Scanner) scanEncodedMapEvent(encodedEvent []byte, event map[string]interface{}) (ScanResult, error) {
response, err := s.lowLevelScan(encodedEvent)
if err != nil {
return ScanResult{}, err
}
// prepare and return the result
result, err := decodeEventMapResponse(response, event)
if err != nil {
return ScanResult{}, fmt.Errorf("scan: %v", err)
}
return result, nil
}
func (s *Scanner) scanEncodedStringEvent(encodedEvent []byte) (ScanResult, error) {
response, err := s.lowLevelScan(encodedEvent)
if err != nil {
return ScanResult{}, err
}
// prepare and return the result
result, err := decodeResponse(response)
if err != nil {
return ScanResult{}, fmt.Errorf("scan: %v", err)
}
return result, nil
}
// Scan sends the string event to the SDS shared library for processing.
func (s *Scanner) Scan(event []byte) (ScanResult, error) {
encodedEvent := make([]byte, 0)
encodedEvent, err := encodeStringEvent(event, encodedEvent)
if err != nil {
return ScanResult{}, err
}
var result ScanResult
if result, err = s.scanEncodedStringEvent(encodedEvent); err != nil {
return ScanResult{}, err
}
// if not mutated, return the original event.
if !result.Mutated {
result.Event = event
}
return result, err
}
// ScanEventsMap sends a map event to the SDS shared library for processing.
// In case of mutation, event is updated in place.
// The returned ScanResult contains the mutated string in the Event attribute (not the event)
func (s *Scanner) ScanEventsMap(event map[string]interface{}) (ScanResult, error) {
encodedEvent := make([]byte, 0)
encodedEvent, err := encodeMapEvent(event, encodedEvent)
if err != nil {
return ScanResult{}, err
}
return s.scanEncodedMapEvent(encodedEvent, event)
}
// encodeStringEvent encodes teh given event to send it to the SDS shared library.
func encodeStringEvent(log []byte, result []byte) ([]byte, error) {
result = append(result, byte(3)) // string data
result = binary.BigEndian.AppendUint32(result, uint32(len(log)))
result = append(result, log...)
return result, nil
}
func encodeValueRecursive(v interface{}, result []byte) ([]byte, error) {
switch v := v.(type) {
case string:
return encodeStringEvent([]byte(v), result)
case map[string]interface{}:
return encodeMapEvent(v, result)
case []interface{}:
return encodeListEvent(v, result)
default:
return result, fmt.Errorf("encodeValueRecursive: unknown type %T", v)
}
}
func encodeMapEvent(event map[string]interface{}, result []byte) ([]byte, error) {
for k, v := range event {
// // push path field
result = append(result, 0) // push map type
result = binary.BigEndian.AppendUint32(result, uint32(len(k))) // length of the key
result = append(result, []byte(k)...) // key
var err error = nil
result, err = encodeValueRecursive(v, result)
if err != nil {
return result, err
}
// pop index
result = append(result, 2) // pop path index
}
return result, nil
}
func encodeListEvent(log []interface{}, result []byte) ([]byte, error) {
for idx, v := range log {
// push path field
result = append(result, 1) // push index
result = binary.BigEndian.AppendUint32(result, uint32(idx)) // index
var err error = nil
result, err = encodeValueRecursive(v, result)
if err != nil {
return result, err
}
// pop index
result = append(result, 2) // pop path index
}
return result, nil
}
func decodeMatchResponse(result *ScanResult, buf *bytes.Buffer) {
// starts with a rule ID
ruleIdx := binary.BigEndian.Uint32(buf.Next(4))
// then a path
path := nextString(buf)
// then a replacement type
// TODO(remy): implement me
//replacementType := nextString(buf)
nextString(buf)
startIndex := binary.BigEndian.Uint32(buf.Next(4))
endIndexExclusive := binary.BigEndian.Uint32(buf.Next(4))
shiftOffset := int32(binary.BigEndian.Uint32(buf.Next(4)))
result.Matches = append(result.Matches, RuleMatch{
RuleIdx: ruleIdx,
Path: string(path),
StartIndex: startIndex,
EndIndexExclusive: endIndexExclusive,
ShiftOffset: shiftOffset,
})
}
func decodeEventMapResponse(rawData []byte, event map[string]interface{}) (ScanResult, error) {
buf := bytes.NewBuffer(rawData)
var result ScanResult
for buf.Len() > 0 {
typ, err := buf.ReadByte()
if err != nil {
return ScanResult{}, fmt.Errorf("decodeEventMapResponse: %v", err)
}
switch typ {
case 4: // Mutation
result.Mutated = true
if result.Event, err = applyStringMutationMap(buf, event); err != nil {
return ScanResult{}, fmt.Errorf("applyStringMutationMap: %v", err)
}
case 5: // Match
decodeMatchResponse(&result, buf)
default:
return ScanResult{}, fmt.Errorf("decodeEventMapResponse: can't decode response, unknown byte marker: %x", typ)
}
}
return result, nil
}
// decodeResponse reads the binary response returned by the SDS shared library
// on a `scan` call.
func decodeResponse(rawData []byte) (ScanResult, error) {
buf := bytes.NewBuffer(rawData)
var result ScanResult
for buf.Len() > 0 {
typ, err := buf.ReadByte()
if err != nil {
return ScanResult{}, fmt.Errorf("decodeResponse: %v", err)
}
switch typ {
case 4: // Mutation
result.Mutated = true
if result.Event, err = decodeMutation(buf); err != nil {
return ScanResult{}, fmt.Errorf("decodeResponse: %v", err)
}
case 5: // Match
decodeMatchResponse(&result, buf)
default:
return ScanResult{}, fmt.Errorf("decodeResponse: can't decode response, unknown byte marker: %x", typ)
}
}
return result, nil
}
// nextString using this format:
// * 8 bytes: string size
// * string size: the string
// This method DO NOT copy data around but re-use the underlying slicebuffer instead.
// Best usage si to use it after a call to `GoBytes` which takes care of copying
// the data in the Go world.
func nextString(buf *bytes.Buffer) []byte {
size := binary.BigEndian.Uint32(buf.Next(4))
rv := buf.Next(int(size))
return rv
}
func nextInt(buf *bytes.Buffer) int {
return int(binary.BigEndian.Uint32(buf.Next(4)))
}
func applyStringMutationMap(buf *bytes.Buffer, event map[string]interface{}) ([]byte, error) {
tag, err := buf.ReadByte()
if err != nil {
return nil, fmt.Errorf("decodeMapMutation: %v", err)
}
return applyStringMutationMapWithTag(buf, event, tag)
}
func applyStringMutationMapWithTag(buf *bytes.Buffer, event map[string]interface{}, tag byte) ([]byte, error) {
if tag != 0 {
return nil, fmt.Errorf("decodeMapMutation: expected path field")
}
fieldName := nextString(buf)
nextTag, err := buf.ReadByte()
if err != nil {
return nil, fmt.Errorf("decodeMapMutation: %v", err)
}
if nextTag == 3 {
// new string value
res := nextString(buf)
// Update the event with the new value.
event[string(fieldName)] = string(res)
return res, nil
} else {
return applyStringMutation(buf, event[string(fieldName)], nextTag)
}
}
func applyStringMutationListWithTag(buf *bytes.Buffer, event []interface{}, tag byte) ([]byte, error) {
if tag != 1 {
return nil, fmt.Errorf("decodeListMutation: expected path index")
}
indexInArray := nextInt(buf)
nextTag, err := buf.ReadByte()
if err != nil {
return nil, fmt.Errorf("decodeListMutation: %v", err)
}
if nextTag == 3 {
// new string value
res := nextString(buf)
// Update the event with the new value.
event[indexInArray] = string(res)
return res, nil
} else {
// rewind 1 byte in buf as marker is used by applyStringMutation
return applyStringMutation(buf, event[indexInArray], nextTag)
}
}
func applyStringMutation(buf *bytes.Buffer, value interface{}, tag byte) ([]byte, error) {
switch reflect.TypeOf(value).Kind() {
case reflect.Map:
return applyStringMutationMapWithTag(buf, value.(map[string]interface{}), tag)
case reflect.Slice:
return applyStringMutationListWithTag(buf, value.([]interface{}), tag)
}
return nil, fmt.Errorf("applyStringMutation: unknown type %T", value)
}
// decodeMutation returns the result of a mutation done by the SDS shared library.
// TODO(remy): only the redacted/processed event is used, implement what's necessary
// to return Path/Segment information.
func decodeMutation(buf *bytes.Buffer) ([]byte, error) {
// first, we will be reading a possibly empty path
// if we see a '0' byte value, we are reading a field
// if we see a '1' byte value, we are reading an index
// if we see a '3' byte value, we are not reading a path anymore, but a content string
// of the possibly redacted event.
done := false
var processed []byte
for !done {
marker, err := buf.ReadByte()
if err != nil {
return nil, fmt.Errorf("decodeMutation: %v", err)
}
switch marker {
case 0:
// reading a field
// TODO(remy): not implemented: use the Path/Segments information
// and return it in the Go bindings Scan call.
nextString(buf)
case 1:
// reading an index
// TODO(remy): not implemented: use the Path/Segments information
// and return it in the Go bindings Scan call.
binary.BigEndian.Uint32(buf.Next(4))
case 3:
// reading content string
processed = nextString(buf)
done = true
}
}
return processed, nil
}