Fix data loss on bad name passed --dev-plugin

If data from the database is not loaded but a shutdown is triggered then
the master server will overwrite the database with empty data, causing
unexpected data loss.  This occurs for example when the --dev-plugin
option throws.

Fix by setting the database entries to null before they are loaded so
that should an attempt be made to save them without having been loaded
first it will throw an error instead.
This commit is contained in:
Hornwitser committed 2021-03-13 21:01:53 +01:00
1 parent 78ce6fc382
commit a8e1971483
1 file changed
+12 -6
+12 -6
View File
@@ -41,8 +41,10 @@ let loadedPlugins = {};
let devMiddleware;
let clusterLogger;
let db = {
instances: new Map(),
slaves: new Map(),
instances: null,
slaves: null,
roles: null,
users: null,
};
let slaveConnections = new Map();
let controlConnections = [];
@@ -489,6 +491,14 @@ async function shutdown() {
logger.info("Saving configs");
await fs.outputFile(masterConfigPath, JSON.stringify(masterConfig.serialize(), null, 4));
if (devMiddleware) {
await new Promise((resolve, reject) => { devMiddleware.close(resolve); });
}
if (!db.slaves) {
return;
}
await saveMap(masterConfig.get("master.database_directory"), "slaves.json", db.slaves);
await saveInstances(masterConfig.get("master.database_directory"), "instances.json", db.instances);
await saveUsers(masterConfig.get("master.database_directory"), "users.json");
@@ -497,10 +507,6 @@ async function shutdown() {
stopAcceptingNewSessions = true;
if (devMiddleware) {
await new Promise((resolve, reject) => { devMiddleware.close(resolve); });
}
let disconnectTasks = [];
for (let controlConnection of controlConnections) {
controlConnection.connector.setTimeout(masterConfig.get("master.connector_shutdown_timeout"));