-
Notifications
You must be signed in to change notification settings - Fork 156
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
fix(provider): redact sensitive headers from HTTP logging #479
Merged
Merged
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 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,98 @@ | ||
package tfe | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"net/http/httputil" | ||
"os" | ||
"strings" | ||
) | ||
|
||
type loggingTransport struct { | ||
name string | ||
delegate http.RoundTripper | ||
} | ||
|
||
const ( | ||
EnvLog = "TF_LOG" | ||
) | ||
|
||
// redactedHeaders is a list of lowercase headers (with trailing colons) that signal that the | ||
// header values should be redacted from logs | ||
var redactedHeaders = []string{"authorization:", "proxy-authorization:"} | ||
|
||
// IsDebugOrHigher returns whether or not the current log level is debug or trace | ||
func IsDebugOrHigher() bool { | ||
level := strings.ToUpper(os.Getenv(EnvLog)) | ||
return level == "DEBUG" || level == "TRACE" | ||
} | ||
|
||
// RoundTrip is a transport method that logs the request and response if the TF_LOG level is | ||
// TRACE or DEBUG | ||
func (t *loggingTransport) RoundTrip(req *http.Request) (*http.Response, error) { | ||
if IsDebugOrHigher() { | ||
reqData, err := httputil.DumpRequestOut(req, true) | ||
if err == nil { | ||
log.Printf("[DEBUG] "+logReqMsg, t.name, filterAndPrettyPrintLines(reqData)) | ||
} else { | ||
log.Printf("[ERROR] %s API Request error: %#v", t.name, err) | ||
} | ||
} | ||
|
||
resp, err := t.delegate.RoundTrip(req) | ||
if err != nil { | ||
return resp, err | ||
} | ||
|
||
if IsDebugOrHigher() { | ||
respData, err := httputil.DumpResponse(resp, true) | ||
if err == nil { | ||
log.Printf("[DEBUG] "+logRespMsg, t.name, filterAndPrettyPrintLines(respData)) | ||
} else { | ||
log.Printf("[ERROR] %s API Response error: %#v", t.name, err) | ||
} | ||
} | ||
|
||
return resp, nil | ||
} | ||
|
||
// NewLoggingTransport wraps the given transport with a logger that logs request and | ||
// response details | ||
func NewLoggingTransport(name string, t http.RoundTripper) *loggingTransport { | ||
return &loggingTransport{name, t} | ||
} | ||
|
||
// filterAndPrettyPrintLines iterates through a []byte line-by-line, | ||
// redacting any sensitive lines and transforming any lines that are complete json into | ||
// pretty-printed json. | ||
func filterAndPrettyPrintLines(b []byte) string { | ||
parts := strings.Split(string(b), "\n") | ||
for i, p := range parts { | ||
for _, check := range redactedHeaders { | ||
if strings.HasPrefix(strings.ToLower(p), check) { | ||
// This looks like a sensitive header to redact, so overwrite the entire line | ||
parts[i] = fmt.Sprintf("%s <REDACTED>", p[0:len(check)]) | ||
continue | ||
} | ||
} | ||
if b := []byte(p); json.Valid(b) { | ||
var out bytes.Buffer | ||
_ = json.Indent(&out, b, "", " ") // already checked for validity | ||
parts[i] = out.String() | ||
} | ||
} | ||
return strings.Join(parts, "\n") | ||
} | ||
|
||
const logReqMsg = `%s API Request Details: | ||
---[ REQUEST ]--------------------------------------- | ||
%s | ||
-----------------------------------------------------` | ||
|
||
const logRespMsg = `%s API Response Details: | ||
---[ RESPONSE ]-------------------------------------- | ||
%s | ||
-----------------------------------------------------` |
This file contains 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
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.
Is the
continue
redundant here?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.
No, because I don't want to validate this as JSON (line 81-- it would not be valid)