/
redact.go
50 lines (43 loc) · 1.54 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
package utils
import "net/url"
const (
redactionMarker = "redacted"
AWSSecretAccessKey = "AWS_SECRET_ACCESS_KEY"
GCPCredentials = "CREDENTIALS"
)
// redactedQueryParams is the set of query parameter names registered by the
// external storage providers that should be redacted from external storage URIs
// whenever they are displayed to a user.
var RedactedQueryParams = map[string]struct{}{}
// SanitizeExternalStorageURI returns the external storage URI with with some
// secrets redacted, for use when showing these URIs in the UI, to provide some
// protection from shoulder-surfing. The param is still present -- just
// redacted -- to make it clearer that that value is indeed persisted interally.
// extraParams which should be scrubbed -- for params beyond those that the
// various cloud-storage URIs supported by this package know about -- can be
// passed allowing this function to be used to scrub other URIs too.
func SanitizeExternalStorageURI(path string, extraParams []string) (string, error) {
uri, err := url.Parse(path)
if err != nil {
return "", err
}
if uri.User != nil {
if _, passwordSet := uri.User.Password(); passwordSet {
uri.User = url.UserPassword(uri.User.Username(), redactionMarker)
}
}
params := uri.Query()
for param := range params {
if _, ok := RedactedQueryParams[param]; ok {
params.Set(param, redactionMarker)
} else {
for _, p := range extraParams {
if param == p {
params.Set(param, redactionMarker)
}
}
}
}
uri.RawQuery = params.Encode()
return uri.String(), nil
}