Use $RUNNER_TEMP when TF_CLI_CONFIG_FILE not in use

If you don't specify the TF_CLI_CONFIG_FILE environment variable, the
default config is written to $HOME directory, which could theoretically
be shared by multiple runners when using self-hosted runners.

When TF_CLI_CONFIG_FILE is _not_ in use, I replaced the usage of
$HOME with the directory $RUNNER_TEMP, whose setup/cleanup is
managed by the runner framework and exported a TF_CLI_CONFIG_FILE.
This commit is contained in:
Brandon Croft 2022-11-08 14:33:20 -07:00
parent 633666f66e
commit c9de504a6e
No known key found for this signature in database
GPG key ID: B01E32423322EB9D
4 changed files with 44 additions and 70 deletions

View file

@ -97,15 +97,15 @@ credentials "${credentialsHostname}" {
}`.trim();
// eslint-enable
// default to OS-specific path
let credsFile = osPlat === 'win32'
? `${process.env.APPDATA}/terraform.rc`
: `${process.env.HOME}/.terraformrc`;
// set or use the TF_CLI_CONFIG_FILE environment variable
let credsFile = process.env.TF_CLI_CONFIG_FILE;
if (!credsFile) {
credsFile = path.join(process.env.RUNNER_TEMP, 'setup-terraform.tfrc');
core.debug(`Default CLI config created as ${credsFile}`);
core.exportVariable('TF_CLI_CONFIG_FILE', credsFile);
}
// override with TF_CLI_CONFIG_FILE environment variable
credsFile = process.env.TF_CLI_CONFIG_FILE ? process.env.TF_CLI_CONFIG_FILE : credsFile;
// get containing folder
// create containing folder in case it doesn't exist
const credsFolder = path.dirname(credsFile);
core.debug(`Creating ${credsFolder}`);