Initial Commit

This commit is contained in:
2026-05-27 23:29:53 +01:00
commit fbc70f96e6
10000 changed files with 1102152 additions and 0 deletions

View File

@@ -0,0 +1,44 @@
# `@suaveplan/api-secrets-vault` — `@suaveplan/vault`-backed secrets adapter
**Package**: `@suaveplan/api-secrets-vault`
**Status**: PROPOSED
**Type**: new-package
**Stage**: 8 — see umbrella `openspec/changes/api-plugin-foundation/`
## Why
For multi-host / cloud deployments, secrets need to live in an encrypted, replicated container with a controlled unlock flow. `@suaveplan/vault` already implements that container (BIP39 + Argon2id + 2SKD + HKDF + SRP-6a + SecureBuffer). Wrapping it as the `secrets` capability gives the host a production-grade backend that sits alongside its own DB without depending on a third-party vault service.
## What changes
- ADDED: `createVaultSecretsAdapter({ vault, allowlist })` — implements the abstract `secrets` capability over a `@suaveplan/vault` instance. `vault` is passed in unlocked by the host's startup sequence; the adapter never sees the master key.
- ADDED: Per-plugin scoping — same allowlist contract as `api-secrets-bun`; reads outside the allowlist raise `SECRETS_VAULT_NOT_ALLOWED`.
- ADDED: Watcher support — when the vault publishes a `secret.updated` event for a name in the allowlist, the adapter emits a `secret.changed` event on the host event bus so plugins that cached the value can refresh.
- ADDED: Conformance — passes `runSecretsConformance` from `api-secrets/testing`.
- ADDED: Error codes (`SECRETS_VAULT_*`): `NOT_FOUND`, `NOT_ALLOWED`, `VAULT_LOCKED`, `VAULT_UNAVAILABLE`, `WRITE_FAILED`.
## Out of scope
- Vault unlock UX — host owns the unlock flow (interactive prompt, KMS-derived key, etc.).
- Vault snapshot / restore — `@suaveplan/vault` owns it.
- Cross-region vault replication — operator concern.
## Source mapping
Genesis-original. Source of design intent: `docs/superpowers/specs/2026-05-13-api-plugin-foundation-design.md`.
| Requirement | Design doc section |
|---|---|
| `secrets` capability | §3, §5, §13.2 |
| Per-plugin allowlist | §3 |
## Verification approach
- `src/adapter/adapter.test.ts` — read / write / list against a real in-memory vault; locked vault raises `SECRETS_VAULT_LOCKED`.
- `src/adapter/adapter.spec-conformance.test.ts` — `runSecretsConformance` passes.
- `src/watcher/watcher.test.ts` — `secret.updated` from vault → `secret.changed` on host bus, only for names in allowlist.
- `tests/dep-graph.test.ts` — runtime deps (`api-types`, `api-secrets`, `vault`, `error`, `logger`, `events`).
## Migration impact
None — additive only. `api-host` registers this when `host.secrets.backend = "vault"`.

View File

