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

transport,grpc: Integrate delegating resolver and introduce dial options for target host resolution #7881

Merged
merged 60 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
619dcd4
Change proxy behaviour
eshitachandwani Dec 3, 2024
65b33bd
change delegating resolver
eshitachandwani Dec 4, 2024
94364e2
Improving code
eshitachandwani Dec 5, 2024
9f6a067
correct import
eshitachandwani Dec 5, 2024
5ce86d0
improve tests
eshitachandwani Dec 5, 2024
fbef3a4
address comments
eshitachandwani Dec 5, 2024
342c332
add warning and httpfunc test
eshitachandwani Dec 5, 2024
8726188
add warning and httpfunc test
eshitachandwani Dec 5, 2024
1cfb089
from delegating_pr
eshitachandwani Dec 7, 2024
32d9de5
improve code
eshitachandwani Dec 8, 2024
34b902f
improve code
eshitachandwani Dec 8, 2024
11c8fb1
improve
eshitachandwani Dec 9, 2024
b4c9980
correct tests
eshitachandwani Dec 18, 2024
93fc3e1
trying tests
eshitachandwani Dec 18, 2024
e22ad1d
tests
eshitachandwani Dec 19, 2024
b515565
test
eshitachandwani Dec 19, 2024
7ae2797
correct test environment
eshitachandwani Dec 20, 2024
5ce4658
Correct pr
eshitachandwani Dec 23, 2024
a292b63
Merge branch 'master' into proxy_pr2
eshitachandwani Dec 23, 2024
4483e95
rebase
eshitachandwani Dec 23, 2024
d4f6215
comment
eshitachandwani Dec 23, 2024
333a68c
proxy testutils refactor
eshitachandwani Dec 23, 2024
cacf058
correct vet.sh
eshitachandwani Dec 23, 2024
7c5b1b3
correct vet
eshitachandwani Dec 23, 2024
cb6b09c
Merge branch 'grpc:master' into proxy_pr2
eshitachandwani Dec 27, 2024
ff05ee3
something
eshitachandwani Dec 27, 2024
104cd18
working tests manual resolver
eshitachandwani Dec 27, 2024
fb23cca
working tests manual resolver
eshitachandwani Dec 27, 2024
ef927ce
test
eshitachandwani Dec 27, 2024
bc5efc3
trying test without manual resolver
eshitachandwani Jan 2, 2025
876f09e
e2e tests
eshitachandwani Jan 3, 2025
3e20010
correct vet
eshitachandwani Jan 3, 2025
6827a62
correct e2e tests
eshitachandwani Jan 3, 2025
9acd8de
correct vet
eshitachandwani Jan 3, 2025
8f5055e
vet
eshitachandwani Jan 3, 2025
bbd7f02
vet
eshitachandwani Jan 3, 2025
128da2c
addresses comments
eshitachandwani Jan 7, 2025
7cb9ec8
add package comment
eshitachandwani Jan 7, 2025
0d055b3
correct timeout
eshitachandwani Jan 7, 2025
07b9344
format
eshitachandwani Jan 8, 2025
f75662d
address comments
eshitachandwani Jan 9, 2025
2df2f2e
new test for env variable
eshitachandwani Jan 10, 2025
9e52674
test
eshitachandwani Jan 10, 2025
7d230bc
remove mocking and check enviornment
eshitachandwani Jan 10, 2025
27cc055
comment for httpproxy
eshitachandwani Jan 10, 2025
227c748
address comments
eshitachandwani Jan 14, 2025
bdbe501
remove helper
eshitachandwani Jan 15, 2025
8a43a91
proxy helper function
eshitachandwani Jan 15, 2025
a73d511
proxy helper function
eshitachandwani Jan 15, 2025
0c154cf
proxy helper function
eshitachandwani Jan 15, 2025
d396d31
change proxy_utils
eshitachandwani Jan 16, 2025
fd088d1
change proxy_utils
eshitachandwani Jan 16, 2025
32fa83c
change proxy_utils
eshitachandwani Jan 16, 2025
382bcb1
addr comments
eshitachandwani Jan 16, 2025
69037b0
add userSet option
eshitachandwani Jan 17, 2025
127f485
change userinfo to pointer
eshitachandwani Jan 22, 2025
2a4f24c
change proxy server return ip address
eshitachandwani Jan 22, 2025
9f26c4f
chamge HTTPProxy to new
eshitachandwani Jan 23, 2025
a3dcaa7
Merge branch 'grpc:master' into proxy_pr2
eshitachandwani Jan 23, 2025
dd7ee47
Merge branch 'grpc:master' into proxy_pr2
eshitachandwani Jan 24, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions internal/proxyattributes/proxyattributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ const proxyOptionsKey = keyType("grpc.resolver.delegatingresolver.proxyOptions")
// Options holds the proxy connection details needed during the CONNECT
// handshake.
type Options struct {
User url.Userinfo
UserSet bool
User *url.Userinfo
ConnectAddr string
}

