From 88013272f4087339a3fe3b47c9a1d6ebe90d7562 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Toledano?= Date: Tue, 12 Mar 2024 09:40:56 +0100 Subject: [PATCH] fix: connect over https (#99) * fix: connect over https * fixes + changelog * make lint happy --- CHANGELOG.md | 4 +++ config.go | 38 ++++++++++++++++++++++++--- config_test.go | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 3 +++ go.sum | 6 +++++ 5 files changed, 117 insertions(+), 3 deletions(-) create mode 100644 config_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index f14e690..4184d62 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,10 @@ Ref: https://keepachangelog.com/en/1.0.0/ ## [Unreleased] +### Bug Fixes + +* [99](https://github.com/cosmos/rosetta/pull/99) Rosetta now can connect to cometBFT over HTTPS. + ### Improvements * [93](https://github.com/cosmos/rosetta/pull/93) Removes the use of `LegacyMsg.GetSigners()` in favor of `codec.GetMsgV1Signers`. diff --git a/config.go b/config.go index 395f99f..67f44ce 100644 --- a/config.go +++ b/config.go @@ -2,12 +2,12 @@ package rosetta import ( "fmt" - "strings" "time" crgerrs "github.com/cosmos/rosetta/lib/errors" "github.com/coinbase/rosetta-sdk-go/types" + url "github.com/goware/urlx" "github.com/spf13/pflag" crg "github.com/cosmos/rosetta/lib/server" @@ -18,6 +18,14 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" ) +const ( + HTTP = "http" + HTTPS = "https" + TCP = "tcp" + + HTTPSPORT = "443" +) + // configuration defaults constants const ( // DefaultBlockchain defines the default blockchain identifier name @@ -150,13 +158,37 @@ func (c *Config) validate() error { if c.TendermintRPC == "" { return crgerrs.WrapError(crgerrs.ErrConfig, "cometbft rpc not provided") } - if !strings.HasPrefix(c.TendermintRPC, "tcp://") { - c.TendermintRPC = fmt.Sprintf("tcp://%s", c.TendermintRPC) + validatedURL, err := c.validateURL(c.TendermintRPC) + if err != nil { + return err } + c.TendermintRPC = validatedURL return nil } +func (c *Config) validateURL(tendermintRPC string) (string, error) { + u, err := url.Parse(tendermintRPC) + if err != nil { + return "", crgerrs.WrapError(crgerrs.ErrConfig, err.Error()) + } + + if u.Port() == HTTPSPORT && u.Scheme != HTTPS { + u.Scheme = HTTPS + } + + if u.Port() == "" { + switch u.Scheme { + case HTTP, TCP: + u.Host += ":80" + case HTTPS: + u.Host += ":443" + } + } + + return u.String(), nil +} + // WithCodec extends the configuration with a predefined Codec func (c *Config) WithCodec(ir codectypes.InterfaceRegistry, cdc *codec.ProtoCodec) { c.Codec = cdc diff --git a/config_test.go b/config_test.go new file mode 100644 index 0000000..de8a455 --- /dev/null +++ b/config_test.go @@ -0,0 +1,69 @@ +package rosetta + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestConfig_validateUrl(t *testing.T) { + tests := []struct { + name string + tendermintRPC string + expected string + }{ + { + name: "complete url", + tendermintRPC: "https://myhost.com:443", + expected: "https://myhost.com:443", + }, + { + name: "no schema no port", + tendermintRPC: "myhost.com", + expected: "http://myhost.com:80", + }, + { + name: "http schema with no port", + tendermintRPC: "http://myHost.com", + expected: "http://myhost.com:80", + }, + { + name: "https schema with no port", + tendermintRPC: "https://myHost.com", + expected: "https://myhost.com:443", + }, + { + name: "no schema with port", + tendermintRPC: "myHost.com:2344", + expected: "http://myhost.com:2344", + }, + { + name: "no schema with port 443", + tendermintRPC: "myHost.com:443", + expected: "https://myhost.com:443", + }, + { + name: "tcp schema", + tendermintRPC: "tcp://localhost:26657", + expected: "tcp://localhost:26657", + }, + { + name: "localhost", + tendermintRPC: "localhost", + expected: "http://localhost:80", + }, + { + name: "not normalized url", + tendermintRPC: "hTTp://tHISmyWebsite.COM", + expected: "http://thismywebsite.com:80", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + c := &Config{} + got, err := c.validateURL(tt.tendermintRPC) + require.NoError(t, err) + require.Equal(t, tt.expected, got) + }) + } +} diff --git a/go.mod b/go.mod index ec11a0b..1a6b1d8 100644 --- a/go.mod +++ b/go.mod @@ -13,6 +13,7 @@ require ( github.com/cosmos/gogoproto v1.4.11 github.com/cosmos/rosetta-sdk-go v0.10.0 github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 + github.com/goware/urlx v0.3.2 github.com/spf13/cobra v1.8.0 github.com/spf13/pflag v1.0.5 github.com/stretchr/testify v1.8.4 @@ -31,6 +32,8 @@ require ( github.com/99designs/keyring v1.2.1 // indirect github.com/DataDog/datadog-go v3.2.0+incompatible // indirect github.com/DataDog/zstd v1.5.5 // indirect + github.com/PuerkitoBio/purell v1.1.1 // indirect + github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect github.com/beorn7/perks v1.0.1 // indirect github.com/bgentry/speakeasy v0.1.1-0.20220910012023-760eaf8b6816 // indirect github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect diff --git a/go.sum b/go.sum index e3c4f5f..9690746 100644 --- a/go.sum +++ b/go.sum @@ -34,6 +34,10 @@ github.com/DataDog/zstd v1.5.5/go.mod h1:g4AWEaM3yOg3HYfnJ3YIawPnVdXJh9QME85blwS github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0= github.com/OneOfOne/xxhash v1.2.2 h1:KMrpdQIwFcEqXDklaen+P1axHaj9BSKzvpUUfnHldSE= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= +github.com/PuerkitoBio/purell v1.1.1 h1:WEQqlqaGbrPkxLJWfBwQmfEAE1Z7ONdDLqrN38tNFfI= +github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0= +github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 h1:d+Bc7a5rLufV/sSk/8dngufqelfh6jnri85riMAaF/M= +github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE= github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo= github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI= github.com/VividCortex/gohistogram v1.0.0 h1:6+hBz+qvs0JOrrNhhmR7lFxo5sINxBCGXrdtl/UvroE= @@ -334,6 +338,8 @@ github.com/gorilla/websocket v0.0.0-20170926233335-4201258b820c/go.mod h1:E7qHFY github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/goware/urlx v0.3.2 h1:gdoo4kBHlkqZNaf6XlQ12LGtQOmpKJrR04Rc3RnpJEo= +github.com/goware/urlx v0.3.2/go.mod h1:h8uwbJy68o+tQXCGZNa9D73WN8n0r9OBae5bUnLcgjw= github.com/grpc-ecosystem/go-grpc-middleware v1.0.1-0.20190118093823-f849b5445de4/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs= github.com/grpc-ecosystem/go-grpc-middleware v1.2.2/go.mod h1:EaizFBKfUKtMIF5iaDEhniwNedqGo9FuLFzppDr3uwI= github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 h1:UH//fgunKIs4JdUbpDl1VZCDaL56wXCB/5+wF6uHfaI=