Skip to content

Commit

Permalink
Fix logging of request bodies containing percent characters. (#881)
Browse files Browse the repository at this point in the history
## Changes
Because the debug string for requests/responses can contain percent
characters, simply logging the request/response causes Go to interpret
this as a format string. Instead, we should use format string `"%s"` to
ensure that the percents are printed as expected to the debug log.

## Tests
<!-- 
How is this tested? Please see the checklist below and also describe any
other relevant tests
-->

- [ ] `make test` passing
- [ ] `make fmt` applied
- [ ] relevant integration tests applied
  • Loading branch information
mgyucht authored Apr 5, 2024
1 parent b210aa9 commit ed385d6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion httpclient/api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ func (c *ApiClient) recordRequestLog(
DebugTruncateBytes: c.config.DebugTruncateBytes,
DebugAuthorizationHeader: true,
}.String()
logger.Debugf(ctx, message)
logger.Debugf(ctx, "%s", message)
}

// RoundTrip implements http.RoundTripper to integrate with golang.org/x/oauth2
Expand Down
33 changes: 33 additions & 0 deletions httpclient/api_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/databricks/databricks-sdk-go/common"
"github.com/databricks/databricks-sdk-go/logger"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/oauth2"
"golang.org/x/time/rate"
Expand Down Expand Up @@ -472,6 +473,38 @@ func TestInlineArrayDebugging_StreamResponse(t *testing.T) {
< <Streaming response>`, bufLogger.String())
}

func TestLogQueryParametersWithPercent(t *testing.T) {
prevLogger := logger.DefaultLogger
bufLogger := &BufferLogger{}
logger.DefaultLogger = bufLogger
defer func() {
logger.DefaultLogger = prevLogger
}()

c := NewApiClient(ClientConfig{
DebugTruncateBytes: 2048,
Transport: hc(func(r *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: 200,
Body: io.NopCloser(strings.NewReader(`{"foo": "bar"}`)),
Request: r,
}, nil
}),
})
headers := map[string]string{"Accept": "application/json"}
err := c.Do(context.Background(), "GET", "/a",
WithRequestHeaders(headers),
WithRequestData(map[string]any{
"filter": "name like '%'",
}))
assert.NoError(t, err)
assert.Equal(t, `[DEBUG] GET /a?filter=name like '%'
<
< {
< "foo": "bar"
< }`, bufLogger.String())
}

func TestStreamRequestFromFileWithReset(t *testing.T) {
// make a temporary file with some content
f, err := os.CreateTemp("", "databricks-client-test")
Expand Down

0 comments on commit ed385d6

Please sign in to comment.