Skip to content

Commit 7fe1f93

Browse files
committed
fix(daytona): harden brokered fallback
1 parent dd0bd80 commit 7fe1f93

7 files changed

Lines changed: 195 additions & 3 deletions

File tree

‎internal/cli/doctor_test.go‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,21 @@ func TestDoctorChecksProviderReadinessForCoordinatorSupportedDaytona(t *testing.
650650
_ = json.NewEncoder(w).Encode(CoordinatorWhoami{Auth: "token", Owner: "[email protected]", Org: "example-org"})
651651
case "/v1/providers/daytona/readiness":
652652
readinessCalled = true
653-
_ = json.NewEncoder(w).Encode(CoordinatorProviderReadiness{Provider: "daytona", Configured: true})
653+
_ = json.NewEncoder(w).Encode(CoordinatorProviderReadiness{
654+
Provider: "daytona",
655+
Configured: true,
656+
Checks: []DoctorCheck{{
657+
Status: "ok",
658+
Check: "daytona-fallback",
659+
Message: "auth=ready control_plane=ready inventory=ready leases=0 mutation=false",
660+
Details: map[string]string{
661+
"client_auth": "crabbox",
662+
"control_plane": "coordinator",
663+
"data_plane": "ssh-rsync",
664+
"mutation": "false",
665+
},
666+
}},
667+
})
654668
default:
655669
http.Error(w, "unexpected "+r.URL.Path, http.StatusBadRequest)
656670
}
@@ -670,6 +684,9 @@ func TestDoctorChecksProviderReadinessForCoordinatorSupportedDaytona(t *testing.
670684
if !strings.Contains(stdout.String(), "ok provider provider=daytona coordinator_secrets=ready") {
671685
t.Fatalf("missing Daytona readiness: %q", stdout.String())
672686
}
687+
if !strings.Contains(stdout.String(), "ok daytona-fallback auth=ready control_plane=ready inventory=ready leases=0 mutation=false") {
688+
t.Fatalf("missing Daytona fallback readiness: %q", stdout.String())
689+
}
673690
}
674691

675692
func testCrabboxUserToken(t *testing.T, expiresAt time.Time) string {

‎internal/cli/provider_backend_test.go‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,21 @@ func TestLoadBackendWrapsCoordinatorOnlyForSupportedSSHProviders(t *testing.T) {
323323
t.Fatalf("backend=%T, want coordinatorLeaseBackend", backend)
324324
}
325325

326+
t.Setenv("HOME", t.TempDir())
327+
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
328+
t.Setenv("DAYTONA_API_KEY", "")
329+
t.Setenv("DAYTONA_JWT_TOKEN", "")
330+
t.Setenv("CRABBOX_DAYTONA_API_KEY", "")
331+
t.Setenv("CRABBOX_DAYTONA_JWT_TOKEN", "")
332+
cfg.Provider = "daytona"
333+
backend, err = loadBackend(cfg, testRuntimeWithRunner(&recordingCommandRunner{}))
334+
if err != nil {
335+
t.Fatalf("load Daytona coordinator backend without client Daytona auth: %v", err)
336+
}
337+
if _, ok := backend.(*coordinatorLeaseBackend); !ok {
338+
t.Fatalf("backend=%T, want coordinatorLeaseBackend", backend)
339+
}
340+
326341
cfg.Provider = "ssh"
327342
backend, err = loadBackend(cfg, testRuntimeWithRunner(&recordingCommandRunner{}))
328343
if err != nil {

‎internal/providers/daytona/provider_test.go‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,18 @@ func TestProviderSupportsCoordinator(t *testing.T) {
1616
t.Fatalf("features=%v missing %s", spec.Features, feature)
1717
}
1818
}
19+
for _, unsupported := range []core.Feature{
20+
core.FeatureDesktop,
21+
core.FeatureBrowser,
22+
core.FeatureCode,
23+
core.FeatureTailscale,
24+
core.FeatureCheckpoint,
25+
core.FeatureFork,
26+
core.FeatureRestore,
27+
core.FeatureSnapshot,
28+
} {
29+
if spec.Features.Has(unsupported) {
30+
t.Fatalf("features=%v unexpectedly includes %s", spec.Features, unsupported)
31+
}
32+
}
1933
}

