diff --git a/apps/server/src/cli/connect.test.ts b/apps/server/src/cli/connect.test.ts index 4fbec456da093..630fe7cbfbb63 100644 --- a/apps/server/src/cli/connect.test.ts +++ b/apps/server/src/cli/connect.test.ts @@ -18,6 +18,11 @@ import { Command } from "effect/cli"; import { makeCli } from "../binCli.ts"; import { CLOUD_CLI_DESIRED_LINK_SECRET } from "../cloud/CliState.ts"; +import { + CLOUD_ENDPOINT_RUNTIME_CONFIG, + CLOUD_LINKED_USER_ID, + RELAY_ENVIRONMENT_CREDENTIAL_SECRET, +} from "../cloud/config.ts"; import { makePersistedServerRuntimeState, persistServerRuntimeState, @@ -104,6 +109,8 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer, NetService.layer)) readonly portFreed?: boolean; readonly command?: "unlink" | "status"; readonly linkMode?: "managed" | "publish_only"; + /** Linked from desktop, web, or mobile: no CLI-desired link is stored. */ + readonly linkedByApp?: boolean; }, ) => Effect.gen(function* () { @@ -143,10 +150,25 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer, NetService.layer)) if (options?.command === "status") { // Status asks the server for reachability only for a managed link. yield* fs.makeDirectory(`${baseDir}/userdata/secrets`, { recursive: true }); - yield* fs.writeFileString( - `${baseDir}/userdata/secrets/${CLOUD_CLI_DESIRED_LINK_SECRET}.bin`, - options.linkMode ?? "managed", - ); + yield* options.linkedByApp + ? Effect.all([ + fs.writeFileString( + `${baseDir}/userdata/secrets/${CLOUD_LINKED_USER_ID}.bin`, + "user_app_linked", + ), + fs.writeFileString( + `${baseDir}/userdata/secrets/${CLOUD_ENDPOINT_RUNTIME_CONFIG}.bin`, + "{}", + ), + fs.writeFileString( + `${baseDir}/userdata/secrets/${RELAY_ENVIRONMENT_CREDENTIAL_SECRET}.bin`, + "relay_credential", + ), + ]) + : fs.writeFileString( + `${baseDir}/userdata/secrets/${CLOUD_CLI_DESIRED_LINK_SECRET}.bin`, + options.linkMode ?? "managed", + ); } yield* Command.runWith(makeCli({ cloudEnabled: true }), { version: "0.0.0" })([ @@ -219,6 +241,20 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer, NetService.layer)) }), ); + it.effect("status checks reachability for an environment linked from the app", () => + Effect.gen(function* () { + const { requests, printed } = yield* runConnect(process.pid, { + command: "status", + linkedByApp: true, + }); + + assert.deepEqual(requests, ["POST /api/connect/reachability"]); + assert.notInclude(printed, "Exposure: disabled"); + assert.notInclude(printed, "Authorization: missing"); + assert.notInclude(printed, "Next:"); + }), + ); + it.effect("status does not report a publish-only link as exposed", () => Effect.gen(function* () { const { printed } = yield* runConnect(process.pid, { diff --git a/apps/server/src/cli/connect.ts b/apps/server/src/cli/connect.ts index 787e1c31c741c..8ef202c2d014f 100644 --- a/apps/server/src/cli/connect.ts +++ b/apps/server/src/cli/connect.ts @@ -36,10 +36,12 @@ import * as CliTokenManager from "../cloud/CliTokenManager.ts"; import { formatReachabilityFailure } from "../cloud/ConnectReachability.ts"; import { filterRelayResponse } from "../cloud/relayResponse.ts"; import { + CLOUD_ENDPOINT_RUNTIME_CONFIG, CLOUD_LINK_REVOKED, CLOUD_LINKED_USER_ID, isAgentActivityPublishingEnabledValue, PUBLISH_AGENT_ACTIVITY_SECRET, + RELAY_ENVIRONMENT_CREDENTIAL_SECRET, RELAY_URL_SECRET, } from "../cloud/config.ts"; import { relayUrlConfig } from "../cloud/publicConfig.ts"; @@ -199,17 +201,19 @@ function formatCloudStatus( ? "pending server startup" : "not provisioned"; const publishOnly = status.desired && status.linkMode === "publish_only"; - const nextStep = !status.authenticated - ? `Run \`${commands.link}\` to authorize and enable T3 Connect.` - : status.revoked - ? `Run \`${commands.link}\` to link this environment again.` - : !status.desired - ? `Run \`${commands.link}\` to enable T3 Connect.` - : !status.linked - ? publishOnly - ? "Start T3 to provision the environment link." - : "Start T3 to provision the environment link and launch its managed tunnel." - : undefined; + // An app-made link needs no CLI credential once provisioned. + const nextStep = + status.desired && status.linked && !status.revoked + ? undefined + : !status.authenticated + ? `Run \`${commands.link}\` to authorize and enable T3 Connect.` + : status.revoked + ? `Run \`${commands.link}\` to link this environment again.` + : !status.desired + ? `Run \`${commands.link}\` to enable T3 Connect.` + : publishOnly + ? "Start T3 to provision the environment link." + : "Start T3 to provision the environment link and launch its managed tunnel."; return [ "T3 Connect", @@ -672,8 +676,10 @@ const connectStatusCommand = Command.make("status", { const relayClient = yield* RelayClient.RelayClient; const tokens = yield* CliTokenManager.CloudCliTokenManager; const [ - desired, - authenticated, + cliDesired, + managedTunnel, + cliAuthenticated, + relayCredential, cloudUserId, relayUrl, publishAgentActivity, @@ -683,7 +689,9 @@ const connectStatusCommand = Command.make("status", { ] = yield* Effect.all( [ CliState.readCliDesiredCloudLink, + secrets.get(CLOUD_ENDPOINT_RUNTIME_CONFIG), tokens.hasCredential, + secrets.get(RELAY_ENVIRONMENT_CREDENTIAL_SECRET), secrets.get(CLOUD_LINKED_USER_ID), secrets.get(RELAY_URL_SECRET), secrets.get(PUBLISH_AGENT_ACTIVITY_SECRET), @@ -693,6 +701,10 @@ const connectStatusCommand = Command.make("status", { ], { concurrency: "unbounded" }, ); + // A link made in the desktop, web, or mobile app saves the tunnel config but no CLI marker. + const desired = cliDesired || Option.isSome(managedTunnel); + // Such a link authorizes the environment with its relay credential, not a CLI sign-in. + const authenticated = cliAuthenticated || Option.isSome(relayCredential); // A publish-only link has no tunnel for other devices to reach. const reachability = desired && linkMode === "managed" ? yield* checkLiveReachability() : null;