Added way to remove test agents.
Ylian Saint-Hilaire committed
Jan 12, 2021 at 12:43 UTC
fe72281dcd30008645fba1d0ffe676e3f8e5c335
2 files changed
+64
-2
db.js
+1
-1
@@ -1133,7 +1133,7 @@ module.exports.CreateDB = function (parent, func) {
1133
obj.GetAllIdsOfType = function (ids, domain, type, func) { obj.file.find({ type: type, domain: domain, _id: { $in: ids } }).toArray(function (err, docs) { func(err, performTypedRecordDecrypt(docs)); }); };
1134
obj.GetUserWithEmail = function (domain, email, func) { obj.file.find({ type: 'user', domain: domain, email: email }).toArray(function (err, docs) { func(err, performTypedRecordDecrypt(docs)); }); };
1135
obj.GetUserWithVerifiedEmail = function (domain, email, func) { obj.file.find({ type: 'user', domain: domain, email: email, emailVerified: true }).toArray(function (err, docs) { func(err, performTypedRecordDecrypt(docs)); }); };
1136
- obj.Remove = function (id, func) { obj.file.deleteOne({ _id: id }, func); };
1136
+ obj.Remove = function (id, func) { obj.file.deleteOne({ _id: id }, func); }; // TODO: May want to do bulk removes to speed up the database.
1137
obj.RemoveAll = function (func) { obj.file.deleteMany({}, { multi: true }, func); };
1138
obj.RemoveAllOfType = function (type, func) { obj.file.deleteMany({ type: type }, { multi: true }, func); };
1139
obj.InsertMany = function (data, func) { obj.file.insertMany(data, func); };
meshcentral.js
+63
-1
@@ -138,7 +138,7 @@ function CreateMeshCentralServer(config, args) {
138
try { require('./pass').hash('test', function () { }, 0); } catch (e) { console.log('Old version of node, must upgrade.'); return; } // TODO: Not sure if this test works or not.
139
140
// Check for invalid arguments
141
- var validArguments = ['_', 'user', 'port', 'aliasport', 'mpsport', 'mpsaliasport', 'redirport', 'rediraliasport', 'cert', 'mpscert', 'deletedomain', 'deletedefaultdomain', 'showall', 'showusers', 'showitem', 'listuserids', 'showusergroups', 'shownodes', 'showallmeshes', 'showmeshes', 'showevents', 'showsmbios', 'showpower', 'clearpower', 'showiplocations', 'help', 'exactports', 'xinstall', 'xuninstall', 'install', 'uninstall', 'start', 'stop', 'restart', 'debug', 'filespath', 'datapath', 'noagentupdate', 'launch', 'noserverbackup', 'mongodb', 'mongodbcol', 'wanonly', 'lanonly', 'nousers', 'mpspass', 'ciralocalfqdn', 'dbexport', 'dbexportmin', 'dbimport', 'dbmerge', 'dbfix', 'dbencryptkey', 'selfupdate', 'tlsoffload', 'userallowedip', 'userblockedip', 'swarmallowedip', 'agentallowedip', 'agentblockedip', 'fastcert', 'swarmport', 'logintoken', 'logintokenkey', 'logintokengen', 'mailtokengen', 'admin', 'unadmin', 'sessionkey', 'sessiontime', 'minify', 'minifycore', 'dblistconfigfiles', 'dbshowconfigfile', 'dbpushconfigfiles', 'dbpullconfigfiles', 'dbdeleteconfigfiles', 'vaultpushconfigfiles', 'vaultpullconfigfiles', 'vaultdeleteconfigfiles', 'configkey', 'loadconfigfromdb', 'npmpath', 'serverid', 'recordencryptionrecode', 'vault', 'token', 'unsealkey', 'name', 'log', 'dbstats', 'translate', 'createaccount', 'resetaccount', 'pass', 'adminaccount', 'removeaccount', 'domain', 'email', 'configfile', 'maintenancemode', 'nedbtodb'];
141
+ var validArguments = ['_', 'user', 'port', 'aliasport', 'mpsport', 'mpsaliasport', 'redirport', 'rediraliasport', 'cert', 'mpscert', 'deletedomain', 'deletedefaultdomain', 'showall', 'showusers', 'showitem', 'listuserids', 'showusergroups', 'shownodes', 'showallmeshes', 'showmeshes', 'showevents', 'showsmbios', 'showpower', 'clearpower', 'showiplocations', 'help', 'exactports', 'xinstall', 'xuninstall', 'install', 'uninstall', 'start', 'stop', 'restart', 'debug', 'filespath', 'datapath', 'noagentupdate', 'launch', 'noserverbackup', 'mongodb', 'mongodbcol', 'wanonly', 'lanonly', 'nousers', 'mpspass', 'ciralocalfqdn', 'dbexport', 'dbexportmin', 'dbimport', 'dbmerge', 'dbfix', 'dbencryptkey', 'selfupdate', 'tlsoffload', 'userallowedip', 'userblockedip', 'swarmallowedip', 'agentallowedip', 'agentblockedip', 'fastcert', 'swarmport', 'logintoken', 'logintokenkey', 'logintokengen', 'mailtokengen', 'admin', 'unadmin', 'sessionkey', 'sessiontime', 'minify', 'minifycore', 'dblistconfigfiles', 'dbshowconfigfile', 'dbpushconfigfiles', 'dbpullconfigfiles', 'dbdeleteconfigfiles', 'vaultpushconfigfiles', 'vaultpullconfigfiles', 'vaultdeleteconfigfiles', 'configkey', 'loadconfigfromdb', 'npmpath', 'serverid', 'recordencryptionrecode', 'vault', 'token', 'unsealkey', 'name', 'log', 'dbstats', 'translate', 'createaccount', 'resetaccount', 'pass', 'adminaccount', 'removeaccount', 'domain', 'email', 'configfile', 'maintenancemode', 'nedbtodb', 'removetestagents'];
142
for (var arg in obj.args) { obj.args[arg.toLocaleLowerCase()] = obj.args[arg]; if (validArguments.indexOf(arg.toLocaleLowerCase()) == -1) { console.log('Invalid argument "' + arg + '", use --help.'); return; } }
143
if (obj.args.mongodb == true) { console.log('Must specify: --mongodb [connectionstring] \r\nSee https://docs.mongodb.com/manual/reference/connection-string/ for MongoDB connection string.'); return; }
144
for (i in obj.config.settings) { obj.args[i] = obj.config.settings[i]; } // Place all settings into arguments, arguments have already been placed into settings so arguments take precedence.
@@ -783,6 +783,68 @@ function CreateMeshCentralServer(config, args) {
783
});
784
return;
785
}
786
+ if (obj.args.removetestagents) { // Remove all test agents from the database
787
+ db.GetAllType('node', function (err, docs) {
788
+ if ((err != null) || (docs.length == 0)) {
789
+ console.log('Unable to get any nodes from the database');
790
+ process.exit(0);
791
+ } else {
792
+ // Load all users
793
+ var allusers = {}, removeCount = 0;
794
+ obj.db.GetAllType('user', function (err, docs) {
795
+ obj.common.unEscapeAllLinksFieldName(docs);
796
+ for (i in docs) { allusers[docs[i]._id] = docs[i]; }
797
+ });
798
+
799
+ // Look at all devices
800
+ for (var i in docs) {
801
+ if ((docs[i] != null) && (docs[i].agent != null) && (docs[i].agent.id == 23)) {
802
+ // Remove this test node
803
+ var node = docs[i];
804
+
805
+ // Delete this node including network interface information, events and timeline
806
+ removeCount++;
807
+ db.Remove(node._id); // Remove node with that id
808
+ db.Remove('if' + node._id); // Remove interface information
809
+ db.Remove('nt' + node._id); // Remove notes
810
+ db.Remove('lc' + node._id); // Remove last connect time
811
+ db.Remove('si' + node._id); // Remove system information
812
+ if (db.RemoveSMBIOS) { db.RemoveSMBIOS(node._id); } // Remove SMBios data
813
+ db.RemoveAllNodeEvents(node._id); // Remove all events for this node
814
+ db.removeAllPowerEventsForNode(node._id); // Remove all power events for this node
815
+ db.Get('ra' + node._id, function (err, nodes) {
816
+ if ((nodes != null) && (nodes.length == 1)) { db.Remove('da' + nodes[0].daid); } // Remove diagnostic agent to real agent link
817
+ db.Remove('ra' + node._id); // Remove real agent to diagnostic agent link
818
+ });
819
+
820
+ // Remove any user node links
821
+ if (node.links != null) {
822
+ for (var i in node.links) {
823
+ if (i.startsWith('user/')) {
824
+ var cuser = allusers[i];
825
+ if ((cuser != null) && (cuser.links != null) && (cuser.links[node._id] != null)) {
826
+ // Remove the user link & save the user
827
+ delete cuser.links[node._id];
828
+ if (Object.keys(cuser.links).length == 0) { delete cuser.links; }
829
+ db.SetUser(cuser);
830
+ }
831
+ }
832
+ }
833
+ }
834
+
835
+ }
836
+ }
837
+ if (removeCount == 0) {
838
+ console.log("Done, no devices removed.");
839
+ process.exit(0);
840
+ } else {
841
+ console.log("Removed " + removeCount + " device(s), holding 10 seconds...");
842
+ setTimeout(function () { console.log("Done."); process.exit(0); }, 10000)
843
+ }
844
+ }
845
+ });
846
+ return;
847
+ }
848
849
// Import NeDB data into database
850
if (obj.args.nedbtodb) {