Public: anyone can read this; the crew can change it.
See and manage everything on any cloud: provider ▸ account ▸ project ▸ resources, the cloud's own view, a console that always works #17253
Merged 33h 50m ago, live in production
scout/forge:scout/17151-see-and-manage-everything-on-any-cloud-p → main 0 commits fork 3 threads · 3 unresolved
13 files
changed +464
−13
- docs/features/infra.md
- src/actions/machine_view.ts
- src/cloud/droplet.ts
- src/cloud/hetzner.ts
- src/cloud/machine.ts
- src/cloud/provider_detail.ts
- src/http/infra_routes.ts
- src/http/lazy_lists.ts
- src/http/mcp/tools/infra.ts
- src/tui/screens/infra.ts
- src/web/pages/infra.ts
- test/machine-view.test.ts
- test/pinned-ceiling.test.ts
docs/features/infra.md +4
| @@ -90,5 +90,9 @@ Not yet: the page, committing the compose file, sleep and wake, backups, and a S | ||
| 90 | 90 | |
| 91 | 91 | Every DigitalOcean and Hetzner account is read into provider ▸ account ▸ project ▸ resource every five minutes (the `cloud-projects` duty), and on demand (Read now under Projects on /infra, `P` in the terminal's Infra). GETs only, through the audited fetch (`gatedFetch`), with the account's sealed key. DigitalOcean: `GET /v2/projects`, then each project's `/v2/projects/{id}/resources` (URNs `do:droplet:…`, `do:volume:…`), joined with `/v2/droplets` and `/v2/volumes`; a droplet or volume in no project's list is filed under the default. Hetzner: one token is one project, named by the account's project label (else its name); servers, volumes, firewalls and primary IPs, each with its `forge` label. Kept in `cloud_project` and `cloud_resource`, columns named for the OpenTelemetry `cloud.*`/`host.*` attributes (`cloud_provider`, `cloud_account_id`, `cloud_region`, `cloud_resource_id`, `host_type`). Whether a thing is the forge's is the cloud watch's judgement; the rest is "found", named and never touched. /infra's Projects section lists each project with its resources, its monthly cost at list price (a thing the catalog cannot price is counted, not guessed), and made by the forge against found; the terminal's Infra lists the same; `infra` `list` carries `projects` (ten at most, five strays named each) with a `says` line. |
| 92 | 92 | |
| 93 | ## What the cloud says of a machine (#17151) | |
| 94 | ||
| 95 | Every machine that exists at its provider has an **At the cloud** button on /infra, `v` on its row in the terminal, and `infra` `{action: "cloud", id: <name>}` in the MCP. Each asks the provider's own API when it is opened (DigitalOcean's droplet and its actions, Hetzner's server and its actions), so it answers when the box's agent is dead: power state, size, image, region, public and private IP, created time, attached volumes, and the provider's recent actions ("power_on · completed · 2026-12-01 17:44"). Admin only, read-only; `src/actions/machine_view.ts` gives the page, the terminal and the tool the same read (`src/web/pages/infra.ts` `cloudPage`, `src/tui/screens/infra.ts`). A cloud whose driver does not read it yet says so. | |
| 96 | ||
| 93 | 97 | **Provider console (Hetzner):** a Hetzner machine on /infra has a Provider console button (`o` in the terminal's Infra). It asks Hetzner for the server's own console (`POST /v1/servers/{id}/actions/request_console`, through the audited fetch with the account's sealed key) and shows its `wss://` address and password once, on a page no cache keeps: good for about a minute, kept nowhere. The way in when a box's agent is silent and its firewall lets no SSH through. `src/cloud/provider_console.ts`. |
| 94 | 98 |
src/actions/machine_view.ts added +44
| @@ -0,0 +1,44 @@ | ||
| 1 | /** | |
| 2 | * The cloud's own view of a forge's machine (#17151, V2): asked of the provider's API each time it | |
| 3 | * is opened, so it answers when the box's agent is dead. Admin only, read-only (the account's | |
| 4 | * token is opened for a read, never a write). The page, the terminal screen, the API and the | |
| 5 | * `infra` tool (`action: cloud`) all call `machineView`. | |
| 6 | */ | |
| 7 | ||
| 8 | import type { App } from "../app.ts"; | |
| 9 | import { driverOf } from "../cloud/drivers.ts"; | |
| 10 | import { viewOf } from "../cloud/machine.ts"; | |
| 11 | import { viewSays, type ProviderView } from "../cloud/provider_detail.ts"; | |
| 12 | import { getPinned } from "../core/pinned.ts"; | |
| 13 | import type { User } from "../core/users.ts"; | |
| 14 | import { err, ok, type Result } from "../kernel/result.ts"; | |
| 15 | import { admin, withCloud } from "./pinned.ts"; | |
| 16 | ||
| 17 | export interface MachineView { | |
| 18 | readonly name: string; | |
| 19 | readonly target: string; | |
| 20 | readonly account: string; | |
| 21 | readonly says: string; | |
| 22 | readonly view: ProviderView; | |
| 23 | } | |
| 24 | ||
| 25 | /** Why a view was not given: the route maps `missing` to 404 and every other to 422. */ | |
| 26 | export interface ViewRefusal { | |
| 27 | readonly reason: "denied" | "missing" | "retired" | "no_machine" | "unread" | "refused"; | |
| 28 | readonly message: string; | |
| 29 | } | |
| 30 | ||
| 31 | const no = (reason: ViewRefusal["reason"], message: string): Result<MachineView, ViewRefusal> => err({ reason, message }); | |
| 32 | ||
| 33 | export async function machineView(app: App, actor: User | undefined, name: string): Promise<Result<MachineView, ViewRefusal>> { | |
| 34 | const who = admin(actor); | |
| 35 | if (!who.ok) return no("denied", who.error); | |
| 36 | const p = getPinned(app.db, name); | |
| 37 | if (!p) return no("missing", "No such pinned forge."); | |
| 38 | if (p.state === "retired") return no("retired", `${name} is retired: its cloud holds no machine to show.`); | |
| 39 | if (p.remote === "") return no("no_machine", `${name} has no machine at its cloud yet.`); | |
| 40 | const seen = await withCloud(app, who.value, p, "view", (f, token) => viewOf(driverOf(p.target), f, token, p.remote), false); | |
| 41 | if (!seen.ok) return no("refused", seen.error); | |
| 42 | if (seen.value === undefined) return no("unread", `The cloud's own view of ${p.target} is not read here yet.`); | |
| 43 | return ok({ name, target: p.target, account: p.account, says: viewSays(name, seen.value), view: seen.value }); | |
| 44 | } |
src/cloud/droplet.ts +6
| @@ -9,6 +9,7 @@ | ||
| 9 | 9 | * POST /volumes/{id}/snapshots, GET and DELETE /snapshots/{id} (#17072). |
| 10 | 10 | */ |
| 11 | 11 | |
| 12 | import { droplet as dropletView } from "./provider_detail.ts"; | |
| 12 | 13 | import { digitaloceanFirewall } from "./firewall.ts"; |
| 13 | 14 | import { dropThrowawayKey, makeThrowawayKey } from "./throwaway_key.ts"; |
| 14 | 15 | import { bootConfig as boot, forgeTag, ipv4, paged, volumeName, type Booted, type Driver, type Found } from "./machine.ts"; |
| @@ -114,6 +115,11 @@ export const digitalocean: Driver = { | ||
| 114 | 115 | const v = (await call("GET", `/volumes/${volume}`)).volume; |
| 115 | 116 | return Array.isArray(v?.droplet_ids) ? v.droplet_ids.length === 0 : true; |
| 116 | 117 | }, |
| 118 | async viewMachine(call, remote) { | |
| 119 | const d = (await call("GET", `/droplets/${remote}`)).droplet; | |
| 120 | const a = (await call("GET", `/droplets/${remote}/actions?per_page=10`)).actions; | |
| 121 | return dropletView(d, a); | |
| 122 | }, | |
| 117 | 123 | async readMachine(call, remote) { |
| 118 | 124 | const d = (await call("GET", `/droplets/${remote}`)).droplet; |
| 119 | 125 | const v4: unknown[] = Array.isArray(d?.networks?.v4) ? d.networks.v4 : []; |
src/cloud/hetzner.ts +6
| @@ -11,6 +11,7 @@ | ||
| 11 | 11 | * /images/{id} (a snapshot of the server's disk, #17072). |
| 12 | 12 | */ |
| 13 | 13 | |
| 14 | import { server as serverView } from "./provider_detail.ts"; | |
| 14 | 15 | import { hetznerFirewall } from "./firewall.ts"; |
| 15 | 16 | import { dropThrowawayKey, makeThrowawayKey } from "./throwaway_key.ts"; |
| 16 | 17 | import { bootConfig as boot, ipv4, numericId, paged, volumeName, type Booted, type Driver, type Found } from "./machine.ts"; |
| @@ -95,6 +96,11 @@ export const hetzner: Driver = { | ||
| 95 | 96 | const v = (await call("GET", `/volumes/${numericId(volume, "volume")}`)).volume; |
| 96 | 97 | return v?.server === null || v?.server === undefined; |
| 97 | 98 | }, |
| 99 | async viewMachine(call, remote) { | |
| 100 | const s = (await call("GET", `/servers/${numericId(remote, "machine")}`)).server; | |
| 101 | const a = (await call("GET", `/servers/${numericId(remote, "machine")}/actions?sort=id:desc&per_page=10`)).actions; | |
| 102 | return serverView(s, a); | |
| 103 | }, | |
| 98 | 104 | async readMachine(call, remote) { |
| 99 | 105 | const s = (await call("GET", `/servers/${numericId(remote, "machine")}`)).server; |
| 100 | 106 | return { status: String(s?.status ?? ""), address: ipv4(s?.public_net?.ipv4?.ip) }; |
src/cloud/machine.ts +16 −3
| @@ -18,6 +18,7 @@ | ||
| 18 | 18 | * a failure half-way leaves a row a retry can finish, never an orphan. |
| 19 | 19 | */ |
| 20 | 20 | |
| 21 | import type { ProviderView } from "./provider_detail.ts"; | |
| 21 | 22 | import { asciiBoot, yamlString } from "./boot_ascii.ts"; |
| 22 | 23 | import { BUN_PIN } from "../kernel/bun_pin.ts"; |
| 23 | 24 | import type { Fetch } from "./providers.ts"; |
| @@ -237,6 +238,8 @@ export interface Driver { | ||
| 237 | 238 | /** Whether the volume is attached to no machine: a cloud detaches it after a delete answers, and refuses a new machine with it until then. */ |
| 238 | 239 | volumeFree(call: Call, volume: string): Promise<boolean>; |
| 239 | 240 | readMachine(call: Call, remote: string): Promise<{ status: string; address: string }>; |
| 241 | /** What the cloud says of the machine and its recent actions (#17151), where it can be read. */ | |
| 242 | viewMachine?(call: Call, remote: string): Promise<ProviderView>; | |
| 240 | 243 | /** |
| 241 | 244 | * The id of the cloud project called `name`, refused in plain words when there is none. Only a |
| 242 | 245 | * cloud whose resources land in a default project has it (DigitalOcean); a Hetzner token already |
| @@ -500,12 +503,22 @@ function isPublic(a: string): boolean { | ||
| 500 | 503 | /** Only a well-formed, public IPv4 address is ever kept. */ |
| 501 | 504 | export const ipv4 = (v: unknown): string => (typeof v === "string" && IPV4.test(v) && isPublic(v) ? v : ""); |
| 502 | 505 | |
| 503 | /** Read the machine back: its status and public IPv4 (empty while none is assigned). */ | |
| 504 | export async function read(d: Driver, f: Fetch, token: string, remote: string): Promise<{ status: string; address: string }> { | |
| 506 | /** Both reads of one machine go through here: only a well-formed id is ever put in a path. */ | |
| 507 | function onMachine<T>(remote: string, go: () => Promise<T>): Promise<T> { | |
| 505 | 508 | if (!ID.test(remote)) throw new Error("this forge has no machine to read"); |
| 506 | return d.readMachine(caller(d, f, token), remote); | |
| 509 | return go(); | |
| 507 | 510 | } |
| 508 | 511 | |
| 512 | /** | |
| 513 | * The cloud's own view of a machine, or `undefined` when this cloud's driver does not read one yet. | |
| 514 | */ | |
| 515 | export const viewOf = async (d: Driver, f: Fetch, token: string, remote: string): Promise<ProviderView | undefined> => | |
| 516 | onMachine(remote, async () => d.viewMachine?.(caller(d, f, token), remote)); | |
| 517 | ||
| 518 | /** Read the machine back: its status and public IPv4 (empty while none is assigned). */ | |
| 519 | export const read = async (d: Driver, f: Fetch, token: string, remote: string): Promise<{ status: string; address: string }> => | |
| 520 | onMachine(remote, () => d.readMachine(caller(d, f, token), remote)); | |
| 521 | ||
| 509 | 522 | /** |
| 510 | 523 | * What the provider holds under a forge's label that no row owns: the machines and volumes a lost |
| 511 | 524 | * create, a deleted row or a hand at the console left behind, each one billing. Asked of the |
src/cloud/provider_detail.ts added +106
| @@ -0,0 +1,106 @@ | ||
| 1 | /** | |
| 2 | * The cloud's own view of a machine (#17151, V2): what the provider says about it, read from its | |
| 3 | * API and not from the box, so it answers when the agent is dead. Plain data in, plain data out: | |
| 4 | * `droplet` and `server` take what DigitalOcean and Hetzner answered and return a `ProviderView`; | |
| 5 | * a malformed field is dropped, never guessed at. The drivers (`droplet.ts`, `hetzner.ts`) make the | |
| 6 | * two calls each needs; `actions/machine_view.ts` opens the account's token and runs them. | |
| 7 | */ | |
| 8 | ||
| 9 | /** One thing the provider did to the machine: "power_on", "completed", when. */ | |
| 10 | export interface ProviderAction { | |
| 11 | readonly what: string; | |
| 12 | readonly state: string; | |
| 13 | readonly at: string; | |
| 14 | } | |
| 15 | ||
| 16 | export interface ProviderView { | |
| 17 | /** off, active/running, and so on, in the provider's words. */ | |
| 18 | readonly power: string; | |
| 19 | readonly size: string; | |
| 20 | readonly image: string; | |
| 21 | readonly region: string; | |
| 22 | readonly publicIp: string; | |
| 23 | readonly privateIp: string; | |
| 24 | readonly createdAt: string; | |
| 25 | /** Ids of the volumes attached to it. */ | |
| 26 | readonly volumes: readonly string[]; | |
| 27 | /** The provider's recent actions, newest first, at most `MAX_ACTIONS`. */ | |
| 28 | readonly actions: readonly ProviderAction[]; | |
| 29 | } | |
| 30 | ||
| 31 | export const MAX_ACTIONS = 8; | |
| 32 | ||
| 33 | const str = (v: unknown, max = 80): string => (typeof v === "string" ? v.slice(0, max) : ""); | |
| 34 | const IP = /^(?:\d{1,3}\.){3}\d{1,3}$/; | |
| 35 | const ip = (v: unknown): string => (typeof v === "string" && IP.test(v) ? v : ""); | |
| 36 | const list = (v: unknown): any[] => (Array.isArray(v) ? v : []); | |
| 37 | const ids = (v: unknown): string[] => | |
| 38 | list(v).flatMap((x) => (typeof x === "number" ? [String(x)] : typeof x === "string" && /^[A-Za-z0-9-]{1,64}$/.test(x) ? [x] : [])).slice(0, 20); | |
| 39 | ||
| 40 | /** | |
| 41 | * What the provider's action list answered, newest first; the clock is the one the provider gave. | |
| 42 | */ | |
| 43 | function actionsOf(rows: unknown, what: (a: any) => unknown, state: (a: any) => unknown, at: (a: any) => unknown): ProviderAction[] { | |
| 44 | return list(rows) | |
| 45 | .map((a) => ({ what: str(what(a), 40), state: str(state(a), 20), at: str(at(a), 40) })) | |
| 46 | .filter((a) => a.what !== "") | |
| 47 | .sort((a, b) => b.at.localeCompare(a.at)) | |
| 48 | .slice(0, MAX_ACTIONS); | |
| 49 | } | |
| 50 | ||
| 51 | /** DigitalOcean: `GET /droplets/{id}` and `GET /droplets/{id}/actions`. */ | |
| 52 | export function droplet(d: any, actions: unknown): ProviderView { | |
| 53 | const v4 = list(d?.networks?.v4); | |
| 54 | const of = (type: string): string => ip(v4.find((n) => n?.type === type)?.ip_address); | |
| 55 | const image = d?.image; | |
| 56 | return { | |
| 57 | power: str(d?.status, 20), | |
| 58 | size: str(d?.size_slug), | |
| 59 | image: str(image?.description) || [str(image?.distribution), str(image?.name)].filter(Boolean).join(" "), | |
| 60 | region: str(d?.region?.slug, 20), | |
| 61 | publicIp: of("public"), | |
| 62 | privateIp: of("private"), | |
| 63 | createdAt: str(d?.created_at, 40), | |
| 64 | volumes: ids(d?.volume_ids), | |
| 65 | actions: actionsOf(actions, (a) => a?.type, (a) => a?.status, (a) => a?.completed_at ?? a?.started_at), | |
| 66 | }; | |
| 67 | } | |
| 68 | ||
| 69 | /** Hetzner: `GET /servers/{id}` and `GET /servers/{id}/actions`. */ | |
| 70 | export function server(s: any, actions: unknown): ProviderView { | |
| 71 | return { | |
| 72 | power: str(s?.status, 20), | |
| 73 | size: str(s?.server_type?.name), | |
| 74 | image: str(s?.image?.description) || str(s?.image?.name), | |
| 75 | region: str(s?.datacenter?.name, 20) || str(s?.datacenter?.location?.name, 20), | |
| 76 | publicIp: ip(s?.public_net?.ipv4?.ip), | |
| 77 | privateIp: ip(list(s?.private_net)[0]?.ip), | |
| 78 | createdAt: str(s?.created, 40), | |
| 79 | volumes: ids(s?.volumes), | |
| 80 | actions: actionsOf(actions, (a) => a?.command, (a) => a?.status, (a) => a?.finished ?? a?.started), | |
| 81 | }; | |
| 82 | } | |
| 83 | ||
| 84 | /** The view in one sentence, the same at every door. */ | |
| 85 | export function viewSays(name: string, v: ProviderView): string { | |
| 86 | const bits = [`${name} is ${v.power || "in a state the provider did not say"} at its cloud`, v.size, v.image, v.region, v.publicIp === "" ? "no public IP" : v.publicIp]; | |
| 87 | const last = v.actions[0]; | |
| 88 | return `${bits.filter(Boolean).join(" · ")}${last === undefined ? "" : `; last action ${last.what} ${last.state}${last.at === "" ? "" : ` ${last.at.slice(0, 16).replace("T", " ")}`}`}.`; | |
| 89 | } | |
| 90 | ||
| 91 | /** The labelled facts of a view, in one order for every door; empty ones say so in words. */ | |
| 92 | export function viewRows(v: ProviderView): [string, string][] { | |
| 93 | return [ | |
| 94 | ["Power", v.power || "not said"], | |
| 95 | ["Size", v.size || "not said"], | |
| 96 | ["Image", v.image || "not said"], | |
| 97 | ["Region", v.region || "not said"], | |
| 98 | ["Public IP", v.publicIp || "none"], | |
| 99 | ["Private IP", v.privateIp || "none"], | |
| 100 | ["Created", v.createdAt || "not said"], | |
| 101 | ["Volumes", v.volumes.length === 0 ? "none attached" : `${v.volumes.join(", ")} (attached)`], | |
| 102 | ]; | |
| 103 | } | |
| 104 | ||
| 105 | /** One recent action as a line: "power_on · completed · 2026-12-01 17:44". */ | |
| 106 | export const actionLine = (a: ProviderAction): string => [a.what, a.state, a.at.slice(0, 16).replace("T", " ")].filter(Boolean).join(" · "); |
src/http/infra_routes.ts +12 −1
| @@ -17,7 +17,8 @@ import { liveRegionsJs } from "../web/client.ts"; | ||
| 17 | 17 | import { field, readForm } from "../web/form.ts"; |
| 18 | 18 | import { html } from "../web/html.ts"; |
| 19 | 19 | import { flashTo, fragment, page } from "../web/layout.ts"; |
| 20 | import { INFRA_CSS, consolePage, endingPage, buildTray, placesRegion, composeForm, infraJs, infraPage, planTray, recordForm, runningRegion, type ComposeState } from "../web/pages/infra.ts"; | |
| 20 | import { machineView } from "../actions/machine_view.ts"; | |
| 21 | import { INFRA_CSS, cloudPage, consolePage, endingPage, buildTray, placesRegion, composeForm, infraJs, infraPage, planTray, recordForm, runningRegion, type ComposeState } from "../web/pages/infra.ts"; | |
| 21 | 22 | import type { Ctx, Handler } from "./context.ts"; |
| 22 | 23 | import type { Router } from "./router.ts"; |
| 23 | 24 | |
| @@ -260,6 +261,15 @@ function infraEnding(ctx: Ctx): Response { | ||
| 260 | 261 | return page({ ctx, title: `End ${m.value.name}? · forge`, body: endingPage(m.value) }); |
| 261 | 262 | } |
| 262 | 263 | |
| 264 | // GET /infra/:name/cloud: what the provider's API says of the machine, read on open. | |
| 265 | async function infraCloud(ctx: Ctx): Promise<Response> { | |
| 266 | const denied = guard(ctx); | |
| 267 | if (denied) return denied; | |
| 268 | const name = ctx.params.name ?? ""; | |
| 269 | const seen = await machineView(ctx.app, ctx.auth.user, name); | |
| 270 | return page({ ctx, title: `${name} at its cloud · forge`, body: cloudPage(name, seen.ok ? seen.value : undefined, seen.ok ? "" : seen.error.message) }, seen.ok ? 200 : seen.error.reason === "missing" ? 404 : 422); | |
| 271 | } | |
| 272 | ||
| 263 | 273 | // POST /infra/snapshot/:id/restore (a new forge's name) and /delete (arrives from the confirm |
| 264 | 274 | // page). |
| 265 | 275 | async function infraSnapshot(ctx: Ctx): Promise<Response> { |
| @@ -286,6 +296,7 @@ export function infraRoutes(router: Router<Handler>): void { | ||
| 286 | 296 | router.add("POST", "/infra/rebuild", infraRebuild); |
| 287 | 297 | router.add("POST", "/infra/projects", infraProjects); |
| 288 | 298 | router.add("GET", "/infra/:name/ending", infraEnding); |
| 299 | router.add("GET", "/infra/:name/cloud", infraCloud); | |
| 289 | 300 | router.add("POST", "/infra/snapshot/:id/:act", infraSnapshot); |
| 290 | 301 | router.add("POST", "/infra/:name/:act", infraMachine); |
| 291 | 302 | } |
src/http/lazy_lists.ts +1
| @@ -597,6 +597,7 @@ export const OPERATIONS_ROUTES = [ | ||
| 597 | 597 | ["POST", "/infra/rebuild"], |
| 598 | 598 | ["POST", "/infra/projects"], |
| 599 | 599 | ["GET", "/infra/:name/ending"], |
| 600 | ["GET", "/infra/:name/cloud"], | |
| 600 | 601 | ["POST", "/infra/snapshot/:id/:act"], |
| 601 | 602 | ["POST", "/infra/:name/:act"], |
| 602 | 603 | ["GET", "/games"], |
src/http/mcp/tools/infra.ts +12 −4
| @@ -2,6 +2,7 @@ import { cloudLinesFresh } from "../../../actions/cloud_says.ts"; | ||
| 2 | 2 | import { infraCode } from "../../../cloud/infra_read.ts"; |
| 3 | 3 | import { projectsOf, projectsSays } from "../../../cloud/projects.ts"; |
| 4 | 4 | import { buildOf, firewallWant, gameWant, planDatabase, goneWords, infraOf, infraSays, machineOf, nameFor, planById, planCompose, planWants, startBuild } from "../../../actions/infra.ts"; |
| 5 | import { machineView } from "../../../actions/machine_view.ts"; | |
| 5 | 6 | import { bindBucket, readBucket } from "../../../actions/bucket.ts"; |
| 6 | 7 | import { endsWords, keepPinned } from "../../../actions/pinned.ts"; |
| 7 | 8 | import { lookAtRecords, lookWords } from "../../../actions/dns_record.ts"; |
| @@ -21,12 +22,12 @@ const s = (v: unknown, max: number): string => str(v).slice(0, max); | ||
| 21 | 22 | export const tool: Tool = { |
| 22 | 23 | name: "infra", |
| 23 | 24 | listedTo: "admin", |
| 24 | doc: "`{ action, id?, want?, question?, names?, result?, bucket? }`: `list` (its `says` names every machine), `show` an `id` (a plan, an experiment or a machine's name), `plan` a `want` `{app, where, question?, experiment?, days?}` or a firewall `{firewall: name}` or a DNS record `{dns: true, name, account, type: A|SRV, host, value, port?}` or a game box `{game: true, where, name?, repo?, host?}` or Minecraft Java `{design: minecraft, players, where, domain, dns, repo, eula: true, name?, version?}` (its `compose` comes back with the plan) or Postgres `{database: true, app, where: account@region, size?, secret?}` or dead `names`, `apply` its `id`, `check` DNS records, `code` (infra.json against what runs), `keep`, `begin`, `attach`, `record_result`; `price`/`bind` a `bucket` `{region, name, repo?, stored_gib?, out_gib?}`.", | |
| 25 | description: "Cloud machines and spend.", | |
| 25 | doc: "`{ action, id?, want?, question?, names?, result?, bucket? }`: `list` (its `says` names every machine), `show` an `id` (a plan, an experiment or a machine's name), `plan` a `want` `{app, where, question?, experiment?, days?}` or a firewall `{firewall: name}` or a DNS record `{dns: true, name, account, type: A|SRV, host, value, port?}` or a game box `{game: true, where, name?, repo?, host?}` or Minecraft Java `{design: minecraft, players, where, domain, dns, repo, eula: true, name?, version?}` (its `compose` comes back with the plan) or Postgres `{database: true, app, where: account@region, size?, secret?}` or dead `names`, `apply` its `id`, `check` DNS records, `code` (infra.json against what runs), `keep`, `begin`, `attach`, `record_result`; `cloud` a machine's name as `id` (its provider's view: power, size, IPs, actions); `price`/`bind` a `bucket` `{region, name, repo?, stored_gib?, out_gib?}`.", | |
| 26 | description: "Machines, spend.", | |
| 26 | 27 | inputSchema: { |
| 27 | 28 | type: "object", |
| 28 | 29 | properties: { |
| 29 | action: { type: "string", enum: ["list", "show", "plan", "apply", "keep", "check", "code", "begin", "attach", "record_result", "price", "bind"] }, | |
| 30 | action: { type: "string", enum: ["list", "show", "plan", "apply", "keep", "check", "code", "begin", "attach", "record_result", "price", "bind", "cloud"] }, | |
| 30 | 31 | id: { type: "string" }, |
| 31 | 32 | want: { type: "object" }, |
| 32 | 33 | question: { type: "string" }, |
| @@ -83,6 +84,13 @@ export const tool: Tool = { | ||
| 83 | 84 | const files = await infraCode(ctx.app, user); |
| 84 | 85 | return files.ok ? good({ says: files.value.length === 0 ? "no app's repo holds an infra.json" : files.value.map((k) => `${k.app}: ${k.says}`).join("; "), files: files.value }) : bad("not_found", "no such tool here"); |
| 85 | 86 | } |
| 87 | if (action === "cloud") { | |
| 88 | // The provider's own view of a machine, by its name: read from its API, so it answers with | |
| 89 | // the agent dead. | |
| 90 | if (id === "") return bad("no_id", "cloud takes a machine's name as id"); | |
| 91 | const seen = await machineView(ctx.app, user, id); | |
| 92 | return seen.ok ? good({ says: seen.value.says, cloud: seen.value.view }) : bad("refused", seen.error.message); | |
| 93 | } | |
| 86 | 94 | if (action === "show") { |
| 87 | 95 | if (id === "") return bad("no_id", "show takes an id: a plan's, an experiment's, or a machine's name"); |
| 88 | 96 | if (id.startsWith("plan-")) { |
| @@ -228,7 +236,7 @@ export const tool: Tool = { | ||
| 228 | 236 | } |
| 229 | 237 | return good({ says: `Attached ${names.join(", ")}.`, experiment: findExperiment(db, id) }); |
| 230 | 238 | } |
| 231 | if (action !== "record_result") return bad("bad_action", "action is list, show, plan, apply, keep, begin, attach, record_result, price or bind"); | |
| 239 | if (action !== "record_result") return bad("bad_action", "action is list, show, plan, apply, keep, begin, attach, record_result, price, bind or cloud"); | |
| 232 | 240 | const done = recordResult(ctx.app, user.id, id, s(args["result"], 500)); |
| 233 | 241 | if (!done.ok) return bad(done.error, EXPERIMENT_WORDS[done.error]); |
| 234 | 242 | return good({ says: "Result recorded: the experiment is done.", experiment: findExperiment(db, id) }); |
src/tui/screens/infra.ts +20 −1
| @@ -19,6 +19,8 @@ import { deleteSnapshot, endWithSnapshot, restoredName, restoreSnapshot } from " | ||
| 19 | 19 | import { planMinecraft } from "../../actions/minecraft.ts"; |
| 20 | 20 | import { LIFETIME_DAYS, clearPinned, endsWords, keepPinned, retirePinned, retireWords } from "../../actions/pinned.ts"; |
| 21 | 21 | import type { SnapshotView } from "../../actions/infra_snapshot.ts"; |
| 22 | import { machineView } from "../../actions/machine_view.ts"; | |
| 23 | import { actionLine, viewRows } from "../../cloud/provider_detail.ts"; | |
| 22 | 24 | import { dollars } from "../../cloud/prices.ts"; |
| 23 | 25 | import { cloudLine } from "../../actions/cloud_says.ts"; |
| 24 | 26 | import { getPinned } from "../../core/pinned.ts"; |
| @@ -338,6 +340,7 @@ export function infraScreen(f: Session, ctx: Ctx): Screen { | ||
| 338 | 340 | binding(["b"], "new database"), |
| 339 | 341 | binding(["M"], "new Minecraft server"), |
| 340 | 342 | binding(["+"], `keep ${LIFETIME_DAYS} more days`), |
| 343 | binding(["v"], "what the cloud says"), | |
| 341 | 344 | binding(["x"], "end now"), |
| 342 | 345 | binding(["c"], "clear a dead row"), |
| 343 | 346 | binding(["l"], "look at DNS records"), |
| @@ -565,12 +568,28 @@ export function infraScreen(f: Session, ctx: Ctx): Screen { | ||
| 565 | 568 | if (next !== screen) ctx.push(next); |
| 566 | 569 | return true; |
| 567 | 570 | } |
| 568 | if (e.key !== "+" && e.key !== "x" && e.key !== "c" && e.key !== "o") return false; | |
| 571 | if (e.key !== "+" && e.key !== "x" && e.key !== "c" && e.key !== "v" && e.key !== "o") return false; | |
| 569 | 572 | const picked = all[state.index - progress - drift]; |
| 570 | 573 | if (picked === undefined) { |
| 571 | 574 | say("Select a machine first."); |
| 572 | 575 | return true; |
| 573 | 576 | } |
| 577 | if (e.key === "v") { | |
| 578 | say(`Asking the cloud about ${picked.name}…`); | |
| 579 | void machineView(f.app, f.user, picked.name).then((r) => { | |
| 580 | if (!r.ok) return say(r.error.message); | |
| 581 | ctx.push( | |
| 582 | confirmScreen(ctx, screen, { | |
| 583 | title: `${picked.name} at its cloud`, | |
| 584 | body: [r.value.says, ...viewRows(r.value.view).map(([k, v]) => `${k}: ${v}`), ...(r.value.view.actions.length === 0 ? ["No recent actions."] : ["Recent actions:", ...r.value.view.actions.map((a) => ` ${actionLine(a)}`)])], | |
| 585 | actions: [{ label: "Close" }], | |
| 586 | width: 96, | |
| 587 | onChoose: () => undefined, | |
| 588 | }), | |
| 589 | ); | |
| 590 | }); | |
| 591 | return true; | |
| 592 | } | |
| 574 | 593 | // Hetzner's own console: its address and password, good for about a minute, shown once. |
| 575 | 594 | if (e.key === "o") { |
| 576 | 595 | say(`Asking Hetzner for ${picked.name}'s console…`); |
src/web/pages/infra.ts +22
| @@ -16,6 +16,8 @@ import { emptyLine } from "../empty_words.ts"; | ||
| 16 | 16 | import type { CodeFile } from "../../cloud/infra_read.ts"; |
| 17 | 17 | import { restoredName, type SnapshotView } from "../../actions/infra_snapshot.ts"; |
| 18 | 18 | import { ENDED_DAYS, type RecordFields, type Build, type ComposeChoices, type InfraView, type Planned, type Running } from "../../actions/infra.ts"; |
| 19 | import type { MachineView } from "../../actions/machine_view.ts"; | |
| 20 | import { actionLine, viewRows } from "../../cloud/provider_detail.ts"; | |
| 19 | 21 | import { dollars } from "../../cloud/prices.ts"; |
| 20 | 22 | import { type CloudResource, type ProjectView } from "../../cloud/projects.ts"; |
| 21 | 23 | import { pageHead } from "../chrome.ts"; |
| @@ -136,6 +138,7 @@ export function consolePage(name: string, c: { wssUrl: string; password: string | ||
| 136 | 138 | /** Keep and End now: Keep is reversible and posts at once; End now goes through the confirm. */ |
| 137 | 139 | const machineActions = (r: Running): Html => |
| 138 | 140 | html`<div class="row-fields"> |
| 141 | ${!r.atProvider ? null : btn("At the cloud", { href: `/infra/${r.name}/cloud`, variant: "ghost", size: "sm", label: `What the cloud says of ${r.name}` })} | |
| 139 | 142 | ${r.endsAt === null |
| 140 | 143 | ? null |
| 141 | 144 | : html`<form method="post" action="/infra/${r.name}/keep"> |
| @@ -268,6 +271,25 @@ function snapshotRow(x: SnapshotView): Html { | ||
| 268 | 271 | }); |
| 269 | 272 | } |
| 270 | 273 | |
| 274 | /** | |
| 275 | * The cloud's own view of one machine (#17151): read from the provider's API, so it answers with | |
| 276 | * the agent dead. | |
| 277 | */ | |
| 278 | export function cloudPage(name: string, m: MachineView | undefined, why: string): Html { | |
| 279 | return html`<div class="narrow-wide"> | |
| 280 | ${pageHead(`${name} at its cloud`)} | |
| 281 | ${m === undefined | |
| 282 | ? html`<p role="status" data-cloud-refused>${why}</p>` | |
| 283 | : html`<p data-cloud-says>${m.says}</p> | |
| 284 | <dl class="row-facts" data-cloud-facts>${viewRows(m.view).map(([k, v]) => html`<div><dt>${k}</dt><dd>${v}</dd></div>`)}</dl> | |
| 285 | <h2>Recent actions</h2> | |
| 286 | ${m.view.actions.length === 0 | |
| 287 | ? html`<p class="muted">The provider lists no actions.</p>` | |
| 288 | : html`<div data-cloud-actions>${rowList(m.view.actions.map((a) => row({ title: actionLine(a) })))}</div>`}`} | |
| 289 | <div class="form-actions">${btn("Back", { href: `/infra#machine-${name}`, variant: "ghost" })} ${btn("Read again", { href: `/infra/${name}/cloud`, variant: "ghost" })}</div> | |
| 290 | </div>`; | |
| 291 | } | |
| 292 | ||
| 271 | 293 | /** What the offer before End says, and its choices: snapshot first, end without, or cancel. */ |
| 272 | 294 | export function endingPage(r: Running): Html { |
| 273 | 295 | return html`<div class="narrow-wide"> |
test/machine-view.test.ts added +210
| @@ -0,0 +1,210 @@ | ||
| 1 | /** | |
| 2 | * The cloud's own view of a machine (#17151, V2): DigitalOcean's droplet and Hetzner's server read | |
| 3 | * through their drivers into one view; the action, the page, the `infra` tool and the terminal key | |
| 4 | * all say it. A recorded cloud; nothing leaves the process. | |
| 5 | */ | |
| 6 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 7 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 8 | import { tmpdir } from "node:os"; | |
| 9 | import { join } from "node:path"; | |
| 10 | import { createApp, type App } from "../src/app.ts"; | |
| 11 | import { addAccount, allowWrite, net as cloudNet } from "../src/actions/cloud.ts"; | |
| 12 | import { machineView } from "../src/actions/machine_view.ts"; | |
| 13 | import { deployPinned, net } from "../src/actions/pinned.ts"; | |
| 14 | import { digitalocean } from "../src/cloud/droplet.ts"; | |
| 15 | import { hetzner } from "../src/cloud/hetzner.ts"; | |
| 16 | import type { Call } from "../src/cloud/machine.ts"; | |
| 17 | import { droplet, MAX_ACTIONS, server, viewSays } from "../src/cloud/provider_detail.ts"; | |
| 18 | import { getPinned, setRemote } from "../src/core/pinned.ts"; | |
| 19 | import { createSession } from "../src/core/sessions.ts"; | |
| 20 | import { createUser, type User } from "../src/core/users.ts"; | |
| 21 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 22 | import { tool } from "../src/http/mcp/tools/infra.ts"; | |
| 23 | import { setBaseDomain } from "../src/settings/pinned_domain.ts"; | |
| 24 | import { createForgeApp } from "../src/tui/forge.ts"; | |
| 25 | import { infraScreen } from "../src/tui/screens/infra.ts"; | |
| 26 | import { headlessTerminal } from "../src/tui/term/terminal.ts"; | |
| 27 | import { quietLogs } from "./helpers/globals.ts"; | |
| 28 | import { seedPlaces } from "./helpers/places-fixture.ts"; | |
| 29 | import { recordCloud } from "./helpers/recorded-cloud.ts"; | |
| 30 | import { settleApp } from "./helpers/tui.ts"; | |
| 31 | ||
| 32 | quietLogs(); | |
| 33 | ||
| 34 | const DROPLET = { | |
| 35 | status: "active", size_slug: "s-1vcpu-2gb", image: { distribution: "Ubuntu", name: "24.04 (LTS) x64" }, region: { slug: "nyc3" }, created_at: "2026-12-01T17:40:00Z", | |
| 36 | networks: { v4: [{ type: "private", ip_address: "10.116.0.2" }, { type: "public", ip_address: "203.0.113.9" }] }, volume_ids: ["vol-1"], | |
| 37 | }; | |
| 38 | const DO_ACTIONS = [ | |
| 39 | { type: "create", status: "completed", completed_at: "2026-12-01T17:41:00Z" }, | |
| 40 | { type: "power_on", status: "completed", completed_at: "2026-12-01T17:44:00Z" }, | |
| 41 | ]; | |
| 42 | ||
| 43 | describe("the provider's view of a machine", () => { | |
| 44 | test("DigitalOcean: state, size, image, addresses, volumes and actions, newest first", () => { | |
| 45 | const v = droplet(DROPLET, DO_ACTIONS); | |
| 46 | expect(v).toMatchObject({ power: "active", size: "s-1vcpu-2gb", image: "Ubuntu 24.04 (LTS) x64", region: "nyc3", publicIp: "203.0.113.9", privateIp: "10.116.0.2", volumes: ["vol-1"] }); | |
| 47 | expect(v.actions.map((a) => a.what)).toEqual(["power_on", "create"]); | |
| 48 | expect(viewSays("mc", v)).toContain("last action power_on completed 2026-12-01 17:44"); | |
| 49 | }); | |
| 50 | ||
| 51 | test("Hetzner: the server's own fields, and a malformed answer is dropped, never guessed at", () => { | |
| 52 | const v = server({ status: "off", server_type: { name: "cx23" }, image: { description: "Ubuntu 24.04" }, datacenter: { name: "nbg1-dc3" }, public_net: { ipv4: { ip: "203.0.113.7" } }, private_net: [{ ip: "10.0.0.2" }], created: "2026-12-01T10:00:00Z", volumes: [42] }, [{ command: "poweroff", status: "success", finished: "2026-12-01T11:00:00Z" }]); | |
| 53 | expect(v).toMatchObject({ power: "off", size: "cx23", region: "nbg1-dc3", publicIp: "203.0.113.7", privateIp: "10.0.0.2", volumes: ["42"] }); | |
| 54 | const bad = server({ public_net: { ipv4: { ip: "<script>" } }, volumes: [{}, "a/b"] }, "not a list"); | |
| 55 | expect(bad.publicIp).toBe(""); | |
| 56 | expect(bad.volumes).toEqual([]); | |
| 57 | expect(bad.actions).toEqual([]); | |
| 58 | }); | |
| 59 | ||
| 60 | test("actions are bounded", () => { | |
| 61 | const many = Array.from({ length: 50 }, (_, i) => ({ type: `a${i}`, status: "completed", completed_at: `2026-12-01T10:${String(i).padStart(2, "0")}:00Z` })); | |
| 62 | expect(droplet(DROPLET, many).actions).toHaveLength(MAX_ACTIONS); | |
| 63 | }); | |
| 64 | ||
| 65 | test("the drivers make the two reads each", async () => { | |
| 66 | const seen: string[] = []; | |
| 67 | const call: Call = async (_m, path) => { | |
| 68 | seen.push(path); | |
| 69 | return path.endsWith("/actions?per_page=10") || path.includes("/actions?") ? { actions: DO_ACTIONS } : { droplet: DROPLET, server: { status: "running" } }; | |
| 70 | }; | |
| 71 | expect((await digitalocean.viewMachine!(call, "123")).power).toBe("active"); | |
| 72 | expect((await hetzner.viewMachine!(call, "123")).power).toBe("running"); | |
| 73 | expect(seen).toEqual(["/droplets/123", "/droplets/123/actions?per_page=10", "/servers/123", "/servers/123/actions?sort=id:desc&per_page=10"]); | |
| 74 | }); | |
| 75 | }); | |
| 76 | ||
| 77 | describe("opened at every door", () => { | |
| 78 | let dataDir = ""; | |
| 79 | let app: App; | |
| 80 | let admin: User; | |
| 81 | let servers: Servers; | |
| 82 | let cookie = ""; | |
| 83 | const cloud = recordCloud(); | |
| 84 | let hostile = false; | |
| 85 | const fetchView = async (url: string, init?: RequestInit): Promise<Response> => { | |
| 86 | const u = new URL(url); | |
| 87 | if (u.host === "api.digitalocean.com" && (init?.method ?? "GET") === "GET") { | |
| 88 | if (/^\/v2\/droplets\/[^/]+\/actions$/.test(u.pathname)) return new Response(JSON.stringify({ actions: hostile ? [{ type: "<script>alert(1)</script>", status: "completed", completed_at: "2026-12-01T17:44:00Z" }] : DO_ACTIONS }), { status: 200 }); | |
| 89 | if (/^\/v2\/droplets\/[^/]+$/.test(u.pathname)) return new Response(JSON.stringify({ droplet: DROPLET }), { status: 200 }); | |
| 90 | } | |
| 91 | return cloud.fetch(url, init); | |
| 92 | }; | |
| 93 | ||
| 94 | beforeAll(async () => { | |
| 95 | dataDir = await mkdtemp(join(tmpdir(), "forge-machine-view-")); | |
| 96 | app = createApp({ dataDir, port: 0, url: "https://forge.example.net" }); | |
| 97 | await seedPlaces(app.db); | |
| 98 | const u = await createUser(app.db, { username: "ada", email: "[email protected]", password: "a-good-passphrase", origin: "seed", isAdmin: true, trusted: true }); | |
| 99 | if (!u.ok) throw new Error(u.error); | |
| 100 | admin = u.value; | |
| 101 | setBaseDomain(app.db, "forges.example.net", admin.id); | |
| 102 | cloudNet.fetch = async () => new Response(JSON.stringify({ account: { email: "[email protected]", status: "active" } }), { status: 200 }); | |
| 103 | const a = await addAccount(app, admin, "digitalocean", "main", "dop_v1_9Zq4TxLm7Kp2Vb8nWcR5"); | |
| 104 | if (!a.ok) throw new Error(String(a.error)); | |
| 105 | allowWrite(app, admin, "digitalocean", "main", true); | |
| 106 | net.fetch = fetchView; | |
| 107 | net.resolve = async () => []; | |
| 108 | const d = await deployPinned(app, admin, { name: "mc", host: "", target: "digitalocean", account: "main", app: "ada/shop", source: "https://forge.example.net/ada/shop.git", version: "a".repeat(40), place: "digitalocean:nyc3:s-1vcpu-2gb" }); | |
| 109 | if (!d.ok) throw new Error(d.error); | |
| 110 | servers = await serve(app); | |
| 111 | cookie = `forge_session=${createSession(app.db, admin.id).token}`; | |
| 112 | }, 30_000); | |
| 113 | ||
| 114 | afterAll(async () => { | |
| 115 | servers?.stop(); | |
| 116 | app.close(); | |
| 117 | await rm(dataDir, { recursive: true, force: true }); | |
| 118 | }); | |
| 119 | ||
| 120 | test("the action reads the provider, for an admin only", async () => { | |
| 121 | expect(getPinned(app.db, "mc")?.remote).not.toBe(""); | |
| 122 | const seen = await machineView(app, admin, "mc"); | |
| 123 | if (!seen.ok) throw new Error(seen.error.message); | |
| 124 | expect(seen.value.view.publicIp).toBe("203.0.113.9"); | |
| 125 | expect((await machineView(app, undefined, "mc")).ok).toBe(false); | |
| 126 | const none = await machineView(app, admin, "nope"); | |
| 127 | expect(none.ok ? "" : none.error).toEqual({ reason: "missing", message: "No such pinned forge." }); | |
| 128 | const denied = await machineView(app, undefined, "mc"); | |
| 129 | expect(denied.ok ? "" : denied.error.reason).toBe("denied"); | |
| 130 | }); | |
| 131 | ||
| 132 | test("the page shows the facts and the actions, escaped; a stranger gets nothing", async () => { | |
| 133 | const res = await fetch(`${servers.url}/infra/mc/cloud`, { headers: { cookie } }); | |
| 134 | expect(res.status).toBe(200); | |
| 135 | const body = await res.text(); | |
| 136 | expect(body).toContain("203.0.113.9"); | |
| 137 | expect(body).toContain("power_on · completed · 2026-12-01 17:44"); | |
| 138 | expect((await fetch(`${servers.url}/infra/mc/cloud`, { redirect: "manual" })).status).not.toBe(200); | |
| 139 | }); | |
| 140 | ||
| 141 | test("a retired forge, a forge with no machine and a cloud with no view each say so, typed", async () => { | |
| 142 | const remote = getPinned(app.db, "mc")!.remote; | |
| 143 | const reason = async (): Promise<string> => { | |
| 144 | const r = await machineView(app, admin, "mc"); | |
| 145 | return r.ok ? "ok" : r.error.reason; | |
| 146 | }; | |
| 147 | setRemote(app.db, "mc", { state: "retired" }); | |
| 148 | expect(await reason()).toBe("retired"); | |
| 149 | expect((await fetch(`${servers.url}/infra/mc/cloud`, { headers: { cookie } })).status).toBe(422); | |
| 150 | setRemote(app.db, "mc", { state: "running", remote: "" }); | |
| 151 | expect(await reason()).toBe("no_machine"); | |
| 152 | setRemote(app.db, "mc", { remote }); | |
| 153 | const view = digitalocean.viewMachine; | |
| 154 | delete (digitalocean as { viewMachine?: unknown }).viewMachine; | |
| 155 | try { | |
| 156 | expect(await reason()).toBe("unread"); | |
| 157 | const page = await fetch(`${servers.url}/infra/mc/cloud`, { headers: { cookie } }); | |
| 158 | expect(page.status).toBe(422); | |
| 159 | expect(await page.text()).toContain("not read here yet"); | |
| 160 | } finally { | |
| 161 | digitalocean.viewMachine = view!; | |
| 162 | } | |
| 163 | expect(await reason()).toBe("ok"); | |
| 164 | }); | |
| 165 | ||
| 166 | test("a name no forge has is a 404 on the page", async () => { | |
| 167 | expect((await fetch(`${servers.url}/infra/nope/cloud`, { headers: { cookie } })).status).toBe(404); | |
| 168 | }); | |
| 169 | ||
| 170 | test("a provider's hostile text is escaped on the page", async () => { | |
| 171 | hostile = true; | |
| 172 | try { | |
| 173 | const body = await (await fetch(`${servers.url}/infra/mc/cloud`, { headers: { cookie } })).text(); | |
| 174 | expect(body).not.toContain("<script>alert(1)</script>"); | |
| 175 | expect(body).toContain("<script>"); | |
| 176 | } finally { | |
| 177 | hostile = false; | |
| 178 | } | |
| 179 | }); | |
| 180 | ||
| 181 | test("a signed-in non-admin gets neither the page nor the tool's answer", async () => { | |
| 182 | const u = await createUser(app.db, { username: "bob", email: "[email protected]", password: "a-good-passphrase", origin: "seed", isAdmin: false, trusted: true }); | |
| 183 | if (!u.ok) throw new Error(u.error); | |
| 184 | const bobCookie = `forge_session=${createSession(app.db, u.value.id).token}`; | |
| 185 | const res = await fetch(`${servers.url}/infra/mc/cloud`, { headers: { cookie: bobCookie }, redirect: "manual" }); | |
| 186 | expect(res.status).not.toBe(200); | |
| 187 | expect(await res.text()).not.toContain("203.0.113.9"); | |
| 188 | const out = await tool.run({ app } as unknown as Parameters<typeof tool.run>[0], u.value, { action: "cloud", id: "mc" }); | |
| 189 | expect(JSON.stringify(out)).not.toContain("203.0.113.9"); | |
| 190 | }); | |
| 191 | ||
| 192 | test("the infra tool's cloud action says the same", async () => { | |
| 193 | const out = await tool.run({ app } as unknown as Parameters<typeof tool.run>[0], admin, { action: "cloud", id: "mc" }); | |
| 194 | expect(JSON.stringify(out)).toContain("203.0.113.9"); | |
| 195 | const refused = await tool.run({ app } as unknown as Parameters<typeof tool.run>[0], admin, { action: "cloud", id: "" }); | |
| 196 | expect(JSON.stringify(refused)).toContain("no_id"); | |
| 197 | }); | |
| 198 | ||
| 199 | test("the terminal's v shows the cloud's facts", async () => { | |
| 200 | const { app: tui, session } = createForgeApp(app, { sshUsername: "ada", remoteAddress: "127.0.0.1", fingerprint: "SHA256:test", user: admin }, { terminal: headlessTerminal(160, 40), exitHooks: false }); | |
| 201 | tui.start(); | |
| 202 | tui.ctx.push(infraScreen(session, tui.ctx)); | |
| 203 | await settleApp(tui); | |
| 204 | tui.dispatch({ type: "key", key: "v", ctrl: false, alt: false, shift: false }); | |
| 205 | for (let i = 0; i < 20 && !tui.screen.toText().includes("203.0.113.9"); i++) await settleApp(tui); | |
| 206 | expect(tui.screen.toText()).toContain("203.0.113.9"); | |
| 207 | expect(tui.screen.toText()).toContain("power_on"); | |
| 208 | tui.stop?.(); | |
| 209 | }); | |
| 210 | }); |
test/pinned-ceiling.test.ts +5 −4
| @@ -351,7 +351,7 @@ describe("cloud account ceiling", () => { | ||
| 351 | 351 | if (/^import\b[^\n]*$/.test(text.slice(text.lastIndexOf("\n", m.index) + 1, m.index))) continue; |
| 352 | 352 | uses++; |
| 353 | 353 | const before = text.slice(Math.max(0, m.index - 80), m.index); |
| 354 | if (!/\bcapped\(app, [\w.]+, $/.test(before) && !/\b(?:read|retire|strays|ensureFirewall|removeFirewall|snapshotKind|takeSnapshot|readSnapshot|dropSnapshot)\($/.test(before)) bare.push(`${file.slice(src.length + 1)}: …${before.slice(-40)}driverOf(`); | |
| 354 | if (!/\bcapped\(app, [\w.]+, $/.test(before) && !/\b(?:read|retire|strays|ensureFirewall|removeFirewall|snapshotKind|takeSnapshot|readSnapshot|dropSnapshot|viewOf)\($/.test(before)) bare.push(`${file.slice(src.length + 1)}: …${before.slice(-40)}driverOf(`); | |
| 355 | 355 | } |
| 356 | 356 | } |
| 357 | 357 | expect(bare).toEqual([]); |
| @@ -360,9 +360,10 @@ describe("cloud account ceiling", () => { | ||
| 360 | 360 | // (#17072), all in actions/infra_snapshot.ts: snapshotKind twice (the offer, and the check |
| 361 | 361 | // before one is taken), takeSnapshot, readSnapshot and dropSnapshot; and the firewall made and |
| 362 | 362 | // removed (#17070): neither can spend, as a firewall is free at both clouds and each only |
| 363 | // finds, makes or deletes that one firewall, never a machine or a volume. A bare driverOf still | |
| 364 | // fails the pattern above; only those call names are let through. | |
| 365 | expect(uses).toBe(15); | |
| 363 | // finds, makes or deletes that one firewall, never a machine or a volume; and the read-only | |
| 364 | // view of a machine (#17151), handed to `viewOf`. A bare driverOf still fails the pattern | |
| 365 | // above; only those call names are let through. | |
| 366 | expect(uses).toBe(16); | |
| 366 | 367 | }); |
| 367 | 368 | }); |
| 368 | 369 |
new above
offline
This helper only exists to satisfy the ceiling test's name pattern and duplicates the check in
viewOf. Remove it and calldriverOfwhere the test allows it, or teach the test the real read-only path.