Skip to content

Commit

Permalink
fix: make eval opt in for Fn::Eval, Fn::IfEval off by default
Browse files Browse the repository at this point in the history
  • Loading branch information
nmccready committed Aug 24, 2024
1 parent 7e27d12 commit 5ec4c02
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 8 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ Options:
* `--version` print version and exit
* `--context` template full path. only utilized for stdin when the template is piped to this script
example: `cat examples/base.template | ./bin/cli.js --context examples/base.template`
* `--enable` different options / toggles: ['env'] [string] [choices: "env"]
* `--enable` different options / toggles: ['env','eval'] [string] [choices: 'env','eval','env.eval' etc...]
* `env` pre-process env vars and inject into templates as they are processed looks for $KEY or ${KEY} matches
* `-i, --inject` JSON string payload to use for template injection. (Takes precedence over process.env (if enabled) injection and will be merged on top of process.env)
* `--doLog` console log out include options in recurse step.
Expand Down Expand Up @@ -1015,6 +1015,10 @@ In summary falsy values are omitted from an object except `false` and `0`.

## Fn::Eval

Opt in to use `eval` in your templates. This is disabled by default.

`--enable eval` is required to turn on options.doEval in the include function.

```yaml
Fn::Eval:
state: [1, 2, 3]
Expand All @@ -1030,6 +1034,10 @@ Fn::Eval:

## Fn::IfEval

Opt in to use `eval` in your templates. This is disabled by default.

`--enable eval` is required to turn on options.doEval in the include function.

```yaml
Fn::IfEval:
inject:
Expand Down
16 changes: 11 additions & 5 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ const opts = yargs
},
enable: {
string: true,
desc: `enable different options: ['env']`,
choices: ['env'],
desc: `enable different options: ['env','eval'] or a combination of both via comma.`,
choices: ['', 'env', 'env,eval', 'eval,env', 'eval'], // '' hack
default: '',
},
inject: {
alias: 'i',
Expand All @@ -96,6 +97,9 @@ const opts = yargs
})
.parse();

// make enable an array
opts.enable = opts.enable.split(',');

let promise;
if (opts.path) {
let location;
Expand All @@ -105,7 +109,8 @@ if (opts.path) {
else location = `file://${path.join(process.cwd(), opts.path)}`;
promise = include({
url: location,
doEnv: opts.enable === 'env',
doEnv: opts.enable.includes('env'),
doEval: opts.enable.includes('eval'),
inject: opts.inject,
doLog: opts.doLog,
});
Expand All @@ -126,12 +131,13 @@ if (opts.path) {
? path.resolve(opts.context)
: path.join(process.cwd(), 'template.yml');

template = opts.enable === 'env' ? replaceEnv(template) : template;
template = opts.enable.includes('env') ? replaceEnv(template) : template;

return include({
template: yaml.load(template),
url: `file://${location}`,
doEnv: opts.enable === 'env',
doEnv: opts.enable.includes('env'),
doEval: opts.enable.includes('eval'),
inject: opts.inject,
doLog: opts.doLog,
}).catch((err) => console.error(err));
Expand Down
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const { isOurExplicitFunction } = require('./lib/schema');
* doEnv: opts.enable === 'env',
* inject: opts.inject,
* doLog: opts.doLog,
* doEval: opts.doEval, -- allow Fn::Eval to be used
* })
*/
module.exports = async function (options) {
Expand Down Expand Up @@ -231,7 +232,7 @@ async function recurse({ base, scope, cft, ...opts }) {
}
);
}
if (cft['Fn::Eval']) {
if (cft['Fn::Eval'] && opts.doEval) {
return recurse({ base, scope, cft: cft['Fn::Eval'], ...opts }).then(function (json) {
// **WARNING** you have now enabled god mode
// eslint-disable-next-line no-unused-vars, prefer-const
Expand Down Expand Up @@ -386,7 +387,7 @@ async function recurse({ base, scope, cft, ...opts }) {
return isString ? seq.map((i) => String.fromCharCode(i)) : seq;
}

if (cft['Fn::IfEval']) {
if (cft['Fn::IfEval'] && opts.doEval) {
return recurse({ base, scope, cft: cft['Fn::IfEval'], ...opts }).then(function (json) {
// eslint-disable-next-line prefer-const
let { truthy, falsy, evalCond, inject, doLog } = json;
Expand Down
1 change: 1 addition & 0 deletions t/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const extendEnv = require('./tests/extendEnv');
return done();
}
// console.log({out: out.toString()})
out = out || '{}'; // fix for empty output to see failed test
const json = JSON.parse(out.toString());
delete json.Metadata;
assert.deepEqual(json, test.output);
Expand Down
1 change: 1 addition & 0 deletions t/include.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ tests.forEach(function (file) {
// eslint-disable-next-line n/no-path-concat
url: `file://${__dirname}/template.json`,
doEnv: !!test.doEnv || false,
doEval: !!test.doEval || false,
};
if (test.inject) {
opts.inject = test.inject;
Expand Down
4 changes: 4 additions & 0 deletions t/tests/ifeval.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module.exports = {
ifEval: [
{
name: 'truthy',
doEval: true,
template: {
'Fn::IfEval': {
inject: {
Expand All @@ -25,6 +26,7 @@ module.exports = {
},
{
name: 'falsy',
doEval: true,
template: {
'Fn::IfEval': {
inject: {
Expand All @@ -48,6 +50,7 @@ module.exports = {
},
{
name: 'no falsy',
doEval: true,
template: {
'Fn::IfEval': {
inject: {
Expand All @@ -64,6 +67,7 @@ module.exports = {
},
{
name: 'evalCond required',
doEval: true,
template: {
'Fn::IfEval': {
inject: {
Expand Down

0 comments on commit 5ec4c02

Please sign in to comment.