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

creds/google: replace NewComputeEngineCredsWithOptions with NewDefaultCredentialsWithOptions #4830

Merged
merged 7 commits into from Oct 5, 2021
Merged
Show file tree
Hide file tree
Changes from 6 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
82 changes: 35 additions & 47 deletions credentials/google/google.go
Expand Up @@ -35,75 +35,63 @@ const tokenRequestTimeout = 30 * time.Second

var logger = grpclog.Component("credentials")

// NewDefaultCredentials returns a credentials bundle that is configured to work
// with google services.
// DefaultCredentialsOptions constructs options to build DefaultCredentials.
type DefaultCredentialsOptions struct {
// PerRPCCreds is a per RPC credentials that is passed to a bundle.
PerRPCCreds credentials.PerRPCCredentials
}

// NewDefaultCredentialsWithOptions returns a credentials bundle that is
// configured to work with google services.
//
// This API is experimental.
func NewDefaultCredentials() credentials.Bundle {
c := &creds{
newPerRPCCreds: func() credentials.PerRPCCredentials {
ctx, cancel := context.WithTimeout(context.Background(), tokenRequestTimeout)
defer cancel()
perRPCCreds, err := oauth.NewApplicationDefault(ctx)
if err != nil {
logger.Warningf("google default creds: failed to create application oauth: %v", err)
}
return perRPCCreds
},
func NewDefaultCredentialsWithOptions(opts DefaultCredentialsOptions) credentials.Bundle {
if opts.PerRPCCreds == nil {
ctx, cancel := context.WithTimeout(context.Background(), tokenRequestTimeout)
defer cancel()
var err error
opts.PerRPCCreds, err = oauth.NewApplicationDefault(ctx)
if err != nil {
logger.Warningf("NewDefaultCredentialsWithOptions: failed to create application oauth: %v", err)
}
}
c := &creds{opts: opts}
bundle, err := c.NewWithMode(internal.CredsBundleModeFallback)
if err != nil {
logger.Warningf("google default creds: failed to create new creds: %v", err)
logger.Warningf("NewDefaultCredentialsWithOptions: failed to create new creds: %v", err)
}
return bundle
}

// NewComputeEngineCredentials returns a credentials bundle that is configured to work
// with google services. This API must only be used when running on GCE. Authentication configured
// by this API represents the GCE VM's default service account.
// NewDefaultCredentials returns a credentials bundle that is configured to work
// with google services.
//
// This API is experimental.
func NewComputeEngineCredentials() credentials.Bundle {
return NewComputeEngineCredsWithOptions(ComputeEngineCredsOptions{})
}

// ComputeEngineCredsOptions constructs compite engine credentials with options.
type ComputeEngineCredsOptions struct {
// PerRPCCreds is a per RPC credentials that is passed to a bundle.
PerRPCCreds credentials.PerRPCCredentials
func NewDefaultCredentials() credentials.Bundle {
return NewDefaultCredentialsWithOptions(DefaultCredentialsOptions{})
}

// NewComputeEngineCredsWithOptions returns a credentials bundle that is configured to work
// with google services. This API must only be used when running on GCE.
// NewComputeEngineCredentials returns a credentials bundle that is configured to work
// with google services. This API must only be used when running on GCE. Authentication configured
// by this API represents the GCE VM's default service account.
//
// This API is experimental.
dfawley marked this conversation as resolved.
Show resolved Hide resolved
func NewComputeEngineCredsWithOptions(perRPCOpts ComputeEngineCredsOptions) credentials.Bundle {
perRPC := oauth.NewComputeEngine()
if perRPCOpts.PerRPCCreds != nil {
perRPC = perRPCOpts.PerRPCCreds
}
c := &creds{
newPerRPCCreds: func() credentials.PerRPCCredentials {
return perRPC
},
}
bundle, err := c.NewWithMode(internal.CredsBundleModeFallback)
if err != nil {
logger.Warningf("compute engine creds with per rpc: failed to create new creds: %v", err)
}
return bundle
func NewComputeEngineCredentials() credentials.Bundle {
return NewDefaultCredentialsWithOptions(DefaultCredentialsOptions{
PerRPCCreds: oauth.NewComputeEngine(),
})
}

// creds implements credentials.Bundle.
type creds struct {
opts DefaultCredentialsOptions

// Supported modes are defined in internal/internal.go.
mode string
// The transport credentials associated with this bundle.
transportCreds credentials.TransportCredentials
// The per RPC credentials associated with this bundle.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe clarify this comment a bit, like: The active per-RPC credentials for the current mode of this bundle.

Or eliminate this field entirely and make *creds.PerRPCCredentials() return nil if mode!=Fallback|BackendFromBalancer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added active.

perRPCCreds credentials.PerRPCCredentials
// Creates new per RPC credentials
newPerRPCCreds func() credentials.PerRPCCredentials
}

func (c *creds) TransportCredentials() credentials.TransportCredentials {
Expand All @@ -130,8 +118,8 @@ var (
// existing Bundle may cause races.
func (c *creds) NewWithMode(mode string) (credentials.Bundle, error) {
newCreds := &creds{
mode: mode,
newPerRPCCreds: c.newPerRPCCreds,
opts: c.opts,
mode: mode,
}

// Create transport credentials.
Expand All @@ -147,7 +135,7 @@ func (c *creds) NewWithMode(mode string) (credentials.Bundle, error) {
}

if mode == internal.CredsBundleModeFallback || mode == internal.CredsBundleModeBackendFromBalancer {
newCreds.perRPCCreds = newCreds.newPerRPCCreds()
newCreds.perRPCCreds = newCreds.opts.PerRPCCreds
}

return newCreds, nil
Expand Down
6 changes: 3 additions & 3 deletions credentials/google/google_test.go
Expand Up @@ -76,9 +76,9 @@ func overrideNewCredsFuncs() func() {
func TestClientHandshakeBasedOnClusterName(t *testing.T) {
defer overrideNewCredsFuncs()()
for bundleTyp, tc := range map[string]credentials.Bundle{
"defaultCreds": NewDefaultCredentials(),
"computeCreds": NewComputeEngineCredentials(),
"computeCredsPerRPC": NewComputeEngineCredsWithOptions(ComputeEngineCredsOptions{}),
"defaultCredsWithOptions": NewDefaultCredentialsWithOptions(DefaultCredentialsOptions{}),
"defaultCreds": NewDefaultCredentials(),
"computeCreds": NewComputeEngineCredentials(),
} {
tests := []struct {
name string
Expand Down