Skip to content

Commit

Permalink
fix(libs/utils): rely on DNS for rpc/grpc requests to core (celestiao…
Browse files Browse the repository at this point in the history
  • Loading branch information
ramin authored and sebasti810 committed Aug 14, 2024
1 parent e1f44a8 commit 784db4b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
13 changes: 8 additions & 5 deletions libs/utils/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"net"
"net/netip"
"strings"
)

Expand All @@ -24,19 +25,21 @@ func SanitizeAddr(addr string) (string, error) {
return addr, nil
}

// ValidateAddr sanitizes the given address and verifies that it is a valid IP or hostname. The
// sanitized address is returned.
// ValidateAddr sanitizes the given address and verifies that it
// is a valid IP or hostname. The sanitized address is returned.
func ValidateAddr(addr string) (string, error) {
addr, err := SanitizeAddr(addr)
if err != nil {
return addr, err
}

ip := net.ParseIP(addr)
if ip != nil {
return addr, nil
// if the address is a valid IP, return it
ip, err := netip.ParseAddr(addr)
if err == nil {
return ip.String(), nil
}

// if the address is not a valid IP, resolve it
resolved, err := net.ResolveIPAddr("ip4", addr)
if err != nil {
return addr, err
Expand Down
2 changes: 2 additions & 0 deletions libs/utils/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ func TestValidateAddr(t *testing.T) {
{addr: "https://celestia.org", want: want{unresolved: true}},
// Testcase: hostname is valid, but no schema
{addr: "celestia.org", want: want{unresolved: true}},
// Testcase: localhost
{addr: "localhost", want: want{addr: "127.0.0.1"}},
}

for _, tt := range tests {
Expand Down
2 changes: 1 addition & 1 deletion nodebuilder/core/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (cfg *Config) Validate() error {
return fmt.Errorf("nodebuilder/core: grpc port is not set")
}

ip, err := utils.ValidateAddr(cfg.IP)
ip, err := utils.SanitizeAddr(cfg.IP)
if err != nil {
return err
}
Expand Down
13 changes: 11 additions & 2 deletions nodebuilder/core/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ func TestValidate(t *testing.T) {
cfg: Config{},
expectErr: false,
},
{
name: "hostname preserved",
cfg: Config{
IP: "celestia.org",
RPCPort: DefaultRPCPort,
GRPCPort: DefaultGRPCPort,
},
expectErr: false,
},
{
name: "missing RPC port",
cfg: Config{
Expand All @@ -43,13 +52,13 @@ func TestValidate(t *testing.T) {
expectErr: true,
},
{
name: "invalid IP",
name: "invalid IP, but will be accepted as host and not raise an error",
cfg: Config{
IP: "invalid-ip",
RPCPort: DefaultRPCPort,
GRPCPort: DefaultGRPCPort,
},
expectErr: true,
expectErr: false,
},
{
name: "invalid RPC port",
Expand Down

0 comments on commit 784db4b

Please sign in to comment.