mirror of
https://github.com/laurent22/joplin.git
synced 2025-01-11 18:24:43 +02:00
CLI: Added e2ee command to check encryption status of a sync target
This commit is contained in:
parent
6252a4d8c8
commit
f9634ea283
@ -4,22 +4,24 @@ const { cliUtils } = require('./cli-utils.js');
|
|||||||
const EncryptionService = require('lib/services/EncryptionService');
|
const EncryptionService = require('lib/services/EncryptionService');
|
||||||
const DecryptionWorker = require('lib/services/DecryptionWorker');
|
const DecryptionWorker = require('lib/services/DecryptionWorker');
|
||||||
const MasterKey = require('lib/models/MasterKey');
|
const MasterKey = require('lib/models/MasterKey');
|
||||||
|
const BaseItem = require('lib/models/BaseItem');
|
||||||
const Setting = require('lib/models/Setting.js');
|
const Setting = require('lib/models/Setting.js');
|
||||||
|
|
||||||
class Command extends BaseCommand {
|
class Command extends BaseCommand {
|
||||||
|
|
||||||
usage() {
|
usage() {
|
||||||
return 'e2ee <command>';
|
return 'e2ee <command> [path]';
|
||||||
}
|
}
|
||||||
|
|
||||||
description() {
|
description() {
|
||||||
return _('Manages E2EE configuration. Commands are `enable`, `disable` and `decrypt`.');
|
return _('Manages E2EE configuration. Commands are `enable`, `disable`, `decrypt` and `target-status`.');
|
||||||
}
|
}
|
||||||
|
|
||||||
options() {
|
options() {
|
||||||
return [
|
return [
|
||||||
// This is here mostly for testing - shouldn't be used
|
// This is here mostly for testing - shouldn't be used
|
||||||
['-p, --password <password>', 'Use this password as master password (For security reasons, it is not recommended to use this option).'],
|
['-p, --password <password>', 'Use this password as master password (For security reasons, it is not recommended to use this option).'],
|
||||||
|
['-v, --verbose', 'More verbose output for the `target-status` command'],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -72,6 +74,102 @@ class Command extends BaseCommand {
|
|||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (args.command === 'target-status') {
|
||||||
|
const fs = require('fs-extra');
|
||||||
|
const pathUtils = require('lib/path-utils.js');
|
||||||
|
const fsDriver = new (require('lib/fs-driver-node.js').FsDriverNode)();
|
||||||
|
|
||||||
|
const targetPath = args.path;
|
||||||
|
if (!targetPath) throw new Error('Please specify the sync target path.');
|
||||||
|
|
||||||
|
const dirPaths = function(targetPath) {
|
||||||
|
let paths = [];
|
||||||
|
fs.readdirSync(targetPath).forEach((path) => {
|
||||||
|
paths.push(path);
|
||||||
|
});
|
||||||
|
return paths;
|
||||||
|
}
|
||||||
|
|
||||||
|
let itemCount = 0;
|
||||||
|
let resourceCount = 0;
|
||||||
|
let encryptedItemCount = 0;
|
||||||
|
let encryptedResourceCount = 0;
|
||||||
|
let otherItemCount = 0;
|
||||||
|
|
||||||
|
let encryptedPaths = [];
|
||||||
|
let decryptedPaths = [];
|
||||||
|
|
||||||
|
let paths = dirPaths(targetPath);
|
||||||
|
|
||||||
|
for (let i = 0; i < paths.length; i++) {
|
||||||
|
const path = paths[i];
|
||||||
|
const fullPath = targetPath + '/' + path;
|
||||||
|
const stat = await fs.stat(fullPath);
|
||||||
|
|
||||||
|
// this.stdout(fullPath);
|
||||||
|
|
||||||
|
if (path === '.resource') {
|
||||||
|
let resourcePaths = dirPaths(fullPath);
|
||||||
|
for (let j = 0; j < resourcePaths.length; j++) {
|
||||||
|
const resourcePath = resourcePaths[j];
|
||||||
|
resourceCount++;
|
||||||
|
const fullResourcePath = fullPath + '/' + resourcePath;
|
||||||
|
const isEncrypted = await EncryptionService.instance().fileIsEncrypted(fullResourcePath);
|
||||||
|
if (isEncrypted) {
|
||||||
|
encryptedResourceCount++;
|
||||||
|
encryptedPaths.push(fullResourcePath);
|
||||||
|
} else {
|
||||||
|
decryptedPaths.push(fullResourcePath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (stat.isDirectory()) {
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
itemCount++;
|
||||||
|
const content = await fs.readFile(fullPath, 'utf8');
|
||||||
|
const item = await BaseItem.unserialize(content);
|
||||||
|
const ItemClass = BaseItem.itemClass(item);
|
||||||
|
|
||||||
|
if (!ItemClass.encryptionSupported()) {
|
||||||
|
otherItemCount++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const isEncrypted = await EncryptionService.instance().itemIsEncrypted(item);
|
||||||
|
|
||||||
|
if (isEncrypted) {
|
||||||
|
encryptedItemCount++;
|
||||||
|
encryptedPaths.push(fullPath);
|
||||||
|
} else {
|
||||||
|
decryptedPaths.push(fullPath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this.stdout('Encrypted items: ' + encryptedItemCount + '/' + itemCount);
|
||||||
|
this.stdout('Encrypted resources: ' + encryptedResourceCount + '/' + resourceCount);
|
||||||
|
this.stdout('Other items (never encrypted): ' + otherItemCount);
|
||||||
|
|
||||||
|
if (options.verbose) {
|
||||||
|
this.stdout('');
|
||||||
|
this.stdout('# Encrypted paths');
|
||||||
|
this.stdout('');
|
||||||
|
for (let i = 0; i < encryptedPaths.length; i++) {
|
||||||
|
const path = encryptedPaths[i];
|
||||||
|
this.stdout(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.stdout('');
|
||||||
|
this.stdout('# Decrypted paths');
|
||||||
|
this.stdout('');
|
||||||
|
for (let i = 0; i < decryptedPaths.length; i++) {
|
||||||
|
const path = decryptedPaths[i];
|
||||||
|
this.stdout(path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user