-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer.go
170 lines (137 loc) · 2.99 KB
/
buffer.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
170
package trace
import (
"bufio"
"bytes"
"fmt"
"io"
"sync"
"github.com/markelog/trie"
"gitlab.com/gitlab-org/gitlab-runner/helpers"
)
const maskedText = "[MASKED]"
const defaultBytesLimit = 4 * 1024 * 1024 // 4MB
type Buffer struct {
writer io.WriteCloser
lock sync.RWMutex
log bytes.Buffer
logMaskedSize int
bytesLimit int
finish chan struct{}
maskTree *trie.Trie
}
func (b *Buffer) SetMasked(values []string) {
if len(values) == 0 {
b.maskTree = nil
return
}
maskTree := trie.New()
for _, value := range values {
maskTree.Add(value, nil)
}
b.maskTree = maskTree
}
func (b *Buffer) SetLimit(size int) {
b.bytesLimit = size
}
func (b *Buffer) limitExceededMessage() string {
return fmt.Sprintf("\n%sJob's log exceeded limit of %v bytes.%s\n", helpers.ANSI_BOLD_RED, b.bytesLimit, helpers.ANSI_RESET)
}
func (b *Buffer) Bytes() []byte {
b.lock.RLock()
defer b.lock.RUnlock()
return b.log.Bytes()[0:b.logMaskedSize]
}
func (b *Buffer) String() string {
return string(b.Bytes())
}
func (b *Buffer) Write(data []byte) (n int, err error) {
return b.writer.Write(data)
}
func (b *Buffer) Close() error {
// wait for trace to finish
err := b.writer.Close()
<-b.finish
return err
}
func (b *Buffer) advanceAllUnsafe() {
b.logMaskedSize = b.log.Len()
}
func (b *Buffer) advanceAll() {
b.lock.Lock()
defer b.lock.Unlock()
b.advanceAllUnsafe()
}
// advanceLogUnsafe is assumed to be run every character
func (b *Buffer) advanceLogUnsafe() error {
// advance all if no masking is enabled
if b.maskTree == nil {
b.advanceAllUnsafe()
return nil
}
rest := string(b.log.Bytes()[b.logMaskedSize:])
results := b.maskTree.Search(rest)
if len(results) == 0 {
// we can advance as no match was found
b.advanceAllUnsafe()
return nil
}
// full match was found
if len(results) == 1 && results[0].Key == rest {
b.log.Truncate(b.logMaskedSize)
b.log.WriteString(maskedText)
b.advanceAllUnsafe()
}
// partial match, wait for more characters
return nil
}
func (b *Buffer) writeRune(r rune) (int, error) {
b.lock.Lock()
defer b.lock.Unlock()
n, err := b.log.WriteRune(r)
if err != nil {
return n, err
}
err = b.advanceLogUnsafe()
if err != nil {
return n, err
}
if b.log.Len() < b.bytesLimit {
return n, nil
}
b.log.WriteString(b.limitExceededMessage())
return n, io.EOF
}
func (b *Buffer) process(pipe *io.PipeReader) {
defer pipe.Close()
stopped := false
reader := bufio.NewReader(pipe)
for {
r, s, err := reader.ReadRune()
if s <= 0 {
break
} else if stopped {
// ignore symbols if job log exceeded limit
continue
} else if err == nil {
_, err = b.writeRune(r)
if err == io.EOF {
stopped = true
}
} else {
// ignore invalid characters
continue
}
}
b.advanceAll()
close(b.finish)
}
func New() *Buffer {
reader, writer := io.Pipe()
buffer := &Buffer{
writer: writer,
bytesLimit: defaultBytesLimit,
finish: make(chan struct{}),
}
go buffer.process(reader)
return buffer
}