Skip to content

Commit f7d488d

Browse files
authored
credentials: expose NewContextWithRequestInfo publicly (#8198)
1 parent 54e7e26 commit f7d488d

File tree

5 files changed

+22
-19
lines changed

5 files changed

+22
-19
lines changed

credentials/credentials.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,14 +207,32 @@ type RequestInfo struct {
207207
AuthInfo AuthInfo
208208
}
209209

210+
// requestInfoKey is a struct to be used as the key to store RequestInfo in a
211+
// context.
212+
type requestInfoKey struct{}
213+
210214
// RequestInfoFromContext extracts the RequestInfo from the context if it exists.
211215
//
212216
// This API is experimental.
213217
func RequestInfoFromContext(ctx context.Context) (ri RequestInfo, ok bool) {
214-
ri, ok = icredentials.RequestInfoFromContext(ctx).(RequestInfo)
218+
ri, ok = ctx.Value(requestInfoKey{}).(RequestInfo)
215219
return ri, ok
216220
}
217221

222+
// NewContextWithRequestInfo creates a new context from ctx and attaches ri to it.
223+
//
224+
// This RequestInfo will be accessible via RequestInfoFromContext.
225+
//
226+
// Intended to be used from tests for PerRPCCredentials implementations (that
227+
// often need to check connection's SecurityLevel). Should not be used from
228+
// non-test code: the gRPC client already prepares a context with the correct
229+
// RequestInfo attached when calling PerRPCCredentials.GetRequestMetadata.
230+
//
231+
// This API is experimental.
232+
func NewContextWithRequestInfo(ctx context.Context, ri RequestInfo) context.Context {
233+
return context.WithValue(ctx, requestInfoKey{}, ri)
234+
}
235+
218236
// ClientHandshakeInfo holds data to be passed to ClientHandshake. This makes
219237
// it possible to pass arbitrary data to the handshaker from gRPC, resolver,
220238
// balancer etc. Individual credential implementations control the actual

credentials/google/google_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ func TestDefaultCredentialsWithOptions(t *testing.T) {
255255
t.Run(tc.desc, func(t *testing.T) {
256256
bundle := NewDefaultCredentialsWithOptions(tc.defaultCredsOpts)
257257
ri := credentials.RequestInfo{AuthInfo: tc.authInfo}
258-
ctx := icredentials.NewRequestInfoContext(ctx, ri)
258+
ctx := credentials.NewContextWithRequestInfo(ctx, ri)
259259
got, err := bundle.PerRPCCredentials().GetRequestMetadata(ctx, "uri")
260260
if err != nil {
261261
t.Fatalf("Bundle's PerRPCCredentials().GetRequestMetadata() unexpected error = %v", err)

credentials/sts/sts_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import (
3535
"github.com/google/go-cmp/cmp"
3636

3737
"google.golang.org/grpc/credentials"
38-
icredentials "google.golang.org/grpc/internal/credentials"
3938
"google.golang.org/grpc/internal/grpctest"
4039
"google.golang.org/grpc/internal/testutils"
4140
)
@@ -102,7 +101,7 @@ func createTestContext(ctx context.Context, s credentials.SecurityLevel) context
102101
Method: "testInfo",
103102
AuthInfo: auth,
104103
}
105-
return icredentials.NewRequestInfoContext(ctx, ri)
104+
return credentials.NewContextWithRequestInfo(ctx, ri)
106105
}
107106

108107
// errReader implements the io.Reader interface and returns an error from the

internal/credentials/credentials.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,6 @@ import (
2020
"context"
2121
)
2222

23-
// requestInfoKey is a struct to be used as the key to store RequestInfo in a
24-
// context.
25-
type requestInfoKey struct{}
26-
27-
// NewRequestInfoContext creates a context with ri.
28-
func NewRequestInfoContext(ctx context.Context, ri any) context.Context {
29-
return context.WithValue(ctx, requestInfoKey{}, ri)
30-
}
31-
32-
// RequestInfoFromContext extracts the RequestInfo from ctx.
33-
func RequestInfoFromContext(ctx context.Context) any {
34-
return ctx.Value(requestInfoKey{})
35-
}
36-
3723
// clientHandshakeInfoKey is a struct used as the key to store
3824
// ClientHandshakeInfo in a context.
3925
type clientHandshakeInfoKey struct{}

internal/transport/http2_client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ func (t *http2Client) createHeaderFields(ctx context.Context, callHdr *CallHdr)
545545
Method: callHdr.Method,
546546
AuthInfo: t.authInfo,
547547
}
548-
ctxWithRequestInfo := icredentials.NewRequestInfoContext(ctx, ri)
548+
ctxWithRequestInfo := credentials.NewContextWithRequestInfo(ctx, ri)
549549
authData, err := t.getTrAuthData(ctxWithRequestInfo, aud)
550550
if err != nil {
551551
return nil, err

0 commit comments

Comments
 (0)