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

internal/resolver: introduce a new delegating resolver to handle both target URI and proxy address resolution #7857

Merged
merged 75 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from 65 commits
Commits
Show all changes
75 commits
Select commit Hold shift + click to select a range
bccef6b
delegating resolver
eshitachandwani Nov 20, 2024
6b89e0d
correct comments
eshitachandwani Nov 20, 2024
7c2fb21
correct comments
eshitachandwani Nov 20, 2024
e8bb6bc
use env from envconfig
eshitachandwani Nov 20, 2024
36d0573
use env from envconfig
eshitachandwani Nov 20, 2024
5a87284
improve comment and var
eshitachandwani Nov 20, 2024
e05a895
add comment
eshitachandwani Nov 20, 2024
e724a9a
add newaddress function
eshitachandwani Nov 20, 2024
975e235
add target resolution option and test
eshitachandwani Nov 20, 2024
4b8ade9
correct NewAddress function
eshitachandwani Nov 20, 2024
4ba3578
improve style and comments
eshitachandwani Nov 22, 2024
fa55e5b
change variable name
eshitachandwani Nov 22, 2024
df11d7c
change variable name
eshitachandwani Nov 22, 2024
8bcf2b6
test example
eshitachandwani Nov 22, 2024
a5aeb9a
revert commit
eshitachandwani Nov 22, 2024
47e7936
cleanup test and address comments
eshitachandwani Nov 25, 2024
a2e1415
address comments
eshitachandwani Nov 26, 2024
df87a68
address comments
eshitachandwani Nov 26, 2024
ed3d577
add comment
eshitachandwani Nov 26, 2024
dd5bdb3
add comment
eshitachandwani Nov 27, 2024
d24ec8b
address comments
eshitachandwani Nov 27, 2024
74c6cd0
address comments
eshitachandwani Nov 27, 2024
31d8c2c
address comments
eshitachandwani Nov 28, 2024
1643768
mae unexported key
eshitachandwani Nov 28, 2024
4a5ab5b
add backoff
eshitachandwani Nov 29, 2024
fd061c7
add backoff
eshitachandwani Nov 29, 2024
c6dbcbc
Revert "add backoff"
eshitachandwani Nov 29, 2024
2be9db3
revert backoff
eshitachandwani Nov 29, 2024
67e3799
add attributes package
eshitachandwani Nov 29, 2024
f5e1d37
correct atrributes
eshitachandwani Nov 29, 2024
019f621
address comments
eshitachandwani Dec 4, 2024
8348b1f
change tests
eshitachandwani Dec 4, 2024
f0d5b7b
correct comment
eshitachandwani Dec 4, 2024
4ac31f9
improve code
eshitachandwani Dec 6, 2024
ed413f6
improve code
eshitachandwani Dec 6, 2024
402316c
add endpt support
eshitachandwani Dec 7, 2024
dea9ddd
improve
eshitachandwani Dec 9, 2024
40f1035
add comment
eshitachandwani Dec 9, 2024
95d98b9
Merge branch 'grpc:master' into delegating_pr
eshitachandwani Dec 9, 2024
d5f4d8f
address comments
eshitachandwani Dec 10, 2024
e743cbf
add endpnt support
eshitachandwani Dec 10, 2024
e008feb
improve endpt
eshitachandwani Dec 10, 2024
4aea07d
add test for endpoints
eshitachandwani Dec 10, 2024
ed345a1
add test for endpoints
eshitachandwani Dec 10, 2024
7f416b0
condition and comment for endpoint
eshitachandwani Dec 11, 2024
e68d5de
remove condition
eshitachandwani Dec 11, 2024
8848105
refactors
eshitachandwani Dec 12, 2024
61edca1
correct
eshitachandwani Dec 12, 2024
a01cc7a
change endpoint support
eshitachandwani Dec 13, 2024
845680d
correct endpoint behvaiour for DNS
eshitachandwani Dec 13, 2024
c752bd8
correct endpoint behvaiour for DNS
eshitachandwani Dec 13, 2024
d53100c
address comments
eshitachandwani Dec 16, 2024
99292ea
improve
eshitachandwani Dec 17, 2024
74b3cf6
improve
eshitachandwani Dec 17, 2024
c745144
improve
eshitachandwani Dec 17, 2024
628a2a4
test for addresses
eshitachandwani Dec 17, 2024
eeae936
correct var names
eshitachandwani Dec 17, 2024
bc0adeb
rerun test
eshitachandwani Dec 17, 2024
9c2ce28
address comments
eshitachandwani Dec 17, 2024
4482e51
address comments
eshitachandwani Dec 17, 2024
2bafe77
improve comment
eshitachandwani Dec 17, 2024
a0a2e50
address comments
eshitachandwani Dec 18, 2024
ca2cc24
remove internal
eshitachandwani Dec 18, 2024
1f1e7f5
add no op res
eshitachandwani Dec 18, 2024
402badf
comments
eshitachandwani Dec 19, 2024
163ad20
improve
eshitachandwani Dec 19, 2024
db8b92c
Merge branch 'grpc:master' into delegating_pr
eshitachandwani Dec 19, 2024
6623eb5
Merge branch 'grpc:master' into delegating_pr
eshitachandwani Dec 20, 2024
c4bde98
address comments
eshitachandwani Dec 20, 2024
1c4b416
change target resolver state to pointer
eshitachandwani Dec 20, 2024
5dc893c
tests
eshitachandwani Dec 20, 2024
69253c7
pointer
eshitachandwani Dec 20, 2024
c5f8d6f
address comments
eshitachandwani Dec 20, 2024
bbea8c4
formatting
eshitachandwani Dec 23, 2024
fc2c41f
address comments
eshitachandwani Dec 23, 2024
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
55 changes: 55 additions & 0 deletions internal/proxyattributes/proxyattributes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
*
* Copyright 2024 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

