Documentation
¶
Index ¶
- func ContextWithRequest(ctx context.Context, req *Request) context.Context
- func ContextWithRequestType(ctx context.Context, reqType RequestType) context.Context
- func Init() error
- func LogPartnerRequest(log *Request)
- func LogPromoCodeEvent(fields map[string]any)
- func LogRequest(log *Request)
- func LogUltronEx(msg *UltronExMsg)
- func MaskFormURLEncoded(form string, maskChar string, toMasks []MaskData) string
- func MaskJSON(json, maskChar string, toMasks []MaskData) string
- func MaskQueryParams(rawQueryParams, maskChar string, toMasks []MaskData) string
- func MaskXML(xml, maskChar string, toMasks []MaskData) string
- func RedactFormURLEncoded(form string, replacement string, keys []string) string
- func RedactJSON(json, replacement string, keys [][]string) string
- func RedactQueryParams(rawQueryParams, replacement string, sensitiveParams []string) string
- func RedactXML(xml, replacement string, tags []string) string
- func Sync()
- type Headers
- type MaskData
- type MaskRestrictionType
- type Request
- type RequestType
- type UltronExMsg
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ContextWithRequest ¶
ContextWithRequest returns a new context from a parent context with request added into it
func ContextWithRequestType ¶
func ContextWithRequestType(ctx context.Context, reqType RequestType) context.Context
ContextWithRequestType returns a new context from a parent context with request type added into it
func LogPartnerRequest ¶
func LogPartnerRequest(log *Request)
LogPartnerRequest logs a partner request to local file
func LogPromoCodeEvent ¶ added in v0.3.8
LogPromoCodeEvent logs a promo-code event to a local file. The event is logged as a flat JSON object (key/value pairs).
func MaskFormURLEncoded ¶ added in v0.3.1
MaskFormURLEncoded mask parts of the key paths values from the input form encoded string with replacement
Example ¶
formData := url.Values{
"field1": []string{"field1value1", "field1value2"},
"field2": []string{"field2value1"},
"field3": []string{"sensitive_data"},
"field4.nested.data": []string{"data"},
}
input := formData.Encode()
maskData := []MaskData{
{
JSONKeys: []string{"field1"},
FirstCharsToShow: 2,
LastCharsToShow: 2,
KeepSameLength: false,
},
{
JSONKeys: []string{"field3"},
FirstCharsToShow: 0,
LastCharsToShow: 0,
KeepSameLength: true,
},
{
JSONKeys: []string{"field4.nested.data"},
FirstCharsToShow: 0,
LastCharsToShow: 0,
KeepSameLength: true,
},
}
output := MaskFormURLEncoded(input, "*", maskData)
_, _ = fmt.Println(output)
Output: field1=fi%2Ae1&field1=fi%2Ae2&field2=field2value1&field3=%2A%2A%2A%2A%2A%2A%2A%2A%2A%2A%2A%2A%2A%2A&field4.nested.data=%2A%2A%2A%2A
func MaskJSON ¶ added in v0.1.15
MaskJSON mask parts of the json key paths value from the input json with replacement
For nested arrays, use `[]` as the key.
Example ¶
maskData := []MaskData{
{
JSONKeys: []string{"first"},
FirstCharsToShow: 3,
LastCharsToShow: 6,
KeepSameLength: true,
},
{
JSONKeys: []string{"second", "second"},
FirstCharsToShow: 2,
LastCharsToShow: 3,
CharsToIgnore: []rune{'@'},
RestrictionType: MaskRestrictionTypeEmail,
KeepSameLength: true,
},
{
JSONKeys: []string{"second", "third", "first"},
FirstCharsToShow: 3,
LastCharsToShow: 1,
KeepSameLength: true,
},
{
JSONKeys: []string{"second", "fourth", "[]", "email", "value"},
FirstCharsToShow: 1,
LastCharsToShow: 3,
CharsToIgnore: []rune{'@'},
KeepSameLength: true,
},
}
input := `
{
"first": "first value",
"second": {
"first": "1st of second",
"second": "not-an-email.com",
"third": {
"first": "1st of second third",
"second": "2nd of second third",
"third": "3rd of second third"
},
"fourth": [
{ "email": { "value": "first@email.com" } },
{ "email": { "value": "second@email.com" } }
]
}
}
`
output := MaskJSON(input, "*", maskData)
_, _ = fmt.Println(output)
Output: {"first":"fir** value","second":{"first":"1st of second","second":"not-an-email.com","third":{"first":"1st***************d","second":"2nd of second third","third":"3rd of second third"},"fourth":[{"email":{"value":"f****@******com"}},{"email":{"value":"s*****@******com"}}]}}
func MaskQueryParams ¶ added in v0.3.6
MaskQueryParams masks sensitive query parameters in the URL with replacement
func MaskXML ¶ added in v0.1.15
MaskXML masks parts of the inner text of tags from the input XML with replacement
func RedactFormURLEncoded ¶ added in v0.3.1
RedactFormURLEncoded replaces value of keys from the input form encoded string with replacement or defaultReplacement when replacement is empty.
Since input is form encoded string, keys would just be a simple array/list of keys to be redacted.
Example ¶
keys := []string{
"field1",
"field3",
"field4.nested.data",
}
formData := url.Values{
"field1": []string{"field1value1", "field1value2"},
"field2": []string{"field2value1"},
"field3": []string{"sensitive_data"},
"field4.nested.data": []string{"data"},
}
input := formData.Encode()
output := RedactFormURLEncoded(input, "Wego", keys)
_, _ = fmt.Println(output)
Output: field1=Wego&field1=Wego&field2=field2value1&field3=Wego&field4.nested.data=Wego
func RedactJSON ¶
RedactJSON replaces value of key paths from the input JSON with replacement or defaultReplacement when replacement is empty.
For nested arrays, use `[]` as the key.
Example ¶
keys := [][]string{
{"first"},
{"second", "second"},
{"second", "third", "first"},
{"third", "[]", "value"},
}
input := `
{
"first": "first value",
"second": {
"first": "1st of second",
"second": "2nd of second",
"third": {
"first": "1st of second third",
"second": "2nd of second third",
"third": "3rd of second third"
}
},
"third": [
{ "value": "third value" }
]
}
`
output := RedactJSON(input, "Wego", keys)
_, _ = fmt.Println(output)
Output: {"first":"Wego","second":{"first":"1st of second","second":"Wego","third":{"first":"Wego","second":"2nd of second third","third":"3rd of second third"}},"third":[{"value":"Wego"}]}
func RedactQueryParams ¶ added in v0.3.6
RedactQueryParams replaces sensitive query parameters in the URL
Types ¶
type Headers ¶
Headers ...
func (Headers) MarshalLogArray ¶
func (h Headers) MarshalLogArray(enc zapcore.ArrayEncoder) error
MarshalLogArray marshal Headers to zap log array Need to implement this to log it with zap.Array
type MaskData ¶ added in v0.1.15
type MaskData struct {
FirstCharsToShow int
LastCharsToShow int
RestrictionType MaskRestrictionType
CharsToIgnore []rune
XMLTag string
JSONKeys []string
KeepSameLength bool
// contains filtered or unexported fields
}
MaskData the data as well as the information on how to mask the data
type MaskRestrictionType ¶ added in v0.1.15
type MaskRestrictionType string
MaskRestrictionType the type of text to mask, will only mask if text is of the specified type
const ( // MaskRestrictionTypeEmail will only mask email text MaskRestrictionTypeEmail MaskRestrictionType = "email" )
type Request ¶
type Request struct {
Type RequestType
Basics common.Basics
Method string
URL string
RequestHeaders Headers
RequestBody string
IP string
StatusCode int32
ResponseHeaders Headers
ResponseBody string
RequestedAt time.Time
Duration time.Duration
Error error
}
Request contains general information of a request
func RequestFromContext ¶
RequestFromContext gets the request from context
type RequestType ¶
type RequestType string
RequestType ...
func RequestTypeFromContext ¶
func RequestTypeFromContext(ctx context.Context) (reqType RequestType)
RequestTypeFromContext gets the request type from context
type UltronExMsg ¶
UltronExMsg ...
func (UltronExMsg) MarshalLogObject ¶
func (m UltronExMsg) MarshalLogObject(enc zapcore.ObjectEncoder) error
MarshalLogObject marshal UltronExMsg to zap log object The struct need to implement this, so we can log it with zap.Object