From d5ef3128ec98d55e5c8217fe25e3178d71f5fce3 Mon Sep 17 00:00:00 2001 From: lei shi Date: Thu, 22 Feb 2024 15:01:27 -0800 Subject: [PATCH 1/4] remove trailing slash in mercury credentials' URLs --- .../ocr2keeper/evmregistry/v21/registry.go | 4 ++ .../evmregistry/v21/registry_test.go | 53 ++++++++++++++++++- 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/registry.go b/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/registry.go index bb6bd3c0aff..362ca59c3c7 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" @@ -148,6 +149,9 @@ func NewMercuryConfig(credentials *types.MercuryCredentials, abi abi.ABI) *Mercu } func (c *MercuryConfig) Credentials() *types.MercuryCredentials { + // remove the trailing slash from the URL if it exists + c.cred.URL = strings.TrimRight(c.cred.URL, "/") + c.cred.LegacyURL = strings.TrimRight(c.cred.LegacyURL, "/") return c.cred } 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..456b34e6bde 100644 --- a/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/registry_test.go +++ b/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/registry_test.go @@ -8,7 +8,7 @@ import ( "testing" "time" - types2 "github.com/smartcontractkit/chainlink-automation/pkg/v3/types" + "github.com/patrickmn/go-cache" "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/common" @@ -16,6 +16,9 @@ import ( "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 +32,54 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/logprovider" ) +func TestCredentials_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 := &MercuryConfig{ + cred: &types.MercuryCredentials{ + URL: test.URL, + LegacyURL: test.LegacyURL, + Username: "user", + Password: "pass", + }, + Abi: core.StreamsCompatibleABI, + AllowListCache: cache.New(defaultPluginRetryExpiration, cleanupInterval), + pluginRetryCache: cache.New(defaultPluginRetryExpiration, cleanupInterval), + } + + 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 From e368bd4b2cff328613962d1c8f4ad9ce7fbc046c Mon Sep 17 00:00:00 2001 From: lei shi Date: Tue, 26 Mar 2024 15:58:16 -0700 Subject: [PATCH 2/4] remove trailing slash and make it threadsafe --- .changeset/strange-swans-compare.md | 5 +++++ .../plugins/ocr2keeper/evmregistry/v21/registry.go | 14 ++++++++++---- 2 files changed, 15 insertions(+), 4 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 362ca59c3c7..5834ed3cfde 100644 --- a/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/registry.go +++ b/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/registry.go @@ -149,10 +149,16 @@ func NewMercuryConfig(credentials *types.MercuryCredentials, abi abi.ABI) *Mercu } func (c *MercuryConfig) Credentials() *types.MercuryCredentials { - // remove the trailing slash from the URL if it exists - c.cred.URL = strings.TrimRight(c.cred.URL, "/") - c.cred.LegacyURL = strings.TrimRight(c.cred.LegacyURL, "/") - return c.cred + // make a copy and remove the trailing slash from the URL if it exists + cred := &types.MercuryCredentials{ + URL: c.cred.URL, + LegacyURL: c.cred.LegacyURL, + Password: c.cred.Password, + Username: c.cred.Username, + } + cred.LegacyURL = strings.TrimRight(cred.LegacyURL, "/") + cred.URL = strings.TrimRight(cred.URL, "/") + return cred } func (c *MercuryConfig) IsUpkeepAllowed(k string) (interface{}, bool) { From 81acc5170b2b1932e381d9e91a86d6b4342645be Mon Sep 17 00:00:00 2001 From: amirylm Date: Wed, 27 Mar 2024 10:04:45 +0200 Subject: [PATCH 3/4] trim slashes once in NewMercuryConfig --- .../ocr2keeper/evmregistry/v21/registry.go | 28 +++++++------------ .../evmregistry/v21/registry_test.go | 21 +++++--------- 2 files changed, 17 insertions(+), 32 deletions(-) diff --git a/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/registry.go b/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/registry.go index 5834ed3cfde..4b3b4290158 100644 --- a/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/registry.go +++ b/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/registry.go @@ -91,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{ @@ -139,9 +134,15 @@ type MercuryConfig struct { pluginRetryCache *cache.Cache } -func NewMercuryConfig(credentials *types.MercuryCredentials, abi abi.ABI) *MercuryConfig { +func NewMercuryConfig(cred *types.MercuryCredentials, abi abi.ABI) *MercuryConfig { return &MercuryConfig{ - cred: credentials, + cred: &types.MercuryCredentials{ + // removing trailing slashes from URLs + URL: strings.TrimRight(cred.URL, "/"), + LegacyURL: strings.TrimRight(cred.LegacyURL, "/"), + Password: cred.Password, + Username: cred.Username, + }, Abi: abi, AllowListCache: cache.New(defaultPluginRetryExpiration, cleanupInterval), pluginRetryCache: cache.New(defaultPluginRetryExpiration, cleanupInterval), @@ -149,16 +150,7 @@ func NewMercuryConfig(credentials *types.MercuryCredentials, abi abi.ABI) *Mercu } func (c *MercuryConfig) Credentials() *types.MercuryCredentials { - // make a copy and remove the trailing slash from the URL if it exists - cred := &types.MercuryCredentials{ - URL: c.cred.URL, - LegacyURL: c.cred.LegacyURL, - Password: c.cred.Password, - Username: c.cred.Username, - } - cred.LegacyURL = strings.TrimRight(cred.LegacyURL, "/") - cred.URL = strings.TrimRight(cred.URL, "/") - return cred + return c.cred } func (c *MercuryConfig) IsUpkeepAllowed(k string) (interface{}, bool) { 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 456b34e6bde..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,8 +8,6 @@ import ( "testing" "time" - "github.com/patrickmn/go-cache" - "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/common" coreTypes "github.com/ethereum/go-ethereum/core/types" @@ -32,7 +30,7 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/logprovider" ) -func TestCredentials_RemoveTrailingSlash(t *testing.T) { +func TestMercuryConfig_RemoveTrailingSlash(t *testing.T) { tests := []struct { Name string URL string @@ -57,17 +55,12 @@ func TestCredentials_RemoveTrailingSlash(t *testing.T) { for _, test := range tests { t.Run(test.Name, func(t *testing.T) { - mockConfig := &MercuryConfig{ - cred: &types.MercuryCredentials{ - URL: test.URL, - LegacyURL: test.LegacyURL, - Username: "user", - Password: "pass", - }, - Abi: core.StreamsCompatibleABI, - AllowListCache: cache.New(defaultPluginRetryExpiration, cleanupInterval), - pluginRetryCache: cache.New(defaultPluginRetryExpiration, cleanupInterval), - } + mockConfig := NewMercuryConfig(&types.MercuryCredentials{ + URL: test.URL, + LegacyURL: test.LegacyURL, + Username: "user", + Password: "pass", + }, core.StreamsCompatibleABI) result := mockConfig.Credentials() From df616eb8c26fff1065e66c02f0d0672e20345934 Mon Sep 17 00:00:00 2001 From: amirylm Date: Wed, 27 Mar 2024 15:52:51 +0200 Subject: [PATCH 4/4] nil protection --- .../ocr2keeper/evmregistry/v21/registry.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/registry.go b/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/registry.go index 4b3b4290158..206932cf543 100644 --- a/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/registry.go +++ b/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/registry.go @@ -135,14 +135,15 @@ type MercuryConfig struct { } 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: &types.MercuryCredentials{ - // removing trailing slashes from URLs - URL: strings.TrimRight(cred.URL, "/"), - LegacyURL: strings.TrimRight(cred.LegacyURL, "/"), - Password: cred.Password, - Username: cred.Username, - }, + cred: c, Abi: abi, AllowListCache: cache.New(defaultPluginRetryExpiration, cleanupInterval), pluginRetryCache: cache.New(defaultPluginRetryExpiration, cleanupInterval),