‎worker/src/daytona.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,13 @@ export class DaytonaClient {
162162
}
163163

164164
async deleteServer(id: string): Promise<void> {
165-
await this.request<unknown>("DELETE", `/sandbox/${encodeURIComponent(id)}`);
165+
try {
166+
await this.request<unknown>("DELETE", `/sandbox/${encodeURIComponent(id)}`);
167+
} catch (error) {
168+
// A prior cleanup or Daytona-side expiry already reached the desired state.
169+
// Treating 404 as success keeps release and rollback retries idempotent.
170+
if (!isDaytonaNotFound(error)) throw error;
171+
}
166172
}
167173

168174
async createSSHAccess(

‎worker/src/fleet.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6735,6 +6735,8 @@ export class FleetCoordinator {
67356735
);
67366736
if (provider === "aws" && readiness.configured && !this.testProviders.aws) {
67376737
readiness.checks = await this.awsProviderCapacityChecks(url.searchParams);
6738+
} else if (provider === "daytona" && readiness.configured && !this.testProviders.daytona) {
6739+
readiness.checks = await new DaytonaProvider(this.env).readinessChecks();
67386740
}
67396741
return json(readiness);
67406742
}
@@ -20843,6 +20845,37 @@ export class DaytonaProvider implements CloudProvider {
2084320845
return this.client.listCrabboxServers();
2084420846
}
2084520847

20848+
async readinessChecks(): Promise<ProviderReadinessCheck[]> {
20849+
const details = {
20850+
api: "list",
20851+
mutation: "false",
20852+
client_auth: "crabbox",
20853+
control_plane: "coordinator",
20854+
data_plane: "ssh-rsync",
20855+
snapshot: this.env.CRABBOX_DAYTONA_SNAPSHOT?.trim() ? "configured" : "account-default",
20856+
};
20857+
try {
20858+
const servers = await this.client.listCrabboxServers();
20859+
return [
20860+
{
20861+
status: "ok",
20862+
check: "daytona-fallback",
20863+
message: `auth=ready control_plane=ready inventory=ready leases=${servers.length} mutation=false`,
20864+
details: { ...details, leases: String(servers.length) },
20865+
},
20866+
];
20867+
} catch (error) {
20868+
return [
20869+
{
20870+
status: "failed",
20871+
check: "daytona-fallback",
20872+
message: `auth_or_control_plane=failed mutation=false ${coordinatorErrorMessage(this.env, error)}`,
20873+
details,
20874+
},
20875+
];
20876+
}
20877+
}
20878+
2084620879
supportsSSHHostKeyInjection(): boolean {
2084720880
return false;
2084820881
}

‎worker/test/daytona.test.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,19 @@ describe("daytona coordinator client", () => {
169169
expect(diagnostic).toContain("route=iad");
170170
});
171171

