Skip to content

Commit

Permalink
Allow extension of default user agent in GetConfig (GoogleCloudPlatfo…
Browse files Browse the repository at this point in the history
  • Loading branch information
melinath authored and lcaggio committed Mar 17, 2022
1 parent 007efb7 commit d6fa5a3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
18 changes: 17 additions & 1 deletion mmv1/third_party/validator/getconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,23 @@ package google

import (
"context"
"fmt"
"os"

"github.com/pkg/errors"

"github.com/GoogleCloudPlatform/terraform-validator/version"
)

// Return the value of the private userAgent field
func (c *Config) UserAgent() string {
return c.userAgent
}

func GetConfig(ctx context.Context, project string, offline bool) (*Config, error) {
cfg := &Config{
Project: project,
Project: project,
userAgent: fmt.Sprintf("config-validator-tf/%s", version.BuildVersion()),
}

// Search for default credentials
Expand All @@ -26,6 +36,12 @@ func GetConfig(ctx context.Context, project string, offline bool) (*Config, erro
"GOOGLE_IMPERSONATE_SERVICE_ACCOUNT",
})

// opt in extension for adding to the User-Agent header
if ext := os.Getenv("GOOGLE_TERRAFORM_VALIDATOR_USERAGENT_EXTENSION"); ext != "" {
ua := cfg.userAgent
cfg.userAgent = fmt.Sprintf("%s %s", ua, ext)
}

if !offline {
ConfigureBasePaths(cfg)
if err := cfg.LoadAndValidate(ctx); err != nil {
Expand Down
18 changes: 17 additions & 1 deletion mmv1/third_party/validator/getconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ func getAccessToken(cfg *Config) string {
func getImpersonateServiceAccount(cfg *Config) string {
return cfg.ImpersonateServiceAccount
}
func getUserAgent(cfg *Config) string {
return cfg.UserAgent()
}

func TestGetConfigExtractsEnvVars(t *testing.T) {
ctx := context.Background()
Expand All @@ -27,38 +30,51 @@ func TestGetConfigExtractsEnvVars(t *testing.T) {
name string
envKey string
envValue string
expected string
getConfigValue configAttrGetter
}{
{
name: "GOOGLE_CREDENTIALS",
envKey: "GOOGLE_CREDENTIALS",
envValue: "whatever",
expected: "whatever",
getConfigValue: getCredentials,
},
{
name: "GOOGLE_CLOUD_KEYFILE_JSON",
envKey: "GOOGLE_CLOUD_KEYFILE_JSON",
envValue: "whatever",
expected: "whatever",
getConfigValue: getCredentials,
},
{
name: "GCLOUD_KEYFILE_JSON",
envKey: "GCLOUD_KEYFILE_JSON",
envValue: "whatever",
expected: "whatever",
getConfigValue: getCredentials,
},
{
name: "GOOGLE_OAUTH_ACCESS_TOKEN",
envKey: "GOOGLE_OAUTH_ACCESS_TOKEN",
envValue: "whatever",
expected: "whatever",
getConfigValue: getAccessToken,
},
{
name: "GOOGLE_IMPERSONATE_SERVICE_ACCOUNT",
envKey: "GOOGLE_IMPERSONATE_SERVICE_ACCOUNT",
envValue: "whatever",
expected: "whatever",
getConfigValue: getImpersonateServiceAccount,
},
{
name: "GOOGLE_TERRAFORM_VALIDATOR_USERAGENT_EXTENSION",
envKey: "GOOGLE_TERRAFORM_VALIDATOR_USERAGENT_EXTENSION",
envValue: "whatever",
expected: "config-validator-tf/dev whatever",
getConfigValue: getUserAgent,
},
}

for _, c := range cases {
Expand All @@ -74,7 +90,7 @@ func TestGetConfigExtractsEnvVars(t *testing.T) {
t.Fatalf("error building converter: %s", err)
}

assert.EqualValues(t, c.getConfigValue(cfg), c.envValue)
assert.Equal(t, c.expected, c.getConfigValue(cfg))

if isSet {
err = os.Setenv(c.envKey, originalValue)
Expand Down

0 comments on commit d6fa5a3

Please sign in to comment.