Expand All @@ -45,7 +44,8 @@ func Set(addr resolver.Address, opts Options) resolver.Address {
}

// Get returns the Options for the proxy [resolver.Address] and a boolean
// value representing if the attribute is present or not.
// value representing if the attribute is present or not. The returned data
// should not be mutated.
func Get(addr resolver.Address) (Options, bool) {
if a := addr.Attributes.Value(proxyOptionsKey); a != nil {
return a.(Options), true
Expand Down
18 changes: 4 additions & 14 deletions internal/proxyattributes/proxyattributes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ func (s) TestGet(t *testing.T) {
name string
addr resolver.Address
wantConnectAddr string
wantUser url.Userinfo
wantUserSet bool
wantUser *url.Userinfo
wantAttrPresent bool
}{
{
Expand All @@ -62,12 +61,10 @@ func (s) TestGet(t *testing.T) {
addr: resolver.Address{
Addr: "test-address",
Attributes: attributes.New(proxyOptionsKey, Options{
User: *user,
UserSet: true,
User: user,
}),
},
wantUser: *user,
wantUserSet: true,
wantUser: user,
wantAttrPresent: true,
},
{
Expand All @@ -91,9 +88,6 @@ func (s) TestGet(t *testing.T) {
if gotOption.User != tt.wantUser {
t.Errorf("User(%v) = %v, want %v", tt.addr, gotOption.User, tt.wantUser)
}
if gotOption.UserSet != tt.wantUserSet {
t.Errorf("UserSet(%v) = %v, want %v", tt.addr, gotOption.UserSet, tt.wantUserSet)
}
})
}
}
Expand All @@ -103,8 +97,7 @@ func (s) TestGet(t *testing.T) {
func (s) TestSet(t *testing.T) {
addr := resolver.Address{Addr: "test-address"}
pOpts := Options{
User: *url.UserPassword("username", "password"),
UserSet: true,
User: url.UserPassword("username", "password"),
ConnectAddr: "proxy-address",
}

Expand All @@ -120,7 +113,4 @@ func (s) TestSet(t *testing.T) {
if got, want := gotOption.User, pOpts.User; got != want {
t.Errorf("unexpected User proxy attribute = %v, want %v", got, want)
}
if got, want := gotOption.UserSet, pOpts.UserSet; got != want {
t.Errorf("unexpected UserSet proxy attribute = %v, want %v", got, want)
}
}
12 changes: 2 additions & 10 deletions internal/resolver/delegatingresolver/delegatingresolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,16 +205,9 @@ func (r *delegatingResolver) updateClientConnStateLocked() error {
proxyAddr = resolver.Address{Addr: r.proxyURL.Host}
}
var addresses []resolver.Address
var user url.Userinfo
var userSet bool
if r.proxyURL.User != nil {
user = *r.proxyURL.User
userSet = true
}
for _, targetAddr := range (*r.targetResolverState).Addresses {
addresses = append(addresses, proxyattributes.Set(proxyAddr, proxyattributes.Options{
User: user,
UserSet: userSet,
User: r.proxyURL.User,
ConnectAddr: targetAddr.Addr,
}))
}
Expand All @@ -232,8 +225,7 @@ func (r *delegatingResolver) updateClientConnStateLocked() error {
for _, proxyAddr := range r.proxyAddrs {
for _, targetAddr := range endpt.Addresses {
addrs = append(addrs, proxyattributes.Set(proxyAddr, proxyattributes.Options{
User: user,
UserSet: userSet,
User: r.proxyURL.User,
ConnectAddr: targetAddr.Addr,
}))
}
Expand Down
9 changes: 6 additions & 3 deletions internal/testutils/proxyserver/proxyserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@
func (p *ProxyServer) handleRequest(t *testing.T, in net.Conn, waitForServerHello bool) {
req, err := http.ReadRequest(bufio.NewReader(in))
if err != nil {
t.Errorf("failed to read CONNECT req: %v", err)
return
}

Check warning on line 64 in internal/testutils/proxyserver/proxyserver.go

View check run for this annotation

Codecov / codecov/patch

internal/testutils/proxyserver/proxyserver.go#L62-L64

Added lines #L62 - L64 were not covered by tests
if req.Method != http.MethodConnect {
t.Errorf("unexpected Method %q, want %q", req.Method, http.MethodConnect)
}

Check warning on line 67 in internal/testutils/proxyserver/proxyserver.go

View check run for this annotation

Codecov / codecov/patch

internal/testutils/proxyserver/proxyserver.go#L66-L67

Added lines #L66 - L67 were not covered by tests
p.onRequest(req)

t.Logf("Dialing to %s", req.URL.Host)
Expand All @@ -87,11 +87,11 @@
b := make([]byte, 50)
bytesRead, err := out.Read(b)
if err != nil {
t.Errorf("Got error while reading server hello: %v", err)
in.Close()
out.Close()
return
}

Check warning on line 94 in internal/testutils/proxyserver/proxyserver.go

View check run for this annotation

Codecov / codecov/patch

internal/testutils/proxyserver/proxyserver.go#L90-L94

Added lines #L90 - L94 were not covered by tests
buf.Write(b[0:bytesRead])
}
p.in = in
Expand All @@ -102,19 +102,22 @@
go io.Copy(p.out, p.in)
}

// HTTPProxy initializes and starts a proxy server, registers a cleanup to
// NewHTTPProxy initializes and starts a proxy server, registers a cleanup to
// stop it, and returns a ProxyServer.
func HTTPProxy(t *testing.T, reqCheck func(*http.Request), waitForServerHello bool) *ProxyServer {
func NewHTTPProxy(t *testing.T, reqCheck func(*http.Request), waitForServerHello bool) *ProxyServer {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The thing it returns is a ProxyServer from package proxyserver so this should just be New.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right! Done!

t.Helper()
pLis, err := testutils.LocalTCPListener()
if err != nil {
t.Fatalf("failed to listen: %v", err)
}

Check warning on line 112 in internal/testutils/proxyserver/proxyserver.go

View check run for this annotation

Codecov / codecov/patch

internal/testutils/proxyserver/proxyserver.go#L111-L112

Added lines #L111 - L112 were not covered by tests

p := &ProxyServer{
lis: pLis,
onRequest: reqCheck,
Addr: fmt.Sprintf("localhost:%d", testutils.ParsePort(t, pLis.Addr().String())),
// Use "localhost:<port>" to verify the proxy address is handled
// correctly by the delegating resolver and connects to the proxy server
// correctly even when unresolved.
Addr: fmt.Sprintf("localhost:%d", testutils.ParsePort(t, pLis.Addr().String())),
}

// Start the proxy server.
Expand Down
4 changes: 1 addition & 3 deletions internal/transport/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ func doHTTPConnectHandshake(ctx context.Context, conn net.Conn, grpcUA string, o
URL: &url.URL{Host: opts.ConnectAddr},
Header: map[string][]string{"User-Agent": {grpcUA}},
}

if userSet := opts.UserSet; userSet {
user := opts.User
if user := opts.User; user != nil {
u := user.Username()
p, _ := user.Password()
req.Header.Add(proxyAuthHeaderKey, "Basic "+basicAuth(u, p))
Expand Down
16 changes: 8 additions & 8 deletions internal/transport/proxy_ext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (s) TestGRPCDialWithProxy(t *testing.T) {
t.Errorf(" Unexpected request host: %s , want = %s ", got, want)
}
}
pServer := proxyserver.HTTPProxy(t, reqCheck, false)
pServer := proxyserver.NewHTTPProxy(t, reqCheck, false)

// Overwrite the function in the test and restore them in defer.
hpfe := func(req *http.Request) (*url.URL, error) {
Expand Down Expand Up @@ -145,7 +145,7 @@ func (s) TestGRPCDialWithDNSAndProxy(t *testing.T) {
t.Errorf("isIPAddr(%q) = %t, want = %t", host, got, want)
}
}
pServer := proxyserver.HTTPProxy(t, reqCheck, false)
pServer := proxyserver.NewHTTPProxy(t, reqCheck, false)

// Overwrite the function in the test and restore them in defer.
hpfe := func(req *http.Request) (*url.URL, error) {
Expand Down Expand Up @@ -200,7 +200,7 @@ func (s) TestNewClientWithProxy(t *testing.T) {
t.Errorf(" Unexpected request host: %s , want = %s ", got, want)
}
}
pServer := proxyserver.HTTPProxy(t, reqCheck, false)
pServer := proxyserver.NewHTTPProxy(t, reqCheck, false)

// Overwrite the function in the test and restore them in defer.
hpfe := func(req *http.Request) (*url.URL, error) {
Expand Down Expand Up @@ -254,7 +254,7 @@ func (s) TestNewClientWithProxyAndCustomResolver(t *testing.T) {
t.Errorf("isIPAddr(%q) = %t, want = %t", host, got, want)
}
}
pServer := proxyserver.HTTPProxy(t, reqCheck, false)
pServer := proxyserver.NewHTTPProxy(t, reqCheck, false)

// Overwrite the function in the test and restore them in defer.
hpfe := func(req *http.Request) (*url.URL, error) {
Expand Down Expand Up @@ -316,7 +316,7 @@ func (s) TestNewClientWithProxyAndTargetResolutionEnabled(t *testing.T) {
t.Errorf("isIPAddr(%q) = %t, want = %t", host, got, want)
}
}
pServer := proxyserver.HTTPProxy(t, reqCheck, false)
pServer := proxyserver.NewHTTPProxy(t, reqCheck, false)

// Overwrite the function in the test and restore them in defer.
hpfe := func(req *http.Request) (*url.URL, error) {
Expand Down Expand Up @@ -361,7 +361,7 @@ func (s) TestNewClientWithNoProxy(t *testing.T) {
backend := startBackendServer(t)
unresolvedTargetURI := fmt.Sprintf("localhost:%d", testutils.ParsePort(t, backend.Address))
reqCheck := func(_ *http.Request) { t.Error("proxy server should not have received a Connect request") }
pServer := proxyserver.HTTPProxy(t, reqCheck, false)
pServer := proxyserver.NewHTTPProxy(t, reqCheck, false)

// Overwrite the function in the test and restore them in defer.
hpfe := func(req *http.Request) (*url.URL, error) {
Expand Down Expand Up @@ -406,7 +406,7 @@ func (s) TestNewClientWithContextDialer(t *testing.T) {
backend := startBackendServer(t)
unresolvedTargetURI := fmt.Sprintf("localhost:%d", testutils.ParsePort(t, backend.Address))
reqCheck := func(_ *http.Request) { t.Error("proxy server should not have received a Connect request") }
pServer := proxyserver.HTTPProxy(t, reqCheck, false)
pServer := proxyserver.NewHTTPProxy(t, reqCheck, false)

// Overwrite the function in the test and restore them in defer.
hpfe := func(req *http.Request) (*url.URL, error) {
Expand Down Expand Up @@ -475,7 +475,7 @@ func (s) TestBasicAuthInNewClientWithProxy(t *testing.T) {
t.Errorf("unexpected auth %q (%q), want %q (%q)", got, gotDecoded, wantProxyAuthStr, wantDecoded)
}
}
pServer := proxyserver.HTTPProxy(t, reqCheck, false)
pServer := proxyserver.NewHTTPProxy(t, reqCheck, false)

t.Setenv("HTTPS_PROXY", user+":"+password+"@"+pServer.Addr)

Expand Down
2 changes: 1 addition & 1 deletion internal/transport/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (s) TestHTTPConnectWithServerHello(t *testing.T) {
t.Error(err)
}
}
pServer := proxyserver.HTTPProxy(t, reqCheck, true)
pServer := proxyserver.NewHTTPProxy(t, reqCheck, true)

msg := []byte{4, 3, 5, 2}
recvBuf := make([]byte, len(msg))
Expand Down
Loading