-
Notifications
You must be signed in to change notification settings - Fork 5
fix: request logger #161
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
Merged
Merged
fix: request logger #161
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package authz | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "strings" | ||
| ) | ||
|
|
||
| func requestLogger(r *http.Request) string { | ||
| var sb strings.Builder | ||
|
|
||
| sb.WriteString(r.Method) | ||
| sb.WriteString(" ") | ||
| sb.WriteString(r.URL.String()) | ||
|
|
||
| var bodyBytes []byte | ||
| if r.Body != nil { | ||
| defer func() { | ||
| _ = r.Body.Close() | ||
| }() | ||
| bodyBytes = make([]byte, r.ContentLength) | ||
| _, err := r.Body.Read(bodyBytes) | ||
| if err != nil { | ||
| sb.WriteString("Body: <unknown>") | ||
| } else { | ||
| sb.WriteString(" Body: " + string(bodyBytes)) | ||
| } | ||
| } | ||
|
|
||
| return sb.String() | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| package authz | ||
|
|
||
| import ( | ||
| "io" | ||
| "net/http" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestRequestLogger(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| method string | ||
| url string | ||
| body string | ||
| expected string | ||
| }{ | ||
| { | ||
| name: "GET request without query or body", | ||
| method: "GET", | ||
| url: "https://kubernetes.default.svc/api/v1/pods", | ||
| body: "", | ||
| expected: "GET https://kubernetes.default.svc/api/v1/pods", | ||
| }, | ||
| { | ||
| name: "GET request with query parameters", | ||
| method: "GET", | ||
| url: "https://kubernetes.default.svc/api/v1/pods?labelSelector=app%3Dmyapp&limit=10", | ||
| body: "", | ||
| expected: "GET https://kubernetes.default.svc/api/v1/pods?labelSelector=app%3Dmyapp&limit=10", | ||
| }, | ||
| { | ||
| name: "POST request with body", | ||
| method: "POST", | ||
| url: "https://kubernetes.default.svc/api/v1/namespaces/default/pods", | ||
| body: `{"apiVersion":"v1","kind":"Pod","metadata":{"name":"test"}}`, | ||
| expected: `POST https://kubernetes.default.svc/api/v1/namespaces/default/pods Body: {"apiVersion":"v1","kind":"Pod","metadata":{"name":"test"}}`, | ||
| }, | ||
| { | ||
| name: "PUT request with query and body", | ||
| method: "PUT", | ||
| url: "https://kubernetes.default.svc/api/v1/namespaces/default/pods/test?dryRun=All", | ||
| body: `{"metadata":{"labels":{"env":"prod"}}}`, | ||
| expected: `PUT https://kubernetes.default.svc/api/v1/namespaces/default/pods/test?dryRun=All Body: {"metadata":{"labels":{"env":"prod"}}}`, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| var bodyReader io.Reader | ||
| if tt.body != "" { | ||
| bodyReader = strings.NewReader(tt.body) | ||
| } | ||
|
|
||
| req, err := http.NewRequest(tt.method, tt.url, bodyReader) | ||
| require.NoError(t, err) | ||
|
|
||
| result := requestLogger(req) | ||
| require.Equal(t, tt.expected, result) | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I know that this PR isn't changing this behavior, but the first thing I think when I see request body logging is whether it could leak anything sensitive. Are we worried about that in this case?
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.
Body should contain Check request bodies, LR bodies, etc... which shouldn't contain PII. It's not like i'm logging headers 😅