Skip to content
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

fix(auth): default gRPC token type to Bearer if not set #9800

Merged
merged 3 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion auth/grpctransport/dial_socketopt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func TestDialWithDirectPathEnabled(t *testing.T) {

pool, err := Dial(ctx, true, &Options{
Credentials: auth.NewCredentials(&auth.CredentialsOptions{
TokenProvider: staticTP("hey"),
TokenProvider: &staticTP{tok: &auth.Token{Value: "hey"}},
}),
GRPCDialOpts: []grpc.DialOption{userDialer},
Endpoint: "example.google.com:443",
Expand Down
15 changes: 12 additions & 3 deletions auth/grpctransport/grpctransport.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,15 +287,24 @@ func (c *grpcCredentialsProvider) GetRequestMetadata(ctx context.Context, uri ..
return nil, fmt.Errorf("unable to transfer credentials PerRPCCredentials: %v", err)
}
}
metadata := map[string]string{
"authorization": token.Type + " " + token.Value,
}
metadata := make(map[string]string, len(c.metadata)+1)
setAuthMetadata(token, metadata)
for k, v := range c.metadata {
metadata[k] = v
}
return metadata, nil
}

// setAuthMetadata uses the provided token to set the Authorization metadata.
// If the token.Type is empty, the type is assumed to be Bearer.
func setAuthMetadata(token *auth.Token, m map[string]string) {
typ := token.Type
if typ == "" {
typ = internal.TokenTypeBearer
}
m["authorization"] = typ + " " + token.Value
}

func (c *grpcCredentialsProvider) RequireTransportSecurity() bool {
return c.secure
}
Expand Down
51 changes: 45 additions & 6 deletions auth/grpctransport/grpctransport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package grpctransport
import (
"context"
"errors"
"log"
"net"
"testing"

Expand Down Expand Up @@ -83,7 +84,7 @@ func TestDial_FailsValidation(t *testing.T) {
opts: &Options{
DisableAuthentication: true,
Credentials: auth.NewCredentials(&auth.CredentialsOptions{
TokenProvider: staticTP("fakeToken"),
TokenProvider: &staticTP{tok: &auth.Token{Value: "fakeToken"}},
}),
},
},
Expand Down Expand Up @@ -272,6 +273,44 @@ func TestGrpcCredentialsProvider_GetClientUniverseDomain(t *testing.T) {
}
}

func TestGrpcCredentialsProvider_TokenType(t *testing.T) {
tests := []struct {
name string
tok *auth.Token
want string
}{
{
name: "type set",
tok: &auth.Token{
Value: "token",
Type: "Basic",
},
want: "Basic token",
},
{
name: "type set",
tok: &auth.Token{
Value: "token",
},
want: "Bearer token",
},
}
for _, tc := range tests {
cp := grpcCredentialsProvider{
creds: &auth.Credentials{
TokenProvider: &staticTP{tok: tc.tok},
},
}
m, err := cp.GetRequestMetadata(context.Background(), "")
if err != nil {
log.Fatalf("cp.GetRequestMetadata() = %v, want nil", err)
}
if got := m["authorization"]; got != tc.want {
t.Fatalf("got %q, want %q", got, tc.want)
}
}
}

func TestNewClient_DetectedServiceAccount(t *testing.T) {
testQuota := "testquota"
wantHeader := "bar"
Expand Down Expand Up @@ -329,12 +368,12 @@ func TestNewClient_DetectedServiceAccount(t *testing.T) {
}
}

type staticTP string
type staticTP struct {
tok *auth.Token
}

func (tp staticTP) Token(context.Context) (*auth.Token, error) {
return &auth.Token{
Value: string(tp),
}, nil
func (tp *staticTP) Token(context.Context) (*auth.Token, error) {
return tp.tok, nil
}

type fakeEchoService struct {
Expand Down