Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 39 additions & 4 deletions apps/server/src/cli/connect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ 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,
encodeEndpointRuntimeConfigJson,
RELAY_URL_SECRET,
} from "../cloud/config.ts";
import {
makePersistedServerRuntimeState,
persistServerRuntimeState,
Expand Down Expand Up @@ -104,6 +110,7 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer, NetService.layer))
readonly portFreed?: boolean;
readonly command?: "unlink" | "status";
readonly linkMode?: "managed" | "publish_only";
readonly linkedBy?: "cli" | "app";
},
) =>
Effect.gen(function* () {
Expand Down Expand Up @@ -142,10 +149,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",
const secretsDir = `${baseDir}/userdata/secrets`;
yield* fs.makeDirectory(secretsDir, { recursive: true });
const linkSecrets =
options.linkedBy === "app"
? ([
[CLOUD_LINKED_USER_ID, "user_123"],
[RELAY_URL_SECRET, "https://relay.example.test"],
[
CLOUD_ENDPOINT_RUNTIME_CONFIG,
yield* encodeEndpointRuntimeConfigJson({
providerKind: "cloudflare_tunnel",
connectorToken: "connector-token",
tunnelId: "tunnel-id",
}),
],
] as const)
: ([[CLOUD_CLI_DESIRED_LINK_SECRET, options.linkMode ?? "managed"]] as const);
yield* Effect.forEach(linkSecrets, ([name, value]) =>
fs.writeFileString(`${secretsDir}/${name}.bin`, value),
);
}

Expand Down Expand Up @@ -231,6 +253,19 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer, NetService.layer))
}),
);

it.effect("status checks a link the desktop or web app made, which stores no CLI marker", () =>
Effect.gen(function* () {
const { requests, printed } = yield* runConnect(process.pid, {
command: "status",
linkedBy: "app",
});

assert.include(printed, "Exposure: enabled");
assert.notInclude(printed, "t3 connect link");
assert.deepEqual(requests, ["POST /api/connect/reachability"]);
}),
);

it.effect("asks a running server to stop its tunnel", () =>
Effect.gen(function* () {
const { requests, printed, warnings } = yield* runConnect(process.pid);
Expand Down
36 changes: 21 additions & 15 deletions apps/server/src/cli/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ 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,
Expand Down Expand Up @@ -185,6 +186,7 @@ function formatRelayClientStatus(executable: RelayClient.RelayClientStatus): Rea
function formatCloudStatus(
status: CloudCliStatus,
commands: { readonly link: string; readonly serviceStatus: string },
appLinked: boolean,
options?: { readonly json?: boolean },
): string {
if (options?.json) {
Expand All @@ -199,27 +201,28 @@ 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;
const nextStep =
!status.authenticated && !(appLinked && status.linked)
? `Run \`${commands.link}\` to authorize and enable T3 Connect.`
: status.revoked
? `Run \`${commands.link}\` to link this environment again.`
: !status.desired && !appLinked
? `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;

return [
"T3 Connect",
` Exposure: ${publishOnly ? "publish-only" : status.desired ? "enabled" : "disabled"}`,
` Exposure: ${publishOnly ? "publish-only" : status.desired || appLinked ? "enabled" : "disabled"}`,
` Authorization: ${status.authenticated ? "stored credential" : "missing"}`,
` Environment link: ${provisioned}`,
` Relay: ${status.relayUrl ?? "not provisioned"}`,
` Publish agent activity: ${status.publishAgentActivity ? "enabled" : "disabled"}`,
...formatRelayClientStatus(status.relayClient),
...(!status.desired
...(!status.desired && !appLinked
? []
: publishOnly
? [
Expand Down Expand Up @@ -680,6 +683,7 @@ const connectStatusCommand = Command.make("status", {
revoked,
executable,
linkMode,
runtimeConfig,
] = yield* Effect.all(
[
CliState.readCliDesiredCloudLink,
Expand All @@ -690,12 +694,14 @@ const connectStatusCommand = Command.make("status", {
secrets.get(CLOUD_LINK_REVOKED),
relayClient.resolve,
CliState.readCliDesiredLinkMode,
secrets.get(CLOUD_ENDPOINT_RUNTIME_CONFIG),
],
{ concurrency: "unbounded" },
);
const appLinked = !desired && Option.isSome(runtimeConfig) && Option.isNone(revoked);
// A publish-only link has no tunnel for other devices to reach.
const reachability =
desired && linkMode === "managed" ? yield* checkLiveReachability() : null;
(desired && linkMode === "managed") || appLinked ? yield* checkLiveReachability() : null;
const status: CloudCliStatus = {
desired,
authenticated,
Expand All @@ -714,7 +720,7 @@ const connectStatusCommand = Command.make("status", {
link: yield* resolveHostCliHint("connect link", flags.baseDir),
serviceStatus: yield* resolveHostCliHint("service status", flags.baseDir),
};
yield* Console.log(formatCloudStatus(status, commands, { json: flags.json }));
yield* Console.log(formatCloudStatus(status, commands, appLinked, { json: flags.json }));
}),
{
quietLogs: flags.json,
Expand Down
Loading