Multiplayer agent harness for work.
AWS MicroVM: follow nextToken and filter by name when finding images (#2434)
Regan Bellf0cf528d2286
diff --git a/src/sandbox/aws-microvm-api.ts b/src/sandbox/aws-microvm-api.tsindex 54fa964f7..3cab78b04 100644--- a/src/sandbox/aws-microvm-api.ts+++ b/src/sandbox/aws-microvm-api.ts@@ -65,7 +65,7 @@ export interface AwsMicrovmApiOptions {}export interface AwsMicrovmApi {- listImages(): Promise<MicrovmImageSummary[]>;+ listImages(nameFilter?: string): Promise<MicrovmImageSummary[]>;findImage(name: string): Promise<MicrovmImageSummary | null>;createImage(input: {name: string;@@ -102,8 +102,14 @@ export function createMicrovmApi(opts: AwsMicrovmApiOptions): AwsMicrovmApi {const credentials = opts.credentials ?? defaultProvider(opts.profile ? { profile: opts.profile } : {});const signer = new SignatureV4({ service: "lambda", region, sha256: Sha256, credentials });- async function call<T>(method: string, path: string, body?: unknown): Promise<{ status: number; json: T }> {+ async function call<T>(+ method: string,+ path: string,+ body?: unknown,+ query: Record<string, string> = {},+ ): Promise<{ status: number; json: T }> {const payload = body === undefined ? "" : JSON.stringify(body);+ const search = new URLSearchParams(query).toString();const signed = await signer.sign({method,protocol: "https:",@@ -111,11 +117,11 @@ export function createMicrovmApi(opts: AwsMicrovmApiOptions): AwsMicrovmApi {path,headers: { host, "content-type": "application/json" },body: payload,- query: {},+ query,});const res = await fetchWithRetry((signal) =>- doFetch(`https://${host}${path}`, {+ doFetch(`https://${host}${path}${search ? `?${search}` : ""}`, {method,headers: signed.headers as Record<string, string>,signal,@@ -145,12 +151,23 @@ export function createMicrovmApi(opts: AwsMicrovmApiOptions): AwsMicrovmApi {}const api: AwsMicrovmApi = {- async listImages() {- const { json } = await call<{ items?: MicrovmImageSummary[] }>("GET", `${API}/microvm-images`);- return json.items ?? [];+ async listImages(nameFilter?: string) {+ const items: MicrovmImageSummary[] = [];+ let next: string | undefined;+ do {+ const { json } = await call<{ items?: MicrovmImageSummary[]; nextToken?: string }>(+ "GET",+ `${API}/microvm-images`,+ undefined,+ { ...(nameFilter ? { nameFilter } : {}), ...(next ? { nextToken: next } : {}) },+ );+ items.push(...(json.items ?? []));+ next = json.nextToken;+ } while (next);+ return items;},async findImage(name) {- return (await api.listImages()).find((i) => i.name === name) ?? null;+ return (await api.listImages(name)).find((i) => i.name === name) ?? null;},async createImage({ name, s3Uri, baseImageArn, buildRoleArn, clientToken }) {const { json } = await call<MicrovmImageSummary>("POST", `${API}/microvm-images`, {diff --git a/test/aws-microvm-api.test.ts b/test/aws-microvm-api.test.tsindex f07bfeb24..f4a62fecb 100644--- a/test/aws-microvm-api.test.ts+++ b/test/aws-microvm-api.test.ts@@ -7,6 +7,8 @@ import {AwsApiError,type AwsMicrovmApi,} from "../src/sandbox/aws-microvm-api.ts";+import { SignatureV4 } from "@smithy/signature-v4";+import { Sha256 } from "@aws-crypto/sha256-js";interface Recorded {url: string;@@ -273,3 +275,37 @@ test("writeAbs splits writes above the chunk size into parts, joins them in theconst other = writes[0]!.path.replace(/\.part-0$/, "");assert.deepEqual(execs, [`rm -f '${other}'.part-* '${other}'.joined`]);});++test("findImage filters by name, follows nextToken, and signs the query string it sends", async () => {+ const { fetchImpl, calls } = fakeFetch((rec) =>+ rec.url.includes("nextToken=")+ ? { status: 200, body: { items: [{ name: "b-qm-sandbox", imageArn: "arn:b" }] } }+ : { status: 200, body: { items: [{ name: "b-qm-sandbox-old", imageArn: "arn:old" }], nextToken: "p/2" } },+ );+ const api = createMicrovmApi({ region: "us-west-2", credentials: creds, fetchImpl });+ assert.equal((await api.findImage("b-qm-sandbox"))?.imageArn, "arn:b");+ assert.deepEqual(+ calls.map((c) => new URL(c.url).search),+ ["?nameFilter=b-qm-sandbox", "?nameFilter=b-qm-sandbox&nextToken=p%2F2"],+ );+ const signer = new SignatureV4({ service: "lambda", region: "us-west-2", credentials: creds, sha256: Sha256 });+ const { url, headers } = calls[1]!;+ const u = new URL(url);+ const expected = await signer.sign(+ {+ method: "GET",+ protocol: "https:",+ hostname: u.hostname,+ path: u.pathname,+ headers: { host: u.hostname, "content-type": "application/json" },+ body: "",+ query: Object.fromEntries(u.searchParams),+ },+ {+ signingDate: new Date(+ headers["x-amz-date"]!.replace(/(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})Z/, "$1-$2-$3T$4:$5:$6Z"),+ ),+ },+ );+ assert.equal(headers.authorization, (expected.headers as Record<string, string>).authorization);+});