|
| 1 | +package masker |
| 2 | + |
| 3 | +import ( |
| 4 | + "io" |
| 5 | + "time" |
| 6 | +) |
| 7 | + |
| 8 | +// Masker handles the creation and synchronization of streams that have all their writes scanned for secrets and |
| 9 | +// have them redacted if any matches are found. Masking of secrets is a best effort attempt. Output on all streams is |
| 10 | +// buffered to increase the chance of finding secrets if they are spread across multiple writes, but it cannot be |
| 11 | +// guaranteed that these secrets are masked. The duration bytes spend in the buffer is constant. |
| 12 | +// |
| 13 | +// Usage: |
| 14 | +// 1. Create a new Masker using New() |
| 15 | +// 2. Add one more streams using AddStream() |
| 16 | +// 3. Run the Start() method in a separate goroutine |
| 17 | +// 4. After everything has been written to the io.Writers, flush all buffers using Stop() |
| 18 | +type Masker struct { |
| 19 | + bufferDelay time.Duration |
| 20 | + sequences [][]byte |
| 21 | + frames chan frame |
| 22 | + stopChan chan struct{} |
| 23 | + err error |
| 24 | +} |
| 25 | + |
| 26 | +// Options for configuring masking behavior. |
| 27 | +type Options struct { |
| 28 | + // DisableBuffer completely disables the buffering of the masker. This increases output responsiveness |
| 29 | + // but also increases the chance of a secret not being masked. |
| 30 | + DisableBuffer bool |
| 31 | + |
| 32 | + // BufferDelay is the constant duration for which input to a stream is buffered. A higher value increases |
| 33 | + // the chance of secrets being detected for masking. Especially when writes have a variable delay between them, |
| 34 | + // for example in the case data arrives over an unstable network connection. |
| 35 | + // Defaults to 50ms if not set. |
| 36 | + BufferDelay time.Duration |
| 37 | + |
| 38 | + // FrameBufferLength is the number of frames that can be in the buffer simultaneously. |
| 39 | + // If the frame buffer is full, writing to a stream blocks until there is space. |
| 40 | + FrameBufferLength int |
| 41 | +} |
| 42 | + |
| 43 | +// New creates a new Masker that scans all streams for the given sequences and masks them. |
| 44 | +func New(sequences [][]byte, opts *Options) *Masker { |
| 45 | + masker := &Masker{ |
| 46 | + bufferDelay: time.Millisecond * 50, |
| 47 | + sequences: sequences, |
| 48 | + stopChan: make(chan struct{}), |
| 49 | + } |
| 50 | + frameChanlength := 1024 |
| 51 | + if opts != nil { |
| 52 | + if opts.DisableBuffer { |
| 53 | + masker.bufferDelay = 0 |
| 54 | + frameChanlength = 0 |
| 55 | + } else { |
| 56 | + if opts.BufferDelay > 0 { |
| 57 | + masker.bufferDelay = opts.BufferDelay |
| 58 | + } |
| 59 | + if opts.FrameBufferLength > 0 { |
| 60 | + frameChanlength = opts.FrameBufferLength |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + } |
| 65 | + masker.frames = make(chan frame, frameChanlength) |
| 66 | + |
| 67 | + return masker |
| 68 | +} |
| 69 | + |
| 70 | +// AddStream takes in an io.Writer to mask secrets on and returns an io.Writer that has secrets on its output masked. |
| 71 | +func (m *Masker) AddStream(w io.Writer) io.Writer { |
| 72 | + s := stream{ |
| 73 | + dest: w, |
| 74 | + registerFrame: m.registerFrame, |
| 75 | + matches: matches{}, |
| 76 | + matcher: newMatcher(m.sequences), |
| 77 | + } |
| 78 | + return &s |
| 79 | +} |
| 80 | + |
| 81 | +// Start continuously flushes the input buffer for each frame for which the buffer delay has passed. |
| 82 | +// This method blocks until Stop() is called. |
| 83 | +func (m *Masker) Start() { |
| 84 | + for { |
| 85 | + select { |
| 86 | + case <-m.stopChan: |
| 87 | + for t := range m.frames { |
| 88 | + err := t.stream.flush(t.length) |
| 89 | + if err != nil { |
| 90 | + m.handleErr(err) |
| 91 | + } |
| 92 | + } |
| 93 | + m.stopChan <- struct{}{} |
| 94 | + return |
| 95 | + case trigger := <-m.frames: |
| 96 | + <-trigger.timer.C |
| 97 | + |
| 98 | + err := trigger.stream.flush(trigger.length) |
| 99 | + if err != nil { |
| 100 | + m.handleErr(err) |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +// Stop all pending frames and wait for this to complete. |
| 107 | +// This should be run after all input has been written to the io.Writers of the streams. |
| 108 | +// Calling Write() on a stream after calling Stop() will lead to a panic. |
| 109 | +func (m *Masker) Stop() error { |
| 110 | + m.stopChan <- struct{}{} |
| 111 | + close(m.frames) |
| 112 | + <-m.stopChan |
| 113 | + |
| 114 | + return m.err |
| 115 | +} |
| 116 | + |
| 117 | +// registerFrame adds a new frame to the frames channel with a timeout of bufferDelay plus the given offset. |
| 118 | +// After this timer has passed, the frame will be flushed to the output. |
| 119 | +func (m *Masker) registerFrame(s *stream, offset time.Duration, l int) { |
| 120 | + m.frames <- frame{ |
| 121 | + length: l, |
| 122 | + stream: s, |
| 123 | + timer: time.NewTimer(offset + m.bufferDelay), |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +func (m *Masker) handleErr(err error) { |
| 128 | + if err != nil && m.err == nil { |
| 129 | + m.err = err |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +// frame represent a set of bytes in the buffer of a stream that were written in a single call of Write(). |
| 134 | +// The bytes are written to the destination after the timer has expired. |
| 135 | +type frame struct { |
| 136 | + length int |
| 137 | + stream *stream |
| 138 | + timer *time.Timer |
| 139 | +} |
0 commit comments