@oatscenter/trellis 0.100.0-rc.21 → 0.100.0-rc.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/auth/device_activation.d.ts +2 -0
- package/device.js +1 -0
- package/internal_sdk/generated/package.json +1 -1
- package/native/certificate_acceptance.d.ts +9 -7
- package/native/certificate_acceptance.js +96 -28
- package/native/discovery.js +2 -2
- package/package.json +2 -2
- package/service/runtime/core.js +12 -0
|
@@ -76,6 +76,8 @@ export declare function requestDeviceEnrollment(args: {
|
|
|
76
76
|
*/
|
|
77
77
|
export declare function waitForDeviceActivation(args: {
|
|
78
78
|
trellisUrl: string;
|
|
79
|
+
/** Defaults to `fetch`. */
|
|
80
|
+
fetch?: HttpFetch;
|
|
79
81
|
publicIdentityKey: string;
|
|
80
82
|
identitySeed: Uint8Array | string;
|
|
81
83
|
activationKey: Uint8Array | string;
|
package/device.js
CHANGED
|
@@ -137,6 +137,7 @@ function createActivationSession(args) {
|
|
|
137
137
|
return activatedState;
|
|
138
138
|
}
|
|
139
139
|
await waitForDeviceActivation({
|
|
140
|
+
fetch: installationFetch,
|
|
140
141
|
trellisUrl: args.trellisUrl,
|
|
141
142
|
publicIdentityKey: args.identity.publicIdentityKey,
|
|
142
143
|
identitySeed: args.identity.identitySeed,
|
|
@@ -20,13 +20,14 @@ export interface CertificateAcceptance {
|
|
|
20
20
|
*/
|
|
21
21
|
export declare function certificateAcceptanceFor(program: string): CertificateAcceptance;
|
|
22
22
|
/**
|
|
23
|
-
* Trusts the issuer accepted for `named`'s
|
|
24
|
-
* that installation named itself
|
|
23
|
+
* Trusts the issuer accepted for `named`'s installation at `endpoints` too,
|
|
24
|
+
* which that installation named itself. Refuses an endpoint another
|
|
25
|
+
* installation this process connects to already trusts differently.
|
|
25
26
|
* @internal
|
|
26
27
|
*/
|
|
27
|
-
export declare function trustNamedEndpoints(named: string, endpoints: readonly string[]): void
|
|
28
|
+
export declare function trustNamedEndpoints(named: string, endpoints: readonly string[]): Promise<void>;
|
|
28
29
|
/**
|
|
29
|
-
* The issuers accepted for
|
|
30
|
+
* The issuers accepted for `servers`' installation, for a connection to them.
|
|
30
31
|
* @internal
|
|
31
32
|
*/
|
|
32
33
|
export declare function acceptedIssuersFor(servers: readonly string[]): string[] | undefined;
|
|
@@ -64,8 +65,9 @@ export declare function acceptServerCertificate(url: string, acceptance: Certifi
|
|
|
64
65
|
*/
|
|
65
66
|
export declare function acceptServerCertificateFor(url: string, participant: string): Promise<void>;
|
|
66
67
|
/**
|
|
67
|
-
* Trusts the certificates `participant` already accepted at `servers
|
|
68
|
-
* connection that only reaches the broker.
|
|
69
|
-
* server's HTTPS address,
|
|
68
|
+
* Trusts the certificates `participant` already accepted at the `servers`
|
|
69
|
+
* their installations named, for a connection that only reaches the broker.
|
|
70
|
+
* Activation and discovery, which reach the server's HTTPS address, accept
|
|
71
|
+
* them and record what each installation names.
|
|
70
72
|
*/
|
|
71
73
|
export declare function trustAcceptedCertificatesFor(participant: string, servers: readonly string[]): Promise<void>;
|
|
@@ -7,6 +7,7 @@ import { Readable } from "node:stream";
|
|
|
7
7
|
import { connect } from "node:tls";
|
|
8
8
|
import { Agent, request, WebSocket as AgentWebSocket } from "undici";
|
|
9
9
|
import { err, ok } from "@oatscenter/result";
|
|
10
|
+
import { z } from "zod";
|
|
10
11
|
import { AuthError } from "../errors/AuthError.js";
|
|
11
12
|
/** Accepts only a certificate whose issuer has this SHA-256 fingerprint. */
|
|
12
13
|
export const CERTIFICATE_FINGERPRINT_ENV = "TRELLIS_CERTIFICATE_FINGERPRINT";
|
|
@@ -23,11 +24,8 @@ export function certificateAcceptanceFor(program) {
|
|
|
23
24
|
: join(homedir(), ".local", "state");
|
|
24
25
|
return { directory: join(state, "trellis", program, "servers") };
|
|
25
26
|
}
|
|
26
|
-
/**
|
|
27
|
-
|
|
28
|
-
* own connections to that endpoint trust. Nothing else in the process does.
|
|
29
|
-
*/
|
|
30
|
-
const acceptedIssuers = new Map();
|
|
27
|
+
/** By the endpoint each was accepted at. */
|
|
28
|
+
const installations = new Map();
|
|
31
29
|
const defaultPorts = {
|
|
32
30
|
"https:": "443",
|
|
33
31
|
"wss:": "443",
|
|
@@ -41,29 +39,86 @@ function endpointOf(url) {
|
|
|
41
39
|
const host = parsed.hostname.replace(/^\[|\]$/g, "");
|
|
42
40
|
return `${host}:${parsed.port || defaultPorts[parsed.protocol] || ""}`;
|
|
43
41
|
}
|
|
42
|
+
function installationAt(endpoint) {
|
|
43
|
+
for (const installation of installations.values())
|
|
44
|
+
if (installation.endpoints.has(endpoint))
|
|
45
|
+
return installation;
|
|
46
|
+
return undefined;
|
|
47
|
+
}
|
|
48
|
+
function sameIssuers(a, b) {
|
|
49
|
+
return a?.join("\n") === b?.join("\n");
|
|
50
|
+
}
|
|
51
|
+
function remember(endpoint, issuers, file) {
|
|
52
|
+
const previous = installations.get(endpoint);
|
|
53
|
+
const installation = {
|
|
54
|
+
at: endpoint,
|
|
55
|
+
issuers,
|
|
56
|
+
endpoints: previous && sameIssuers(previous.issuers, issuers)
|
|
57
|
+
? previous.endpoints
|
|
58
|
+
: new Set([endpoint]),
|
|
59
|
+
file,
|
|
60
|
+
};
|
|
61
|
+
// What an endpoint presents itself outranks what another installation
|
|
62
|
+
// named it.
|
|
63
|
+
for (const other of installations.values())
|
|
64
|
+
if (other !== previous)
|
|
65
|
+
other.endpoints.delete(endpoint);
|
|
66
|
+
installations.set(endpoint, installation);
|
|
67
|
+
return installation;
|
|
68
|
+
}
|
|
69
|
+
function namedFile(file) {
|
|
70
|
+
return file.replace(/\.pem$/, ".endpoints.json");
|
|
71
|
+
}
|
|
44
72
|
/**
|
|
45
|
-
* Trusts the issuer accepted for `named`'s
|
|
46
|
-
* that installation named itself
|
|
73
|
+
* Trusts the issuer accepted for `named`'s installation at `endpoints` too,
|
|
74
|
+
* which that installation named itself. Refuses an endpoint another
|
|
75
|
+
* installation this process connects to already trusts differently.
|
|
47
76
|
* @internal
|
|
48
77
|
*/
|
|
49
|
-
export function trustNamedEndpoints(named, endpoints) {
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
78
|
+
export async function trustNamedEndpoints(named, endpoints) {
|
|
79
|
+
const at = endpointOf(named);
|
|
80
|
+
await name(installationAt(at) ?? remember(at, undefined), endpoints.map(endpointOf));
|
|
81
|
+
}
|
|
82
|
+
async function name(installation, endpoints) {
|
|
83
|
+
for (const endpoint of endpoints) {
|
|
84
|
+
const other = installationAt(endpoint);
|
|
85
|
+
if (other &&
|
|
86
|
+
other !== installation &&
|
|
87
|
+
!sameIssuers(other.issuers, installation.issuers))
|
|
88
|
+
throw new AuthError({
|
|
89
|
+
reason: "CertificateConflict",
|
|
90
|
+
message: `${installation.at} names ${endpoint}, which another Trellis this program connects to trusts with a different certificate`,
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
for (const endpoint of endpoints)
|
|
94
|
+
installation.endpoints.add(endpoint);
|
|
95
|
+
if (!installation.file)
|
|
96
|
+
return;
|
|
97
|
+
// Saved even when it names only itself, as an installation that offers
|
|
98
|
+
// only its WebSocket on its own port does.
|
|
99
|
+
const file = namedFile(installation.file);
|
|
100
|
+
const saved = JSON.stringify({
|
|
101
|
+
endpoint: installation.at,
|
|
102
|
+
endpoints: [...installation.endpoints],
|
|
103
|
+
});
|
|
104
|
+
if ((await readFile(file, "utf8").catch(() => undefined)) === saved)
|
|
105
|
+
return;
|
|
106
|
+
const staged = `${file}.${randomUUID()}`;
|
|
107
|
+
await writeFile(staged, saved, { mode: 0o600, flag: "wx" });
|
|
108
|
+
await rename(staged, file);
|
|
54
109
|
}
|
|
55
110
|
/**
|
|
56
|
-
* The issuers accepted for
|
|
111
|
+
* The issuers accepted for `servers`' installation, for a connection to them.
|
|
57
112
|
* @internal
|
|
58
113
|
*/
|
|
59
114
|
export function acceptedIssuersFor(servers) {
|
|
60
|
-
const issuers = new Set(servers.flatMap((server) =>
|
|
115
|
+
const issuers = new Set(servers.flatMap((server) => installationAt(endpointOf(server))?.issuers ?? []));
|
|
61
116
|
return issuers.size ? [...issuers] : undefined;
|
|
62
117
|
}
|
|
63
118
|
/** One connection pool per set of accepted issuers. */
|
|
64
119
|
const agents = new WeakMap();
|
|
65
120
|
function agentFor(url) {
|
|
66
|
-
const issuers =
|
|
121
|
+
const issuers = installationAt(endpointOf(url))?.issuers;
|
|
67
122
|
if (!issuers)
|
|
68
123
|
return undefined;
|
|
69
124
|
let agent = agents.get(issuers);
|
|
@@ -161,8 +216,10 @@ export async function acceptServerCertificate(url, acceptance) {
|
|
|
161
216
|
reason: "CertificateMismatch",
|
|
162
217
|
message: `${server} presents issuer fingerprint ${presented.fingerprint}, not the expected ${expected}`,
|
|
163
218
|
}));
|
|
164
|
-
if (presented.authorized)
|
|
219
|
+
if (presented.authorized) {
|
|
220
|
+
remember(server, undefined);
|
|
165
221
|
return ok(undefined);
|
|
222
|
+
}
|
|
166
223
|
const accepted = await readFile(file, "utf8").catch(() => undefined);
|
|
167
224
|
if (accepted !== undefined) {
|
|
168
225
|
const fingerprint = new X509Certificate(accepted).fingerprint256;
|
|
@@ -171,7 +228,7 @@ export async function acceptServerCertificate(url, acceptance) {
|
|
|
171
228
|
reason: "CertificateChanged",
|
|
172
229
|
message: `${server} presents a different certificate than the one accepted for it (accepted ${fingerprint}, presented ${presented.fingerprint}); someone may be intercepting the connection. If the operator replaced it, delete ${file} and connect again`,
|
|
173
230
|
}));
|
|
174
|
-
|
|
231
|
+
remember(server, [accepted], file);
|
|
175
232
|
return ok(undefined);
|
|
176
233
|
}
|
|
177
234
|
if (expected) {
|
|
@@ -208,7 +265,7 @@ export async function acceptServerCertificate(url, acceptance) {
|
|
|
208
265
|
const staged = `${file}.${randomUUID()}`;
|
|
209
266
|
await writeFile(staged, presented.pem, { mode: 0o600, flag: "wx" });
|
|
210
267
|
await rename(staged, file);
|
|
211
|
-
|
|
268
|
+
remember(server, [presented.pem], file);
|
|
212
269
|
return ok(undefined);
|
|
213
270
|
}
|
|
214
271
|
/**
|
|
@@ -222,19 +279,30 @@ export async function acceptServerCertificateFor(url, participant) {
|
|
|
222
279
|
throw accepted.error;
|
|
223
280
|
}
|
|
224
281
|
/**
|
|
225
|
-
* Trusts the certificates `participant` already accepted at `servers
|
|
226
|
-
* connection that only reaches the broker.
|
|
227
|
-
* server's HTTPS address,
|
|
282
|
+
* Trusts the certificates `participant` already accepted at the `servers`
|
|
283
|
+
* their installations named, for a connection that only reaches the broker.
|
|
284
|
+
* Activation and discovery, which reach the server's HTTPS address, accept
|
|
285
|
+
* them and record what each installation names.
|
|
228
286
|
*/
|
|
229
287
|
export async function trustAcceptedCertificatesFor(participant, servers) {
|
|
230
288
|
const { directory } = certificateAcceptanceFor(participantProgram(participant));
|
|
231
|
-
const
|
|
232
|
-
for (const
|
|
233
|
-
if (
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
289
|
+
const wanted = new Set(servers.map(endpointOf));
|
|
290
|
+
for (const entry of await readdir(directory).catch(() => [])) {
|
|
291
|
+
if (!entry.endsWith(".pem"))
|
|
292
|
+
continue;
|
|
293
|
+
const file = join(directory, entry);
|
|
294
|
+
const named = await readFile(namedFile(file), "utf8")
|
|
295
|
+
.then((text) => z
|
|
296
|
+
.object({ endpoint: z.string(), endpoints: z.array(z.string()) })
|
|
297
|
+
.parse(JSON.parse(text)))
|
|
298
|
+
.catch(() => undefined);
|
|
299
|
+
if (!named ||
|
|
300
|
+
installations.has(named.endpoint) ||
|
|
301
|
+
!named.endpoints.some((endpoint) => wanted.has(endpoint)))
|
|
302
|
+
continue;
|
|
303
|
+
// Already saved: only trusted again.
|
|
304
|
+
await name(remember(named.endpoint, [await readFile(file, "utf8")]), named.endpoints);
|
|
305
|
+
}
|
|
238
306
|
}
|
|
239
307
|
function participantProgram(participant) {
|
|
240
308
|
return participant.replace(/[^A-Za-z0-9._-]/g, "_");
|
package/native/discovery.js
CHANGED
|
@@ -79,7 +79,7 @@ export async function discoverNativeOAuth(origin, options = {}) {
|
|
|
79
79
|
throw new AuthError({ reason: "InvalidIssuer" });
|
|
80
80
|
const address = issuer;
|
|
81
81
|
issuer = issuerOf(resource.authorization_servers[0]);
|
|
82
|
-
trustNamedEndpoints(address, [issuer]);
|
|
82
|
+
await trustNamedEndpoints(address, [issuer]);
|
|
83
83
|
response = await get(`${issuer}/.well-known/oauth-authorization-server`);
|
|
84
84
|
if (response.status === 429)
|
|
85
85
|
throw new AuthError({ reason: "OAuthBusy" });
|
|
@@ -123,7 +123,7 @@ export async function discoverNativeOAuth(origin, options = {}) {
|
|
|
123
123
|
}
|
|
124
124
|
// Native NATS is preferred; a Trellis that keeps it private advertises
|
|
125
125
|
// only its WebSocket. One connection can't mix the two.
|
|
126
|
-
trustNamedEndpoints(issuer, metadata.trellis.natsEndpoints);
|
|
126
|
+
await trustNamedEndpoints(issuer, metadata.trellis.natsEndpoints);
|
|
127
127
|
const native = metadata.trellis.natsEndpoints.filter((server) => /^(nats|tls):/.test(server));
|
|
128
128
|
const nativeServers = native.length > 0 ? native : [...metadata.trellis.natsEndpoints];
|
|
129
129
|
if (nativeServers.length === 0)
|
package/package.json
CHANGED
|
@@ -51,7 +51,7 @@
|
|
|
51
51
|
"@nats-io/obj": "^3.4.0",
|
|
52
52
|
"@nats-io/transport-node": "^3.4.0",
|
|
53
53
|
"@noble/hashes": "1.8.0",
|
|
54
|
-
"@oatscenter/result": "0.100.0-rc.
|
|
54
|
+
"@oatscenter/result": "0.100.0-rc.23",
|
|
55
55
|
"@opentelemetry/api": "^1.9.1",
|
|
56
56
|
"@opentelemetry/context-async-hooks": "~2.11.0",
|
|
57
57
|
"@opentelemetry/core": "~2.11.0",
|
|
@@ -71,7 +71,7 @@
|
|
|
71
71
|
"uqr": "0.1.3",
|
|
72
72
|
"zod": "^4.3.6"
|
|
73
73
|
},
|
|
74
|
-
"version": "0.100.0-rc.
|
|
74
|
+
"version": "0.100.0-rc.23",
|
|
75
75
|
"type": "module",
|
|
76
76
|
"license": "Apache-2.0",
|
|
77
77
|
"publishConfig": {
|
package/service/runtime/core.js
CHANGED
|
@@ -2699,6 +2699,10 @@ export class TrellisServiceRuntime extends Trellis {
|
|
|
2699
2699
|
return next;
|
|
2700
2700
|
};
|
|
2701
2701
|
const startLeaseHeartbeat = (runtime, fence) => {
|
|
2702
|
+
// A recovery still in flight when the service stopped must not
|
|
2703
|
+
// renew, or keep the process alive to try.
|
|
2704
|
+
if (this.#operationRecoveryAbort.signal.aborted)
|
|
2705
|
+
return;
|
|
2702
2706
|
const stopHeartbeat = () => {
|
|
2703
2707
|
clearInterval(leaseHeartbeat);
|
|
2704
2708
|
this.#operationLeaseHeartbeats.delete(leaseHeartbeat);
|
|
@@ -2765,6 +2769,8 @@ export class TrellisServiceRuntime extends Trellis {
|
|
|
2765
2769
|
this.#operationLeaseHeartbeats.add(leaseHeartbeat);
|
|
2766
2770
|
};
|
|
2767
2771
|
const watchCancellation = (runtime, fence) => {
|
|
2772
|
+
if (this.#operationRecoveryAbort.signal.aborted)
|
|
2773
|
+
return;
|
|
2768
2774
|
let reading = false;
|
|
2769
2775
|
let cleanupScheduled = !!runtime.cancelRequestedAt;
|
|
2770
2776
|
const cancellationWatch = setInterval(() => {
|
|
@@ -2852,6 +2858,8 @@ export class TrellisServiceRuntime extends Trellis {
|
|
|
2852
2858
|
.take();
|
|
2853
2859
|
if (isErr(staged))
|
|
2854
2860
|
return;
|
|
2861
|
+
if (this.#operationRecoveryAbort.signal.aborted)
|
|
2862
|
+
return;
|
|
2855
2863
|
startLeaseHeartbeat(runtime, fence);
|
|
2856
2864
|
watchCancellation(runtime, fence);
|
|
2857
2865
|
const transferSession = {
|
|
@@ -2897,12 +2905,16 @@ export class TrellisServiceRuntime extends Trellis {
|
|
|
2897
2905
|
runtime.snapshot.revision++;
|
|
2898
2906
|
runtime.snapshot.updatedAt = now();
|
|
2899
2907
|
await this.saveOperationRecord(runtime);
|
|
2908
|
+
if (this.#operationRecoveryAbort.signal.aborted)
|
|
2909
|
+
return;
|
|
2900
2910
|
startLeaseHeartbeat(runtime, fence);
|
|
2901
2911
|
watchCancellation(runtime, fence);
|
|
2902
2912
|
this.#operationTransferSessions.set(runtime.id, reopened);
|
|
2903
2913
|
void scheduleHandler(runtime, fence, runtime.caller, reopened, {}, true);
|
|
2904
2914
|
return;
|
|
2905
2915
|
}
|
|
2916
|
+
if (this.#operationRecoveryAbort.signal.aborted)
|
|
2917
|
+
return;
|
|
2906
2918
|
startLeaseHeartbeat(runtime, fence);
|
|
2907
2919
|
watchCancellation(runtime, fence);
|
|
2908
2920
|
void scheduleHandler(runtime, fence, runtime.caller, undefined, {}, true);
|