Skip to content

Commit 7681605

Browse files
fix(server): t3 update installs from the policy's update mirror (pingdotgg#29911)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 7ab237f commit 7681605

9 files changed

Lines changed: 345 additions & 56 deletions

File tree

‎apps/server/src/cli/update.test.ts‎

Lines changed: 75 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ import * as BootService from "../cloud/bootService.ts";
1818
import { prunePinnedRuntimes } from "../cloud/pinnedRuntime.ts";
1919
import * as ManagedPolicy from "../policy/ManagedPolicy.ts";
2020
import * as ProcessRunner from "../processRunner.ts";
21-
import { repointLauncher, resolveLauncherPath, resolveNewestVersion, runUpdate } from "./update.ts";
21+
import {
22+
repointLauncher,
23+
resolveLauncherPath,
24+
resolveMirrorVersion,
25+
resolveNewestVersion,
26+
runUpdate,
27+
} from "./update.ts";
2228

2329
// lsof listing one process, so prune sees a process list in which no runtime is running.
2430
const unrelatedLsof = ProcessRunner.ProcessRunner.of({
@@ -60,10 +66,59 @@ it.effect("authenticates each release page until the requested channel is found"
6066
}),
6167
);
6268

69+
it.effect("reads the newest version from the update feed file a mirror publishes", () =>
70+
Effect.gen(function* () {
71+
const requested: string[] = [];
72+
const mirror = {
73+
organization: "Example Corp",
74+
feedUrl: "https://updates.example.com/t3code/?token=abc",
75+
};
76+
const resolve = (
77+
channel: "stable" | "nightly" | "preview",
78+
platform: NodeJS.Platform,
79+
arch: string,
80+
feed: string,
81+
) =>
82+
resolveMirrorVersion(mirror, channel).pipe(
83+
Effect.map((version): string => version),
84+
Effect.catchTags({ CliUpdateError: (error) => Effect.succeed(`error: ${error.reason}`) }),
85+
Effect.provideService(HostProcess.Platform, platform),
86+
Effect.provideService(HostProcess.Architecture, arch),
87+
Effect.provideService(
88+
HttpClient.HttpClient,
89+
HttpClient.make((request) => {
90+
requested.push(request.url);
91+
return Effect.succeed(HttpClientResponse.fromWeb(request, new Response(feed)));
92+
}),
93+
),
94+
);
95+
96+
assert.equal(
97+
yield* resolve("stable", "darwin", "arm64", "version: 1.4.0\nfiles: []\n"),
98+
"1.4.0",
99+
);
100+
assert.equal(
101+
yield* resolve("nightly", "linux", "arm64", "version: '1.5.0-nightly.20261101.2'\n"),
102+
"1.5.0-nightly.20261101.2",
103+
);
104+
assert.deepStrictEqual(requested, [
105+
"https://updates.example.com/t3code/latest-mac.yml?token=abc",
106+
"https://updates.example.com/t3code/nightly-linux-arm64.yml?token=abc",
107+
]);
108+
// A feed for the wrong track is not trusted to mean the requested one.
109+
assert.include(
110+
yield* resolve("stable", "win32", "x64", "version: 1.5.0-nightly.20261101.2\n"),
111+
"Could not read latest.yml from Example Corp's update mirror.",
112+
);
113+
assert.include(yield* resolve("preview", "win32", "x64", ""), "has no preview builds");
114+
}),
115+
);
116+
63117
it.layer(NodeServices.layer)("t3 update under a managed policy", (it) => {
64118
const runWithPolicy = (
65119
updates: ManagedUpdatesPolicy,
66120
input: { readonly channel?: "stable" | "nightly"; readonly requestedVersion?: string } = {},
121+
httpClient = HttpClient.make(() => Effect.die("unexpected release lookup")),
67122
) =>
68123
runUpdate({
69124
baseDir: "/unused",
@@ -85,10 +140,7 @@ it.layer(NodeServices.layer)("t3 update under a managed policy", (it) => {
85140
// Refusals happen before the update touches the network, the service, or a process.
86141
Layer.mock(BootService.BootService, {}),
87142
Layer.mock(ProcessRunner.ProcessRunner, {}),
88-
Layer.succeed(
89-
HttpClient.HttpClient,
90-
HttpClient.make(() => Effect.die("unexpected release lookup")),
91-
),
143+
Layer.succeed(HttpClient.HttpClient, httpClient),
92144
),
93145
),
94146
);
@@ -118,6 +170,24 @@ it.layer(NodeServices.layer)("t3 update under a managed policy", (it) => {
118170
}
119171
}),
120172
);
173+
174+
it.effect("looks for updates on the policy's mirror, never the public releases", () =>
175+
Effect.gen(function* () {
176+
const requested: string[] = [];
177+
const error = yield* runWithPolicy(
178+
{ mode: "auto", feedUrl: "https://updates.example.com/t3code" },
179+
{ channel: "stable" },
180+
HttpClient.make((request) => {
181+
requested.push(new URL(request.url).origin);
182+
return Effect.succeed(
183+
HttpClientResponse.fromWeb(request, new Response("", { status: 404 })),
184+
);
185+
}),
186+
);
187+
assert.include(error.message, "from Example Corp's update mirror");
188+
assert.deepStrictEqual(requested, ["https://updates.example.com"]);
189+
}),
190+
);
121191
});
122192

