Skip to content

Commit

Permalink
Override API key with user metadata (#1419)
Browse files Browse the repository at this point in the history
  • Loading branch information
cretz committed Mar 11, 2024
1 parent 682cca0 commit 3b9c572
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
9 changes: 3 additions & 6 deletions internal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -967,13 +967,10 @@ func (a apiKeyCredentials) gRPCIntercept(
if apiKey, err := a(ctx); err != nil {
return err
} else if apiKey != "" {
// Do from-add-new instead of append to overwrite anything there
md, _ := metadata.FromOutgoingContext(ctx)
if md == nil {
md = metadata.MD{}
// Only add API key if it doesn't already exist
if md, _ := metadata.FromOutgoingContext(ctx); len(md.Get("authorization")) == 0 {
ctx = metadata.AppendToOutgoingContext(ctx, "authorization", "Bearer "+apiKey)
}
md["authorization"] = []string{"Bearer " + apiKey}
ctx = metadata.NewOutgoingContext(ctx, md)
}
return invoker(ctx, method, req, reply, cc, opts...)
}
Expand Down
12 changes: 12 additions & 0 deletions internal/grpc_dialer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,18 @@ func TestCredentialsAPIKey(t *testing.T) {
metadata.ValueFromIncomingContext(srv.getSystemInfoRequestContext, "Authorization"),
)

// Overwrite via context
_, err = client.WorkflowService().GetSystemInfo(
metadata.AppendToOutgoingContext(context.Background(), "authorization", "overridden value"),
&workflowservice.GetSystemInfoRequest{},
)
require.NoError(t, err)
require.Equal(
t,
[]string{"overridden value"},
metadata.ValueFromIncomingContext(srv.getSystemInfoRequestContext, "Authorization"),
)

// Callback
client, err = DialClient(ClientOptions{
HostPort: srv.addr,
Expand Down

0 comments on commit 3b9c572

Please sign in to comment.