Skip to content
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 1 commit into from
Apr 21, 2022

Conversation

brandonc
Copy link
Collaborator

@brandonc brandonc commented Apr 21, 2022

Description

Replaces the SDK logging transport with a similar logger that redacts certain headers ("authorization:", "proxy-authorization:") from logging output.

While we wait for the plugin SDK to implement a more robust or configurable solution for the issue of logging sensitive tokens in TRACE and DEBUG levels, we can implement a quick solution in this provider.

Testing plan

  1. Plan any configuration that uses TFE resources using the TF_LOG=TRACE variable
  2. Notice that authorization tokens are now redacted from log output made by terraform:

Example output:

2022-04-21T12:42:13.983-0600 [DEBUG] provider.terraform-provider-tfe: 2022/04/21 12:42:13 [DEBUG] TFE API Request Details:
2022-04-21T12:42:13.983-0600 [DEBUG] provider.terraform-provider-tfe: ---[ REQUEST ]---------------------------------------
2022-04-21T12:42:13.983-0600 [DEBUG] provider.terraform-provider-tfe: POST /api/v2/organizations HTTP/1.1
2022-04-21T12:42:13.983-0600 [DEBUG] provider.terraform-provider-tfe: Host: tfcdev-5839ba17.ngrok.io
2022-04-21T12:42:13.983-0600 [DEBUG] provider.terraform-provider-tfe: User-Agent: go-tfe
2022-04-21T12:42:13.983-0600 [DEBUG] provider.terraform-provider-tfe: Content-Length: 101
2022-04-21T12:42:13.983-0600 [DEBUG] provider.terraform-provider-tfe: Accept: application/vnd.api+json
2022-04-21T12:42:13.983-0600 [DEBUG] provider.terraform-provider-tfe: Authorization: <REDACTED>
2022-04-21T12:42:13.983-0600 [DEBUG] provider.terraform-provider-tfe: Content-Type: application/vnd.api+json
2022-04-21T12:42:13.983-0600 [DEBUG] provider.terraform-provider-tfe: Accept-Encoding: gzip
2022-04-21T12:42:13.983-0600 [DEBUG] provider.terraform-provider-tfe:
2022-04-21T12:42:13.983-0600 [DEBUG] provider.terraform-provider-tfe: {
2022-04-21T12:42:13.983-0600 [DEBUG] provider.terraform-provider-tfe:  "data": {
2022-04-21T12:42:13.983-0600 [DEBUG] provider.terraform-provider-tfe:   "type": "organizations",
2022-04-21T12:42:13.983-0600 [DEBUG] provider.terraform-provider-tfe:   "attributes": {
2022-04-21T12:42:13.983-0600 [DEBUG] provider.terraform-provider-tfe:    "email": "bcroft@hashicorp.com",
2022-04-21T12:42:13.983-0600 [DEBUG] provider.terraform-provider-tfe:    "name": "my-org-name"
2022-04-21T12:42:13.983-0600 [DEBUG] provider.terraform-provider-tfe:   }
2022-04-21T12:42:13.983-0600 [DEBUG] provider.terraform-provider-tfe:  }
2022-04-21T12:42:13.983-0600 [DEBUG] provider.terraform-provider-tfe: }
2022-04-21T12:42:13.983-0600 [DEBUG] provider.terraform-provider-tfe:
2022-04-21T12:42:13.983-0600 [DEBUG] provider.terraform-provider-tfe: -----------------------------------------------------

Notes

I lifted much of this code from the plugin SDK logging package so we should see no other logging behavior changes.

Replaces the SDK logging transport with a similar logger that redacts certain headers ("authorization:", "proxy-authorization:") from logging output
Copy link
Contributor

@sebasslash sebasslash left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very elegant. Nicely done.

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
Copy link
Contributor

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?

Copy link
Collaborator Author

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)

Copy link
Contributor

@Uk1288 Uk1288 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants