diff --git a/modules/gitea/test/token.test.ts b/modules/gitea/test/token.test.ts index bfe5da014..18e5b6d1b 100644 --- a/modules/gitea/test/token.test.ts +++ b/modules/gitea/test/token.test.ts @@ -17,6 +17,7 @@ import { join } from "node:path"; import { collectTools } from "@novox/mesh-sdk/tools"; import { AdminRefused, MintedToken, TOKEN_NAME, TOKEN_SCOPES, leaseOverState, openValue, vaultOverBus, vaultValue, type MintedTokenOptions } from "../dist/token.js"; import { TOKEN_FILE_UNREVOKED, passOver, type ForgeTokens, type ListedToken } from "../dist/tokenfiles.js"; +import { PACKAGES_TOKEN_NAME, PACKAGES_TOKEN_SCOPES } from "../dist/packages-account.js"; import { tokenLeaseTool } from "../dist/tools/tokenlease.js"; import { fakeBus } from "./fakebus.ts"; import { GiteaClient } from "../dist/client.js"; @@ -41,6 +42,8 @@ interface Forge { /** Plant a token by name, beside any other of the same name, as a raced mint would. */ plant(name: string, sha1?: string, scopes?: string[]): void; scopesOf: Map; + /** The account the last mint was for; while it is the tools' account the admin's own list reads empty. */ + mintedFor: string; admins: Map; pullState: string; pullTitle: string; @@ -237,6 +240,8 @@ function fakeForge(): Promise { url: `http://127.0.0.1:${port}`, get mints() { return forge.mints; }, get lastScopes() { return forge.lastScopes; }, + get mintedFor() { return forge.mintedFor; }, + set mintedFor(v: string) { forge.mintedFor = v; }, get pullState() { return forge.pullState; }, get pullTitle() { return forge.pullTitle; }, get branchDeleted() { return forge.branchDeleted; }, @@ -704,6 +709,29 @@ test("a start ends the token files: the lists are read before the first mint dro assert.deepEqual(ORDER, [`own-put ${VAULT} 1`, `delete ${TOKEN_NAME}`], "the own-put, then the drop of the old name"); assert.ok(logs.some((l) => l.includes("token.tmp") && l.includes("no whole token")), logs.join("\n")); assert.ok(logs.every((l) => !l.includes(HEX) && !l.includes(vaulted(bus))), "no line names a token"); + + // A `.tmp` holding a whole token goes with its pair: revoked at the forge by its id, then deleted (H-A-699). + const HEX2 = "fedcba9876543210fedcba9876543210fedcba98"; + forge.tokens.clear(); + forge.plant("kept-by-the-tmp", HEX2, SCOPES); + await leftFiles(env, { "token.tmp": HEX2 + "\n", "token.scopes": SCOPES_LINE + "\n" }); + forge.deletedBy.length = 0; + await minted(env, logs, { stateDir: env.MESH_GITEA_STATE_DIR, tokenFile: "token", findings, listAccounts: async () => ["mesh-tools"] }).listRepos(); + assert.deepEqual(await left(env), [], logs.join("\n")); + assert.equal(forge.tokens.has("kept-by-the-tmp"), false, "the .tmp's token is revoked at the forge"); + + // Both pairs, the tools' `token` and the packages' `packages-token`, go at a first mint, each by its own source. + const env2 = (await delivered(forge)).env; + forge.tokens.clear(); + forge.plant(TOKEN_NAME, HEX, SCOPES); + forge.plant(PACKAGES_TOKEN_NAME, HEX2, PACKAGES_TOKEN_SCOPES); + await leftFiles(env2, { token: HEX + "\n", "token.scopes": SCOPES_LINE + "\n", "packages-token": HEX2 + "\n", "packages-token.scopes": "write:package for mesh-packages\n" }); + const both = { stateDir: env2.MESH_GITEA_STATE_DIR, tokenFile: "token", findings, listAccounts: async () => ["mesh-tools", "mesh-packages"] }; + await new GiteaClient(env2.MESH_GITEA_URL!, source(env2, logs, both)).listRepos(); + await source(env2, logs, { ...both, account: "mesh-packages", oldName: PACKAGES_TOKEN_NAME, + scopes: PACKAGES_TOKEN_SCOPES, tokenFile: "packages-token" }).current(); + assert.deepEqual(await left(env2), [], logs.join("\n")); + assert.equal(forge.tokens.has(PACKAGES_TOKEN_NAME), false, "the packages file's token is revoked at the forge"); }); test("a token file stays when the vault refuses the own-put, and when the forge refuses the revocation or matches nothing: said, written as a finding, cleared once the pair goes", async () => { @@ -759,4 +787,13 @@ test("a start with the vault holding a token and a file left runs the pass; with const failing = { ...options, listAccounts: async () => { throw new Error("no lists without files"); } }; await new GiteaClient(env.MESH_GITEA_URL!, source(env, quiet, failing)).listRepos(); assert.ok(quiet.every((l) => !l.includes("no lists")), quiet.join("\n")); + + // With a retire file left too, the lists are read before the admin's old token goes, so a file holding that token ends (H-A-699 (2)). + forge.mintedFor = ""; // the admin's own list holds the old token, as the forge would list it + forge.plant(TOKEN_NAME, HEX, SCOPES); + await leftFiles(env, { token: HEX + "\n", "token.scopes": SCOPES_LINE + "\n", "mesh-tools.retire-admin": "now\n" }); + const retiring: string[] = []; + await new GiteaClient(env.MESH_GITEA_URL!, source(env, retiring, { ...options, findings: sink() })).listRepos(); + assert.ok(retiring.some((l) => l.includes(`deleted 1 token(s) named "${TOKEN_NAME}"`)), retiring.join("\n")); + assert.deepEqual(await left(env), [], retiring.join("\n")); }); diff --git a/modules/gitea/token.ts b/modules/gitea/token.ts index 52eb09991..edbd7593f 100644 --- a/modules/gitea/token.ts +++ b/modules/gitea/token.ts @@ -380,6 +380,8 @@ export class MintedToken implements TokenSource { } if (this.held === null) { this.log(`reusing the token the vault keeps as ${vaultName(this.opts.account)} (generation ${kept.generation})`); + // The forge's token lists are read before this start deletes any token (H-A-699 (2)). + await this.readFileLists(); // A retirement of the admin's old token that failed after the mint is tried again, once a process. if (this.retireFile && existsSync(this.retireFile)) await this.retireAdmin().catch((err) => this.log(`${err}`)); }