Skip to content

Commit 84e7115

Browse files
committed
Update header redaction function
1 parent 6b2ae42 commit 84e7115

File tree

5 files changed

+55
-6
lines changed

5 files changed

+55
-6
lines changed

httpclient/httpclient_auth_oauth.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func (c *Client) ObtainOAuthToken(credentials AuthConfig) error {
101101
expirationTime := time.Now().Add(expiresIn)
102102

103103
// Modified log call using the helper function
104-
redactedAccessToken := RedactSensitiveData(c, "AccessToken", oauthResp.AccessToken)
104+
redactedAccessToken := RedactSensitiveHeaderData(c, "AccessToken", oauthResp.AccessToken)
105105
log.Info("OAuth token obtained successfully", zap.String("AccessToken", redactedAccessToken), zap.Duration("ExpiresIn", expiresIn), zap.Time("ExpirationTime", expirationTime))
106106

107107
c.Token = oauthResp.AccessToken

httpclient/httpclient_cookies.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package httpclient
2+
3+
import (
4+
"net/http"
5+
"strings"
6+
)
7+
8+
// RedactSensitiveCookies redacts sensitive information from cookies.
9+
// It takes a slice of *http.Cookie and returns a redacted slice of *http.Cookie.
10+
func RedactSensitiveCookies(cookies []*http.Cookie) []*http.Cookie {
11+
// Define sensitive cookie names that should be redacted.
12+
sensitiveCookieNames := map[string]bool{
13+
"SessionID": true, // Example sensitive cookie name
14+
// Add more sensitive cookie names as needed.
15+
}
16+
17+
// Iterate over the cookies and redact sensitive ones.
18+
for _, cookie := range cookies {
19+
if _, found := sensitiveCookieNames[cookie.Name]; found {
20+
cookie.Value = "REDACTED"
21+
}
22+
}
23+
24+
return cookies
25+
}
26+
27+
// Utility function to convert cookies from http.Header to []*http.Cookie.
28+
// This can be useful if cookies are stored in http.Header (e.g., from a response).
29+
func CookiesFromHeader(header http.Header) []*http.Cookie {
30+
cookies := []*http.Cookie{}
31+
for _, cookieHeader := range header["Set-Cookie"] {
32+
if cookie := ParseCookieHeader(cookieHeader); cookie != nil {
33+
cookies = append(cookies, cookie)
34+
}
35+
}
36+
return cookies
37+
}
38+
39+
// ParseCookieHeader parses a single Set-Cookie header and returns an *http.Cookie.
40+
func ParseCookieHeader(header string) *http.Cookie {
41+
headerParts := strings.Split(header, ";")
42+
if len(headerParts) > 0 {
43+
cookieParts := strings.SplitN(headerParts[0], "=", 2)
44+
if len(cookieParts) == 2 {
45+
return &http.Cookie{Name: cookieParts[0], Value: cookieParts[1]}
46+
}
47+
}
48+
return nil
49+
}

httpclient/httpclient_headers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func (h *HeaderManager) SetRequestHeaders(endpoint string) {
109109
}
110110

111111
// LogHeaders prints all the current headers in the http.Request using the zap logger.
112-
// It uses the RedactSensitiveData function to redact sensitive data if required.
112+
// It uses the RedactSensitiveHeaderData function to redact sensitive data if required.
113113
func (h *HeaderManager) LogHeaders(client *Client) {
114114
if h.log.GetLogLevel() <= logger.LogLevelDebug {
115115
// Initialize a new Header to hold the potentially redacted headers
@@ -119,7 +119,7 @@ func (h *HeaderManager) LogHeaders(client *Client) {
119119
// Redact sensitive values
120120
if len(values) > 0 {
121121
// Use the first value for simplicity; adjust if multiple values per header are expected
122-
redactedValue := RedactSensitiveData(client, name, values[0])
122+
redactedValue := RedactSensitiveHeaderData(client, name, values[0])
123123
redactedHeaders.Set(name, redactedValue)
124124
}
125125
}

httpclient/httpclient_helpers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ func ParseISO8601Date(dateStr string) (time.Time, error) {
1010
return time.Parse(time.RFC3339, dateStr)
1111
}
1212

13-
// RedactSensitiveData redacts sensitive data if the HideSensitiveData flag is set to true.
14-
func RedactSensitiveData(client *Client, key string, value string) string {
13+
// RedactSensitiveHeaderData redacts sensitive data if the HideSensitiveData flag is set to true.
14+
func RedactSensitiveHeaderData(client *Client, key string, value string) string {
1515
if client.clientConfig.ClientOptions.HideSensitiveData {
1616
// Define sensitive data keys that should be redacted.
1717
sensitiveKeys := map[string]bool{

httpclient/httpclient_helpers_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func TestRedactSensitiveData(t *testing.T) {
7070
},
7171
}
7272

73-
result := RedactSensitiveData(client, tt.key, tt.value)
73+
result := RedactSensitiveHeaderData(client, tt.key, tt.value)
7474
assert.Equal(t, tt.expectedOutcome, result, "Redaction outcome should match expected")
7575
})
7676
}

0 commit comments

Comments
 (0)