* feat: GCP authentication
Add authentication support for:
- authentication with service account key
- authentication with impersonating service account key
- authentication with application default credentials which are read from the environment
A HTTP client with a token set will be returned and used for authentication with all clients. for example
```
gcpClient, err := gcpConfig.GetClient(ctx)
if err != nil {
log.Fatalf("Failed to get client: %v", err)
}
client, err := computeCloud.NewInstancesRESTClient(ctx, option.WithHTTPClient(gcpClient))
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
```
36 lines
786 B
Go
36 lines
786 B
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
package credential
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func Test_getOpts(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
t.Run("WithCredentialsConfig", func(t *testing.T) {
|
|
c := &Config{
|
|
ProjectId: "test",
|
|
ClientEmail: "test",
|
|
}
|
|
opts, err := getOpts(WithCredentialsConfig(c))
|
|
require.NoError(t, err)
|
|
testOpts := getDefaultOptions()
|
|
testOpts.WithCredentialsConfig = c
|
|
require.Equal(t, opts, testOpts)
|
|
})
|
|
t.Run("WithCredsLastRotatedTime", func(t *testing.T) {
|
|
tm := time.Now()
|
|
opts, err := getOpts(WithCredsLastRotatedTime(tm))
|
|
require.NoError(t, err)
|
|
testOpts := getDefaultOptions()
|
|
testOpts.WithCredsLastRotatedTime = tm
|
|
require.Equal(t, opts, testOpts)
|
|
})
|
|
}
|