-
Notifications
You must be signed in to change notification settings - Fork 4.6k
credentials, transport, grpc : add a call option to override the :authority header on a per-RPC basis #8068
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
Merged
Merged
Changes from 17 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
6840baf
regenrate protos
eshitachandwani 11d96c1
revert
eshitachandwani d5b5af1
authority overwrite
eshitachandwani 34175aa
Merge branch 'grpc:master' into auth_override
eshitachandwani 4791ac7
update callInfo
eshitachandwani ac318de
update callInfo
eshitachandwani d1ea438
efn
eshitachandwani 72d132a
address comments
eshitachandwani b1583a6
Merge branch 'grpc:master' into auth_override
eshitachandwani 74e7a8a
address comments
eshitachandwani 9b81586
address comments
eshitachandwani 515fad1
vet
eshitachandwani 9e48a2a
refactor
eshitachandwani c5aac00
refactor
eshitachandwani e3e6aab
refactor test
eshitachandwani 24b408e
minor changes
eshitachandwani 9777548
fmt
eshitachandwani b9e93d0
final comments
eshitachandwani 4efe178
final comments
eshitachandwani File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,333 @@ | ||
| /* | ||
| * | ||
| * Copyright 2025 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 credentials_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "crypto/tls" | ||
| "fmt" | ||
| "net" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "google.golang.org/grpc" | ||
| "google.golang.org/grpc/codes" | ||
| "google.golang.org/grpc/credentials" | ||
| "google.golang.org/grpc/credentials/insecure" | ||
| "google.golang.org/grpc/internal/stubserver" | ||
| "google.golang.org/grpc/metadata" | ||
| "google.golang.org/grpc/status" | ||
| "google.golang.org/grpc/testdata" | ||
|
|
||
| testgrpc "google.golang.org/grpc/interop/grpc_testing" | ||
| testpb "google.golang.org/grpc/interop/grpc_testing" | ||
| ) | ||
|
|
||
| func authorityChecker(ctx context.Context, wantAuthority string) error { | ||
| md, ok := metadata.FromIncomingContext(ctx) | ||
| if !ok { | ||
| return status.Error(codes.InvalidArgument, "failed to parse metadata") | ||
| } | ||
| auths, ok := md[":authority"] | ||
| if !ok { | ||
| return status.Error(codes.InvalidArgument, "no authority header") | ||
| } | ||
| if len(auths) != 1 { | ||
| return status.Errorf(codes.InvalidArgument, "expected exactly one authority header, got %v", auths) | ||
| } | ||
| if auths[0] != wantAuthority { | ||
| return status.Errorf(codes.InvalidArgument, "invalid authority header %q, want %q", auths[0], wantAuthority) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // Tests the `grpc.CallAuthority` option with TLS credentials. This test verifies | ||
| // that the provided authority is correctly propagated to the server when a | ||
| // correct authority is used. | ||
| func (s) TestCorrectAuthorityWithTLSCreds(t *testing.T) { | ||
| cert, err := tls.LoadX509KeyPair(testdata.Path("x509/server1_cert.pem"), testdata.Path("x509/server1_key.pem")) | ||
| if err != nil { | ||
| t.Fatalf("Failed to load key pair: %s", err) | ||
| } | ||
| creds, err := credentials.NewClientTLSFromFile(testdata.Path("x509/server_ca_cert.pem"), "x.test.example.com") | ||
| if err != nil { | ||
| t.Fatalf("Failed to create credentials %v", err) | ||
| } | ||
| authority := "auth.test.example.com" | ||
eshitachandwani marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ss := &stubserver.StubServer{ | ||
| EmptyCallF: func(ctx context.Context, _ *testpb.Empty) (*testpb.Empty, error) { | ||
| if err := authorityChecker(ctx, authority); err != nil { | ||
| return nil, err | ||
| } | ||
| return &testpb.Empty{}, nil | ||
| }, | ||
| } | ||
| if err := ss.StartServer(grpc.Creds(credentials.NewServerTLSFromCert(&cert))); err != nil { | ||
| t.Fatalf("Error starting endpoint server: %v", err) | ||
| } | ||
| defer ss.Stop() | ||
|
|
||
| cc, err := grpc.NewClient(ss.Address, grpc.WithTransportCredentials(creds)) | ||
| if err != nil { | ||
| t.Fatalf("grpc.NewClient(%q) = %v", ss.Address, err) | ||
| } | ||
| defer cc.Close() | ||
|
|
||
| ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) | ||
| defer cancel() | ||
|
|
||
| if _, err = testgrpc.NewTestServiceClient(cc).EmptyCall(ctx, &testpb.Empty{}, grpc.CallAuthority(authority)); status.Code(err) != codes.OK { | ||
| t.Fatalf("EmptyCall() returned status %v, want %v", status.Code(err), codes.OK) | ||
| } | ||
|
|
||
| } | ||
|
|
||
| // Tests the `grpc.CallAuthority` option with TLS credentials. This test verifies | ||
| // that the RPC fails with `UNAVAILABLE` status code and doesn't reach the server | ||
| // when an incorrect authority is used. | ||
| func (s) TestIncorrectAuthorityWithTLS(t *testing.T) { | ||
| cert, err := tls.LoadX509KeyPair(testdata.Path("x509/server1_cert.pem"), testdata.Path("x509/server1_key.pem")) | ||
| if err != nil { | ||
| t.Fatalf("Failed to load key pair: %s", err) | ||
| } | ||
| creds, err := credentials.NewClientTLSFromFile(testdata.Path("x509/server_ca_cert.pem"), "x.test.example.com") | ||
| if err != nil { | ||
| t.Fatalf("Failed to create credentials %v", err) | ||
| } | ||
| authority := "auth.example.com" | ||
eshitachandwani marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| serverCalled := make(chan struct{}) | ||
| ss := &stubserver.StubServer{ | ||
| EmptyCallF: func(ctx context.Context, _ *testpb.Empty) (*testpb.Empty, error) { | ||
| close(serverCalled) | ||
| return nil, nil | ||
| }, | ||
| } | ||
| if err := ss.StartServer(grpc.Creds(credentials.NewServerTLSFromCert(&cert))); err != nil { | ||
| t.Fatalf("Error starting endpoint server: %v", err) | ||
| } | ||
| defer ss.Stop() | ||
|
|
||
| cc, err := grpc.NewClient(ss.Address, grpc.WithTransportCredentials(creds)) | ||
| if err != nil { | ||
| t.Fatalf("grpc.NewClient(%q) = %v", ss.Address, err) | ||
| } | ||
| defer cc.Close() | ||
|
|
||
| ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) | ||
| defer cancel() | ||
|
|
||
| if _, err = testgrpc.NewTestServiceClient(cc).EmptyCall(ctx, &testpb.Empty{}, grpc.CallAuthority(authority)); status.Code(err) != codes.Unavailable { | ||
| t.Fatalf("EmptyCall() returned status %v, want %v", status.Code(err), codes.Unavailable) | ||
| } | ||
| select { | ||
| case <-serverCalled: | ||
| t.Fatalf("Server handler should not have been called") | ||
| case <-time.After(defaultTestShortTimeout): | ||
| } | ||
| } | ||
|
|
||
| // Tests the scenario where the `grpc.CallAuthority` call option is used with | ||
| // insecure transport credentials. The test verifies that the specified | ||
| // authority is correctly propagated to the server. | ||
| func (s) TestAuthorityCallOptionWithInsecureCreds(t *testing.T) { | ||
| authority := "test.server.name" | ||
eshitachandwani marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ss := &stubserver.StubServer{ | ||
| EmptyCallF: func(ctx context.Context, _ *testpb.Empty) (*testpb.Empty, error) { | ||
| if err := authorityChecker(ctx, authority); err != nil { | ||
| return nil, err | ||
| } | ||
| return &testpb.Empty{}, nil | ||
| }, | ||
| } | ||
| if err := ss.Start(nil); err != nil { | ||
| t.Fatalf("Error starting endpoint server: %v", err) | ||
| } | ||
| defer ss.Stop() | ||
|
|
||
| cc, err := grpc.NewClient(ss.Address, grpc.WithTransportCredentials(insecure.NewCredentials())) | ||
| if err != nil { | ||
| t.Fatalf("grpc.NewClient(%q) = %v", ss.Address, err) | ||
| } | ||
| defer cc.Close() | ||
|
|
||
| ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) | ||
| defer cancel() | ||
| if _, err = testgrpc.NewTestServiceClient(cc).EmptyCall(ctx, &testpb.Empty{}, grpc.CallAuthority(authority)); err != nil { | ||
| t.Fatalf("EmptyCall() rpc failed: %v", err) | ||
| } | ||
| } | ||
|
|
||
| // testAuthInfoNoValidator implements only credentials.AuthInfo and not | ||
| // credentials.AuthorityValidator. | ||
| type testAuthInfoNoValidator struct{} | ||
|
|
||
| // AuthType returns the authentication type. | ||
| func (testAuthInfoNoValidator) AuthType() string { | ||
| return "test" | ||
| } | ||
|
|
||
| // testAuthInfoWithValidator implements both credentials.AuthInfo and | ||
| // credentials.AuthorityValidator. | ||
| type testAuthInfoWithValidator struct { | ||
| validAuthority string | ||
| } | ||
|
|
||
| // AuthType returns the authentication type. | ||
| func (testAuthInfoWithValidator) AuthType() string { | ||
| return "test" | ||
| } | ||
|
|
||
| // ValidateAuthority implements credentials.AuthorityValidator. | ||
| func (v testAuthInfoWithValidator) ValidateAuthority(authority string) error { | ||
| if authority == v.validAuthority { | ||
| return nil | ||
| } | ||
| return fmt.Errorf("invalid authority") | ||
|
||
| } | ||
|
|
||
| // testCreds is a test TransportCredentials that can optionally support | ||
| // authority validation. | ||
| type testCreds struct { | ||
| WithValidator bool | ||
easwars marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
eshitachandwani marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Authority string | ||
| } | ||
|
|
||
| // ClientHandshake performs the client-side handshake. | ||
| func (c *testCreds) ClientHandshake(ctx context.Context, authority string, rawConn net.Conn) (net.Conn, credentials.AuthInfo, error) { | ||
| if c.WithValidator { | ||
| return rawConn, testAuthInfoWithValidator{validAuthority: c.Authority}, nil | ||
| } | ||
| return rawConn, testAuthInfoNoValidator{}, nil | ||
| } | ||
|
|
||
| // ServerHandshake performs the server-side handshake. | ||
| func (c *testCreds) ServerHandshake(rawConn net.Conn) (net.Conn, credentials.AuthInfo, error) { | ||
| if c.WithValidator { | ||
| return rawConn, testAuthInfoWithValidator{validAuthority: c.Authority}, nil | ||
| } | ||
| return rawConn, testAuthInfoNoValidator{}, nil | ||
| } | ||
|
|
||
| // Clone creates a copy of testCreds. | ||
| func (c *testCreds) Clone() credentials.TransportCredentials { | ||
| return &testCreds{WithValidator: c.WithValidator} | ||
| } | ||
|
|
||
| // Info provides protocol information. | ||
| func (c *testCreds) Info() credentials.ProtocolInfo { | ||
| return credentials.ProtocolInfo{} | ||
| } | ||
|
|
||
| // OverrideServerName overrides the server name used for verification. | ||
| func (c *testCreds) OverrideServerName(serverName string) error { | ||
| return nil | ||
| } | ||
|
|
||
| // TestAuthorityValidationFailureWithCustomCreds tests the `grpc.CallAuthority` call | ||
| // option using custom credentials. It verifies behavior both, when the | ||
eshitachandwani marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // credentials implement AuthorityValidator with incorrect authority override, | ||
| // as well as when the credentials do not implement AuthorityValidator. Both the | ||
| // cases are expected to fail with `UNAVAILABLE` status code. | ||
| func (s) TestAuthorityValidationFailureWithCustomCreds(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| creds credentials.TransportCredentials | ||
| authority string | ||
| wantStatus codes.Code | ||
eshitachandwani marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }{ | ||
| { | ||
| name: "IncorrectAuthorityWithFakeCreds", | ||
| authority: "auth.example.com", | ||
| creds: &testCreds{WithValidator: true, Authority: "auth.test.example.com"}, | ||
| wantStatus: codes.Unavailable, | ||
| }, | ||
| { | ||
| name: "FakeCredsWithNoAuthValidator", | ||
| creds: &testCreds{WithValidator: false}, | ||
| authority: "auth.test.example.com", | ||
| wantStatus: codes.Unavailable, | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| serverCalled := make(chan struct{}) | ||
| ss := stubserver.StubServer{ | ||
| EmptyCallF: func(ctx context.Context, _ *testpb.Empty) (*testpb.Empty, error) { | ||
| close(serverCalled) | ||
| return nil, nil | ||
| }, | ||
| } | ||
| if err := ss.StartServer(); err != nil { | ||
| t.Fatalf("Failed to start stub server: %v", err) | ||
| } | ||
| defer ss.Stop() | ||
|
|
||
| cc, err := grpc.NewClient(ss.Address, grpc.WithTransportCredentials(tt.creds)) | ||
| if err != nil { | ||
| t.Fatalf("grpc.NewClient(%q) = %v", ss.Address, err) | ||
| } | ||
| defer cc.Close() | ||
|
|
||
| ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) | ||
| defer cancel() | ||
| if _, err = testgrpc.NewTestServiceClient(cc).EmptyCall(ctx, &testpb.Empty{}, grpc.CallAuthority(tt.authority)); status.Code(err) != tt.wantStatus { | ||
| t.Fatalf("EmptyCall() returned status %v, want %v", status.Code(err), tt.wantStatus) | ||
| } | ||
| select { | ||
| case <-serverCalled: | ||
| t.Fatalf("Server should not have been called") | ||
| case <-time.After(defaultTestShortTimeout): | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| } | ||
|
|
||
| // TestCorrectAuthorityWithCustomCreds tests the `grpc.CallAuthority` call | ||
| // option using custom credentials. It verifies that the provided authority is | ||
| // correctly propagated to the server when a correct authority is used. | ||
| func (s) TestCorrectAuthorityWithCustomCreds(t *testing.T) { | ||
| authority := "auth.test.example.com" | ||
| creds := &testCreds{WithValidator: true, Authority: "auth.test.example.com"} | ||
| ss := stubserver.StubServer{ | ||
| EmptyCallF: func(ctx context.Context, _ *testpb.Empty) (*testpb.Empty, error) { | ||
| if err := authorityChecker(ctx, authority); err != nil { | ||
| return nil, err | ||
| } | ||
| return &testpb.Empty{}, nil | ||
| }, | ||
| } | ||
| if err := ss.StartServer(); err != nil { | ||
| t.Fatalf("Failed to start stub server: %v", err) | ||
| } | ||
| defer ss.Stop() | ||
|
|
||
| cc, err := grpc.NewClient(ss.Address, grpc.WithTransportCredentials(creds)) | ||
| if err != nil { | ||
| t.Fatalf("grpc.NewClient(%q) = %v", ss.Address, err) | ||
| } | ||
| defer cc.Close() | ||
|
|
||
| ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) | ||
| defer cancel() | ||
| if _, err = testgrpc.NewTestServiceClient(cc).EmptyCall(ctx, &testpb.Empty{}, grpc.CallAuthority(authority)); status.Code(err) != codes.OK { | ||
| t.Fatalf("EmptyCall() returned status %v, want %v", status.Code(err), codes.OK) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.