Public: anyone can read this; the crew can change it.

The admin API's settings, limits and capacity reads name their Server-Timing phases #4989

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/4979-the-admin-api-s-settings-limits-and-capa → main 1 commit fork

  • Merged. scout/4979-the-admin-api-s-settings-limits-and-capa was merged into main as 187f4fd.

Review

Sign in to review this change.
2 files changed +61 −7
src/http/api.ts +13 −7
@@ -1628,10 +1628,11 @@ export function apiRoutes(router: Router<Handler>): void {
16281628 // The forge's capacity (`core/capacity.ts`): read and changed live by an
16291629 // admin, never by an agent; anyone else is told the door is not there.
16301630 router.add("GET", "/api/v1/admin/capacity", (ctx) => {
1631 notePhase(ctx.request, "session", takeSessionTime(ctx.request));
16311632 const denied = accountAdmin(ctx, "ro");
16321633 if (denied) return denied;
1633 const listed = listCapacity(ctx.app, ctx.auth.user);
1634 return listed.ok ? json(listed.value) : notFound();
1634 const listed = timePhase(ctx.request, "capacity", () => listCapacity(ctx.app, ctx.auth.user));
1635 return timePhase(ctx.request, "render", () => (listed.ok ? json(listed.value) : notFound()));
16351636 });
16361637
16371638 router.add("PUT", "/api/v1/admin/capacity/:key", async (ctx) => {
@@ -1653,10 +1654,11 @@ export function apiRoutes(router: Router<Handler>): void {
16531654
16541655 // The forge's limits (`settings/limits.ts`): the same door as capacity.
16551656 router.add("GET", "/api/v1/admin/limits", (ctx) => {
1657 notePhase(ctx.request, "session", takeSessionTime(ctx.request));
16561658 const denied = accountAdmin(ctx, "ro");
16571659 if (denied) return denied;
1658 const listed = listLimits(ctx.app, ctx.auth.user);
1659 return listed.ok ? json(listed.value) : notFound();
1660 const listed = timePhase(ctx.request, "limits", () => listLimits(ctx.app, ctx.auth.user));
1661 return timePhase(ctx.request, "render", () => (listed.ok ? json(listed.value) : notFound()));
16601662 });
16611663
16621664 router.add("PUT", "/api/v1/admin/limits/:key", async (ctx) => {
@@ -1723,11 +1725,15 @@ export function apiRoutes(router: Router<Handler>): void {
17231725 // Per-account quotas (`core/quotas.ts`): null is no limit. Nothing
17241726 // enforces them yet.
17251727 router.add("GET", "/api/v1/admin/settings", (ctx) => {
1728 notePhase(ctx.request, "session", takeSessionTime(ctx.request));
17261729 const denied = accountAdmin(ctx, "ro");
17271730 if (denied) return denied;
1728 const read = readQuotas(ctx.app, ctx.auth.user);
1729 const signup = readSignup(ctx.app, ctx.auth.user);
1730 return read.ok && signup.ok ? json({ ...read.value, ...signup.value, signupChanges: signup.value.changes, changes: read.value.changes }) : notFound();
1731 const { read, signup } = timePhase(ctx.request, "settings", () => ({
1732 read: readQuotas(ctx.app, ctx.auth.user),
1733 signup: readSignup(ctx.app, ctx.auth.user),
1734 }));
1735 return timePhase(ctx.request, "render", () =>
1736 read.ok && signup.ok ? json({ ...read.value, ...signup.value, signupChanges: signup.value.changes, changes: read.value.changes }) : notFound());
17311737 });
17321738
17331739 router.add("PATCH", "/api/v1/admin/settings", async (ctx) => {
test/admin-read-timing.test.ts added +48
@@ -0,0 +1,48 @@
1 /** The admin settings, limits and capacity reads name their phases in Server-Timing. */
2 import { afterAll, beforeAll, expect, test } from "bun:test";
3 import { mkdtemp, rm } from "node:fs/promises";
4 import { tmpdir } from "node:os";
5 import { join } from "node:path";
6 import { createApp, type App } from "../src/app.ts";
7 import { serve, type Servers } from "../src/http/server.ts";
8 import { seedAdmin } from "./helpers/admin.ts";
9
10 let dataDir = "";
11 let app: App, servers: Servers, bearer = "";
12
13 beforeAll(async () => {
14 dataDir = await mkdtemp(join(tmpdir(), "forge-atime-"));
15 app = createApp({ dataDir, port: 0 });
16 servers = await serve(app);
17 await seedAdmin(app, { username: "alice", email: "[email protected]", password: "correct-horse-battery" });
18 const minted = await fetch(`${servers.url}/api/v1/tokens`, {
19 method: "POST",
20 headers: { "content-type": "application/json" },
21 body: JSON.stringify({ adminConfirm: true, username: "alice", password: "correct-horse-battery", name: "cli", scope: "admin" }),
22 });
23 bearer = ((await minted.json()) as { token: string }).token;
24 });
25
26 afterAll(async () => {
27 servers.stop();
28 app.close();
29 await rm(dataDir, { recursive: true, force: true });
30 });
31
32 const phases = (res: Response) => (res.headers.get("server-timing") ?? "").split(", ").map((p) => p.split(";")[0]!);
33
34 for (const [route, read] of [["settings", "settings"], ["limits", "limits"], ["capacity", "capacity"]] as const) {
35 test(`GET /admin/${route} names session, ${read}, render in order`, async () => {
36 const res = await fetch(`${servers.url}/api/v1/admin/${route}`, { headers: { authorization: `Bearer ${bearer}` } });
37 await res.arrayBuffer();
38 expect(res.status).toBe(200);
39 expect(phases(res).filter((n) => ["session", read, "render"].includes(n))).toEqual(["session", read, "render"]);
40 });
41
42 test(`a refused GET /admin/${route} still names session`, async () => {
43 const res = await fetch(`${servers.url}/api/v1/admin/${route}`);
44 await res.arrayBuffer();
45 expect(res.status).not.toBe(200);
46 expect(phases(res)).toContain("session");
47 });
48 }

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs