|
| 1 | +/** |
| 2 | + * Copyright (C) 2024 Mailvelope GmbH |
| 3 | + * Licensed under the GNU Affero General Public License version 3 |
| 4 | + */ |
| 5 | + |
| 6 | +'use strict'; |
| 7 | + |
| 8 | +const config = require('../../config/config'); |
| 9 | +const Mongo = require('../modules/mongo'); |
| 10 | +const PurifyKey = require('../modules/purify-key'); |
| 11 | +const PGP = require('../modules/pgp'); |
| 12 | + |
| 13 | +const DB_TYPE = 'publickey'; |
| 14 | +const KEY_SIZE = 1; // divided by 4/3 gives binary size of key |
| 15 | +const MAX_UPLOAD_DATE = new Date(new Date().setDate(new Date().getDate() - config.publicKey.purgeTimeInDays)); // now - purgeTimeInDays |
| 16 | +const YEAR = parseInt(process.argv[2] ?? MAX_UPLOAD_DATE.getFullYear()); |
| 17 | + |
| 18 | +let mongo; |
| 19 | +let pgp; |
| 20 | + |
| 21 | +async function init() { |
| 22 | + mongo = new Mongo(); |
| 23 | + await mongo.init(config.mongo); |
| 24 | + const purify = new PurifyKey({...config.purify, maxNumUserEmail: 60, maxNumSubkey: 25, maxSizeKey: 64 * 1024}); |
| 25 | + pgp = new PGP(purify); |
| 26 | +} |
| 27 | + |
| 28 | +function aggregate() { |
| 29 | + return mongo.aggregate([ |
| 30 | + {$match: {uploaded: {$gte: new Date(YEAR, 0, 1), $lt: new Date(YEAR + 1, 0, 1)}}}, |
| 31 | + {$match: {uploaded: {$lt: MAX_UPLOAD_DATE}}}, |
| 32 | + {$project: {keySize: {$binarySize: '$publicKeyArmored'}}}, |
| 33 | + {$match: {keySize: {$gt: KEY_SIZE}}} |
| 34 | + ], DB_TYPE); |
| 35 | +} |
| 36 | + |
| 37 | +async function clean() { |
| 38 | + try { |
| 39 | + console.log(`Start cleaning year ${YEAR}...`); |
| 40 | + await init(); |
| 41 | + const result = await aggregate(); |
| 42 | + let count = 0; |
| 43 | + for await (const document of result) { |
| 44 | + await cleanKey(document); |
| 45 | + count++; |
| 46 | + } |
| 47 | + console.log('Number of keys processed:', count); |
| 48 | + } catch (e) { |
| 49 | + console.log('Error while traversing keys:', e); |
| 50 | + } finally { |
| 51 | + await mongo.disconnect(); |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +async function cleanKey({_id}) { |
| 56 | + const key = await mongo.get({_id}, DB_TYPE); |
| 57 | + if (!key.publicKeyArmored) { |
| 58 | + console.log('No armored key. Key is not yet verified. Skip'); |
| 59 | + return; |
| 60 | + } |
| 61 | + try { |
| 62 | + const purified = await pgp.parseKey(key.publicKeyArmored); |
| 63 | + // filter out all unverified user ID and those that are not in the purified set |
| 64 | + key.userIds = key.userIds.filter(userId => userId.verified && purified.userIds.some(id => id.email === userId.email)); |
| 65 | + if (!key.userIds.length) { |
| 66 | + throw new Error('No user ID after comparing with purified key.'); |
| 67 | + } |
| 68 | + const publicKeyArmored = await pgp.filterKeyByUserIds(key.userIds, purified.publicKeyArmored); |
| 69 | + key.publicKeyArmored = publicKeyArmored; |
| 70 | + await mongo.replace({_id}, key, DB_TYPE); |
| 71 | + } catch (e) { |
| 72 | + console.log('Parsing of key failed:', e.message); |
| 73 | + await mongo.remove({_id}, DB_TYPE); |
| 74 | + console.log(`Key ${key.fingerprint} removed.`); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +clean(); |
0 commit comments