Add owner-only room backup snapshots and audited exports
This commit is contained in:
1 parent
92564f8de5
commit
53a1ba77a1
19 files changed
+5613
-4
No files matched your search
@@ -0,0 +1,24 @@
|
||||
import { env } from 'cloudflare:workers';
|
||||
import { AdminError, requireAdmin } from '@/db/admin-access';
|
||||
import { downloadAdminBackup } from '@/db/admin-backups';
|
||||
import { adminFailure, adminToken } from '@/lib/admin-http';
|
||||
|
||||
export async function GET(request: Request, context: { params: Promise<{ id: string }> }) {
|
||||
try {
|
||||
const identity = await requireAdmin(env.DB, adminToken(request), ['owner']);
|
||||
const expected = request.headers.get('X-Dune-Admin-Id');
|
||||
if (expected !== null && expected !== identity.id)
|
||||
throw new AdminError('The signed-in administrator changed. Reload administration before continuing.', 401);
|
||||
const { id } = await context.params;
|
||||
const { backup, payload } = await downloadAdminBackup(env.DB, identity, id);
|
||||
return new Response(payload, {
|
||||
headers: {
|
||||
'Content-Type': 'application/json; charset=utf-8',
|
||||
'Content-Disposition': `attachment; filename="dune-room-${backup.roomCode}-backup-${backup.id}.json"`,
|
||||
'Cache-Control': 'no-store',
|
||||
'Vary': 'Cookie',
|
||||
'X-Content-Type-Options': 'nosniff',
|
||||
},
|
||||
});
|
||||
} catch (error) { return adminFailure(error); }
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
import { env } from 'cloudflare:workers';
|
||||
import { AdminError, requireAdmin } from '@/db/admin-access';
|
||||
import { captureAdminBackup, listAdminBackups, validBackupInput } from '@/db/admin-backups';
|
||||
import { adminBody, adminFailure, adminResponse, adminToken } from '@/lib/admin-http';
|
||||
|
||||
function matchIdentity(request: Request, id: string, mutation: boolean) {
|
||||
const header = request.headers.get('X-Dune-Admin-Id');
|
||||
if ((mutation || header !== null) && header !== id)
|
||||
throw new AdminError('The signed-in administrator changed. Reload administration before continuing.', 401);
|
||||
}
|
||||
|
||||
export async function GET(request: Request) {
|
||||
try {
|
||||
const identity = await requireAdmin(env.DB, adminToken(request), ['owner']);
|
||||
matchIdentity(request, identity.id, false);
|
||||
const search = new URL(request.url).searchParams;
|
||||
if ([...search.keys()].some(key => key !== 'room') || search.getAll('room').length !== 1)
|
||||
throw new AdminError('Enter exactly one room code.', 400);
|
||||
return adminResponse({ backups: await listAdminBackups(env.DB, identity, search.get('room') ?? '') });
|
||||
} catch (error) { return adminFailure(error); }
|
||||
}
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const input = await adminBody(request, env.DUNE_PUBLIC_ORIGIN);
|
||||
const identity = await requireAdmin(env.DB, adminToken(request), ['owner']);
|
||||
matchIdentity(request, identity.id, true);
|
||||
if (!validBackupInput(input)) throw new AdminError('Provide a room, current version, operation identifier and operational reason.', 400);
|
||||
return adminResponse(await captureAdminBackup(env.DB, identity, input));
|
||||
} catch (error) { return adminFailure(error); }
|
||||
}
|
||||
Reference in new issue
Block a user