Added database links cleanup code.

This commit is contained in:
Ylian Saint-Hilaire 2020-03-30 17:47:04 -07:00
parent 27ad66831e
commit 95e8b87509
5 changed files with 1070 additions and 350 deletions

View File

@ -504,6 +504,9 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
if (docs == null) { docs = []; } if (docs == null) { docs = []; }
var r = {}; var r = {};
for (i in docs) { for (i in docs) {
// Check device links, if a link points to an unknown user, remove it.
parent.cleanDevice(docs[i]);
// Remove any connectivity and power state information, that should not be in the database anyway. // Remove any connectivity and power state information, that should not be in the database anyway.
// TODO: Find why these are sometimes saves in the db. // TODO: Find why these are sometimes saves in the db.
if (docs[i].conn != null) { delete docs[i].conn; } if (docs[i].conn != null) { delete docs[i].conn; }
@ -747,6 +750,13 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
if (parent.parent.config.settings.maxinvalidlogin == false) { if (parent.parent.config.settings.maxinvalidlogin == false) {
r = 'Bad login filter is disabled.'; r = 'Bad login filter is disabled.';
} else { } else {
if (cmdargs['_'] == 'reset') {
// Reset bad login table
parent.badLoginTable = {};
parent.badLoginTableLastClean = 0;
r = 'Done.'
} else if (cmdargs['_'] == '') {
// Show current bad login table
if (typeof parent.parent.config.settings.maxinvalidlogin.coolofftime == 'number') { if (typeof parent.parent.config.settings.maxinvalidlogin.coolofftime == 'number') {
r = "Max is " + parent.parent.config.settings.maxinvalidlogin.count + " bad login(s) in " + parent.parent.config.settings.maxinvalidlogin.time + " minute(s), " + parent.parent.config.settings.maxinvalidlogin.coolofftime + " minute(s) cooloff.\r\n"; r = "Max is " + parent.parent.config.settings.maxinvalidlogin.count + " bad login(s) in " + parent.parent.config.settings.maxinvalidlogin.time + " minute(s), " + parent.parent.config.settings.maxinvalidlogin.coolofftime + " minute(s) cooloff.\r\n";
} else { } else {
@ -767,6 +777,9 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
} }
} }
if (badLoginCount == 0) { r += 'No bad logins.'; } if (badLoginCount == 0) { r += 'No bad logins.'; }
} else {
r = 'Usage: badlogin [reset]';
}
} }
break; break;
} }
@ -1305,7 +1318,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
// Remove the link and save the node to the database // Remove the link and save the node to the database
delete node.links[deluser._id]; delete node.links[deluser._id];
if (Object.keys(node.links).length == 0) { delete node.links; } if (Object.keys(node.links).length == 0) { delete node.links; }
db.Set(node); db.Set(parent.cleanDevice(node));
// Event the node change // Event the node change
var event = { etype: 'node', userid: user._id, username: user.name, action: 'changenode', nodeid: node._id, domain: domain.id, msg: (command.rights == 0) ? ('Removed user device rights for ' + node.name) : ('Changed user device rights for ' + node.name), node: parent.CloneSafeNode(node) } var event = { etype: 'node', userid: user._id, username: user.name, action: 'changenode', nodeid: node._id, domain: domain.id, msg: (command.rights == 0) ? ('Removed user device rights for ' + node.name) : ('Changed user device rights for ' + node.name), node: parent.CloneSafeNode(node) }
@ -2518,7 +2531,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
// Save the device // Save the device
if (nodeChanged == true) { if (nodeChanged == true) {
// Save the node to the database // Save the node to the database
db.Set(node); db.Set(parent.cleanDevice(node));
// Event the node change // Event the node change
var event = { etype: 'node', userid: user._id, username: user.name, action: 'changenode', nodeid: node._id, domain: domain.id, msg: (command.rights == 0) ? ('Removed user device rights for ' + node.name) : ('Changed user device rights for ' + node.name), node: parent.CloneSafeNode(node) } var event = { etype: 'node', userid: user._id, username: user.name, action: 'changenode', nodeid: node._id, domain: domain.id, msg: (command.rights == 0) ? ('Removed user device rights for ' + node.name) : ('Changed user device rights for ' + node.name), node: parent.CloneSafeNode(node) }
@ -2720,7 +2733,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
// Perform the switch, start by saving the node with the new meshid. // Perform the switch, start by saving the node with the new meshid.
const oldMeshId = node.meshid; const oldMeshId = node.meshid;
node.meshid = command.meshid; node.meshid = command.meshid;
db.Set(node); db.Set(parent.cleanDevice(node));
// If the device is connected on this server, switch it now. // If the device is connected on this server, switch it now.
var agentSession = parent.wsagents[node._id]; var agentSession = parent.wsagents[node._id];
@ -2997,7 +3010,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
if (change == 1) { if (change == 1) {
// Save the node // Save the node
db.Set(node); db.Set(parent.cleanDevice(node));
// Event the node change. Only do this if the database will not do it. // Event the node change. Only do this if the database will not do it.
event.msg = 'Changed device ' + node.name + ' from group ' + mesh.name + ': ' + changes.join(', '); event.msg = 'Changed device ' + node.name + ' from group ' + mesh.name + ': ' + changes.join(', ');

View File

@ -1112,7 +1112,7 @@
for (var i in nodes) { if (nodes[i]._id == message.event.nodeid) { index = i; break; } } for (var i in nodes) { if (nodes[i]._id == message.event.nodeid) { index = i; break; } }
if (index != -1) { if (index != -1) {
var node = nodes[index]; var node = nodes[index];
if (meshes[message.event.newMeshId] == null) { if ((meshes[message.event.newMeshId] == null) && ((userinfo.links == null) || (userinfo.links[node._id] == null))) {
// We don't see the new mesh, remove this device // We don't see the new mesh, remove this device
// TODO: Correctly disconnect from this node (Desktop/Terminal/Files...) // TODO: Correctly disconnect from this node (Desktop/Terminal/Files...)
@ -1764,7 +1764,7 @@
r += '</span><span id=MxMESH style=cursor:pointer onclick=goForward("' + nodes[i].meshid + '")>' + EscapeHtml(meshes[nodes[i].meshid].name) + '</span>' + extra + '<span id=DevxHeader' + deviceHeaderId + ' style=color:lightgray></span></div>'; r += '</span><span id=MxMESH style=cursor:pointer onclick=goForward("' + nodes[i].meshid + '")>' + EscapeHtml(meshes[nodes[i].meshid].name) + '</span>' + extra + '<span id=DevxHeader' + deviceHeaderId + ' style=color:lightgray></span></div>';
current = nodes[i].meshid; current = nodes[i].meshid;
} else { } else {
r += '</span><span id=MxMESH style=cursor:pointer><i>' + "Indivitual Devices" + '</i></span>' + extra + '<span id=DevxHeader' + deviceHeaderId + ' style=color:lightgray></span></div>'; r += '</span><span id=MxMESH><i>' + "Indivitual Devices" + '</i></span><span id=DevxHeader' + deviceHeaderId + ' style=color:lightgray></span></div>';
current = '*'; current = '*';
} }

View File

@ -2465,7 +2465,7 @@
for (var i in nodes) { if (nodes[i]._id == message.event.nodeid) { index = i; break; } } for (var i in nodes) { if (nodes[i]._id == message.event.nodeid) { index = i; break; } }
if (index != -1) { if (index != -1) {
var node = nodes[index]; var node = nodes[index];
if (meshes[message.event.newMeshId] == null) { if ((meshes[message.event.newMeshId] == null) && ((userinfo.links == null) || (userinfo.links[node._id] == null))) {
// We don't see the new mesh, remove this device // We don't see the new mesh, remove this device
// TODO: Correctly disconnect from this node (Desktop/Terminal/Files...) // TODO: Correctly disconnect from this node (Desktop/Terminal/Files...)
@ -2994,7 +2994,7 @@
r += '<span id=MxMESH cmenu=meshContextMenu tabindex=0 style=cursor:pointer onclick=gotoMesh("' + node.meshid + '") onkeypress="if (event.key==\'Enter\') gotoMesh(\'' + node.meshid + '\')">' + EscapeHtml(meshes[node.meshid].name) + '</span>' + getMeshActions(mesh2, meshrights) + '</div>'; r += '<span id=MxMESH cmenu=meshContextMenu tabindex=0 style=cursor:pointer onclick=gotoMesh("' + node.meshid + '") onkeypress="if (event.key==\'Enter\') gotoMesh(\'' + node.meshid + '\')">' + EscapeHtml(meshes[node.meshid].name) + '</span>' + getMeshActions(mesh2, meshrights) + '</div>';
current = node.meshid; current = node.meshid;
} else { } else {
r += '<span id=MxMESH cmenu=meshContextMenu tabindex=0 style=cursor:pointer><i>' + "Indivitual Devices" + '</i></span></div>'; r += '<span id=MxMESH><i>' + "Indivitual Devices" + '</i></span></div>';
current = '*'; current = '*';
} }
if (view == 2) { r += '</div>'; } if (view == 2) { r += '</div>'; }

View File

@ -216,6 +216,8 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
} }
// Fetch all device groups (meshes) from the database, keep this in memory // Fetch all device groups (meshes) from the database, keep this in memory
// As we load things in memory, we will also be doing some cleaning up.
// We will not save any clean up in the database right now, instead it will be saved next time there is a change.
obj.db.GetAllType('mesh', function (err, docs) { obj.db.GetAllType('mesh', function (err, docs) {
obj.common.unEscapeAllLinksFieldName(docs); obj.common.unEscapeAllLinksFieldName(docs);
for (var i in docs) { obj.meshes[docs[i]._id] = docs[i]; } // Get all meshes, including deleted ones. for (var i in docs) { obj.meshes[docs[i]._id] = docs[i]; } // Get all meshes, including deleted ones.
@ -223,14 +225,63 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
// Fetch all user groups from the database, keep this in memory // Fetch all user groups from the database, keep this in memory
obj.db.GetAllType('ugrp', function (err, docs) { obj.db.GetAllType('ugrp', function (err, docs) {
obj.common.unEscapeAllLinksFieldName(docs); obj.common.unEscapeAllLinksFieldName(docs);
for (var i in docs) { obj.userGroups[docs[i]._id] = docs[i]; } // Get all user groups
// We loaded the users, device groups and suer group state, start the server // Perform user group link cleanup
for (var i in docs) {
const ugrp = docs[i];
if (ugrp.links != null) {
for (var j in ugrp.links) {
if (j.startsWith('user/') && (obj.users[j] == null)) { delete ugrp.links[j]; } // User group has a link to a user that does not exist
else if (j.startsWith('mesh/') && ((obj.meshes[j] == null) || (obj.meshes[j].deleted != null))) { delete ugrp.links[j]; } // User has a link to a device group that does not exist
}
}
obj.userGroups[docs[i]._id] = docs[i]; // Get all user groups
}
// Perform device group link cleanup
for (var i in obj.meshes) {
const mesh = obj.meshes[i];
if (mesh.links != null) {
for (var j in mesh.links) {
if (j.startsWith('ugrp/') && (obj.userGroups[j] == null)) { delete mesh.links[j]; } // Device group has a link to a user group that does not exist
else if (j.startsWith('user/') && (obj.users[j] == null)) { delete mesh.links[j]; } // Device group has a link to a user that does not exist
}
}
}
// Perform user link cleanup
for (var i in obj.users) {
const user = obj.users[i];
if (user.links != null) {
for (var j in user.links) {
if (j.startsWith('ugrp/') && (obj.userGroups[j] == null)) { delete user.links[j]; } // User has a link to a user group that does not exist
else if (j.startsWith('mesh/') && ((obj.meshes[j] == null) || (obj.meshes[j].deleted != null))) { delete user.links[j]; } // User has a link to a device group that does not exist
//else if (j.startsWith('node/') && (obj.nodes[j] == null)) { delete user.links[j]; } // TODO
}
//if (Object.keys(user.links).length == 0) { delete user.links; }
}
}
// We loaded the users, device groups and user group state, start the server
serverStart(); serverStart();
}); });
}); });
}); });
// Clean up a device, used before saving it in the database
obj.cleanDevice = function (device) {
// Check device links, if a link points to an unknown user, remove it.
if (device.links != null) {
for (var j in device.links) {
if (obj.users[j] == null) {
delete device.links[j];
if (Object.keys(device.links).length == 0) { delete device.links; }
}
}
}
return device;
}
// Return statistics about this web server // Return statistics about this web server
obj.getStats = function () { obj.getStats = function () {
return { return {
@ -1372,7 +1423,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
// Remove the link and save the node to the database // Remove the link and save the node to the database
delete node.links[deluser._id]; delete node.links[deluser._id];
if (Object.keys(node.links).length == 0) { delete node.links; } if (Object.keys(node.links).length == 0) { delete node.links; }
db.Set(node); db.Set(obj.cleanDevice(node));
// Event the node change // Event the node change
var event = { etype: 'node', userid: user._id, username: user.name, action: 'changenode', nodeid: node._id, domain: domain.id, msg: (command.rights == 0) ? ('Removed user device rights for ' + node.name) : ('Changed user device rights for ' + node.name), node: parent.CloneSafeNode(node) } var event = { etype: 'node', userid: user._id, username: user.name, action: 'changenode', nodeid: node._id, domain: domain.id, msg: (command.rights == 0) ? ('Removed user device rights for ' + node.name) : ('Changed user device rights for ' + node.name), node: parent.CloneSafeNode(node) }
@ -3081,7 +3132,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
// Update the database // Update the database
var oldname = node.host; var oldname = node.host;
node.host = amthost; node.host = amthost;
obj.db.Set(node); obj.db.Set(obj.cleanDevice(node));
// Event the node change // Event the node change
var event = { etype: 'node', action: 'changenode', nodeid: node._id, domain: domain.id, msg: 'Intel(R) AMT host change ' + node.name + ' from group ' + mesh.name + ': ' + oldname + ' to ' + amthost }; var event = { etype: 'node', action: 'changenode', nodeid: node._id, domain: domain.id, msg: 'Intel(R) AMT host change ' + node.name + ' from group ' + mesh.name + ': ' + oldname + ' to ' + amthost };

1302
x.txt

File diff suppressed because it is too large Load Diff