Files
outline/server/scripts/20240930113921-hash-api-keys.ts
Tom Moor 1a02b0d9d7 Add script to backfill ApiKey hashes (#7717)
* Add hashed column for API keys

---------

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
2024-10-03 16:27:25 -07:00

54 lines
1.5 KiB
TypeScript

import "./bootstrap";
import { Transaction } from "sequelize";
import { ApiKey } from "@server/models";
import { sequelize } from "@server/storage/database";
let page = parseInt(process.argv[2], 10);
page = Number.isNaN(page) ? 0 : page;
export default async function main(exit = false, limit = 100) {
const work = async (page: number): Promise<void> => {
console.log(`Backfill apiKey hash… page ${page}`);
let apiKeys: ApiKey[] = [];
await sequelize.transaction(async (transaction) => {
apiKeys = await ApiKey.unscoped().findAll({
limit,
offset: page * limit,
order: [["createdAt", "ASC"]],
lock: Transaction.LOCK.UPDATE,
transaction,
});
for (const apiKey of apiKeys) {
try {
if (!apiKey.hash) {
console.log(`Migrating ${apiKey.id}`);
apiKey.value = apiKey.secret;
apiKey.hash = ApiKey.hash(apiKey.secret);
// @ts-expect-error secret is deprecated
apiKey.secret = null;
await apiKey.save({ transaction });
}
} catch (err) {
console.error(`Failed at ${apiKey.id}:`, err);
continue;
}
}
});
return apiKeys.length === limit ? work(page + 1) : undefined;
};
await work(page);
console.log("Backfill complete");
if (exit) {
process.exit(0);
}
}
// In the test suite we import the script rather than run via node CLI
if (process.env.NODE_ENV !== "test") {
void main(true);
}