-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Redact HTTP dumps #130
Redact HTTP dumps #130
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package utils | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestDumpTransport(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function |
||
reqTestCases := []struct { | ||
description string | ||
path string | ||
headers map[string]string | ||
body string | ||
assert func(t *testing.T, req *http.Request, dump string) | ||
}{ | ||
{ | ||
description: "Only Authz header is redacted", | ||
headers: map[string]string{ | ||
"Authorization": "some-token", | ||
"Other-Header": "other-value", | ||
}, | ||
assert: func(t *testing.T, req *http.Request, dump string) { | ||
assert.NotContains(t, dump, "some-token") | ||
assert.Contains(t, dump, "other-value") | ||
|
||
assert.Equal(t, req.Header.Get("Authorization"), "some-token") | ||
assert.Equal(t, req.Header.Get("Other-Header"), "other-value") | ||
}, | ||
}, | ||
{ | ||
description: "Request body is redacted on authentication requests", | ||
path: "/authn-xyz/account/login", | ||
body: "some-body", | ||
assert: func(t *testing.T, req *http.Request, dump string) { | ||
assert.Contains(t, dump, redactedString) | ||
assert.NotContains(t, dump, "some-body") | ||
|
||
reqBody, err := ioutil.ReadAll(req.Body) | ||
assert.Nil(t, err) | ||
assert.Equal(t, string(reqBody), "some-body") | ||
}, | ||
}, | ||
{ | ||
description: "Request body is maintained on other requests", | ||
body: "some-body", | ||
assert: func(t *testing.T, req *http.Request, dump string) { | ||
assert.Contains(t, dump, "some-body") | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range reqTestCases { | ||
t.Run(tc.description, func(t *testing.T) { | ||
req, err := http.NewRequest( | ||
"POST", | ||
fmt.Sprintf("http://somehost.com%s", tc.path), | ||
bytes.NewBuffer([]byte(tc.body)), | ||
) | ||
assert.Nil(t, err) | ||
for k, v := range tc.headers { | ||
req.Header.Add(k, v) | ||
} | ||
|
||
dump := NewDumpTransport(nil, nil).dumpRequest(req) | ||
tc.assert(t, req, string(dump)) | ||
}) | ||
} | ||
|
||
respTestCases := []struct { | ||
description string | ||
body string | ||
assert func(t *testing.T, res *http.Response, dump string) | ||
}{ | ||
{ | ||
description: "Body is redacted if it contains a Conjur token", | ||
body: "{\"protected\":\"abcde\",\"payload\":\"fghijk\",\"signature\":\"lmnop\"}", | ||
assert: func(t *testing.T, res *http.Response, dump string) { | ||
assert.Contains(t, dump, redactedString) | ||
assert.NotContains(t, dump, "{\"protected\":\"abcde\",\"payload\":\"fghijk\",\"signature\":\"lmnop\"}") | ||
|
||
reqBody, err := ioutil.ReadAll(res.Body) | ||
assert.Nil(t, err) | ||
assert.Contains(t, string(reqBody), "{\"protected\":\"abcde\",\"payload\":\"fghijk\",\"signature\":\"lmnop\"}") | ||
}, | ||
}, | ||
{ | ||
description: "Body is maintained otherwise", | ||
body: "some-body", | ||
assert: func(t *testing.T, res *http.Response, dump string) { | ||
assert.Contains(t, dump, "some-body") | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range respTestCases { | ||
t.Run(tc.description, func(t *testing.T) { | ||
resp := http.Response{ | ||
Body: ioutil.NopCloser(bytes.NewBufferString(tc.body)), | ||
} | ||
|
||
dump := NewDumpTransport(nil, nil).dumpResponse(&resp) | ||
tc.assert(t, &resp, string(dump)) | ||
}) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we print
[REDACTED]
here like we do for the Auth header? Right now it just prints nothing.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Definitely can - just pushed this. I was using
httputil.DumpX
's built-in body redaction, which is an empty body.