Skip to content

Commit 48f5f6a

Browse files
author
PR Batch Tester
committed
fix(server): check the connector matches the saved tunnel
Reject connector inspection for a different runtime configuration so recovery cannot cache the old host under its replacement. Extend the existing rotation test without adding fixtures.
1 parent 0d2037c commit 48f5f6a

5 files changed

Lines changed: 32 additions & 24 deletions

File tree

‎apps/server/src/cloud/CloudLink.lifecycle.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ describe("releaseManagedTunnelOnShutdown", () => {
198198
recoveryRequests: Stream.empty,
199199
requestRecovery: () => Effect.void,
200200
tunnelConnected: Stream.empty,
201-
inspectConnector: Effect.succeed(null),
201+
inspectConnector: () => Effect.succeed(null),
202202
readinessChanged: Stream.empty,
203203
withLinkStateLock: (effect) => effect,
204204
}),

‎apps/server/src/cloud/ConnectReachability.test.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,14 @@ describe("classifyReachability", () => {
8282

8383
describe("ConnectReachability", () => {
8484
it.effect("exposes the checked tunnel's MCP URL until its config changes", () => {
85-
let config = new TextEncoder().encode("first");
85+
const tunnelConfig = { providerKind: "cloudflare_tunnel", connectorToken: "first" };
86+
let config = new TextEncoder().encode(JSON.stringify(tunnelConfig));
8687
const dependencies = Layer.mergeAll(
8788
Layer.mock(ServerSecretStore.ServerSecretStore)({
8889
get: () => Effect.sync(() => Option.some(config)),
8990
}),
9091
Layer.mock(ManagedEndpointRuntime.CloudManagedEndpointRuntime)({
91-
inspectConnector: Effect.succeed(connected),
92+
inspectConnector: () => Effect.succeed(connected),
9293
readinessChanged: Stream.empty,
9394
}),
9495
Layer.succeed(
@@ -109,7 +110,9 @@ describe("ConnectReachability", () => {
109110
);
110111

111112
// A relink stores a new tunnel before its connector reports in.
112-
config = new TextEncoder().encode("second");
113+
config = new TextEncoder().encode(
114+
JSON.stringify({ ...tunnelConfig, connectorToken: "second" }),
115+
);
113116
expect(yield* reachability.latest).toEqual(Option.none());
114117
}).pipe(Effect.provide(ConnectReachability.layer.pipe(Layer.provide(dependencies))));
115118
});

‎apps/server/src/cloud/ConnectReachability.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { HttpClient } from "effect/http";
1818
import { ChildProcessSpawner } from "effect/process";
1919

2020
import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
21-
import { CLOUD_ENDPOINT_RUNTIME_CONFIG } from "./config.ts";
21+
import { CLOUD_ENDPOINT_RUNTIME_CONFIG, decodeRuntimeConfig } from "./config.ts";
2222
import * as ManagedEndpointRuntime from "./ManagedEndpointRuntime.ts";
2323

2424
// cloudflared finds the edge through the SRV record `_v2-origintunneld._tcp.argotunnel.com`,
@@ -241,7 +241,8 @@ const make = Effect.gen(function* () {
241241
// Runs only inside the `ConnectReachability.check` span.
242242
const gatherFacts = Effect.fnUntraced(function* (tunnelConfig: string | null) {
243243
const managedTunnelConfigured = tunnelConfig !== null;
244-
const connector = managedTunnelConfigured ? yield* runtime.inspectConnector : null;
244+
const config = tunnelConfig ? Option.getOrNull(decodeRuntimeConfig(tunnelConfig)) : null;
245+
const connector = config ? yield* runtime.inspectConnector(config) : null;
245246
const endpoint =
246247
connector?.hostname && connector.readyConnections !== 0
247248
? yield* probeEndpoint(connector.hostname)

‎apps/server/src/cloud/ManagedEndpointRuntime.test.ts‎

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -470,24 +470,20 @@ describe("CloudManagedEndpointRuntime", () => {
470470
);
471471
const runtime = yield* buildCloudManagedEndpointRuntime(spawner);
472472

473-
yield* runtime.applyConfig({
474-
providerKind: "cloudflare_tunnel",
475-
connectorToken: "token-1",
476-
tunnelId: "tunnel-1",
477-
tunnelName: "t3-code-env-1",
478-
});
479-
yield* runtime.applyConfig({
473+
const config = {
480474
providerKind: "cloudflare_tunnel",
481475
connectorToken: "token-1",
482476
tunnelId: "tunnel-1",
483477
tunnelName: "t3-code-env-1",
484-
});
485-
yield* runtime.applyConfig({
486-
providerKind: "cloudflare_tunnel",
487-
connectorToken: "token-2",
488-
tunnelId: "tunnel-1",
489-
tunnelName: "t3-code-env-1",
490-
});
478+
} as const;
479+
const replacement = { ...config, connectorToken: "token-2" };
480+
yield* runtime.applyConfig(config);
481+
expect(yield* runtime.inspectConnector(config)).not.toBeNull();
482+
expect(yield* runtime.inspectConnector(replacement)).toBeNull();
483+
yield* runtime.applyConfig(config);
484+
yield* runtime.applyConfig(replacement);
485+
expect(yield* runtime.inspectConnector(replacement)).not.toBeNull();
486+
expect(yield* runtime.inspectConnector(config)).toBeNull();
491487
const stopped = yield* runtime.applyConfig(null);
492488

493489
expect(spawned.map((command) => command.command)).toEqual(["cloudflared", "cloudflared"]);

‎apps/server/src/cloud/ManagedEndpointRuntime.ts‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,10 @@ export class CloudManagedEndpointRuntime extends Context.Service<
5757
readonly applyConfig: (
5858
config: RelayManagedEndpointRuntimeConfig | null,
5959
) => Effect.Effect<CloudManagedEndpointRuntimeStatus>;
60-
/** `null` when no connector is running. */
61-
readonly inspectConnector: Effect.Effect<ConnectorFacts | null>;
60+
/** `null` when no running connector matches this configuration. */
61+
readonly inspectConnector: (
62+
config: RelayManagedEndpointRuntimeConfig,
63+
) => Effect.Effect<ConnectorFacts | null>;
6264
/** Emits when a connector registers a connection or its readiness changes. */
6365
readonly readinessChanged: Stream.Stream<void>;
6466
readonly recoveryRequests: Stream.Stream<RelayManagedEndpointRuntimeConfig>;
@@ -759,9 +761,15 @@ export const make = Effect.gen(function* () {
759761
Effect.orElseSucceed(() => null),
760762
);
761763

762-
const inspectConnector = Effect.gen(function* () {
764+
const inspectConnector = Effect.fn("CloudManagedEndpointRuntime.inspectConnector")(function* (
765+
expectedConfig: RelayManagedEndpointRuntimeConfig,
766+
) {
763767
const active = yield* Ref.get(activeRef);
764-
if (!active || !(yield* active.child.isRunning.pipe(Effect.orElseSucceed(() => false)))) {
768+
if (
769+
!active ||
770+
active.configKey !== runtimeConfigKey(expectedConfig) ||
771+
!(yield* active.child.isRunning.pipe(Effect.orElseSucceed(() => false)))
772+
) {
765773
return null;
766774
}
767775
const protocol = yield* Ref.get(active.protocol);

0 commit comments

Comments
 (0)