diff --git a/server/src/__tests__/openapi-routes.test.ts b/server/src/__tests__/openapi-routes.test.ts index 5f409e5f5fb..69c2a5e4bfa 100644 --- a/server/src/__tests__/openapi-routes.test.ts +++ b/server/src/__tests__/openapi-routes.test.ts @@ -128,6 +128,7 @@ const explicitOpenApiOperationCoverageExclusions = new Set([ // Exact Cloud-control signed action; ordinary board/agent credentials cannot invoke it. "POST /api/instance/connector-events/wake", "POST /api/instance/ephemeral-enrollment/prepare", + "GET /api/instance/ephemeral-enrollment/eligibility", // Muse uses its closed, versioned receiver protocol and dedicated credentials, // documented in doc/muse-personal-agent.md and the shared protocol contract. // Board connection/qualification management remains in this OpenAPI document. diff --git a/server/src/middleware/cloud-control.ts b/server/src/middleware/cloud-control.ts index 69d3edb290b..a6f22efcf28 100644 --- a/server/src/middleware/cloud-control.ts +++ b/server/src/middleware/cloud-control.ts @@ -22,6 +22,7 @@ export function verifiedIdleCapabilities(req: Request): VerifiedIdleCapabilities /** Endpoint → method → the one action a control assertion must name to take it. */ const ACTIONS_BY_ENDPOINT: Record> = { "/api/instance/ephemeral-enrollment/prepare": { POST: "ephemeral-enrollment:prepare" }, + "/api/instance/ephemeral-enrollment/eligibility": { GET: "ephemeral-enrollment:eligibility" }, "/api/instance/connector-events/wake": { POST: "connector-events:wake" }, "/api/instance/task-drain/status": { POST: "task-drain:read-body" }, "/api/instance/task-drain": { diff --git a/server/src/routes/ephemeral-enrollment.test.ts b/server/src/routes/ephemeral-enrollment.test.ts index 83c860743ad..166ff12b412 100644 --- a/server/src/routes/ephemeral-enrollment.test.ts +++ b/server/src/routes/ephemeral-enrollment.test.ts @@ -10,7 +10,9 @@ import { CLOUD_CONTROL_AUDIENCE, CLOUD_CONTROL_HEADER, CLOUD_CONTROL_JWS_TYPE, CLOUD_RUNTIME_IDENTITY_ISSUER, resetCloudControlReplayFenceForTests } from "../services/cloud-runtime-identity.js"; const prepare = vi.hoisted(() => vi.fn()); -vi.mock("../services/ephemeral-enrollment.js", () => ({ prepareOnlineEphemeralEnrollment: prepare })); +const eligibility = vi.hoisted(() => vi.fn()); +vi.mock("../services/ephemeral-enrollment.js", () => ({ prepareOnlineEphemeralEnrollment: prepare, + readOnlineEphemeralEnrollmentEligibility: eligibility })); const pair = generateKeyPairSync("ed25519"); const jwk = { ...pair.publicKey.export({ format: "jwk" }), kid: "enrollment-fixture", alg: "EdDSA", use: "sig" }; const path = "/api/instance/ephemeral-enrollment/prepare"; @@ -42,6 +44,7 @@ beforeEach(() => { vi.stubEnv("PAPERCLIP_CLOUD_STACK_ID", "stack-fixture"); vi.stubEnv("PAPERCLIP_CLOUD_RUNTIME_IDENTITY_JWKS", JSON.stringify({ keys: [jwk] })); prepare.mockResolvedValue({ attemptId, enrollmentReady: false, decisionSigningSecret: "fixture-original-key" }); + eligibility.mockResolvedValue({ eligible: false, blockers: ["workspaces_require_migration"], ephemeralServer: false }); }); afterEach(() => vi.unstubAllEnvs()); @@ -79,3 +82,24 @@ it("keeps enrollment HTTP diagnostics secret-sensitive, including rejected metho expect(isSecretSensitiveHttpRequest(method, `https://fixture.test${path}`)).toBe(true); } }); + +const eligibilityPath = "/api/instance/ephemeral-enrollment/eligibility"; + +it("reports durable-data eligibility to a signed read and changes nothing", async () => { + const result = await request(app()).get(eligibilityPath).set(CLOUD_CONTROL_HEADER, assertion("ephemeral-enrollment:eligibility")); + expect(result.status).toBe(200); + expect(result.headers["cache-control"]).toBe("no-store"); + expect(result.body).toEqual({ eligible: false, blockers: ["workspaces_require_migration"], ephemeralServer: false }); + expect(prepare).not.toHaveBeenCalled(); +}); + +it.each([false, true])("refuses an unsigned eligibility read, including a tenant instance admin (%s)", async tenant => { + expect((await request(app(tenant)).get(eligibilityPath)).status).toBe(403); + expect(eligibility).not.toHaveBeenCalled(); +}); + +it("refuses a prepare assertion on the eligibility read", async () => { + const result = await request(app()).get(eligibilityPath).set(CLOUD_CONTROL_HEADER, assertion("ephemeral-enrollment:prepare")); + expect(result.status).toBe(401); + expect(eligibility).not.toHaveBeenCalled(); +}); diff --git a/server/src/routes/ephemeral-enrollment.ts b/server/src/routes/ephemeral-enrollment.ts index b626232c9f7..338c83a3ccc 100644 --- a/server/src/routes/ephemeral-enrollment.ts +++ b/server/src/routes/ephemeral-enrollment.ts @@ -3,7 +3,7 @@ import { z } from "zod"; import type { Db } from "@paperclipai/db"; import { validate } from "../middleware/validate.js"; import { verifiedCloudControlClaims } from "../middleware/cloud-control.js"; -import { prepareOnlineEphemeralEnrollment } from "../services/ephemeral-enrollment.js"; +import { prepareOnlineEphemeralEnrollment, readOnlineEphemeralEnrollmentEligibility } from "../services/ephemeral-enrollment.js"; export function ephemeralEnrollmentRoutes(db: Db) { const router = Router(); @@ -16,5 +16,11 @@ export function ephemeralEnrollmentRoutes(db: Db) { if (claims?.action !== "ephemeral-enrollment:prepare") { res.status(403).json({ error: "cloud_control_required" }); return; } res.json(await prepareOnlineEphemeralEnrollment(db, req.body.attemptId, claims)); }); + router.get("/instance/ephemeral-enrollment/eligibility", async (req, res) => { + res.set("Cache-Control", "no-store"); + const claims = req.actor.source === "cloud_control" ? verifiedCloudControlClaims(req) : undefined; + if (claims?.action !== "ephemeral-enrollment:eligibility") { res.status(403).json({ error: "cloud_control_required" }); return; } + res.json(await readOnlineEphemeralEnrollmentEligibility(db)); + }); return router; } diff --git a/server/src/services/cloud-runtime-identity.ts b/server/src/services/cloud-runtime-identity.ts index 8d2280a599a..996d5b44d95 100644 --- a/server/src/services/cloud-runtime-identity.ts +++ b/server/src/services/cloud-runtime-identity.ts @@ -449,6 +449,7 @@ export const CLOUD_CONTROL_AUDIENCE = "paperclip-cloud-control/v1"; export const CLOUD_CONTROL_JWS_TYPE = "paperclip-cloud-control+jwt"; export const CLOUD_CONTROL_ACTIONS = [ "ephemeral-enrollment:prepare", + "ephemeral-enrollment:eligibility", "task-drain:read", "task-drain:read-body", "task-drain:start", diff --git a/server/src/services/ephemeral-enrollment.ts b/server/src/services/ephemeral-enrollment.ts index 415fdad5243..18376cf2d22 100644 --- a/server/src/services/ephemeral-enrollment.ts +++ b/server/src/services/ephemeral-enrollment.ts @@ -9,6 +9,11 @@ import { cloudTenantPrimaryCompanyId, getCloudStackContext } from "./cloud-insta import { resolveDecisionSigningSecret } from "./decision-signing.js"; import { assertEphemeralServerConfig, ephemeralServerEnabled, readEphemeralServerDataEligibility } from "./ephemeral-server-contract.js"; +export async function readOnlineEphemeralEnrollmentEligibility(db: Db) { + const { eligible, blockers } = await readEphemeralServerDataEligibility(db, { purpose: "startup" }); + return { eligible, blockers, ephemeralServer: ephemeralServerEnabled() }; +} + export async function prepareOnlineEphemeralEnrollment(db: Db, attemptId: string, control: Pick) { const stackId = getCloudStackContext()?.stackId; const commit = readBuildCommit();