snapshot of https://github.com/grpc/grpc-go.git (master, 2026-09-15; blobs > 131072B omitted)
Some checks failed
codecov / upload (push) Failing after 13m59s
CodeQL / Analyze (push) Failing after 1s
Testing / vet-proto (push) Failing after 1s
Testing / tests (arm, latest) (push) Waiting to run
Testing / extras (latest) (push) Failing after 1s
Testing / static checks (latest-1) (push) Failing after 1s
Testing / tests (-race, latest) (push) Failing after 1s
Testing / tests (i386, latest) (push) Failing after 1s
Testing / tests (latest) (push) Failing after 1s
Testing / tests (latest-1) (push) Failing after 1s
Lock Threads / lock (push) Failing after 1s
govulncheck / govulncheck (.) (push) Failing after 1s
govulncheck / govulncheck (./cmd/protoc-gen-go-grpc) (push) Failing after 1s
govulncheck / govulncheck (./gcp/observability) (push) Failing after 1s
govulncheck / govulncheck (./security/advancedtls) (push) Failing after 1s
govulncheck / govulncheck (./stats/opencensus) (push) Failing after 1s
Stale bot / stale (push) Failing after 1s
Some checks failed
codecov / upload (push) Failing after 13m59s
CodeQL / Analyze (push) Failing after 1s
Testing / vet-proto (push) Failing after 1s
Testing / tests (arm, latest) (push) Waiting to run
Testing / extras (latest) (push) Failing after 1s
Testing / static checks (latest-1) (push) Failing after 1s
Testing / tests (-race, latest) (push) Failing after 1s
Testing / tests (i386, latest) (push) Failing after 1s
Testing / tests (latest) (push) Failing after 1s
Testing / tests (latest-1) (push) Failing after 1s
Lock Threads / lock (push) Failing after 1s
govulncheck / govulncheck (.) (push) Failing after 1s
govulncheck / govulncheck (./cmd/protoc-gen-go-grpc) (push) Failing after 1s
govulncheck / govulncheck (./gcp/observability) (push) Failing after 1s
govulncheck / govulncheck (./security/advancedtls) (push) Failing after 1s
govulncheck / govulncheck (./stats/opencensus) (push) Failing after 1s
Stale bot / stale (push) Failing after 1s
This commit is contained in:
54
internal/proxyattributes/proxyattributes.go
Normal file
54
internal/proxyattributes/proxyattributes.go
Normal file
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
*
|
||||
* 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.
|
||||
type Options struct {
|
||||
User *url.Userinfo
|
||||
ConnectAddr string
|
||||
}
|
||||
|
||||
// Set returns a copy of addr with opts set in its attributes.
|
||||
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. 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
|
||||
}
|
||||
return Options{}, false
|
||||
}
|
||||
116
internal/proxyattributes/proxyattributes_test.go
Normal file
116
internal/proxyattributes/proxyattributes_test.go
Normal file
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user