forked from ruiaylin/pgparser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redact.go
163 lines (148 loc) · 4.8 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright 2020 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package log
import (
"context"
"os"
"reflect"
"strings"
"time"
"github.com/cockroachdb/errors"
"github.com/cockroachdb/logtags"
"github.com/cockroachdb/redact"
)
// EditSensitiveData describes how the messages in log entries should
// be edited through the API.
type EditSensitiveData int
const (
invalidEditMode EditSensitiveData = iota
// WithMarkedSensitiveData is the "raw" log with sensitive data markers included.
WithMarkedSensitiveData
// WithFlattenedSensitiveData is the log with markers stripped.
WithFlattenedSensitiveData
// WithoutSensitiveData is the log with the sensitive data redacted.
WithoutSensitiveData
)
// SelectEditMode returns an EditSensitiveData value that's suitable
// for use with NewDecoder depending on client-side desired
// "redact" and "keep redactable" flags.
// (See the documentation for the Logs and LogFile RPCs
// and that of the 'merge-logs' CLI command.)
func SelectEditMode(redact, keepRedactable bool) EditSensitiveData {
editMode := WithMarkedSensitiveData
if redact {
editMode = WithoutSensitiveData
}
if !keepRedactable && !redact {
editMode = WithFlattenedSensitiveData
}
return editMode
}
type redactEditor func(redactablePackage) redactablePackage
func getEditor(editMode EditSensitiveData) redactEditor {
switch editMode {
case WithMarkedSensitiveData:
return func(r redactablePackage) redactablePackage {
if !r.redactable {
r.msg = []byte(redact.EscapeBytes(r.msg))
r.redactable = true
}
return r
}
case WithFlattenedSensitiveData:
return func(r redactablePackage) redactablePackage {
if r.redactable {
r.msg = redact.RedactableBytes(r.msg).StripMarkers()
r.redactable = false
}
return r
}
case WithoutSensitiveData:
return func(r redactablePackage) redactablePackage {
if r.redactable {
r.msg = []byte(redact.RedactableBytes(r.msg).Redact())
} else {
r.msg = redact.RedactedMarker()
r.redactable = true
}
return r
}
case invalidEditMode:
fallthrough
default:
panic(errors.AssertionFailedf("unrecognized mode: %v", editMode))
}
}
// Safe constructs a SafeFormatter / SafeMessager.
// This is obsolete. Use redact.Safe directly.
// TODO(knz): Remove this.
var Safe = redact.Safe
func init() {
// We consider booleans and numeric values to be always safe for
// reporting. A log call can opt out by using redact.Unsafe() around
// a value that would be otherwise considered safe.
redact.RegisterSafeType(reflect.TypeOf(true)) // bool
redact.RegisterSafeType(reflect.TypeOf(123)) // int
redact.RegisterSafeType(reflect.TypeOf(int8(0)))
redact.RegisterSafeType(reflect.TypeOf(int16(0)))
redact.RegisterSafeType(reflect.TypeOf(int32(0)))
redact.RegisterSafeType(reflect.TypeOf(int64(0)))
redact.RegisterSafeType(reflect.TypeOf(uint8(0)))
redact.RegisterSafeType(reflect.TypeOf(uint16(0)))
redact.RegisterSafeType(reflect.TypeOf(uint32(0)))
redact.RegisterSafeType(reflect.TypeOf(uint64(0)))
redact.RegisterSafeType(reflect.TypeOf(float32(0)))
redact.RegisterSafeType(reflect.TypeOf(float64(0)))
redact.RegisterSafeType(reflect.TypeOf(complex64(0)))
redact.RegisterSafeType(reflect.TypeOf(complex128(0)))
// Signal names are also safe for reporting.
redact.RegisterSafeType(reflect.TypeOf(os.Interrupt))
// Times and durations too.
redact.RegisterSafeType(reflect.TypeOf(time.Time{}))
redact.RegisterSafeType(reflect.TypeOf(time.Duration(0)))
}
type redactablePackage struct {
msg []byte
redactable bool
}
const redactableIndicator = "⋮"
var redactableIndicatorBytes = []byte(redactableIndicator)
func redactTags(ctx context.Context, buf *strings.Builder) {
tags := logtags.FromContext(ctx)
if tags == nil {
return
}
comma := ""
for _, t := range tags.Get() {
buf.WriteString(comma)
buf.WriteString(t.Key())
if v := t.Value(); v != nil && v != "" {
if len(t.Key()) > 1 {
buf.WriteByte('=')
}
redact.Fprint(buf, v)
}
comma = ","
}
}
// TestingSetRedactable sets the redactable flag for usage in a test.
// The caller is responsible for calling the cleanup function. This
// is exported for use in tests only -- it causes the logging
// configuration to be at risk of leaking unsafe information due to
// asynchronous direct writes to fd 2 / os.Stderr.
//
// See the discussion on SetupRedactionAndStderrRedirects() for
// details.
func TestingSetRedactable(redactableLogs bool) (cleanup func()) {
prev := mainLog.redactableLogs.Swap(redactableLogs)
return func() {
mainLog.redactableLogs.Set(prev)
}
}