From 1a1da607ca5524265f0ddaf91fc28c44bdc8f7d0 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Tue, 13 Jun 2023 00:20:40 +0200 Subject: [PATCH 1/2] Pass through proxy related environment variables --- bundle/deploy/terraform/init.go | 18 ++++++++++ bundle/deploy/terraform/init_test.go | 50 ++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/bundle/deploy/terraform/init.go b/bundle/deploy/terraform/init.go index 14a5d2c02d..6d73749db5 100644 --- a/bundle/deploy/terraform/init.go +++ b/bundle/deploy/terraform/init.go @@ -103,6 +103,18 @@ 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"} { + for _, v := range []string{strings.ToUpper(v), strings.ToLower(v)} { + if val, ok := os.LookupEnv(v); ok { + env[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 +154,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..80171ab566 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)) +} From 0616e1d628005baee3ff0d390ef300f3049a7179 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Wed, 14 Jun 2023 20:47:30 +0200 Subject: [PATCH 2/2] Pass through uppercase version of proxy environment variable names --- bundle/deploy/terraform/init.go | 5 ++++- bundle/deploy/terraform/init_test.go | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/bundle/deploy/terraform/init.go b/bundle/deploy/terraform/init.go index 6d73749db5..0ce15acbba 100644 --- a/bundle/deploy/terraform/init.go +++ b/bundle/deploy/terraform/init.go @@ -106,9 +106,12 @@ func setTempDirEnvVars(env map[string]string, b *bundle.Bundle) error { // 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 { - env[v] = val + // Only set uppercase version of the variable. + env[strings.ToUpper(v)] = val } } } diff --git a/bundle/deploy/terraform/init_test.go b/bundle/deploy/terraform/init_test.go index 80171ab566..aafe5660cb 100644 --- a/bundle/deploy/terraform/init_test.go +++ b/bundle/deploy/terraform/init_test.go @@ -260,7 +260,7 @@ func TestSetProxyEnvVars(t *testing.T) { 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)) + assert.ElementsMatch(t, []string{"HTTP_PROXY", "HTTPS_PROXY", "NO_PROXY"}, maps.Keys(env)) // Upper case set. clearEnv()