Files
t3code/apps/server/src/cloud/CloudLink.lifecycle.test.ts
T

1218 lines
48 KiB
TypeScript

import * as NodeServices from "@effect/platform-node/NodeServices";
import { describe, expect, it } from "@effect/vitest";
import * as Deferred from "effect/Deferred";
import * as Duration from "effect/Duration";
import * as Effect from "effect/Effect";
import * as Fiber from "effect/Fiber";
import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as Path from "effect/Path";
import * as PlatformError from "effect/PlatformError";
import * as Schema from "effect/Schema";
import * as TestClock from "effect/testing/TestClock";
import * as Stream from "effect/Stream";
import { HttpClient, HttpClientResponse, HttpServer, type HttpClientRequest } from "effect/http";
import * as NetAddress from "effect/net/NetAddress";
import { DESKTOP_UPDATE_RESTART_MARKER_FILE, EnvironmentId } from "@t3tools/contracts";
import * as EnvironmentAuth from "../auth/EnvironmentAuth.ts";
import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
import * as ServerConfigModule from "../config.ts";
import { writeServiceState } from "../serviceLauncher.ts";
import {
SERVICE_LAUNCHER_PROTOCOL,
SERVICE_STATE_FILE,
SERVICE_STOP_MARKER_FILE,
type ServiceUpdateRecord,
} from "./serviceProtocol.ts";
import * as ServerEnvironment from "../environment/ServerEnvironment.ts";
import * as AgentAwarenessRelay from "../relay/AgentAwarenessRelay.ts";
import { CLOUD_CLI_DESIRED_LINK_SECRET } from "./CliState.ts";
import * as CliTokenManager from "./CliTokenManager.ts";
import { RelayManagedEndpointRecoveryRegistrationRequest } from "@t3tools/contracts/relay";
import {
CLOUD_ENDPOINT_CONFIRMED_ORIGIN,
CLOUD_ENDPOINT_RUNTIME_CONFIG,
CLOUD_LINKED_USER_ID,
decodeConfirmedOrigin,
decodeRuntimeConfig,
RELAY_ENVIRONMENT_CREDENTIAL_SECRET,
RELAY_ISSUER_SECRET,
RELAY_URL_SECRET,
} from "./config.ts";
import * as CloudLink from "./CloudLink.ts";
import { consumeCloudReplayGuards } from "./CloudLink.ts";
import { pendingServiceUpdateExists } from "./updateHandoff.ts";
import {
managedTunnelStartupAction,
retryManagedTunnelRegistration,
} from "./managedTunnelStartup.ts";
import * as ManagedEndpointRuntime from "./ManagedEndpointRuntime.ts";
const storeFailure = (tag: "AlreadyExists" | "PermissionDenied") =>
new ServerSecretStore.SecretStorePersistError({
resource: "cloud replay guard",
cause: PlatformError.systemError({
_tag: tag,
module: "FileSystem",
method: "open",
pathOrDescriptor: "cloud-replay-guard.bin",
}),
});
const unusedSecretStoreOperation = () => Effect.die("unused secret-store operation");
// Linking wakes the awareness relay; these tests do not run it.
const idleAwarenessRelay = AgentAwarenessRelay.AgentAwarenessRelay.of({
publishThread: () => Effect.void,
drain: Effect.void,
requestCatchUp: () => Effect.void,
start: () => Effect.void,
});
// The connect routes register recovery against the listening port; these tests
// call the service directly.
const idleHttpServer = HttpServer.HttpServer.of({
address: NetAddress.inetAddressFromIpStringUnsafe("127.0.0.1", 3773),
serve: (() => Effect.void) as HttpServer.HttpServer["Service"]["serve"],
});
const decodeManagedTunnelRecoveryRegistration = Schema.decodeUnknownEffect(
Schema.fromJsonString(RelayManagedEndpointRecoveryRegistrationRequest),
);
function makeSecretStore(
create: ServerSecretStore.ServerSecretStore["Service"]["create"],
): ServerSecretStore.ServerSecretStore["Service"] {
return {
get: unusedSecretStoreOperation,
set: unusedSecretStoreOperation,
create,
getOrCreateRandom: unusedSecretStoreOperation,
remove: unusedSecretStoreOperation,
};
}
it("preserves messages surfaced by cloud 500 responses", () => {
const cause = new Error("cloud operation failed");
expect([
new EnvironmentAuth.ServerAuthLinkedCloudAccountVerificationError({ cause }).message,
new EnvironmentAuth.ServerAuthLinkedCloudAccountReadError({ cause }).message,
new EnvironmentAuth.ServerAuthLinkedCloudAccountMissingError({}).message,
new EnvironmentAuth.ServerAuthCloudLinkJwtSigningError({ cause }).message,
new EnvironmentAuth.ServerAuthCloudMintPublicKeyMissingError({}).message,
new EnvironmentAuth.ServerAuthCloudRelayIssuerMissingError({}).message,
new EnvironmentAuth.ServerAuthCloudHealthJwtSigningError({ cause }).message,
new EnvironmentAuth.ServerAuthCloudMintJwtSigningError({ cause }).message,
]).toEqual([
"Could not verify the linked cloud account.",
"Could not read the linked cloud account.",
"Cloud linked user is not installed for this environment.",
"Failed to sign cloud link JWT.",
"Cloud mint public key is not installed for this environment.",
"Cloud relay issuer is not installed for this environment.",
"Failed to sign cloud health JWT.",
"Failed to sign cloud mint JWT.",
]);
});
describe("consumeCloudReplayGuards", () => {
it.effect("reports already-created guards as replay conflicts", () =>
Effect.gen(function* () {
const consumed = yield* consumeCloudReplayGuards({
secrets: makeSecretStore(() => Effect.fail(storeFailure("AlreadyExists"))),
names: ["cloud-jti", "cloud-nonce"],
value: new Uint8Array(),
});
expect(consumed).toBe(false);
}),
);
it.effect("preserves replay-store availability failures", () =>
Effect.gen(function* () {
const failure = storeFailure("PermissionDenied");
const error = yield* Effect.flip(
consumeCloudReplayGuards({
secrets: makeSecretStore(() => Effect.fail(failure)),
names: ["cloud-jti", "cloud-nonce"],
value: new Uint8Array(),
}),
);
expect(error).toBe(failure);
}),
);
});
describe("reconcileDesiredCloudLink", () => {
it.effect("requires stored CLI authorization without exposing an HTTP endpoint", () =>
Effect.gen(function* () {
const link = yield* CloudLink.CloudLink;
const error = yield* Effect.flip(link.reconcileDesiredLink("http://127.0.0.1:3774"));
expect(error._tag).toBe("CloudLinkAuthorizationMissingError");
}).pipe(
Effect.provide(CloudLink.layer),
Effect.provideService(HttpServer.HttpServer, idleHttpServer),
Effect.provideService(
ServerConfigModule.ServerConfig,
ServerConfigModule.ServerConfig.of({} as ServerConfigModule.ServerConfig["Service"]),
),
Effect.provideService(
ServerSecretStore.ServerSecretStore,
makeSecretStore(unusedSecretStoreOperation),
),
Effect.provideService(
ServerEnvironment.ServerEnvironment,
ServerEnvironment.ServerEnvironment.of({
getEnvironmentId: unusedSecretStoreOperation(),
getDescriptor: unusedSecretStoreOperation(),
}),
),
Effect.provideService(
ManagedEndpointRuntime.CloudManagedEndpointRuntime,
ManagedEndpointRuntime.CloudManagedEndpointRuntime.of({
applyConfig: unusedSecretStoreOperation,
recoveryRequests: Stream.empty,
requestRecovery: () => Effect.void,
tunnelConnected: Stream.empty,
withLinkStateLock: (effect) => effect,
} satisfies ManagedEndpointRuntime.CloudManagedEndpointRuntime["Service"]),
),
Effect.provideService(
EnvironmentAuth.EnvironmentAuth,
EnvironmentAuth.EnvironmentAuth.of({} as EnvironmentAuth.EnvironmentAuth["Service"]),
),
Effect.provideService(
CliTokenManager.CloudCliTokenManager,
CliTokenManager.CloudCliTokenManager.of({
get: unusedSecretStoreOperation(),
getExisting: Effect.succeedNone,
hasCredential: unusedSecretStoreOperation(),
store: () => unusedSecretStoreOperation(),
clear: unusedSecretStoreOperation(),
}),
),
Effect.provideService(
HttpClient.HttpClient,
HttpClient.make(() => unusedSecretStoreOperation()),
),
Effect.provideService(AgentAwarenessRelay.AgentAwarenessRelay, idleAwarenessRelay),
Effect.provide(NodeServices.layer),
),
);
});
describe("releaseManagedTunnelOnShutdown", () => {
const cliToken: CliTokenManager.PersistedToken = {
accessToken: "cli-access-token",
refreshToken: "cli-refresh-token",
expiresAtEpochMs: Number.MAX_SAFE_INTEGER,
};
function makeMemorySecretStore(initial: Iterable<readonly [string, string]> = []) {
const values = new Map<string, Uint8Array>(
Array.from(initial, ([name, value]) => [name, new TextEncoder().encode(value)] as const),
);
const store: ServerSecretStore.ServerSecretStore["Service"] = {
get: (name) => Effect.sync(() => Option.fromNullishOr(values.get(name))),
set: (name, value) =>
Effect.sync(() => {
values.set(name, value);
}),
create: (name, value) =>
Effect.sync(() => {
values.set(name, value);
}),
getOrCreateRandom: unusedSecretStoreOperation,
remove: (name) =>
Effect.sync(() => {
values.delete(name);
}),
};
return { store, values };
}
interface ReleaseHarness {
readonly store: ServerSecretStore.ServerSecretStore["Service"];
readonly applyConfigCalls: Array<unknown>;
readonly requests: Array<HttpClientRequest.HttpClientRequest>;
readonly onRequest?: (request: HttpClientRequest.HttpClientRequest) => Effect.Effect<void>;
readonly respond?: () => Response;
readonly respondEffect?: Effect.Effect<Response>;
}
// Writes the launcher's durable state file into this test's baseDir with
// the launcher's own writer; the release reads it to detect an in-flight
// update handoff.
const writeLauncherState = (update: ServiceUpdateRecord) =>
Effect.gen(function* () {
const path = yield* Path.Path;
const config = yield* ServerConfigModule.ServerConfig;
const statePath = path.join(config.baseDir, "runtime", SERVICE_STATE_FILE);
yield* Effect.promise(() =>
writeServiceState(statePath, {
protocol: SERVICE_LAUNCHER_PROTOCOL,
activeVersion: "0.0.30",
update,
}),
);
});
// Writes the marker the desktop app leaves just before it stops its backend
// to install an update, and returns when it was written.
const writeDesktopUpdateRestartMarker = Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const config = yield* ServerConfigModule.ServerConfig;
const runtimeDir = path.join(config.baseDir, "runtime");
const markerPath = path.join(runtimeDir, DESKTOP_UPDATE_RESTART_MARKER_FILE);
yield* fs.makeDirectory(runtimeDir, { recursive: true });
yield* fs.writeFileString(markerPath, "");
const { mtime } = yield* fs.stat(markerPath);
return Option.getOrThrow(mtime).getTime();
});
const provideReleaseHarness =
(harness: ReleaseHarness) =>
<A, E, R>(effect: Effect.Effect<A, E, R>) =>
effect.pipe(
Effect.provide(CloudLink.layer),
Effect.provideService(HttpServer.HttpServer, idleHttpServer),
Effect.provideService(ServerSecretStore.ServerSecretStore, harness.store),
Effect.provideService(AgentAwarenessRelay.AgentAwarenessRelay, idleAwarenessRelay),
Effect.provideService(
ServerEnvironment.ServerEnvironment,
ServerEnvironment.ServerEnvironment.of({
getEnvironmentId: Effect.succeed(EnvironmentId.make("env_123")),
getDescriptor: Effect.die("unused"),
}),
),
Effect.provideService(
ManagedEndpointRuntime.CloudManagedEndpointRuntime,
ManagedEndpointRuntime.CloudManagedEndpointRuntime.of({
applyConfig: (config) =>
Effect.sync(() => {
harness.applyConfigCalls.push(config);
return config === null
? ({
status: "disabled",
} satisfies ManagedEndpointRuntime.CloudManagedEndpointRuntimeStatus)
: ({
status: "running",
providerKind: "cloudflare_tunnel",
pid: 123,
} satisfies ManagedEndpointRuntime.CloudManagedEndpointRuntimeStatus);
}),
recoveryRequests: Stream.empty,
requestRecovery: () => Effect.void,
tunnelConnected: Stream.empty,
withLinkStateLock: (effect) => effect,
}),
),
Effect.provideService(
EnvironmentAuth.EnvironmentAuth,
EnvironmentAuth.EnvironmentAuth.of({} as EnvironmentAuth.EnvironmentAuth["Service"]),
),
Effect.provideService(
CliTokenManager.CloudCliTokenManager,
CliTokenManager.CloudCliTokenManager.of({
get: unusedSecretStoreOperation(),
getExisting: Effect.succeedSome(cliToken),
hasCredential: unusedSecretStoreOperation(),
store: () => unusedSecretStoreOperation(),
clear: unusedSecretStoreOperation(),
}),
),
Effect.provideService(
HttpClient.HttpClient,
HttpClient.make((request) =>
Effect.sync(() => {
harness.requests.push(request);
}).pipe(
Effect.andThen(harness.onRequest?.(request) ?? Effect.void),
Effect.andThen(
harness.respondEffect ??
Effect.sync(() => (harness.respond ?? (() => Response.json({ ok: true })))()),
),
Effect.map((response) => HttpClientResponse.fromWeb(request, response)),
),
),
),
// The release consults the launcher state file under the configured
// baseDir, so every harness run gets a scoped temp baseDir.
Effect.provide(
ServerConfigModule.layerTest("/", { prefix: "t3-http-release-test-" }).pipe(
Layer.provideMerge(NodeServices.layer),
),
),
Effect.scoped,
);
// The persisted state of a CLI-managed link whose tunnel is releasable.
const managedLinkSecrets = [
[CLOUD_ENDPOINT_RUNTIME_CONFIG, "runtime-config"],
[CLOUD_ENDPOINT_CONFIRMED_ORIGIN, "confirmed-origin"],
[RELAY_URL_SECRET, "https://relay.example.test"],
[CLOUD_CLI_DESIRED_LINK_SECRET, "managed"],
] as const;
it.effect("does not recreate a link that was unlinked while startup registration retried", () => {
const { store, values } = makeMemorySecretStore(managedLinkSecrets);
const applyConfigCalls: Array<unknown> = [];
const requests: Array<HttpClientRequest.HttpClientRequest> = [];
return Effect.gen(function* () {
const link = yield* CloudLink.CloudLink;
// Registration started while this marker existed. Unlink removes it
// before startup receives the relay's final not_linked response.
values.delete(CLOUD_CLI_DESIRED_LINK_SECRET);
expect(yield* link.reconcileDesiredLinkIfStillDesired("http://127.0.0.1:3773")).toBeNull();
expect(requests).toEqual([]);
expect(applyConfigCalls).toEqual([]);
}).pipe(provideReleaseHarness({ store, applyConfigCalls, requests }));
});
it.effect("stops the connector, releases the relay tunnel, and drops the dead token", () => {
const { store, values } = makeMemorySecretStore(managedLinkSecrets);
const applyConfigCalls: Array<unknown> = [];
const requests: Array<HttpClientRequest.HttpClientRequest> = [];
return Effect.gen(function* () {
const link = yield* CloudLink.CloudLink;
const released = yield* link.releaseManagedTunnelOnShutdown();
expect(released).toBe(true);
expect(applyConfigCalls).toEqual([null]);
expect(requests).toHaveLength(1);
const request = requests[0]!;
expect(request.method).toBe("DELETE");
expect(request.url).toBe(
"https://relay.example.test/v1/client/environment-links/env_123/tunnel",
);
expect(request.headers.authorization).toBe("Bearer cli-access-token");
expect(values.has(CLOUD_ENDPOINT_RUNTIME_CONFIG)).toBe(false);
expect(values.has(CLOUD_ENDPOINT_CONFIRMED_ORIGIN)).toBe(false);
}).pipe(provideReleaseHarness({ store, applyConfigCalls, requests }));
});
it.effect("does nothing for links without a stored managed tunnel runtime config", () => {
const { store } = makeMemorySecretStore();
const applyConfigCalls: Array<unknown> = [];
const requests: Array<HttpClientRequest.HttpClientRequest> = [];
return Effect.gen(function* () {
const link = yield* CloudLink.CloudLink;
const released = yield* link.releaseManagedTunnelOnShutdown();
expect(released).toBe(false);
expect(applyConfigCalls).toEqual([]);
expect(requests).toEqual([]);
}).pipe(provideReleaseHarness({ store, applyConfigCalls, requests }));
});
it.effect("leaves the tunnel of a web/mobile-installed link untouched", () => {
// A managed runtime config without a CLI-desired link: the environment was
// linked by a web/mobile client, and nothing re-provisions the tunnel on
// the next boot, so shutdown must not release it.
const { store, values } = makeMemorySecretStore([
[CLOUD_ENDPOINT_RUNTIME_CONFIG, "runtime-config"],
[RELAY_URL_SECRET, "https://relay.example.test"],
]);
const applyConfigCalls: Array<unknown> = [];
const requests: Array<HttpClientRequest.HttpClientRequest> = [];
return Effect.gen(function* () {
const link = yield* CloudLink.CloudLink;
const released = yield* link.releaseManagedTunnelOnShutdown();
expect(released).toBe(false);
expect(applyConfigCalls).toEqual([]);
expect(requests).toEqual([]);
expect(values.has(CLOUD_ENDPOINT_RUNTIME_CONFIG)).toBe(true);
}).pipe(provideReleaseHarness({ store, applyConfigCalls, requests }));
});
it.effect("leaves the tunnel of a publish-only desired link untouched", () => {
const { store, values } = makeMemorySecretStore([
[CLOUD_ENDPOINT_RUNTIME_CONFIG, "runtime-config"],
[RELAY_URL_SECRET, "https://relay.example.test"],
[CLOUD_CLI_DESIRED_LINK_SECRET, "publish_only"],
]);
const applyConfigCalls: Array<unknown> = [];
const requests: Array<HttpClientRequest.HttpClientRequest> = [];
return Effect.gen(function* () {
const link = yield* CloudLink.CloudLink;
const released = yield* link.releaseManagedTunnelOnShutdown();
expect(released).toBe(false);
expect(applyConfigCalls).toEqual([]);
expect(requests).toEqual([]);
expect(values.has(CLOUD_ENDPOINT_RUNTIME_CONFIG)).toBe(true);
}).pipe(provideReleaseHarness({ store, applyConfigCalls, requests }));
});
it.effect("keeps the tunnel when shutdown hands off to a pending update", () => {
const { store, values } = makeMemorySecretStore(managedLinkSecrets);
const applyConfigCalls: Array<unknown> = [];
const requests: Array<HttpClientRequest.HttpClientRequest> = [];
return Effect.gen(function* () {
const link = yield* CloudLink.CloudLink;
yield* writeLauncherState({
id: "update-1",
fromVersion: "0.0.30",
targetVersion: "0.0.31",
dbPath: "/tmp/state.sqlite",
status: "pending",
});
const released = yield* link.releaseManagedTunnelOnShutdown();
// The launcher restarts a server immediately, so the tunnel is not
// orphaned; keeping it avoids the hostname route re-propagation that
// dominates update downtime. The stored config must survive so the
// next boot respawns the connector against the same tunnel.
expect(released).toBe(false);
expect(applyConfigCalls).toEqual([]);
expect(requests).toEqual([]);
expect(values.has(CLOUD_ENDPOINT_RUNTIME_CONFIG)).toBe(true);
}).pipe(provideReleaseHarness({ store, applyConfigCalls, requests }));
});
it.effect("keeps the tunnel once when the desktop app restarts it for an update", () => {
const { store, values } = makeMemorySecretStore(managedLinkSecrets);
const applyConfigCalls: Array<unknown> = [];
const requests: Array<HttpClientRequest.HttpClientRequest> = [];
return Effect.gen(function* () {
const link = yield* CloudLink.CloudLink;
yield* TestClock.setTime(yield* writeDesktopUpdateRestartMarker);
expect(yield* link.releaseManagedTunnelOnShutdown()).toBe(false);
expect(requests).toEqual([]);
expect(values.has(CLOUD_ENDPOINT_RUNTIME_CONFIG)).toBe(true);
// The shutdown consumed the marker, so a later quit releases the tunnel.
expect(yield* link.releaseManagedTunnelOnShutdown()).toBe(true);
expect(requests).toHaveLength(1);
}).pipe(provideReleaseHarness({ store, applyConfigCalls, requests }));
});
it.effect("releases the tunnel when the desktop update marker is stale", () => {
const { store } = makeMemorySecretStore(managedLinkSecrets);
const applyConfigCalls: Array<unknown> = [];
const requests: Array<HttpClientRequest.HttpClientRequest> = [];
return Effect.gen(function* () {
const link = yield* CloudLink.CloudLink;
const writtenAt = yield* writeDesktopUpdateRestartMarker;
yield* TestClock.setTime(writtenAt + Duration.toMillis(Duration.minutes(2)));
expect(yield* link.releaseManagedTunnelOnShutdown()).toBe(true);
expect(requests).toHaveLength(1);
}).pipe(provideReleaseHarness({ store, applyConfigCalls, requests }));
});
it.effect("still releases a pending update when the launcher is stopping", () => {
// `t3 service uninstall` or `systemctl stop` during the pending window:
// the launcher writes its stop marker before signalling the child, so no
// replacement server is coming and the tunnel must not be kept.
const { store, values } = makeMemorySecretStore(managedLinkSecrets);
const applyConfigCalls: Array<unknown> = [];
const requests: Array<HttpClientRequest.HttpClientRequest> = [];
return Effect.gen(function* () {
const link = yield* CloudLink.CloudLink;
yield* writeLauncherState({
id: "update-1",
fromVersion: "0.0.30",
targetVersion: "0.0.31",
dbPath: "/tmp/state.sqlite",
status: "pending",
});
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const config = yield* ServerConfigModule.ServerConfig;
yield* fs.writeFileString(path.join(config.baseDir, "runtime", SERVICE_STOP_MARKER_FILE), "");
expect(yield* pendingServiceUpdateExists).toBe(true);
const released = yield* link.releaseManagedTunnelOnShutdown();
expect(released).toBe(true);
expect(requests).toHaveLength(1);
expect(values.has(CLOUD_ENDPOINT_RUNTIME_CONFIG)).toBe(false);
}).pipe(provideReleaseHarness({ store, applyConfigCalls, requests }));
});
it.effect("still releases when the recorded update already settled", () => {
const { store, values } = makeMemorySecretStore(managedLinkSecrets);
const applyConfigCalls: Array<unknown> = [];
const requests: Array<HttpClientRequest.HttpClientRequest> = [];
return Effect.gen(function* () {
const link = yield* CloudLink.CloudLink;
yield* writeLauncherState({
id: "update-1",
fromVersion: "0.0.30",
targetVersion: "0.0.31",
status: "committed",
});
const released = yield* link.releaseManagedTunnelOnShutdown();
expect(released).toBe(true);
expect(requests).toHaveLength(1);
expect(values.has(CLOUD_ENDPOINT_RUNTIME_CONFIG)).toBe(false);
}).pipe(provideReleaseHarness({ store, applyConfigCalls, requests }));
});
it.effect("keeps a runtime config that a fast restart replaced mid-release", () => {
const { store, values } = makeMemorySecretStore(managedLinkSecrets);
const applyConfigCalls: Array<unknown> = [];
const requests: Array<HttpClientRequest.HttpClientRequest> = [];
const freshConfig = new TextEncoder().encode("fresh-runtime-config");
return Effect.gen(function* () {
const link = yield* CloudLink.CloudLink;
const released = yield* link.releaseManagedTunnelOnShutdown();
expect(released).toBe(true);
// The finalizer only drops the config it released; the one written by
// the restarted process while the DELETE was in flight stays.
expect(values.get(CLOUD_ENDPOINT_RUNTIME_CONFIG)).toBe(freshConfig);
}).pipe(
provideReleaseHarness({
store,
applyConfigCalls,
requests,
respond: () => {
// A restarted process reconciled and stored a fresh connector config
// while this shutdown's release request was in flight.
values.set(CLOUD_ENDPOINT_RUNTIME_CONFIG, freshConfig);
return Response.json({ ok: true });
},
}),
);
});
it.effect("keeps the stored connector token when the relay skipped the release", () => {
// ok:false means a concurrent provision owns the recorded tunnel, so the
// stored runtime config (possibly freshly written by that provision) must
// survive.
const { store, values } = makeMemorySecretStore(managedLinkSecrets);
const applyConfigCalls: Array<unknown> = [];
const requests: Array<HttpClientRequest.HttpClientRequest> = [];
return Effect.gen(function* () {
const link = yield* CloudLink.CloudLink;
const released = yield* link.releaseManagedTunnelOnShutdown();
expect(released).toBe(false);
expect(requests).toHaveLength(1);
expect(values.has(CLOUD_ENDPOINT_RUNTIME_CONFIG)).toBe(true);
}).pipe(
provideReleaseHarness({
store,
applyConfigCalls,
requests,
respond: () => Response.json({ ok: false }),
}),
);
});
it.effect("keeps the stored connector token when the relay release request fails", () => {
const { store, values } = makeMemorySecretStore(managedLinkSecrets);
const applyConfigCalls: Array<unknown> = [];
const requests: Array<HttpClientRequest.HttpClientRequest> = [];
return Effect.gen(function* () {
const link = yield* CloudLink.CloudLink;
const result = yield* Effect.result(link.releaseManagedTunnelOnShutdown());
expect(result._tag).toBe("Failure");
expect(requests).toHaveLength(1);
// The tunnel still exists, so the stored token stays valid across the
// restart and the next boot can bring the connector back immediately.
expect(values.has(CLOUD_ENDPOINT_RUNTIME_CONFIG)).toBe(true);
}).pipe(
provideReleaseHarness({
store,
applyConfigCalls,
requests,
respond: () => Response.json({ ok: false }, { status: 503 }),
}),
);
});
it.effect("registers an existing tunnel and starts the confirmed connector", () => {
const { store } = makeMemorySecretStore([
[
CLOUD_ENDPOINT_RUNTIME_CONFIG,
'{"providerKind":"cloudflare_tunnel","connectorToken":"existing-token","tunnelId":"existing-tunnel"}',
],
[RELAY_URL_SECRET, "https://relay.example.test"],
[CLOUD_LINKED_USER_ID, "user-123"],
[RELAY_ENVIRONMENT_CREDENTIAL_SECRET, "environment-credential"],
]);
const applyConfigCalls: Array<unknown> = [];
const requests: Array<HttpClientRequest.HttpClientRequest> = [];
return Effect.gen(function* () {
const link = yield* CloudLink.CloudLink;
expect(yield* link.registerManagedTunnelRecovery("http://127.0.0.1:3773")).toMatchObject({
status: "ready",
});
expect(requests).toHaveLength(1);
expect(requests[0]?.method).toBe("POST");
expect(requests[0]?.url).toBe(
"https://relay.example.test/v1/environments/env_123/tunnel/recovery",
);
expect(requests[0]?.headers.authorization).toBe("Bearer environment-credential");
const body = requests[0]?.body;
expect(body?._tag).toBe("Uint8Array");
if (body?._tag === "Uint8Array") {
expect(
yield* decodeManagedTunnelRecoveryRegistration(new TextDecoder().decode(body.body)),
).toMatchObject({
cloudUserId: "user-123",
tunnelId: "existing-tunnel",
origin: { localHttpHost: "127.0.0.1", localHttpPort: 3773 },
});
}
expect(applyConfigCalls).toHaveLength(1);
}).pipe(
provideReleaseHarness({
store,
applyConfigCalls,
requests,
respond: () => Response.json({ status: "ready" }),
}),
);
});
it.effect("reconciles a changed port after a relay outage outlasts the startup fallback", () => {
const config = {
providerKind: "cloudflare_tunnel" as const,
connectorToken: "existing-token",
tunnelId: "existing-tunnel",
};
const { store } = makeMemorySecretStore([
[CLOUD_ENDPOINT_RUNTIME_CONFIG, JSON.stringify(config)],
[
CLOUD_ENDPOINT_CONFIRMED_ORIGIN,
JSON.stringify({
config,
origin: { localHttpHost: "127.0.0.1", localHttpPort: 3773 },
}),
],
[RELAY_URL_SECRET, "https://relay.example.test"],
[CLOUD_LINKED_USER_ID, "user-123"],
[RELAY_ENVIRONMENT_CREDENTIAL_SECRET, "environment-credential"],
]);
const applyConfigCalls: Array<unknown> = [];
const requests: Array<HttpClientRequest.HttpClientRequest> = [];
let relayAvailable = false;
const localOrigin = "http://127.0.0.1:4884";
return Effect.gen(function* () {
const link = yield* CloudLink.CloudLink;
const fallbackStarted = yield* Deferred.make<void>();
const firstFailure = yield* Deferred.make<void>();
expect(yield* link.startManagedTunnelIfOriginConfirmed(localOrigin)).toBe(false);
const registration = yield* Effect.forkChild(
retryManagedTunnelRegistration(
link
.registerManagedTunnelRecovery(localOrigin)
.pipe(Effect.tapError(() => Deferred.succeed(firstFailure, undefined))),
CloudLink.shouldRetryCloudLink,
link
.startManagedTunnelIfOriginConfirmed(localOrigin, {
requireConfirmedOrigin: false,
})
.pipe(
Effect.orDie,
Effect.tap((started) => {
expect(started).toBe(true);
return Deferred.succeed(fallbackStarted, undefined);
}),
Effect.asVoid,
),
),
{ startImmediately: true },
);
yield* Deferred.await(firstFailure);
yield* TestClock.adjust("15 minutes");
yield* Effect.raceFirst(
Deferred.await(fallbackStarted),
Fiber.join(registration).pipe(
Effect.andThen(Effect.die("Registration ended before starting the fallback")),
),
);
expect(applyConfigCalls).toEqual([config]);
const attemptsBeforeRecovery = requests.length;
relayAvailable = true;
yield* TestClock.adjust("1 minute");
expect(yield* Fiber.join(registration)).toMatchObject({ status: "ready" });
expect(requests.length).toBeGreaterThan(attemptsBeforeRecovery);
const marker = yield* store.get(CLOUD_ENDPOINT_CONFIRMED_ORIGIN);
expect(Option.isSome(marker)).toBe(true);
if (Option.isSome(marker)) {
expect(
Option.getOrThrow(decodeConfirmedOrigin(new TextDecoder().decode(marker.value))),
).toEqual({
config,
origin: { localHttpHost: "127.0.0.1", localHttpPort: 4884 },
});
}
}).pipe(
provideReleaseHarness({
store,
applyConfigCalls,
requests,
respond: () =>
relayAvailable
? Response.json({ status: "ready" })
: Response.json({ message: "relay unavailable" }, { status: 503 }),
}),
);
});
it.effect(
"starts a connector with a marker for the current origin without contacting relay",
() => {
const configJson =
'{"providerKind":"cloudflare_tunnel","connectorToken":"existing-token","tunnelId":"existing-tunnel"}';
const config = {
providerKind: "cloudflare_tunnel" as const,
connectorToken: "existing-token",
tunnelId: "existing-tunnel",
};
const { store } = makeMemorySecretStore([
[CLOUD_ENDPOINT_RUNTIME_CONFIG, configJson],
[
CLOUD_ENDPOINT_CONFIRMED_ORIGIN,
`{"config":${configJson},"origin":{"localHttpHost":"127.0.0.1","localHttpPort":3773}}`,
],
]);
const applyConfigCalls: Array<unknown> = [];
const requests: Array<HttpClientRequest.HttpClientRequest> = [];
return Effect.gen(function* () {
const link = yield* CloudLink.CloudLink;
expect(yield* link.startManagedTunnelIfOriginConfirmed("http://127.0.0.1:3773")).toBe(true);
expect(applyConfigCalls).toEqual([config]);
expect(requests).toEqual([]);
}).pipe(provideReleaseHarness({ store, applyConfigCalls, requests }));
},
);
it.effect.each([
{ name: "missing", marker: undefined, origin: "http://127.0.0.1:3773" },
{
name: "stale",
marker:
'{"config":{"providerKind":"cloudflare_tunnel","connectorToken":"existing-token","tunnelId":"existing-tunnel"},"origin":{"localHttpHost":"127.0.0.1","localHttpPort":3773}}',
origin: "http://127.0.0.1:4884",
},
])("does not start a connector with a $name origin marker", ({ marker, origin }) => {
const entries: Array<readonly [string, string]> = [
[
CLOUD_ENDPOINT_RUNTIME_CONFIG,
'{"providerKind":"cloudflare_tunnel","connectorToken":"existing-token","tunnelId":"existing-tunnel"}',
],
];
if (marker !== undefined) entries.push([CLOUD_ENDPOINT_CONFIRMED_ORIGIN, marker]);
const { store } = makeMemorySecretStore(entries);
const applyConfigCalls: Array<unknown> = [];
const requests: Array<HttpClientRequest.HttpClientRequest> = [];
return Effect.gen(function* () {
const link = yield* CloudLink.CloudLink;
expect(yield* link.startManagedTunnelIfOriginConfirmed(origin)).toBe(false);
expect(applyConfigCalls).toEqual([]);
expect(requests).toEqual([]);
}).pipe(provideReleaseHarness({ store, applyConfigCalls, requests }));
});
it.effect(
"starts the stored connector without a marker when confirmation is not required",
() => {
const config = {
providerKind: "cloudflare_tunnel" as const,
connectorToken: "existing-token",
tunnelId: "existing-tunnel",
};
const { store } = makeMemorySecretStore([
[CLOUD_ENDPOINT_RUNTIME_CONFIG, JSON.stringify(config)],
]);
const applyConfigCalls: Array<unknown> = [];
const requests: Array<HttpClientRequest.HttpClientRequest> = [];
return Effect.gen(function* () {
const link = yield* CloudLink.CloudLink;
expect(
yield* link.startManagedTunnelIfOriginConfirmed("http://127.0.0.1:3773", {
requireConfirmedOrigin: false,
}),
).toBe(true);
expect(applyConfigCalls).toEqual([config]);
expect(requests).toEqual([]);
}).pipe(provideReleaseHarness({ store, applyConfigCalls, requests }));
},
);
it.effect.each(["replaced", "removed"] as const)(
"does not activate a tunnel when its runtime config is %s during registration",
(mutation) => {
const originalConfig =
'{"providerKind":"cloudflare_tunnel","connectorToken":"existing-token","tunnelId":"existing-tunnel"}';
const { store, values } = makeMemorySecretStore([
[CLOUD_ENDPOINT_RUNTIME_CONFIG, originalConfig],
[RELAY_URL_SECRET, "https://relay.example.test"],
[CLOUD_LINKED_USER_ID, "user-123"],
[RELAY_ENVIRONMENT_CREDENTIAL_SECRET, "environment-credential"],
]);
const applyConfigCalls: Array<unknown> = [];
const requests: Array<HttpClientRequest.HttpClientRequest> = [];
return Effect.gen(function* () {
const link = yield* CloudLink.CloudLink;
expect(yield* link.registerManagedTunnelRecovery("http://127.0.0.1:3773")).toEqual({
status: "superseded",
});
expect(applyConfigCalls).toEqual([]);
expect(values.has(CLOUD_ENDPOINT_CONFIRMED_ORIGIN)).toBe(false);
}).pipe(
provideReleaseHarness({
store,
applyConfigCalls,
requests,
respond: () => {
if (mutation === "replaced") {
values.set(
CLOUD_ENDPOINT_RUNTIME_CONFIG,
new TextEncoder().encode(
'{"providerKind":"cloudflare_tunnel","connectorToken":"fresh-token","tunnelId":"fresh-tunnel"}',
),
);
} else {
values.delete(CLOUD_ENDPOINT_RUNTIME_CONFIG);
}
return Response.json({ status: "ready" });
},
}),
);
},
);
it.effect("requests startup recovery for a legacy config without a recorded tunnel ID", () => {
const { store } = makeMemorySecretStore([
[
CLOUD_ENDPOINT_RUNTIME_CONFIG,
'{"providerKind":"cloudflare_tunnel","connectorToken":"token"}',
],
[RELAY_URL_SECRET, "https://relay.example.test"],
[CLOUD_LINKED_USER_ID, "user-123"],
[RELAY_ENVIRONMENT_CREDENTIAL_SECRET, "environment-credential"],
]);
const applyConfigCalls: Array<unknown> = [];
const requests: Array<HttpClientRequest.HttpClientRequest> = [];
return Effect.gen(function* () {
const link = yield* CloudLink.CloudLink;
const registration = yield* link.registerManagedTunnelRecovery("http://127.0.0.1:3773");
expect(registration).toEqual({
status: "recovery_required",
config: { providerKind: "cloudflare_tunnel", connectorToken: "token" },
});
expect(
managedTunnelStartupAction({
wantsCliLink: false,
registration,
}),
).toEqual({
action: "request_recovery",
config: { providerKind: "cloudflare_tunnel", connectorToken: "token" },
});
expect(requests).toEqual([]);
expect(applyConfigCalls).toEqual([]);
}).pipe(provideReleaseHarness({ store, applyConfigCalls, requests }));
});
it.effect("recovers a web-linked tunnel with its environment credential", () => {
const oldConfig =
'{"providerKind":"cloudflare_tunnel","connectorToken":"old-token","tunnelId":"old-tunnel"}';
const nextConfig = {
providerKind: "cloudflare_tunnel",
connectorToken: "new-token",
tunnelId: "new-tunnel",
} as const;
const { store, values } = makeMemorySecretStore([
[CLOUD_ENDPOINT_RUNTIME_CONFIG, oldConfig],
[RELAY_URL_SECRET, "https://relay.example.test"],
[CLOUD_LINKED_USER_ID, "user-123"],
[RELAY_ENVIRONMENT_CREDENTIAL_SECRET, "environment-credential"],
]);
const applyConfigCalls: Array<unknown> = [];
const requests: Array<HttpClientRequest.HttpClientRequest> = [];
return Effect.gen(function* () {
const link = yield* CloudLink.CloudLink;
expect(yield* link.recoverManagedTunnel("http://127.0.0.1:3773")).toBe(true);
expect(requests).toHaveLength(1);
expect(requests[0]?.method).toBe("POST");
expect(requests[0]?.url).toBe("https://relay.example.test/v1/environments/env_123/tunnel");
expect(requests[0]?.headers.authorization).toBe("Bearer environment-credential");
expect(applyConfigCalls).toEqual([nextConfig]);
expect(
Option.getOrNull(
decodeRuntimeConfig(new TextDecoder().decode(values.get(CLOUD_ENDPOINT_RUNTIME_CONFIG))),
),
).toEqual(nextConfig);
}).pipe(
provideReleaseHarness({
store,
applyConfigCalls,
requests,
respond: () =>
Response.json({
endpoint: {
httpBaseUrl: "https://environment.example.test/",
wsBaseUrl: "wss://environment.example.test/ws",
providerKind: "cloudflare_tunnel",
},
endpointRuntime: nextConfig,
}),
}),
);
});
it.effect("allows managed tunnel provisioning to take longer than ten seconds", () =>
Effect.gen(function* () {
const oldConfig =
'{"providerKind":"cloudflare_tunnel","connectorToken":"old-token","tunnelId":"old-tunnel"}';
const nextConfig = {
providerKind: "cloudflare_tunnel" as const,
connectorToken: "new-token",
tunnelId: "new-tunnel",
};
const { store } = makeMemorySecretStore([
[CLOUD_ENDPOINT_RUNTIME_CONFIG, oldConfig],
[RELAY_URL_SECRET, "https://relay.example.test"],
[CLOUD_LINKED_USER_ID, "user-123"],
[RELAY_ENVIRONMENT_CREDENTIAL_SECRET, "environment-credential"],
]);
const applyConfigCalls: Array<unknown> = [];
const requests: Array<HttpClientRequest.HttpClientRequest> = [];
const requestStarted = yield* Deferred.make<void>();
const response = yield* Deferred.make<Response>();
const recovery = yield* CloudLink.CloudLink.use((link) =>
link.recoverManagedTunnel("http://127.0.0.1:3773"),
).pipe(
provideReleaseHarness({
store,
applyConfigCalls,
requests,
onRequest: () => Deferred.succeed(requestStarted, undefined),
respondEffect: Deferred.await(response),
}),
Effect.forkChild({ startImmediately: true }),
);
yield* Deferred.await(requestStarted);
expect(requests).toHaveLength(1);
yield* TestClock.adjust("11 seconds");
yield* Effect.yieldNow;
yield* Deferred.succeed(
response,
Response.json({
endpoint: {
httpBaseUrl: "https://environment.example.test/",
wsBaseUrl: "wss://environment.example.test/ws",
providerKind: "cloudflare_tunnel",
},
endpointRuntime: nextConfig,
}),
);
expect(yield* Fiber.join(recovery)).toBe(true);
expect(requests).toHaveLength(1);
expect(applyConfigCalls).toEqual([nextConfig]);
}),
);
it.effect("does not recover an environment without a managed tunnel credential", () => {
const { store } = makeMemorySecretStore([
[CLOUD_ENDPOINT_RUNTIME_CONFIG, "old-config"],
[RELAY_URL_SECRET, "https://relay.example.test"],
]);
const applyConfigCalls: Array<unknown> = [];
const requests: Array<HttpClientRequest.HttpClientRequest> = [];
return Effect.gen(function* () {
const link = yield* CloudLink.CloudLink;
expect(yield* link.recoverManagedTunnel("http://127.0.0.1:3773")).toBe(false);
expect(applyConfigCalls).toEqual([]);
expect(requests).toEqual([]);
}).pipe(provideReleaseHarness({ store, applyConfigCalls, requests }));
});
it.effect("ignores recovery requests for a tunnel that has already been replaced", () => {
const { store } = makeMemorySecretStore([
[
CLOUD_ENDPOINT_RUNTIME_CONFIG,
'{"providerKind":"cloudflare_tunnel","connectorToken":"current-token","tunnelId":"current-tunnel"}',
],
[RELAY_URL_SECRET, "https://relay.example.test"],
[CLOUD_LINKED_USER_ID, "user-123"],
[RELAY_ENVIRONMENT_CREDENTIAL_SECRET, "environment-credential"],
]);
const applyConfigCalls: Array<unknown> = [];
const requests: Array<HttpClientRequest.HttpClientRequest> = [];
return Effect.gen(function* () {
const link = yield* CloudLink.CloudLink;
expect(
yield* link.recoverManagedTunnel("http://127.0.0.1:3773", {
providerKind: "cloudflare_tunnel",
connectorToken: "old-token",
tunnelId: "old-tunnel",
}),
).toBe(false);
expect(requests).toEqual([]);
expect(applyConfigCalls).toEqual([]);
}).pipe(provideReleaseHarness({ store, applyConfigCalls, requests }));
});
it.effect.each([
{ status: 401, rejection: "unauthorized" },
{ status: 403, rejection: "forbidden" },
{ status: 409, rejection: "rejected" },
])("preserves a permanent $status relay recovery failure", ({ status, rejection }) => {
const { store } = makeMemorySecretStore([
[CLOUD_ENDPOINT_RUNTIME_CONFIG, "old-config"],
[RELAY_URL_SECRET, "https://relay.example.test"],
[CLOUD_LINKED_USER_ID, "user-123"],
[RELAY_ENVIRONMENT_CREDENTIAL_SECRET, "environment-credential"],
]);
const applyConfigCalls: Array<unknown> = [];
const requests: Array<HttpClientRequest.HttpClientRequest> = [];
return Effect.gen(function* () {
const link = yield* CloudLink.CloudLink;
const error = yield* Effect.flip(link.recoverManagedTunnel("http://127.0.0.1:3773"));
expect(error).toMatchObject({ _tag: "RelayRequestError", rejection });
expect(CloudLink.shouldRetryCloudLink(error)).toBe(false);
expect(requests).toHaveLength(1);
expect(applyConfigCalls).toEqual([]);
}).pipe(
provideReleaseHarness({
store,
applyConfigCalls,
requests,
respond: () => Response.json({}, { status }),
}),
);
});
it.effect("keeps a tunnel configuration replaced during recovery", () => {
const { store, values } = makeMemorySecretStore([
[CLOUD_ENDPOINT_RUNTIME_CONFIG, "old-config"],
[RELAY_URL_SECRET, "https://relay.example.test"],
[CLOUD_LINKED_USER_ID, "user-123"],
[RELAY_ENVIRONMENT_CREDENTIAL_SECRET, "environment-credential"],
]);
const applyConfigCalls: Array<unknown> = [];
const requests: Array<HttpClientRequest.HttpClientRequest> = [];
const freshConfig = new TextEncoder().encode("fresh-config");
return Effect.gen(function* () {
const link = yield* CloudLink.CloudLink;
expect(yield* link.recoverManagedTunnel("http://127.0.0.1:3773")).toBe(false);
expect(values.get(CLOUD_ENDPOINT_RUNTIME_CONFIG)).toBe(freshConfig);
expect(applyConfigCalls).toEqual([]);
}).pipe(
provideReleaseHarness({
store,
applyConfigCalls,
requests,
respond: () => {
values.set(CLOUD_ENDPOINT_RUNTIME_CONFIG, freshConfig);
return Response.json({
endpoint: {
httpBaseUrl: "https://environment.example.test/",
wsBaseUrl: "wss://environment.example.test/ws",
providerKind: "cloudflare_tunnel",
},
endpointRuntime: {
providerKind: "cloudflare_tunnel",
connectorToken: "replacement-token",
},
});
},
}),
);
});
// The relay commits the link before the server stores it (#11898). A write
// that fails part-way must not leave the stale tunnel config that reports
// the link as managed, or Settings never relinks.
it.effect("does not report a managed tunnel after a relay config write fails part-way", () => {
const { store, values } = makeMemorySecretStore([
[CLOUD_ENDPOINT_RUNTIME_CONFIG, "managed-config"],
[CLOUD_ENDPOINT_CONFIRMED_ORIGIN, "confirmed-origin"],
[RELAY_URL_SECRET, "https://relay.example.test"],
[CLOUD_LINKED_USER_ID, "user-123"],
[RELAY_ENVIRONMENT_CREDENTIAL_SECRET, "old-credential"],
]);
const failingStore: ServerSecretStore.ServerSecretStore["Service"] = {
...store,
set: (name, value) =>
name === RELAY_ISSUER_SECRET
? Effect.fail(
new ServerSecretStore.SecretStorePersistError({
resource: name,
cause: PlatformError.systemError({
_tag: "PermissionDenied",
module: "FileSystem",
method: "rename",
pathOrDescriptor: `${name}.bin`,
}),
}),
)
: store.set(name, value),
};
const applyConfigCalls: Array<unknown> = [];
const requests: Array<HttpClientRequest.HttpClientRequest> = [];
const cloudMintPublicKey =
"-----BEGIN PUBLIC KEY-----\nMCowBQYDK2VwAyEAMY09Vo5CzEA6aEkhB/0h27t4Qs71SHS1fTSI4K0KvZQ=\n-----END PUBLIC KEY-----\n";
return Effect.gen(function* () {
const link = yield* CloudLink.CloudLink;
const error = yield* Effect.flip(
link.applyRelayConfig({
relayUrl: "https://relay.example.test",
cloudUserId: "user-123",
environmentCredential: "new-credential",
cloudMintPublicKey,
endpointRuntime: null,
}),
);
expect(error._tag).toBe("CloudLinkInternalError");
expect(yield* link.linkState()).toMatchObject({
linked: true,
cloudUserId: "user-123",
managedTunnelActive: false,
});
expect(values.has(CLOUD_ENDPOINT_RUNTIME_CONFIG)).toBe(false);
}).pipe(provideReleaseHarness({ store: failingStore, applyConfigCalls, requests }));
});
});