// Package proxyattributes contains functions for getting and setting proxy
// attributes like the CONNECT address and user info.
package proxyattributes

import (
"net/url"

"google.golang.org/grpc/resolver"
)

type keyType string

const proxyOptionsKey = keyType("grpc.resolver.delegatingresolver.proxyOptions")

// Options holds the proxy connection details needed during the CONNECT
// handshake. It includes the user information and the connect address.
dfawley marked this conversation as resolved.
Show resolved Hide resolved
type Options struct {
User url.Userinfo
ConnectAddr string
}

// Set returns a copy of addr with attributes containing the provided user
// and connect address, which are needed during the CONNECT handshake for a
// proxy connection.
dfawley marked this conversation as resolved.
Show resolved Hide resolved
func Set(addr resolver.Address, opts Options) resolver.Address {
addr.Attributes = addr.Attributes.WithValue(proxyOptionsKey, opts)
return addr
}

// Get returns the Options for the proxy [resolver.Address] and a boolean
// value representing if the attribute is present or not.
func Get(addr resolver.Address) (Options, bool) {
if a := addr.Attributes.Value(proxyOptionsKey); a != nil {
return a.(Options), true
}
return Options{}, false
}
116 changes: 116 additions & 0 deletions internal/proxyattributes/proxyattributes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
*
* Copyright 2024 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package proxyattributes

import (
"net/url"
"testing"

"google.golang.org/grpc/attributes"
"google.golang.org/grpc/internal/grpctest"
"google.golang.org/grpc/resolver"
)

type s struct {
grpctest.Tester
}

func Test(t *testing.T) {
grpctest.RunSubTests(t, s{})
}

// Tests that Get returns a valid proxy attribute.
func (s) TestGet(t *testing.T) {
user := url.UserPassword("username", "password")
tests := []struct {
name string
addr resolver.Address
wantConnectAddr string
wantUser url.Userinfo
wantAttrPresent bool
}{
{
name: "connect_address_in_attribute",
addr: resolver.Address{
Addr: "test-address",
Attributes: attributes.New(proxyOptionsKey, Options{
ConnectAddr: "proxy-address",
}),
},
wantConnectAddr: "proxy-address",
wantAttrPresent: true,
},
{
name: "user_in_attribute",
addr: resolver.Address{
Addr: "test-address",
Attributes: attributes.New(proxyOptionsKey, Options{
User: *user,
}),
},
wantUser: *user,
wantAttrPresent: true,
},
{
name: "no_attribute",
addr: resolver.Address{Addr: "test-address"},
wantAttrPresent: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotOption, attrPresent := Get(tt.addr)
if attrPresent != tt.wantAttrPresent {
t.Errorf("Get(%v) = %v, want %v", tt.addr, attrPresent, tt.wantAttrPresent)
}

if gotOption.ConnectAddr != tt.wantConnectAddr {
t.Errorf("ConnectAddr(%v) = %v, want %v", tt.addr, gotOption.ConnectAddr, tt.wantConnectAddr)
}

if gotOption.User != tt.wantUser {
t.Errorf("User(%v) = %v, want %v", tt.addr, gotOption.User, tt.wantUser)
}
})
}
}

// Tests that Set returns a copy of addr with attributes containing correct
// user and connect address.
func (s) TestSet(t *testing.T) {
addr := resolver.Address{Addr: "test-address"}
pOpts := Options{
User: *url.UserPassword("username", "password"),
ConnectAddr: "proxy-address",
}

// Call Set and validate attributes
populatedAddr := Set(addr, pOpts)
gotOption, attrPresent := Get(populatedAddr)
if !attrPresent {
t.Errorf("Get(%v) = %v, want %v ", populatedAddr, attrPresent, true)
}
if got, want := gotOption.ConnectAddr, pOpts.ConnectAddr; got != want {
t.Errorf("Unexpected ConnectAddr proxy atrribute = %v, want %v", got, want)
}
if got, want := gotOption.User, pOpts.User; got != want {
t.Errorf("unexpected User proxy attribute = %v, want %v", got, want)
}
}
Loading
Loading