Skip to content

Commit

Permalink
add target resolution option and test
Browse files Browse the repository at this point in the history
  • Loading branch information
eshitachandwani committed Nov 20, 2024
1 parent e724a9a commit 975e235
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 7 deletions.
5 changes: 2 additions & 3 deletions internal/resolver/delegatingresolver/delegatingresolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func mapAddress(address string) (*url.URL, error) {

// New creates a new delegating resolver that will call the target and proxy
// child resolvers based on the proxy enviorinment configurations.
func New(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOptions, targetResolverBuilder resolver.Builder) (resolver.Resolver, error) {
func New(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOptions, targetResolverBuilder resolver.Builder, targetResolutionEnabled bool) (resolver.Resolver, error) {
r := &delegatingResolver{
target: target,
cc: cc,
Expand All @@ -87,15 +87,14 @@ func New(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOpti
}

if r.proxyURL == nil {
// Add an info log here.
logger.Info("No proxy URL detected")
return targetResolverBuilder.Build(target, cc, opts)
}

// When the scheme is 'dns', resolution should be handled by the proxy, not
// the client. Therefore, we bypass the target resolver and store the
// unresolved target address.
if target.URL.Scheme == "dns" {
if target.URL.Scheme == "dns" && !targetResolutionEnabled {
r.targetAddrs = []resolver.Address{{Addr: target.Endpoint()}}
} else {
// targetResolverBuilder must not be nil. If it is nil, the channel should
Expand Down
61 changes: 57 additions & 4 deletions internal/resolver/delegatingresolver/delegatingresolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (s) TestDelegatingResolverNoProxy(t *testing.T) {

tcc, stateCh, _ := createTestResolverClientConn(t)
// Create a delegating resolver with no proxy configuration
dr, err := New(resolver.Target{URL: *testutils.MustParseURL(target)}, tcc, resolver.BuildOptions{}, mr)
dr, err := New(resolver.Target{URL: *testutils.MustParseURL(target)}, tcc, resolver.BuildOptions{}, mr, false)
if err != nil || dr == nil {
t.Fatalf("Failed to create delegating resolver: %v", err)
}
Expand Down Expand Up @@ -144,10 +144,63 @@ func setupDNS(t *testing.T) *manual.Resolver {
return mr
}

// TestDelegatingResolverwithDNSAndProxy verifies that the delegating resolver
// correctly updates state when the target URI scheme is DNS and a proxy is
// configured and target resolution is enabled.
func (s) TestDelegatingResolverwithDNSAndProxyWithTargetResolution(t *testing.T) {
// Enable HTTP Proxy env var.
origHTTPProxy := envconfig.HTTPProxy
envconfig.HTTPProxy = "testProxyAddr.com"
defer func() { envconfig.HTTPProxy = origHTTPProxy }()
hpfe := func(req *http.Request) (*url.URL, error) {
if req.URL.Host == targetTestAddr {
return &url.URL{
Scheme: "https",
Host: envProxyAddr,
}, nil
}
return nil, nil
}
defer overwrite(hpfe)()
mrTarget := setupDNS(t) // Manual resolver to control the target resolution.
mrProxy := setupDNS(t) // Set up a manual DNS resolver to control the proxy address resolution.
target := "dns:///" + targetTestAddr

tcc, stateCh, _ := createTestResolverClientConn(t)
dr, err := New(resolver.Target{URL: *testutils.MustParseURL(target)}, tcc, resolver.BuildOptions{}, mrTarget, true)
if err != nil {
t.Fatalf("Failed to create delegating resolver: %v", err)
}
if dr == nil {
t.Fatalf("Failed to create delegating resolver")
}
mrTarget.UpdateState(resolver.State{
Addresses: []resolver.Address{
{Addr: resolvedTargetTestAddr},
},
ServiceConfig: &serviceconfig.ParseResult{},
})

mrProxy.UpdateState(resolver.State{
Addresses: []resolver.Address{{Addr: resolvedProxyTestAddr}},
ServiceConfig: &serviceconfig.ParseResult{},
})

// Verify that the delegating resolver outputs the same address.
expectedAddr := resolver.Address{Addr: resolvedProxyTestAddr}
expectedAddr = SetConnectAddr(expectedAddr, resolvedTargetTestAddr)
expectedState := resolver.State{Addresses: []resolver.Address{expectedAddr}}

state := <-stateCh
if len(state.Addresses) != 1 || !cmp.Equal(expectedState, state) {
t.Fatalf("Unexpected state from delegating resolver: %v\n, want %v\n", state, expectedState)
}
}

// TestDelegatingResolverwithDNSAndProxy verifies that the delegating resolver
// correctly updates state when the target URI scheme is DNS and a proxy is
// configured.
func (s) TestDelegatingResolverwithDNSAndProxy(t *testing.T) {
func (s) TestDelegatingResolverwithDNSAndProxyWithNoTargetResolution(t *testing.T) {
// Enable HTTP Proxy env var.
origHTTPProxy := envconfig.HTTPProxy
envconfig.HTTPProxy = "testProxyAddr.com"
Expand All @@ -167,7 +220,7 @@ func (s) TestDelegatingResolverwithDNSAndProxy(t *testing.T) {
target := "dns:///" + targetTestAddr

tcc, stateCh, _ := createTestResolverClientConn(t)
dr, err := New(resolver.Target{URL: *testutils.MustParseURL(target)}, tcc, resolver.BuildOptions{}, mrTarget)
dr, err := New(resolver.Target{URL: *testutils.MustParseURL(target)}, tcc, resolver.BuildOptions{}, mrTarget, false)
if err != nil {
t.Fatalf("Failed to create delegating resolver: %v", err)
}
Expand Down Expand Up @@ -215,7 +268,7 @@ func (s) TestDelegatingResolverwithCustomResolverAndProxy(t *testing.T) {
target := "test:///" + targetTestAddr

tcc, stateCh, _ := createTestResolverClientConn(t)
dr, err := New(resolver.Target{URL: *testutils.MustParseURL(target)}, tcc, resolver.BuildOptions{}, mrTarget)
dr, err := New(resolver.Target{URL: *testutils.MustParseURL(target)}, tcc, resolver.BuildOptions{}, mrTarget, false)
if err != nil {
t.Fatalf("Failed to create delegating resolver: %v", err)
}
Expand Down

0 comments on commit 975e235

Please sign in to comment.