From 1875908b599074f7a7e0410da5e00789b2ca1e79 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Wed, 14 Jun 2023 21:58:26 +0200 Subject: [PATCH] Pass through proxy related environment variables (#465) ## Changes If set on the host, we must pass them through to Terraform. ## Tests Unit tests pass. --- bundle/deploy/terraform/init.go | 21 ++++++++++++ bundle/deploy/terraform/init_test.go | 50 ++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/bundle/deploy/terraform/init.go b/bundle/deploy/terraform/init.go index 14a5d2c02d..0ce15acbba 100644 --- a/bundle/deploy/terraform/init.go +++ b/bundle/deploy/terraform/init.go @@ -103,6 +103,21 @@ func setTempDirEnvVars(env map[string]string, b *bundle.Bundle) error { return nil } +// This function passes through all proxy related environment variables. +func setProxyEnvVars(env map[string]string, b *bundle.Bundle) error { + for _, v := range []string{"http_proxy", "https_proxy", "no_proxy"} { + // The case (upper or lower) is notoriously inconsistent for tools on Unix systems. + // We therefore try to read both the upper and lower case versions of the variable. + for _, v := range []string{strings.ToUpper(v), strings.ToLower(v)} { + if val, ok := os.LookupEnv(v); ok { + // Only set uppercase version of the variable. + env[strings.ToUpper(v)] = val + } + } + } + return nil +} + func (m *initialize) Apply(ctx context.Context, b *bundle.Bundle) error { tfConfig := b.Config.Bundle.Terraform if tfConfig == nil { @@ -142,6 +157,12 @@ func (m *initialize) Apply(ctx context.Context, b *bundle.Bundle) error { return err } + // Set the proxy related environment variables + err = setProxyEnvVars(env, b) + if err != nil { + return err + } + // Configure environment variables for auth for Terraform to use. log.Debugf(ctx, "Environment variables for Terraform: %s", strings.Join(maps.Keys(env), ", ")) err = tf.SetEnv(env) diff --git a/bundle/deploy/terraform/init_test.go b/bundle/deploy/terraform/init_test.go index 872d55c7eb..aafe5660cb 100644 --- a/bundle/deploy/terraform/init_test.go +++ b/bundle/deploy/terraform/init_test.go @@ -5,12 +5,14 @@ import ( "os" "os/exec" "runtime" + "strings" "testing" "github.com/databricks/cli/bundle" "github.com/databricks/cli/bundle/config" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "golang.org/x/exp/maps" ) func unsetEnv(t *testing.T, name string) { @@ -222,3 +224,51 @@ func TestSetTempDirEnvVarsForWindowsWithoutAnyTempDirEnvVarsSet(t *testing.T) { "TMP": tmpDir, }, env) } + +func TestSetProxyEnvVars(t *testing.T) { + b := &bundle.Bundle{ + Config: config.Root{ + Path: t.TempDir(), + Bundle: config.Bundle{ + Environment: "whatever", + }, + }, + } + + // Temporarily clear environment variables. + clearEnv := func() { + for _, v := range []string{"http_proxy", "https_proxy", "no_proxy"} { + for _, v := range []string{strings.ToUpper(v), strings.ToLower(v)} { + t.Setenv(v, "foo") + os.Unsetenv(v) + } + } + } + + // No proxy env vars set. + clearEnv() + env := make(map[string]string, 0) + err := setProxyEnvVars(env, b) + require.NoError(t, err) + assert.Len(t, env, 0) + + // Lower case set. + clearEnv() + t.Setenv("http_proxy", "foo") + t.Setenv("https_proxy", "foo") + t.Setenv("no_proxy", "foo") + env = make(map[string]string, 0) + err = setProxyEnvVars(env, b) + require.NoError(t, err) + assert.ElementsMatch(t, []string{"HTTP_PROXY", "HTTPS_PROXY", "NO_PROXY"}, maps.Keys(env)) + + // Upper case set. + clearEnv() + t.Setenv("HTTP_PROXY", "foo") + t.Setenv("HTTPS_PROXY", "foo") + t.Setenv("NO_PROXY", "foo") + env = make(map[string]string, 0) + err = setProxyEnvVars(env, b) + require.NoError(t, err) + assert.ElementsMatch(t, []string{"HTTP_PROXY", "HTTPS_PROXY", "NO_PROXY"}, maps.Keys(env)) +}