Skip to content
This repository has been archived by the owner on Jun 28, 2024. It is now read-only.

Commit

Permalink
fix: avoid nil pointer error when debugging requests with no body (#176)
Browse files Browse the repository at this point in the history
@cprivitere reported that cluster-api-provider-packet experiences nil
pointer errors when debugging is enabled and a request has no body. This
behavior seems to have started in v0.25.1, which introduced automatic
auth token redaction in debug output.

In order to redact the auth token without impacting the original
request, we were cloning the request object. In order to clone the
request body, we had to call `request.GetBody()`, but that function
causes a nil pointer error if the request body is nil.

This updates the auth token redaction code to temporarily overwrite the
auth token in the original request, rather than cloning the request &
its potentially-nil body.
  • Loading branch information
ctreatma committed Nov 28, 2023
1 parent ad2dc45 commit d87441c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
16 changes: 10 additions & 6 deletions metal/v1/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 10 additions & 6 deletions templates/client.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -249,18 +249,22 @@ func parameterToJson(obj interface{}) (string, error) {
// callAPI do the request.
func (c *APIClient) callAPI(request *http.Request) (*http.Response, error) {
if c.cfg.Debug {
r := request.Clone(context.TODO())
r.Body, _ = request.GetBody()
h := r.Header
if len(h.Get("X-Auth-Token")) != 0 {
h.Set("X-Auth-Token", "**REDACTED**")
authToken, hasAuth := request.Header["X-Auth-Token"]
if hasAuth {
request.Header.Set("X-Auth-Token", "**REDACTED**")
}
dump, err := httputil.DumpRequestOut(r, true)
dump, err := httputil.DumpRequestOut(request, true)
if hasAuth {
request.Header["X-Auth-Token"] = authToken
}
if err != nil {
return nil, err
}
log.Printf("\n%s\n", string(dump))
}
resp, err := c.cfg.HTTPClient.Do(request)
Expand Down

0 comments on commit d87441c

Please sign in to comment.