Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(nodebuilder): Fix dns lookup #2332

Merged
merged 10 commits into from
Jun 13, 2023
9 changes: 6 additions & 3 deletions libs/utils/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@ func ValidateAddr(addr string) (string, error) {
}

if ip := net.ParseIP(addr); ip == nil {
_, err = net.LookupHost(addr)
addrs, err := net.LookupHost(addr)
if err != nil {
return addr, fmt.Errorf("could not resolve hostname or ip: %w", err)
return addr, fmt.Errorf("could not resolve %v: %w", addr, err)
}
if len(addrs) == 0 {
return addr, fmt.Errorf("no IP addresses found for DNS record: %v", addr)
}
addr = addrs[0]
}

return addr, nil
}
35 changes: 29 additions & 6 deletions libs/utils/address_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package utils

import (
"net"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -13,8 +14,12 @@ func TestSanitizeAddr(t *testing.T) {
}{
// Testcase: trims protocol prefix
{addr: "http://celestia.org", want: "celestia.org"},
// Testcase: protocol prefix trimmed already
{addr: "celestia.org", want: "celestia.org"},
// Testcase: trims protocol prefix, and trims port and trailing slash suffix
{addr: "tcp://192.168.42.42:5050/", want: "192.168.42.42"},
// Testcase: invariant ip
{addr: "192.168.42.42", want: "192.168.42.42"},
}

for _, tt := range tests {
Expand All @@ -27,23 +32,41 @@ func TestSanitizeAddr(t *testing.T) {
}

func TestValidateAddr(t *testing.T) {
type want struct {
addr string
unresolved bool
}
var tests = []struct {
addr string
want string
want want
}{
// Testcase: ip is valid
{addr: "192.168.42.42:5050", want: "192.168.42.42"},
// Testcase: hostname is valid
{addr: "https://celestia.org", want: "celestia.org"},
{addr: "192.168.42.42:5050", want: want{addr: "192.168.42.42"}},
// Testcase: ip is valid, no port
{addr: "192.168.42.42", want: want{addr: "192.168.42.42"}},
// Testcase: resolves localhost
{addr: "http://localhost:8080/", want: "localhost"},
{addr: "http://localhost:8080/", want: want{unresolved: true}},
// Testcase: hostname is valid
{addr: "https://celestia.org", want: want{unresolved: true}},
// Testcase: hostname is valid, but no schema
{addr: "celestia.org", want: want{unresolved: true}},
}

for _, tt := range tests {
t.Run(tt.addr, func(t *testing.T) {
got, err := ValidateAddr(tt.addr)
require.NoError(t, err)
require.Equal(t, tt.want, got)

// validate that returned value is ip
if ip := net.ParseIP(got); ip == nil {
t.Fatalf("empty ip")
}

if tt.want.unresolved {
// unresolved addr has no addr to compare with
return
}
require.Equal(t, tt.want.addr, got)
})
}
}
27 changes: 6 additions & 21 deletions nodebuilder/core/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package core

import (
"fmt"
"net"
"net/url"

"github.com/spf13/cobra"
flag "github.com/spf13/pflag"
Expand Down Expand Up @@ -40,7 +38,10 @@ func Flags() *flag.FlagSet {
}

// ParseFlags parses Core flags from the given cmd and saves them to the passed config.
func ParseFlags(cmd *cobra.Command, cfg *Config) error {
func ParseFlags(
cmd *cobra.Command,
cfg *Config,
) error {
coreIP := cmd.Flag(coreFlag).Value.String()
if coreIP == "" {
if cmd.Flag(coreGRPCFlag).Changed || cmd.Flag(coreRPCFlag).Changed {
Expand All @@ -49,27 +50,11 @@ func ParseFlags(cmd *cobra.Command, cfg *Config) error {
return nil
}

ip := net.ParseIP(coreIP)
if ip == nil {
u, err := url.Parse(coreIP)
if err != nil {
return fmt.Errorf("failed to parse url: %w", err)
}
ips, err := net.LookupIP(u.Host)
if err != nil {
return fmt.Errorf("failed to resolve DNS record: %v", err)
}
if len(ips) == 0 {
return fmt.Errorf("no IP addresses found for DNS record")
}
ip = ips[0]
}

rpc := cmd.Flag(coreRPCFlag).Value.String()
grpc := cmd.Flag(coreGRPCFlag).Value.String()

cfg.IP = ip.String()
cfg.IP = coreIP
cfg.RPCPort = rpc
cfg.GRPCPort = grpc
return nil
return cfg.Validate()
}