Skip to content

Commit 3b9c572

Browse files
authored
Override API key with user metadata (#1419)
1 parent 682cca0 commit 3b9c572

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

internal/client.go

+3-6
Original file line numberDiff line numberDiff line change
@@ -967,13 +967,10 @@ func (a apiKeyCredentials) gRPCIntercept(
967967
if apiKey, err := a(ctx); err != nil {
968968
return err
969969
} else if apiKey != "" {
970-
// Do from-add-new instead of append to overwrite anything there
971-
md, _ := metadata.FromOutgoingContext(ctx)
972-
if md == nil {
973-
md = metadata.MD{}
970+
// Only add API key if it doesn't already exist
971+
if md, _ := metadata.FromOutgoingContext(ctx); len(md.Get("authorization")) == 0 {
972+
ctx = metadata.AppendToOutgoingContext(ctx, "authorization", "Bearer "+apiKey)
974973
}
975-
md["authorization"] = []string{"Bearer " + apiKey}
976-
ctx = metadata.NewOutgoingContext(ctx, md)
977974
}
978975
return invoker(ctx, method, req, reply, cc, opts...)
979976
}

internal/grpc_dialer_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,18 @@ func TestCredentialsAPIKey(t *testing.T) {
457457
metadata.ValueFromIncomingContext(srv.getSystemInfoRequestContext, "Authorization"),
458458
)
459459

460+
// Overwrite via context
461+
_, err = client.WorkflowService().GetSystemInfo(
462+
metadata.AppendToOutgoingContext(context.Background(), "authorization", "overridden value"),
463+
&workflowservice.GetSystemInfoRequest{},
464+
)
465+
require.NoError(t, err)
466+
require.Equal(
467+
t,
468+
[]string{"overridden value"},
469+
metadata.ValueFromIncomingContext(srv.getSystemInfoRequestContext, "Authorization"),
470+
)
471+
460472
// Callback
461473
client, err = DialClient(ClientOptions{
462474
HostPort: srv.addr,

0 commit comments

Comments
 (0)