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

@ -23,18 +23,12 @@ const setup = require('../lib/setup-terraform');
// .fn(console.error);
describe('Setup Terraform', () => {
const HOME = process.env.HOME;
const APPDATA = process.env.APPDATA;
beforeEach(() => {
process.env.HOME = '/tmp/asdf';
process.env.APPDATA = '/tmp/asdf';
process.env.RUNNER_TEMP = '/tmp/asdf'
});
afterEach(async () => {
await io.rmRF(process.env.HOME);
process.env.HOME = HOME;
process.env.APPDATA = APPDATA;
await io.rmRF(process.env.RUNNER_TEMP);
});
test('gets specific version and adds token and hostname on linux, amd64', async () => {
@ -73,8 +67,8 @@ describe('Setup Terraform', () => {
// downloaded CLI has been added to path
expect(core.addPath).toHaveBeenCalled();
// expect credentials are in ${HOME}.terraformrc
const creds = await fs.readFile(`${process.env.HOME}/.terraformrc`, { encoding: 'utf8' });
// expect credentials are in ${RUNNER_TEMP}/setup-terraform.tfrc
const creds = await fs.readFile(`${process.env.RUNNER_TEMP}/setup-terraform.tfrc`, { encoding: 'utf8' });
expect(creds.indexOf(credentialsHostname)).toBeGreaterThan(-1);
expect(creds.indexOf(credentialsToken)).toBeGreaterThan(-1);
});
@ -118,8 +112,8 @@ describe('Setup Terraform', () => {
// downloaded CLI has been added to path
expect(core.addPath).toHaveBeenCalled();
// expect credentials are in ${HOME}.terraformrc
const creds = await fs.readFile(`${process.env.HOME}/terraform.rc`, { encoding: 'utf8' });
// expect credentials are in ${RUNNER_TEMP}/setup-terraform.tfrc
const creds = await fs.readFile(`${process.env.RUNNER_TEMP}/setup-terraform.tfrc`, { encoding: 'utf8' });
expect(creds.indexOf(credentialsHostname)).toBeGreaterThan(-1);
expect(creds.indexOf(credentialsToken)).toBeGreaterThan(-1);
});
@ -161,8 +155,8 @@ describe('Setup Terraform', () => {
// downloaded CLI has been added to path
expect(core.addPath).toHaveBeenCalled();
// expect credentials are in ${HOME}.terraformrc
const creds = await fs.readFile(`${process.env.HOME}/.terraformrc`, { encoding: 'utf8' });
// expect credentials are in ${RUNNER_TEMP}/setup-terraform.tfrc
const creds = await fs.readFile(`${process.env.RUNNER_TEMP}/setup-terraform.tfrc`, { encoding: 'utf8' });
expect(creds.indexOf(credentialsHostname)).toBeGreaterThan(-1);
expect(creds.indexOf(credentialsToken)).toBeGreaterThan(-1);
});
@ -204,8 +198,8 @@ describe('Setup Terraform', () => {
// downloaded CLI has been added to path
expect(core.addPath).toHaveBeenCalled();
// expect credentials are in ${HOME}.terraformrc
const creds = await fs.readFile(`${process.env.HOME}/.terraformrc`, { encoding: 'utf8' });
// expect credentials are in ${RUNNER_TEMP}/setup-terraform.tfrc
const creds = await fs.readFile(`${process.env.RUNNER_TEMP}/setup-terraform.tfrc`, { encoding: 'utf8' });
expect(creds.indexOf(credentialsHostname)).toBeGreaterThan(-1);
expect(creds.indexOf(credentialsToken)).toBeGreaterThan(-1);
});
@ -246,8 +240,8 @@ describe('Setup Terraform', () => {
// downloaded CLI has been added to path
expect(core.addPath).toHaveBeenCalled();
// expect credentials are in ${HOME}.terraformrc
const creds = await fs.readFile(`${process.env.HOME}/.terraformrc`, { encoding: 'utf8' });
// expect credentials are in ${RUNNER_TEMP}/setup-terraform.tfrc
const creds = await fs.readFile(`${process.env.RUNNER_TEMP}/setup-terraform.tfrc`, { encoding: 'utf8' });
expect(creds.indexOf(credentialsHostname)).toBeGreaterThan(-1);
expect(creds.indexOf(credentialsToken)).toBeGreaterThan(-1);
});
@ -288,8 +282,8 @@ describe('Setup Terraform', () => {
// downloaded CLI has been added to path
expect(core.addPath).toHaveBeenCalled();
// expect credentials are in ${HOME}.terraformrc
const creds = await fs.readFile(`${process.env.HOME}/.terraformrc`, { encoding: 'utf8' });
// expect credentials are in ${RUNNER_TEMP}/setup-terraform.tfrc
const creds = await fs.readFile(`${process.env.RUNNER_TEMP}/setup-terraform.tfrc`, { encoding: 'utf8' });
expect(creds.indexOf(credentialsHostname)).toBeGreaterThan(-1);
expect(creds.indexOf(credentialsToken)).toBeGreaterThan(-1);
});
@ -330,8 +324,8 @@ describe('Setup Terraform', () => {
// downloaded CLI has been added to path
expect(core.addPath).toHaveBeenCalled();
// expect credentials are in ${HOME}.terraformrc
const creds = await fs.readFile(`${process.env.HOME}/.terraformrc`, { encoding: 'utf8' });
// expect credentials are in ${RUNNER_TEMP}/setup-terraform.tfrc
const creds = await fs.readFile(`${process.env.RUNNER_TEMP}/setup-terraform.tfrc`, { encoding: 'utf8' });
expect(creds.indexOf(credentialsHostname)).toBeGreaterThan(-1);
expect(creds.indexOf(credentialsToken)).toBeGreaterThan(-1);
});
@ -372,8 +366,8 @@ describe('Setup Terraform', () => {
// downloaded CLI has been added to path
expect(core.addPath).toHaveBeenCalled();
// expect credentials are in ${HOME}.terraformrc
const creds = await fs.readFile(`${process.env.HOME}/.terraformrc`, { encoding: 'utf8' });
// expect credentials are in ${RUNNER_TEMP}/setup-terraform.tfrc
const creds = await fs.readFile(`${process.env.RUNNER_TEMP}/setup-terraform.tfrc`, { encoding: 'utf8' });
expect(creds.indexOf(credentialsHostname)).toBeGreaterThan(-1);
expect(creds.indexOf(credentialsToken)).toBeGreaterThan(-1);
});
@ -414,8 +408,8 @@ describe('Setup Terraform', () => {
// downloaded CLI has been added to path
expect(core.addPath).toHaveBeenCalled();
// expect credentials are in ${HOME}.terraformrc
const creds = await fs.readFile(`${process.env.HOME}/.terraformrc`, { encoding: 'utf8' });
// expect credentials are in ${RUNNER_TEMP}/setup-terraform.tfrc
const creds = await fs.readFile(`${process.env.RUNNER_TEMP}/setup-terraform.tfrc`, { encoding: 'utf8' });
expect(creds.indexOf(credentialsHostname)).toBeGreaterThan(-1);
expect(creds.indexOf(credentialsToken)).toBeGreaterThan(-1);
});