diff --git a/lib/API/schema.json b/lib/API/schema.json index 51ab302be3..0a97563561 100644 --- a/lib/API/schema.json +++ b/lib/API/schema.json @@ -13,6 +13,15 @@ "name_prefix": { "type": "string" }, + "filter_env": { + "type": [ + "boolean", + "array", + "string" + ], + "docDefault": false, + "docDescription": "Enable filtering global environments" + }, "namespace": { "type": "string", "docDefault": "default", diff --git a/lib/Common.js b/lib/Common.js index 55748242a1..823d204664 100644 --- a/lib/Common.js +++ b/lib/Common.js @@ -187,8 +187,17 @@ Common.prepareAppConf = function(opts, app) { else env = process.env; - // Change to double check (dropped , {pm_cwd: cwd}) - app.env = [{}, env, app.env || {}].reduce(function(e1, e2){ + function filterEnv (envObj) { + var new_env = {}; + var allowedKeys = app.filter_env.reduce((acc, current) => + acc.filter( item => !item.includes(current)), Object.keys(envObj)) + allowedKeys.forEach( key => new_env[key] = envObj[key]); + return new_env + } + + app.env = [ + {}, app.filter_env && app.filter_env.length ? filterEnv(process.env) : env, app.env || {} + ].reduce(function(e1, e2){ return util._extend(e1, e2); }); diff --git a/lib/binaries/CLI.js b/lib/binaries/CLI.js index 9d0026c7fc..a05217a34d 100644 --- a/lib/binaries/CLI.js +++ b/lib/binaries/CLI.js @@ -35,6 +35,7 @@ commander.version(pkg.version) .option('-o --output ', 'specify log file for stdout') .option('-e --error ', 'specify log file for stderr') .option('-l --log [path]', 'specify log file which gathers both stdout and stderr') + .option('--filter_env [envs]', 'filter out outgoing global values that contain provided strings', function(v, m) { m.push(v); return m;}, []) .option('--log-type ', 'specify log output style (raw by default, json optional)') .option('--log-date-format ', 'add custom prefix timestamp to logs') .option('--time', 'enable time logging') diff --git a/test/programmatic/filter_env.mocha.js b/test/programmatic/filter_env.mocha.js new file mode 100644 index 0000000000..a88cf1c27d --- /dev/null +++ b/test/programmatic/filter_env.mocha.js @@ -0,0 +1,81 @@ +//#4596 + +process.chdir(__dirname) + +process.env.SHOULD_NOT_BE_THERE = 'true' + +var PM2 = require('../..') +var should = require('should') + +describe('API checks', function() { + before(function(done) { + PM2.delete('all', function() { done() }) + }) + + after(function(done) { + PM2.kill(done) + }) + + afterEach(function(done) { + PM2.delete('all', done) + }) + + it('should start app and validate presence of env var', function(done) { + PM2.start({ + script: './../fixtures/echo.js' + }, (err) => { + should(err).be.null() + PM2.list(function(err, list) { + should(err).be.null() + should(list.length).eql(1) + should.exists(list[0].pm2_env.SHOULD_NOT_BE_THERE) + done() + }) + }) + }) + + it('should start app with filtered env wth array of env to be ignored', function(done) { + PM2.start({ + script: './../fixtures/echo.js', + filter_env: ['SHOULD_NOT_BE_THERE'] + }, (err) => { + should(err).be.null() + PM2.list(function(err, list) { + should(err).be.null() + should(list.length).eql(1) + should.not.exists(list[0].pm2_env.SHOULD_NOT_BE_THERE) + done() + }) + }) + }) + + it('should start app with filtered env with string env name to be ignored', function(done) { + PM2.start({ + script: './../fixtures/echo.js', + filter_env: 'SHOULD_NOT_BE_THERE' + }, (err) => { + should(err).be.null() + PM2.list(function(err, list) { + should(err).be.null() + should(list.length).eql(1) + should.not.exists(list[0].pm2_env.SHOULD_NOT_BE_THERE) + done() + }) + }) + }) + + it('should start app with filtered env at true to drop all local env', function(done) { + PM2.start({ + script: './../fixtures/echo.js', + filter_env: true + }, (err) => { + should(err).be.null() + PM2.list(function(err, list) { + should(err).be.null() + should(list.length).eql(1) + should.not.exists(list[0].pm2_env.SHOULD_NOT_BE_THERE) + done() + }) + }) + }) +})