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

Fix handling encrypted creds on /flows api

This commit is contained in:
Nick O'Leary 2021-03-06 20:27:51 +00:00
parent 101378c625
commit 99a9e3a91b
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
2 changed files with 21 additions and 11 deletions

View File

@ -160,16 +160,22 @@ function setFlows(_config,_credentials,type,muteLog,forceStart,user) {
var credsDirty;
if (_credentials) {
credentials.clean(config);
// A full set of credentials have been provided. Use those instead
let credentialSavePromises = [];
for (let id in _credentials) {
if (_credentials.hasOwnProperty(id)) {
credentialSavePromises.push(credentials.add(id,_credentials[id]));
if (_credentials['$']) {
// this is a set of encrypted credentials - pass to load to decrypt
// the complete set
configSavePromise = credentials.load(_credentials);
} else {
credentials.clean(config);
// A full set of credentials have been provided. Use those instead
let credentialSavePromises = [];
for (let id in _credentials) {
if (_credentials.hasOwnProperty(id)) {
credentialSavePromises.push(credentials.add(id,_credentials[id]));
}
}
configSavePromise = Promise.all(credentialSavePromises);
credsDirty = true;
}
configSavePromise = Promise.all(credentialSavePromises);
credsDirty = true;
} else {
// Allow the credential store to remove anything no longer needed
credentials.clean(config);

View File

@ -33,6 +33,7 @@ describe('flows/index', function() {
var eventsOn;
var credentialsClean;
var credentialsLoad;
var credentialsAdd;
var flowCreate;
var getType;
@ -80,6 +81,7 @@ describe('flows/index', function() {
}
return Promise.resolve();
});
credentialsAdd = sinon.stub(credentials,"add", async function(id, conf){})
flowCreate = sinon.stub(Flow,"create",function(parent, global, flow) {
var id;
if (typeof flow === 'undefined') {
@ -117,6 +119,7 @@ describe('flows/index', function() {
eventsOn.restore();
credentialsClean.restore();
credentialsLoad.restore();
credentialsAdd.restore();
flowCreate.restore();
flows.stopFlows().then(done);
@ -196,9 +199,10 @@ describe('flows/index', function() {
flows.init({log:mockLog, settings:{},storage:storage});
flows.setFlows(originalConfig,credentials).then(function() {
credentialsClean.called.should.be.false();
credentialsLoad.called.should.be.true();
credentialsLoad.lastCall.args[0].should.eql(credentials);
credentialsClean.called.should.be.true();
credentialsAdd.called.should.be.true();
credentialsAdd.lastCall.args[0].should.eql("t1-1");
credentialsAdd.lastCall.args[1].should.eql({"a":1});
flows.getFlows().flows.should.eql(originalConfig);
done();
});