From 90ea24bc33f2663c414a57e1eb71a2bc98b5ddfa Mon Sep 17 00:00:00 2001 From: Lei Date: Wed, 27 Mar 2024 11:03:44 -0700 Subject: [PATCH] Trailing slash (#12606) * remove trailing slash in mercury credentials' URLs * remove trailing slash and make it threadsafe * trim slashes once in NewMercuryConfig * nil protection --------- Co-authored-by: amirylm --- .changeset/strange-swans-compare.md | 5 ++ .../ocr2keeper/evmregistry/v21/registry.go | 19 ++++---- .../evmregistry/v21/registry_test.go | 48 ++++++++++++++++++- 3 files changed, 62 insertions(+), 10 deletions(-) create mode 100644 .changeset/strange-swans-compare.md diff --git a/.changeset/strange-swans-compare.md b/.changeset/strange-swans-compare.md new file mode 100644 index 00000000000..a5690cc5d93 --- /dev/null +++ b/.changeset/strange-swans-compare.md @@ -0,0 +1,5 @@ +--- +"chainlink": patch +--- + +remove trailing slash diff --git a/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/registry.go b/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/registry.go index bb6bd3c0aff..206932cf543 100644 --- a/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/registry.go +++ b/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/registry.go @@ -6,6 +6,7 @@ import ( "fmt" "math/big" "net/http" + "strings" "sync" "time" @@ -90,12 +91,7 @@ func NewEvmRegistry( blockSub *BlockSubscriber, finalityDepth uint32, ) *EvmRegistry { - mercuryConfig := &MercuryConfig{ - cred: mc, - Abi: core.StreamsCompatibleABI, - AllowListCache: cache.New(defaultAllowListExpiration, cleanupInterval), - pluginRetryCache: cache.New(defaultPluginRetryExpiration, cleanupInterval), - } + mercuryConfig := NewMercuryConfig(mc, core.StreamsCompatibleABI) hc := http.DefaultClient return &EvmRegistry{ @@ -138,9 +134,16 @@ type MercuryConfig struct { pluginRetryCache *cache.Cache } -func NewMercuryConfig(credentials *types.MercuryCredentials, abi abi.ABI) *MercuryConfig { +func NewMercuryConfig(cred *types.MercuryCredentials, abi abi.ABI) *MercuryConfig { + c := &types.MercuryCredentials{} + if cred != nil { + c.Password = cred.Password + c.Username = cred.Username + c.URL = strings.TrimRight(cred.URL, "/") + c.LegacyURL = strings.TrimRight(cred.LegacyURL, "/") + } return &MercuryConfig{ - cred: credentials, + cred: c, Abi: abi, AllowListCache: cache.New(defaultPluginRetryExpiration, cleanupInterval), pluginRetryCache: cache.New(defaultPluginRetryExpiration, cleanupInterval), diff --git a/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/registry_test.go b/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/registry_test.go index 024c5e79925..4dcb72fde86 100644 --- a/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/registry_test.go +++ b/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/registry_test.go @@ -8,14 +8,15 @@ import ( "testing" "time" - types2 "github.com/smartcontractkit/chainlink-automation/pkg/v3/types" - "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/common" coreTypes "github.com/ethereum/go-ethereum/core/types" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" + types2 "github.com/smartcontractkit/chainlink-automation/pkg/v3/types" + "github.com/smartcontractkit/chainlink-common/pkg/types" + types3 "github.com/smartcontractkit/chainlink/v2/core/chains/evm/headtracker/types" "github.com/smartcontractkit/chainlink/v2/core/chains/evm/logpoller" "github.com/smartcontractkit/chainlink/v2/core/chains/evm/logpoller/mocks" @@ -29,6 +30,49 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/logprovider" ) +func TestMercuryConfig_RemoveTrailingSlash(t *testing.T) { + tests := []struct { + Name string + URL string + LegacyURL string + }{ + { + Name: "Both have trailing slashes", + URL: "http://example.com/", + LegacyURL: "http://legacy.example.com/", + }, + { + Name: "One has trailing slashes", + URL: "http://example.com", + LegacyURL: "http://legacy.example.com/", + }, + { + Name: "Neither has trailing slashes", + URL: "http://example.com", + LegacyURL: "http://legacy.example.com", + }, + } + + for _, test := range tests { + t.Run(test.Name, func(t *testing.T) { + mockConfig := NewMercuryConfig(&types.MercuryCredentials{ + URL: test.URL, + LegacyURL: test.LegacyURL, + Username: "user", + Password: "pass", + }, core.StreamsCompatibleABI) + + result := mockConfig.Credentials() + + // Assert that trailing slashes are removed + assert.Equal(t, "http://example.com", result.URL) + assert.Equal(t, "http://legacy.example.com", result.LegacyURL) + assert.Equal(t, "user", result.Username) + assert.Equal(t, "pass", result.Password) + }) + } +} + func TestPollLogs(t *testing.T) { tests := []struct { Name string