172+
it("treats an already deleted sandbox as successful cleanup", async () => {
173+
const client = new DaytonaClient(baseEnv);
174+
client.fetcher = vi
175+
.fn<typeof fetch>()
176+
.mockResolvedValueOnce(new Response(null, { status: 404 }))
177+
.mockResolvedValueOnce(new Response(null, { status: 204 }))
178+
.mockResolvedValueOnce(new Response("unavailable", { status: 503 }));
179+
180+
await expect(client.deleteServer("sandbox-one")).resolves.toBeUndefined();
181+
await expect(client.deleteServer("sandbox-one")).resolves.toBeUndefined();
182+
await expect(client.deleteServer("sandbox-one")).rejects.toThrow("http 503");
183+
});
184+
172185
it.each(["started", "running", "ready", "active", " Active "])(
173186
"accepts Daytona ready state %j",
174187
async (state) => {

‎worker/test/fleet.test.ts‎

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28023,13 +28023,59 @@ describe("fleet identity", () => {
2802328023
missing: ["DAYTONA_CRABBOX_KEY"],
2802428024
});
2802528025

28026-
const daytonaFleet = testFleet(undefined, {}, { DAYTONA_CRABBOX_KEY: "live-key" });
28026+
const daytonaRequests: Request[] = [];
28027+
vi.stubGlobal(
28028+
"fetch",
28029+
vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => {
28030+
const daytonaRequest = input instanceof Request ? input : new Request(input, init);
28031+
daytonaRequests.push(daytonaRequest.clone());
28032+
return Response.json({
28033+
items: [
28034+
{
28035+
id: "sandbox-one",
28036+
name: "crabbox-one",
28037+
state: "started",
28038+
labels: { crabbox: "true" },
28039+
},
28040+
],
28041+
nextCursor: null,
28042+
});
28043+
}),
28044+
);
28045+
const daytonaFleet = testFleet(
28046+
undefined,
28047+
{},
28048+
{
28049+
DAYTONA_CRABBOX_KEY: "live-key",
28050+
CRABBOX_DAYTONA_API_URL: "https://daytona.example/api",
28051+
CRABBOX_DAYTONA_SNAPSHOT: "crabbox-ready",
28052+
},
28053+
);
2802728054
const daytona = await daytonaFleet.fetch(request("GET", "/v1/providers/daytona/readiness"));
2802828055
await expect(daytona.json()).resolves.toMatchObject({
2802928056
provider: "daytona",
2803028057
configured: true,
2803128058
missing: [],
28059+
checks: [
28060+
{
28061+
status: "ok",
28062+
check: "daytona-fallback",
28063+
details: {
28064+
api: "list",
28065+
mutation: "false",
28066+
client_auth: "crabbox",
28067+
control_plane: "coordinator",
28068+
data_plane: "ssh-rsync",
28069+
snapshot: "configured",
28070+
leases: "1",
28071+
},
28072+
},
28073+
],
2803228074
});
28075+
expect(daytonaRequests).toHaveLength(1);
28076+
expect(daytonaRequests[0]?.method).toBe("GET");
28077+
expect(daytonaRequests[0]?.headers.get("authorization")).toBe("Bearer live-key");
28078+
expect(new URL(daytonaRequests[0]!.url).pathname).toBe("/api/sandbox");
2803328079
});
2803428080

2803528081
it("reports AWS capacity quota readiness before a lease is requested", async () => {
@@ -28082,6 +28128,54 @@ describe("fleet identity", () => {
2808228128
);
2808328129
});
2808428130

28131+
it("reports a configured but unusable Daytona fallback without leaking its key", async () => {
28132+
vi.stubGlobal(
28133+
"fetch",
28134+
vi.fn(
28135+
async () =>
28136+
new Response(
28137+
JSON.stringify({
28138+
error: "unauthorized",
28139+
workerKey: "daytona-live-secret",
28140+
}),
28141+
{ status: 401 },
28142+
),
28143+
),
28144+
);
28145+
const fleet = testFleet(
28146+
undefined,
28147+
{},
28148+
{
28149+
DAYTONA_CRABBOX_KEY: "daytona-live-secret",
28150+
CRABBOX_DAYTONA_API_URL: "https://daytona.example/api",
28151+
},
28152+
);
28153+
28154+
const response = await fleet.fetch(request("GET", "/v1/providers/daytona/readiness"));
28155+
expect(response.status).toBe(200);
28156+
const body = await response.text();
28157+
expect(body).not.toContain("daytona-live-secret");
28158+
expect(JSON.parse(body)).toMatchObject({
28159+
provider: "daytona",
28160+
configured: true,
28161+
missing: [],
28162+
checks: [
28163+
{
28164+
status: "failed",
28165+
check: "daytona-fallback",
28166+
details: {
28167+
api: "list",
28168+
mutation: "false",
28169+
client_auth: "crabbox",
28170+
control_plane: "coordinator",
28171+
data_plane: "ssh-rsync",
28172+
snapshot: "account-default",
28173+
},
28174+
},
28175+
],
28176+
});
28177+
});
28178+
2808528179
it("rejects path-bearing AWS regions before signed requests", async () => {
2808628180
const fetchMock = vi.fn<typeof fetch>();
2808728181
vi.stubGlobal("fetch", fetchMock);

0 commit comments

Comments
 (0)