Skip to content

Commit d042169

Browse files
author
Dave Conway-Jones
committed
Let credentials also use $(...) substitutions from ENV
to close node-red#1051 (and add to test)
1 parent 6efd048 commit d042169

2 files changed

Lines changed: 19 additions & 3 deletions

File tree

red/runtime/nodes/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,20 @@ function createNode(node,def) {
6969
var creds = credentials.get(id);
7070
if (creds) {
7171
//console.log("Attaching credentials to ",node.id);
72+
// allow $(foo) syntax to substitute env variables for credentials also...
73+
var EnvVarPropertyRE = /^\$\((\S+)\)$/;
74+
var loopOver = function (obj) {
75+
for (var o in obj) {
76+
if (typeof obj[o] === "object" && obj[o] !== null) { loopOver(obj[o]); }
77+
else {
78+
var m;
79+
if ( (m = EnvVarPropertyRE.exec(obj[o])) !== null ) {
80+
if (process.env.hasOwnProperty(m[1])) { obj[o] = process.env[m[1]]; }
81+
}
82+
}
83+
}
84+
}
85+
loopOver(creds);
7286
node.credentials = creds;
7387
} else if (credentials.getDefinition(node.type)) {
7488
node.credentials = {};

test/red/runtime/nodes/index_spec.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ describe("red/nodes/index", function() {
3838
index.clearRegistry();
3939
});
4040

41+
process.env.foo="bar";
4142
var testFlows = [{"type":"test","id":"tab1","label":"Sheet 1"}];
42-
var testCredentials = {"tab1":{"b":1,"c":2}};
43+
var testCredentials = {"tab1":{"b":1, "c":"2", "d":"$(foo)"}};
4344
var storage = {
4445
getFlows: function() {
4546
return when({red:123,flows:testFlows,credentials:testCredentials});
@@ -58,7 +59,7 @@ describe("red/nodes/index", function() {
5859
var runtime = {
5960
settings: settings,
6061
storage: storage,
61-
log: {debug:function(){},warn:function(){}}
62+
log: {debug:function() {}, warn:function() {}}
6263
};
6364

6465
function TestNode(n) {
@@ -75,7 +76,8 @@ describe("red/nodes/index", function() {
7576
index.loadFlows().then(function() {
7677
var testnode = new TestNode({id:'tab1',type:'test',name:'barney'});
7778
testnode.credentials.should.have.property('b',1);
78-
testnode.credentials.should.have.property('c',2);
79+
testnode.credentials.should.have.property('c',"2");
80+
testnode.credentials.should.have.property('d',"bar");
7981
done();
8082
}).otherwise(function(err) {
8183
done(err);

0 commit comments

Comments
 (0)