C4: the vault keeps the forge token with the scopes it was minted with
The file beside the token that kept its scopes went with C4, and with it the re-mint of a token carrying a scope beyond the tools' (write:admin). The value written through own-put now holds the token and its scopes; a token minted with other scopes, or a value naming none, is minted again unasked and the one it replaces deleted at the forge. The Go bundles read the token out of that value. The lost test is back as a row of the scope test in test/token.test.ts. Builds: design 47 H2 §808; H-A-439
This commit is contained in:
@@ -15,7 +15,7 @@ import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
|
||||
import { collectTools } from "@novox/mesh-sdk/tools";
|
||||
import { AdminRefused, MintedToken, TOKEN_NAME, TOKEN_SCOPES, leaseOverState, vaultOverBus, type MintedTokenOptions } from "../dist/token.js";
|
||||
import { AdminRefused, MintedToken, TOKEN_NAME, TOKEN_SCOPES, leaseOverState, openValue, vaultOverBus, vaultValue, type MintedTokenOptions } from "../dist/token.js";
|
||||
import { tokenLeaseTool } from "../dist/tools/tokenlease.js";
|
||||
import { fakeBus } from "./fakebus.ts";
|
||||
import { GiteaClient } from "../dist/client.js";
|
||||
@@ -288,6 +288,8 @@ function source(env: NodeJS.ProcessEnv, logs: string[], extra: Partial<MintedTok
|
||||
}
|
||||
|
||||
const VAULT = "module/gitea/mesh-tools";
|
||||
/** The token the vault keeps for the tools' account, out of the value that holds it with its scopes (H-A-439). */
|
||||
const vaulted = (bus: ReturnType<typeof fakeBus>) => openValue(bus.vault.get(VAULT)?.value ?? "").token;
|
||||
const forge = await fakeForge();
|
||||
after(() => forge.close());
|
||||
|
||||
@@ -304,7 +306,8 @@ test("first start: mints mesh-tools-1 with the admin account, writes it through
|
||||
// Design 47 H2 §808, H-A-422 (2), H-A-426: named per mint, kept as the module's own secret, never a file.
|
||||
const token = forge.tokens.get("mesh-tools-1")!;
|
||||
assert.ok(token);
|
||||
assert.deepEqual([bus.vault.get(VAULT)?.value, bus.vault.get(VAULT)?.generation], [token, 1]);
|
||||
assert.deepEqual([vaulted(bus), bus.vault.get(VAULT)?.generation], [token, 1]);
|
||||
assert.deepEqual(openValue(bus.vault.get(VAULT)!.value).scopes, [...TOKEN_SCOPES], "kept with the scopes it was minted with (H-A-439)");
|
||||
await assert.rejects(stat(join(env.MESH_GITEA_STATE_DIR!, "token")), /ENOENT/);
|
||||
assert.ok(logs.some((l) => l.startsWith("minted mesh-tools-1")), logs.join("\n"));
|
||||
assert.ok(logs.every((l) => !l.includes(token) && !l.includes(PASSWORD)), logs.join("\n"));
|
||||
@@ -336,7 +339,7 @@ test("the forge rejects the vault's token: mesh-tools-2 is minted once, written
|
||||
|
||||
assert.equal(repos.length, 1);
|
||||
assert.equal(forge.mints, before + 1);
|
||||
assert.equal(bus.vault.get(VAULT)?.value, forge.tokens.get("mesh-tools-2"));
|
||||
assert.equal(vaulted(bus), forge.tokens.get("mesh-tools-2"));
|
||||
// H-A-422 (2): the own-put first, the replaced token's delete after, by id; the forge holds one tools' token after.
|
||||
assert.deepEqual(ORDER, [`own-put ${VAULT} 2`, "delete mesh-tools-1"]);
|
||||
assert.deepEqual(forge.deletedBy, ["id"]);
|
||||
@@ -353,9 +356,9 @@ test("an own-put that fails leaves the old token standing, and the next mint rep
|
||||
forge.tokens.set("mesh-tools-1", "revoked-meanwhile");
|
||||
ORDER.length = 0;
|
||||
let failPut = true;
|
||||
const failing = minted(env, logs, { vault: { get: vaultOverBus().get, put: async (a, t) => {
|
||||
const failing = minted(env, logs, { vault: { get: vaultOverBus().get, put: async (a, t, scopes) => {
|
||||
if (failPut) { failPut = false; throw new Error("the vault is away"); }
|
||||
return vaultOverBus().put(a, t);
|
||||
return vaultOverBus().put(a, t, scopes);
|
||||
} } });
|
||||
|
||||
await assert.rejects(failing.listRepos(), /the vault is away/);
|
||||
@@ -365,7 +368,7 @@ test("an own-put that fails leaves the old token standing, and the next mint rep
|
||||
|
||||
assert.equal((await failing.listRepos()).length, 1);
|
||||
assert.deepEqual(ORDER, ["delete mesh-tools-2", `own-put ${VAULT} 2`, "delete mesh-tools-1"]);
|
||||
assert.notEqual(bus.vault.get(VAULT)?.value, old);
|
||||
assert.notEqual(vaulted(bus), old);
|
||||
});
|
||||
|
||||
test("a token older than token-ttl is minted again, and the one it replaces deleted", async () => {
|
||||
@@ -389,7 +392,7 @@ test("a token older than token-ttl is minted again, and the one it replaces dele
|
||||
assert.ok(logs.some((l) => l.includes("older than token-ttl")), logs.join("\n"));
|
||||
});
|
||||
|
||||
test("a token lacking a scope the tools need: the forge refuses the call for the scope, the token is re-minted with the whole list, and the call goes through", async () => {
|
||||
test("a token lacking a scope the tools need: the forge refuses the call for the scope, the token is re-minted with the whole list, and the call goes through", async (t) => {
|
||||
const { env, bus, logs } = await delivered(forge);
|
||||
forge.tokens.clear();
|
||||
const client = minted(env, logs);
|
||||
@@ -403,7 +406,33 @@ test("a token lacking a scope the tools need: the forge refuses the call for the
|
||||
assert.equal(repos.length, 1);
|
||||
assert.equal(forge.mints, before + 1);
|
||||
assert.deepEqual(forge.lastScopes, [...TOKEN_SCOPES]);
|
||||
assert.equal(bus.vault.get(VAULT)?.value, forge.tokens.get("mesh-tools-2"));
|
||||
assert.equal(vaulted(bus), forge.tokens.get("mesh-tools-2"));
|
||||
|
||||
// Design 47 H2 §808, H-A-439: the vault keeps the scopes beside the token, so one carrying a scope beyond the tools'
|
||||
// is minted again unasked, as when they were kept in a file beside it.
|
||||
await t.test("a token kept with write:admin is minted again without it at the next start, and the forge drops the old one", async () => {
|
||||
const { env, bus, logs } = await delivered(forge);
|
||||
forge.tokens.clear();
|
||||
await minted(env, logs).listRepos();
|
||||
const old = forge.tokens.get("mesh-tools-1")!;
|
||||
// An earlier mint asked write:admin too, and the vault keeps what it asked.
|
||||
bus.vault.get(VAULT)!.value = vaultValue(old, [...TOKEN_SCOPES, "write:admin"]);
|
||||
forge.scopesOf.set(old, [...TOKEN_SCOPES, "write:admin"]);
|
||||
const before = forge.mints;
|
||||
|
||||
const again: string[] = [];
|
||||
await minted(env, again).listRepos();
|
||||
|
||||
assert.equal(forge.mints, before + 1);
|
||||
assert.deepEqual(forge.lastScopes, [...TOKEN_SCOPES]);
|
||||
assert.ok(![...forge.tokens.values()].includes(old), "the old token still stands on the forge");
|
||||
assert.deepEqual(openValue(bus.vault.get(VAULT)!.value), { token: forge.tokens.get("mesh-tools-2"), scopes: [...TOKEN_SCOPES] });
|
||||
assert.ok(again.some((l) => l.includes("minted with other scopes")), again.join("\n"));
|
||||
// An admin route through the escape hatch is refused before it is sent.
|
||||
const tool = getGiteaTools(minted(env, again)).find((x) => x.name === "gitea_api")!;
|
||||
await assert.rejects(tool.run({ path: "/admin/users/mesh_novox_builder", method: "PATCH", body: { admin: true } }, undefined as never),
|
||||
/not a write gitea_api makes/);
|
||||
});
|
||||
});
|
||||
|
||||
test("the first mint deletes by id every token under the old fixed name, two raced ones too, and keeps the account's others", async () => {
|
||||
@@ -545,7 +574,7 @@ test("forge_token_lease says the lease's holder and since when, and each token's
|
||||
assert.equal(tool.name, "forge_token_lease");
|
||||
assert.deepEqual(said.tokens.map((t: any) => [t.account, t.held, t.generation, t["age-seconds"]]),
|
||||
[["mesh-tools", true, 1, 90], ["mesh-packages", false, undefined, undefined]]);
|
||||
assert.ok(!JSON.stringify(said).includes(bus.vault.get(VAULT)!.value), "the token is never said");
|
||||
assert.ok(!JSON.stringify(said).includes(vaulted(bus)), "the token is never said");
|
||||
// The lease lives mint-lease: past it, nobody holds it.
|
||||
clock += 30_000;
|
||||
assert.deepEqual((await tool.run({}, undefined as never) as any).lease, { held: false });
|
||||
|
||||
@@ -14,7 +14,7 @@ import { mkdtemp, readFile, writeFile } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
|
||||
import { MintedToken, TOKEN_NAME, TOKEN_SCOPES, leaseOverState, vaultOverBus } from "../dist/token.js";
|
||||
import { MintedToken, TOKEN_NAME, TOKEN_SCOPES, leaseOverState, openValue, vaultOverBus } from "../dist/token.js";
|
||||
import { fakeBus } from "./fakebus.ts";
|
||||
import { GiteaClient } from "../dist/client.js";
|
||||
import { TOOLS_ACCOUNT, TOOLS_TEAM_UNITS, BUILDER_ACCOUNT, AGENT_ACCOUNT, meshAccounts } from "../dist/tools-account.js";
|
||||
@@ -269,7 +269,7 @@ after(() => forge.close());
|
||||
|
||||
/** The bus of the environment made last: the vault's secrets and the lease (design 47 H2 §808). */
|
||||
let bus = fakeBus();
|
||||
const vaulted = (account: string) => bus.vault.get(`module/gitea/${account}`)?.value;
|
||||
const vaulted = (account: string) => { const v = bus.vault.get(`module/gitea/${account}`)?.value; return v === undefined ? undefined : openValue(v).token; };
|
||||
|
||||
async function environment(keepMainRules = false): Promise<NodeJS.ProcessEnv & { MESH_GITEA_STATE_DIR: string }> {
|
||||
bus = fakeBus();
|
||||
|
||||
Reference in New Issue
Block a user