2018-04-15 12:18:10 +02:00
|
|
|
/**
|
|
|
|
* Copyright JS Foundation and other contributors, http://js.foundation
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
**/
|
|
|
|
|
|
|
|
/**
|
2018-12-01 00:01:09 +01:00
|
|
|
* @mixin @node-red/runtime_settings
|
2018-04-15 12:18:10 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
var util = require("util");
|
|
|
|
var runtime;
|
|
|
|
|
|
|
|
function extend(target, source) {
|
|
|
|
var keys = Object.keys(source);
|
|
|
|
var i = keys.length;
|
|
|
|
while(i--) {
|
|
|
|
var value = source[keys[i]]
|
|
|
|
var type = typeof value;
|
|
|
|
if (type === 'string' || type === 'number' || type === 'boolean' || Array.isArray(value)) {
|
|
|
|
target[keys[i]] = value;
|
|
|
|
} else if (value === null) {
|
|
|
|
if (target.hasOwnProperty(keys[i])) {
|
|
|
|
delete target[keys[i]];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Object
|
|
|
|
if (target.hasOwnProperty(keys[i])) {
|
2021-05-25 22:02:14 +02:00
|
|
|
// Target property exists. Need to handle the case
|
|
|
|
// where the existing property is a string/number/boolean
|
|
|
|
// and is being replaced wholesale.
|
|
|
|
var targetType = typeof target[keys[i]];
|
|
|
|
if (targetType === 'string' || targetType === 'number' || targetType === 'boolean') {
|
|
|
|
target[keys[i]] = value;
|
|
|
|
} else {
|
|
|
|
target[keys[i]] = extend(target[keys[i]],value);
|
|
|
|
}
|
2018-04-15 12:18:10 +02:00
|
|
|
} else {
|
|
|
|
target[keys[i]] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
2018-04-20 21:50:20 +02:00
|
|
|
function getSSHKeyUsername(userObj) {
|
2018-04-15 12:18:10 +02:00
|
|
|
var username = '__default';
|
2018-10-26 22:21:59 +02:00
|
|
|
if ( userObj && userObj.username ) {
|
|
|
|
username = userObj.username;
|
2018-04-15 12:18:10 +02:00
|
|
|
}
|
|
|
|
return username;
|
|
|
|
}
|
|
|
|
var api = module.exports = {
|
|
|
|
init: function(_runtime) {
|
|
|
|
runtime = _runtime;
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Gets the runtime settings object
|
|
|
|
* @param {Object} opts
|
|
|
|
* @param {User} opts.user - the user calling the api
|
2019-08-09 17:56:11 +02:00
|
|
|
* @param {Object} opts.req - the request to log (optional)
|
2018-04-15 12:18:10 +02:00
|
|
|
* @return {Promise<Object>} - the runtime settings
|
2018-12-01 00:01:09 +01:00
|
|
|
* @memberof @node-red/runtime_settings
|
2018-04-15 12:18:10 +02:00
|
|
|
*/
|
2020-11-30 17:58:05 +01:00
|
|
|
getRuntimeSettings: async function(opts) {
|
|
|
|
var safeSettings = {
|
|
|
|
httpNodeRoot: runtime.settings.httpNodeRoot||"/",
|
|
|
|
version: runtime.settings.version
|
|
|
|
}
|
|
|
|
if (opts.user) {
|
|
|
|
safeSettings.user = {}
|
|
|
|
var props = ["anonymous","username","image","permissions"];
|
|
|
|
props.forEach(prop => {
|
|
|
|
if (opts.user.hasOwnProperty(prop)) {
|
|
|
|
safeSettings.user[prop] = opts.user[prop];
|
2018-04-20 21:50:20 +02:00
|
|
|
}
|
2020-11-30 17:58:05 +01:00
|
|
|
})
|
|
|
|
}
|
2018-07-30 00:47:19 +02:00
|
|
|
|
2020-11-30 17:58:05 +01:00
|
|
|
if (!runtime.settings.disableEditor) {
|
|
|
|
safeSettings.context = runtime.nodes.listContextStores();
|
2021-04-29 17:53:59 +02:00
|
|
|
if (runtime.settings.editorTheme && runtime.settings.editorTheme.codeEditor) {
|
|
|
|
safeSettings.codeEditor = runtime.settings.editorTheme.codeEditor || {};
|
|
|
|
safeSettings.codeEditor.lib = safeSettings.codeEditor.lib || "ace";
|
|
|
|
safeSettings.codeEditor.options = safeSettings.codeEditor.options || {};
|
|
|
|
}
|
2020-12-17 15:35:01 +01:00
|
|
|
safeSettings.libraries = runtime.library.getLibraries();
|
2020-11-30 17:58:05 +01:00
|
|
|
if (util.isArray(runtime.settings.paletteCategories)) {
|
|
|
|
safeSettings.paletteCategories = runtime.settings.paletteCategories;
|
|
|
|
}
|
2018-04-15 12:18:10 +02:00
|
|
|
|
2020-11-30 17:58:05 +01:00
|
|
|
if (runtime.settings.flowFilePretty) {
|
|
|
|
safeSettings.flowFilePretty = runtime.settings.flowFilePretty;
|
|
|
|
}
|
2018-04-15 12:18:10 +02:00
|
|
|
|
2021-01-06 18:36:59 +01:00
|
|
|
if (runtime.settings.editorTheme && runtime.settings.editorTheme.palette) {
|
|
|
|
if (runtime.settings.editorTheme.palette.upload === false || runtime.settings.editorTheme.palette.editable === false) {
|
|
|
|
safeSettings.externalModules = {palette: { } }
|
|
|
|
}
|
|
|
|
if (runtime.settings.editorTheme.palette.upload === false) {
|
|
|
|
safeSettings.externalModules.palette.allowUpload = false;
|
|
|
|
}
|
|
|
|
if (runtime.settings.editorTheme.palette.editable === false) {
|
|
|
|
safeSettings.externalModules.palette.allowInstall = false;
|
|
|
|
safeSettings.externalModules.palette.allowUpload = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-24 00:29:07 +01:00
|
|
|
if (runtime.settings.externalModules) {
|
2021-01-06 18:36:59 +01:00
|
|
|
safeSettings.externalModules = extend(safeSettings.externalModules||{},runtime.settings.externalModules);
|
2020-12-24 00:29:07 +01:00
|
|
|
}
|
|
|
|
|
2020-12-02 14:14:39 +01:00
|
|
|
if (!runtime.nodes.installerEnabled()) {
|
2020-12-24 00:29:07 +01:00
|
|
|
safeSettings.externalModules = safeSettings.externalModules || {};
|
|
|
|
safeSettings.externalModules.palette = safeSettings.externalModules.palette || {};
|
|
|
|
safeSettings.externalModules.palette.allowInstall = false;
|
|
|
|
safeSettings.externalModules.palette.allowUpload = false;
|
2020-11-30 17:58:05 +01:00
|
|
|
}
|
|
|
|
if (runtime.storage.projects) {
|
|
|
|
var activeProject = runtime.storage.projects.getActiveProject();
|
|
|
|
if (activeProject) {
|
|
|
|
safeSettings.project = activeProject;
|
|
|
|
} else if (runtime.storage.projects.flowFileExists()) {
|
|
|
|
safeSettings.files = {
|
|
|
|
flow: runtime.storage.projects.getFlowFilename(),
|
|
|
|
credentials: runtime.storage.projects.getCredentialsFilename()
|
2018-04-20 21:50:20 +02:00
|
|
|
}
|
2020-07-31 16:26:21 +02:00
|
|
|
}
|
2020-11-30 17:58:05 +01:00
|
|
|
safeSettings.git = {
|
|
|
|
globalUser: runtime.storage.projects.getGlobalGitUser()
|
|
|
|
}
|
|
|
|
}
|
2018-04-15 12:18:10 +02:00
|
|
|
|
2020-11-30 17:58:05 +01:00
|
|
|
safeSettings.flowEncryptionType = runtime.nodes.getCredentialKeyType();
|
|
|
|
runtime.settings.exportNodeSettings(safeSettings);
|
2021-02-19 12:59:49 +01:00
|
|
|
runtime.plugins.exportPluginSettings(safeSettings);
|
2020-11-30 17:58:05 +01:00
|
|
|
}
|
2018-04-15 12:18:10 +02:00
|
|
|
|
2020-11-30 17:58:05 +01:00
|
|
|
return safeSettings;
|
2018-04-15 12:18:10 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets an individual user's settings object
|
|
|
|
* @param {Object} opts
|
|
|
|
* @param {User} opts.user - the user calling the api
|
2019-08-09 17:56:11 +02:00
|
|
|
* @param {Object} opts.req - the request to log (optional)
|
2018-04-15 12:18:10 +02:00
|
|
|
* @return {Promise<Object>} - the user settings
|
2018-12-01 00:01:09 +01:00
|
|
|
* @memberof @node-red/runtime_settings
|
2018-04-15 12:18:10 +02:00
|
|
|
*/
|
2020-11-30 17:58:05 +01:00
|
|
|
getUserSettings: async function(opts) {
|
2018-04-15 12:18:10 +02:00
|
|
|
var username;
|
|
|
|
if (!opts.user || opts.user.anonymous) {
|
|
|
|
username = '_';
|
|
|
|
} else {
|
|
|
|
username = opts.user.username;
|
|
|
|
}
|
2020-11-30 17:58:05 +01:00
|
|
|
return runtime.settings.getUserSettings(username)||{};
|
2018-04-15 12:18:10 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates an individual user's settings object.
|
|
|
|
* @param {Object} opts
|
|
|
|
* @param {User} opts.user - the user calling the api
|
|
|
|
* @param {Object} opts.settings - the updates to the user settings
|
2019-08-09 17:56:11 +02:00
|
|
|
* @param {Object} opts.req - the request to log (optional)
|
2018-04-15 12:18:10 +02:00
|
|
|
* @return {Promise<Object>} - the user settings
|
2018-12-01 00:01:09 +01:00
|
|
|
* @memberof @node-red/runtime_settings
|
2018-04-15 12:18:10 +02:00
|
|
|
*/
|
2020-11-30 17:58:05 +01:00
|
|
|
updateUserSettings: async function(opts) {
|
2018-04-15 12:18:10 +02:00
|
|
|
var username;
|
|
|
|
if (!opts.user || opts.user.anonymous) {
|
|
|
|
username = '_';
|
|
|
|
} else {
|
|
|
|
username = opts.user.username;
|
|
|
|
}
|
2020-11-30 17:58:05 +01:00
|
|
|
var currentSettings = runtime.settings.getUserSettings(username)||{};
|
|
|
|
currentSettings = extend(currentSettings, opts.settings);
|
|
|
|
try {
|
|
|
|
return runtime.settings.setUserSettings(username, currentSettings).then(function() {
|
|
|
|
runtime.log.audit({event: "settings.update",username:username}, opts.req);
|
|
|
|
return;
|
|
|
|
}).catch(function(err) {
|
2019-08-09 17:56:11 +02:00
|
|
|
runtime.log.audit({event: "settings.update",username:username,error:err.code||"unexpected_error",message:err.toString()}, opts.req);
|
2018-04-15 12:18:10 +02:00
|
|
|
err.status = 400;
|
2020-11-30 17:58:05 +01:00
|
|
|
throw err;
|
|
|
|
});
|
|
|
|
} catch(err) {
|
|
|
|
runtime.log.warn(runtime.log._("settings.user-not-available",{message:runtime.log._("settings.not-available")}));
|
|
|
|
runtime.log.audit({event: "settings.update",username:username,error:err.code||"unexpected_error",message:err.toString()}, opts.req);
|
|
|
|
err.status = 400;
|
|
|
|
throw err;
|
|
|
|
}
|
2018-04-15 12:18:10 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a list of a user's ssh keys
|
|
|
|
* @param {Object} opts
|
|
|
|
* @param {User} opts.user - the user calling the api
|
2019-08-09 17:56:11 +02:00
|
|
|
* @param {Object} opts.req - the request to log (optional)
|
2018-04-15 12:18:10 +02:00
|
|
|
* @return {Promise<Object>} - the user's ssh keys
|
2018-12-01 00:01:09 +01:00
|
|
|
* @memberof @node-red/runtime_settings
|
2018-04-15 12:18:10 +02:00
|
|
|
*/
|
2020-11-30 17:58:05 +01:00
|
|
|
getUserKeys: async function(opts) {
|
|
|
|
var username = getSSHKeyUsername(opts.user);
|
|
|
|
return runtime.storage.projects.ssh.listSSHKeys(username).catch(function(err) {
|
|
|
|
err.status = 400;
|
|
|
|
throw err;
|
|
|
|
return reject(err);
|
2018-04-15 12:18:10 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a user's ssh public key
|
|
|
|
* @param {Object} opts
|
|
|
|
* @param {User} opts.user - the user calling the api
|
|
|
|
* @param {User} opts.id - the id of the key to return
|
2019-08-09 17:56:11 +02:00
|
|
|
* @param {Object} opts.req - the request to log (optional)
|
2018-04-15 12:18:10 +02:00
|
|
|
* @return {Promise<String>} - the user's ssh public key
|
2018-12-01 00:01:09 +01:00
|
|
|
* @memberof @node-red/runtime_settings
|
2018-04-15 12:18:10 +02:00
|
|
|
*/
|
2020-11-30 17:58:05 +01:00
|
|
|
getUserKey: async function(opts) {
|
|
|
|
var username = getSSHKeyUsername(opts.user);
|
|
|
|
// console.log('username:', username);
|
|
|
|
return runtime.storage.projects.ssh.getSSHKey(username, opts.id).then(function(data) {
|
|
|
|
if (data) {
|
|
|
|
return data;
|
|
|
|
} else {
|
|
|
|
var err = new Error("Key not found");
|
|
|
|
err.code = "not_found";
|
|
|
|
err.status = 404;
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}).catch(function(err) {
|
|
|
|
if (!err.status) {
|
2018-04-15 12:18:10 +02:00
|
|
|
err.status = 400;
|
2020-11-30 17:58:05 +01:00
|
|
|
}
|
|
|
|
throw err;
|
2018-04-15 12:18:10 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a new ssh key pair
|
|
|
|
* @param {Object} opts
|
|
|
|
* @param {User} opts.user - the user calling the api
|
|
|
|
* @param {User} opts.name - the id of the key to return
|
|
|
|
* @param {User} opts.password - (optional) the password for the key pair
|
|
|
|
* @param {User} opts.comment - (option) a comment to associate with the key pair
|
|
|
|
* @param {User} opts.size - (optional) the size of the key. Default: 2048
|
2019-08-09 17:56:11 +02:00
|
|
|
* @param {Object} opts.req - the request to log (optional)
|
2018-04-15 12:18:10 +02:00
|
|
|
* @return {Promise<String>} - the id of the generated key
|
2018-12-01 00:01:09 +01:00
|
|
|
* @memberof @node-red/runtime_settings
|
2018-04-15 12:18:10 +02:00
|
|
|
*/
|
2020-11-30 17:58:05 +01:00
|
|
|
generateUserKey: async function(opts) {
|
|
|
|
var username = getSSHKeyUsername(opts.user);
|
|
|
|
return runtime.storage.projects.ssh.generateSSHKey(username, opts).catch(function(err) {
|
|
|
|
err.status = 400;
|
|
|
|
throw err;
|
2018-04-15 12:18:10 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes a user's ssh key pair
|
|
|
|
* @param {Object} opts
|
|
|
|
* @param {User} opts.user - the user calling the api
|
|
|
|
* @param {User} opts.id - the id of the key to delete
|
2019-08-09 17:56:11 +02:00
|
|
|
* @param {Object} opts.req - the request to log (optional)
|
2018-04-15 12:18:10 +02:00
|
|
|
* @return {Promise} - resolves when deleted
|
2018-12-01 00:01:09 +01:00
|
|
|
* @memberof @node-red/runtime_settings
|
2018-04-15 12:18:10 +02:00
|
|
|
*/
|
2020-11-30 17:58:05 +01:00
|
|
|
removeUserKey: async function(opts) {
|
|
|
|
var username = getSSHKeyUsername(opts.user);
|
|
|
|
return runtime.storage.projects.ssh.deleteSSHKey(username, opts.id).catch(function(err) {
|
|
|
|
err.status = 400;
|
|
|
|
throw err;
|
2018-04-15 12:18:10 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|