1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Add envVarExcludes setting to block named env vars

This commit is contained in:
Nick O'Leary 2019-03-07 22:54:20 +00:00
parent 3213c03754
commit 28e08ebaf5
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
4 changed files with 41 additions and 12 deletions

View File

@ -70,6 +70,7 @@ function init(runtime) {
typeEventRegistered = true;
}
Flow.init(runtime);
flowUtil.init(runtime);
}
function loadFlows() {
@ -676,7 +677,7 @@ const flowAPI = {
getNode: getNode,
handleError: () => false,
handleStatus: () => false,
getSetting: k => process.env[k]
getSetting: k => flowUtil.getEnvVar(k)
}

View File

@ -19,6 +19,8 @@ var Log = require("@node-red/util").log;
var subflowInstanceRE = /^subflow:(.+)$/;
var typeRegistry = require("@node-red/registry");
var envVarExcludes = {};
function diffNodes(oldNode,newNode) {
if (oldNode == null) {
return true;
@ -52,13 +54,9 @@ function mapEnvVarProperties(obj,prop,flow) {
} else if (typeof obj[prop] === 'string') {
if (obj[prop][0] === "$" && (EnvVarPropertyRE_old.test(v) || EnvVarPropertyRE.test(v)) ) {
var envVar = v.substring(2,v.length-1);
if (!flow) {
obj[prop] = process.env.hasOwnProperty(envVar)?process.env[envVar]:v;
} else {
var r = flow.getSetting(envVar);
obj[prop] = r!==undefined?r:obj[prop];
}
}
} else {
for (var p in v) {
if (v.hasOwnProperty(p)) {
@ -69,7 +67,15 @@ function mapEnvVarProperties(obj,prop,flow) {
}
module.exports = {
init: function(runtime) {
envVarExcludes = {};
if (runtime.settings.hasOwnProperty('envVarExcludes') && Array.isArray(runtime.settings.envVarExcludes)) {
runtime.settings.envVarExcludes.forEach(v => envVarExcludes[v] = true);
}
},
getEnvVar: function(k) {
return !envVarExcludes[k]?process.env[k]:undefined
},
diffNodes: diffNodes,
mapEnvVarProperties: mapEnvVarProperties,

View File

@ -50,7 +50,7 @@ describe('flows/util', function() {
var foo = {a:"$(foo1)",b:"$(foo2)",c:{d:"$(foo3)"}};
for (var p in foo) {
if (foo.hasOwnProperty(p)) {
flowUtil.mapEnvVarProperties(foo,p);
flowUtil.mapEnvVarProperties(foo,p,{getSetting: p => process.env[p]});
}
}
foo.should.eql({ a: 'bar1', b: 'bar2', c: { d: 'bar3' } } );
@ -59,7 +59,7 @@ describe('flows/util', function() {
var foo = {a:"${foo1}",b:"${foo2}",c:{d:"${foo3}"}};
for (var p in foo) {
if (foo.hasOwnProperty(p)) {
flowUtil.mapEnvVarProperties(foo,p);
flowUtil.mapEnvVarProperties(foo,p,{getSetting: p => process.env[p]});
}
}
foo.should.eql({ a: 'bar1', b: 'bar2', c: { d: 'bar3' } } );
@ -77,6 +77,27 @@ describe('flows/util', function() {
foo.should.eql({ a: '$(unknown)', b: 'FOO2', c: { d: 'FOO3' } } );
});
});
describe('#getEnvVar',function() {
before(function() {
process.env.foo1 = "bar1";
})
after(function() {
delete process.env.foo1;
})
it('returns a known env var', function() {
flowUtil.init({settings:{}});
flowUtil.getEnvVar("foo1").should.equal("bar1")
})
it('returns undefined for an unknown env var', function() {
flowUtil.init({settings:{}});
(flowUtil.getEnvVar("foo2") === undefined).should.be.true()
})
it('returns undefined for an excluded env var', function() {
flowUtil.init({settings:{envVarExcludes:['foo1']}});
(flowUtil.getEnvVar("foo1") === undefined).should.be.true()
})
});
describe('#diffNodes',function() {
it('handles a null old node', function() {

View File

@ -31,17 +31,18 @@ describe("red/nodes/index", function() {
before(function() {
sinon.stub(index,"startFlows");
process.env.NODE_RED_HOME = NR_TEST_UTILS.resolve("node-red");
process.env.foo="bar";
});
after(function() {
index.startFlows.restore();
delete process.env.NODE_RED_HOME;
delete process.env.foo;
});
afterEach(function() {
index.clearRegistry();
});
process.env.foo="bar";
var testFlows = [{"type":"test","id":"tab1","label":"Sheet 1"}];
var testCredentials = {"tab1":{"b":1, "c":"2", "d":"$(foo)"}};
var storage = {
@ -68,8 +69,8 @@ describe("red/nodes/index", function() {
};
function TestNode(n) {
this._flow = {getSetting: p => process.env[p]};
index.createNode(this, n);
var node = this;
this.on("log", function() {
// do nothing
});