forked from GoogleCloudPlatform/k8s-config-connector
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
66 additions
and
0 deletions.
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 |
---|---|---|
|
@@ -16,7 +16,11 @@ package e2e | |
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"gopkg.in/dnaeon/go-vcr.v3/cassette" | ||
"gopkg.in/dnaeon/go-vcr.v3/recorder" | ||
"k8s.io/klog/v2" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
@@ -161,6 +165,68 @@ func testFixturesInSeries(ctx context.Context, t *testing.T, testPause bool, can | |
t.Errorf("[VCR] Failed stop vcr recorder: %v", err) | ||
} | ||
}) | ||
|
||
hook := func(i *cassette.Interaction) error { | ||
var requestHeadersToRemove = []string{ | ||
"Authorization", | ||
"User-Agent", | ||
} | ||
for _, header := range requestHeadersToRemove { | ||
delete(i.Request.Headers, header) | ||
} | ||
|
||
var responseHeadersToRemove = []string{ | ||
"Cache-Control", | ||
"Server", | ||
"Vary", | ||
"X-Content-Type-Options", | ||
"X-Frame-Options", | ||
"X-Xss-Protection", | ||
"Date", | ||
"Etag", | ||
} | ||
for _, header := range responseHeadersToRemove { | ||
delete(i.Response.Headers, header) | ||
} | ||
|
||
replaceFunc := func(s string) string { | ||
result := strings.Replace(s, uniqueID, "uniqueid111111", -1) | ||
result = strings.Replace(result, project.ProjectID, "cnrm-user", -1) | ||
return result | ||
} | ||
|
||
i.Request.Body = replaceFunc(i.Request.Body) | ||
i.Response.Body = replaceFunc(i.Response.Body) | ||
i.Request.URL = replaceFunc(i.Request.URL) | ||
|
||
s := i.Response.Body | ||
obj := make(map[string]any) | ||
if err := json.Unmarshal([]byte(s), &obj); err != nil { | ||
klog.Fatalf("error from json.Unmarshal(%q): %v", s, err) | ||
} | ||
toReplace, _, _ := unstructured.NestedString(obj, "user") | ||
if len(toReplace) != 0 { | ||
s = strings.Replace(s, toReplace, "[email protected]", -1) | ||
} | ||
|
||
// A very hacky way to replace actual debug info from log. | ||
if strings.Contains(s, "error") && strings.Contains(s, "debugInfo") { | ||
keyword := "\"debugInfo\": \"" | ||
// Find index of the start of debugInfo string | ||
startIndex := strings.Index(s, keyword) | ||
// Find index of the end of debugInfo string, by searching for the end quote character | ||
subString := s[startIndex+len(keyword):] | ||
endIndex := strings.Index(subString, "\"") | ||
// Use both indexes to get the string contains the message we need to remove | ||
temp := s[startIndex+len(keyword) : startIndex+len(keyword)+endIndex] | ||
// Replace | ||
s = strings.Replace(s, temp, "fake debug info", -1) | ||
} | ||
|
||
i.Response.Body = s | ||
return nil | ||
} | ||
h.VCRRecorder.AddHook(hook, recorder.BeforeSaveHook) | ||
} | ||
|
||
primaryResource, opt := loadFixture(project) | ||
|