123193
it.layer(NodeServices.layer)("t3 update launcher", (it) => {

‎apps/server/src/cli/update.ts‎

Lines changed: 62 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import {
55
cliReleaseIndexPageUrl,
66
cliReleaseChannelOf,
77
newestCliReleaseVersion,
8+
releaseAssetUrl,
9+
updateFeedFileName,
810
type CliReleaseChannel,
911
} from "@t3tools/shared/cliRelease";
1012
import * as Console from "effect/Console";
@@ -24,6 +26,7 @@ import {
2426
ensurePinnedRuntimeInstalled,
2527
launcherOwnsVersionsDir,
2628
pinnedRuntimeCommand,
29+
isUnpublishedRelease,
2730
PinnedRuntimeInstallError,
2831
pinnedRuntimePaths,
2932
pinnedRuntimeVersionsDir,
@@ -33,7 +36,7 @@ import {
3336
} from "../cloud/pinnedRuntime.ts";
3437
import { compareExactServiceVersions, isExactServiceVersion } from "../cloud/serviceProtocol.ts";
3538
import * as ManagedPolicy from "../policy/ManagedPolicy.ts";
36-
import { managedUpdateRefusal } from "../policy/managedUpdates.ts";
39+
import { managedUpdateMirror, managedUpdateRefusal } from "../policy/managedUpdates.ts";
3740
import * as ProcessRunner from "../processRunner.ts";
3841
import { isProcessAlive, readPersistedServerRuntimeState } from "../serverRuntimeState.ts";
3942
import { projectLocationFlags, resolveCliAuthConfig } from "./config.ts";
@@ -94,6 +97,47 @@ export const resolveNewestVersion = Effect.fn("cli.update.resolve_newest")(funct
9497
return yield* new CliUpdateError({ reason: `No published ${channel} release was found.` });
9598
});
9699

100+
/** The version an update mirror's feed file for this platform offers on `channel`. */
101+
export const resolveMirrorVersion = Effect.fn("cli.update.resolve_mirror")(function* (
102+
mirror: { readonly organization: string; readonly feedUrl: string },
103+
channel: CliReleaseChannel,
104+
) {
105+
if (channel === "preview") {
106+
return yield* new CliUpdateError({
107+
reason: `${mirror.organization}'s update mirror has no preview builds.`,
108+
});
109+
}
110+
const httpClient = yield* HttpClient.HttpClient;
111+
const fileName = updateFeedFileName(
112+
channel === "nightly" ? "nightly" : "latest",
113+
yield* HostProcess.Platform,
114+
yield* HostProcess.Architecture,
115+
);
116+
const url = releaseAssetUrl(mirror.feedUrl, fileName);
117+
// Not the URL, whose query can hold the mirror's access token.
118+
const unreadable = new CliUpdateError({
119+
reason: `Could not read ${fileName} from ${mirror.organization}'s update mirror.`,
120+
});
121+
const body = yield* httpClient.get(url).pipe(
122+
Effect.flatMap(HttpClientResponse.filterStatusOk),
123+
Effect.flatMap((response) => response.text),
124+
Effect.mapError(() => unreadable),
125+
Effect.timeoutOrElse({
126+
duration: RELEASE_INDEX_TIMEOUT,
127+
orElse: () => Effect.fail(unreadable),
128+
}),
129+
);
130+
const version = /^version:\s*["']?([^"'\s]+)/m.exec(body)?.[1];
131+
if (
132+
version === undefined ||
133+
!isExactServiceVersion(version) ||
134+
cliReleaseChannelOf(version) !== channel
135+
) {
136+
return yield* unreadable;
137+
}
138+
return version;
139+
});
140+
97141
/**
98142
* Only a launcher that already points into this home's `runtime/versions` tree is touched; a
99143
* plain copy of the executable, or a launcher for some other install, is left alone. It is
@@ -320,6 +364,7 @@ export const runUpdate = Effect.fn("cli.update.run")(function* (input: {
320364
const httpClient = yield* HttpClient.HttpClient;
321365
const service = yield* BootService.BootService;
322366
const policy = yield* ManagedPolicy.ManagedPolicy;
367+
const mirror = managedUpdateMirror(policy.document);
323368

324369
const currentVersion = packageJson.version;
325370
const channel = input.channel ?? cliReleaseChannelOf(currentVersion);
@@ -338,9 +383,11 @@ export const runUpdate = Effect.fn("cli.update.run")(function* (input: {
338383
const progress = createUpdateProgress();
339384
progress.status("Checking for updates...");
340385
const targetVersion = yield* (
341-
input.requestedVersion === undefined
342-
? resolveNewestVersion(channel)
343-
: Effect.succeed(input.requestedVersion)
386+
input.requestedVersion !== undefined
387+
? Effect.succeed(input.requestedVersion)
388+
: mirror === undefined
389+
? resolveNewestVersion(channel)
390+
: resolveMirrorVersion(mirror, channel)
344391
).pipe(Effect.ensuring(Effect.sync(progress.finish)));
345392
const targetChannel = cliReleaseChannelOf(targetVersion);
346393

@@ -455,7 +502,8 @@ export const runUpdate = Effect.fn("cli.update.run")(function* (input: {
455502
httpClient,
456503
platform,
457504
arch,
458-
releaseBaseUrl: environment[CLI_RELEASE_BASE_URL_ENV]?.trim() || undefined,
505+
releaseBaseUrl: mirror?.feedUrl ?? (environment[CLI_RELEASE_BASE_URL_ENV]?.trim() || undefined),
506+
requirePublishedRelease: mirror !== undefined,
459507
validate: (paths) =>
460508
runner
461509
.run({
@@ -481,17 +529,15 @@ export const runUpdate = Effect.fn("cli.update.run")(function* (input: {
481529
),
482530
}).pipe(
483531
Effect.ensuring(Effect.sync(progress.finish)),
484-
Effect.catchIf(
485-
(error): error is PinnedRuntimeInstallError =>
486-
error._tag === "PinnedRuntimeInstallError" &&
487-
error.step.startsWith("downloading the t3 release checksums") &&
488-
String(error.cause).includes("404"),
489-
() =>
490-
Effect.fail(
491-
new CliUpdateError({
492-
reason: `No release archive was published for t3@${targetVersion}.`,
493-
}),
494-
),
532+
Effect.catchIf(isUnpublishedRelease, () =>
533+
Effect.fail(
534+
new CliUpdateError({
535+
reason:
536+
mirror === undefined
537+
? `No release archive was published for t3@${targetVersion}.`
538+
: `${mirror.organization}'s update mirror has no t3@${targetVersion} for this machine.`,
539+
}),
540+
),
495541
),
496542
);
497543

‎apps/server/src/cloud/pinnedRuntime.test.ts‎

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import * as HostProcess from "@t3tools/shared/HostProcess";
1616
import * as ProcessRunner from "../processRunner.ts";
1717
import {
1818
ensurePinnedRuntimeInstalled,
19+
isUnpublishedRelease,
1920
pinnedRuntimeCommand,
2021
pinnedRuntimePaths,
2122
PinnedRuntimeInstallError,
@@ -39,7 +40,7 @@ const validChecksums = archiveHex(archiveBytes).pipe(
3940
const releaseHttpClient = (checksums: string, requests: string[] = []) =>
4041
HttpClient.make((request) => {
4142
requests.push(request.url);
42-
const body = request.url.endsWith("/SHA256SUMS") ? checksums : archiveBytes;
43+
const body = new URL(request.url).pathname.endsWith("/SHA256SUMS") ? checksums : archiveBytes;
4344
return Effect.succeed(HttpClientResponse.fromWeb(request, new Response(body)));
4445
});
4546
const extractingRunner = (fs: FileSystem.FileSystem, path: Path.Path, commands: string[] = []) =>
@@ -155,6 +156,40 @@ it.layer(NodeServices.layer)("ensurePinnedRuntimeInstalled", (it) => {
155156
}),
156157
);
157158

159+
it.effect("reports a release that lists an archive it does not serve as unpublished", () =>
160+
Effect.gen(function* () {
161+
const fs = yield* FileSystem.FileSystem;
162+
const path = yield* Path.Path;
163+
const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pinned-archive-missing-" });
164+
const checksums = yield* validChecksums;
165+
const failsWith = (status: number) =>
166+
ensurePinnedRuntimeInstalled({
167+
baseDir,
168+
version,
169+
fs,
170+
path,
171+
platform: "linux",
172+
arch: "x64",
173+
httpClient: HttpClient.make((request) =>
174+
Effect.succeed(
175+
HttpClientResponse.fromWeb(
176+
request,
177+
request.url.endsWith("/SHA256SUMS")
178+
? new Response(checksums)
179+
: new Response("", { status }),
180+
),
181+
),
182+
),
183+
// "404" in the mirror's path must not read a server error as a missing release.
184+
releaseBaseUrl: "https://updates.example.com/404/t3code",
185+
runner: extractingRunner(fs, path),
186+
validate: () => Effect.die("must not validate a missing archive"),
187+
}).pipe(Effect.flip);
188+
assert.isTrue(isUnpublishedRelease(yield* failsWith(404)));
189+
assert.isFalse(isUnpublishedRelease(yield* failsWith(500)));
190+
}),
191+
);
192+
158193
it.effect("refuses an archive whose checksum does not match the release", () =>
159194
Effect.gen(function* () {
160195
const fs = yield* FileSystem.FileSystem;
@@ -267,6 +302,42 @@ it.layer(NodeServices.layer)("ensurePinnedRuntimeInstalled", (it) => {
267302
}),
268303
);
269304

305+
it.effect("reuses a cached runtime under a mirror only while the mirror lists it", () =>
306+
Effect.gen(function* () {
307+
const fs = yield* FileSystem.FileSystem;
308+
const path = yield* Path.Path;
309+
const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pinned-runtime-mirror-" });
310+
const cached = pinnedRuntimePaths(path, baseDir, version, "linux");
311+
yield* fs.makeDirectory(path.dirname(cached.entryPath), { recursive: true });
312+
yield* fs.writeFileString(cached.entryPath, "#!/bin/sh\n");
313+
yield* fs.writeFileString(cached.sentinelPath, `${version}\n`);
314+
const install = (checksums: string, requests: string[]) =>
315+
ensurePinnedRuntimeInstalled({
316+
baseDir,
317+
version,
318+
fs,
319+
path,
320+
platform: "linux",
321+
arch: "x64",
322+
httpClient: releaseHttpClient(checksums, requests),
323+
// A signed mirror URL keeps its query on every file it serves.
324+
releaseBaseUrl: "https://updates.example.com/t3code/?token=abc",
325+
requirePublishedRelease: true,
326+
runner: extractingRunner(fs, path),
327+
validate: () => Effect.void,
328+
});
329+
330+
const withdrawn = yield* install("", []).pipe(Effect.flip);
331+
assert.include(withdrawn.message, `finding ${archiveName} in the t3 release checksums`);
332+
const requests: string[] = [];
333+
assert.equal((yield* install(yield* validChecksums, requests)).entryPath, cached.entryPath);
334+
// Listed, so the cached runtime is used without downloading it again.
335+
assert.deepEqual(requests, [
336+
`https://updates.example.com/t3code/v${version}/SHA256SUMS?token=abc`,
337+
]);
338+
}),
339+
);
340+
270341
it.effect("preserves a completed runtime when validation fails", () =>
271342
Effect.gen(function* () {
272343
const fs = yield* FileSystem.FileSystem;

0 commit comments

Comments
 (0)