From dbdb02fdc47d9662a0232005385887b8dff79911 Mon Sep 17 00:00:00 2001 From: Jackson Date: Wed, 20 Nov 2024 14:42:26 -0500 Subject: [PATCH 1/3] Log less --- libs/wallet/provider/uphold/uphold.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/libs/wallet/provider/uphold/uphold.go b/libs/wallet/provider/uphold/uphold.go index 4acf95185..8e1ac7564 100644 --- a/libs/wallet/provider/uphold/uphold.go +++ b/libs/wallet/provider/uphold/uphold.go @@ -17,6 +17,7 @@ import ( "net/http/httputil" "net/url" "os" + "regexp" "strconv" "strings" "time" @@ -236,6 +237,12 @@ func submit( if err != nil { return nil, resp, fmt.Errorf("%w: %s", errorutils.ErrFailedBodyRead, err.Error()) } + sbody := string(body) + for _, p := range []string{`"description":\s*"[^"]+"\s*,?`, `"\w+Country":\s*"[^"]+"\s*,?`} { + re := regexp.MustCompile(p) + sbody = re.ReplaceAllString(sbody, "") + } + sbody = regexp.MustCompile(`,}`).ReplaceAllString(sbody, "}") if logger != nil { logger.Debug(). @@ -243,18 +250,18 @@ func submit( Str("type", "http.Response"). Int("status", resp.StatusCode). Str("headers", string(jsonHeaders)). - Msg(string(body)) + Msg(sbody) } if resp.StatusCode/100 != 2 { var uhErr upholdError - if json.Unmarshal(body, &uhErr) != nil { - return nil, resp, fmt.Errorf("Error %d, %s", resp.StatusCode, body) + if json.Unmarshal([]byte(sbody), &uhErr) != nil { + return nil, resp, fmt.Errorf("Error %d, %s", resp.StatusCode, sbody) } uhErr.RequestID = resp.Header.Get("Request-Id") return nil, resp, uhErr } - return body, resp, nil + return []byte(sbody), resp, nil } type createCardRequest struct { From 2ebcbcd7f23fca074475fa40099a63a6ddc5fc08 Mon Sep 17 00:00:00 2001 From: Jackson Date: Fri, 22 Nov 2024 16:04:03 -0500 Subject: [PATCH 2/3] Move redaction into function and add test --- libs/wallet/provider/uphold/uphold.go | 16 ++++++++++------ libs/wallet/provider/uphold/uphold_test.go | 12 ++++++++++++ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/libs/wallet/provider/uphold/uphold.go b/libs/wallet/provider/uphold/uphold.go index 8e1ac7564..173b449cb 100644 --- a/libs/wallet/provider/uphold/uphold.go +++ b/libs/wallet/provider/uphold/uphold.go @@ -237,12 +237,7 @@ func submit( if err != nil { return nil, resp, fmt.Errorf("%w: %s", errorutils.ErrFailedBodyRead, err.Error()) } - sbody := string(body) - for _, p := range []string{`"description":\s*"[^"]+"\s*,?`, `"\w+Country":\s*"[^"]+"\s*,?`} { - re := regexp.MustCompile(p) - sbody = re.ReplaceAllString(sbody, "") - } - sbody = regexp.MustCompile(`,}`).ReplaceAllString(sbody, "}") + sbody := redactUnneededContent(string(body)) if logger != nil { logger.Debug(). @@ -1187,3 +1182,12 @@ func FundWallet(ctx context.Context, destWallet *Wallet, amount decimal.Decimal) return balance.TotalProbi, nil } + +func redactUnneededContent(sbody string) string { + for _, p := range []string{`"description":\s*"[^"]+"\s*,?`, `"\w+Country":\s*"[^"]+"\s*,?`} { + re := regexp.MustCompile(p) + sbody = re.ReplaceAllString(sbody, "") + } + sbody = strings.ReplaceAll(sbody, ",}", "}") + return sbody +} diff --git a/libs/wallet/provider/uphold/uphold_test.go b/libs/wallet/provider/uphold/uphold_test.go index 84d5f023f..45cccd3e6 100644 --- a/libs/wallet/provider/uphold/uphold_test.go +++ b/libs/wallet/provider/uphold/uphold_test.go @@ -3,6 +3,7 @@ package uphold import ( "context" "encoding/hex" + "encoding/json" "errors" "net/http" "net/url" @@ -351,3 +352,14 @@ func requireDonorWallet(t *testing.T) *Wallet { return &Wallet{Info: info, PrivKey: privateKey, PubKey: publicKey} } + +func TestRedactUnneeded(t *testing.T) { + response := `{"description":"some unneeded content","UKCountry":"foo","TestCountry":"bar","NoMatch": true}` + result := `{"NoMatch": true}` + testValue := redactUnneededContent(response) + assert.Equal(t, result, testValue) + var dat map[string]interface{} + if err := json.Unmarshal([]byte(testValue), &dat); err != nil { + t.Fatal(err) + } +} From e09aa436e1fc483c25aa308104bfcdd0adbbfac1 Mon Sep 17 00:00:00 2001 From: Jackson Date: Mon, 25 Nov 2024 11:29:25 -0500 Subject: [PATCH 3/3] Precompile regex --- libs/wallet/provider/uphold/uphold.go | 10 ++++++---- libs/wallet/provider/uphold/uphold_test.go | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/libs/wallet/provider/uphold/uphold.go b/libs/wallet/provider/uphold/uphold.go index 173b449cb..f860b47c0 100644 --- a/libs/wallet/provider/uphold/uphold.go +++ b/libs/wallet/provider/uphold/uphold.go @@ -1183,11 +1183,13 @@ func FundWallet(ctx context.Context, destWallet *Wallet, amount decimal.Decimal) return balance.TotalProbi, nil } +var redactRegexp = []regexp.Regexp{ + *regexp.MustCompile(`"description":\s*"[^"]+"\s*,?`), + *regexp.MustCompile(`"\w+Country":\s*"[^"]+"\s*,?`), +} func redactUnneededContent(sbody string) string { - for _, p := range []string{`"description":\s*"[^"]+"\s*,?`, `"\w+Country":\s*"[^"]+"\s*,?`} { - re := regexp.MustCompile(p) + for _, re := range redactRegexp { sbody = re.ReplaceAllString(sbody, "") } - sbody = strings.ReplaceAll(sbody, ",}", "}") - return sbody + return strings.ReplaceAll(sbody, ",}", "}") } diff --git a/libs/wallet/provider/uphold/uphold_test.go b/libs/wallet/provider/uphold/uphold_test.go index 45cccd3e6..16bcaf918 100644 --- a/libs/wallet/provider/uphold/uphold_test.go +++ b/libs/wallet/provider/uphold/uphold_test.go @@ -354,7 +354,7 @@ func requireDonorWallet(t *testing.T) *Wallet { } func TestRedactUnneeded(t *testing.T) { - response := `{"description":"some unneeded content","UKCountry":"foo","TestCountry":"bar","NoMatch": true}` + response := `{"description":"some unneeded content","OKCountry":"foo","TestCountry":"bar","NoMatch": true}` result := `{"NoMatch": true}` testValue := redactUnneededContent(response) assert.Equal(t, result, testValue)