Ensure global-config nodes lookup cred values properly

Fixes #4396
This commit is contained in:
Nick O'Leary 2023-11-01 11:07:48 +01:00
parent ee48a2f2bf
commit 861dc0c383
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
2 changed files with 18 additions and 3 deletions

View File

@ -161,7 +161,8 @@ class Flow {
for (let i = 0; i < configNodes.length; i++) { for (let i = 0; i < configNodes.length; i++) {
const node = this.flow.configs[configNodes[i]] const node = this.flow.configs[configNodes[i]]
if (node.type === 'global-config' && node.env) { if (node.type === 'global-config' && node.env) {
const nodeEnv = await flowUtil.evaluateEnvProperties(this, node.env, credentials.get(node.id)) const globalCreds = credentials.get(node.id)?.map || {}
const nodeEnv = await flowUtil.evaluateEnvProperties(this, node.env, globalCreds)
this._env = { ...this._env, ...nodeEnv } this._env = { ...this._env, ...nodeEnv }
} }
} }

View File

@ -26,6 +26,7 @@ var flowUtils = NR_TEST_UTILS.require("@node-red/runtime/lib/flows/util");
var Flow = NR_TEST_UTILS.require("@node-red/runtime/lib/flows/Flow"); var Flow = NR_TEST_UTILS.require("@node-red/runtime/lib/flows/Flow");
var flows = NR_TEST_UTILS.require("@node-red/runtime/lib/flows"); var flows = NR_TEST_UTILS.require("@node-red/runtime/lib/flows");
var Node = NR_TEST_UTILS.require("@node-red/runtime/lib/nodes/Node"); var Node = NR_TEST_UTILS.require("@node-red/runtime/lib/nodes/Node");
var credentials = NR_TEST_UTILS.require("@node-red/runtime/lib/nodes/credentials");
var hooks = NR_TEST_UTILS.require("@node-red/util/lib/hooks"); var hooks = NR_TEST_UTILS.require("@node-red/util/lib/hooks");
var typeRegistry = NR_TEST_UTILS.require("@node-red/registry"); var typeRegistry = NR_TEST_UTILS.require("@node-red/registry");
@ -61,6 +62,7 @@ describe('Flow', function() {
this.scope = n.scope; this.scope = n.scope;
var node = this; var node = this;
this.foo = n.foo; this.foo = n.foo;
this.bar = n.bar;
this.handled = 0; this.handled = 0;
this.stopped = false; this.stopped = false;
currentNodes[node.id] = node; currentNodes[node.id] = node;
@ -1372,13 +1374,24 @@ describe('Flow', function() {
it("global flow can access global-config defined environment variables", async function () { it("global flow can access global-config defined environment variables", async function () {
after(function() { after(function() {
delete process.env.V0; delete process.env.V0;
credentials.get.restore()
}) })
sinon.stub(credentials,"get").callsFake(function(id) {
if (id === 'gc') {
return { map: { GC_CRED: 'gc_cred' }}
}
return null
})
const config = flowUtils.parseConfig([ const config = flowUtils.parseConfig([
{id:"gc", type:"global-config", env:[ {id:"gc", type:"global-config", env:[
{"name": "GC0", value: "3+4", type: "jsonata"} {"name": "GC0", value: "3+4", type: "jsonata"},
{"name": "GC_CRED", type: "cred"},
]}, ]},
{id:"t1",type:"tab" }, {id:"t1",type:"tab" },
{id:"1",x:10,y:10,z:"t1",type:"test",foo:"${GC0}",wires:[]}, {id:"1",x:10,y:10,z:"t1",type:"test",foo:"${GC0}", bar:"${GC_CRED}", wires:[]},
]); ]);
// Two-arg call - makes this the global flow that handles global-config nodes // Two-arg call - makes this the global flow that handles global-config nodes
const globalFlow = Flow.create({getSetting:v=>process.env[v]},config); const globalFlow = Flow.create({getSetting:v=>process.env[v]},config);
@ -1390,6 +1403,7 @@ describe('Flow', function() {
var activeNodes = flow.getActiveNodes(); var activeNodes = flow.getActiveNodes();
activeNodes["1"].foo.should.equal(7); activeNodes["1"].foo.should.equal(7);
activeNodes["1"].bar.should.equal('gc_cred');
await flow.stop() await flow.stop()
await globalFlow.stop() await globalFlow.stop()