From 69e77144a1eaf09290cb69ca2ca7a238c0ec86ea Mon Sep 17 00:00:00 2001 From: Jacob McCann Date: Tue, 30 May 2017 09:48:34 -0500 Subject: [PATCH] Remove secrets handling and add copying TF_VAR_ vars to lowercase names --- plugin.go | 20 ++++++++++---------- plugin_test.go | 21 +++++++++++++++++++++ 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/plugin.go b/plugin.go index 4cd9f55..f84a406 100644 --- a/plugin.go +++ b/plugin.go @@ -5,6 +5,7 @@ import ( "io/ioutil" "os" "os/exec" + "regexp" "strings" "time" @@ -53,9 +54,7 @@ func (p Plugin) Exec() error { var commands []*exec.Cmd - if len(p.Config.Secrets) != 0 { - exportSecrets(p.Config.Secrets) - } + CopyTfEnv() if p.Config.Cacert != "" { commands = append(commands, installCaCert(p.Config.Cacert)) @@ -108,9 +107,14 @@ func installCaCert(cacert string) *exec.Cmd { ) } -func exportSecrets(secrets map[string]string) { - for k, v := range secrets { - os.Setenv(fmt.Sprintf("%s", k), fmt.Sprintf("%s", os.Getenv(v))) +func CopyTfEnv() { + tfVar := regexp.MustCompile(`^TF_VAR_.*$`) + for _, e := range os.Environ() { + pair := strings.Split(e, "=") + if tfVar.MatchString(pair[0]) { + name := strings.Split(pair[0], "TF_VAR_") + os.Setenv(fmt.Sprintf("TF_VAR_%s", strings.ToLower(name[1])), pair[1]) + } } } @@ -187,10 +191,6 @@ func planCommand(config Config) *exec.Cmd { args = append(args, "-var") args = append(args, fmt.Sprintf("%s=%s", k, v)) } - for k, v := range config.Secrets { - args = append(args, "-var") - args = append(args, fmt.Sprintf("%s=%s", k, os.Getenv(v))) - } if config.Parallelism > 0 { args = append(args, fmt.Sprintf("-parallelism=%d", config.Parallelism)) } diff --git a/plugin_test.go b/plugin_test.go index d7cc9da..b951d8b 100644 --- a/plugin_test.go +++ b/plugin_test.go @@ -1,9 +1,12 @@ package main import ( + "os" "os/exec" "reflect" "testing" + + . "github.com/franela/goblin" ) func Test_destroyCommand(t *testing.T) { @@ -131,3 +134,21 @@ func Test_planCommand(t *testing.T) { }) } } + +func TestPlugin(t *testing.T) { + g := Goblin(t) + + g.Describe("CopyTfEnv", func() { + g.It("Should create copies of TF_VAR_ to lowercase", func() { + // Set some initial TF_VAR_ that are uppercase + os.Setenv("TF_VAR_SOMETHING", "some value") + os.Setenv("TF_VAR_SOMETHING_ELSE", "some other value") + + CopyTfEnv() + + // Make sure new env vars exist with proper values + g.Assert(os.Getenv("TF_VAR_something")).Equal("some value") + g.Assert(os.Getenv("TF_VAR_something_else")).Equal("some other value") + }) + }) +}