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

Let credentials also use $(...) substitutions from ENV

to close #1051
(and add to test)
This commit is contained in:
Dave Conway-Jones 2016-11-16 21:45:11 +00:00
parent 6efd048fd6
commit d042169f2e
2 changed files with 19 additions and 3 deletions

View File

@ -69,6 +69,20 @@ function createNode(node,def) {
var creds = credentials.get(id); var creds = credentials.get(id);
if (creds) { if (creds) {
//console.log("Attaching credentials to ",node.id); //console.log("Attaching credentials to ",node.id);
// allow $(foo) syntax to substitute env variables for credentials also...
var EnvVarPropertyRE = /^\$\((\S+)\)$/;
var loopOver = function (obj) {
for (var o in obj) {
if (typeof obj[o] === "object" && obj[o] !== null) { loopOver(obj[o]); }
else {
var m;
if ( (m = EnvVarPropertyRE.exec(obj[o])) !== null ) {
if (process.env.hasOwnProperty(m[1])) { obj[o] = process.env[m[1]]; }
}
}
}
}
loopOver(creds);
node.credentials = creds; node.credentials = creds;
} else if (credentials.getDefinition(node.type)) { } else if (credentials.getDefinition(node.type)) {
node.credentials = {}; node.credentials = {};

View File

@ -38,8 +38,9 @@ describe("red/nodes/index", function() {
index.clearRegistry(); index.clearRegistry();
}); });
process.env.foo="bar";
var testFlows = [{"type":"test","id":"tab1","label":"Sheet 1"}]; var testFlows = [{"type":"test","id":"tab1","label":"Sheet 1"}];
var testCredentials = {"tab1":{"b":1,"c":2}}; var testCredentials = {"tab1":{"b":1, "c":"2", "d":"$(foo)"}};
var storage = { var storage = {
getFlows: function() { getFlows: function() {
return when({red:123,flows:testFlows,credentials:testCredentials}); return when({red:123,flows:testFlows,credentials:testCredentials});
@ -58,7 +59,7 @@ describe("red/nodes/index", function() {
var runtime = { var runtime = {
settings: settings, settings: settings,
storage: storage, storage: storage,
log: {debug:function(){},warn:function(){}} log: {debug:function() {}, warn:function() {}}
}; };
function TestNode(n) { function TestNode(n) {
@ -75,7 +76,8 @@ describe("red/nodes/index", function() {
index.loadFlows().then(function() { index.loadFlows().then(function() {
var testnode = new TestNode({id:'tab1',type:'test',name:'barney'}); var testnode = new TestNode({id:'tab1',type:'test',name:'barney'});
testnode.credentials.should.have.property('b',1); testnode.credentials.should.have.property('b',1);
testnode.credentials.should.have.property('c',2); testnode.credentials.should.have.property('c',"2");
testnode.credentials.should.have.property('d',"bar");
done(); done();
}).otherwise(function(err) { }).otherwise(function(err) {
done(err); done(err);