forked from pingdotgg/t3code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathonboarding.ts
More file actions
272 lines (252 loc) · 9.7 KB
/
Copy pathonboarding.ts
File metadata and controls
272 lines (252 loc) · 9.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import type { DesktopSshEnvironmentTarget, EnvironmentId } from "@t3tools/contracts";
import { resolveRemotePairingTarget } from "@t3tools/shared/remote";
import * as Context from "effect/Context";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as Schema from "effect/Schema";
import * as SubscriptionRef from "effect/SubscriptionRef";
import * as HttpClient from "effect/unstable/http/HttpClient";
import { bootstrapRemoteBearerSession } from "../authorization/remote.ts";
import { deriveWsBaseUrl, normalizeHttpBaseUrl } from "../environment/endpoint.ts";
import { fetchRemoteEnvironmentDescriptor } from "../environment/descriptor.ts";
import * as ClientCapabilities from "../platform/capabilities.ts";
import {
BearerConnectionCredential,
BearerConnectionProfile,
BearerConnectionRegistration,
type ConnectionCatalogEntry,
type ConnectionCredential,
SshConnectionProfile,
SshConnectionRegistration,
} from "./catalog.ts";
import * as ConnectionCredentialStore from "./credentialStore.ts";
import { mapRemoteEnvironmentError } from "./errors.ts";
import {
BearerConnectionTarget,
ConnectionBlockedError,
SshConnectionTarget,
type ConnectionAttemptError,
} from "./model.ts";
import * as Persistence from "../platform/persistence.ts";
import * as EnvironmentRegistry from "./registry.ts";
export interface PairingConnectionInput {
readonly pairingUrl?: string;
readonly host?: string;
readonly pairingCode?: string;
}
export interface SshConnectionInput {
readonly target: DesktopSshEnvironmentTarget;
readonly label?: string;
}
export interface BearerConnectionUpdateInput {
readonly environmentId: EnvironmentId;
readonly label: string;
readonly httpBaseUrl: string;
}
export class ConnectionOnboarding extends Context.Service<
ConnectionOnboarding,
{
readonly registerPairing: (
input: PairingConnectionInput,
) => Effect.Effect<
EnvironmentId,
ConnectionAttemptError | Persistence.ConnectionPersistenceError
>;
readonly registerSsh: (
input: SshConnectionInput,
) => Effect.Effect<
EnvironmentId,
ConnectionAttemptError | Persistence.ConnectionPersistenceError
>;
readonly updateBearer: (
input: BearerConnectionUpdateInput,
) => Effect.Effect<void, ConnectionAttemptError | Persistence.ConnectionPersistenceError>;
}
>()("@t3tools/client-runtime/connection/onboarding/ConnectionOnboarding") {}
const resolvePairingTarget = Effect.fn("clientRuntime.connection.onboarding.resolvePairingTarget")(
function* (input: PairingConnectionInput) {
return yield* Effect.try({
try: () => resolveRemotePairingTarget(input),
catch: (cause) =>
new ConnectionBlockedError({
reason: "configuration",
detail: cause instanceof Error ? cause.message : "The pairing details are invalid.",
}),
});
},
);
export const preparePairingRegistration = Effect.fn(
"clientRuntime.connection.onboarding.preparePairingRegistration",
)(function* (input: PairingConnectionInput) {
const target = yield* resolvePairingTarget(input);
const presentation = yield* ClientCapabilities.ClientPresentation;
const descriptor = yield* fetchRemoteEnvironmentDescriptor({
httpBaseUrl: target.httpBaseUrl,
}).pipe(Effect.mapError(mapRemoteEnvironmentError));
const access = yield* bootstrapRemoteBearerSession({
httpBaseUrl: target.httpBaseUrl,
credential: target.credential,
scopes: presentation.scopes,
clientMetadata: presentation.metadata,
}).pipe(Effect.mapError(mapRemoteEnvironmentError));
const connectionId = `bearer:${descriptor.environmentId}`;
return new BearerConnectionRegistration({
target: new BearerConnectionTarget({
environmentId: descriptor.environmentId,
label: descriptor.label,
connectionId,
}),
profile: new BearerConnectionProfile({
connectionId,
environmentId: descriptor.environmentId,
label: descriptor.label,
httpBaseUrl: target.httpBaseUrl,
wsBaseUrl: target.wsBaseUrl,
}),
credential: new BearerConnectionCredential({
token: access.access_token,
}),
});
});
export const registerPairingConnection = Effect.fn(
"clientRuntime.connection.onboarding.registerPairingConnection",
)(function* (input: PairingConnectionInput) {
const registration = yield* preparePairingRegistration(input);
const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
yield* registry.register(registration);
return registration.target.environmentId;
});
const isBearerCredential = Schema.is(BearerConnectionCredential);
const isBearerProfile = Schema.is(BearerConnectionProfile);
export const updateBearerConnection = Effect.fn(
"clientRuntime.connection.onboarding.updateBearerConnection",
)(function* (input: BearerConnectionUpdateInput) {
const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
const credentials = yield* ConnectionCredentialStore.ConnectionCredentialStore;
const entry = (yield* SubscriptionRef.get(registry.entries)).get(input.environmentId);
const credential =
entry?.target._tag === "BearerConnectionTarget"
? yield* credentials.get(entry.target.connectionId)
: Option.none();
const registration = yield* prepareBearerConnectionUpdate({
input,
entry: Option.fromUndefinedOr(entry),
credential,
});
yield* registry.register(registration);
});
export const prepareBearerConnectionUpdate = Effect.fn(
"clientRuntime.connection.onboarding.prepareBearerConnectionUpdate",
)(function* (options: {
readonly input: BearerConnectionUpdateInput;
readonly entry: Option.Option<ConnectionCatalogEntry>;
readonly credential: Option.Option<ConnectionCredential>;
}) {
const entry = Option.getOrNull(options.entry);
if (
entry === undefined ||
entry === null ||
entry.target._tag !== "BearerConnectionTarget" ||
Option.isNone(entry.profile) ||
!isBearerProfile(entry.profile.value)
) {
return yield* new ConnectionBlockedError({
reason: "configuration",
detail: "Only saved bearer environments can be edited.",
});
}
const credential = options.credential;
if (Option.isNone(credential) || !isBearerCredential(credential.value)) {
return yield* new ConnectionBlockedError({
reason: "authentication",
detail: "The saved bearer credential is unavailable.",
});
}
const label = options.input.label.trim();
if (label === "") {
return yield* new ConnectionBlockedError({
reason: "configuration",
detail: "Environment label cannot be empty.",
});
}
const httpBaseUrl = yield* Effect.try({
try: () => normalizeHttpBaseUrl(options.input.httpBaseUrl),
catch: (cause) =>
new ConnectionBlockedError({
reason: "configuration",
detail: cause instanceof Error ? cause.message : "The environment URL is invalid.",
}),
});
const connectionId = entry.target.connectionId;
return new BearerConnectionRegistration({
target: new BearerConnectionTarget({
environmentId: options.input.environmentId,
label,
connectionId,
}),
profile: new BearerConnectionProfile({
connectionId,
environmentId: options.input.environmentId,
label,
httpBaseUrl,
wsBaseUrl: deriveWsBaseUrl(httpBaseUrl),
}),
credential: credential.value,
});
});
export const prepareSshRegistration = Effect.fn(
"clientRuntime.connection.onboarding.prepareSshRegistration",
)(function* (input: SshConnectionInput) {
const gateway = yield* ClientCapabilities.SshEnvironmentGateway;
const provisioned = yield* gateway.provision(input.target);
const connectionId = `ssh:${provisioned.environmentId}`;
const label = input.label?.trim() || provisioned.label || provisioned.bootstrap.target.alias;
return new SshConnectionRegistration({
target: new SshConnectionTarget({
environmentId: provisioned.environmentId,
label,
connectionId,
}),
profile: new SshConnectionProfile({
connectionId,
environmentId: provisioned.environmentId,
label,
target: provisioned.bootstrap.target,
}),
});
});
export const registerSshConnection = Effect.fn(
"clientRuntime.connection.onboarding.registerSshConnection",
)(function* (input: SshConnectionInput) {
const registration = yield* prepareSshRegistration(input);
const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
yield* registry.register(registration);
return registration.target.environmentId;
});
export const make = Effect.gen(function* () {
const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
const presentation = yield* ClientCapabilities.ClientPresentation;
const httpClient = yield* HttpClient.HttpClient;
const ssh = yield* ClientCapabilities.SshEnvironmentGateway;
const credentials = yield* ConnectionCredentialStore.ConnectionCredentialStore;
return ConnectionOnboarding.of({
registerPairing: (input) =>
registerPairingConnection(input).pipe(
Effect.provideService(EnvironmentRegistry.EnvironmentRegistry, registry),
Effect.provideService(ClientCapabilities.ClientPresentation, presentation),
Effect.provideService(HttpClient.HttpClient, httpClient),
),
registerSsh: (input) =>
registerSshConnection(input).pipe(
Effect.provideService(EnvironmentRegistry.EnvironmentRegistry, registry),
Effect.provideService(ClientCapabilities.SshEnvironmentGateway, ssh),
),
updateBearer: (input) =>
updateBearerConnection(input).pipe(
Effect.provideService(EnvironmentRegistry.EnvironmentRegistry, registry),
Effect.provideService(ConnectionCredentialStore.ConnectionCredentialStore, credentials),
),
});
});
export const layer = Layer.effect(ConnectionOnboarding, make);