Skip to content

Commit

Permalink
Fix 3204: consul to accept hostnames again (#3211)
Browse files Browse the repository at this point in the history
Signed-off-by: Bernd Verst <[email protected]>
  • Loading branch information
berndverst authored Nov 6, 2023
1 parent 7fc784e commit fe466be
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
8 changes: 7 additions & 1 deletion nameresolution/consul/consul.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,13 @@ func formatAddress(address string, port string) (addr string, err error) {
return fmt.Sprintf("[%s]:%s", address, port), nil
}

return "", fmt.Errorf("invalid ip address %s", address)
// addr is not a valid IP address
// use net.JoinHostPort to format address if address is a valid hostname
if _, err := net.LookupHost(address); err == nil {
return net.JoinHostPort(address, port), nil
}

return "", fmt.Errorf("invalid ip address or unreachable hostname: %s", address)
}

// getConfig configuration from metadata, defaults are best suited for self-hosted mode.
Expand Down
29 changes: 29 additions & 0 deletions nameresolution/consul/consul_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,35 @@ func TestResolveID(t *testing.T) {
assert.Equal(t, "[2001:db8:3333:4444:5555:6666:7777:8888]:50005", addr)
},
},
{
"should get localhost (hostname) from service",
nr.ResolveRequest{
ID: "test-app",
},
func(t *testing.T, req nr.ResolveRequest) {
t.Helper()
mock := mockClient{
mockHealth: mockHealth{
serviceResult: []*consul.ServiceEntry{
{
Service: &consul.AgentService{
Address: "localhost",
Port: 8600,
Meta: map[string]string{
"DAPR_PORT": "50005",
},
},
},
},
},
}
resolver := newResolver(logger.NewLogger("test"), testConfig, &mock, &registry{}, make(chan struct{}))

addr, _ := resolver.ResolveID(context.Background(), req)

assert.Equal(t, "localhost:50005", addr)
},
},
{
"should get random address from service",
nr.ResolveRequest{
Expand Down

0 comments on commit fe466be

Please sign in to comment.