Skip to content

Commit

Permalink
credentials/google: introduce a new API `NewComputeEngineCredsWithOpt…
Browse files Browse the repository at this point in the history
…ions` (#4767)
  • Loading branch information
mohanli-ml committed Sep 30, 2021
1 parent 2ae5ac1 commit 127c052
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
22 changes: 20 additions & 2 deletions credentials/google/google.go
Expand Up @@ -64,14 +64,32 @@ func NewDefaultCredentials() credentials.Bundle {
//
// 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
}

// NewComputeEngineCredsWithOptions returns a credentials bundle that is configured to work
// with google services. This API must only be used when running on GCE.
//
// This API is experimental.
func NewComputeEngineCredsWithOptions(perRPCOpts ComputeEngineCredsOptions) credentials.Bundle {
perRPC := oauth.NewComputeEngine()
if perRPCOpts.PerRPCCreds != nil {
perRPC = perRPCOpts.PerRPCCreds
}
c := &creds{
newPerRPCCreds: func() credentials.PerRPCCredentials {
return oauth.NewComputeEngine()
return perRPC
},
}
bundle, err := c.NewWithMode(internal.CredsBundleModeFallback)
if err != nil {
logger.Warningf("compute engine creds: failed to create new creds: %v", err)
logger.Warningf("compute engine creds with per rpc: failed to create new creds: %v", err)
}
return bundle
}
Expand Down
5 changes: 3 additions & 2 deletions credentials/google/google_test.go
Expand Up @@ -76,8 +76,9 @@ func overrideNewCredsFuncs() func() {
func TestClientHandshakeBasedOnClusterName(t *testing.T) {
defer overrideNewCredsFuncs()()
for bundleTyp, tc := range map[string]credentials.Bundle{
"defaultCreds": NewDefaultCredentials(),
"computeCreds": NewComputeEngineCredentials(),
"defaultCreds": NewDefaultCredentials(),
"computeCreds": NewComputeEngineCredentials(),
"computeCredsPerRPC": NewComputeEngineCredsWithOptions(ComputeEngineCredsOptions{}),
} {
tests := []struct {
name string
Expand Down

0 comments on commit 127c052

Please sign in to comment.