mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-26 06:37:33 +00:00
feat(sdk): allow new signing key (#2088)
This commit is contained in:
@@ -46,9 +46,67 @@ export async function retroSignVersions(
|
||||
}
|
||||
|
||||
const unsigned = entry.versions.filter((v) => !v.signature);
|
||||
const signatures = await signPinnedArtifacts(unsigned, key, 'retro-sign');
|
||||
|
||||
// All-or-nothing: only mutate the entry once every artifact verified and signed.
|
||||
for (const v of unsigned) {
|
||||
v.signature = signatures.get(v.version)!;
|
||||
log(` ✓ retro-signed v${v.version}`);
|
||||
}
|
||||
return unsigned.map((v) => v.version);
|
||||
}
|
||||
|
||||
/**
|
||||
* Key ROTATION: re-sign every published version with a NEW key and swap the entry's
|
||||
* `authorPublicKey` to it.
|
||||
*
|
||||
* This is the artifact half of a rotation, and the only half tooling can do. The other half is
|
||||
* human: a registry maintainer must apply the `allow-key-change` label to the PR, and every admin
|
||||
* who already installed the plugin must re-trust it (SIGNATURE_KEY_CHANGED, one instance at a
|
||||
* time). Callers own saying that out loud — this function just makes the entry internally
|
||||
* consistent, which unsigned-or-old-key versions would not be: TREK verifies whichever version it
|
||||
* installs against `authorPublicKey`, so after a rotation EVERY version must carry a new-key
|
||||
* signature, not just the newest.
|
||||
*/
|
||||
export async function rotateSignatures(
|
||||
entry: RetroSignableEntry,
|
||||
newKeyPath: string,
|
||||
log: (line: string) => void = () => {},
|
||||
): Promise<string[]> {
|
||||
if (!entry.authorPublicKey) {
|
||||
throw new Error(
|
||||
'this plugin is not published signed — there is no key to rotate. To START signing, publish a signed update (pass --sign); the older versions are retro-signed for you.',
|
||||
);
|
||||
}
|
||||
const key = loadPrivateKey(newKeyPath);
|
||||
if (publicKeyBase64(key) === entry.authorPublicKey) {
|
||||
throw new Error(`the key at ${newKeyPath} is already the published authorPublicKey — nothing to rotate.`);
|
||||
}
|
||||
|
||||
const signatures = await signPinnedArtifacts(entry.versions, key, 'rotate');
|
||||
|
||||
// All-or-nothing: only mutate the entry once every artifact verified and signed.
|
||||
for (const v of entry.versions) {
|
||||
v.signature = signatures.get(v.version)!;
|
||||
log(` ✓ re-signed v${v.version} with the new key`);
|
||||
}
|
||||
entry.authorPublicKey = publicKeyBase64(key);
|
||||
return entry.versions.map((v) => v.version);
|
||||
}
|
||||
|
||||
/**
|
||||
* Download each version's pinned artifact, verify the bytes still hash to the pinned sha256, and
|
||||
* sign those exact bytes. Returns version → signature; throws — signing nothing — if ANY version
|
||||
* fails, so both callers stay all-or-nothing.
|
||||
*/
|
||||
async function signPinnedArtifacts(
|
||||
versions: RetroSignableEntry['versions'],
|
||||
key: import('node:crypto').KeyObject,
|
||||
mode: 'retro-sign' | 'rotate',
|
||||
): Promise<Map<string, string>> {
|
||||
const signatures = new Map<string, string>();
|
||||
const failures: string[] = [];
|
||||
for (const v of unsigned) {
|
||||
for (const v of versions) {
|
||||
try {
|
||||
const r = await fetch(v.downloadUrl, { headers: { 'User-Agent': 'trek-plugin' } });
|
||||
if (!r.ok) { failures.push(`v${v.version}: HTTP ${r.status} fetching ${v.downloadUrl}`); continue; }
|
||||
@@ -65,17 +123,14 @@ export async function retroSignVersions(
|
||||
}
|
||||
|
||||
if (failures.length) {
|
||||
const why = mode === 'rotate'
|
||||
? 'a rotated entry must have every version re-signed with the new key'
|
||||
: 'the registry requires every version signed once a key is present';
|
||||
throw new Error(
|
||||
`could not retro-sign ${failures.length} older version(s) — the registry requires every version signed once a key is present:\n` +
|
||||
`could not ${mode} ${failures.length} version(s) — ${why}:\n` +
|
||||
failures.map((f) => ` ✗ ${f}`).join('\n') +
|
||||
'\n\nNothing was signed. Fix the artifacts above (or remove those versions from the entry) and re-run.',
|
||||
);
|
||||
}
|
||||
|
||||
// All-or-nothing: only mutate the entry once every artifact verified and signed.
|
||||
for (const v of unsigned) {
|
||||
v.signature = signatures.get(v.version)!;
|
||||
log(` ✓ retro-signed v${v.version}`);
|
||||
}
|
||||
return unsigned.map((v) => v.version);
|
||||
return signatures;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user