Public: anyone can read this; the crew can change it.
Cloud accounts have a terminal screen and MCP tools as well as the page: list, attach, verify, rotate, rename, remove, and today's vendor calls #2205
scout/forge:scout/2199-cloud-accounts-have-a-terminal-screen-an → main 1 commit fork
8 files
changed +500
−3
README.md +1 −1
| @@ -790,7 +790,7 @@ purpose, host and path, bytes out, when, allowed or refused — no token, no | ||
| 790 | 790 | query) is listed at **Settings → Cloud accounts → Every vendor call today**. |
| 791 | 791 | Rows are pruned past 90 days on every write; `at` is indexed. Left out: one |
| 792 | 792 | token per vendor is not enforced (several names per vendor stay allowed), and |
| 793 | an agent's own vendor calls beyond the hand-out are not metered. Cloud accounts have no terminal or MCP door yet. | |
| 793 | an agent's own vendor calls beyond the hand-out are not metered. A forge admin reaches the same accounts in the terminal (`:` → Cloud accounts: each account with what it is used for, verified / last used and today's vendor calls; `a` attach, `v` verify, `r` rotate, `n` rename, `x` remove, the token typed masked) and over MCP (`cloud_accounts` lists them with today's audit rows; `cloud_account` with `verb` attach, verify, rotate, rename or remove needs an admin-scope token). Both call `actions/cloud.ts`, neither ever shows a token, and anyone else is refused. A rename is refused when the account or the secret `CLOUD_<PROVIDER>_<NAME>` already exists. | |
| 794 | 794 | |
| 795 | 795 | ## Share to the forge |
| 796 | 796 |
src/actions/cloud.ts +6 −2
| @@ -21,7 +21,7 @@ import { | ||
| 21 | 21 | } from "../core/cloud_account.ts"; |
| 22 | 22 | import { createIssue } from "../core/issues.ts"; |
| 23 | 23 | import { getRepo } from "../core/repos.ts"; |
| 24 | import { resolveSecrets } from "../core/secret_store.ts"; | |
| 24 | import { listSecretNames, resolveSecrets } from "../core/secret_store.ts"; | |
| 25 | 25 | import type { User } from "../core/users.ts"; |
| 26 | 26 | import { id } from "../kernel/id.ts"; |
| 27 | 27 | import { err, ok, type Result } from "../kernel/result.ts"; |
| @@ -111,7 +111,11 @@ export function renameAccount( | ||
| 111 | 111 | const dest = target(provider, to); |
| 112 | 112 | if (!dest.ok) return dest; |
| 113 | 113 | if (!getCloudAccount(app.db, from.value.provider, from.value.name)) return err("No such credential."); |
| 114 | if (getCloudAccount(app.db, dest.value.provider, dest.value.name)) return err("An account has that name already."); | |
| 114 | if ( | |
| 115 | getCloudAccount(app.db, dest.value.provider, dest.value.name) || | |
| 116 | listSecretNames(app.db, "forge").some((s) => s.name === dest.value.secret) | |
| 117 | ) | |
| 118 | return err("An account has that name already."); | |
| 115 | 119 | const value = open(app, who.value, from.value.secret, `rename:${from.value.provider}`); |
| 116 | 120 | if (value === undefined) return err("No such credential."); |
| 117 | 121 | const set = putSecret(app, who.value, "forge", "", dest.value.secret, value); |
src/cloud/overview.ts added +53
| @@ -0,0 +1,53 @@ | ||
| 1 | /** | |
| 2 | * Cloud accounts as plain rows for the doors that are not the page (the | |
| 3 | * terminal, MCP): provider, name, what it is used for, when it was verified and | |
| 4 | * last used, and how many vendor calls it made today (UTC day). Never a token | |
| 5 | * value: nothing here opens the sealed secret. A forge admin's; anyone else is | |
| 6 | * told `not_found`. | |
| 7 | */ | |
| 8 | ||
| 9 | import type { App } from "../app.ts"; | |
| 10 | import { auditToday, type AuditRow } from "../core/cloud_audit.ts"; | |
| 11 | import { listCloudAccounts, recentCloudReads } from "../core/cloud_account.ts"; | |
| 12 | import type { User } from "../core/users.ts"; | |
| 13 | import { err, ok, type Result } from "../kernel/result.ts"; | |
| 14 | import { usedFor, type VendorUse } from "./declared.ts"; | |
| 15 | import { VENDOR_OF, type Provider } from "./providers.ts"; | |
| 16 | ||
| 17 | export interface AccountRow { | |
| 18 | readonly provider: Provider; | |
| 19 | readonly name: string; | |
| 20 | readonly summary: string; | |
| 21 | readonly usedFor: string; | |
| 22 | readonly verifiedAt: string | null; | |
| 23 | readonly lastUsedAt: string | null; | |
| 24 | readonly writeAllowed: boolean; | |
| 25 | readonly callsToday: number; | |
| 26 | } | |
| 27 | ||
| 28 | export interface CloudOverview { | |
| 29 | readonly accounts: AccountRow[]; | |
| 30 | /** Today's vendor calls, newest first. */ | |
| 31 | readonly today: AuditRow[]; | |
| 32 | } | |
| 33 | ||
| 34 | export function cloudOverview( | |
| 35 | app: App, | |
| 36 | actor: User | undefined, | |
| 37 | uses: ReadonlyMap<string, readonly VendorUse[]> = new Map(), | |
| 38 | ): Result<CloudOverview, string> { | |
| 39 | if (actor?.isAdmin !== true) return err("not_found"); | |
| 40 | const today = auditToday(app.db); | |
| 41 | const reads = recentCloudReads(app.db); | |
| 42 | const accounts = listCloudAccounts(app.db).map((a) => ({ | |
| 43 | provider: a.provider, | |
| 44 | name: a.name, | |
| 45 | summary: a.summary, | |
| 46 | usedFor: usedFor(uses.get(VENDOR_OF[a.provider])), | |
| 47 | verifiedAt: a.verifiedAt, | |
| 48 | lastUsedAt: reads.find((r) => r.provider === a.provider && r.name === a.name)?.at ?? null, | |
| 49 | writeAllowed: a.writeAllowed, | |
| 50 | callsToday: today.filter((r) => r.provider === a.provider && r.account === a.name).length, | |
| 51 | })); | |
| 52 | return ok({ accounts, today }); | |
| 53 | } |
src/http/mcp.ts +57
| @@ -22,6 +22,10 @@ import { mayView, mayViewId, sealedScope } from "../core/sealed.ts"; | ||
| 22 | 22 | import { listAccess } from "../actions/access_log.ts"; |
| 23 | 23 | import { changeCapacity, listCapacity } from "../actions/capacity.ts"; |
| 24 | 24 | import { changeLimit, listLimits } from "../actions/limits.ts"; |
| 25 | import { addAccount, removeAccount, renameAccount, reverify, rotateAccount } from "../actions/cloud.ts"; | |
| 26 | import { usesByVendor, type VendorUse } from "../cloud/declared.ts"; | |
| 27 | import { cloudOverview } from "../cloud/overview.ts"; | |
| 28 | import { PROVIDERS } from "../cloud/providers.ts"; | |
| 25 | 29 | import { changeQuotas, readQuotas } from "../actions/quotas.ts"; |
| 26 | 30 | import { changeSignup, readSignup } from "../actions/signup.ts"; |
| 27 | 31 | import { SIGNUP_MODES } from "../settings/signup.ts"; |
| @@ -2144,6 +2148,59 @@ const TOOLS: readonly Tool[] = [ | ||
| 2144 | 2148 | }); |
| 2145 | 2149 | }, |
| 2146 | 2150 | }, |
| 2151 | { | |
| 2152 | name: "cloud_accounts", | |
| 2153 | description: | |
| 2154 | "Cloud accounts, for a forge admin: each attached provider token (github, cloudflare, vercel) with what it is used for, when it was verified and last used, whether runs may write with it, and today's vendor calls (UTC day) — then every audit row of today, newest first. Never a token value. Needs a write-scope token. The same as Settings → Cloud accounts.", | |
| 2155 | inputSchema: { type: "object", properties: {} }, | |
| 2156 | writes: true, | |
| 2157 | async run(ctx, user) { | |
| 2158 | const uses = await usesByVendor(ctx.app.db, ctx.app.reposRoot, user).catch(() => new Map<string, VendorUse[]>()); | |
| 2159 | const seen = cloudOverview(ctx.app, user, uses); | |
| 2160 | if (!seen.ok) return bad("not_found", "no such tool here"); | |
| 2161 | return good(seen.value); | |
| 2162 | }, | |
| 2163 | }, | |
| 2164 | { | |
| 2165 | name: "cloud_account", | |
| 2166 | description: | |
| 2167 | "Change a cloud account, as a forge admin with an admin-scope token. `verb` is attach (checks `value` with the provider's read-only calls, then seals it), verify (re-checks the sealed token), rotate (checks a new `value`, then replaces it), rename (to `to`; refused when the account or the secret CLOUD_<PROVIDER>_<NAME> already exists) or remove. `provider` is github, cloudflare or vercel; `name` is lowercase letters, digits and underscores. The answer never contains the token. The same as Settings → Cloud accounts.", | |
| 2168 | inputSchema: { | |
| 2169 | type: "object", | |
| 2170 | properties: { | |
| 2171 | verb: { type: "string", enum: ["attach", "verify", "rotate", "rename", "remove"] }, | |
| 2172 | provider: { type: "string", enum: [...PROVIDERS] }, | |
| 2173 | name: { type: "string", maxLength: 32 }, | |
| 2174 | value: { type: "string", maxLength: 4096, description: "The token, for attach and rotate." }, | |
| 2175 | to: { type: "string", maxLength: 32, description: "The new name, for rename." }, | |
| 2176 | }, | |
| 2177 | required: ["verb", "provider", "name"], | |
| 2178 | }, | |
| 2179 | writes: true, | |
| 2180 | async run(ctx, user, args) { | |
| 2181 | if (!user.isAdmin) return bad("not_found", "no such tool here"); | |
| 2182 | if (!scopeAllows(ctx.auth.scope ?? "ro", "admin")) | |
| 2183 | return bad("forbidden", "changing cloud accounts needs an admin-scope token"); | |
| 2184 | const verb = str(args["verb"]); | |
| 2185 | const [provider, name] = [str(args["provider"]), str(args["name"])]; | |
| 2186 | const value = str(args["value"]); | |
| 2187 | const done = | |
| 2188 | verb === "attach" | |
| 2189 | ? await addAccount(ctx.app, user, provider, name, value) | |
| 2190 | : verb === "rotate" | |
| 2191 | ? await rotateAccount(ctx.app, user, provider, name, value) | |
| 2192 | : verb === "verify" | |
| 2193 | ? await reverify(ctx.app, user, provider, name) | |
| 2194 | : verb === "rename" | |
| 2195 | ? renameAccount(ctx.app, user, provider, name, str(args["to"])) | |
| 2196 | : verb === "remove" | |
| 2197 | ? removeAccount(ctx.app, user, provider, name) | |
| 2198 | : undefined; | |
| 2199 | if (done === undefined) return bad("bad_verb", "verb is attach, verify, rotate, rename or remove"); | |
| 2200 | if (!done.ok) return bad("refused", done.error); | |
| 2201 | return good({ says: `${verb} ${provider}/${name}: ${typeof done.value === "string" ? done.value : "done"}.` }); | |
| 2202 | }, | |
| 2203 | }, | |
| 2147 | 2204 | { |
| 2148 | 2205 | name: "quotas", |
| 2149 | 2206 | description: |
src/tui/forge.ts +9
| @@ -42,6 +42,7 @@ import { dashboardScreen } from "./screens/dashboard.ts"; | ||
| 42 | 42 | import { mayManageFlags } from "../core/flags.ts"; |
| 43 | 43 | import { accessScreen } from "./screens/access.ts"; |
| 44 | 44 | import { flagsScreen } from "./screens/flags.ts"; |
| 45 | import { cloudScreen } from "./screens/cloud.ts"; | |
| 45 | 46 | import { quotasScreen } from "./screens/quotas.ts"; |
| 46 | 47 | import { domainsScreen } from "./screens/domains.ts"; |
| 47 | 48 | import { resilienceLines, resilienceScreen } from "./screens/resilience.ts"; |
| @@ -353,6 +354,14 @@ export function forgeCommands(session: Session): (ctx: Ctx) => Command[] { | ||
| 353 | 354 | hint: "what one account may own, and who changed it", |
| 354 | 355 | run: () => ctx.push(quotasScreen(session, ctx)), |
| 355 | 356 | }); |
| 357 | // Cloud accounts, as Settings → Cloud accounts has them: a forge admin (#2199). | |
| 358 | if (session.user?.isAdmin === true) | |
| 359 | commands.push({ | |
| 360 | id: "cloud", | |
| 361 | title: "Cloud accounts", | |
| 362 | hint: "provider tokens, what they are used for, today's calls", | |
| 363 | run: () => ctx.push(cloudScreen(session, ctx)), | |
| 364 | }); | |
| 356 | 365 | // Each failure domain and its last drill, as Settings → Failure domains says it (#1945). |
| 357 | 366 | if (session.user?.isAdmin === true && session.user.kind !== "agent") |
| 358 | 367 | commands.push({ |
src/tui/screens/cloud.ts added +159
| @@ -0,0 +1,159 @@ | ||
| 1 | /** | |
| 2 | * Cloud accounts in the terminal, a forge admin's (#2199): each account with | |
| 3 | * what it is used for, when it was verified and last used, and today's vendor | |
| 4 | * calls, as Settings → Cloud accounts shows them. `a` attaches (verified, then | |
| 5 | * sealed), `v` re-verifies, `r` rotates, `n` renames, `x` removes — all through | |
| 6 | * `actions/cloud.ts`, so the checks are the page's. A token is typed masked and | |
| 7 | * is never drawn, kept or put in a note. Anyone else sees nothing and does nothing. | |
| 8 | */ | |
| 9 | ||
| 10 | import { addAccount, removeAccount, renameAccount, reverify, rotateAccount } from "../../actions/cloud.ts"; | |
| 11 | import { usesByVendor, type VendorUse } from "../../cloud/declared.ts"; | |
| 12 | import { cloudOverview } from "../../cloud/overview.ts"; | |
| 13 | import { LABEL, PROVIDERS } from "../../cloud/providers.ts"; | |
| 14 | import type { Ctx, Screen } from "../app/app.ts"; | |
| 15 | import { binding } from "../app/keys.ts"; | |
| 16 | import { style } from "../term/style.ts"; | |
| 17 | import { truncate } from "../term/width.ts"; | |
| 18 | import type { Session } from "../session.ts"; | |
| 19 | import { fill, len, split } from "../ui/layout.ts"; | |
| 20 | import { ListState, drawList, later, promptScreen, type ListRow } from "./common.ts"; | |
| 21 | ||
| 22 | const day = (at: string | null): string => (at === null ? "never" : at.slice(0, 16).replace("T", " ")); | |
| 23 | ||
| 24 | export function cloudScreen(f: Session, ctx: Ctx): Screen { | |
| 25 | const state = new ListState(); | |
| 26 | let note = ""; | |
| 27 | let uses: ReadonlyMap<string, readonly VendorUse[]> = new Map(); | |
| 28 | const read = () => cloudOverview(f.app, f.user, uses); | |
| 29 | const allowed = (): boolean => read().ok; | |
| 30 | const accounts = () => { | |
| 31 | const r = read(); | |
| 32 | return r.ok ? r.value.accounts : []; | |
| 33 | }; | |
| 34 | ||
| 35 | if (f.user?.isAdmin === true) | |
| 36 | void usesByVendor(f.app.db, f.app.reposRoot, f.user) | |
| 37 | .then((m) => { | |
| 38 | uses = m; | |
| 39 | ctx.invalidate(); | |
| 40 | }) | |
| 41 | .catch(() => undefined); | |
| 42 | ||
| 43 | const say = (text: string): void => { | |
| 44 | note = text; | |
| 45 | ctx.invalidate(); | |
| 46 | }; | |
| 47 | const settle = async (r: Promise<{ ok: boolean; value?: unknown; error?: unknown }>, good: string): Promise<void> => { | |
| 48 | const made = await r; | |
| 49 | say(made.ok ? good : String(made.error)); | |
| 50 | }; | |
| 51 | ||
| 52 | const rows = (): ListRow[] => | |
| 53 | accounts().map((a) => ({ | |
| 54 | title: `${a.provider}/${a.name}`, | |
| 55 | secondary: `verified ${day(a.verifiedAt)} · used ${day(a.lastUsedAt)} · ${a.callsToday} call${a.callsToday === 1 ? "" : "s"} today`, | |
| 56 | meta: a.writeAllowed ? "writes allowed" : "read-only", | |
| 57 | })); | |
| 58 | ||
| 59 | const attach = (parent: Screen): Screen => | |
| 60 | promptScreen(ctx, parent, { | |
| 61 | title: "Attach a cloud account", | |
| 62 | label: `provider/name, provider is ${PROVIDERS.join(", ")}`, | |
| 63 | placeholder: "github/main", | |
| 64 | onSubmit: (raw) => { | |
| 65 | const [provider = "", name = "", ...rest] = raw.split("/"); | |
| 66 | if (rest.length > 0 || name === "") return "Write it as provider/name."; | |
| 67 | if (!(PROVIDERS as readonly string[]).includes(provider)) return `A provider is ${PROVIDERS.join(", ")}.`; | |
| 68 | return later(ctx, () => | |
| 69 | promptScreen(ctx, parent, { | |
| 70 | title: `${LABEL[provider as (typeof PROVIDERS)[number]]} / ${name}`, | |
| 71 | label: "the token (checked, then sealed, never shown)", | |
| 72 | mask: true, | |
| 73 | onSubmit: (value) => settle(addAccount(f.app, f.user, provider, name, value), `Sealed ${provider}/${name}.`), | |
| 74 | }), | |
| 75 | ); | |
| 76 | }, | |
| 77 | }); | |
| 78 | ||
| 79 | const screen: Screen = { | |
| 80 | title: "Cloud accounts", | |
| 81 | keys: [ | |
| 82 | binding(["j", "k"], "move cursor"), | |
| 83 | binding(["a"], "attach"), | |
| 84 | binding(["v"], "verify"), | |
| 85 | binding(["r"], "rotate"), | |
| 86 | binding(["n"], "rename"), | |
| 87 | binding(["x"], "remove"), | |
| 88 | ], | |
| 89 | render(area, s) { | |
| 90 | const c = ctx.theme.c; | |
| 91 | const r = read(); | |
| 92 | const [head, list] = split(area, "v", [len(1), fill(1)]); | |
| 93 | const selected = r.ok ? r.value.accounts[state.index] : undefined; | |
| 94 | const text = | |
| 95 | note !== "" | |
| 96 | ? note | |
| 97 | : !r.ok | |
| 98 | ? "Cloud accounts are a forge admin's." | |
| 99 | : selected !== undefined | |
| 100 | ? `${selected.summary} ${selected.usedFor}` | |
| 101 | : "No account attached. Press a to attach one."; | |
| 102 | s.text(head!.x, head!.y, truncate(text, head!.w), style(c.mutedFg, c.bg), head!.x + head!.w); | |
| 103 | drawList(s, list!, c, state, rows()); | |
| 104 | }, | |
| 105 | key(e) { | |
| 106 | if (state.key(e, accounts().length)) return true; | |
| 107 | if (e.ctrl || e.alt || !allowed()) return false; | |
| 108 | if (e.key === "a") { | |
| 109 | ctx.push(attach(screen)); | |
| 110 | return true; | |
| 111 | } | |
| 112 | const a = accounts()[state.index]; | |
| 113 | if (a === undefined) return false; | |
| 114 | const { provider, name } = a; | |
| 115 | if (e.key === "v") { | |
| 116 | say(`Verifying ${provider}/${name}…`); | |
| 117 | void settle(reverify(f.app, f.user, provider, name), `Verified ${provider}/${name}.`); | |
| 118 | } else if (e.key === "r") | |
| 119 | ctx.push( | |
| 120 | promptScreen(ctx, screen, { | |
| 121 | title: `Rotate ${provider}/${name}`, | |
| 122 | label: "the new token (checked, then sealed)", | |
| 123 | mask: true, | |
| 124 | onSubmit: (value) => settle(rotateAccount(f.app, f.user, provider, name, value), `Rotated ${provider}/${name}.`), | |
| 125 | }), | |
| 126 | ); | |
| 127 | else if (e.key === "n") | |
| 128 | ctx.push( | |
| 129 | promptScreen(ctx, screen, { | |
| 130 | title: `Rename ${provider}/${name}`, | |
| 131 | label: "new name", | |
| 132 | onSubmit: (to) => { | |
| 133 | const r = renameAccount(f.app, f.user, provider, name, to); | |
| 134 | if (!r.ok) return r.error; | |
| 135 | say(`Renamed to ${provider}/${to}.`); | |
| 136 | return undefined; | |
| 137 | }, | |
| 138 | }), | |
| 139 | ); | |
| 140 | else if (e.key === "x") | |
| 141 | ctx.push( | |
| 142 | promptScreen(ctx, screen, { | |
| 143 | title: `Remove ${provider}/${name}`, | |
| 144 | label: `type ${name} to remove it and its sealed token`, | |
| 145 | onSubmit: (typed) => { | |
| 146 | if (typed !== name) return `Type ${name} to confirm.`; | |
| 147 | const r = removeAccount(f.app, f.user, provider, name); | |
| 148 | if (!r.ok) return r.error; | |
| 149 | say(`Removed ${provider}/${name}.`); | |
| 150 | return undefined; | |
| 151 | }, | |
| 152 | }), | |
| 153 | ); | |
| 154 | else return false; | |
| 155 | return true; | |
| 156 | }, | |
| 157 | }; | |
| 158 | return screen; | |
| 159 | } |
test/cloud-doors.test.ts added +213
| @@ -0,0 +1,213 @@ | ||
| 1 | /** | |
| 2 | * Cloud accounts at the terminal and MCP doors (#2199): a forge admin lists, | |
| 3 | * attaches, verifies, rotates, renames and removes through the functions the | |
| 4 | * page uses; anyone else is refused at both; no frame and no result carries a | |
| 5 | * token; a rename is refused when the secret name is taken even with no row. | |
| 6 | */ | |
| 7 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 8 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 9 | import { tmpdir } from "node:os"; | |
| 10 | import { join } from "node:path"; | |
| 11 | import { createApp, type App } from "../src/app.ts"; | |
| 12 | import { addAccount, net, renameAccount } from "../src/actions/cloud.ts"; | |
| 13 | import { recordAudit } from "../src/core/cloud_audit.ts"; | |
| 14 | import { getCloudAccount, listCloudAccounts } from "../src/core/cloud_account.ts"; | |
| 15 | import { listSecretNames, setSecret } from "../src/core/secret_store.ts"; | |
| 16 | import { createToken } from "../src/core/tokens.ts"; | |
| 17 | import { createUser, getUserByName, type User } from "../src/core/users.ts"; | |
| 18 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 19 | import { createForgeApp } from "../src/tui/forge.ts"; | |
| 20 | import { cloudScreen } from "../src/tui/screens/cloud.ts"; | |
| 21 | import { Session } from "../src/tui/session.ts"; | |
| 22 | import { headlessTerminal } from "../src/tui/term/terminal.ts"; | |
| 23 | import { quietLogs } from "./helpers/globals.ts"; | |
| 24 | import { settleApp } from "./helpers/tui.ts"; | |
| 25 | ||
| 26 | quietLogs(); | |
| 27 | ||
| 28 | const T1 = "ghp_first9Zq4TxLm7Kp2Vb8nWcR5"; | |
| 29 | const T2 = "ghp_second7Hs3DfGj6Lm1Qw0Zx8"; | |
| 30 | net.fetch = async (_url, init) => { | |
| 31 | const auth = String((init?.headers as Record<string, string> | undefined)?.authorization ?? ""); | |
| 32 | return new Response(JSON.stringify({ login: "octo" }), { | |
| 33 | status: auth.includes(T1) || auth.includes(T2) ? 200 : 401, | |
| 34 | }); | |
| 35 | }; | |
| 36 | ||
| 37 | describe("cloud accounts at the terminal and MCP doors", () => { | |
| 38 | let dataDir = ""; | |
| 39 | let app: App; | |
| 40 | let servers: Servers; | |
| 41 | let alice: User; | |
| 42 | let adminToken = ""; | |
| 43 | let rwToken = ""; | |
| 44 | let bobToken = ""; | |
| 45 | ||
| 46 | beforeAll(async () => { | |
| 47 | dataDir = await mkdtemp(join(tmpdir(), "forge-cloud-doors-")); | |
| 48 | app = createApp({ dataDir, port: 0 }); | |
| 49 | servers = await serve(app); | |
| 50 | for (const [username, adminIfFirst] of [["alice", true], ["bob", false]] as const) { | |
| 51 | const made = await createUser(app.db, { trusted: true, username, email: `${username}@f.test`, password: "a-good-passphrase", adminIfFirst }); | |
| 52 | if (!made.ok) throw new Error(made.error); | |
| 53 | } | |
| 54 | alice = getUserByName(app.db, "alice")!; | |
| 55 | const bob = getUserByName(app.db, "bob")!; | |
| 56 | adminToken = createToken(app.db, alice.id, "admin", "admin").token; | |
| 57 | rwToken = createToken(app.db, alice.id, "rw", "rw").token; | |
| 58 | bobToken = createToken(app.db, bob.id, "admin", "admin").token; | |
| 59 | }); | |
| 60 | afterAll(async () => { | |
| 61 | servers.stop(); | |
| 62 | app.close(); | |
| 63 | await rm(dataDir, { recursive: true, force: true }); | |
| 64 | }); | |
| 65 | ||
| 66 | const mcp = async (token: string, name: string, args: unknown = {}) => { | |
| 67 | const res = await fetch(`${servers.url}/mcp`, { | |
| 68 | method: "POST", | |
| 69 | headers: { "content-type": "application/json", accept: "application/json, text/event-stream", authorization: `Bearer ${token}` }, | |
| 70 | body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/call", params: { name, arguments: args } }), | |
| 71 | }); | |
| 72 | const body = (await res.json()) as any; | |
| 73 | const text: string = body.result.content[0].text; | |
| 74 | return { error: body.result.isError === true, text }; | |
| 75 | }; | |
| 76 | ||
| 77 | const boot = (username: string) => { | |
| 78 | const user = getUserByName(app.db, username)!; | |
| 79 | const { app: tui } = createForgeApp(app, { sshUsername: "t", remoteAddress: "127.0.0.1", user }, { terminal: headlessTerminal(160, 32), exitHooks: false }); | |
| 80 | tui.start(); | |
| 81 | const key = (k: string) => tui.dispatch({ type: "key", key: k, ctrl: false, alt: false, shift: false }); | |
| 82 | return { | |
| 83 | text: () => tui.screen.toText(), | |
| 84 | press: async (...keys: string[]) => { | |
| 85 | keys.forEach(key); | |
| 86 | await settleApp(tui); | |
| 87 | }, | |
| 88 | type: async (t: string) => { | |
| 89 | for (const ch of t) key(ch); | |
| 90 | await settleApp(tui); | |
| 91 | }, | |
| 92 | palette: async (q: string) => { | |
| 93 | tui.dispatch({ type: "key", key: "k", ctrl: true, alt: false, shift: false }); | |
| 94 | await settleApp(tui); | |
| 95 | for (const ch of q) key(ch); | |
| 96 | await settleApp(tui); | |
| 97 | }, | |
| 98 | stop: () => tui.stop(), | |
| 99 | }; | |
| 100 | }; | |
| 101 | ||
| 102 | test("terminal: an admin attaches, verifies, rotates, renames and removes, and no frame holds a token", async () => { | |
| 103 | const h = boot("alice"); | |
| 104 | await h.palette("Cloud accounts"); | |
| 105 | await h.press("enter"); | |
| 106 | expect(h.text()).toContain("No account attached"); | |
| 107 | ||
| 108 | await h.press("a"); | |
| 109 | await h.type("github/main"); | |
| 110 | await h.press("enter"); | |
| 111 | await h.type(T1); | |
| 112 | expect(h.text()).not.toContain(T1); | |
| 113 | await h.press("enter"); | |
| 114 | expect(listCloudAccounts(app.db).map((a) => a.name)).toEqual(["main"]); | |
| 115 | expect(listSecretNames(app.db, "forge").map((s) => s.name)).toEqual(["CLOUD_GITHUB_MAIN"]); | |
| 116 | expect(h.text()).toContain("github/main"); | |
| 117 | expect(h.text()).toContain("1 call today"); | |
| 118 | expect(h.text()).not.toContain(T1); | |
| 119 | ||
| 120 | await h.press("v"); | |
| 121 | expect(h.text()).toContain("Verified github/main"); | |
| 122 | // The attach made one gated vendor call, the verify one more, and this is a third. | |
| 123 | recordAudit(app.db, { provider: "github", account: "main", purpose: "verify", target: "https://api.github.com/user", bytesOut: 0, allowed: true }); | |
| 124 | await h.press("j", "k"); | |
| 125 | expect(h.text()).toContain("3 calls today"); | |
| 126 | ||
| 127 | await h.press("r"); | |
| 128 | await h.type(T2); | |
| 129 | expect(h.text()).not.toContain(T2); | |
| 130 | await h.press("enter"); | |
| 131 | expect(h.text()).toContain("Rotated github/main"); | |
| 132 | expect(h.text()).not.toContain(T2); | |
| 133 | ||
| 134 | await h.press("n"); | |
| 135 | await h.type("work"); | |
| 136 | await h.press("enter"); | |
| 137 | expect(getCloudAccount(app.db, "github", "work")).toBeDefined(); | |
| 138 | expect(getCloudAccount(app.db, "github", "main")).toBeUndefined(); | |
| 139 | ||
| 140 | await h.press("x"); | |
| 141 | await h.type("work"); | |
| 142 | await h.press("enter"); | |
| 143 | expect(listCloudAccounts(app.db)).toEqual([]); | |
| 144 | expect(listSecretNames(app.db, "forge")).toEqual([]); | |
| 145 | h.stop(); | |
| 146 | }); | |
| 147 | ||
| 148 | test("terminal: a non-admin has no command and, driving the screen, does nothing", async () => { | |
| 149 | const h = boot("bob"); | |
| 150 | await h.palette("Cloud accounts"); | |
| 151 | expect(h.text()).not.toContain("provider tokens"); | |
| 152 | h.stop(); | |
| 153 | ||
| 154 | const bob = getUserByName(app.db, "bob")!; | |
| 155 | const session = new Session(app, { sshUsername: "t", remoteAddress: "127.0.0.1", user: bob }); | |
| 156 | const pushed: unknown[] = []; | |
| 157 | const screen = cloudScreen(session, { push: (s: unknown) => pushed.push(s), invalidate() {} } as never); | |
| 158 | for (const k of ["a", "v", "r", "n", "x"]) | |
| 159 | screen.key?.({ type: "key", key: k, ctrl: false, alt: false, shift: false } as never); | |
| 160 | expect(pushed).toEqual([]); | |
| 161 | }); | |
| 162 | ||
| 163 | test("MCP: an admin lists, attaches, verifies, rotates, renames and removes; no result holds a token", async () => { | |
| 164 | const attached = await mcp(adminToken, "cloud_account", { verb: "attach", provider: "github", name: "main", value: T1 }); | |
| 165 | expect(attached.error).toBe(false); | |
| 166 | expect(attached.text).not.toContain(T1); | |
| 167 | const listed = await mcp(adminToken, "cloud_accounts"); | |
| 168 | const value = JSON.parse(listed.text); | |
| 169 | expect(value.accounts).toHaveLength(1); | |
| 170 | expect(value.accounts[0]).toMatchObject({ provider: "github", name: "main", callsToday: value.today.length }); | |
| 171 | expect(value.today.length).toBeGreaterThan(0); | |
| 172 | expect(value.today[0]).toMatchObject({ provider: "github", account: "main" }); | |
| 173 | expect(listed.text).not.toContain(T1); | |
| 174 | ||
| 175 | for (const args of [ | |
| 176 | { verb: "verify", provider: "github", name: "main" }, | |
| 177 | { verb: "rotate", provider: "github", name: "main", value: T2 }, | |
| 178 | { verb: "rename", provider: "github", name: "main", to: "work" }, | |
| 179 | ]) { | |
| 180 | const r = await mcp(adminToken, "cloud_account", args); | |
| 181 | expect(r.error).toBe(false); | |
| 182 | expect(r.text).not.toContain(T1); | |
| 183 | expect(r.text).not.toContain(T2); | |
| 184 | } | |
| 185 | expect(getCloudAccount(app.db, "github", "work")).toBeDefined(); | |
| 186 | expect((await mcp(adminToken, "cloud_account", { verb: "remove", provider: "github", name: "work" })).error).toBe(false); | |
| 187 | expect(listCloudAccounts(app.db)).toEqual([]); | |
| 188 | }); | |
| 189 | ||
| 190 | test("MCP: a non-admin is refused, and a write-scope token cannot change accounts", async () => { | |
| 191 | for (const r of [ | |
| 192 | await mcp(bobToken, "cloud_accounts"), | |
| 193 | await mcp(bobToken, "cloud_account", { verb: "attach", provider: "github", name: "main", value: T1 }), | |
| 194 | ]) { | |
| 195 | expect(r.error).toBe(true); | |
| 196 | expect(r.text).not.toContain(T1); | |
| 197 | } | |
| 198 | expect(listCloudAccounts(app.db)).toEqual([]); | |
| 199 | const weak = await mcp(rwToken, "cloud_account", { verb: "attach", provider: "github", name: "main", value: T1 }); | |
| 200 | expect(weak.error).toBe(true); | |
| 201 | expect(listCloudAccounts(app.db)).toEqual([]); | |
| 202 | }); | |
| 203 | ||
| 204 | test("a rename is refused when the secret exists, with no account row", async () => { | |
| 205 | expect((await addAccount(app, alice, "github", "main", T1)).ok).toBe(true); | |
| 206 | expect(setSecret(app.db, dataDir, "forge", "", "CLOUD_GITHUB_TAKEN", "an-unrelated-sealed-value").ok).toBe(true); | |
| 207 | expect(getCloudAccount(app.db, "github", "taken")).toBeUndefined(); | |
| 208 | const r = renameAccount(app, alice, "github", "main", "taken"); | |
| 209 | expect(r.ok).toBe(false); | |
| 210 | expect(getCloudAccount(app.db, "github", "main")).toBeDefined(); | |
| 211 | expect(listSecretNames(app.db, "forge").map((s) => s.name)).toEqual(["CLOUD_GITHUB_MAIN", "CLOUD_GITHUB_TAKEN"]); | |
| 212 | }); | |
| 213 | }); |
test/mcp.test.ts +2
| @@ -212,6 +212,8 @@ describe("the forge over MCP", () => { | ||
| 212 | 212 | "system", |
| 213 | 213 | "contribute", |
| 214 | 214 | "limits", |
| 215 | "cloud_accounts", | |
| 216 | "cloud_account", | |
| 215 | 217 | "quotas", |
| 216 | 218 | "signup", |
| 217 | 219 | "boards", |
new above
offline