forked from Azure/azure-sdk-for-go
/
sanitizer.go
458 lines (403 loc) · 14 KB
/
sanitizer.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
//go:build go1.18
// +build go1.18
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package recording
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"github.com/dnaeon/go-vcr/cassette"
"github.com/dnaeon/go-vcr/recorder"
)
type Sanitizer struct {
recorder *recorder.Recorder
headersToSanitize []string
urlSanitizer StringSanitizer
bodySanitizer StringSanitizer
}
// StringSanitizer is a func that will modify the string pointed to by the parameter into a sanitized value.
type StringSanitizer func(*string)
// SanitizedValue is the default placeholder value to be used for sanitized strings.
const SanitizedValue string = "sanitized"
// SanitizedBase64Value is the default placeholder value to be used for sanitized base-64 encoded strings.
const SanitizedBase64Value string = "Kg=="
var sanitizedValueSlice = []string{SanitizedValue}
// defaultSanitizer returns a new RecordingSanitizer with the default sanitizing behavior.
// To customize sanitization, call AddSanitizedHeaders, AddBodySanitizer, or AddUrlSanitizer.
func defaultSanitizer(recorder *recorder.Recorder) *Sanitizer {
// The default sanitizer sanitizes the Authorization header
s := &Sanitizer{headersToSanitize: []string{"Authorization"}, recorder: recorder, urlSanitizer: DefaultStringSanitizer, bodySanitizer: DefaultStringSanitizer}
recorder.AddSaveFilter(s.applySaveFilter)
return s
}
// AddSanitizedHeaders adds the supplied header names to the list of headers to be sanitized on request and response recordings.
func (s *Sanitizer) AddSanitizedHeaders(headers ...string) {
s.headersToSanitize = append(s.headersToSanitize, headers...)
}
// AddBodysanitizer configures the supplied StringSanitizer to sanitize recording request and response bodies
func (s *Sanitizer) AddBodysanitizer(sanitizer StringSanitizer) {
s.bodySanitizer = sanitizer
}
// AddUriSanitizer configures the supplied StringSanitizer to sanitize recording request and response URLs
func (s *Sanitizer) AddUrlSanitizer(sanitizer StringSanitizer) {
s.urlSanitizer = sanitizer
}
func (s *Sanitizer) sanitizeHeaders(header http.Header) {
for _, headerName := range s.headersToSanitize {
if _, ok := header[headerName]; ok {
header[headerName] = sanitizedValueSlice
}
}
}
func (s *Sanitizer) sanitizeBodies(body *string) {
s.bodySanitizer(body)
}
func (s *Sanitizer) sanitizeURL(url *string) {
s.urlSanitizer(url)
}
func (s *Sanitizer) applySaveFilter(i *cassette.Interaction) error {
s.sanitizeHeaders(i.Request.Headers)
s.sanitizeHeaders(i.Response.Headers)
s.sanitizeURL(&i.Request.URL)
if len(i.Request.Body) > 0 {
s.sanitizeBodies(&i.Request.Body)
}
if len(i.Response.Body) > 0 {
s.sanitizeBodies(&i.Response.Body)
}
return nil
}
func DefaultStringSanitizer(s *string) {}
func handleProxyResponse(resp *http.Response, err error) error {
if err != nil {
return err
}
if resp.StatusCode == http.StatusAccepted || resp.StatusCode == http.StatusOK {
return nil
}
body, err := io.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return err
}
return fmt.Errorf("there was an error communicating with the test proxy: %s", body)
}
// handleTestLevelSanitizer sets the "x-recording-id" header if options.TestInstance is not nil
func handleTestLevelSanitizer(req *http.Request, options *RecordingOptions) {
if options == nil || options.TestInstance == nil {
return
}
if recordingID := GetRecordingId(options.TestInstance); recordingID != "" {
req.Header.Set(IDHeader, recordingID)
}
}
// AddBodyKeySanitizer adds a sanitizer for JSON Bodies. jsonPath is the path to the key, value
// is the value to replace with, and regex is the string to match in the body. If your regex includes a group
// options.GroupForReplace specifies which group to replace
func AddBodyKeySanitizer(jsonPath, value, regex string, options *RecordingOptions) error {
if recordMode == LiveMode {
return nil
}
if options == nil {
options = defaultOptions()
}
url := fmt.Sprintf("%s/Admin/AddSanitizer", options.baseURL())
req, err := http.NewRequest("POST", url, nil)
if err != nil {
return err
}
req.Header.Set("x-abstraction-identifier", "BodyKeySanitizer")
handleTestLevelSanitizer(req, options)
marshalled, err := json.MarshalIndent(struct {
JSONPath string `json:"jsonPath"`
Value string `json:"value,omitempty"`
Regex string `json:"regex,omitempty"`
GroupForReplace string `json:"groupForReplace,omitempty"`
}{
JSONPath: jsonPath,
Value: value,
Regex: regex,
GroupForReplace: options.GroupForReplace,
}, "", "")
if err != nil {
return err
}
req.Body = io.NopCloser(bytes.NewReader(marshalled))
req.ContentLength = int64(len(marshalled))
return handleProxyResponse(client.Do(req))
}
// AddBodyRegexSanitizer offers regex replace within a returned JSON body. value is the
// substitution value, regex can be a simple regex or a substitution operation if
// options.GroupForReplace is set.
func AddBodyRegexSanitizer(value, regex string, options *RecordingOptions) error {
if recordMode == LiveMode {
return nil
}
if options == nil {
options = defaultOptions()
}
url := fmt.Sprintf("%s/Admin/AddSanitizer", options.baseURL())
req, err := http.NewRequest("POST", url, nil)
if err != nil {
return err
}
req.Header.Set("x-abstraction-identifier", "BodyRegexSanitizer")
handleTestLevelSanitizer(req, options)
marshalled, err := json.MarshalIndent(struct {
Value string `json:"value"`
Regex string `json:"regex,omitempty"`
GroupForReplace string `json:"groupForReplace,omitempty"`
}{
Value: value,
Regex: regex,
GroupForReplace: options.GroupForReplace,
}, "", "")
if err != nil {
return err
}
req.Body = io.NopCloser(bytes.NewReader(marshalled))
req.ContentLength = int64(len(marshalled))
return handleProxyResponse(client.Do(req))
}
// AddContinuationSanitizer is used to anonymize private keys in response/request pairs.
// key: the name of the header whos value will be replaced from response -> next request
// method: the method by which the value of the targeted key will be replaced. Defaults to GUID replacement
// resetAfterFirt: Do we need multiple pairs replaced? Or do we want to replace each value with the same value.
func AddContinuationSanitizer(key, method string, resetAfterFirst bool, options *RecordingOptions) error {
if recordMode == LiveMode {
return nil
}
if options == nil {
options = defaultOptions()
}
url := fmt.Sprintf("%s/Admin/AddSanitizer", options.baseURL())
req, err := http.NewRequest("POST", url, nil)
if err != nil {
return err
}
req.Header.Set("x-abstraction-identifier", "ContinuationSanitizer")
handleTestLevelSanitizer(req, options)
marshalled, err := json.MarshalIndent(struct {
Key string `json:"key"`
Method string `json:"method"`
ResetAfterFirst string `json:"resetAfterFirst"`
}{
Key: key,
Method: method,
ResetAfterFirst: fmt.Sprintf("%v", resetAfterFirst),
}, "", "")
if err != nil {
return err
}
req.Body = io.NopCloser(bytes.NewReader(marshalled))
req.ContentLength = int64(len(marshalled))
return handleProxyResponse(client.Do(req))
}
// AddGeneralRegexSanitizer adds a general regex across request/response Body, Headers, and URI.
// value is the substitution value, regex can be defined as a simple regex replace or a substition
// operation if options.GroupForReplace specifies which group to replace.
func AddGeneralRegexSanitizer(value, regex string, options *RecordingOptions) error {
if recordMode == LiveMode {
return nil
}
if options == nil {
options = defaultOptions()
}
url := fmt.Sprintf("%s/Admin/AddSanitizer", options.baseURL())
req, err := http.NewRequest("POST", url, nil)
if err != nil {
return err
}
req.Header.Set("x-abstraction-identifier", "GeneralRegexSanitizer")
handleTestLevelSanitizer(req, options)
marshalled, err := json.MarshalIndent(struct {
Value string `json:"value"`
Regex string `json:"regex"`
GroupForReplace string `json:"groupForReplace,omitempty"`
}{
Value: value,
Regex: regex,
GroupForReplace: options.GroupForReplace,
}, "", "")
if err != nil {
return err
}
req.Body = io.NopCloser(bytes.NewReader(marshalled))
req.ContentLength = int64(len(marshalled))
return handleProxyResponse(client.Do(req))
}
// AddHeaderRegexSanitizer can be used to replace a key with a specific value: set regex to ""
// OR can be used to do a simple regex replace operation by setting key, value, and regex.
// OR To do a substitution operation if options.GroupForReplace is set.
// key is the name of the header to operate against. value is the substitution or whole new header
// value. regex can be defined as a simple regex replace or a substitution operation if
// options.GroupForReplace is set.
func AddHeaderRegexSanitizer(key, value, regex string, options *RecordingOptions) error {
if recordMode == LiveMode {
return nil
}
if options == nil {
options = defaultOptions()
}
url := fmt.Sprintf("%s/Admin/AddSanitizer", options.baseURL())
req, err := http.NewRequest("POST", url, nil)
if err != nil {
return err
}
req.Header.Set("x-abstraction-identifier", "HeaderRegexSanitizer")
handleTestLevelSanitizer(req, options)
marshalled, err := json.MarshalIndent(struct {
Key string `json:"key"`
Value string `json:"value,omitempty"`
Regex string `json:"regex,omitempty"`
GroupForReplace string `json:"groupForReplace,omitempty"`
}{
Key: key,
Value: value,
Regex: regex,
GroupForReplace: options.GroupForReplace,
}, "", "")
if err != nil {
return err
}
req.Body = io.NopCloser(bytes.NewReader(marshalled))
req.ContentLength = int64(len(marshalled))
return handleProxyResponse(client.Do(req))
}
// AddOAuthResponseSanitizer cleans all request/response pairs taht match an oauth regex in their URI
func AddOAuthResponseSanitizer(options *RecordingOptions) error {
if recordMode == LiveMode {
return nil
}
if options == nil {
options = defaultOptions()
}
url := fmt.Sprintf("%s/Admin/AddSanitizer", options.baseURL())
req, err := http.NewRequest("POST", url, nil)
if err != nil {
return err
}
req.Header.Set("x-abstraction-identifier", "OAuthResponseSanitizer")
handleTestLevelSanitizer(req, options)
return handleProxyResponse(client.Do(req))
}
// AddRemoveHeaderSanitizer removes a list of headers from request/responses.
func AddRemoveHeaderSanitizer(headersForRemoval []string, options *RecordingOptions) error {
if recordMode == LiveMode {
return nil
}
if options == nil {
options = defaultOptions()
}
url := fmt.Sprintf("%s/Admin/AddSanitizer", options.baseURL())
req, err := http.NewRequest("POST", url, nil)
if err != nil {
return err
}
req.Header.Set("x-abstraction-identifier", "RemoveHeaderSanitizer")
handleTestLevelSanitizer(req, options)
if options.TestInstance != nil {
recordingID := GetRecordingId(options.TestInstance)
if recordingID == "" {
return fmt.Errorf("did not find a recording ID for test with name '%s'. Did you make sure to call Start?", options.TestInstance.Name())
}
req.Header.Set(IDHeader, recordingID)
}
marshalled, err := json.MarshalIndent(struct {
HeadersForRemoval string `json:"headersForRemoval"`
}{
HeadersForRemoval: strings.Join(headersForRemoval, ""),
}, "", "")
if err != nil {
return err
}
req.Body = io.NopCloser(bytes.NewReader(marshalled))
req.ContentLength = int64(len(marshalled))
return handleProxyResponse(client.Do(req))
}
// AddURISanitizer sanitizes URIs via regex. value is the substition value, regex is
// either a simple regex or a substitution operation if options.GroupForReplace is defined.
func AddURISanitizer(value, regex string, options *RecordingOptions) error {
if recordMode == LiveMode {
return nil
}
if options == nil {
options = defaultOptions()
}
url := fmt.Sprintf("%v/Admin/AddSanitizer", options.baseURL())
req, err := http.NewRequest("POST", url, nil)
if err != nil {
return err
}
req.Header.Set("x-abstraction-identifier", "UriRegexSanitizer")
handleTestLevelSanitizer(req, options)
marshalled, err := json.MarshalIndent(struct {
Value string `json:"value"`
Regex string `json:"regex"`
}{
Value: value,
Regex: regex,
}, "", "")
if err != nil {
return err
}
req.Body = io.NopCloser(bytes.NewReader(marshalled))
req.ContentLength = int64(len(marshalled))
return handleProxyResponse(client.Do(req))
}
// AddURISubscriptionIDSanitizer replaces real subscriptionIDs within a URI with a default
// or configured fake value. To use the default value set value to "", otherwise value specifies the replacement value.
func AddURISubscriptionIDSanitizer(value string, options *RecordingOptions) error {
if recordMode == LiveMode {
return nil
}
if options == nil {
options = defaultOptions()
}
url := fmt.Sprintf("%s/Admin/AddSanitizer", options.baseURL())
req, err := http.NewRequest("POST", url, nil)
if err != nil {
return err
}
req.Header.Set("x-abstraction-identifier", "UriSubscriptionIdSanitizer")
handleTestLevelSanitizer(req, options)
if value != "" {
marshalled, err := json.MarshalIndent(struct {
Value string `json:"value,omitempty"`
}{
Value: value,
}, "", "")
if err != nil {
return err
}
req.Body = io.NopCloser(bytes.NewReader(marshalled))
req.ContentLength = int64(len(marshalled))
}
return handleProxyResponse(client.Do(req))
}
// ResetProxy restores the proxy's default sanitizers, matchers, and transforms
func ResetProxy(options *RecordingOptions) error {
if recordMode == LiveMode {
return nil
}
if options == nil {
options = defaultOptions()
}
url := fmt.Sprintf("%v/Admin/Reset", options.baseURL())
req, err := http.NewRequest("POST", url, nil)
if options.TestInstance != nil {
recordingID := GetRecordingId(options.TestInstance)
if recordingID == "" {
return fmt.Errorf("did not find a recording ID for test with name '%s'. Did you make sure to call Start?", options.TestInstance.Name())
}
req.Header.Set(IDHeader, recordingID)
}
if err != nil {
return err
}
return handleProxyResponse(client.Do(req))
}