Fixed --recordencryptionrecode when used with NeDB, added mesh record encryption.

Ylian Saint-Hilaire committed Aug 27, 2020 at 16:57 UTC 926e7888e001ae4315208954874d40ff0aa12b1f
2 files changed +16 -5
db.js
+14 -3
@@ -244,7 +244,15 @@ module.exports.CreateDB = function (parent, func) {
244 if (err == null) { for (var i in docs) { count++; obj.Set(docs[i]); } }
245 obj.GetAllType('node', function (err, docs) {
246 if (err == null) { for (var i in docs) { count++; obj.Set(docs[i]); } }
247 - func(count);
247 + obj.GetAllType('mesh', function (err, docs) {
248 + if (err == null) { for (var i in docs) { count++; obj.Set(docs[i]); } }
249 + if (obj.databaseType == 1) { // If we are using NeDB, compact the database.
250 + obj.file.persistence.compactDatafile();
251 + obj.file.on('compaction.done', function () { func(count); }); // It's important to wait for compaction to finish before exit, otherwise NeDB may corrupt.
252 + } else {
253 + func(count); // For all other databases, normal exit.
254 + }
255 + });
256 });
257 });
258 }
@@ -257,6 +265,8 @@ module.exports.CreateDB = function (parent, func) {
265 data[i] = performPartialRecordDecrypt(data[i]);
266 } else if ((data[i].type == 'node') && (data[i].intelamt != null)) {
267 data[i].intelamt = performPartialRecordDecrypt(data[i].intelamt);
268 + } else if ((data[i].type == 'mesh') && (data[i].amt != null)) {
269 + data[i].amt = performPartialRecordDecrypt(data[i].amt);
270 }
271 }
272 return data;
@@ -267,6 +277,7 @@ module.exports.CreateDB = function (parent, func) {
277 if (obj.dbRecordsEncryptKey == null) return data;
278 if (data.type == 'user') { return performPartialRecordEncrypt(Clone(data), ['otpkeys', 'otphkeys', 'otpsecret', 'salt', 'hash', 'oldpasswords']); }
279 else if ((data.type == 'node') && (data.intelamt != null)) { var xdata = Clone(data); xdata.intelamt = performPartialRecordEncrypt(xdata.intelamt, ['user', 'pass']); return xdata; }
280 + else if ((data.type == 'mesh') && (data.amt != null)) { var xdata = Clone(data); xdata.amt = performPartialRecordEncrypt(xdata.amt, ['password']); return xdata; }
281 return data;
282 }
283
@@ -329,13 +340,13 @@ module.exports.CreateDB = function (parent, func) {
340 // If a DB record encryption key is provided, perform database record encryption
341 if ((typeof parent.args.dbrecordsencryptkey == 'string') && (parent.args.dbrecordsencryptkey.length != 0)) {
342 // Hash the database password into a AES256 key and setup encryption and decryption.
332 - obj.dbRecordsEncryptKey = obj.dbRecordsDecryptKey = parent.crypto.createHash('sha384').update(parent.args.dbrecordsencryptkey).digest("raw").slice(0, 32);
343 + obj.dbRecordsEncryptKey = obj.dbRecordsDecryptKey = parent.crypto.createHash('sha384').update(parent.args.dbrecordsencryptkey).digest('raw').slice(0, 32);
344 }
345
346 // If a DB record decryption key is provided, perform database record decryption
347 if ((typeof parent.args.dbrecordsdecryptkey == 'string') && (parent.args.dbrecordsdecryptkey.length != 0)) {
348 // Hash the database password into a AES256 key and setup encryption and decryption.
338 - obj.dbRecordsDecryptKey = parent.crypto.createHash('sha384').update(parent.args.dbrecordsdecryptkey).digest("raw").slice(0, 32);
349 + obj.dbRecordsDecryptKey = parent.crypto.createHash('sha384').update(parent.args.dbrecordsdecryptkey).digest('raw').slice(0, 32);
350 }
351
352 if (parent.args.mariadb || parent.args.mysql) {
meshcentral-config-schema.json
+2 -2
@@ -18,8 +18,8 @@
18 "sessionKey": { "type": "string" },
19 "sessionSameSite": { "type": "string" },
20 "dbEncryptKey": { "type": "string" },
21 - "dbRecordsEncryptKey": { "type": "string" },
22 - "dbRecordsDecryptKey": { "type": "string" },
21 + "dbRecordsEncryptKey": { "type": "string", "default": null },
22 + "dbRecordsDecryptKey": { "type": "string", "default": null },
23 "dbExpire": {
24 "type": "object",
25 "properties": {