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

Allow extension of default user agent in GetConfig #5639

Merged
merged 1 commit into from
Jan 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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