Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass through environment variables when SetEnv() is used #337

Open
radeksimko opened this issue Sep 29, 2022 · 0 comments
Open

Pass through environment variables when SetEnv() is used #337

radeksimko opened this issue Sep 29, 2022 · 0 comments

Comments

@radeksimko
Copy link
Member

Background

Currently when SetEnv() is used, for example to set a custom plugin cache directory

tf.SetEnv(map[string]string{
	"TF_PLUGIN_CACHE_DIR": "./foobar",
})

then Terraform receives any environment variables set in the map + a handful of default Terraform (TF_*) ones we set internally.

Without SetEnv() we transparently pass through [almost] all environment variables, except for the Terraform ones which we want to be set explicitly as setting these would affect the behaviour/implementation which relies on specific output format or behaviour (e.g. no log lines in the output and no colouring).

This has a potentially unexpected negative side effect in that variables such as TMP, PATH and other common ones, which the Go stdlib relies on, are unavailable, breaking certain parts of the Terraform workflow.

For example, it is not possible to run terraform init on Windows without TMP unless it runs with elevated permissions, because Terraform uses ioutil.TempFile(). Temporary files are created in $TMP on Windows, which is typically set to C:\Users\Radek\AppData\Local\Temp which only needs permissions of a regular user. If that variable isn't available, then Go defaults to C:\Windows which typically requires elevated permissions.

Proposal

Consider passing through all environment variables when SetEnv() is used, or allow-list a handful of common ones, such as TMP.

Workaround

It is possible to work around this problem using e.g.

tf.SetEnv(map[string]string{
	"TF_PLUGIN_CACHE_DIR": "./foobar",
	"TMP": os.Getenv("TMP"),
})

but it's likely there are other parts of Terraform which rely on other variables. e.g. I can imagine the need for HOME (Unix) HOMEPATH (Windows) or APPDATA in order to look up the CLI config file. https://developer.hashicorp.com/terraform/cli/config/config-file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant