* 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)
}
```
49 lines
1.0 KiB
Go
49 lines
1.0 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
package credential
|
|
|
|
import "time"
|
|
|
|
// options = how options are represented
|
|
type Options struct {
|
|
WithCredentialsConfig *Config
|
|
WithCredsLastRotatedTime time.Time
|
|
}
|
|
|
|
// getOpts - iterate the inbound Options and return a struct
|
|
func getOpts(opts ...Option) (*Options, error) {
|
|
defaultOptions := getDefaultOptions()
|
|
for _, opt := range opts {
|
|
if err := opt(defaultOptions); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return defaultOptions, nil
|
|
}
|
|
|
|
// Option - how Options are passed as arguments
|
|
type Option func(*Options) error
|
|
|
|
func getDefaultOptions() *Options {
|
|
return &Options{
|
|
WithCredentialsConfig: &Config{},
|
|
}
|
|
}
|
|
|
|
// WithCredentialsConfig - set the credentials config
|
|
func WithCredentialsConfig(c *Config) Option {
|
|
return func(o *Options) error {
|
|
o.WithCredentialsConfig = c
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// WithCredsLastRotatedTime - set the last rotated time
|
|
func WithCredsLastRotatedTime(t time.Time) Option {
|
|
return func(o *Options) error {
|
|
o.WithCredsLastRotatedTime = t
|
|
return nil
|
|
}
|
|
}
|