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
21 changes: 8 additions & 13 deletions credentials/google/google.go
Expand Up @@ -46,21 +46,16 @@ type DefaultCredentialsOptions struct {
//
// This API is experimental.
func NewDefaultCredentialsWithOptions(opts DefaultCredentialsOptions) credentials.Bundle {
perRPC := opts.PerRPCCreds
if perRPC == nil {
if opts.PerRPCCreds == nil {
ctx, cancel := context.WithTimeout(context.Background(), tokenRequestTimeout)
defer cancel()
var err error
perRPC, err = oauth.NewApplicationDefault(ctx)
opts.PerRPCCreds, err = oauth.NewApplicationDefault(ctx)
if err != nil {
logger.Warningf("NewDefaultCredentialsWithOptions: failed to create application oauth: %v", err)
}
}
c := &creds{
newPerRPCCreds: func() credentials.PerRPCCredentials {
return perRPC
},
}
c := &creds{opts: opts}
bundle, err := c.NewWithMode(internal.CredsBundleModeFallback)
if err != nil {
logger.Warningf("NewDefaultCredentialsWithOptions: failed to create new creds: %v", err)
Expand Down Expand Up @@ -89,14 +84,14 @@ func NewComputeEngineCredentials() credentials.Bundle {

// 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 @@ -123,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 @@ -140,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