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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update recorder to skip saving 429 responses #382

Merged
merged 3 commits into from
Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/hashicorp/terraform-plugin-docs v0.13.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.24.0
github.com/stretchr/testify v1.8.1
gopkg.in/dnaeon/go-vcr.v3 v3.1.1
gopkg.in/dnaeon/go-vcr.v3 v3.1.2
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -639,8 +639,8 @@ gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/dnaeon/go-vcr.v3 v3.1.1 h1:iulXYE3JB3ZEnd0jS5CvBzJQc7JUp/BtjyI2/DAcmMs=
gopkg.in/dnaeon/go-vcr.v3 v3.1.1/go.mod h1:2IMOnnlx9I6u9x+YBsM3tAMx6AlOxnJ0pWxQAzZ79Ag=
gopkg.in/dnaeon/go-vcr.v3 v3.1.2 h1:F1smfXBqQqwpVifDfUBQG6zzaGjzT+EnVZakrOdr5wA=
gopkg.in/dnaeon/go-vcr.v3 v3.1.2/go.mod h1:2IMOnnlx9I6u9x+YBsM3tAMx6AlOxnJ0pWxQAzZ79Ag=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME=
gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI=
Expand Down
68 changes: 40 additions & 28 deletions internal/recorder/http_recorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import (
"os"
"strings"
"testing"
"time"

"github.com/auth0/go-auth0"
"github.com/auth0/go-auth0/management"
"github.com/stretchr/testify/require"
"gopkg.in/dnaeon/go-vcr.v3/cassette"
Expand Down Expand Up @@ -58,44 +56,57 @@ func New(t *testing.T) *Recorder {
}

func removeSensitiveDataFromRecordings(t *testing.T, recorderTransport *recorder.Recorder) {
allowedHeaders := map[string]bool{
"Content-Type": true,
"User-Agent": true,
}

recorderTransport.AddHook(
func(i *cassette.Interaction) error {
for header := range i.Request.Headers {
if _, ok := allowedHeaders[header]; !ok {
delete(i.Request.Headers, header)
}
}
for header := range i.Response.Headers {
if _, ok := allowedHeaders[header]; !ok {
delete(i.Response.Headers, header)
}
}
skip429Response(i)
redactHeaders(i)

domain := os.Getenv("AUTH0_DOMAIN")
require.NotEmpty(t, domain, "removeSensitiveDataFromRecordings(): AUTH0_DOMAIN is empty")

redactSensitiveDataInClient(t, i, domain)

i.Request.Host = strings.Replace(i.Request.Host, domain, RecordingsDomain, -1)
i.Request.URL = strings.Replace(i.Request.URL, domain, RecordingsDomain, -1)

i.Response.Duration = time.Millisecond
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We don't really need to set the duration to 1ms as we already set SkipRequestLatency: true on the recorder options.


domainParts := strings.Split(domain, ".")
i.Response.Body = strings.Replace(i.Response.Body, domainParts[0], recordingsTenant, -1)
i.Request.Body = strings.Replace(i.Request.Body, domainParts[0], recordingsTenant, -1)
redactDomain(i, domain)

return nil
},
recorder.BeforeSaveHook,
)
}

func skip429Response(i *cassette.Interaction) {
if i.Response.Code == http.StatusTooManyRequests {
i.DiscardOnSave = true
}
}

func redactHeaders(i *cassette.Interaction) {
allowedHeaders := map[string]bool{
"Content-Type": true,
"User-Agent": true,
}

for header := range i.Request.Headers {
if _, ok := allowedHeaders[header]; !ok {
delete(i.Request.Headers, header)
}
}
for header := range i.Response.Headers {
if _, ok := allowedHeaders[header]; !ok {
delete(i.Response.Headers, header)
}
}
}

func redactDomain(i *cassette.Interaction, domain string) {
i.Request.Host = strings.Replace(i.Request.Host, domain, RecordingsDomain, -1)
i.Request.URL = strings.Replace(i.Request.URL, domain, RecordingsDomain, -1)

domainParts := strings.Split(domain, ".")

i.Response.Body = strings.Replace(i.Response.Body, domainParts[0], recordingsTenant, -1)
i.Request.Body = strings.Replace(i.Request.Body, domainParts[0], recordingsTenant, -1)
}

func redactSensitiveDataInClient(t *testing.T, i *cassette.Interaction, domain string) {
create := i.Request.URL == "https://"+domain+"/api/v2/clients" && i.Request.Method == http.MethodPost
read := strings.Contains(i.Request.URL, "https://"+domain+"/api/v2/clients/") && i.Request.Method == http.MethodGet
Expand All @@ -107,10 +118,11 @@ func redactSensitiveDataInClient(t *testing.T, i *cassette.Interaction, domain s
err := json.Unmarshal([]byte(i.Response.Body), &client)
require.NoError(t, err)

redacted := "[REDACTED]"
client.SigningKeys = []map[string]string{
{"cert": "[REDACTED]"},
{"cert": redacted},
}
client.ClientSecret = auth0.String("[REDACTED]")
client.ClientSecret = &redacted

clientBody, err := json.Marshal(client)
require.NoError(t, err)
Expand Down