Pass `USERPROFILE` environment variable to Terraform (#1001)

## Changes
It appears that `USERPROFILE` env variable indicates where Azure CLI
stores configuration data (aka `.azure` folder).

https://learn.microsoft.com/en-us/cli/azure/azure-cli-configuration#cli-configuration-file

Passing it to terraform executable allows it to correctly authenticate
using Azure CLI.

Fixes #983 

## Tests
Ran deployment on Window VM before and after the fix.
This commit is contained in:
Andrew Nester 2023-11-22 10:16:28 +01:00 committed by GitHub
parent fa89db57e9
commit 48e293c72c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 37 deletions

View File

@ -81,6 +81,13 @@ func inheritEnvVars(ctx context.Context, environ map[string]string) error {
environ["HOME"] = home
}
// Include $USERPROFILE in set of environment variables to pass along.
// This variable is used by Azure CLI on Windows to find stored credentials and metadata
userProfile, ok := env.Lookup(ctx, "USERPROFILE")
if ok {
environ["USERPROFILE"] = userProfile
}
// Include $PATH in set of environment variables to pass along.
// This is necessary to ensure that our Terraform provider can use the
// same auxiliary programs (e.g. `az`, or `gcloud`) as the CLI.
@ -113,8 +120,6 @@ func setTempDirEnvVars(ctx context.Context, environ map[string]string, b *bundle
environ["TMP"] = v
} else if v, ok := env.Lookup(ctx, "TEMP"); ok {
environ["TEMP"] = v
} else if v, ok := env.Lookup(ctx, "USERPROFILE"); ok {
environ["USERPROFILE"] = v
} else {
tmpDir, err := b.CacheDir(ctx, "tmp")
if err != nil {

View File

@ -163,36 +163,6 @@ func TestSetTempDirEnvVarsForWindowWithUserProfileAndTempSet(t *testing.T) {
}, env)
}
func TestSetTempDirEnvVarsForWindowWithUserProfileSet(t *testing.T) {
if runtime.GOOS != "windows" {
t.SkipNow()
}
b := &bundle.Bundle{
Config: config.Root{
Path: t.TempDir(),
Bundle: config.Bundle{
Target: "whatever",
},
},
}
// Set environment variables
unsetEnv(t, "TMP")
unsetEnv(t, "TEMP")
t.Setenv("USERPROFILE", "c:\\foo\\c")
// compute env
env := make(map[string]string, 0)
err := setTempDirEnvVars(context.Background(), env, b)
require.NoError(t, err)
// assert that we pass through the user profile
assert.Equal(t, map[string]string{
"USERPROFILE": "c:\\foo\\c",
}, env)
}
func TestSetTempDirEnvVarsForWindowsWithoutAnyTempDirEnvVarsSet(t *testing.T) {
if runtime.GOOS != "windows" {
t.SkipNow()
@ -284,9 +254,18 @@ func TestInheritEnvVars(t *testing.T) {
require.NoError(t, err)
require.Equal(t, map[string]string{
"HOME": "/home/testuser",
"PATH": "/foo:/bar",
"TF_CLI_CONFIG_FILE": "/tmp/config.tfrc",
}, env)
require.Equal(t, env["HOME"], "/home/testuser")
require.Equal(t, env["PATH"], "/foo:/bar")
require.Equal(t, env["TF_CLI_CONFIG_FILE"], "/tmp/config.tfrc")
}
func TestSetUserProfileFromInheritEnvVars(t *testing.T) {
t.Setenv("USERPROFILE", "c:\\foo\\c")
env := make(map[string]string, 0)
err := inheritEnvVars(context.Background(), env)
require.NoError(t, err)
assert.Contains(t, env, "USERPROFILE")
assert.Equal(t, env["USERPROFILE"], "c:\\foo\\c")
}