@@ -0,0 +1,78 @@
<!-- markdownlint-disable MD024 -->
# Capability Spec Delta: api-secrets-vault (api-secrets-vault)
**Package**: `@suaveplan/api-secrets-vault`
**Status**: PROPOSED
> Every requirement below carries a `**Verified by:**` line at authoring time.
## ADDED Requirements
### Requirement: Vault-backed adapter implements `secrets`
The package SHALL implement the abstract `secrets` capability over a `@suaveplan/vault` instance. The vault is passed in already unlocked; the adapter MUST NOT see the master key.
#### Scenario: get returns stored value
- WHEN a secret is set on the underlying vault AND `get(name)` is called
- THEN the adapter returns the value AND `api.secrets.reads_total{backend="vault"}` increments
**Verified by:** `src/adapter/adapter.test.ts::"get returns stored value AND increments reads_total"`
---
### Requirement: Locked vault surfaces explicitly
The package SHALL distinguish a locked vault (`SECRETS_VAULT_LOCKED`) from a missing entry (`SECRETS_VAULT_NOT_FOUND`).
#### Scenario: locked vault raises specific code
- WHEN the vault is locked at read time
- THEN reads raise `SECRETS_VAULT_LOCKED` AND no plaintext fallback occurs
**Verified by:** `src/adapter/adapter.test.ts::"locked vault raises SECRETS_VAULT_LOCKED"`
---
### Requirement: Per-plugin allowlist enforcement
The package SHALL refuse reads of secret names not in the plugin manifest's `secrets[]`. Refusals raise `SECRETS_VAULT_NOT_ALLOWED`.
#### Scenario: un-manifested read refused
- WHEN a plugin's manifest declares `secrets = [{ name: "STRIPE" }]` AND code calls `get("AWS")`
- THEN the call raises `SECRETS_VAULT_NOT_ALLOWED`
**Verified by:** `src/allowlist/allowlist.test.ts::"un-manifested read raises SECRETS_VAULT_NOT_ALLOWED"`
---
### Requirement: Vault updates surface as `secret.changed` events
The package SHALL subscribe to the vault's `secret.updated` event AND, when the updated secret is in the plugin's allowlist, emit `secret.changed` on the host event bus carrying `{ name }` only (no value).
#### Scenario: allowlisted update emits secret.changed
- WHEN the vault publishes `secret.updated` for a name in the allowlist
- THEN the host bus receives exactly one `secret.changed` event AND the payload contains only `{ name }`
#### Scenario: non-allowlisted update does not emit
- WHEN the vault publishes `secret.updated` for a name NOT in the allowlist
- THEN no `secret.changed` event is emitted on the host bus
**Verified by:** `src/watcher/watcher.test.ts::"allowlisted update emits secret.changed"` AND `src/watcher/watcher.test.ts::"non-allowlisted update does not emit"`
---
### Requirement: Passes `runSecretsConformance`
The package SHALL pass `runSecretsConformance` from `@suaveplan/api-secrets/testing`.
#### Scenario: conformance passes
- WHEN `runSecretsConformance("vault", makeVaultAdapter)` runs
- THEN every harness `it(...)` passes
**Verified by:** `src/adapter/adapter.spec-conformance.test.ts::"vault adapter passes runSecretsConformance"`

View File

