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

credentials: Support ALTSPerRPCCreds in DefaultCredentialsOptions #7775

Merged
merged 5 commits into from
Oct 25, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions credentials/google/google.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
type DefaultCredentialsOptions struct {
// PerRPCCreds is a per RPC credentials that is passed to a bundle.
PerRPCCreds credentials.PerRPCCredentials
// ALTSPerRPCCreds is a per RPC credentials that, if specified, will
// supercede PerRPCCreds above for and only for ALTS connections.
ALTSPerRPCCreds credentials.PerRPCCredentials
}

// NewDefaultCredentialsWithOptions returns a credentials bundle that is
Expand All @@ -55,6 +58,12 @@
logger.Warningf("NewDefaultCredentialsWithOptions: failed to create application oauth: %v", err)
}
}
if opts.ALTSPerRPCCreds != nil {
opts.PerRPCCreds = &dualPerRPCCreds{
perRPCCreds: opts.PerRPCCreds,
altsPerRPCCreds: opts.ALTSPerRPCCreds,
}
}
c := &creds{opts: opts}
bundle, err := c.NewWithMode(internal.CredsBundleModeFallback)
if err != nil {
Expand Down Expand Up @@ -143,3 +152,27 @@

return newCreds, nil
}

// dualPerRPCCreds implements credentials.PerRPCCredentials by embedding the
// fallback PerRPCCredentials and the ALTS one. It pickes one of them based on
// the channel type.
type dualPerRPCCreds struct {
perRPCCreds credentials.PerRPCCredentials
altsPerRPCCreds credentials.PerRPCCredentials
}

func (d *dualPerRPCCreds) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) {
ri, ok := credentials.RequestInfoFromContext(ctx)
if !ok {
return nil, fmt.Errorf("request info not found from context")
}

Check warning on line 168 in credentials/google/google.go

View check run for this annotation

Codecov / codecov/patch

credentials/google/google.go#L167-L168

Added lines #L167 - L168 were not covered by tests
if authType := ri.AuthInfo.AuthType(); authType == "alts" {
return d.altsPerRPCCreds.GetRequestMetadata(ctx, uri...)
}
// This ensures backward compatibility even if authType is not "tls".
return d.perRPCCreds.GetRequestMetadata(ctx, uri...)
}

func (d *dualPerRPCCreds) RequireTransportSecurity() bool {
return d.altsPerRPCCreds.RequireTransportSecurity() || d.perRPCCreds.RequireTransportSecurity()

Check warning on line 177 in credentials/google/google.go

View check run for this annotation

Codecov / codecov/patch

credentials/google/google.go#L176-L177

Added lines #L176 - L177 were not covered by tests
}
98 changes: 98 additions & 0 deletions credentials/google/google_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"net"
"testing"

"github.com/google/go-cmp/cmp"
"google.golang.org/grpc/credentials"
icredentials "google.golang.org/grpc/internal/credentials"
"google.golang.org/grpc/internal/grpctest"
Expand Down Expand Up @@ -59,6 +60,18 @@ func (t *testAuthInfo) AuthType() string {
return t.typ
}

type testPerRPCCreds struct {
md map[string]string
}

func (c *testPerRPCCreds) RequireTransportSecurity() bool {
return true
}

func (c *testPerRPCCreds) GetRequestMetadata(context.Context, ...string) (map[string]string, error) {
return c.md, nil
}

var (
testTLS = &testCreds{typ: "tls"}
testALTS = &testCreds{typ: "alts"}
Expand Down Expand Up @@ -161,3 +174,88 @@ func (s) TestClientHandshakeBasedOnClusterName(t *testing.T) {
}
}
}

func TestDefaultCredentialsWithOptions(t *testing.T) {
md1 := map[string]string{"foo": "tls"}
md2 := map[string]string{"foo": "alts"}
tests := []struct {
desc string
defaultCredsOpts DefaultCredentialsOptions
authInfo credentials.AuthInfo
wantedMetadata map[string]string
}{
{
desc: "no ALTSPerRPCCreds with tls channel",
defaultCredsOpts: DefaultCredentialsOptions{
PerRPCCreds: &testPerRPCCreds{
md: md1,
},
},
authInfo: &testAuthInfo{typ: "tls"},
wantedMetadata: md1,
},
{
desc: "no ALTSPerRPCCreds with alts channel",
defaultCredsOpts: DefaultCredentialsOptions{
PerRPCCreds: &testPerRPCCreds{
md: md1,
},
},
authInfo: &testAuthInfo{typ: "alts"},
wantedMetadata: md1,
},
{
desc: "ALTSPerRPCCreds specified with tls channel",
defaultCredsOpts: DefaultCredentialsOptions{
PerRPCCreds: &testPerRPCCreds{
md: md1,
},
ALTSPerRPCCreds: &testPerRPCCreds{
md: md2,
},
},
authInfo: &testAuthInfo{typ: "tls"},
wantedMetadata: md1,
},
{
desc: "ALTSPerRPCCreds specified with alts channel",
defaultCredsOpts: DefaultCredentialsOptions{
PerRPCCreds: &testPerRPCCreds{
md: md1,
},
ALTSPerRPCCreds: &testPerRPCCreds{
md: md2,
},
},
authInfo: &testAuthInfo{typ: "alts"},
wantedMetadata: md2,
},
{
desc: "ALTSPerRPCCreds specified with unknown channel",
defaultCredsOpts: DefaultCredentialsOptions{
PerRPCCreds: &testPerRPCCreds{
md: md1,
},
ALTSPerRPCCreds: &testPerRPCCreds{
md: md2,
},
},
authInfo: &testAuthInfo{typ: "foo"},
wantedMetadata: md1,
},
}
for _, tc := range tests {
t.Run(tc.desc, func(t *testing.T) {
bundle := NewDefaultCredentialsWithOptions(tc.defaultCredsOpts)
ri := credentials.RequestInfo{AuthInfo: tc.authInfo}
ctx := icredentials.NewRequestInfoContext(context.Background(), ri)
got, err := bundle.PerRPCCredentials().GetRequestMetadata(ctx, "uri")
if err != nil {
t.Fatalf("Bundle's PerRPCCredentials().GetRequestMetadata() unexpected error = %v", err)
}
if diff := cmp.Diff(got, tc.wantedMetadata); diff != "" {
t.Errorf("Unexpected request metadata from bundle's PerRPCCredentials. Diff (-got +want):\n%v", diff)
}
})
}
}