Skip to content
Merged
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
45 changes: 22 additions & 23 deletions apps/desktop/src/shell/DesktopShellEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import * as Duration from "effect/Duration";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process";
import * as ChildProcess from "effect/unstable/process/ChildProcess";
import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner";

import * as DesktopEnvironment from "../app/DesktopEnvironment.ts";

Expand All @@ -19,13 +20,11 @@ interface WindowsProbeOptions {
readonly loadProfile: boolean;
}

export interface DesktopShellEnvironmentShape {
readonly installIntoProcess: Effect.Effect<void, never>;
}

export class DesktopShellEnvironment extends Context.Service<
DesktopShellEnvironment,
DesktopShellEnvironmentShape
{
readonly installIntoProcess: Effect.Effect<void>;
}
>()("@t3tools/desktop/shell/DesktopShellEnvironment") {}

const LOGIN_SHELL_ENV_NAMES = [
Expand Down Expand Up @@ -336,20 +335,20 @@ const installShellEnvironment = (
return Effect.void;
};

export const layer = Layer.effect(
DesktopShellEnvironment,
Effect.gen(function* () {
const environment = yield* DesktopEnvironment.DesktopEnvironment;
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
return DesktopShellEnvironment.of({
installIntoProcess: installShellEnvironment({
env: process.env,
platform: environment.platform,
userShell: Option.none(),
}).pipe(
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
Effect.withSpan("desktop.shellEnvironment.installIntoProcess"),
),
});
}),
);
export const make = Effect.gen(function* () {
const environment = yield* DesktopEnvironment.DesktopEnvironment;
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
const installIntoProcess: DesktopShellEnvironment["Service"]["installIntoProcess"] =
installShellEnvironment({
env: process.env,
platform: environment.platform,
userShell: Option.none(),
}).pipe(
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
Effect.withSpan("desktop.shellEnvironment.installIntoProcess"),
);

return DesktopShellEnvironment.of({ installIntoProcess });
});

export const layer = Layer.effect(DesktopShellEnvironment, make);
15 changes: 14 additions & 1 deletion apps/desktop/src/ssh/DesktopSshEnvironment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ function makeTempHomeDir() {
}

describe("sshEnvironment", () => {
it("keeps prompt presentation diagnostics distinct from the legacy wrapper message", () => {
const cause = new DesktopSshPasswordPrompts.DesktopSshPromptPresentationError({
requestId: "prompt-1",
destination: "devbox",
cause: new Error("renderer send failed"),
});

assert.equal(cause.message, "Failed to present SSH password prompt for devbox.");
assert.equal(
DesktopSshEnvironment.toSshPasswordPromptError(cause).message,
"T3 Code window is not available for SSH authentication.",
);
});

it("treats password prompt timeouts as cancellable authentication prompts", () => {
assert.equal(
DesktopSshEnvironment.isDesktopSshPasswordPromptCancellation(
Expand Down Expand Up @@ -104,7 +118,6 @@ describe("sshEnvironment", () => {
Layer.succeed(DesktopSshPasswordPrompts.DesktopSshPasswordPrompts, {
request: () => Effect.die("unexpected password prompt request"),
resolve: () => Effect.die("unexpected password prompt resolution"),
cancelPending: () => Effect.void,
}),
),
Layer.provideMerge(NodeServices.layer),
Expand Down
102 changes: 61 additions & 41 deletions apps/desktop/src/ssh/DesktopSshEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ import type {
DesktopSshEnvironmentTarget,
} from "@t3tools/contracts";
import * as NetService from "@t3tools/shared/Net";
import {
SshPasswordPrompt,
type SshPasswordPromptShape,
type SshPasswordRequest,
} from "@t3tools/ssh/auth";
import * as SshAuth from "@t3tools/ssh/auth";
import { discoverSshHosts } from "@t3tools/ssh/config";
import {
SshCommandError,
Expand All @@ -19,14 +15,14 @@ import {
SshPasswordPromptError,
SshReadinessError,
} from "@t3tools/ssh/errors";
import { SshEnvironmentManager, type RemoteT3RunnerOptions } from "@t3tools/ssh/tunnel";
import * as SshTunnel from "@t3tools/ssh/tunnel";
import * as Context from "effect/Context";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import * as Path from "effect/Path";
import { HttpClient } from "effect/unstable/http";
import { ChildProcessSpawner } from "effect/unstable/process";
import * as HttpClient from "effect/unstable/http/HttpClient";
import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner";

import * as DesktopSshPasswordPrompts from "./DesktopSshPasswordPrompts.ts";

Expand All @@ -52,27 +48,25 @@ export type DesktopSshEnvironmentError =
| DesktopSshEnvironmentDiscoverError
| DesktopSshEnvironmentOperationError;

export interface DesktopSshEnvironmentShape {
readonly discoverHosts: (input?: {
readonly homeDir?: string;
}) => Effect.Effect<readonly DesktopDiscoveredSshHost[], DesktopSshEnvironmentDiscoverError>;
readonly ensureEnvironment: (
target: DesktopSshEnvironmentTarget,
options?: { readonly issuePairingToken?: boolean },
) => Effect.Effect<DesktopSshEnvironmentBootstrap, DesktopSshEnvironmentOperationError>;
readonly disconnectEnvironment: (
target: DesktopSshEnvironmentTarget,
) => Effect.Effect<void, DesktopSshEnvironmentOperationError>;
}

export class DesktopSshEnvironment extends Context.Service<
DesktopSshEnvironment,
DesktopSshEnvironmentShape
{
readonly discoverHosts: (input?: {
readonly homeDir?: string;
}) => Effect.Effect<readonly DesktopDiscoveredSshHost[], DesktopSshEnvironmentDiscoverError>;
readonly ensureEnvironment: (
target: DesktopSshEnvironmentTarget,
options?: { readonly issuePairingToken?: boolean },
) => Effect.Effect<DesktopSshEnvironmentBootstrap, DesktopSshEnvironmentOperationError>;
readonly disconnectEnvironment: (
target: DesktopSshEnvironmentTarget,
) => Effect.Effect<void, DesktopSshEnvironmentOperationError>;
}
>()("@t3tools/desktop/ssh/DesktopSshEnvironment") {}

export interface DesktopSshEnvironmentLayerOptions {
readonly resolveCliPackageSpec?: () => string;
readonly resolveCliRunner?: Effect.Effect<RemoteT3RunnerOptions>;
readonly resolveCliRunner?: Effect.Effect<SshTunnel.RemoteT3RunnerOptions>;
}

function discoverDesktopSshHostsEffect(input?: { readonly homeDir?: string }) {
Expand All @@ -88,27 +82,53 @@ export function isDesktopSshPasswordPromptCancellation(
);
}

function unexpectedPasswordPromptError(error: never): never {
throw new Error(`Unhandled desktop SSH password prompt error: ${String(error)}`);
}

export function toSshPasswordPromptError(
cause: DesktopSshPasswordPrompts.DesktopSshPasswordPromptRequestError,
): SshPasswordPromptError {
let message: string;
switch (cause._tag) {
case "DesktopSshPromptRequestIdGenerationError":
message = "Secure randomness is unavailable.";
break;
case "DesktopSshPromptWindowUnavailableError":
case "DesktopSshPromptPresentationError":
message = "T3 Code window is not available for SSH authentication.";
break;
case "DesktopSshPromptTimedOutError":
message = `SSH authentication timed out for ${cause.destination}.`;
break;
case "DesktopSshPromptCancelledError":
message = `SSH authentication cancelled for ${cause.destination}.`;
break;
case "DesktopSshPromptWindowClosedError":
message = "SSH authentication was cancelled because the app window closed.";
break;
case "DesktopSshPromptServiceStoppedError":
message = "SSH password prompt service stopped.";
break;
default:
return unexpectedPasswordPromptError(cause);
}
return new SshPasswordPromptError({ message, cause });
}

const makePasswordPrompt = (
prompts: DesktopSshPasswordPrompts.DesktopSshPasswordPromptsShape,
): SshPasswordPromptShape => ({
prompts: DesktopSshPasswordPrompts.DesktopSshPasswordPrompts["Service"],
): SshAuth.SshPasswordPrompt["Service"] => ({
isAvailable: true,
request: (request: SshPasswordRequest) =>
prompts.request(request).pipe(
Effect.mapError(
(cause) =>
new SshPasswordPromptError({
message: cause.message,
cause,
}),
),
),
request: (request: SshAuth.SshPasswordRequest) =>
prompts.request(request).pipe(Effect.mapError(toSshPasswordPromptError)),
});

const make = Effect.gen(function* () {
const manager = yield* SshEnvironmentManager;
export const make = Effect.gen(function* () {
const manager = yield* SshTunnel.SshEnvironmentManager;
const prompts = yield* DesktopSshPasswordPrompts.DesktopSshPasswordPrompts;
const runtimeContext = yield* Effect.context<DesktopSshEnvironmentRuntimeServices>();
const passwordPrompt = SshPasswordPrompt.of(makePasswordPrompt(prompts));
const passwordPrompt = SshAuth.SshPasswordPrompt.of(makePasswordPrompt(prompts));

return DesktopSshEnvironment.of({
discoverHosts: (input) =>
Expand All @@ -120,15 +140,15 @@ const make = Effect.gen(function* () {
manager
.ensureEnvironment(target, ensureOptions)
.pipe(
Effect.provideService(SshPasswordPrompt, passwordPrompt),
Effect.provideService(SshAuth.SshPasswordPrompt, passwordPrompt),
Effect.provide(runtimeContext),
Effect.withSpan("desktop.ssh.ensureEnvironment"),
),
disconnectEnvironment: (target) =>
manager
.disconnectEnvironment(target)
.pipe(
Effect.provideService(SshPasswordPrompt, passwordPrompt),
Effect.provideService(SshAuth.SshPasswordPrompt, passwordPrompt),
Effect.provide(runtimeContext),
Effect.withSpan("desktop.ssh.disconnectEnvironment"),
),
Expand All @@ -138,7 +158,7 @@ const make = Effect.gen(function* () {
export const layer = (options: DesktopSshEnvironmentLayerOptions = {}) =>
Layer.effect(DesktopSshEnvironment, make).pipe(
Layer.provide(
SshEnvironmentManager.layer({
SshTunnel.SshEnvironmentManager.layer({
...(options.resolveCliPackageSpec === undefined
? {}
: { resolveCliPackageSpec: options.resolveCliPackageSpec }),
Expand Down
Loading
Loading