Skip to content

Commit

Permalink
filter_env to skip local env variables #4596
Browse files Browse the repository at this point in the history
  • Loading branch information
Unitech committed Apr 12, 2020
1 parent 23c1e0d commit 03f09ef
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -8,7 +8,7 @@ git:
depth: 2
os:
- linux
arch:
arch:
- amd64
- s390x
before_install:
Expand Down
9 changes: 9 additions & 0 deletions lib/API/schema.json
Expand Up @@ -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",
Expand Down
22 changes: 20 additions & 2 deletions lib/Common.js
Expand Up @@ -187,11 +187,29 @@ 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) {
if (app.filter_env == true)
return {}

if (typeof app.filter_env === 'string') {
delete envObj[app.filter_env]
return 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 ? filterEnv(process.env) : env, app.env || {}
].reduce(function(e1, e2){
return util._extend(e1, e2);
});


app.pm_cwd = cwd;
// Interpreter
try {
Expand Down
1 change: 1 addition & 0 deletions lib/binaries/CLI.js
Expand Up @@ -35,6 +35,7 @@ commander.version(pkg.version)
.option('-o --output <path>', 'specify log file for stdout')
.option('-e --error <path>', '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 <type>', 'specify log output style (raw by default, json optional)')
.option('--log-date-format <date format>', 'add custom prefix timestamp to logs')
.option('--time', 'enable time logging')
Expand Down
81 changes: 81 additions & 0 deletions 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()
})
})
})
})
1 change: 1 addition & 0 deletions test/unit.sh
Expand Up @@ -52,6 +52,7 @@ cd test/programmatic
# Abort script at first error
# set -e

runUnitTest ./filter_env.mocha.js
runUnitTest ./resurect_state.mocha.js
runUnitTest ./programmatic.js
runUnitTest ./namespace.mocha.js
Expand Down

0 comments on commit 03f09ef

Please sign in to comment.