Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

sshEnvironment.ts

235 lines8.3 KB

1import {
2 bootstrapRemoteBearerSession,
3 fetchRemoteSessionState,
4 issueRemoteWebSocketTicket,
5 RemoteEnvironmentAuthUndeclaredStatusError,
6 type RemoteEnvironmentAuthError,
7} from "@t3tools/client-runtime/authorization";
8import { fetchRemoteEnvironmentDescriptor } from "@t3tools/client-runtime/environment";
9import {
10 EnvironmentAuthInvalidError,
11 DesktopDiscoveredSshHostSchema,
12 DesktopSshBearerBootstrapInputSchema,
13 DesktopSshBearerRequestInputSchema,
14 DesktopSshEnvironmentEnsureInputSchema,
15 DesktopSshEnvironmentEnsureResultSchema,
16 DesktopSshEnvironmentTargetSchema,
17 DesktopSshHttpBaseUrlInputSchema,
18 DesktopSshPasswordPromptCancelledType,
19 DesktopSshPasswordPromptResolutionInputSchema,
20 ExecutionEnvironmentDescriptor,
21 EnvironmentInternalError,
22 EnvironmentOperationForbiddenError,
23 EnvironmentRequestInvalidError,
24 EnvironmentScopeRequiredError,
25 AuthAccessTokenResult,
26 AuthSessionState,
27 AuthWebSocketTicketResult,
28} from "@t3tools/contracts";
29import { SshHttpBridgeError } from "@t3tools/ssh/errors";
30import { resolveLoopbackSshHttpBaseUrl } from "@t3tools/ssh/tunnel";
31import * as Data from "effect/Data";
32import * as Effect from "effect/Effect";
33import * as Schema from "effect/Schema";
34​
35import * as IpcChannels from "../channels.ts";
36import * as DesktopIpc from "../DesktopIpc.ts";
37import * as DesktopSshEnvironment from "../../ssh/DesktopSshEnvironment.ts";
38import * as DesktopSshPasswordPrompts from "../../ssh/DesktopSshPasswordPrompts.ts";
39​
40type DesktopSshEnvironmentRequestOperation =
41 | "fetch-environment-descriptor"
42 | "bootstrap-bearer-session"
43 | "fetch-session-state"
44 | "issue-websocket-ticket";
45​
46type DesktopSshEnvironmentRequestCause = RemoteEnvironmentAuthError | SshHttpBridgeError;
47​
48const isEnvironmentAuthInvalidError = Schema.is(EnvironmentAuthInvalidError);
49const isEnvironmentInternalError = Schema.is(EnvironmentInternalError);
50const isEnvironmentOperationForbiddenError = Schema.is(EnvironmentOperationForbiddenError);
51const isEnvironmentRequestInvalidError = Schema.is(EnvironmentRequestInvalidError);
52const isEnvironmentScopeRequiredError = Schema.is(EnvironmentScopeRequiredError);
53​
54function readSshHttpStatus(cause: DesktopSshEnvironmentRequestCause): number | null {
55 if (
56 cause instanceof RemoteEnvironmentAuthUndeclaredStatusError ||
57 cause instanceof SshHttpBridgeError
58 ) {
59 return cause.status ?? null;
60 }
61 if (isEnvironmentRequestInvalidError(cause)) {
62 return 400;
63 }
64 if (isEnvironmentAuthInvalidError(cause)) {
65 return 401;
66 }
67 if (isEnvironmentScopeRequiredError(cause)) {
68 return 403;
69 }
70 if (isEnvironmentOperationForbiddenError(cause)) {
71 return 403;
72 }
73 if (isEnvironmentInternalError(cause)) {
74 return 500;
75 }
76 return null;
78​
79export class DesktopSshEnvironmentRequestError extends Data.TaggedError(
80 "DesktopSshEnvironmentRequestError",
81)<{
82 readonly operation: DesktopSshEnvironmentRequestOperation;
83 readonly cause: DesktopSshEnvironmentRequestCause;
84 readonly sshHttpStatus: number | null;
85}> {
86 override get message() {
87 const prefix = this.sshHttpStatus === null ? "" : `[ssh_http:${this.sshHttpStatus}] `;
88 return `${prefix}SSH remote API request failed during ${this.operation}.`;
89 }
91​
92const withLoopbackSshApi =
93 <A, R>(
94 operation: DesktopSshEnvironmentRequestOperation,
95 use: (httpBaseUrl: string) => Effect.Effect<A, RemoteEnvironmentAuthError, R>,
96 ) =>
97 (httpBaseUrl: string): Effect.Effect<A, DesktopSshEnvironmentRequestError, R> =>
98 resolveLoopbackSshHttpBaseUrl(httpBaseUrl).pipe(
99 Effect.flatMap(use),
100 Effect.mapError(
101 (cause) =>
102 new DesktopSshEnvironmentRequestError({
103 operation,
104 cause,
105 sshHttpStatus: readSshHttpStatus(cause),
106 }),
107 ),
108 );
109​
110export const discoverSshHosts = DesktopIpc.makeIpcMethod({
111 channel: IpcChannels.DISCOVER_SSH_HOSTS_CHANNEL,
112 payload: Schema.Void,
113 result: Schema.Array(DesktopDiscoveredSshHostSchema),
114 handler: Effect.fn("desktop.ipc.sshEnvironment.discoverHosts")(function* () {
115 const sshEnvironment = yield* DesktopSshEnvironment.DesktopSshEnvironment;
116 return yield* sshEnvironment.discoverHosts();
117 }),
118});
119​
120export const resolveSshHost = DesktopIpc.makeIpcMethod({
121 channel: IpcChannels.RESOLVE_SSH_HOST_CHANNEL,
122 payload: Schema.String,
123 result: DesktopSshEnvironmentTargetSchema,
124 handler: Effect.fn("desktop.ipc.sshEnvironment.resolveHost")(function* (alias) {
125 const sshEnvironment = yield* DesktopSshEnvironment.DesktopSshEnvironment;
126 return yield* sshEnvironment.resolveHost(alias);
127 }),
128});
129​
130export const ensureSshEnvironment = DesktopIpc.makeIpcMethod({
131 channel: IpcChannels.ENSURE_SSH_ENVIRONMENT_CHANNEL,
132 payload: DesktopSshEnvironmentEnsureInputSchema,
133 result: DesktopSshEnvironmentEnsureResultSchema,
134 handler: Effect.fn("desktop.ipc.sshEnvironment.ensureEnvironment")(function* ({
135 target,
136 options,
137 }) {
138 const sshEnvironment = yield* DesktopSshEnvironment.DesktopSshEnvironment;
139 return yield* sshEnvironment.ensureEnvironment(target, options).pipe(
140 Effect.catch((error) =>
141 DesktopSshEnvironment.isDesktopSshPasswordPromptCancellation(error)
142 ? Effect.succeed({
143 type: DesktopSshPasswordPromptCancelledType,
144 message: error.message,
145 })
146 : Effect.fail(error),
147 ),
148 );
149 }),
150});
151​
152export const disconnectSshEnvironment = DesktopIpc.makeIpcMethod({
153 channel: IpcChannels.DISCONNECT_SSH_ENVIRONMENT_CHANNEL,
154 payload: DesktopSshEnvironmentTargetSchema,
155 result: Schema.Void,
156 handler: Effect.fn("desktop.ipc.sshEnvironment.disconnectEnvironment")(function* (target) {
157 const sshEnvironment = yield* DesktopSshEnvironment.DesktopSshEnvironment;
158 yield* sshEnvironment.disconnectEnvironment(target);
159 }),
160});
161​
162export const fetchSshEnvironmentDescriptor = DesktopIpc.makeIpcMethod({
163 channel: IpcChannels.FETCH_SSH_ENVIRONMENT_DESCRIPTOR_CHANNEL,
164 payload: DesktopSshHttpBaseUrlInputSchema,
165 result: ExecutionEnvironmentDescriptor,
166 handler: Effect.fn("desktop.ipc.sshEnvironment.fetchDescriptor")(function* ({ httpBaseUrl }) {
167 return yield* withLoopbackSshApi("fetch-environment-descriptor", (resolvedHttpBaseUrl) =>
168 fetchRemoteEnvironmentDescriptor({ httpBaseUrl: resolvedHttpBaseUrl }),
169 )(httpBaseUrl);
170 }),
171});
172​
173export const bootstrapSshBearerSession = DesktopIpc.makeIpcMethod({
174 channel: IpcChannels.BOOTSTRAP_SSH_BEARER_SESSION_CHANNEL,
175 payload: DesktopSshBearerBootstrapInputSchema,
176 result: AuthAccessTokenResult,
177 handler: Effect.fn("desktop.ipc.sshEnvironment.bootstrapBearerSession")(function* ({
178 httpBaseUrl,
179 credential,
180 }) {
181 return yield* withLoopbackSshApi("bootstrap-bearer-session", (resolvedHttpBaseUrl) =>
182 bootstrapRemoteBearerSession({
183 httpBaseUrl: resolvedHttpBaseUrl,
184 credential,
185 }),
186 )(httpBaseUrl);
187 }),
188});
189​
190export const fetchSshSessionState = DesktopIpc.makeIpcMethod({
191 channel: IpcChannels.FETCH_SSH_SESSION_STATE_CHANNEL,
192 payload: DesktopSshBearerRequestInputSchema,
193 result: AuthSessionState,
194 handler: Effect.fn("desktop.ipc.sshEnvironment.fetchSessionState")(function* ({
195 httpBaseUrl,
196 bearerToken,
197 }) {
198 return yield* withLoopbackSshApi("fetch-session-state", (resolvedHttpBaseUrl) =>
199 fetchRemoteSessionState({
200 httpBaseUrl: resolvedHttpBaseUrl,
201 bearerToken,
202 }),
203 )(httpBaseUrl);
204 }),
205});
206​
207export const issueSshWebSocketTicket = DesktopIpc.makeIpcMethod({
208 channel: IpcChannels.ISSUE_SSH_WEBSOCKET_TOKEN_CHANNEL,
209 payload: DesktopSshBearerRequestInputSchema,
210 result: AuthWebSocketTicketResult,
211 handler: Effect.fn("desktop.ipc.sshEnvironment.issueWebSocketTicket")(function* ({
212 httpBaseUrl,
213 bearerToken,
214 }) {
215 return yield* withLoopbackSshApi("issue-websocket-ticket", (resolvedHttpBaseUrl) =>
216 issueRemoteWebSocketTicket({
217 httpBaseUrl: resolvedHttpBaseUrl,
218 bearerToken,
219 }),
220 )(httpBaseUrl);
221 }),
222});
223​
224export const resolveSshPasswordPrompt = DesktopIpc.makeIpcMethod({
225 channel: IpcChannels.RESOLVE_SSH_PASSWORD_PROMPT_CHANNEL,
226 payload: DesktopSshPasswordPromptResolutionInputSchema,
227 result: Schema.Void,
228 handler: Effect.fn("desktop.ipc.sshEnvironment.resolvePasswordPrompt")(function* ({
229 requestId,
230 password,
231 }) {
232 const prompts = yield* DesktopSshPasswordPrompts.DesktopSshPasswordPrompts;
233 yield* prompts.resolve({ requestId, password });
234 }),
235});