This repository has been archived by the owner on Jul 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redact.go
146 lines (125 loc) · 2.98 KB
/
redact.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
package redact
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"reflect"
"github.com/gorilla/schema"
"github.com/sirupsen/logrus"
)
func redactStructFields(obj interface{}) {
val := reflect.ValueOf(obj).Elem()
typ := val.Type()
for i := 0; i < typ.NumField(); i++ {
field := typ.Field(i)
tagVal, tagExists := field.Tag.Lookup("redact")
switch field.Type.Kind() {
case reflect.Pointer:
if tagExists {
if field.Type.String() == "*string" {
if !val.Field(i).IsNil() {
val.Field(i).Set(reflect.ValueOf(&tagVal))
}
}
}
case reflect.String:
if tagExists {
val.Field(i).SetString(tagVal)
}
case reflect.Struct:
redactStructFields(val.Field(i).Addr().Interface())
case reflect.Slice:
if field.Type.Elem().Kind() == reflect.Struct {
slice := val.Field(i)
for j := 0; j < slice.Len(); j++ {
elem := slice.Index(j).Addr().Interface()
redactStructFields(elem)
}
}
}
}
}
func redactFormFields(obj interface{}) {
val := reflect.ValueOf(obj).Elem()
typ := val.Type()
for i := 0; i < typ.NumField(); i++ {
field := typ.Field(i)
tagVal, tagExists := field.Tag.Lookup("redact")
switch field.Type.Kind() {
case reflect.Pointer:
if tagExists {
if field.Type.String() == "*string" {
if !val.Field(i).IsNil() {
val.Field(i).Set(reflect.ValueOf(&tagVal))
}
}
}
case reflect.String:
if tagExists {
val.Field(i).SetString(tagVal)
}
}
}
}
func redactHeaderField(k string, redactList *[]string) bool {
for _, redactionKey := range *redactList {
if k == redactionKey {
return true
}
}
return false
}
func RedactForm[T any](target *T, querystring string) interface{} {
form, err := url.ParseQuery(querystring)
if err != nil {
return err.Error()
}
decoder := schema.NewDecoder()
decoder.IgnoreUnknownKeys(true)
if err := decoder.Decode(target, form); err != nil {
return err.Error()
}
redactFormFields(target)
return target
}
func Unmarshal(data []byte, v any) interface{} {
var bMap map[string]interface{}
if err := json.Unmarshal(data, &v); err != nil {
if err := json.Unmarshal(data, &bMap); err != nil {
return fmt.Sprint(bMap)
}
}
redactStructFields(v)
return v
}
func Redact(v any) interface{} {
payloadBytes, err := json.Marshal(v)
if err != nil {
return v
}
var bMap map[string]interface{}
if err := json.Unmarshal(payloadBytes, &v); err != nil {
if err := json.Unmarshal(payloadBytes, &bMap); err != nil {
return fmt.Sprint(bMap)
}
}
redactStructFields(v)
return v
}
func RedactHttpHeader(header *http.Header, supplimentaryRedactionList *[]string) *http.Header {
redactList := []string{"Authorization"}
if supplimentaryRedactionList != nil {
redactList = append(redactList, *supplimentaryRedactionList...)
}
redactedRequest := header.Clone()
for k := range *header {
if redactHeaderField(k, &redactList) {
redactedRequest.Set(k, "REDACTED")
}
}
return &redactedRequest
}
type FieldLogger interface {
logrus.FieldLogger
}