From d042169f2e4c15cbdc975e62a035b44d04aa5186 Mon Sep 17 00:00:00 2001 From: Dave Conway-Jones Date: Wed, 16 Nov 2016 21:45:11 +0000 Subject: [PATCH] Let credentials also use $(...) substitutions from ENV to close #1051 (and add to test) --- red/runtime/nodes/index.js | 14 ++++++++++++++ test/red/runtime/nodes/index_spec.js | 8 +++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/red/runtime/nodes/index.js b/red/runtime/nodes/index.js index 609d2e345..8368f8bad 100644 --- a/red/runtime/nodes/index.js +++ b/red/runtime/nodes/index.js @@ -69,6 +69,20 @@ function createNode(node,def) { var creds = credentials.get(id); if (creds) { //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; } else if (credentials.getDefinition(node.type)) { node.credentials = {}; diff --git a/test/red/runtime/nodes/index_spec.js b/test/red/runtime/nodes/index_spec.js index 87cf38f02..8e34c8b95 100644 --- a/test/red/runtime/nodes/index_spec.js +++ b/test/red/runtime/nodes/index_spec.js @@ -38,8 +38,9 @@ describe("red/nodes/index", function() { index.clearRegistry(); }); + process.env.foo="bar"; 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 = { getFlows: function() { return when({red:123,flows:testFlows,credentials:testCredentials}); @@ -58,7 +59,7 @@ describe("red/nodes/index", function() { var runtime = { settings: settings, storage: storage, - log: {debug:function(){},warn:function(){}} + log: {debug:function() {}, warn:function() {}} }; function TestNode(n) { @@ -75,7 +76,8 @@ describe("red/nodes/index", function() { index.loadFlows().then(function() { var testnode = new TestNode({id:'tab1',type:'test',name:'barney'}); 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(); }).otherwise(function(err) { done(err);