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
credentials: add RequestInfo to context passed to GetRequestMetadata #3057
Changes from 10 commits
dc6e857
5b75aba
dea8acc
7bda886
2cc8abd
f342e9a
f5b8f37
5415fd6
aeab10b
b6d8014
e220384
9d4b418
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,7 @@ import ( | |
|
||
"github.com/golang/protobuf/proto" | ||
"google.golang.org/grpc/credentials/internal" | ||
ginternal "google.golang.org/grpc/internal" | ||
) | ||
|
||
// PerRPCCredentials defines the common interface for the credentials which need to | ||
|
@@ -334,3 +335,27 @@ func cloneTLSConfig(cfg *tls.Config) *tls.Config { | |
|
||
return cfg.Clone() | ||
} | ||
|
||
// RequestInfo contains request data to be attached to the context passed along to GetRequestMetadata calls. This API is experimental. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please put "This API is experimental" on a new line, with a blank comment line between it and the rest of the comments. (And below; see There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's also document on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. Let me know if you would like to change the wording. |
||
type RequestInfo struct { | ||
// The method passed to Invoke or NewStream for this RPC. (For proto methods, this has the format "/some.Service/Method") | ||
Method string | ||
} | ||
|
||
// requestInfoKey is a struct to be used as the key when attaching a RequestInfo to a context object. | ||
type requestInfoKey struct{} | ||
|
||
// RequestInfoFromContext extracts the RequestInfo from the context. This API is experimental. | ||
func RequestInfoFromContext(ctx context.Context) RequestInfo { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bah, I missed this before, sorry: it's typical to also return an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
ri, ok := ctx.Value(requestInfoKey{}).(RequestInfo) | ||
if !ok { | ||
return RequestInfo{} | ||
} | ||
return ri | ||
} | ||
|
||
func init() { | ||
ginternal.NewRequestInfoContext = func(ctx context.Context, ri RequestInfo) context.Context { | ||
return context.WithValue(ctx, requestInfoKey{}, ri) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,6 +35,7 @@ import ( | |
|
||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/credentials" | ||
"google.golang.org/grpc/internal" | ||
"google.golang.org/grpc/internal/channelz" | ||
"google.golang.org/grpc/internal/syscall" | ||
"google.golang.org/grpc/keepalive" | ||
|
@@ -547,6 +548,10 @@ func (t *http2Client) getCallAuthData(ctx context.Context, audience string, call | |
// streams. | ||
func (t *http2Client) NewStream(ctx context.Context, callHdr *CallHdr) (_ *Stream, err error) { | ||
ctx = peer.NewContext(ctx, t.getPeer()) | ||
ri := credentials.RequestInfo{ | ||
Method: callHdr.Method, | ||
} | ||
ctx = internal.NewRequestInfoContext.(func(context.Context, credentials.RequestInfo) context.Context)(ctx, ri) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't consider this initially, but.. This will leave the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. Though I had to remove the unit test that tested via NewStream. So the only test coverage is via the e2e test. That said it appears that is the case for everything in the method that calls createAudience. ¯_(ツ)_/¯ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer API-level tests to unit tests for most things, so that's totally fine by me. |
||
headerFields, err := t.createHeaderFields(ctx, callHdr) | ||
if err != nil { | ||
return nil, err | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.