Skip to content

Commit 4680429

Browse files
credentials/local: implement ValidateAuthority (#8291)
1 parent b3d63b1 commit 4680429

File tree

2 files changed

+77
-59
lines changed

2 files changed

+77
-59
lines changed

credentials/credentials_ext_test.go

Lines changed: 71 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"google.golang.org/grpc/codes"
3131
"google.golang.org/grpc/credentials"
3232
"google.golang.org/grpc/credentials/insecure"
33+
"google.golang.org/grpc/credentials/local"
3334
"google.golang.org/grpc/internal/stubserver"
3435
"google.golang.org/grpc/metadata"
3536
"google.golang.org/grpc/status"
@@ -57,45 +58,88 @@ func authorityChecker(ctx context.Context, wantAuthority string) error {
5758
return nil
5859
}
5960

60-
// Tests the `grpc.CallAuthority` option with TLS credentials. This test verifies
61-
// that the provided authority is correctly propagated to the server when a
62-
// correct authority is used.
63-
func (s) TestCorrectAuthorityWithTLSCreds(t *testing.T) {
61+
func loadTLSCreds(t *testing.T) (grpc.ServerOption, grpc.DialOption) {
62+
t.Helper()
6463
cert, err := tls.LoadX509KeyPair(testdata.Path("x509/server1_cert.pem"), testdata.Path("x509/server1_key.pem"))
6564
if err != nil {
66-
t.Fatalf("Failed to load key pair: %s", err)
65+
t.Fatalf("Failed to load key pair: %v", err)
66+
return nil, nil
6767
}
68-
creds, err := credentials.NewClientTLSFromFile(testdata.Path("x509/server_ca_cert.pem"), "x.test.example.com")
68+
serverCreds := grpc.Creds(credentials.NewServerTLSFromCert(&cert))
69+
70+
clientCreds, err := credentials.NewClientTLSFromFile(testdata.Path("x509/server_ca_cert.pem"), "x.test.example.com")
6971
if err != nil {
70-
t.Fatalf("Failed to create credentials %v", err)
72+
t.Fatalf("Failed to create client credentials: %v", err)
7173
}
74+
return serverCreds, grpc.WithTransportCredentials(clientCreds)
75+
}
76+
77+
// Tests the scenario where the `grpc.CallAuthority` call option is used with
78+
// different transport credentials. The test verifies that the specified
79+
// authority is correctly propagated to the serve when a correct authority is
80+
// used.
81+
func (s) TestCorrectAuthorityWithCreds(t *testing.T) {
7282
const authority = "auth.test.example.com"
73-
ss := &stubserver.StubServer{
74-
EmptyCallF: func(ctx context.Context, _ *testpb.Empty) (*testpb.Empty, error) {
75-
if err := authorityChecker(ctx, authority); err != nil {
76-
return nil, err
77-
}
78-
return &testpb.Empty{}, nil
83+
84+
tests := []struct {
85+
name string
86+
creds func(t *testing.T) (grpc.ServerOption, grpc.DialOption)
87+
expectedAuth string
88+
}{
89+
{
90+
name: "Insecure",
91+
creds: func(t *testing.T) (grpc.ServerOption, grpc.DialOption) {
92+
c := insecure.NewCredentials()
93+
return grpc.Creds(c), grpc.WithTransportCredentials(c)
94+
},
95+
expectedAuth: authority,
96+
},
97+
{
98+
name: "Local",
99+
creds: func(t *testing.T) (grpc.ServerOption, grpc.DialOption) {
100+
c := local.NewCredentials()
101+
return grpc.Creds(c), grpc.WithTransportCredentials(c)
102+
},
103+
expectedAuth: authority,
104+
},
105+
{
106+
name: "TLS",
107+
creds: func(t *testing.T) (grpc.ServerOption, grpc.DialOption) {
108+
return loadTLSCreds(t)
109+
},
110+
expectedAuth: authority,
79111
},
80112
}
81-
if err := ss.StartServer(grpc.Creds(credentials.NewServerTLSFromCert(&cert))); err != nil {
82-
t.Fatalf("Error starting endpoint server: %v", err)
83-
}
84-
defer ss.Stop()
85113

86-
cc, err := grpc.NewClient(ss.Address, grpc.WithTransportCredentials(creds))
87-
if err != nil {
88-
t.Fatalf("grpc.NewClient(%q) = %v", ss.Address, err)
89-
}
90-
defer cc.Close()
114+
for _, tt := range tests {
115+
t.Run(tt.name, func(t *testing.T) {
116+
ss := &stubserver.StubServer{
117+
EmptyCallF: func(ctx context.Context, _ *testpb.Empty) (*testpb.Empty, error) {
118+
if err := authorityChecker(ctx, tt.expectedAuth); err != nil {
119+
return nil, err
120+
}
121+
return &testpb.Empty{}, nil
122+
},
123+
}
124+
serverOpt, dialOpt := tt.creds(t)
125+
if err := ss.StartServer(serverOpt); err != nil {
126+
t.Fatalf("Error starting endpoint server: %v", err)
127+
}
128+
defer ss.Stop()
91129

92-
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
93-
defer cancel()
130+
cc, err := grpc.NewClient(ss.Address, dialOpt)
131+
if err != nil {
132+
t.Fatalf("grpc.NewClient(%q) = %v", ss.Address, err)
133+
}
134+
defer cc.Close()
94135

95-
if _, err = testgrpc.NewTestServiceClient(cc).EmptyCall(ctx, &testpb.Empty{}, grpc.CallAuthority(authority)); status.Code(err) != codes.OK {
96-
t.Fatalf("EmptyCall() returned status %v, want %v", status.Code(err), codes.OK)
136+
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
137+
defer cancel()
138+
if _, err = testgrpc.NewTestServiceClient(cc).EmptyCall(ctx, &testpb.Empty{}, grpc.CallAuthority(tt.expectedAuth)); err != nil {
139+
t.Fatalf("EmptyCall() rpc failed: %v", err)
140+
}
141+
})
97142
}
98-
99143
}
100144

101145
// Tests the `grpc.CallAuthority` option with TLS credentials. This test verifies
@@ -143,38 +187,6 @@ func (s) TestIncorrectAuthorityWithTLS(t *testing.T) {
143187
}
144188
}
145189

146-
// Tests the scenario where the `grpc.CallAuthority` call option is used with
147-
// insecure transport credentials. The test verifies that the specified
148-
// authority is correctly propagated to the server.
149-
func (s) TestAuthorityCallOptionWithInsecureCreds(t *testing.T) {
150-
const authority = "test.server.name"
151-
152-
ss := &stubserver.StubServer{
153-
EmptyCallF: func(ctx context.Context, _ *testpb.Empty) (*testpb.Empty, error) {
154-
if err := authorityChecker(ctx, authority); err != nil {
155-
return nil, err
156-
}
157-
return &testpb.Empty{}, nil
158-
},
159-
}
160-
if err := ss.Start(nil); err != nil {
161-
t.Fatalf("Error starting endpoint server: %v", err)
162-
}
163-
defer ss.Stop()
164-
165-
cc, err := grpc.NewClient(ss.Address, grpc.WithTransportCredentials(insecure.NewCredentials()))
166-
if err != nil {
167-
t.Fatalf("grpc.NewClient(%q) = %v", ss.Address, err)
168-
}
169-
defer cc.Close()
170-
171-
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
172-
defer cancel()
173-
if _, err = testgrpc.NewTestServiceClient(cc).EmptyCall(ctx, &testpb.Empty{}, grpc.CallAuthority(authority)); err != nil {
174-
t.Fatalf("EmptyCall() rpc failed: %v", err)
175-
}
176-
}
177-
178190
// testAuthInfoNoValidator implements only credentials.AuthInfo and not
179191
// credentials.AuthorityValidator.
180192
type testAuthInfoNoValidator struct{}

credentials/local/local.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ func (info) AuthType() string {
4949
return "local"
5050
}
5151

52+
// ValidateAuthority allows any value to be overridden for the :authority
53+
// header.
54+
func (info) ValidateAuthority(string) error {
55+
return nil
56+
}
57+
5258
// localTC is the credentials required to establish a local connection.
5359
type localTC struct {
5460
info credentials.ProtocolInfo

0 commit comments

Comments
 (0)