2018-03-09 22:59:12 +02:00
|
|
|
const BaseItem = require('lib/models/BaseItem');
|
|
|
|
const { Logger } = require('lib/logger.js');
|
2017-12-14 20:53:08 +02:00
|
|
|
|
2017-12-14 19:58:10 +02:00
|
|
|
class DecryptionWorker {
|
2018-03-09 22:59:12 +02:00
|
|
|
|
2017-12-14 19:58:10 +02:00
|
|
|
constructor() {
|
2018-03-09 22:59:12 +02:00
|
|
|
this.state_ = 'idle';
|
2017-12-31 16:23:05 +02:00
|
|
|
this.logger_ = new Logger();
|
2017-12-14 20:53:08 +02:00
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
this.dispatch = (action) => {
|
2017-12-17 21:51:45 +02:00
|
|
|
//console.warn('DecryptionWorker.dispatch is not defined');
|
2017-12-14 20:53:08 +02:00
|
|
|
};
|
2017-12-14 21:39:13 +02:00
|
|
|
|
|
|
|
this.scheduleId_ = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
setLogger(l) {
|
|
|
|
this.logger_ = l;
|
|
|
|
}
|
|
|
|
|
|
|
|
logger() {
|
|
|
|
return this.logger_;
|
2017-12-14 20:53:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static instance() {
|
|
|
|
if (this.instance_) return this.instance_;
|
|
|
|
this.instance_ = new DecryptionWorker();
|
|
|
|
return this.instance_;
|
|
|
|
}
|
|
|
|
|
2017-12-14 21:39:13 +02:00
|
|
|
setEncryptionService(v) {
|
|
|
|
this.encryptionService_ = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
encryptionService() {
|
2018-03-09 22:59:12 +02:00
|
|
|
if (!this.encryptionService_) throw new Error('DecryptionWorker.encryptionService_ is not set!!');
|
2017-12-14 20:53:08 +02:00
|
|
|
return this.encryptionService_;
|
2017-12-14 19:58:10 +02:00
|
|
|
}
|
|
|
|
|
2017-12-14 21:39:13 +02:00
|
|
|
async scheduleStart() {
|
|
|
|
if (this.scheduleId_) return;
|
|
|
|
|
|
|
|
this.scheduleId_ = setTimeout(() => {
|
|
|
|
this.scheduleId_ = null;
|
2017-12-26 12:38:53 +02:00
|
|
|
this.start({
|
2018-03-09 22:59:12 +02:00
|
|
|
materKeyNotLoadedHandler: 'dispatch',
|
2017-12-26 12:38:53 +02:00
|
|
|
});
|
2017-12-14 21:39:13 +02:00
|
|
|
}, 1000);
|
|
|
|
}
|
|
|
|
|
2017-12-26 12:38:53 +02:00
|
|
|
async start(options = null) {
|
|
|
|
if (options === null) options = {};
|
2018-03-09 22:59:12 +02:00
|
|
|
if (!('materKeyNotLoadedHandler' in options)) options.materKeyNotLoadedHandler = 'throw';
|
2017-12-26 12:38:53 +02:00
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
if (this.state_ !== 'idle') {
|
2017-12-26 12:38:53 +02:00
|
|
|
this.logger().info('DecryptionWorker: cannot start because state is "' + this.state_ + '"');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
this.logger().info('DecryptionWorker: starting decryption...');
|
2017-12-14 19:58:10 +02:00
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
this.state_ = 'started';
|
2017-12-14 20:53:08 +02:00
|
|
|
|
|
|
|
let excludedIds = [];
|
|
|
|
|
2017-12-14 21:39:13 +02:00
|
|
|
try {
|
2018-01-28 19:37:29 +02:00
|
|
|
const notLoadedMasterKeyDisptaches = [];
|
|
|
|
|
2017-12-14 21:39:13 +02:00
|
|
|
while (true) {
|
|
|
|
const result = await BaseItem.itemsThatNeedDecryption(excludedIds);
|
|
|
|
const items = result.items;
|
|
|
|
|
|
|
|
for (let i = 0; i < items.length; i++) {
|
|
|
|
const item = items[i];
|
2018-01-31 21:51:29 +02:00
|
|
|
|
2018-02-01 22:15:31 +02:00
|
|
|
// Temp hack
|
2018-02-15 20:33:08 +02:00
|
|
|
// if (['edf44b7a0e4f8cbf248e206cd8dfa800', '2ccb3c9af0b1adac2ec6b66a5961fbb1'].indexOf(item.id) >= 0) {
|
|
|
|
// excludedIds.push(item.id);
|
|
|
|
// continue;
|
|
|
|
// }
|
2018-01-31 21:51:29 +02:00
|
|
|
|
2017-12-14 21:39:13 +02:00
|
|
|
const ItemClass = BaseItem.itemClass(item);
|
2018-03-09 22:59:12 +02:00
|
|
|
|
2018-03-05 20:21:42 +02:00
|
|
|
// Don't log in production as it results in many messages when importing many items
|
|
|
|
// this.logger().info('DecryptionWorker: decrypting: ' + item.id + ' (' + ItemClass.tableName() + ')');
|
2017-12-14 21:39:13 +02:00
|
|
|
try {
|
|
|
|
await ItemClass.decrypt(item);
|
|
|
|
} catch (error) {
|
2018-02-14 17:28:56 +02:00
|
|
|
excludedIds.push(item.id);
|
2018-03-09 22:59:12 +02:00
|
|
|
|
|
|
|
if (error.code === 'masterKeyNotLoaded' && options.materKeyNotLoadedHandler === 'dispatch') {
|
2018-01-28 19:37:29 +02:00
|
|
|
if (notLoadedMasterKeyDisptaches.indexOf(error.masterKeyId) < 0) {
|
|
|
|
this.dispatch({
|
2018-03-09 22:59:12 +02:00
|
|
|
type: 'MASTERKEY_ADD_NOT_LOADED',
|
2018-01-28 19:37:29 +02:00
|
|
|
id: error.masterKeyId,
|
|
|
|
});
|
|
|
|
notLoadedMasterKeyDisptaches.push(error.masterKeyId);
|
|
|
|
}
|
2017-12-14 21:39:13 +02:00
|
|
|
continue;
|
|
|
|
}
|
2018-02-26 21:25:54 +02:00
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
if (error.code === 'masterKeyNotLoaded' && options.materKeyNotLoadedHandler === 'throw') {
|
2018-02-26 21:25:54 +02:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
this.logger().warn('DecryptionWorker: error for: ' + item.id + ' (' + ItemClass.tableName() + ')', error);
|
2017-12-14 20:53:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 21:39:13 +02:00
|
|
|
if (!result.hasMore) break;
|
|
|
|
}
|
|
|
|
} catch (error) {
|
2018-03-09 22:59:12 +02:00
|
|
|
this.logger().error('DecryptionWorker:', error);
|
|
|
|
this.state_ = 'idle';
|
2017-12-26 12:38:53 +02:00
|
|
|
throw error;
|
2017-12-14 20:53:08 +02:00
|
|
|
}
|
2017-12-14 21:39:13 +02:00
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
this.logger().info('DecryptionWorker: completed decryption.');
|
2017-12-26 12:38:53 +02:00
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
this.state_ = 'idle';
|
2017-12-14 19:58:10 +02:00
|
|
|
}
|
2018-03-09 22:59:12 +02:00
|
|
|
|
2017-12-14 19:58:10 +02:00
|
|
|
}
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
module.exports = DecryptionWorker;
|