Added basic AceBase support (#4398)
Ylian Saint-Hilaire committed
Aug 13, 2022 at 18:32 UTC
1f239481b7d798258392b776a75306ce138f4e4c
5 files changed
+296
-9
common.js
+7
-1
@@ -150,7 +150,7 @@ module.exports.objKeysToLower = function (obj, exceptions) {
150
return obj;
151
};
152
153
-// Escape and unexcape feild names so there are no invalid characters for MongoDB
153
+// Escape and unescape feild names so there are no invalid characters for MongoDB
154
module.exports.escapeFieldName = function (name) { if ((name.indexOf('%') == -1) && (name.indexOf('.') == -1) && (name.indexOf('$') == -1)) return name; return name.split('%').join('%25').split('.').join('%2E').split('$').join('%24'); };
155
module.exports.unEscapeFieldName = function (name) { if (name.indexOf('%') == -1) return name; return name.split('%2E').join('.').split('%24').join('$').split('%25').join('%'); };
156
@@ -161,6 +161,12 @@ module.exports.unEscapeLinksFieldName = function (doc) { if (doc.links != null)
161
//module.exports.escapeAllLinksFieldName = function (docs) { for (var i in docs) { module.exports.escapeLinksFieldName(docs[i]); } return docs; };
162
module.exports.unEscapeAllLinksFieldName = function (docs) { for (var i in docs) { docs[i] = module.exports.unEscapeLinksFieldName(docs[i]); } return docs; };
163
164
+// Escape field names for aceBase
165
+var aceEscFields = ['links', 'ssh', 'rdp', 'notify'];
166
+module.exports.aceEscapeFieldNames = function (docx) { var doc = Object.assign({}, docx); for (var k in aceEscFields) { if (typeof doc[aceEscFields[k]] == 'object') { doc[aceEscFields[k]] = Object.assign({}, doc[aceEscFields[k]]); for (var i in doc[aceEscFields[k]]) { var ue = encodeURIComponent(i); if (ue !== i) { doc[aceEscFields[k]][ue] = doc[aceEscFields[k]][i]; delete doc[aceEscFields[k]][i]; } } } } return doc; };
167
+module.exports.aceUnEscapeFieldNames = function (doc) { for (var k in aceEscFields) { if (typeof doc[aceEscFields[k]] == 'object') { for (var j in doc[aceEscFields[k]]) { var ue = decodeURIComponent(j); if (ue !== j) { doc[aceEscFields[k]][ue] = doc[aceEscFields[k]][j]; delete doc[aceEscFields[k]][j]; } } } } return doc; };
168
+module.exports.aceUnEscapeAllFieldNames = function (docs) { for (var i in docs) { docs[i] = module.exports.aceUnEscapeFieldNames(docs[i]); } return docs; };
169
+
170
// Validation methods
171
module.exports.validateString = function (str, minlen, maxlen) { return ((str != null) && (typeof str == 'string') && ((minlen == null) || (str.length >= minlen)) && ((maxlen == null) || (str.length <= maxlen))); };
172
module.exports.validateInt = function (int, minval, maxval) { return ((int != null) && (typeof int == 'number') && ((minval == null) || (int >= minval)) && ((maxval == null) || (int <= maxval))); };
db.js
+264
-7
@@ -235,7 +235,10 @@ module.exports.CreateDB = function (parent, func) {
235
obj.removeDomain = function (domainName, func) {
236
var pendingCalls;
237
// Remove all events, power events and SMBIOS data from the main collection. They are all in seperate collections now.
238
- if ((obj.databaseType == 4) || (obj.databaseType == 5) || (obj.databaseType == 6)) {
238
+ if (obj.databaseType == 7) {
239
+ // AceBase
240
+
241
+ } else if ((obj.databaseType == 4) || (obj.databaseType == 5) || (obj.databaseType == 6)) {
242
// MariaDB, MySQL or PostgreSQL
243
pendingCalls = 2;
244
sqlDbQuery('DELETE FROM main WHERE domain = $1', [domainName], function () { if (--pendingCalls == 0) { func(); } });
@@ -260,7 +263,10 @@ module.exports.CreateDB = function (parent, func) {
263
// TODO: Remove all meshes that dont have any links
264
265
// Remove all events, power events and SMBIOS data from the main collection. They are all in seperate collections now.
263
- if ((obj.databaseType == 4) || (obj.databaseType == 5) || (obj.databaseType == 6)) {
266
+ if (obj.databaseType == 7) {
267
+ // AceBase
268
+
269
+ } else if ((obj.databaseType == 4) || (obj.databaseType == 5) || (obj.databaseType == 6)) {
270
// MariaDB, MySQL or PostgreSQL
271
obj.RemoveAllOfType('event', function () { });
272
obj.RemoveAllOfType('power', function () { });
@@ -371,7 +377,10 @@ module.exports.CreateDB = function (parent, func) {
377
if (meshChange) { obj.Set(docs[i]); }
378
}
379
}
374
- if (obj.databaseType == 6) {
380
+ if (obj.databaseType == 7) {
381
+ // AceBase
382
+
383
+ } else if (obj.databaseType == 6) {
384
// Postgres
385
sqlDbQuery('DELETE FROM Main WHERE ((extra != NULL) AND (extra LIKE (\'mesh/%\')) AND (extra != ANY ($1)))', [meshlist], function (err, response) { });
386
} else if ((obj.databaseType == 4) || (obj.databaseType == 5)) {
@@ -429,7 +438,10 @@ module.exports.CreateDB = function (parent, func) {
438
// Get the number of records in the database for various types, this is the slow NeDB way.
439
// WARNING: This is a terrible query for database performance. Only do this when needed. This query will look at almost every document in the database.
440
obj.getStats = function (func) {
432
- if (obj.databaseType == 6) {
441
+ if (obj.databaseType == 7) {
442
+ // AceBase
443
+ // TODO
444
+ } else if (obj.databaseType == 6) {
445
// PostgreSQL
446
// TODO
447
} else if (obj.databaseType == 5) {
@@ -641,7 +653,15 @@ module.exports.CreateDB = function (parent, func) {
653
});
654
}
655
644
- if (parent.args.mariadb || parent.args.mysql) {
656
+ if (parent.args.acebase) {
657
+ // AceBase database setup
658
+ obj.databaseType = 7;
659
+ const { AceBase } = require('acebase');
660
+ // For information on AceBase sponsor: https://github.com/appy-one/acebase/discussions/100
661
+ obj.file = new AceBase('meshcentral', { sponsor: ((typeof parent.args.acebase == 'object') && (parent.args.acebase.sponsor)), logLevel: 'error', storage: { path: parent.datapath } });
662
+ // Get all the databases ready
663
+ obj.file.ready(function () { setupFunctions(func); }); // Completed setup of AceBase
664
+ } else if (parent.args.mariadb || parent.args.mysql) {
665
var connectinArgs = (parent.args.mariadb) ? parent.args.mariadb : parent.args.mysql;
666
var dbname = (connectinArgs.database != null) ? connectinArgs.database : 'meshcentral';
667
@@ -1179,7 +1199,244 @@ module.exports.CreateDB = function (parent, func) {
1199
}
1200
1201
function setupFunctions(func) {
1182
- if (obj.databaseType == 6) {
1202
+ if (obj.databaseType == 7) {
1203
+ // Database actions on the main collection (AceBase)
1204
+ obj.Set = function (data, func) {
1205
+ data = common.escapeLinksFieldNameEx(data);
1206
+ var xdata = performTypedRecordEncrypt(data);
1207
+ obj.dbCounters.fileSet++;
1208
+ obj.file.ref('meshcentral/' + encodeURIComponent(xdata._id)).set(common.aceEscapeFieldNames(xdata)).then(function (ref) { if (func) { func(); } })
1209
+ };
1210
+ obj.Get = function (id, func) {
1211
+ obj.file.ref('meshcentral/' + encodeURIComponent(id)).get(function (snapshot) {
1212
+ if (snapshot.exists()) { func(null, performTypedRecordDecrypt([common.aceUnEscapeFieldNames(snapshot.val())])); } else { func(null, []); }
1213
+ });
1214
+ };
1215
+ obj.GetAll = function (func) {
1216
+ obj.file.query('meshcentral').get(function (snapshots) {
1217
+ const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, common.aceUnEscapeAllFieldNames(docs));
1218
+ });
1219
+ };
1220
+ obj.GetHash = function (id, func) {
1221
+ obj.file.ref('meshcentral/' + encodeURIComponent(id)).get({ include: ['hash'] }, function (snapshot) {
1222
+ if (snapshot.exists()) { func(null, snapshot.val()); } else { func(null, null); }
1223
+ });
1224
+ };
1225
+ obj.GetAllTypeNoTypeField = function (type, domain, func) {
1226
+ obj.file.query('meshcentral').take(999999).filter('type', '==', type).filter('domain', '==', domain).get({ exclude: ['type'] }, function (snapshots) {
1227
+ const docs = [];
1228
+ for (var i in snapshots) { const x = snapshots[i].val(); docs.push(x); }
1229
+ func(null, common.aceUnEscapeAllFieldNames(docs));
1230
+ });
1231
+ }
1232
+ obj.GetAllTypeNoTypeFieldMeshFiltered = function (meshes, extrasids, domain, type, id, func) {
1233
+ if (meshes.length == 0) { func(null, []); return; }
1234
+ var query = obj.file.query('meshcentral').take(999999).filter('type', '==', type).filter('domain', '==', domain);
1235
+ if (id) { query = query.filter('_id', '==', id); }
1236
+ if (extrasids == null) {
1237
+ query = query.filter('meshid', 'in', meshes);
1238
+ query.get(function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, performTypedRecordDecrypt(docs)); });
1239
+ } else {
1240
+ // TODO: This is a slow query as we did not find a filter-or-filter, so we query everything and filter manualy.
1241
+ query.get(function (snapshots) {
1242
+ const docs = [];
1243
+ for (var i in snapshots) { const x = snapshots[i].val(); if ((extrasids.indexOf(x._id) >= 0) || (meshes.indexOf(x.meshid) >= 0)) { docs.push(x); } }
1244
+ func(null, performTypedRecordDecrypt(docs));
1245
+ });
1246
+ }
1247
+ };
1248
+ obj.GetAllTypeNodeFiltered = function (nodes, domain, type, id, func) {
1249
+ var query = obj.file.query('meshcentral').take(999999).filter('type', '==', type).filter('domain', '==', domain).filter('nodeid', 'in', nodes);
1250
+ if (id) { query = query.filter('_id', '==', id); }
1251
+ query.get(function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, performTypedRecordDecrypt(docs)); });
1252
+ };
1253
+ obj.GetAllType = function (type, func) {
1254
+ obj.file.query('meshcentral').take(999999).filter('type', '==', type).get(function (snapshots) {
1255
+ const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); }
1256
+ func(null, common.aceUnEscapeAllFieldNames(performTypedRecordDecrypt(docs)));
1257
+ });
1258
+ };
1259
+ obj.GetAllIdsOfType = function (ids, domain, type, func) { obj.file.query('meshcentral').take(999999).filter('_id', 'in', ids).filter('domain', '==', domain).filter('type', '==', type).get(function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, performTypedRecordDecrypt(docs)); }); };
1260
+ obj.GetUserWithEmail = function (domain, email, func) { obj.file.query('meshcentral').take(999999).filter('type', '==', 'user').filter('domain', '==', domain).filter('email', '==', email).get({ exclude: ['type'] }, function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, performTypedRecordDecrypt(docs)); }); };
1261
+ obj.GetUserWithVerifiedEmail = function (domain, email, func) { obj.file.query('meshcentral').take(999999).filter('type', '==', 'user').filter('domain', '==', domain).filter('email', '==', email).filter('emailVerified', '==', true).get({ exclude: ['type'] }, function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, performTypedRecordDecrypt(docs)); }); };
1262
+ obj.Remove = function (id, func) { obj.file.ref('meshcentral/' + encodeURIComponent(id)).remove().then(function () { if (func) { func(); } }); };
1263
+ obj.RemoveAll = function (func) { obj.file.query('meshcentral').remove().then(function () { if (func) { func(); } }); };
1264
+ obj.RemoveAllOfType = function (type, func) { obj.file.query('meshcentral').filter('type', '==', type).remove().then(function () { if (func) { func(); } }); };
1265
+ obj.InsertMany = function (data, func) { var count = data.length; for (var i in data) { obj.file.ref('meshcentral/' + encodeURIComponent(data[i]._id)).set(common.aceEscapeFieldNames(data[i])).then(function (ref) { if (func && (--count == 0)) { func(); } }) } }; // Insert records directly, no link escaping
1266
+ obj.RemoveMeshDocuments = function (id) { obj.file.query('meshcentral').filter('meshid', '==', id).remove(); obj.file.ref('meshcentral/' + encodeURIComponent('nt' + id)).remove(); };
1267
+ obj.MakeSiteAdmin = function (username, domain) { obj.Get('user/' + domain + '/' + username, function (err, docs) { if ((err == null) && (docs.length == 1)) { docs[0].siteadmin = 0xFFFFFFFF; obj.Set(docs[0]); } }); };
1268
+ obj.DeleteDomain = function (domain, func) { obj.file.query('meshcentral').filter('domain', '==', domain).remove().then(function () { if (func) { func(); } }); };
1269
+ obj.SetUser = function (user) { if (user.subscriptions != null) { var u = Clone(user); if (u.subscriptions) { delete u.subscriptions; } obj.Set(u); } else { obj.Set(user); } };
1270
+ obj.dispose = function () { for (var x in obj) { if (obj[x].close) { obj[x].close(); } delete obj[x]; } };
1271
+ obj.getLocalAmtNodes = function (func) { obj.file.query('meshcentral').take(999999).filter('type', '==', 'node').filter('host', 'exists').filter('host', '!=', null).filter('intelamt', 'exists').get(function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, performTypedRecordDecrypt(docs)); }); };
1272
+ obj.getAmtUuidMeshNode = function (domainid, mtype, uuid, func) { obj.file.query('meshcentral').take(999999).filter('type', '==', 'node').filter('domain', '==', domainid).filter('mtype', '!=', mtype).filter('intelamt.uuid', '==', uuid).get(function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, performTypedRecordDecrypt(docs)); }); };
1273
+ obj.isMaxType = function (max, type, domainid, func) { if (max == null) { func(false); } else { obj.file.query('meshcentral').take(999999).filter('type', '==', type).filter('domain', '==', domainid).get({ snapshots: false }, function (snapshots) { func((snapshots.length > max), snapshots.length); }); } }
1274
+
1275
+ // Database actions on the events collection
1276
+ obj.GetAllEvents = function (func) { obj.file.query('events').take(999999).get(function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs); }); };
1277
+ obj.StoreEvent = function (event, func) {
1278
+ if (typeof event.account == 'object') { event = Object.assign({}, event); event.account = common.aceEscapeFieldNames(event.account); }
1279
+ obj.dbCounters.eventsSet++;
1280
+ obj.file.ref('events').push(event).then(function (userRef) { if (func) { func(); } });
1281
+ };
1282
+ obj.GetEvents = function (ids, domain, func) {
1283
+ // This request is slow since we have not found a .filter() that will take two arrays and match a single item.
1284
+ obj.file.query('events').filter('domain', '==', domain).take(999999).sort('time', false).get({ exclude: ['_id', 'domain', 'node', 'type'] }, function (snapshots) {
1285
+ const docs = [];
1286
+ for (var i in snapshots) {
1287
+ const doc = snapshots[i].val();
1288
+ if ((doc.ids == null) || (!Array.isArray(doc.ids))) continue;
1289
+ var found = false;
1290
+ for (var j in doc.ids) { if (ids.indexOf(doc.ids[j]) >= 0) { found = true; } } // Check if one of the items in both arrays matches
1291
+ if (found) { delete doc.ids; if (typeof doc.account == 'object') { doc.account = common.aceUnEscapeFieldNames(doc.account); } docs.push(doc); }
1292
+ }
1293
+ func(null, docs);
1294
+ });
1295
+ };
1296
+ obj.GetEventsWithLimit = function (ids, domain, limit, func) {
1297
+ // This request is slow since we have not found a .filter() that will take two arrays and match a single item.
1298
+ obj.file.query('events').filter('domain', '==', domain).take(limit).sort('time', false).get({ exclude: ['_id', 'domain', 'node', 'type'] }, function (snapshots) {
1299
+ const docs = [];
1300
+ for (var i in snapshots) {
1301
+ const doc = snapshots[i].val();
1302
+ if ((doc.ids == null) || (!Array.isArray(doc.ids))) continue;
1303
+ var found = false;
1304
+ for (var j in doc.ids) { if (ids.indexOf(doc.ids[j]) >= 0) { found = true; } } // Check if one of the items in both arrays matches
1305
+ if (found) { delete doc.ids; if (typeof doc.account == 'object') { doc.account = common.aceUnEscapeFieldNames(doc.account); } docs.push(doc); }
1306
+ }
1307
+ func(null, docs);
1308
+ });
1309
+ };
1310
+ obj.GetUserEvents = function (ids, domain, userid, func) {
1311
+ obj.file.query('events').take(999999).filter('domain', '==', domain).filter('userid', 'in', userid).filter('ids', 'in', ids).sort('time', false).get({ exclude: ['_id', 'domain', 'node', 'type', 'ids'] }, function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs); });
1312
+ };
1313
+ obj.GetUserEventsWithLimit = function (ids, domain, userid, limit, func) {
1314
+ obj.file.query('events').take(limit).filter('domain', '==', domain).filter('userid', 'in', userid).filter('ids', 'in', ids).sort('time', false).get({ exclude: ['_id', 'domain', 'node', 'type', 'ids'] }, function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs); });
1315
+ };
1316
+ obj.GetEventsTimeRange = function (ids, domain, msgids, start, end, func) {
1317
+ obj.file.query('events').take(999999).filter('domain', '==', domain).filter('ids', 'in', ids).filter('msgid', 'in', msgids).filter('time', 'between', [start, end]).sort('time', false).get({ exclude: ['type', '_id', 'domain', 'node'] }, function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs); });
1318
+ };
1319
+ obj.GetUserLoginEvents = function (domain, userid, func) {
1320
+ obj.file.query('events').take(999999).filter('domain', '==', domain).filter('action', 'in', ['authfail', 'login']).filter('userid', '==', userid).filter('msgArgs', 'exists').sort('time', false).get({ include: ['action', 'time', 'msgid', 'msgArgs', 'tokenName'] }, function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs); });
1321
+ };
1322
+ obj.GetNodeEventsWithLimit = function (nodeid, domain, limit, func) {
1323
+ obj.file.query('events').take(limit).filter('domain', '==', domain).filter('nodeid', '==', nodeid).sort('time', false).get({ exclude: ['type', 'etype', '_id', 'domain', 'ids', 'node', 'nodeid'] }, function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs); });
1324
+ };
1325
+ obj.GetNodeEventsSelfWithLimit = function (nodeid, domain, userid, limit, func) {
1326
+ obj.file.query('events').take(limit).filter('domain', '==', domain).filter('nodeid', '==', nodeid).filter('userid', '==', userid).sort('time', false).get({ exclude: ['type', 'etype', '_id', 'domain', 'ids', 'node', 'nodeid'] }, function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs); });
1327
+ };
1328
+ obj.RemoveAllEvents = function (domain) {
1329
+ obj.file.query('events').take(999999).filter('domain', '==', domain).remove().then(function () { if (func) { func(); } });;
1330
+ };
1331
+ obj.RemoveAllNodeEvents = function (domain, nodeid) {
1332
+ if ((domain == null) || (nodeid == null)) return;
1333
+ obj.file.query('events').take(999999).filter('domain', '==', domain).filter('nodeid', '==', nodeid).remove().then(function () { if (func) { func(); } });;
1334
+ };
1335
+ obj.RemoveAllUserEvents = function (domain, userid) {
1336
+ if ((domain == null) || (userid == null)) return;
1337
+ obj.file.query('events').take(999999).filter('domain', '==', domain).filter('userid', '==', userid).remove().then(function () { if (func) { func(); } });;
1338
+ };
1339
+ obj.GetFailedLoginCount = function (userid, domainid, lastlogin, func) {
1340
+ obj.file.query('events').take(999999).filter('domain', '==', domainid).filter('userid', '==', userid).filter('time', '>', lastlogin).sort('time', false).get({ snapshots: false }, function (snapshots) { func(null, snapshots.length); });
1341
+ }
1342
+
1343
+ // Database actions on the power collection
1344
+ obj.getAllPower = function (func) {
1345
+ obj.file.query('power').take(999999).get(function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs); });
1346
+ };
1347
+ obj.storePowerEvent = function (event, multiServer, func) {
1348
+ if (multiServer != null) { event.server = multiServer.serverid; }
1349
+ obj.file.ref('power').push(event).then(function (userRef) { if (func) { func(); } });
1350
+ };
1351
+ obj.getPowerTimeline = function (nodeid, func) {
1352
+ obj.file.query('power').take(999999).filter('nodeid', 'in', ['*', nodeid]).sort('time').get({ exclude: ['_id', 'nodeid', 's'] }, function (snapshots) {
1353
+ const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs);
1354
+ });
1355
+ };
1356
+ obj.removeAllPowerEvents = function () {
1357
+ obj.file.query('power').take(999999).remove().then(function () { if (func) { func(); } });
1358
+ };
1359
+ obj.removeAllPowerEventsForNode = function (nodeid) {
1360
+ if (nodeid == null) return;
1361
+ obj.file.query('power').take(999999).filter('nodeid', '==', nodeid).remove().then(function () { if (func) { func(); } });
1362
+ };
1363
+
1364
+ // Database actions on the SMBIOS collection
1365
+ if (obj.smbiosfile != null) {
1366
+ obj.GetAllSMBIOS = function (func) {
1367
+ obj.file.query('smbios').take(999999).get(function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs); });
1368
+ };
1369
+ obj.SetSMBIOS = function (smbios, func) {
1370
+ obj.file.ref('meshcentral/' + encodeURIComponent(smbios._id)).set(smbios).then(function (ref) { if (func) { func(); } })
1371
+ };
1372
+ obj.RemoveSMBIOS = function (id) {
1373
+ obj.file.query('smbios').filter('_id', 'in', id).take(999999).remove().then(function () { if (func) { func(); } });
1374
+ };
1375
+ obj.GetSMBIOS = function (id, func) {
1376
+ obj.file.query('smbios').filter('_id', 'in', id).take(1).get(function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs); });
1377
+ };
1378
+ }
1379
+
1380
+ // Database actions on the Server Stats collection
1381
+ obj.SetServerStats = function (data, func) {
1382
+ obj.file.ref('stats').push(data).then(function (userRef) { if (func) { func(); } });
1383
+ };
1384
+ obj.GetServerStats = function (hours, func) {
1385
+ var t = new Date();
1386
+ t.setTime(t.getTime() - (60 * 60 * 1000 * hours));
1387
+ obj.file.query('stats').take(999999).filter('time', '>', t).get({ exclude: ['_id', 'cpu'] }, function (snapshots) {
1388
+ const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs);
1389
+ });
1390
+ };
1391
+
1392
+ // Read a configuration file from the database
1393
+ obj.getConfigFile = function (path, func) { obj.Get('cfile/' + path, func); }
1394
+
1395
+ // Write a configuration file to the database
1396
+ obj.setConfigFile = function (path, data, func) { obj.Set({ _id: 'cfile/' + path, type: 'cfile', data: data.toString('base64') }, func); }
1397
+
1398
+ // List all configuration files
1399
+ obj.listConfigFiles = function (func) {
1400
+ obj.file.query('meshcentral').take(999999).filter('type', '==', 'cfile').sort('_id').get(function (snapshots) {
1401
+ const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs);
1402
+ });
1403
+ }
1404
+
1405
+ // Get all configuration files
1406
+ obj.getAllConfigFiles = function (password, func) {
1407
+ obj.file.query('meshcentral').take(999999).filter('type', '==', 'cfile').sort('_id').get(function (snapshots) {
1408
+ const docs = [];
1409
+ for (var i in snapshots) { docs.push(snapshots[i].val()); }
1410
+ var r = null;
1411
+ for (var i = 0; i < docs.length; i++) {
1412
+ var name = docs[i]._id.split('/')[1];
1413
+ var data = obj.decryptData(password, docs[i].data);
1414
+ if (data != null) { if (r == null) { r = {}; } r[name] = data; }
1415
+ }
1416
+ func(r);
1417
+ });
1418
+ }
1419
+
1420
+ // Get database information
1421
+ obj.getDbStats = function (func) {
1422
+ obj.stats = { c: 5 };
1423
+ obj.file.query('meshcentral').take(999999).get({ snapshots: false }, function (snapshots) { obj.stats.meshcentral = snapshots.length; if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } });
1424
+ obj.file.query('events').take(999999).get({ snapshots: false }, function (snapshots) { obj.stats.events = snapshots.length; if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } });
1425
+ obj.file.query('power').take(999999).get({ snapshots: false }, function (snapshots) { obj.stats.power = snapshots.length; if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } });
1426
+ obj.file.query('smbios').take(999999).get({ snapshots: false }, function (snapshots) { obj.stats.smbios = snapshots.length; if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } });
1427
+ obj.file.query('stats').take(999999).get({ snapshots: false }, function (snapshots) { obj.stats.serverstats = snapshots.length; if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } });
1428
+ }
1429
+
1430
+ // Plugin operations
1431
+ if (obj.pluginsActive) {
1432
+ obj.addPlugin = function (plugin, func) { plugin.type = 'plugin'; obj.file.ref('plugin/' + encodeURIComponent(plugin._id)).set(plugin).then(function (ref) { if (func) { func(); } }) }; // Add a plugin
1433
+ obj.getPlugins = function (func) { obj.file.query('plugin').take(999999).sort('name').get({ exclude: ['type'] }, function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs); }); }; // Get all plugins
1434
+ obj.getPlugin = function (id, func) { obj.file.query('plugin').take(999999).filter('_id', '==', id).get(function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs); }); }; // Get plugin
1435
+ obj.deletePlugin = function (id, func) { obj.file.ref('plugin/' + encodeURIComponent(id)).remove().then(function () { if (func) { func(); } }); }; // Delete plugin
1436
+ obj.setPluginStatus = function (id, status, func) { obj.file.ref('plugin/' + encodeURIComponent(id)).update(args).then(function (ref) { if (func) { func(); } }) };
1437
+ obj.updatePlugin = function (id, args, func) { delete args._id; obj.file.ref('plugin/' + encodeURIComponent(id)).set(args).then(function (ref) { if (func) { func(); } }) };
1438
+ }
1439
+ } else if (obj.databaseType == 6) {
1440
// Database actions on the main collection (Postgres)
1441
obj.Set = function (value, func) {
1442
obj.dbCounters.fileSet++;
@@ -2003,7 +2260,7 @@ module.exports.CreateDB = function (parent, func) {
2260
const newAutoBackupPath = parent.path.join(backupPath, newAutoBackupFile);
2261
2262
r += 'DB Name: ' + dbname + '\r\n';
2006
- r += 'DB Type: ' + ['None', 'NeDB', 'MongoJS', 'MongoDB', 'MariaDB', 'MySQL'][obj.databaseType] + '\r\n';
2263
+ r += 'DB Type: ' + ['None', 'NeDB', 'MongoJS', 'MongoDB', 'MariaDB', 'MySQL', 'AceBase'][obj.databaseType] + '\r\n';
2264
r += 'BackupPath: ' + backupPath + '\r\n';
2265
r += 'newAutoBackupFile: ' + newAutoBackupFile + '\r\n';
2266
r += 'newAutoBackupPath: ' + newAutoBackupPath + '\r\n';
meshcentral-config-schema.json
+7
@@ -35,6 +35,13 @@
35
}
36
}
37
},
38
+ "acebase": {
39
+ "type": "object",
40
+ "description": "Add this section to enable AceBase database support, this is a local database system much like NeDB.",
41
+ "properties": {
42
+ "sponsor": { "type": "boolean", "default": false, "description": "Set true to remove the AceBase banner on startup." },
43
+ }
44
+ },
45
"mySQL": {
46
"type": "object",
47
"description": "Add this section to connect MeshCentral to a MySQL database instance.",
meshcentral.js
+17
-1
@@ -802,6 +802,20 @@ function CreateMeshCentralServer(config, args) {
802
803
require('./db.js').CreateDB(obj,
804
function (db) {
805
+
806
+
807
+ //db.Get('user//admin', function (err, docs) { console.log('GetResult', err, docs); });
808
+ //db.Set({ _id: 'user//admin', type: 'user', domain: 'a', test: 'this is a user' }, function () { console.log('SetResult'); });
809
+ //db.Get('user//admin', function (err, docs) { console.log('GetResult', err, docs); });
810
+ //db.GetAll(function (err, docs) { console.log('GetAll', err, docs); });
811
+ //db.GetAllTypeNoTypeField('user', 'a', function (err, docs) { console.log('GetAllTypeNoTypeField', err, docs); });
812
+ //db.isMaxType(10, 'user', 'a', function (max, count) { console.log('yy', max, count); })
813
+
814
+ //db.StoreEvent({ test: "this is an event" }, function () { console.log('event stored'); });
815
+ //db.GetAllEvents(function (err, docs) { console.log('events', docs); });
816
+
817
+ //return;
818
+
819
obj.db = db;
820
obj.db.SetupDatabase(function (dbversion) {
821
// See if any database operations needs to be completed
@@ -1198,12 +1212,13 @@ function CreateMeshCentralServer(config, args) {
1212
// Lower case all keys in the config file
1213
common.objKeysToLower(config2, ['ldapoptions', 'defaultuserwebstate', 'forceduserwebstate', 'httpheaders']);
1214
1201
- // Grad some of the values from the original config.json file if present.
1215
+ // Grab some of the values from the original config.json file if present.
1216
config2['mysql'] = config['mysql'];
1217
config2['mariadb'] = config['mariadb'];
1218
config2['mongodb'] = config['mongodb'];
1219
config2['mongodbcol'] = config['mongodbcol'];
1220
config2['dbencryptkey'] = config['dbencryptkey'];
1221
+ config2['acebase'] = config['acebase'];
1222
1223
// We got a new config.json from the database, let's use it.
1224
config = obj.config = config2;
@@ -3894,6 +3909,7 @@ function mainStart() {
3909
if (config.settings.mongodb != null) { modules.push('mongodb@4.1.0'); modules.push('saslprep'); } // Add MongoDB, official driver.
3910
if (config.settings.postgres != null) { modules.push('pg@8.7.1'); modules.push('pgtools@0.3.2'); } // Add Postgres, Postgres driver.
3911
if (config.settings.mariadb != null) { modules.push('mariadb'); } // Add MariaDB, official driver.
3912
+ if (config.settings.acebase != null) { modules.push('acebase'); } // Add AceBase, official driver.
3913
if (config.settings.vault != null) { modules.push('node-vault'); } // Add official HashiCorp's Vault module.
3914
if (config.settings.plugins != null) { modules.push('semver'); } // Required for version compat testing and update checks
3915
if ((config.settings.plugins != null) && (config.settings.plugins.proxy != null)) { modules.push('https-proxy-agent'); } // Required for HTTP/HTTPS proxy support
sample-config-advanced.json
+1
@@ -3,6 +3,7 @@
3
"__comment__": "This is a sample configuration file, all values and sections that start with underscore (_) are ignored. Edit a section and remove the _ in front of the name. Refer to the user's guide for details.",
4
"settings": {
5
"_cert": "myserver.mydomain.com",
6
+ "_acebase": { "_sponsor": true },
7
"_mongoDb": "mongodb://127.0.0.1:27017",
8
"_mongoDbName": "meshcentral",
9
"_mongoDbChangeStream": true,