forked from mtusi2026/mtusi2026lab7
lab7: app security, Argon2id, mTLS, OpenBao client, least-privilege worker
This commit is contained in:
@@ -2,33 +2,48 @@ import { fileURLToPath } from 'node:url';
|
||||
import { parseAppConfig } from '../config/app-config.schema.js';
|
||||
import { DatabaseClient } from '../database/database.client.js';
|
||||
import { MetricsService } from '../metrics/metrics.service.js';
|
||||
import { RuntimeSecretsStore } from '../openbao/runtime-secrets.service.js';
|
||||
import { TlsMaterialManager } from '../security/tls-material.js';
|
||||
import { ObjectStorage } from '../storage/object-storage.js';
|
||||
import { AttachmentSweeper } from './attachment-sweeper.service.js';
|
||||
import { AttachmentsRepository } from './attachments.repository.js';
|
||||
|
||||
// Manual operator pass over unfinished uploads (the API also sweeps in the
|
||||
// background). Never touches rows before expiry + ATTACHMENT_SWEEP_GRACE_SECONDS.
|
||||
// in the stand: docker compose exec api node dist/attachments/attachments-sweep.cli.js
|
||||
// on the host: npm run attachments:sweep (needs reachable DATABASE_URL and S3)
|
||||
async function main(): Promise<void> {
|
||||
const config = parseAppConfig(process.env as Record<string, unknown>);
|
||||
const rawEnv = process.env as Record<string, unknown>;
|
||||
const config = parseAppConfig(rawEnv);
|
||||
const tlsMaterial = TlsMaterialManager.initFromEnv('cli', rawEnv);
|
||||
const secretsStore = await RuntimeSecretsStore.bootstrap(
|
||||
'api',
|
||||
tlsMaterial,
|
||||
rawEnv,
|
||||
undefined,
|
||||
{ background: false },
|
||||
);
|
||||
const configService = {
|
||||
get: (key: keyof typeof config) => config[key],
|
||||
getOrThrow: (key: keyof typeof config) => config[key],
|
||||
} as never;
|
||||
const metrics = new MetricsService();
|
||||
const database = new DatabaseClient(configService, metrics);
|
||||
const database = new DatabaseClient(
|
||||
configService,
|
||||
metrics,
|
||||
tlsMaterial,
|
||||
secretsStore,
|
||||
);
|
||||
const storage = new ObjectStorage(configService, tlsMaterial, secretsStore);
|
||||
try {
|
||||
const sweeper = new AttachmentSweeper(
|
||||
configService,
|
||||
new AttachmentsRepository(database),
|
||||
new ObjectStorage(configService),
|
||||
storage,
|
||||
metrics,
|
||||
);
|
||||
let total = 0;
|
||||
for (;;) {
|
||||
const { claimed, deleted } = await sweeper.sweepOnce();
|
||||
total += deleted;
|
||||
// A pass that claimed rows but could not delete any would loop forever.
|
||||
if (claimed === 0 || deleted === 0) break;
|
||||
}
|
||||
process.stdout.write(
|
||||
@@ -36,6 +51,9 @@ async function main(): Promise<void> {
|
||||
);
|
||||
} finally {
|
||||
await database.onModuleDestroy();
|
||||
storage.onModuleDestroy();
|
||||
secretsStore.close();
|
||||
tlsMaterial.onModuleDestroy();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user