This repository has been archived by the owner on Mar 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcred-filter.go
169 lines (130 loc) · 2.98 KB
/
cred-filter.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
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"log"
"os"
"sort"
"strings"
)
func main() {
source := os.Stdin
destination := NewLineWriter(os.Stdout)
if len(os.Args) > 1 && os.Args[1] == "-stderr" {
destination = NewLineWriter(os.Stderr)
}
defer destination.Close()
redacted, maxSize := RedactedList()
err := Stream(source, destination, redacted, maxSize)
if err != nil {
log.Fatal(err)
}
}
type LineWriter struct {
buffer []byte
writer io.Writer
}
func NewLineWriter(writer io.Writer) *LineWriter {
return &LineWriter{
buffer: []byte{},
writer: writer,
}
}
func (lw *LineWriter) Write(p []byte) (int, error) {
lw.buffer = append(lw.buffer, p...)
index := bytes.IndexRune(lw.buffer, '\n')
if index != -1 {
_, err := lw.writer.Write(lw.buffer[:index+1])
if err != nil {
return 0, err
}
lw.buffer = lw.buffer[index+1:]
}
return len(p), nil
}
func (lw *LineWriter) Close() error {
_, err := lw.writer.Write(lw.buffer)
if err != nil {
return err
}
return nil
}
type RedactedVariable struct {
Name string
Value []byte
}
func RedactedList() ([]RedactedVariable, int) {
whiteList := map[string]struct{}{}
for _, value := range strings.Split(os.Getenv("CREDENTIAL_FILTER_WHITELIST"), ",") {
whiteList[value] = struct{}{}
}
var redacted []RedactedVariable
for _, variable := range os.Environ() {
pair := strings.Split(variable, "=")
if pair[1] == "" {
continue
}
if _, ok := whiteList[pair[0]]; ok {
continue
}
redacted = append(redacted, RedactedVariable{
Name: pair[0],
Value: []byte(pair[1]),
})
}
sort.Slice(redacted, func(i, j int) bool {
return len(redacted[i].Value) > len(redacted[j].Value)
})
var maxSize int
if len(redacted) > 0 {
maxSize = len(redacted[0].Value)
}
return redacted, maxSize
}
func Stream(source io.Reader, destination io.WriteCloser, redacted []RedactedVariable, maxSize int) error {
if maxSize == 0 {
_, err := io.Copy(destination, source)
if err != nil {
return fmt.Errorf("failed to copy source to destination: %w", err)
}
return nil
}
reader := bufio.NewReader(source)
preview, err := reader.Peek(maxSize)
if err != nil && err != io.EOF {
return fmt.Errorf("failed to preview source: %w", err)
}
for {
var match bool
for _, r := range redacted {
if bytes.HasPrefix(preview, r.Value) {
_, err = reader.Discard(len(r.Value))
if err != nil {
return fmt.Errorf("failed to discard from source: %w", err)
}
fmt.Fprintf(destination, "[redacted %s]", r.Name)
match = true
break
}
}
if !match {
b, err := reader.ReadByte()
if err != nil {
if err == io.EOF {
return nil
}
return fmt.Errorf("failed to read byte from source: %w", err)
}
_, err = destination.Write([]byte{b})
if err != nil {
return fmt.Errorf("failed to write byte to destination: %w", err)
}
}
preview, err = reader.Peek(maxSize)
if err != nil && err != io.EOF {
return fmt.Errorf("failed to preview source: %w", err)
}
}
}