-
Notifications
You must be signed in to change notification settings - Fork 125
/
dump.go
344 lines (311 loc) · 9.03 KB
/
dump.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
/*
Copyright (c) 2018 Red Hat, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// This file contains the implementations of the methods of the connection that are used to dump to
// the log the details of HTTP requests and responses.
package sdk
import (
"bytes"
"context"
"io"
"mime"
"net/http"
"net/url"
"sort"
"strings"
jsoniter "github.com/json-iterator/go"
"github.com/openshift-online/ocm-sdk-go/helpers"
"github.com/openshift-online/ocm-sdk-go/logging"
)
// dumpTransportWrapper is a transport wrapper that creates round trippers that dump the details of
// the request and the responses to the log.
type dumpTransportWrapper struct {
logger logging.Logger
}
// Wrap creates a round tripper on top of the given one that sends to the log the details of
// requests and responses.
func (w *dumpTransportWrapper) Wrap(transport http.RoundTripper) http.RoundTripper {
return &dumpRoundTripper{
logger: w.logger,
next: transport,
}
}
// dumpRoundTripper is a round tripper that dumps the details of the requests and the responses to
// the log.
type dumpRoundTripper struct {
logger logging.Logger
next http.RoundTripper
}
// Make sure that we implement the http.RoundTripper interface:
var _ http.RoundTripper = &dumpRoundTripper{}
// RoundTrip is he implementation of the http.RoundTripper interface.
func (d *dumpRoundTripper) RoundTrip(request *http.Request) (response *http.Response, err error) {
// Get the context:
ctx := request.Context()
// Read the complete body in memory, in order to send it to the log, and replace it with a
// reader that reads it from memory:
if request.Body != nil {
var body []byte
body, err = io.ReadAll(request.Body)
if err != nil {
return
}
err = request.Body.Close()
if err != nil {
return
}
d.dumpRequest(ctx, request, body)
request.Body = io.NopCloser(bytes.NewBuffer(body))
} else {
d.dumpRequest(ctx, request, nil)
}
// Call the next round tripper:
response, err = d.next.RoundTrip(request)
if err != nil {
return
}
// Read the complete response body in memory, in order to send it the log, and replace it
// with a reader that reads it from memory:
if response.Body != nil {
var body []byte
body, err = io.ReadAll(response.Body)
if err != nil {
return
}
err = response.Body.Close()
if err != nil {
return
}
d.dumpResponse(ctx, response, body)
response.Body = io.NopCloser(bytes.NewBuffer(body))
} else {
d.dumpResponse(ctx, response, nil)
}
return
}
const (
// redactionStr replaces sensitive values in output.
redactionStr = "***"
)
// redactFields are removed from log output when dumped.
var redactFields = map[string]bool{
"access_token": true,
"admin": true,
"id_token": true,
"refresh_token": true,
"password": true,
"client_secret": true,
"kubeconfig": true,
"ssh": true,
"access_key_id": true,
"secret_access_key": true,
}
// dumpRequest dumps to the log, in debug level, the details of the given HTTP request.
func (d *dumpRoundTripper) dumpRequest(ctx context.Context, request *http.Request, body []byte) {
d.logger.Debug(ctx, "Request method is %s", request.Method)
d.logger.Debug(ctx, "Request URL is '%s'", request.URL)
if request.Host != "" {
d.logger.Debug(ctx, "Request header 'Host' is '%s'", request.Host)
}
header := request.Header
names := make([]string, len(header))
i := 0
for name := range header {
names[i] = name
i++
}
sort.Strings(names)
for _, name := range names {
values := header[name]
for _, value := range values {
if strings.ToLower(name) == "authorization" {
d.logger.Debug(ctx, "Request header '%s' is omitted", name)
} else {
d.logger.Debug(ctx, "Request header '%s' is '%s'", name, value)
}
}
}
if body != nil {
d.logger.Debug(ctx, "Request body follows")
d.dumpBody(ctx, header, body)
}
}
// dumpResponse dumps to the log, in debug level, the details of the given HTTP response.
func (d *dumpRoundTripper) dumpResponse(ctx context.Context, response *http.Response, body []byte) {
d.logger.Debug(ctx, "Response protocol is '%s'", response.Proto)
d.logger.Debug(ctx, "Response status is '%s'", response.Status)
header := response.Header
names := make([]string, len(header))
i := 0
for name := range header {
names[i] = name
i++
}
sort.Strings(names)
for _, name := range names {
values := header[name]
for _, value := range values {
d.logger.Debug(ctx, "Response header '%s' is '%s'", name, value)
}
}
if body != nil {
d.logger.Debug(ctx, "Response body follows")
d.dumpBody(ctx, header, body)
}
}
// dumpBody checks the content type used in the given header and then it dumps the given body in a
// format suitable for that content type.
func (d *dumpRoundTripper) dumpBody(ctx context.Context, header http.Header, body []byte) {
// Try to parse the content type:
var mediaType string
contentType := header.Get("Content-Type")
if contentType != "" {
var err error
mediaType, _, err = mime.ParseMediaType(contentType)
if err != nil {
d.logger.Error(ctx, "Can't parse content type '%s': %v", contentType, err)
}
} else {
mediaType = contentType
}
// Dump the body according to the content type:
switch mediaType {
case "application/x-www-form-urlencoded":
d.dumpForm(ctx, body)
case "application/json", "":
d.dumpJSON(ctx, body)
default:
d.dumpBytes(ctx, body)
}
}
// dumpForm sends to the log the contents of the given form data, excluding security sensitive
// fields.
func (d *dumpRoundTripper) dumpForm(ctx context.Context, data []byte) {
// Parse the form:
form, err := url.ParseQuery(string(data))
if err != nil {
d.dumpBytes(ctx, data)
return
}
// Redact values corresponding to security sensitive fields:
for name, values := range form {
if redactFields[name] {
for i := range values {
values[i] = redactionStr
}
}
}
// Get and sort the names of the fields of the form, so that the generated output will be
// predictable:
names := make([]string, 0, len(form))
for name := range form {
names = append(names, name)
}
sort.Strings(names)
// Write the names and values to the buffer while redacting the sensitive fields:
buffer := &bytes.Buffer{}
for _, name := range names {
key := url.QueryEscape(name)
values := form[name]
for _, value := range values {
var redacted string
if redactFields[name] {
redacted = redactionStr
} else {
redacted = url.QueryEscape(value)
}
if buffer.Len() > 0 {
buffer.WriteByte('&') // #nosec G104
}
buffer.WriteString(key) // #nosec G104
buffer.WriteByte('=') // #nosec G104
buffer.WriteString(redacted) // #nosec G104
}
}
// Send the redacted data to the log:
d.dumpBytes(ctx, buffer.Bytes())
}
// dumpJSON tries to parse the given data as a JSON document. If that works, then it dumps it
// indented, otherwise dumps it as is.
func (d *dumpRoundTripper) dumpJSON(ctx context.Context, data []byte) {
it, err := helpers.NewIterator(data)
if err != nil {
d.logger.Debug(ctx, "%s", data)
} else {
var buf bytes.Buffer
str := helpers.NewStream(&buf)
// remove sensitive information
d.redactSensitive(it, str)
err := str.Flush()
if err != nil {
d.logger.Debug(ctx, "%s", data)
} else {
d.logger.Debug(ctx, "%s", buf.String())
}
}
}
// dumpBytes dump the given data as an array of bytes.
func (d *dumpRoundTripper) dumpBytes(ctx context.Context, data []byte) {
d.logger.Debug(ctx, "%s", data)
}
// redactSensitive replaces sensitive fields within a response with redactionStr.
func (d *dumpRoundTripper) redactSensitive(it *jsoniter.Iterator, str *jsoniter.Stream) {
switch it.WhatIsNext() {
case jsoniter.ObjectValue:
str.WriteObjectStart()
first := true
for field := it.ReadObject(); field != ""; field = it.ReadObject() {
if !first {
str.WriteMore()
}
first = false
str.WriteObjectField(field)
if v, ok := redactFields[field]; ok && v {
str.WriteString(redactionStr)
it.Skip()
continue
}
d.redactSensitive(it, str)
}
str.WriteObjectEnd()
case jsoniter.ArrayValue:
str.WriteArrayStart()
first := true
for it.ReadArray() {
if !first {
str.WriteMore()
}
first = false
d.redactSensitive(it, str)
}
str.WriteArrayEnd()
case jsoniter.StringValue:
str.WriteString(it.ReadString())
case jsoniter.NumberValue:
v := it.ReadNumber()
i, err := v.Int64()
if err == nil {
str.WriteInt64(i)
break
}
f, err := v.Float64()
if err == nil {
str.WriteFloat64(f)
}
case jsoniter.BoolValue:
str.WriteBool(it.ReadBool())
case jsoniter.NilValue:
str.WriteNil()
// Skip because no reading from it is involved
it.Skip()
}
}