@@ -0,0 +1,71 @@
# Tasks — `@suaveplan/api-secrets-vault`
## 0. Pre-flight (non-negotiable)
- [x] 0.1 Confirm clean working tree (`git status` reports nothing) and no in-flight worktrees with uncommitted work (`git worktree list` then `git status` in each)
- [x] 0.2 If any worktrees exist under `.claude/worktrees/`, account for each one: still-active agent, work-pending-merge, or stale-and-prune. Stale worktrees MUST be removed (`git worktree remove`) before opening a new one — code that has been lost twice on main is the failure mode this guards against.
- [x] 0.3 Confirm the package set in `MANIFEST.md` matches `find packages -maxdepth 3 -name package.json` count. Drift here means a prior change archived without regenerating the manifest.
## 1. Spec authoring
- [x] 1.1 Read design doc (§3, §5, §13.2)
- [x] 1.2 Read `openspec/AGENTS.md` Sections 13 and 14
- [x] 1.3 Draft `proposal.md`
- [x] 1.4 Draft `specs/api-secrets-vault/spec.md`
- [x] 1.5 Every requirement carries `**Verified by:**`
## 2. Implementation
- [x] 2.1 Scaffold `packages/api-stack/api/api-secrets-vault/`
- [x] 2.2 `src/adapter/` — `createVaultSecretsAdapter`
- [x] 2.3 `src/allowlist/` — per-plugin allowlist enforcement
- [x] 2.4 `src/watcher/` — vault `secret.updated` → bus `secret.changed`
- [x] 2.5 `src/error-codes/error-codes.ts` (feature-folder per CLAUDE.md "No flat modules at src/ root")
- [x] 2.6 `src/index.ts` — explicit named re-exports + side-effect `error-codes` import
- [x] 2.7 `src/from-vault/` — `fromVault(vault: Vault)` bridge to a real `@suaveplan/vault.Vault`
- [x] 2.8 `src/otel/` — `getSecretsVaultInstruments` + `api.secrets.reads_total{backend="vault"}` counter
- [x] 2.9 `src/types/` — structural `VaultSecretsBackend` contract
## 3. Tests
- [x] 3.1 `src/adapter/adapter.test.ts`
- [x] 3.2 `src/adapter/adapter.spec-conformance.test.ts` — passes `runSecretsConformance`
- [x] 3.3 `src/watcher/watcher.test.ts`
- [x] 3.4 `tests/dep-graph.test.ts`
- [x] 3.5 Every `it(...)` carries `{ timeout: 5000 }` (30000 for crypto-heavy unlock; 120000 for the shared `beforeAll`)
- [x] 3.6 `src/allowlist/allowlist.test.ts` — INVALID_OPTIONS / NOT_ALLOWED arms
- [x] 3.7 `src/error-codes/error-codes.test.ts` — registry shape + class identity
- [x] 3.8 `src/otel/instruments.test.ts` — counter emission
- [x] 3.9 `src/from-vault/from-vault.test.ts` — WRITE_FAILED error wrapping (structural shim around `vault.items`; integration suite drives the bridge against the real Vault)
- [x] 3.10 `tests/integration.test.ts` — REAL `@suaveplan/vault.Vault` end-to-end (shared `beforeAll` unlock + OWASP-min Argon2id params + separate locked-vault describe to isolate `zeroizeAll()`)
## 4. Documentation
- [x] 4.1 Sibling `.md` per implementation `.ts` (`docs:check` → 7/7 = 100%)
- [x] 4.2 `README.md` `## Modules`
- [x] 4.3 Update `CLAUDE.md` package listing (orchestrator)
## 5. Pre-archive verification (the 5-gate triple)
- [x] 5.1 `bun run check` — biome clean (no fixes applied on rerun)
- [x] 5.2 `bunx turbo lint typecheck test --filter=@suaveplan/api-secrets-vault` — green
- [x] 5.3 `bunx turbo test:coverage --filter=@suaveplan/api-secrets-vault` — **100/100/100/100** across adapter, allowlist, error-codes, from-vault, otel, watcher
- [x] 5.4 `bun scripts/openspec-conformance.ts packages/api-stack/api/api-secrets-vault` — **PASS (0 error, 0 warn, 0 info)**
- [x] 5.5 `bunx turbo build --filter=@suaveplan/api-secrets-vault` — vite build clean, dts emitted
## 6. Pre-archive 7-row checklist
- [x] 6.1 Generics threaded — `SuaveplanError<SecretsVaultCode>` parameterises throwables; no `unknown` leaks in public d.ts (conformance verified)
- [x] 6.2 Zod schemas wired at boundaries — `allowlist`, `adapter`, `watcher` all validate construction options with `safeParse` and raise `SECRETS_VAULT.INVALID_OPTIONS` on failure
- [x] 6.3 Every `SECRETS_VAULT.*` code thrown by a production path — NOT_FOUND, NOT_ALLOWED, VAULT_LOCKED, VAULT_UNAVAILABLE, WRITE_FAILED, INVALID_OPTIONS all asserted in tests against real throw sites
- [x] 6.4 Strict-mode: locked vault raises `VAULT_LOCKED` from BOTH `get` and `require`; no plaintext fallback (covered by `adapter.test.ts` + integration `locked-vault` describe)
- [x] 6.5 OTel `api.secrets.reads_total{backend="vault"}` asserted — `adapter.test.ts::"get returns stored value AND increments reads_total"` flushes the in-memory exporter and finds the metric
- [x] 6.6 Lifecycle dedup — adapter `dispose()` idempotent (3× call test); watcher subscription single (`listenerCount === 1` assertion); watcher dispose idempotent + survives lazy backend unsubscribes (in-listener disposed-flag guard)
- [x] 6.7 No retroactive tick — every box ticked above reflects evidence in this branch
## 7. Archive
- [x] 7.1 Fold spec delta into `openspec/specs/api-secrets-vault/spec.md`
- [x] 7.2 Move folder to archive
- [x] 7.3 Tick `8.14` in umbrella tasks
- [x] 7.4 Refresh `MANIFEST.md`