-
Notifications
You must be signed in to change notification settings - Fork 4.8k
xds: Implement GrpcService parsing and shared side channels (gRFC A102) #9319
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 3 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e0b6447
xds: add grpcservice package and Config.AllowedGrpcService lookup
mbissa fef7829
grpcservice: align with finalized AllowedGRPCService API
mbissa 9662c23
filter wiring for A102
mbissa 979d438
address review comments: use imetadata validation and mirror clientco…
mbissa d3b9bc9
address review comments: make grpcservice a pure parser, relocate acc…
mbissa 7bdfc36
review comments
mbissa f5b2491
review comments - simplifying package structure
mbissa 7e9e127
review comments
mbissa 206b758
cleanup
mbissa 3519e1d
address review comments: compare full structures in Equal and tests
mbissa 651d85a
Address review comments from easwars and eshitachandwani
mbissa 395a24c
last nits
mbissa 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| /* | ||
| * | ||
| * Copyright 2026 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 accesstokencreds implements static access token CallCredentials for | ||
| // xDS-configured side channels, as specified in gRFC A102. | ||
| package accesstokencreds | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
|
|
||
| "google.golang.org/grpc/credentials" | ||
| ) | ||
|
|
||
| // NewCallCredentials returns call credentials that attach a static bearer | ||
| // token to outgoing RPCs. The config must be a JSON object of the form | ||
| // {"token": <non-empty string>}. | ||
| // | ||
| // The caller is expected to invoke the cancel function when they are done | ||
| // using the returned call creds. This cancel function is idempotent. | ||
| func NewCallCredentials(configJSON json.RawMessage) (credentials.PerRPCCredentials, func(), error) { | ||
| var cfg struct { | ||
| Token string `json:"token"` | ||
| } | ||
| emptyFn := func() {} | ||
|
|
||
| if err := json.Unmarshal(configJSON, &cfg); err != nil { | ||
| return nil, emptyFn, fmt.Errorf("failed to unmarshal access token call credentials config: %v", err) | ||
| } | ||
| if cfg.Token == "" { | ||
| return nil, emptyFn, fmt.Errorf("token is required in access token call credentials config") | ||
| } | ||
| return &callCreds{token: cfg.Token}, emptyFn, nil | ||
| } | ||
|
|
||
| // callCreds implements credentials.PerRPCCredentials by attaching a static | ||
| // bearer token to each RPC. | ||
| type callCreds struct { | ||
| token string | ||
| } | ||
|
|
||
| // GetRequestMetadata returns the token as an authorization header, but only | ||
| // when the connection provides privacy and integrity. On weaker connections | ||
| // the token is withheld without failing the RPC, as per gRFC A102. | ||
| func (c *callCreds) GetRequestMetadata(ctx context.Context, _ ...string) (map[string]string, error) { | ||
| ri, ok := credentials.RequestInfoFromContext(ctx) | ||
| if !ok || credentials.CheckSecurityLevel(ri.AuthInfo, credentials.PrivacyAndIntegrity) != nil { | ||
| return nil, nil | ||
| } | ||
| return map[string]string{"authorization": "Bearer " + c.token}, nil | ||
| } | ||
|
|
||
| // RequireTransportSecurity returns false. The credentials may be used on any | ||
|
easwars marked this conversation as resolved.
Outdated
|
||
| // connection, but GetRequestMetadata withholds the token on connections that | ||
| // do not provide privacy and integrity. | ||
|
easwars marked this conversation as resolved.
Outdated
|
||
| func (c *callCreds) RequireTransportSecurity() bool { | ||
| return false | ||
| } | ||
128 changes: 128 additions & 0 deletions
128
internal/xds/bootstrap/accesstokencreds/call_creds_test.go
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,128 @@ | ||
| /* | ||
| * | ||
| * Copyright 2026 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 accesstokencreds | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "google.golang.org/grpc/credentials" | ||
| "google.golang.org/grpc/internal/grpctest" | ||
| ) | ||
|
|
||
| type s struct { | ||
| grpctest.Tester | ||
| } | ||
|
|
||
| func Test(t *testing.T) { | ||
| grpctest.RunSubTests(t, s{}) | ||
| } | ||
|
|
||
| func (s) TestNewCallCredentialsWithInvalidConfig(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| config string | ||
| }{ | ||
| { | ||
| name: "not_an_object", | ||
| config: `""`, | ||
| }, | ||
| { | ||
| name: "empty_config", | ||
| config: `{}`, | ||
| }, | ||
| { | ||
| name: "empty_token", | ||
| config: `{"token": ""}`, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| callCreds, cleanup, err := NewCallCredentials(json.RawMessage(tt.config)) | ||
| if err == nil { | ||
| t.Fatalf("NewCallCredentials(%s): got nil, want error", tt.config) | ||
| } | ||
| if callCreds != nil { | ||
| t.Errorf("NewCallCredentials(%s): returned non-nil call credentials", tt.config) | ||
| } | ||
| if cleanup == nil { | ||
| t.Errorf("NewCallCredentials(%s): returned nil cleanup function", tt.config) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // Tests that the token is attached as a bearer authorization header on | ||
| // connections providing privacy and integrity, and is withheld without error | ||
| // on weaker connections. | ||
| func (s) TestGetRequestMetadata(t *testing.T) { | ||
| const config = `{"token": "test-token"}` | ||
| callCreds, cleanup, err := NewCallCredentials(json.RawMessage(config)) | ||
| if err != nil { | ||
| t.Fatalf("NewCallCredentials(%s) failed: %v", config, err) | ||
| } | ||
| defer cleanup() | ||
|
|
||
| if callCreds.RequireTransportSecurity() { | ||
| t.Error("RequireTransportSecurity() = true, want false") | ||
| } | ||
|
|
||
| ctx, cancel := context.WithTimeout(context.Background(), time.Second) | ||
| defer cancel() | ||
|
|
||
| // The token must be attached on a connection with privacy and integrity. | ||
| secureCtx := credentials.NewContextWithRequestInfo(ctx, credentials.RequestInfo{ | ||
| AuthInfo: &testAuthInfo{secLevel: credentials.PrivacyAndIntegrity}, | ||
| }) | ||
| md, err := callCreds.GetRequestMetadata(secureCtx) | ||
| if err != nil { | ||
| t.Fatalf("GetRequestMetadata() on a secure connection failed: %v", err) | ||
| } | ||
| if got, want := md["authorization"], "Bearer test-token"; got != want { | ||
| t.Fatalf("GetRequestMetadata() on a secure connection returned authorization header %q, want %q", got, want) | ||
| } | ||
|
|
||
| // The token must be withheld, without error, on an insecure connection. | ||
| insecureCtx := credentials.NewContextWithRequestInfo(ctx, credentials.RequestInfo{ | ||
| AuthInfo: &testAuthInfo{secLevel: credentials.NoSecurity}, | ||
| }) | ||
| md, err = callCreds.GetRequestMetadata(insecureCtx) | ||
| if err != nil { | ||
| t.Fatalf("GetRequestMetadata() on an insecure connection failed: %v", err) | ||
| } | ||
| if len(md) != 0 { | ||
| t.Fatalf("GetRequestMetadata() on an insecure connection returned metadata %v, want none", md) | ||
| } | ||
| } | ||
|
|
||
| // testAuthInfo implements credentials.AuthInfo for testing. | ||
| type testAuthInfo struct { | ||
| secLevel credentials.SecurityLevel | ||
| } | ||
|
|
||
| func (t *testAuthInfo) AuthType() string { | ||
| return "test" | ||
| } | ||
|
|
||
| func (t *testAuthInfo) GetCommonAuthInfo() credentials.CommonAuthInfo { | ||
| return credentials.CommonAuthInfo{SecurityLevel: t.secLevel} | ||
| } |
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.
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.