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

feat: (Blueprint) Allow customization of setup sa key #1065

Merged
merged 4 commits into from
Feb 7, 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
2 changes: 1 addition & 1 deletion infra/blueprint-test/pkg/discovery/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func GetKnownDirInParents(dir string, max int) (string, error) {
if !os.IsNotExist(err) {
return dirInParent, err
}
return GetKnownDirInParents(path.Join("..", dir), max-1)
return GetKnownDirInParents(dirInParent, max-1)
}

// findDirs returns a map of directories in path
Expand Down
16 changes: 3 additions & 13 deletions infra/blueprint-test/pkg/gcloud/gcloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package gcloud

import (
"os"
"strings"

"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/utils"
Expand Down Expand Up @@ -116,18 +115,9 @@ func ActivateCredsAndEnvVars(t testing.TB, creds string) {
RunCmd(t, "auth activate-service-account", WithCommonArgs([]string{"--key-file", credsPath}))
// set auth related env vars
// TF provider auth
err = os.Setenv("GOOGLE_CREDENTIALS", creds)
if err != nil {
t.Fatal(err)
}
utils.SetEnv(t, "GOOGLE_CREDENTIALS", creds)
// gcloud SDK override
err = os.Setenv("CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE", credsPath)
if err != nil {
t.Fatal(err)
}
utils.SetEnv(t, "CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE", credsPath)
// ADC
err = os.Setenv("GOOGLE_APPLICATION_CREDENTIALS", credsPath)
if err != nil {
t.Fatal(err)
}
utils.SetEnv(t, "GOOGLE_APPLICATION_CREDENTIALS", credsPath)
}
29 changes: 19 additions & 10 deletions infra/blueprint-test/pkg/tft/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const setupKeyOutputName = "sa_key"
type TFBlueprintTest struct {
discovery.BlueprintTestConfig // additional blueprint test configs
name string // descriptive name for the test
saKey string // optional setup sa key
tfDir string // directory containing Terraform configs
tfEnvVars map[string]string // variables to pass to Terraform as environment variables prefixed with TF_VAR_
setupDir string // optional directory containing applied TF configs to import outputs as variables for the test
Expand All @@ -60,6 +61,12 @@ func WithName(name string) tftOption {
}
}

func WithSetupSaKey(saKey string) tftOption {
return func(f *TFBlueprintTest) {
f.saKey = saKey
}
}

func WithFixtureName(fixtureName string) tftOption {
return func(f *TFBlueprintTest) {
// when a test is invoked for an explicit blueprint fixture
Expand Down Expand Up @@ -153,20 +160,22 @@ func NewTFBlueprintTest(t testing.TB, opts ...tftOption) *TFBlueprintTest {
tft.setupDir = setupDir
}
}
//load TFEnvVars from setup outputs
// load setup sa Key
if tft.saKey != "" {
gcloud.ActivateCredsAndEnvVars(tft.t, tft.saKey)
}
// load TFEnvVars from setup outputs
if tft.setupDir != "" {
tft.logger.Logf(tft.t, "Loading env vars from setup %s", tft.setupDir)
loadTFEnvVar(tft.tfEnvVars, tft.getTFOutputsAsInputs(terraform.OutputAll(tft.t, &terraform.Options{TerraformDir: tft.setupDir, Logger: tft.logger})))
// setup credentials if explicit sa_key output from setup
credsEnc, exists := tft.tfEnvVars[fmt.Sprintf("TF_VAR_%s", setupKeyOutputName)]
if !exists {
tft.logger.Logf(tft.t, "Unable to find %s output from setup, skipping credential activation", setupKeyOutputName)
} else {
credDec, err := b64.StdEncoding.DecodeString(credsEnc)
if err != nil {
t.Fatalf("Unable to decode %s output from setup: %v", setupKeyOutputName, err)
if credsEnc, exists := tft.tfEnvVars[fmt.Sprintf("TF_VAR_%s", setupKeyOutputName)]; tft.saKey == "" && exists {
if credDec, err := b64.StdEncoding.DecodeString(credsEnc); err == nil {
gcloud.ActivateCredsAndEnvVars(tft.t, string(credDec))
} else {
tft.t.Fatalf("Unable to decode setup sa key: %v", err)
}
gcloud.ActivateCredsAndEnvVars(tft.t, string(credDec))
} else {
tft.logger.Logf(tft.t, "Skipping credential activation %s output from setup", setupKeyOutputName)
}
}

Expand Down
8 changes: 8 additions & 0 deletions infra/blueprint-test/pkg/utils/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,11 @@ func ValFromEnv(t testing.TB, k string) string {
}
return v
}

// SetEnv set a environment variable.
func SetEnv(t testing.TB, key string, value string) {
err := os.Setenv(key, value)
if err != nil {
t.Fatal("Unable to put environment variable %s: %v", key, err)
}
}
2 changes: 1 addition & 1 deletion infra/blueprint-test/pkg/utils/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ func WriteTmpFile(data string) (string, error) {
if err != nil {
return "", err
}
defer f.Close()
_, err = f.Write([]byte(data))
if err != nil {
return "", err
}
f.Close()
return f.Name(), nil
}