mirror of https://github.com/databricks/cli.git
Pass through proxy related environment variables (#465)
## Changes If set on the host, we must pass them through to Terraform. ## Tests Unit tests pass.
This commit is contained in:
parent
30e9cf048c
commit
1875908b59
|
@ -103,6 +103,21 @@ func setTempDirEnvVars(env map[string]string, b *bundle.Bundle) error {
|
||||||
return nil
|
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 {
|
func (m *initialize) Apply(ctx context.Context, b *bundle.Bundle) error {
|
||||||
tfConfig := b.Config.Bundle.Terraform
|
tfConfig := b.Config.Bundle.Terraform
|
||||||
if tfConfig == nil {
|
if tfConfig == nil {
|
||||||
|
@ -142,6 +157,12 @@ func (m *initialize) Apply(ctx context.Context, b *bundle.Bundle) error {
|
||||||
return err
|
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.
|
// Configure environment variables for auth for Terraform to use.
|
||||||
log.Debugf(ctx, "Environment variables for Terraform: %s", strings.Join(maps.Keys(env), ", "))
|
log.Debugf(ctx, "Environment variables for Terraform: %s", strings.Join(maps.Keys(env), ", "))
|
||||||
err = tf.SetEnv(env)
|
err = tf.SetEnv(env)
|
||||||
|
|
|
@ -5,12 +5,14 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/databricks/cli/bundle"
|
"github.com/databricks/cli/bundle"
|
||||||
"github.com/databricks/cli/bundle/config"
|
"github.com/databricks/cli/bundle/config"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
"golang.org/x/exp/maps"
|
||||||
)
|
)
|
||||||
|
|
||||||
func unsetEnv(t *testing.T, name string) {
|
func unsetEnv(t *testing.T, name string) {
|
||||||
|
@ -222,3 +224,51 @@ func TestSetTempDirEnvVarsForWindowsWithoutAnyTempDirEnvVarsSet(t *testing.T) {
|
||||||
"TMP": tmpDir,
|
"TMP": tmpDir,
|
||||||
}, env)
|
}, 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))
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue