Skip to content

Commit

Permalink
Perform env var ref substitution
Browse files Browse the repository at this point in the history
  • Loading branch information
derrickreimer committed Jul 13, 2020
1 parent 9404103 commit bd54d51
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
28 changes: 27 additions & 1 deletion src/cmds/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const version = require('../../package.json').version;
const log = require('../log');
const messages = require('../messages');
const shim = require('../shim');
const env = require('process').env;
const { stripIndent } = require('common-tags');

const indent = (text, depth = 2) => {
Expand Down Expand Up @@ -168,10 +169,35 @@ exports.handler = async args => {
return;
}

// Replace environment variable $-references with the actual values
// If the environment variable is not defined, store in an array an present
// an error to the user.
let undefinedEnvRefs = [];

const rawConfigWithSecrets = rawConfig.replace(
/\$([A-Za-z0-9_]+)/gi,
(_match, variableName) => {
let value = env[variableName];
if (value) return value;
undefinedEnvRefs.push(variableName);
}
);

if (undefinedEnvRefs.length > 0) {
log.error(
`The following environment variables were referenced but are not defined: ${undefinedEnvRefs.join(
', '
)}`
);

process.exitCode = 1;
return;
}

let config;

try {
config = JSON.parse(rawConfig);
config = JSON.parse(rawConfigWithSecrets);
} catch (err) {
log.error('Configuration could not be parsed');
process.exitCode = 1;
Expand Down
16 changes: 16 additions & 0 deletions test/cmds/__snapshots__/deploy.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,19 @@ Array [
],
]
`;
exports[`substitutes all referenced environment variables 1`] = `
Array [
Array [
"✔ Deployment succeeded (xxxx-xxxx-xxxx)",
],
]
`;
exports[`throws an error if undefined env vars are referenced 1`] = `
Array [
Array [
"✕ The following environment variables were referenced but are not defined: MY_SECRET_1, API_KEY_1",
],
]
`;
33 changes: 32 additions & 1 deletion test/cmds/deploy.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const deploy = require('@statickit/deploy');
const version = require('../../package.json').version;

jest.mock('process', () => ({ env: {} }));
jest.mock('process', () => ({
env: { MY_SECRET: 'pa$$w0rd', API_KEY: '12345' }
}));
jest.mock('@statickit/deploy');
jest.mock('../../src/shim');

Expand Down Expand Up @@ -190,3 +192,32 @@ it('displays general validation errors', async () => {
await cmd.handler({ config: '{}', key: 'xxx' });
expect(console.error.mock.calls).toMatchSnapshot();
});

it('substitutes all referenced environment variables', async () => {
deploy.request.mockImplementation(params => {
expect(params.config.mySecret).toBe('pa$$w0rd');
expect(params.config.apiKey).toBe('12345');
return Promise.resolve({
status: 200,
data: { id: 'xxxx-xxxx-xxxx', shim: null }
});
});

const config = {
mySecret: '$MY_SECRET',
apiKey: '$API_KEY'
};

await cmd.handler({ config: JSON.stringify(config), key: 'xxx' });
expect(console.log.mock.calls).toMatchSnapshot();
});

it('throws an error if undefined env vars are referenced', async () => {
const config = {
mySecret: '$MY_SECRET_1',
apiKey: '$API_KEY_1'
};

await cmd.handler({ config: JSON.stringify(config), key: 'xxx' });
expect(console.error.mock.calls).toMatchSnapshot();
});

0 comments on commit bd54d51

Please sign in to comment.