9
9
"strings"
10
10
)
11
11
12
+ const redactedString = "[REDACTED]"
13
+
12
14
type dumpTransport struct {
13
15
roundTripper http.RoundTripper
14
16
logRequest func ([]byte )
@@ -22,7 +24,7 @@ func redactAuthz(req *http.Request) (restore func()) {
22
24
23
25
origAuthz := req .Header .Get ("Authorization" )
24
26
if origAuthz != "" {
25
- req .Header .Set ("Authorization" , "[REDACTED]" )
27
+ req .Header .Set ("Authorization" , redactedString )
26
28
restore = func () {
27
29
req .Header .Set ("Authorization" , origAuthz )
28
30
}
@@ -50,31 +52,77 @@ func redactBody(rc io.ReadCloser, rx *regexp.Regexp) (bool, io.ReadCloser, error
50
52
return rx .Match (content .Bytes ()), io .NopCloser (& content ), nil
51
53
}
52
54
55
+ func redactRequestBody (req * http.Request ) (restore func ()) {
56
+ restore = func () {}
57
+
58
+ redactedReader := io .NopCloser (strings .NewReader (redactedString ))
59
+
60
+ redact , origBody , _ := redactBody (req .Body , regexp .MustCompile (".*" ))
61
+
62
+ if redact {
63
+ origLength := req .ContentLength
64
+
65
+ req .Body = redactedReader
66
+ req .ContentLength = int64 (len (redactedString ))
67
+
68
+ restore = func () {
69
+ req .Body = origBody
70
+ req .ContentLength = origLength
71
+ }
72
+ } else {
73
+ req .Body = origBody
74
+ }
75
+
76
+ return
77
+ }
78
+
79
+ func redactResponseBody (res * http.Response ) (restore func ()) {
80
+ restore = func () {}
81
+
82
+ redactedReader := io .NopCloser (strings .NewReader (redactedString ))
83
+
84
+ redact , origBody , _ := redactBody (res .Body , regexp .MustCompile ("{\" protected\" :\" .*\" ,\" payload\" :\" .*\" ,\" signature\" :\" .*\" }" ))
85
+
86
+ if redact {
87
+ origLength := res .ContentLength
88
+
89
+ res .Body = redactedReader
90
+ res .ContentLength = int64 (len (redactedString ))
91
+
92
+ restore = func () {
93
+ res .Body = origBody
94
+ res .ContentLength = origLength
95
+ }
96
+ } else {
97
+ res .Body = origBody
98
+ }
99
+
100
+ return
101
+ }
102
+
53
103
// dumpRequest logs the contents of a given HTTP request, but first:
54
104
// 1. sanitizes the Authorization header
55
105
// 2. sanitizes the request body if the request is for authentication
56
106
func (d * dumpTransport ) dumpRequest (req * http.Request ) []byte {
57
- restore := redactAuthz (req )
58
- defer restore ()
107
+ restoreAuthz := redactAuthz (req )
108
+ defer restoreAuthz ()
59
109
60
- redact := false
61
- var body io.ReadCloser
62
110
if strings .Contains (req .URL .Path , "/authn" ) {
63
- redact , body , _ = redactBody (req . Body , regexp . MustCompile ( ".*" ) )
64
- req . Body = body
111
+ restoreBody := redactRequestBody (req )
112
+ defer restoreBody ()
65
113
}
66
114
67
- dump , _ := httputil .DumpRequestOut (req , ! redact )
115
+ dump , _ := httputil .DumpRequestOut (req , true )
68
116
return dump
69
117
}
70
118
71
119
// dumpResponse logs the contents of a given HTTP response, but first
72
120
// sanitizes the response body if it includes a Conjur token.
73
121
func (d * dumpTransport ) dumpResponse (res * http.Response ) []byte {
74
- redact , body , _ := redactBody (res . Body , regexp . MustCompile ( "{ \" protected \" : \" .* \" , \" payload \" : \" .* \" , \" signature \" : \" .* \" }" ) )
75
- res . Body = body
122
+ restoreBody := redactResponseBody (res )
123
+ defer restoreBody ()
76
124
77
- dump , _ := httputil .DumpResponse (res , ! redact )
125
+ dump , _ := httputil .DumpResponse (res , true )
78
126
return dump
79
127
}
80
128
0 commit comments