38 lines
1.5 KiB
JavaScript
38 lines
1.5 KiB
JavaScript
'use strict';
|
|
|
|
exports = module.exports = {
|
|
check
|
|
};
|
|
|
|
const backups = require('./backups.js'),
|
|
backupFormats = require('./backupformats.js'),
|
|
backupSites = require('./backupsites.js'),
|
|
BoxError = require('./boxerror'),
|
|
consumers = require('node:stream/consumers'),
|
|
crypto = require('node:crypto');
|
|
|
|
async function check(backupId, progressCallback) {
|
|
const backup = await backups.get(backupId);
|
|
if (!backup) throw new BoxError(BoxError.BAD_FIELD, 'Backup not found');
|
|
|
|
const backupSite = await backupSites.get(backup.siteId);
|
|
if (!backupSite) throw new BoxError(BoxError.BAD_FIELD, 'Backup site not found');
|
|
|
|
const stream = await backupSites.storageApi(backupSite).download(backupSite.config, `${backup.remotePath}.backupinfo`);
|
|
const buffer = await consumers.buffer(stream);
|
|
|
|
const validSignature = crypto.verify(null /* algo */, buffer, backupSite.integrityKeyPair.publicKey, Buffer.from(backup.integrity.signature, 'utf8'));
|
|
progressCallback({ message: `Signature valid? ${validSignature}`});
|
|
|
|
const backupInfo = JSON.parse(buffer.toString('utf8'));
|
|
const integrityMap = new Map(Object.entries(backupInfo));
|
|
|
|
const verifyResult = await backupFormats.api(backupSite.format).verify(backupSite, backup.remotePath, integrityMap, progressCallback);
|
|
progressCallback({ message: 'Verification done' });
|
|
|
|
return {
|
|
signature: { status: validSignature ? 'passed' : 'failed' },
|
|
...verifyResult
|
|
};
|
|
}
|