Skip to content

Commit 470c0bb

Browse files
authored
Explicitly set charset for SCIM requests (#1043)
Added printing of response headers when debugging is enabled Fixes #1034
1 parent 7dff0f2 commit 470c0bb

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

common/http.go

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ func (c *DatabricksClient) completeUrl(r *http.Request) error {
343343
// Scim sets SCIM headers
344344
func (c *DatabricksClient) Scim(ctx context.Context, method, path string, request interface{}, response interface{}) error {
345345
body, err := c.authenticatedQuery(ctx, method, path, request, c.completeUrl, func(r *http.Request) error {
346-
r.Header.Set("Content-Type", "application/scim+json")
346+
r.Header.Set("Content-Type", "application/scim+json; charset=utf-8")
347347
if c.isAccountsClient() && c.AccountID != "" {
348348
// until `/preview` is there for workspace scim
349349
r.URL.Path = strings.ReplaceAll(path, "/preview", fmt.Sprintf("/api/2.0/accounts/%s", c.AccountID))
@@ -397,6 +397,7 @@ func (c *DatabricksClient) redactedDump(body []byte) (res string) {
397397
if len(body) == 0 {
398398
return
399399
}
400+
400401
var requestMap map[string]interface{}
401402
err := json.Unmarshal(body, &requestMap)
402403
if err != nil {
@@ -428,6 +429,22 @@ func (c *DatabricksClient) userAgent(ctx context.Context) string {
428429
Version(), resource, terraformVersion)
429430
}
430431

432+
func (c *DatabricksClient) createDebugHeaders(header http.Header, host string) string {
433+
headers := ""
434+
if c.DebugHeaders {
435+
if host != "" {
436+
headers += fmt.Sprintf("\n * Host: %s", host)
437+
}
438+
for k, v := range header {
439+
headers += fmt.Sprintf("\n * %s: %s", k, onlyNBytes(strings.Join(v, ""), c.DebugTruncateBytes))
440+
}
441+
if len(headers) > 0 {
442+
headers += "\n"
443+
}
444+
}
445+
return headers
446+
}
447+
431448
// todo: do is better name
432449
func (c *DatabricksClient) genericQuery(ctx context.Context, method, requestURL string, data interface{},
433450
visitors ...func(*http.Request) error) (body []byte, err error) {
@@ -452,16 +469,7 @@ func (c *DatabricksClient) genericQuery(ctx context.Context, method, requestURL
452469
return nil, err
453470
}
454471
}
455-
headers := ""
456-
if c.DebugHeaders {
457-
headers += fmt.Sprintf("\n * Host: %s", c.Host)
458-
for k, v := range request.Header {
459-
headers += fmt.Sprintf("\n * %s: %s", k, onlyNBytes(strings.Join(v, ""), c.DebugTruncateBytes))
460-
}
461-
if len(headers) > 0 {
462-
headers += "\n"
463-
}
464-
}
472+
headers := c.createDebugHeaders(request.Header, c.Host)
465473
log.Printf("[DEBUG] %s %s %s%v", method, request.URL.Path, headers, c.redactedDump(requestBody)) // lgtm[go/clear-text-logging]
466474

467475
r, err := retryablehttp.FromRequest(request)
@@ -486,7 +494,8 @@ func (c *DatabricksClient) genericQuery(ctx context.Context, method, requestURL
486494
if err != nil {
487495
return nil, err
488496
}
489-
log.Printf("[DEBUG] %s %v <- %s %s", resp.Status, c.redactedDump(body), method, request.URL.Path)
497+
headers = c.createDebugHeaders(resp.Header, "")
498+
log.Printf("[DEBUG] %s %s %s <- %s %s", resp.Status, headers, c.redactedDump(body), method, strings.ReplaceAll(request.URL.Path, "\n", ""))
490499
return body, nil
491500
}
492501

0 commit comments

Comments
 (0)