-
Notifications
You must be signed in to change notification settings - Fork 4.6k
delegatingresolver: add default port to addresses #8613
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
base: master
Are you sure you want to change the base?
Changes from all commits
4186a66
2156251
07add50
abc1773
0702e0c
2f50b78
aee0066
3d93bc1
80a3e98
c19e88d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,11 +22,13 @@ package delegatingresolver | |
|
||
import ( | ||
"fmt" | ||
"net" | ||
"net/http" | ||
"net/url" | ||
"sync" | ||
|
||
"google.golang.org/grpc/grpclog" | ||
"google.golang.org/grpc/internal/envconfig" | ||
"google.golang.org/grpc/internal/proxyattributes" | ||
"google.golang.org/grpc/internal/transport" | ||
"google.golang.org/grpc/internal/transport/networktype" | ||
|
@@ -40,6 +42,8 @@ var ( | |
HTTPSProxyFromEnvironment = http.ProxyFromEnvironment | ||
) | ||
|
||
const defaultPort = "443" | ||
|
||
// delegatingResolver manages both target URI and proxy address resolution by | ||
// delegating these tasks to separate child resolvers. Essentially, it acts as | ||
// an intermediary between the gRPC ClientConn and the child resolvers. | ||
|
@@ -107,10 +111,18 @@ func New(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOpti | |
targetResolver: nopResolver{}, | ||
} | ||
|
||
addr := target.Endpoint() | ||
var err error | ||
r.proxyURL, err = proxyURLForTarget(target.Endpoint()) | ||
if target.URL.Scheme == "dns" && !targetResolutionEnabled && envconfig.EnableDefaultPortForProxyTarget { | ||
addr, err = parseTarget(addr) | ||
if err != nil { | ||
return nil, fmt.Errorf("delegating_resolver: invalid target address %q: %v", target.Endpoint(), err) | ||
} | ||
} | ||
|
||
r.proxyURL, err = proxyURLForTarget(addr) | ||
if err != nil { | ||
return nil, fmt.Errorf("delegating_resolver: failed to determine proxy URL for target %s: %v", target, err) | ||
return nil, fmt.Errorf("delegating_resolver: failed to determine proxy URL for target %q: %v", target, err) | ||
} | ||
|
||
// proxy is not configured or proxy address excluded using `NO_PROXY` env | ||
|
@@ -132,8 +144,8 @@ func New(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOpti | |
// bypass the target resolver and store the unresolved target address. | ||
if target.URL.Scheme == "dns" && !targetResolutionEnabled { | ||
r.targetResolverState = &resolver.State{ | ||
Addresses: []resolver.Address{{Addr: target.Endpoint()}}, | ||
Endpoints: []resolver.Endpoint{{Addresses: []resolver.Address{{Addr: target.Endpoint()}}}}, | ||
Addresses: []resolver.Address{{Addr: addr}}, | ||
Endpoints: []resolver.Endpoint{{Addresses: []resolver.Address{{Addr: addr}}}}, | ||
} | ||
r.updateTargetResolverState(*r.targetResolverState) | ||
return r, nil | ||
|
@@ -202,6 +214,31 @@ func needsProxyResolver(state *resolver.State) bool { | |
return false | ||
} | ||
|
||
func parseTarget(target string) (string, error) { | ||
if target == "" { | ||
return "", fmt.Errorf("missing address") | ||
} | ||
if host, port, err := net.SplitHostPort(target); err == nil { | ||
if port == "" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Based on the documentation of
Please correct me if I'm wrong. |
||
// If the port field is empty (target ends with colon), e.g. "[::1]:", | ||
// this is an error. | ||
Comment on lines
+223
to
+224
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optional: I think it would be better to group these inline comments into a docstring for the function. That way, one can just read that docstring and get to know what the function is doing instead of reading it one piece at a time. |
||
return "", fmt.Errorf("missing port after port-separator colon") | ||
} | ||
// target has port, i.e ipv4-host:port, [ipv6-host]:port, host-name:port | ||
if host == "" { | ||
// Keep consistent with net.Dial(): If the host is empty, as in ":80", | ||
// the local system is assumed. | ||
host = "localhost" | ||
} | ||
return net.JoinHostPort(host, port), nil | ||
} | ||
if host, port, err := net.SplitHostPort(target + ":" + defaultPort); err == nil { | ||
// target doesn't have port | ||
return net.JoinHostPort(host, port), nil | ||
} | ||
return net.JoinHostPort(target, defaultPort), nil | ||
} | ||
|
||
func skipProxy(address resolver.Address) bool { | ||
// Avoid proxy when network is not tcp. | ||
networkType, ok := networktype.Get(address) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,10 +23,12 @@ import ( | |
"errors" | ||
"net/http" | ||
"net/url" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"google.golang.org/grpc/internal/envconfig" | ||
"google.golang.org/grpc/internal/grpctest" | ||
"google.golang.org/grpc/internal/proxyattributes" | ||
"google.golang.org/grpc/internal/resolver/delegatingresolver" | ||
|
@@ -246,54 +248,104 @@ func (s) TestDelegatingResolverwithDNSAndProxyWithTargetResolution(t *testing.T) | |
} | ||
} | ||
|
||
// Tests the scenario where a proxy is configured, the target URI contains the | ||
// "dns" scheme, and target resolution is disabled(default behavior). The test | ||
// verifies that the addresses returned by the delegating resolver include the | ||
// proxy resolver's addresses, with the unresolved target URI as an attribute | ||
// of the proxy address. | ||
// Tests the creation of a delegating resolver when a proxy is configured. It | ||
// verifies both successful creation for valid targets and correct error | ||
// handling for invalid ones. | ||
// | ||
// For successful cases, it ensures the final address is from the proxy resolver | ||
// and contains the original, correctly-formatted target address as an | ||
// attribute. | ||
func (s) TestDelegatingResolverwithDNSAndProxyWithNoTargetResolution(t *testing.T) { | ||
const ( | ||
targetTestAddr = "test.com" | ||
envProxyAddr = "proxytest.com" | ||
resolvedProxyTestAddr1 = "11.11.11.11:7687" | ||
) | ||
overrideTestHTTPSProxy(t, envProxyAddr) | ||
|
||
targetResolver := manual.NewBuilderWithScheme("dns") | ||
target := targetResolver.Scheme() + ":///" + targetTestAddr | ||
// Set up a manual DNS resolver to control the proxy address resolution. | ||
proxyResolver, proxyResolverBuilt := setupDNS(t) | ||
|
||
tcc, stateCh, _ := createTestResolverClientConn(t) | ||
if _, err := delegatingresolver.New(resolver.Target{URL: *testutils.MustParseURL(target)}, tcc, resolver.BuildOptions{}, targetResolver, false); err != nil { | ||
t.Fatalf("Failed to create delegating resolver: %v", err) | ||
} | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) | ||
defer cancel() | ||
|
||
// Wait for the proxy resolver to be built before calling UpdateState. | ||
mustBuildResolver(ctx, t, proxyResolverBuilt) | ||
proxyResolver.UpdateState(resolver.State{ | ||
Addresses: []resolver.Address{ | ||
{Addr: resolvedProxyTestAddr1}, | ||
tests := []struct { | ||
name string | ||
target string | ||
wantConnectAddress string | ||
wantErrorSubstring string | ||
disableDefaultPortEnvVar bool | ||
}{ | ||
{ | ||
name: "no port in target", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
target: "test.com", | ||
wantConnectAddress: "test.com:443", | ||
}, | ||
{ | ||
name: "port specified in target", | ||
target: "test.com:8080", | ||
wantConnectAddress: "test.com:8080", | ||
}, | ||
{ | ||
name: "colon after host in target but no post", | ||
target: "test.com:", | ||
wantErrorSubstring: "missing port after port-separator colon", | ||
}, | ||
{ | ||
name: "add default port env variable diabled, no port in target", | ||
target: "test.com", | ||
wantConnectAddress: "test.com", | ||
disableDefaultPortEnvVar: true, | ||
}, | ||
}) | ||
|
||
wantState := resolver.State{ | ||
Addresses: []resolver.Address{proxyAddressWithTargetAttribute(resolvedProxyTestAddr1, targetTestAddr)}, | ||
Endpoints: []resolver.Endpoint{{Addresses: []resolver.Address{proxyAddressWithTargetAttribute(resolvedProxyTestAddr1, targetTestAddr)}}}, | ||
} | ||
|
||
var gotState resolver.State | ||
select { | ||
case gotState = <-stateCh: | ||
case <-ctx.Done(): | ||
t.Fatal("Context timed out when waiting for a state update from the delegating resolver") | ||
} | ||
|
||
if diff := cmp.Diff(gotState, wantState); diff != "" { | ||
t.Fatalf("Unexpected state from delegating resolver. Diff (-got +want):\n%v", diff) | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
if test.disableDefaultPortEnvVar { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you have to check this field? Why can't you set the value of the env var to the one specified by the test unconditionally? That way, the code will be immune to changes in the default value of the env var. |
||
testutils.SetEnvConfig(t, &envconfig.EnableDefaultPortForProxyTarget, false) | ||
} | ||
overrideTestHTTPSProxy(t, envProxyAddr) | ||
|
||
targetResolver := manual.NewBuilderWithScheme("dns") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't you have to unregister the original |
||
target := targetResolver.Scheme() + ":///" + test.target | ||
// Set up a manual DNS resolver to control the proxy address resolution. | ||
proxyResolver, proxyResolverBuilt := setupDNS(t) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh I see that this helper does re-register the original |
||
|
||
tcc, stateCh, _ := createTestResolverClientConn(t) | ||
_, err := delegatingresolver.New(resolver.Target{URL: *testutils.MustParseURL(target)}, tcc, resolver.BuildOptions{}, targetResolver, false) | ||
if test.wantErrorSubstring != "" { | ||
// Case 1: We expected an error. | ||
if err == nil { | ||
t.Fatalf("Delegating resolver created, want error containing %q", test.wantErrorSubstring) | ||
} | ||
if !strings.Contains(err.Error(), test.wantErrorSubstring) { | ||
t.Fatalf("Delegating resolver failed with error %v, want error containing %v", err.Error(), test.wantErrorSubstring) | ||
} | ||
return | ||
} | ||
|
||
// Case 2: We did NOT expect an error. | ||
if err != nil { | ||
t.Fatalf("Delegating resolver creation failed unexpectedly with error: %v", err) | ||
} | ||
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) | ||
defer cancel() | ||
Comment on lines
+307
to
+323
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test logic seems complicated enough to me that you can actually split the tests into two: go/gotip/episodes/50 |
||
|
||
// Wait for the proxy resolver to be built before calling UpdateState. | ||
mustBuildResolver(ctx, t, proxyResolverBuilt) | ||
proxyResolver.UpdateState(resolver.State{ | ||
Addresses: []resolver.Address{ | ||
{Addr: resolvedProxyTestAddr1}, | ||
}, | ||
}) | ||
|
||
wantState := resolver.State{ | ||
Addresses: []resolver.Address{proxyAddressWithTargetAttribute(resolvedProxyTestAddr1, test.wantConnectAddress)}, | ||
Endpoints: []resolver.Endpoint{{Addresses: []resolver.Address{proxyAddressWithTargetAttribute(resolvedProxyTestAddr1, test.wantConnectAddress)}}}, | ||
} | ||
|
||
var gotState resolver.State | ||
select { | ||
case gotState = <-stateCh: | ||
case <-ctx.Done(): | ||
t.Fatal("Context timed out when waiting for a state update from the delegating resolver") | ||
} | ||
|
||
if diff := cmp.Diff(gotState, wantState); diff != "" { | ||
t.Fatalf("Unexpected state from delegating resolver. Diff (-got +want):\n%v", diff) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this even possible since we already parse the target URI specified by the user in clientconn.go before we get here?
Ideally, it would be nicer to avoid checking conditions that are not possible, since it would simplify the code.