From 4931bb08c1e785772c24e61c15fb583cea8c8479 Mon Sep 17 00:00:00 2001 From: Gleb Kanterov Date: Thu, 6 Jul 2023 15:12:43 +0200 Subject: [PATCH 1/4] Propagate TF_CLI_CONFIG_FILE env variable --- bundle/deploy/terraform/init.go | 23 +++++++++++++++++++---- bundle/deploy/terraform/init_test.go | 16 ++++++++++++++++ 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/bundle/deploy/terraform/init.go b/bundle/deploy/terraform/init.go index 14a5d2c02d7..b3175e9df9a 100644 --- a/bundle/deploy/terraform/init.go +++ b/bundle/deploy/terraform/init.go @@ -70,6 +70,22 @@ func (m *initialize) findExecPath(ctx context.Context, b *bundle.Bundle, tf *con return tf.ExecPath, nil } +// This function inherits some environment variables for Terraform CLI. +func inheritEnvVars(env map[string]string) error { + // Include $HOME in set of environment variables to pass along. + home, ok := os.LookupEnv("HOME") + if ok { + env["HOME"] = home + } + + // Include $TF_CLI_CONFIG_FILE to override terraform provider in development. + if config_file, ok := os.LookupEnv("TF_CLI_CONFIG_FILE"); ok { + env["TF_CLI_CONFIG_FILE"] = config_file + } + + return nil +} + // This function sets temp dir location for terraform to use. If user does not // specify anything here, we fall back to a `tmp` directory in the bundle's cache // directory @@ -130,10 +146,9 @@ func (m *initialize) Apply(ctx context.Context, b *bundle.Bundle) error { return err } - // Include $HOME in set of environment variables to pass along. - home, ok := os.LookupEnv("HOME") - if ok { - env["HOME"] = home + err = inheritEnvVars(env) + if err != nil { + return err } // Set the temporary directory environment variables diff --git a/bundle/deploy/terraform/init_test.go b/bundle/deploy/terraform/init_test.go index 872d55c7eb9..f8ee6fad269 100644 --- a/bundle/deploy/terraform/init_test.go +++ b/bundle/deploy/terraform/init_test.go @@ -222,3 +222,19 @@ func TestSetTempDirEnvVarsForWindowsWithoutAnyTempDirEnvVarsSet(t *testing.T) { "TMP": tmpDir, }, env) } + +func TestInheritEnvVars(t *testing.T) { + env := map[string]string{} + + t.Setenv("HOME", "/home/testuser") + t.Setenv("TF_CLI_CONFIG_FILE", "/tmp/config.tfrc") + + err := inheritEnvVars(env) + + require.NoError(t, err) + + require.Equal(t, map[string]string{ + "HOME": "/home/testuser", + "TF_CLI_CONFIG_FILE": "/tmp/config.tfrc", + }, env) +} From 929ef860333e24c5ff13dcd5c826b449c08c430d Mon Sep 17 00:00:00 2001 From: Gleb Kanterov Date: Thu, 6 Jul 2023 15:41:36 +0200 Subject: [PATCH 2/4] Update init_test.go --- bundle/deploy/terraform/init_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bundle/deploy/terraform/init_test.go b/bundle/deploy/terraform/init_test.go index 9ac80f7ef50..79e18170ef9 100644 --- a/bundle/deploy/terraform/init_test.go +++ b/bundle/deploy/terraform/init_test.go @@ -272,7 +272,7 @@ func TestSetProxyEnvVars(t *testing.T) { require.NoError(t, err) assert.ElementsMatch(t, []string{"HTTP_PROXY", "HTTPS_PROXY", "NO_PROXY"}, maps.Keys(env)) } - + func TestInheritEnvVars(t *testing.T) { env := map[string]string{} From 19ff45c9ecd563f75b17920848176165a25f8de1 Mon Sep 17 00:00:00 2001 From: Gleb Kanterov Date: Thu, 6 Jul 2023 16:55:30 +0200 Subject: [PATCH 3/4] Update init.go --- bundle/deploy/terraform/init.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bundle/deploy/terraform/init.go b/bundle/deploy/terraform/init.go index 13513299618..6653ff383c0 100644 --- a/bundle/deploy/terraform/init.go +++ b/bundle/deploy/terraform/init.go @@ -78,9 +78,9 @@ func inheritEnvVars(env map[string]string) error { env["HOME"] = home } - // Include $TF_CLI_CONFIG_FILE to override terraform provider in development. - if config_file, ok := os.LookupEnv("TF_CLI_CONFIG_FILE"); ok { - env["TF_CLI_CONFIG_FILE"] = config_file + configFile, ok := os.LookupEnv("TF_CLI_CONFIG_FILE") + if ok { + env["TF_CLI_CONFIG_FILE"] = configFile } return nil From 1177e8de5773bd7846f24edf86e724131e47e132 Mon Sep 17 00:00:00 2001 From: Gleb Kanterov Date: Thu, 6 Jul 2023 16:55:56 +0200 Subject: [PATCH 4/4] Update init.go --- bundle/deploy/terraform/init.go | 1 + 1 file changed, 1 insertion(+) diff --git a/bundle/deploy/terraform/init.go b/bundle/deploy/terraform/init.go index 6653ff383c0..eb3e99d18a8 100644 --- a/bundle/deploy/terraform/init.go +++ b/bundle/deploy/terraform/init.go @@ -78,6 +78,7 @@ func inheritEnvVars(env map[string]string) error { env["HOME"] = home } + // Include $TF_CLI_CONFIG_FILE to override terraform provider in development. configFile, ok := os.LookupEnv("TF_CLI_CONFIG_FILE") if ok { env["TF_CLI_CONFIG_FILE"] = configFile