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
2 changes: 2 additions & 0 deletions .github/workflows/mirror-cnb.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ on:
- lithe-plugin-index.json
- lithe-plugin-deltas.json
- lithe-plugin-recommendations.json
- lithe-runtime-mirrors.json
- assets/**
- scripts/mirror-cnb.mjs
- scripts/validate-runtime-mirrors.mjs
workflow_dispatch:
inputs:
plugin:
Expand Down
5 changes: 4 additions & 1 deletion .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,17 @@ jobs:
run: sudo apt-get install -y zstd

- name: Unit tests
run: node --test scripts/store.test.mjs scripts/plugin-deltas.test.mjs
run: node --test scripts/store.test.mjs scripts/plugin-deltas.test.mjs scripts/mirror-cnb.test.mjs

- name: Validate index
run: node scripts/validate-index.mjs lithe-plugin-index.json

- name: Validate recommendations
run: node scripts/validate-recommendations.mjs lithe-plugin-recommendations.json lithe-plugin-index.json

- name: Validate runtime mirrors
run: node scripts/validate-runtime-mirrors.mjs lithe-runtime-mirrors.json

- name: Pick the base index
id: base
run: |
Expand Down
13 changes: 13 additions & 0 deletions lithe-runtime-mirrors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"schemaVersion": 0,
"runtimes": [
{
"id": "eclipse-jdtls-1.61.0",
"assetName": "jdt-language-server-1.61.0-202609031315.tar.gz",
"sourceURL": "https://download.eclipse.org/jdtls/milestones/1.61.0/jdt-language-server-1.61.0-202609031315.tar.gz",
"sha256": "338e7e73d61836651ba2453919a0d34fa763eb4e7c03342092309bffb8934c64",
"license": "EPL-2.0",
"licenseURL": "https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt"
}
]
}
87 changes: 82 additions & 5 deletions scripts/mirror-cnb.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ const DEFAULT_KEEP_VERSIONS = 3;
const MAX_CAPABILITY_EXTRA_VERSIONS = 2;
const MAX_ARCHIVE_BYTES = 512 * 1024 * 1024;
const MAX_ICON_BYTES = 2 * 1024 * 1024;
const DEFAULT_DOWNLOAD_TIMEOUT_MS = 60_000;
const ARCHIVE_DOWNLOAD_TIMEOUT_MS = 180_000;
const CHECKSUMS_ASSET = ".mirror-checksums.json";
const ICONS_TAG = "plugin-icons";
const RUNTIMES_TAG = "managed-runtimes";
const RUNTIMES_FILE = "lithe-runtime-mirrors.json";
const PLUGIN_TAG_PREFIX = "plugin-";
const MAX_ATTEMPTS = 3;
const RETRY_DELAY_MS = 1500;
Expand Down Expand Up @@ -282,9 +286,9 @@ class CNBClient {
}
}

async function downloadAndVerify(url, destPath, expectedSha256, maxBytes) {
async function downloadAndVerify(url, destPath, expectedSha256, maxBytes, timeoutMs = DEFAULT_DOWNLOAD_TIMEOUT_MS) {
return await withRetry(`Download ${path.basename(destPath)}`, async () => {
const res = await fetch(url, { signal: AbortSignal.timeout(60_000) });
const res = await fetch(url, { signal: AbortSignal.timeout(timeoutMs) });
if (!res.ok) {
const err = new Error(`GET ${url} returned HTTP ${res.status}`);
err.retryable = res.status >= 500 || res.status === 429;
Expand Down Expand Up @@ -358,7 +362,13 @@ async function syncReleaseAssets(client, repo, { tag, title, body, targetCommiti
continue;
}
const localFile = path.join(scratchDir, assetName);
const actualDigest = await downloadAndVerify(spec.url, localFile, spec.sha256, spec.maxBytes);
const actualDigest = await downloadAndVerify(
spec.url,
localFile,
spec.sha256,
spec.maxBytes,
spec.timeoutMs ?? DEFAULT_DOWNLOAD_TIMEOUT_MS,
);
await client.uploadAsset(repo, release.id, localFile, assetName);
await rm(localFile, { force: true });
nextChecksums[assetName] = actualDigest;
Expand All @@ -382,6 +392,41 @@ async function syncReleaseAssets(client, repo, { tag, title, body, targetCommiti
}
}

async function loadRuntimeMirrors(filePath = RUNTIMES_FILE) {
let raw;
try {
raw = await readFile(filePath, "utf8");
} catch (err) {
if (err?.code === "ENOENT") return null;
throw err;
}
const parsed = JSON.parse(raw);
if (!parsed || typeof parsed !== "object" || parsed.schemaVersion !== 0 || !Array.isArray(parsed.runtimes)) {
throw new Error(`Invalid ${filePath}: expected schemaVersion 0 and runtimes array`);
}
for (const entry of parsed.runtimes) {
if (!entry?.id || !entry?.assetName || !entry?.sourceURL || !entry?.sha256) {
throw new Error(`Invalid runtime mirror entry in ${filePath}`);
}
if (entry.assetName === CHECKSUMS_ASSET || entry.assetName.includes("/") || entry.assetName.includes("\\") || entry.assetName === "." || entry.assetName === "..") {
throw new Error(`Unsafe runtime mirror assetName in ${filePath}: ${entry.assetName}`);
}
}
return parsed;
}

function buildRuntimeReleaseBody(runtimes) {
const lines = [
"CNB mirror of verified external managed runtime archives referenced by Lithe-store plugins.",
"Each archive is redistributed verbatim with upstream SHA-256 verification:",
"",
];
for (const item of runtimes) {
lines.push(`- \`${item.id}\` (\`${item.assetName}\`): ${item.sourceURL} — License: [${item.license}](${item.licenseURL}), SHA-256: \`${item.sha256}\``);
}
return lines.join("\n");
}

export async function run(argv = process.argv.slice(2)) {
const opts = parseArgs(argv);
if (opts.help) {
Expand Down Expand Up @@ -434,14 +479,24 @@ export async function run(argv = process.argv.slice(2)) {
if (prev && prev.sha256 !== dl.sha256) {
throw new Error(`Conflicting SHA-256 for asset ${assetName} in ${plugin.id}`);
}
desired.set(assetName, { url: dl.downloadURL, sha256: dl.sha256, maxBytes: MAX_ARCHIVE_BYTES });
desired.set(assetName, {
url: dl.downloadURL,
sha256: dl.sha256,
maxBytes: MAX_ARCHIVE_BYTES,
timeoutMs: ARCHIVE_DOWNLOAD_TIMEOUT_MS,
});
}
}
for (const patch of deltasByPlugin.get(plugin.id) || []) {
if (!selectedVersionNames.has(patch.toVersion)) continue;
const assetName = path.basename(new URL(patch.downloadURL).pathname);
if (!assetName) continue;
desired.set(assetName, { url: patch.downloadURL, sha256: patch.sha256, maxBytes: MAX_ARCHIVE_BYTES });
desired.set(assetName, {
url: patch.downloadURL,
sha256: patch.sha256,
maxBytes: MAX_ARCHIVE_BYTES,
timeoutMs: ARCHIVE_DOWNLOAD_TIMEOUT_MS,
});
}

const tag = `${PLUGIN_TAG_PREFIX}${plugin.id}`;
Expand Down Expand Up @@ -471,8 +526,30 @@ export async function run(argv = process.argv.slice(2)) {
scratchDir,
});

const runtimeManifest = await loadRuntimeMirrors();
if (runtimeManifest && runtimeManifest.runtimes.length > 0) {
const runtimeAssets = new Map();
for (const entry of runtimeManifest.runtimes) {
runtimeAssets.set(entry.assetName, {
url: entry.sourceURL,
sha256: entry.sha256,
maxBytes: MAX_ARCHIVE_BYTES,
timeoutMs: ARCHIVE_DOWNLOAD_TIMEOUT_MS,
});
}
await syncReleaseAssets(client, opts.repo, {
tag: RUNTIMES_TAG,
title: "CNB mirror for managed plugin runtimes",
body: buildRuntimeReleaseBody(runtimeManifest.runtimes),
targetCommitish: headSha,
desiredAssets: runtimeAssets,
scratchDir,
});
}

const activeTags = new Set((index.plugins || []).map((p) => `${PLUGIN_TAG_PREFIX}${p.id}`));
activeTags.add(ICONS_TAG);
activeTags.add(RUNTIMES_TAG);
for (const rel of await client.listAllReleases(opts.repo)) {
const tag = rel.tag_name || "";
if (tag.startsWith(PLUGIN_TAG_PREFIX) && !activeTags.has(tag)) {
Expand Down
122 changes: 122 additions & 0 deletions scripts/mirror-cnb.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import os from "node:os";
import path from "node:path";
import test from "node:test";
import { run, selectVersionsToMirror } from "./mirror-cnb.mjs";
import { validateRuntimeMirrors } from "./validate-runtime-mirrors.mjs";

const sha256 = (text) => createHash("sha256").update(text).digest("hex");

Expand Down Expand Up @@ -305,3 +306,124 @@ test("mirror-cnb refuses to upload on SHA-256 mismatch", { timeout: 30000 }, asy
await mock.close();
}
});

test("mirror-cnb syncs managed runtime mirrors, skips unchanged assets, prunes removed entries, and rejects SHA-256 mismatch", { timeout: 30000 }, async () => {
const files = new Map([
["/jdtls/jdtls-1.61.0.tar.gz", "jdtls-archive-bytes"],
["/jdtls/old-runtime.tar.gz", "old-runtime-bytes"],
]);
const mock = await startMockServers(files);
const work = await mkdtemp(path.join(os.tmpdir(), "lithe-store-runtimes-"));
const prevCwd = process.cwd();
const prevEnv = { ...process.env };
try {
process.chdir(work);
process.env.CNB_TOKEN = "cnb-secret";
process.env.CNB_API_BASE = mock.cnbBase;
process.env.CNB_WEB_BASE = mock.cnbBase;

await writeFile(path.join(work, "lithe-plugin-index.json"), JSON.stringify({ schemaVersion: 0, plugins: [] }));
await writeFile(path.join(work, "lithe-plugin-deltas.json"), JSON.stringify({ schemaVersion: 0, patches: [] }));
await writeFile(path.join(work, "lithe-runtime-mirrors.json"), JSON.stringify({
schemaVersion: 0,
runtimes: [{
id: "eclipse-jdtls-1.61.0",
assetName: "jdtls-1.61.0.tar.gz",
sourceURL: `${mock.ghBase}/jdtls/jdtls-1.61.0.tar.gz`,
sha256: sha256("jdtls-archive-bytes"),
license: "EPL-2.0",
licenseURL: "https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt",
}],
}));

await run(["--skip-git-push"]);

const runtimesRel = mock.releases.get("managed-runtimes");
assert.ok(runtimesRel);
assert.match(runtimesRel.body || "", /EPL-2\.0/);
assert.deepEqual(
runtimesRel.assets.map((a) => a.name).sort(),
[".mirror-checksums.json", "jdtls-1.61.0.tar.gz"],
);

// Rerun with identical manifest: zero re-downloads or uploads.
const eventCount = mock.events.length;
await run(["--skip-git-push"]);
const rerunEvents = mock.events.slice(eventCount);
assert.equal(rerunEvents.some((e) => e.startsWith("GH GET")), false);
assert.equal(rerunEvents.some((e) => e.startsWith("CNB PUT")), false);

// Removing an older asset from the manifest prunes it from the release.
runtimesRel.assets.push({ id: "stale-asset", name: "stale-runtime.tar.gz" });
await run(["--skip-git-push"]);
assert.deepEqual(
runtimesRel.assets.map((a) => a.name).sort(),
[".mirror-checksums.json", "jdtls-1.61.0.tar.gz"],
);

// Mismatched SHA-256 refuses to upload.
await writeFile(path.join(work, "lithe-runtime-mirrors.json"), JSON.stringify({
schemaVersion: 0,
runtimes: [{
id: "eclipse-jdtls-1.61.0",
assetName: "jdtls-bad.tar.gz",
sourceURL: `${mock.ghBase}/jdtls/jdtls-1.61.0.tar.gz`,
sha256: "0".repeat(64),
license: "EPL-2.0",
licenseURL: "https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt",
}],
}));
const badStart = mock.events.length;
await assert.rejects(() => run(["--skip-git-push"]), /SHA-256 mismatch/);
assert.equal(mock.events.slice(badStart).some((e) => e.startsWith("CNB PUT")), false);
} finally {
process.chdir(prevCwd);
process.env = prevEnv;
await rm(work, { recursive: true, force: true });
await mock.close();
}
});

test("validateRuntimeMirrors accepts valid manifest and rejects invalid fields, order, or unsafe URLs", () => {
const valid = {
schemaVersion: 0,
runtimes: [{
id: "eclipse-jdtls-1.61.0",
assetName: "jdt-language-server-1.61.0-202609031315.tar.gz",
sourceURL: "https://download.eclipse.org/jdtls/milestones/1.61.0/jdt-language-server-1.61.0-202609031315.tar.gz",
sha256: "338e7e73d61836651ba2453919a0d34fa763eb4e7c03342092309bffb8934c64",
license: "EPL-2.0",
licenseURL: "https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt",
}],
};
assert.deepEqual(validateRuntimeMirrors(valid), []);

const bad = {
schemaVersion: 1,
runtimes: [
{
id: "z-runtime",
assetName: "../bad.tar.gz",
sourceURL: "http://insecure.example/a.tar.gz",
sha256: "not-hex",
license: "",
licenseURL: "https://user:pass@example.com/license",
},
{
id: "a-runtime",
assetName: "ok.tar.gz",
sourceURL: "https://example.com/ok.tar.gz",
sha256: "a".repeat(64),
license: "MIT",
licenseURL: "https://example.com/license",
},
],
};
const problems = validateRuntimeMirrors(bad);
assert.ok(problems.some((p) => p.includes("schemaVersion")));
assert.ok(problems.some((p) => p.includes("assetName")));
assert.ok(problems.some((p) => p.includes("sourceURL")));
assert.ok(problems.some((p) => p.includes("sha256")));
assert.ok(problems.some((p) => p.includes("licenseURL")));
assert.ok(problems.some((p) => p.includes("sorted")));
});
Loading