Skip to content

Commit

Permalink
[keyserver] cron job to sync reserved usernames
Browse files Browse the repository at this point in the history
Summary:
daily cron job to send all the usernames from ashoat's keyserver to the identity service.

the logic here is very similar to the logic added to the account-creator.js

Depends on D8330

Test Plan: Modified cron job schedule to force a run and observed that usernames were added to the identity service's reserved usernames table locally

Reviewers: ashoat

Reviewed By: ashoat

Subscribers: tomek

Differential Revision: https://phab.comm.dev/D8331
  • Loading branch information
vdhanan committed Jul 4, 2023
1 parent 897c86f commit 1dc6de0
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
16 changes: 15 additions & 1 deletion keyserver/src/cron/cron.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import schedule from 'node-schedule';
import { backupDB } from './backups.js';
import { createDailyUpdatesThread } from './daily-updates.js';
import { updateAndReloadGeoipDB } from './update-geoip-db.js';
import { updateIdentityReservedUsernames } from './update-identity-reserved-usernames.js';
import { deleteOrphanedActivity } from '../deleters/activity-deleters.js';
import { deleteExpiredCookies } from '../deleters/cookie-deleters.js';
import { deleteOrphanedDays } from '../deleters/day-deleters.js';
Expand Down Expand Up @@ -92,7 +93,20 @@ if (cluster.isMaster) {
}
},
);

schedule.scheduleJob(
'0 5 * * *', // every day at 5:00 AM in the keyserver's timezone
async () => {
try {
await updateIdentityReservedUsernames();
} catch (e) {
console.warn(
'encountered error while trying to update reserved usernames on ' +
'identity service',
e,
);
}
},
);
schedule.scheduleJob(
'0 0 * * *', // every day at midnight in the keyserver's timezone
async () => {
Expand Down
28 changes: 28 additions & 0 deletions keyserver/src/cron/update-identity-reserved-usernames.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// @flow

import { getRustAPI } from 'rust-node-addon';

import type { ReservedUsernameMessage } from 'lib/types/crypto-types.js';

import { fetchAllUsernames } from '../fetchers/user-fetchers.js';
import { fetchOlmAccount } from '../updaters/olm-account-updater.js';

async function updateIdentityReservedUsernames(): Promise<void> {
const [usernames, rustAPI, accountInfo] = await Promise.all([
fetchAllUsernames(),
getRustAPI(),
fetchOlmAccount('content'),
]);
const issuedAt = new Date().toISOString();
const reservedUsernameMessage: ReservedUsernameMessage = {
statement: 'Add the following usernames to reserved list',
payload: usernames,
issuedAt,
};
const stringifiedMessage = JSON.stringify(reservedUsernameMessage);
const signature = accountInfo.account.sign(stringifiedMessage);

await rustAPI.addReservedUsernames(stringifiedMessage, signature);
}

export { updateIdentityReservedUsernames };

0 comments on commit 1dc6de0

Please sign in to comment.