Skip to content

Commit

Permalink
Scrub out real admin URIs from cassettes VCR records.
Browse files Browse the repository at this point in the history
  • Loading branch information
monde committed Aug 2, 2023
1 parent 048897e commit 35e5ae5
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions okta/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,15 @@ func vcrCachedConfig(ctx context.Context, d *schema.ResourceData, configureFunc
})

rec.AddHook(func(i *cassette.Interaction) error {
i.Request.Headers.Del("User-Agent")
// need to scrub OKTA_ORG_NAME+OKTA_BASE_URL strings and rewrite as
// [cassette-name].dne-okta.com so that HTTP requests that escape VCR
// are bad.
vcrHostname := fmt.Sprintf("%s.%s", mgr.CurrentCassette, TestDomainName)
vcrAdminHostname := fmt.Sprintf("%s.%s", mgr.CurrentCassette, TestDomainName)
orgUrl, _ := url.Parse(i.Request.URL)
adminOrgURI := fmt.Sprintf("%s-admin.%s", os.Getenv("OKTA_ORG_NAME"), os.Getenv("OKTA_BASE_URL"))

// re-write the Authorization header
authHeader := "Authorization"
if auth, ok := firstHeaderValue(authHeader, i.Request.Headers); ok {
i.Request.Headers.Del(authHeader)
Expand All @@ -418,6 +426,7 @@ func vcrCachedConfig(ctx context.Context, d *schema.ResourceData, configureFunc
}

// save disk space, clean up what gets written to disk
i.Request.Headers.Del("User-Agent")
deleteResponseHeaders := []string{
"Cache-Control",
"Content-Security-Policy",
Expand All @@ -444,15 +453,17 @@ func vcrCachedConfig(ctx context.Context, d *schema.ResourceData, configureFunc
}
}

// need to scrub OKTA_ORG_NAME+OKTA_BASE_URL strings and rewrite as
// [cassette-name].dne-okta.com so that HTTP requests that escape VCR
// are bad.
vcrHostname := fmt.Sprintf("%s.%s", mgr.CurrentCassette, TestDomainName)
i.Request.Host = vcrHostname
orgUrl, _ := url.Parse(i.Request.URL)
i.Request.URL = strings.ReplaceAll(i.Request.URL, orgUrl.Host, vcrHostname)
i.Request.Body = strings.ReplaceAll(i.Response.Body, orgUrl.Host, vcrHostname)
i.Request.Body = strings.ReplaceAll(i.Response.Body, adminOrgURI, vcrAdminHostname)
i.Request.Body = strings.ReplaceAll(i.Response.Body, os.Getenv("OKTA_ORG_NAME"), mgr.CurrentCassette)

i.Response.Body = strings.ReplaceAll(i.Response.Body, orgUrl.Host, vcrHostname)
i.Response.Body = strings.ReplaceAll(i.Response.Body, adminOrgURI, vcrAdminHostname)
i.Response.Body = strings.ReplaceAll(i.Response.Body, os.Getenv("OKTA_ORG_NAME"), mgr.CurrentCassette)
headerLinks := replaceHeaderValues(i.Response.Headers["Link"], orgUrl.Host, vcrHostname)
headerLinks = replaceHeaderValues(headerLinks, adminOrgURI, vcrAdminHostname)
i.Response.Headers.Del("Link")
for _, val := range headerLinks {
i.Response.Headers.Add("Link", val)
Expand Down

0 comments on commit 35e5ae5

Please sign in to comment.