|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright 2026 gRPC authors. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +package accesstokencreds |
| 20 | + |
| 21 | +import ( |
| 22 | + "context" |
| 23 | + "encoding/json" |
| 24 | + "testing" |
| 25 | + "time" |
| 26 | + |
| 27 | + "google.golang.org/grpc/credentials" |
| 28 | + "google.golang.org/grpc/internal/grpctest" |
| 29 | +) |
| 30 | + |
| 31 | +type s struct { |
| 32 | + grpctest.Tester |
| 33 | +} |
| 34 | + |
| 35 | +func Test(t *testing.T) { |
| 36 | + grpctest.RunSubTests(t, s{}) |
| 37 | +} |
| 38 | + |
| 39 | +func (s) TestNewCallCredentialsWithInvalidConfig(t *testing.T) { |
| 40 | + tests := []struct { |
| 41 | + name string |
| 42 | + config string |
| 43 | + }{ |
| 44 | + { |
| 45 | + name: "not_an_object", |
| 46 | + config: `""`, |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "empty_config", |
| 50 | + config: `{}`, |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "empty_token", |
| 54 | + config: `{"token": ""}`, |
| 55 | + }, |
| 56 | + } |
| 57 | + |
| 58 | + for _, tt := range tests { |
| 59 | + t.Run(tt.name, func(t *testing.T) { |
| 60 | + callCreds, cleanup, err := NewCallCredentials(json.RawMessage(tt.config)) |
| 61 | + if err == nil { |
| 62 | + t.Fatalf("NewCallCredentials(%s): got nil, want error", tt.config) |
| 63 | + } |
| 64 | + if callCreds != nil { |
| 65 | + t.Errorf("NewCallCredentials(%s): returned non-nil call credentials", tt.config) |
| 66 | + } |
| 67 | + if cleanup == nil { |
| 68 | + t.Errorf("NewCallCredentials(%s): returned nil cleanup function", tt.config) |
| 69 | + } |
| 70 | + }) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +// Tests that the token is attached as a bearer authorization header on |
| 75 | +// connections providing privacy and integrity, and is withheld without error |
| 76 | +// on weaker connections. |
| 77 | +func (s) TestGetRequestMetadata(t *testing.T) { |
| 78 | + const config = `{"token": "test-token"}` |
| 79 | + callCreds, cleanup, err := NewCallCredentials(json.RawMessage(config)) |
| 80 | + if err != nil { |
| 81 | + t.Fatalf("NewCallCredentials(%s) failed: %v", config, err) |
| 82 | + } |
| 83 | + defer cleanup() |
| 84 | + |
| 85 | + if callCreds.RequireTransportSecurity() { |
| 86 | + t.Error("RequireTransportSecurity() = true, want false") |
| 87 | + } |
| 88 | + |
| 89 | + ctx, cancel := context.WithTimeout(context.Background(), time.Second) |
| 90 | + defer cancel() |
| 91 | + |
| 92 | + // The token must be attached on a connection with privacy and integrity. |
| 93 | + secureCtx := credentials.NewContextWithRequestInfo(ctx, credentials.RequestInfo{ |
| 94 | + AuthInfo: &testAuthInfo{secLevel: credentials.PrivacyAndIntegrity}, |
| 95 | + }) |
| 96 | + md, err := callCreds.GetRequestMetadata(secureCtx) |
| 97 | + if err != nil { |
| 98 | + t.Fatalf("GetRequestMetadata() on a secure connection failed: %v", err) |
| 99 | + } |
| 100 | + if got, want := md["authorization"], "Bearer test-token"; got != want { |
| 101 | + t.Fatalf("GetRequestMetadata() on a secure connection returned authorization header %q, want %q", got, want) |
| 102 | + } |
| 103 | + |
| 104 | + // The token must be withheld, without error, on an insecure connection. |
| 105 | + insecureCtx := credentials.NewContextWithRequestInfo(ctx, credentials.RequestInfo{ |
| 106 | + AuthInfo: &testAuthInfo{secLevel: credentials.NoSecurity}, |
| 107 | + }) |
| 108 | + md, err = callCreds.GetRequestMetadata(insecureCtx) |
| 109 | + if err != nil { |
| 110 | + t.Fatalf("GetRequestMetadata() on an insecure connection failed: %v", err) |
| 111 | + } |
| 112 | + if len(md) != 0 { |
| 113 | + t.Fatalf("GetRequestMetadata() on an insecure connection returned metadata %v, want none", md) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +// testAuthInfo implements credentials.AuthInfo for testing. |
| 118 | +type testAuthInfo struct { |
| 119 | + secLevel credentials.SecurityLevel |
| 120 | +} |
| 121 | + |
| 122 | +func (t *testAuthInfo) AuthType() string { |
| 123 | + return "test" |
| 124 | +} |
| 125 | + |
| 126 | +func (t *testAuthInfo) GetCommonAuthInfo() credentials.CommonAuthInfo { |
| 127 | + return credentials.CommonAuthInfo{SecurityLevel: t.secLevel} |
| 128 | +} |
0 commit comments