UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

165 lines • 6.96 kB
/** * Shipped custody-infrastructure pins and the ONE device-side key check (custody design 3.1, 6.6(a)). * * The party being checked supplies only the key facts; account ids, the KMS ARN namespace, role names and * policy templates are constants in this release. Until the custody account exists no account id is * pinned, so every check fails closed as `mismatch`. */ import { canonicalJson, toStoreValue } from "@earendil-works/pi-agent-core"; import { decodeBase64 } from "./ecdsa-p256.js"; import { MAX_FACTS_AGE_MS, MAX_FACTS_FUTURE_SKEW_MS, } from "./key-facts-client.js"; import { KEY_POLICY_TEMPLATE_V1, } from "./key-policy-template.js"; export const PINNED_CUSTODY_INFRA = { custodyAccountIds: [], arnNamespace: "arn:aws:kms:us-east-1:", roleNames: { runtime: ["custody-runtime"], registrySigner: ["registry-signer"], provisioning: ["custody-provisioning"], reader: ["key-facts-reader"], breakglass: ["custody-breakglass"], }, templates: { 1: KEY_POLICY_TEMPLATE_V1 }, keyFactsEndpoint: undefined, keyFactsVerifierKeys: [], }; const SERVICE_KEY_NOTICES = { "template-unknown": "update omo to trust the new custody key", "facts-stale": "the custody key facts are out of date; check this device's clock", "principal-id": "the custody key's access policy no longer matches; cloud custody for this key is unavailable", }; const KMS_KEY_ARN = /^(arn:[a-z0-9-]+:kms:[a-z0-9-]+:)(\d{12}):key\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/; const ROLE_ORDER = [ "runtime", "registrySigner", "provisioning", "reader", "breakglass", ]; export function roleArn(accountId, roleName) { return `arn:aws:iam::${accountId}:role/${roleName}`; } function roleCombinations(accountId, pinned) { let partial = [{}]; for (const role of ROLE_ORDER) { partial = partial.flatMap((done) => pinned.roleNames[role].map((name) => ({ ...done, [role]: roleArn(accountId, name) }))); } return partial.flatMap(({ runtime, registrySigner, provisioning, reader, breakglass }) => runtime && registrySigner && provisioning && reader && breakglass ? [{ runtime, registrySigner, provisioning, reader, breakglass }] : []); } function parsePolicy(policy) { let parsed; try { parsed = JSON.parse(policy); } catch { return undefined; } if (typeof parsed !== "object" || parsed === null || !("Statement" in parsed) || !Array.isArray(parsed.Statement)) return undefined; const principals = []; for (const statement of parsed.Statement) { const aws = statement?.Principal?.AWS; for (const value of Array.isArray(aws) ? aws : [aws]) { if (typeof value !== "string") return undefined; principals.push(value); } } return { canonical: canonicalJson(toStoreValue(parsed)), principals }; } function sameSpki(a, b) { const left = decodeBase64(a); const right = decodeBase64(b); return left !== undefined && right !== undefined && Buffer.from(left).equals(Buffer.from(right)); } /** Custody entries list `[kemArn, sigArn]`; a placement-registry entry lists `[sigArn]`. */ function expectedKeys(entry) { if (entry.kind === "placement-registry") return [{ usage: "SIGN_VERIFY", spki: entry.sigPk, kind: "registry" }]; if (!entry.kemPk) return undefined; return [ { usage: "KEY_AGREEMENT", spki: entry.kemPk, kind: "custody-kem" }, { usage: "SIGN_VERIFY", spki: entry.sigPk, kind: "custody-sig" }, ]; } function checkOneKey(arn, facts, expected, template, pinned) { const match = KMS_KEY_ARN.exec(arn); const accountId = match?.[2]; if (!match || !accountId) return "arn"; if (match[1] !== pinned.arnNamespace) return "arn-namespace"; if (!pinned.custodyAccountIds.includes(accountId)) return "account-not-pinned"; if (facts.keyUsage !== expected.usage) return "key-usage"; if (facts.origin !== "AWS_KMS" || facts.keyManager !== "CUSTOMER" || facts.keyState !== "Enabled" || facts.keySpec !== "ECC_NIST_P256" || facts.multiRegion || facts.tags["omo-custody"] !== "true" || facts.grantCount !== 0) { return "key-property"; } if (!sameSpki(facts.spki, expected.spki)) return "spki"; const policy = parsePolicy(facts.policy); if (!policy) return "policy"; if (policy.principals.some((principal) => !principal.startsWith("arn:"))) return "principal-id"; const combinations = roleCombinations(accountId, pinned); const pinnedArns = new Set(combinations.flatMap((roles) => Object.values(roles))); if (policy.principals.some((principal) => !pinnedArns.has(principal))) return "role-outside-pins"; const matches = combinations.some((roles) => template.render({ accountId, keyKind: expected.kind, roles }) === policy.canonical); return matches ? "ok" : "policy"; } function isFresh(facts, now) { return now - facts.readAt <= MAX_FACTS_AGE_MS && facts.readAt - now <= MAX_FACTS_FUTURE_SKEW_MS; } function outcome(result, reason) { const notices = SERVICE_KEY_NOTICES; const notice = notices[reason]; return notice === undefined ? { result, reason } : { result, reason, notice }; } /** `facts` maps each of `entry.kmsKeyArns` to the verifier's answer for exactly that ARN. */ export function checkServiceKeyDetailed(entry, facts, options) { const pinned = options.pinned ?? PINNED_CUSTODY_INFRA; const template = pinned.templates[entry.templateVersion]; if (!template) return outcome("unknown", "template-unknown"); const expected = expectedKeys(entry); if (!expected || entry.kmsKeyArns.length !== expected.length || new Set(entry.kmsKeyArns).size !== expected.length) { return outcome("mismatch", "shape"); } const known = []; for (const arn of entry.kmsKeyArns) { const fact = facts.get(arn); if (fact === undefined || fact === "unknown") return outcome("unknown", "facts-unknown"); if ("refusal" in fact) return outcome("unknown", "facts-stale"); if (!isFresh(fact, options.now)) return outcome("unknown", "facts-stale"); known.push(fact); } for (const [index, key] of expected.entries()) { const arn = entry.kmsKeyArns[index]; const fact = known[index]; if (arn === undefined || fact === undefined) return outcome("mismatch", "shape"); const reason = checkOneKey(arn, fact, key, template, pinned); if (reason !== "ok") return outcome("mismatch", reason); } return outcome("ok", "ok"); } export function checkServiceKey(entry, facts, options) { return checkServiceKeyDetailed(entry, facts, options).result; } //# sourceMappingURL=pinned-infra.js.map