-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathredactions.go
52 lines (45 loc) · 1.41 KB
/
redactions.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
package pqstream
import (
"encoding/json"
"strings"
"github.com/tmc/pqstream/pqs"
)
// FieldRedactions describes how redaction fields are specified.
// Top level map key is the schema, inner map key is the table and slice is the fields to redact.
type FieldRedactions map[string]map[string][]string
// DecodeRedactions returns a FieldRedactions map decoded from redactions specified in json format.
func DecodeRedactions(r string) (FieldRedactions, error) {
rfields := make(FieldRedactions)
if err := json.NewDecoder(strings.NewReader(r)).Decode(&rfields); err != nil {
return nil, err
}
return rfields, nil
}
// WithFieldRedactions controls which fields are redacted from the feed.
func WithFieldRedactions(r FieldRedactions) ServerOption {
return func(s *Server) {
s.redactions = r
}
}
// redactFields search through redactionMap if there's any redacted fields
// specified that match the fields of the current event.
func (s *Server) redactFields(e *pqs.RawEvent) {
if tables, ok := s.redactions[e.GetSchema()]; ok {
if fields, ok := tables[e.GetTable()]; ok {
for _, rf := range fields {
if e.Payload != nil {
if _, ok := e.Payload.Fields[rf]; ok {
//remove field from payload
delete(e.Payload.Fields, rf)
}
}
if e.Previous != nil {
if _, ok := e.Previous.Fields[rf]; ok {
//remove field from previous payload
delete(e.Previous.Fields, rf)
}
}
}
}
}
}