Compare commits

...
85 Commits
Author SHA1 Message Date
bsodfather 44774883a0 fix(epic3): выводить Tools-сигнал из клиентской проекции tools/list (Story 3.5, FR-14)
CI / UI Checks (pull_request) Successful in 8s
CI / Rust Checks (pull_request) Successful in 10m49s
CI / Rust Feature Validation (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Successful in 1s
CI / Deploy (pull_request) Skipped
Community Release / release (push) Successful in 25m35s
QA F1: CatalogFingerprint сравнивал только Tool-bindings, а tools/list
строится из catalog_tool_definitions, которая ещё зависит от
tool_selection_policy (mode, groups, search.max_results); в Search-режиме
отдаваемый список вообще не зависит от bindings. Из-за этого commit,
меняющий только политику, не порождал notifications/tools/list_changed
(клиент оставался со stale-каталогом), а binding-only commit в Search
порождал ложный сигнал.

Теперь Tools-часть отпечатка — канонизированные определения, которые
реально отдаёт tools/list (отсортированы, чтобы порядок чтения не был
ложным изменением): сигнал есть ровно тогда, когда меняется видимая
клиенту проекция.

ATDD: новый сценарий на обеих ревизиях — policy-only commit (Direct ->
Search) даёт ровно один Tools-кадр и авторитетный tools/list с
search_tools/call_tool, а последующий binding-only commit в Search-режиме
не сигналит. Юнит-оракулы переведены на реальную проекцию: policy-only
change, невидимое поле политики, version bump с идентичным определением и
binding change в Search-режиме — silent.
2026-10-26 17:22:07 +00:00
hermes-dev a0365bb80b feat(epic3): отправлять Tools catalog change signals после commit (Story 3.5, FR-14)
Клиент, согласовавший tools list-changed, получает bounded
`notifications/tools/list_changed` после коммита видимой Tool-проекции
опубликованного Agent snapshot; `tools/list` остаётся авторитетным для
клиента без notification и после reconnect.

- signals.rs: третий тип сигнала в существующем stream-local watcher
  (один tick = максимум один кадр на тип, fixed-capacity queue, без
  cross-process bus и без post-commit hook); fingerprint сравнивает
  tool_name/operation.version/title/description, поэтому Tool-only
  commit не даёт Resource/Prompt сигналов и наоборот.
- registry.rs: tools.listChanged=true (реально отправляется в обеих
  revision); modern filter toolsListChanged теперь подтверждается
  acknowledge и не шлётся вне согласованного набора.
- legacy GET-стрим несёт Tools-сигнал вместе с Resource/Prompt.
- ATDD: RED на baseline (ack пуст, Tools-кадра нет, capability false),
  GREEN — 8 integration-сценариев × обе revision, unit-оракулы wire,
  representatives official MCP SDK 1.32.2 smoke со штатными headers.
- capability inventory/baseline manifest и frozen projection обновлены
  под tools.listChanged=true; ci-story-checks.sh дополнен AC 3.5.
2026-10-26 17:22:07 +00:00
bsodfather 2499016dc7 fix(epic3): принимать поток progress при JSON-first Accept официального SDK (Story 3.4, FR-14)
CI / Rust Checks (pull_request) Successful in 9m12s
CI / Rust Feature Validation (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Successful in 1s
CI / Deploy (pull_request) Skipped
CI / UI Checks (pull_request) Successful in 10s
Community Release / release (push) Successful in 24m39s
Официальный MCP SDK перечисляет медиатипы в `Accept` JSON-first
(`application/json, text/event-stream`). Прежняя реализация выбирала
negotiated JSON-тело, и клиент, которому нужен progress, не получал
ни одного кадра.

Коррекция переиспользует уже провалидированные шаги: для `tools/call`
с допустимым progress token и предложенным event-stream режим ответа
переключается на SSE. Admission/авторизация не двигались; другой метод,
отсутствующий или непригодный token и JSON-only `Accept` сохраняют
negotiated режим.

ATDD: тот же JSON-first `Accept` даёт строго монотонные progress 1/2/3 и
прежний result в обеих supported revisions, а негативные контроли
(без token и с непригодным token) остаются на JSON-теле.
2026-10-26 16:03:48 +00:00
bsodfather f613250fd3 fix(epic3): сделать ATDD отмены чувствительным к регрессии и покрыть bounds токена в обеих revision (Story 3.4, FR-14)
- CANCEL_GRACE_MS 1200 -> 2500 (> LONG_HOLD_MS) и const-assert на этот порядок:
  при незавершённой отмене вызов успевает завершиться и записать исход внутри
  окна проверки, поэтому тест доказывает остановку, а не только «ещё не готово».
- malformed/oversized/empty/object/array token теперь проверяются и в modern
  revision, не только в legacy (обе читают одно поле RequestMetaObject).
- комментарий cancelled_response приведён к фактическому поведению: у
  остановленного вызова строки Invocation History нет вообще.
2026-10-26 13:07:14 +00:00
hermes_dev 862cde8559 feat(epic3): progress и безопасная отмена Tool calls (Story 3.4, FR-14)
- request-scoped lifecycle одного tools/call: валидированный `_meta.progressToken`
  (строковый/числовой, 1..=128 байт) и bounded очередь реальных этапов
  (tool_resolved → authorization_resolved → execution_dispatched) в собственный
  SSE-stream запроса; без token или без stream-carrier результат не меняется;
- сериализация `notifications/progress` по pinned схеме обеих revisions
  (progressToken/progress/message, total не изобретается);
- отмена по revision: 2025-11-25 — `notifications/cancelled` через bounded
  in-flight таблицу (владение = session + platform credential, не progress token);
  2026-07-28 — закрытие response stream; в обоих случаях drop будущего вызова =
  кооперативная остановка, terminal-сообщение не отправляется;
- streamed ответ сохраняет проекцию Story 3.18: closed `notifications/message`
  перед финальным кадром (перенос logging-scope в тело стрима);
- ATDD: 12 end-to-end тестов на реальном endpoint/каталоге/counted upstream
  (обе revisions, malformed/oversized/reused token, owner/foreign/late cancel,
  disconnect, ambiguous outbound, representative client smoke) + 7 unit-тестов
  границ lifecycle; entry в scripts/ci-story-checks.sh.
2026-10-26 12:02:11 +00:00
bsodfather 49c8af24ed fix(epic3): обновить checksum inventory в capability baseline (Story 3.19)
CI / Rust Checks (pull_request) Successful in 8m5s
CI / Rust Feature Validation (pull_request) Skipped
CI / UI Checks (pull_request) Successful in 31m50s
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Successful in 1s
CI / Deploy (pull_request) Skipped
Community Release / release (push) Successful in 22m34s
Story 3.19 меняет `docs/capability-inventory.json` (статусы `listChanged`,
`subscriptions/listen`, `notifications/*/list_changed`), поэтому
`docs/capability-baseline/manifest.json` должен ссылаться на новый SHA-256 того
же артефакта: `85970569…` → `10ca3538aa1b6b25868f80e19932c7ef918398c4817c52bc1aae52f18c31522b`,
ровно как в `tests/fixtures/mcp-revisions/*.provenance.json` этого же delta.

Поймано Required CI на `a965bc4` (run 887, job 2491): шаг
`Check Capability Baseline` падал с `CHECKSUM_MISMATCH
pointer=/artifacts/0/sha256`; остальные шаги того же job'а (tooling unit tests
305 OK, capability inventory `status=valid protocol_items=50`, community scope)
были зелёными. Локально после правки:
`validate-capability-baseline.py` → `baseline_version=2026.08.24.1 flows=35`,
`validate-capability-inventory.py` → `status=valid`, community scope (1263
файла), runtime-config/config/metrics/rust boundaries и code health — 0.

Содержательных изменений продукта здесь нет: только checksum-указатель на уже
изменённый в предыдущем коммите артефакт.
2026-10-25 21:08:20 +00:00
bsodfather a965bc4137 feat(epic3): уведомления об изменении Resource/Prompt каталога MCP (Story 3.19)
CI / UI Checks (pull_request) Successful in 9s
CI / Rust Checks (pull_request) Failing after 4m10s
CI / Rust Feature Validation (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Failing after 1s
CI / Deploy (pull_request) Skipped
Story 3.19 / FR-19 на принятой базе Story 3.18 (`47a6e54`, PR #99): после
commit опубликованного каталога Agent клиент получает
`notifications/resources/list_changed` и/или
`notifications/prompts/list_changed`, обе pinned revisions, а обычное чтение
каталога остаётся авторитетным при потере сигнала. Scope ограничен
list-change: `resources/subscribe`/`unsubscribe` остаются `-32601`, значение
`resources.subscribe` остаётся `false` (одобренный владельцем вариант A).

Публикация переносит только semantic delta Story 3.19 из
`/home/codex/code/crank-worktrees/story31-worktree`
(`1b6db09` + cumulative overlay) на принятый head 3.18, а не копирует
worktree:
- новый `protocol/signals.rs` (watcher, отпечаток каталога, bounded filter,
  revision-correct encoders, bounded legacy stream registry) и
  `protocol/signals_tests.rs` (17 unit oracles);
- `subscriptions/listen` (modern-only dispatch registration) + честные
  `listChanged: true` объявления; `app/endpoint.rs` получает legacy GET
  producer/DELETE retire, `app/dispatch.rs` — modern listen stream;
- ATDD `mcp_signals` 5 сценариев на реальном endpoint/PostgreSQL/artifact
  store; capability inventory и frozen/provenance проекции обновлены
  (`b90b59f3…`, inventory `10ca3538…`);
- `scripts/ci-story-checks.sh`: добавлены 3.19-проверки, записи Story 3.18
  сохранены (гейт не ослаблен).

Принятая правка code-health `206ca78` сохранена: `app/endpoint.rs` 402 строки
(было 381), `app/legacy_dispatch.rs` 731 без изменений, т.е. split не
переписан. Новый ATDD-файл `mcp_signals.rs` до 1138 строк превышал лимит
`scripts/check-rust-code-health.sh`, поэтому его сценарии механически
вынесены в `mcp_signals/scenarios.rs` без изменения проверок.

Наследованный дефект tooling-гейта: `tests/unit/test_validate_capability_inventory.py`
требует, чтобы `Run tooling unit tests` шёл раньше
`scripts/validate-capability-inventory.py`; в принятом `ci.yml` (3.18) шаг
оказался в условном feature-job. Шаг перенесён в обязательный `rust` job перед
проверкой inventory — исполняемый tooling-гейт теперь проверяется там же, где
применяется; ни одна проверка не отключена и не ослаблена.

Проверки на этом коммите: `ci-story-checks.sh` exit 0 — 4/9/1/12/10/3/5(+1
manual ignored)/17 passed; `crank-community-mcp --lib` 102/0; transport 20/0,
mcp_prompts 15/0, revision_matrix::negotiation 23/0, era_isolation 5/0;
`cargo fmt --check` и `cargo clippy -p crank-community-mcp -p mcp-server
--all-targets -D warnings` — 0; `check-rust-code-health.sh` — pass;
capability inventory `status=valid protocol_items=50 revisions=2`;
`python3 -m unittest discover -s tests/unit` — 305 OK.

NOT_RUN (честно, не PASS): полный `just verify`, межкомпонентная интеграция,
нагрузка и квалификация ≥3 реальных клиентов — остаются на собранной фиче 3.20
по validation-policy.md.
2026-10-25 21:00:51 +00:00
hermes-dev 47a6e54660 fix(ui): bump nginx runtime base to 1.30.6 for the image scan gate
CI / Rust Checks (pull_request) Successful in 6m33s
CI / Rust Feature Validation (pull_request) Skipped
CI / UI Checks (pull_request) Successful in 16m36s
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Successful in 1s
CI / Deploy (pull_request) Skipped
Community Release / release (push) Failing after 16m35s
2026-10-25 16:19:11 +00:00
bsodfather 206ca784f7 fix(epic3): уложить endpoint.rs Story 3.18 в лимит 1000 строк
CI / UI Checks (pull_request) Successful in 11s
CI / Rust Checks (pull_request) Successful in 9m2s
CI / Rust Feature Validation (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Successful in 1s
CI / Deploy (pull_request) Skipped
The Story 3.18 logging arm pushed `crates/crank-community-mcp/src/app/endpoint.rs`
to 1058 lines, so `scripts/check-rust-code-health.sh` failed the Required CI
(Rust Checks, run 870 / job 2408) at the pinned 1000-line default.

Move the whole legacy-era dispatch of `mcp_post_response` verbatim into
`app/legacy_dispatch.rs` (`handle_legacy_request`), leaving the endpoint as the
transport/admission layer it documents itself to be. endpoint.rs is now 381
lines and the new module 731, both inside the limit; no baseline pin and no
check was touched.

Behaviour is unchanged: the moved match keeps every arm, envelope, status code
and safe detail as-is (legacy-only logging/setLevel, -32602 INVALID_LEVEL,
EVENT_LEVEL_APPLIED Notice acknowledgment, per-session level isolation), the
pre-dispatch legacy level read stays in `mcp_post_response`, and the only edits
to the moved text are the mechanical `&credential` -> `credential` (the
credential now arrives as a reference) and `protocol_version` already being
`Option<&str>`.
2026-10-25 15:45:01 +00:00
bsodfather a438c1df42 feat(epic3): безопасное structured logging MCP (Story 3.18)
CI / UI Checks (pull_request) Successful in 11s
CI / Rust Checks (pull_request) Failing after 4m11s
CI / Rust Feature Validation (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Failing after 1s
CI / Deploy (pull_request) Skipped
Story 3.18 (#25) surface: клиент получает только закрытые structured
`notifications/message` выбранного уровня, без запросов, секретов и
internal causes.

- `logging` capability advertising в обеих поддерживаемых revisions;
  `logging/setLevel` — legacy-only dispatch (modern — `_meta` per request);
- закрытый словарь LoggingLevel (RFC 5424), malformed → -32602 с одним
  safe detail; закрытый набор safe events, task-local request scope;
  bounded in-memory opt-in, без миграций;
- `notifications/message` идут перед ответом в SSE; JSON-режим их не несёт;
- ATDD в `scripts/ci-story-checks.sh`: mcp_logging (10), inventory projection (3),
  protocol::logging unit (12);
- проекция capability inventory/frozen oracle переведена на `logging`.

Story ATDD и affected проверки пройдены на этом дереве (fmt, clippy,
python oracle 56 OK, capability inventory valid). Полный `just verify` —
NOT_RUN на story по validation-policy, остаётся на собранной фиче.
2026-10-25 12:50:00 +00:00
bsodfather aed83268a0 fix(release): publish assets to the public HTTPS Gitea origin
CI / UI Checks (pull_request) Successful in 8s
CI / Rust Checks (pull_request) Successful in 6m53s
CI / Rust Feature Validation (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Successful in 1s
CI / Deploy (pull_request) Skipped
Community Release / release (push) Failing after 38m13s
The release job container sees gitea.server_url as the internal
http://gitea:3000 origin, so scripts/ci-release-assets.py refused to
forward its token (run 865 failed at the publish step after packaging).
Point the publisher at https://git.itexp.me, the origin the repository
already uses for the public URL.
2026-10-25 11:37:27 +00:00
Hermes DevOps 12d8a3d490 fix(test): isolate Valkey in the CI job network and bound readiness
CI / UI Checks (pull_request) Successful in 8s
CI / Rust Checks (pull_request) Successful in 6m1s
CI / Rust Feature Validation (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Successful in 1s
CI / Deploy (pull_request) Skipped
Community Release / release (push) Failing after 18m56s
2026-10-25 10:59:18 +00:00
bsodfather e57ab47c05 ci: run the Story 3.16 composite-prompt ATDD in the story gate
CI / UI Checks (pull_request) Successful in 8s
CI / Rust Checks (pull_request) Canceled after 19m49s
CI / Rust Feature Validation (pull_request) Canceled after 0s
CI / Frontend E2E (pull_request) Canceled after 0s
CI / Community Image Smoke (pull_request) Canceled after 0s
CI / Deploy (pull_request) Canceled after 0s
CI / Required Checks (pull_request) Failing after 1s
The recovery branch publishes Story 3.16 together with Story 3.17, so the targeted story gate
must exercise both accepted user outcomes: composite Prompts with published Resources and the
bounded completion/resources-list admission of 3.17.
2026-10-25 10:32:28 +00:00
Hermes DevOps 1baeefe889 ci: validate accepted stories with targeted ATDD and gate feature checks
CI / UI Checks (pull_request) Successful in 10s
CI / Rust Checks (pull_request) Failing after 25m35s
CI / Rust Feature Validation (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Failing after 1s
CI / Deploy (pull_request) Skipped
2026-10-25 10:18:09 +00:00
bsodfather 9567bbf3aa fix(ci): pass the community scope guard and publish release artifacts before registry steps
CI / Rust Checks (pull_request) Canceled after 38m52s
CI / UI Checks (pull_request) Canceled after 0s
Community Release / release (push) Canceled after 38m39s
CI / Frontend E2E (pull_request) Canceled after 0s
CI / Community Image Smoke (pull_request) Canceled after 0s
CI / Deploy (pull_request) Canceled after 0s
- the community scope guard rejected the innocuous English comment 'One timed
  resources/list' in the Story 3.8 discovery diagnostic (its 'one-time-token'
  marker matches any 'one time' phrase). Reword the comment; no behaviour change.
- move the release artifact publication ahead of the registry/OpenBao steps, so a
  credential problem cannot leave the archives, checksums and SBOM unpublished.
2026-10-25 09:27:55 +00:00
bsodfather 54ce770682 feat(epic3): deliver the accepted Epic 3 scope through Story 3.17
CI / Rust Checks (pull_request) Failing after 13m41s
CI / UI Checks (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Deploy (pull_request) Skipped
Community Release / release (push) Failing after 13m33s
Recovery delivery of the accumulated, TL-accepted Epic 3 work that had never been
committed to Git (stories 3.6-3.17, see the crank board). Story 3.18 is not accepted
yet and is deliberately excluded: its logging surface, its capability registration,
its oracle projections and its tests are not part of this tree.

Accepted scope
- 3.6 typed static Resource Drafts; 3.7 publication and exact binding of static
  Resource Versions; 3.8 MCP discovery/read and revocation of static Resources;
- 3.9 Resource import/export in API and UI plus TLS dependency maintenance;
  3.10 REST-backed Resource templates; 3.11 shared REST read runtime and the MCP
  revision/capability foundation;
- 3.12 parameterised Prompt Drafts with preview; 3.13 Prompt publication and exact
  version binding to Agents; 3.14 MCP prompts; 3.15 Prompt import/export;
- 3.16 composite Prompts with published Resources (dependency content scan plus
  dynamic read through the accepted 3.11 runtime, admin preview and MCP prompts/get);
- 3.17 bounded MCP completion suggestions from typed published schemas and the
  resources/list admission fix for bound template publications.

Accepted evidence being reused (unchanged code paths)
- 3.16 candidate: core 7, admin-api 22, registry 4, mcp-server 4, UI 6; one final
  `just verify` run 1651 passed / 0 failed / 2 ignored.
- 3.17 candidate: full run 1672 passed / 0 failed / 2 ignored, QA targeted reruns and
  a real MCP Inspector 2.12.0 client smoke.

Delivery engineering in this commit
- CI: one event per change (feature branches are gated by their pull request, push
  runs only on main), story events run the changed packages, the full workspace suite
  stays the feature/release gate; no cache/sharding without a measured runner check.
- Release: the packaged archives, checksums and SBOM are now uploaded to the Gitea
  release entry of the tag instead of dying with the runner workspace.

Not included on purpose: Story 3.18 work, build caches, runtime databases, local
credentials and scratch evidence.

Refs: crank kanban t_e39d7263; baseline 1b6db09bd1.
2026-10-25 09:08:22 +00:00
bsodfather 1b6db09bd1 fix(import): единая идентичность внешних URI (#96)
CI / Rust Checks (push) Successful in 25m54s
CI / UI Checks (push) Successful in 5s
CI / Community Image Smoke (push) Successful in 1m47s
CI / Frontend E2E (push) Successful in 10m12s
CI / Deploy (push) Successful in 1m22s
Единая transport-canonical идентичность внешних документов; normalizer v6 с immutable historical replay; matrix 1.0.4 / corpus 1.0.5. PR CI run840 success, локальная приёмка и independent review выполнены.

Co-authored-by: bsodfather <bsodfather@gmail.com>
2026-10-11 06:19:16 +00:00
bsodfather 178093e81b Импорт OpenAPI: закрыть пробелы проверок второго этапа (#95)
CI / Rust Checks (push) Successful in 25m23s
CI / UI Checks (push) Successful in 6s
CI / Community Image Smoke (push) Successful in 2m0s
CI / Frontend E2E (push) Successful in 9m12s
CI / Deploy (push) Successful in 1m22s
2026-10-10 11:54:58 +00:00
bsodfather ad23cd9a46 fix(import): подтвердить обязательные гарантии второго эпика
CI / Rust Checks (pull_request) Successful in 24m43s
CI / UI Checks (pull_request) Successful in 6s
CI / Community Image Smoke (pull_request) Successful in 9m5s
CI / Frontend E2E (pull_request) Successful in 12m43s
CI / Deploy (pull_request) Skipped
2026-10-10 13:25:05 +03:00
bsodfather 31f6f00d3b Безопасность: сократить серверные образы и проверять все уязвимости (#93)
CI / Rust Checks (push) Successful in 22m49s
CI / UI Checks (push) Successful in 6s
CI / Community Image Smoke (push) Successful in 1m54s
CI / Frontend E2E (push) Successful in 6m59s
CI / Deploy (push) Successful in 1m41s
2026-10-09 23:15:05 +00:00
bsodfather f685616ae8 fix(security): minimize runtime images and enforce complete vulnerability scans
CI / Rust Checks (pull_request) Successful in 23m1s
CI / UI Checks (pull_request) Successful in 6s
CI / Community Image Smoke (pull_request) Successful in 7m28s
CI / Frontend E2E (pull_request) Successful in 9m33s
CI / Deploy (pull_request) Skipped
2026-10-10 01:41:38 +03:00
bsodfather 0c1fb38e31 Merge pull request 'Завершить импорт OpenAPI и пошаговый первый запуск (2-15)' (#90) from fix/2-15-verify-and-deliver-20261005 into main
CI / Rust Checks (push) Successful in 22m50s
CI / UI Checks (push) Successful in 6s
CI / Community Image Smoke (push) Successful in 2m25s
CI / Frontend E2E (push) Successful in 7m52s
CI / Deploy (push) Successful in 1m39s
2026-10-09 17:09:54 +00:00
bsodfather 249b777bec Исправить перекрытие навигации и обновить пакеты образов
CI / Rust Checks (pull_request) Successful in 22m46s
CI / UI Checks (pull_request) Successful in 6s
CI / Frontend E2E (pull_request) Successful in 11m48s
CI / Community Image Smoke (pull_request) Successful in 13m42s
CI / Deploy (pull_request) Skipped
2026-10-09 19:13:02 +03:00
bsodfather f48754aaf5 fix(ci): use a pinned Gitea compatible artifact action
CI / Rust Checks (pull_request) Successful in 22m57s
CI / UI Checks (pull_request) Successful in 6s
CI / Frontend E2E (pull_request) Successful in 12m27s
CI / Community Image Smoke (pull_request) Failing after 16m23s
CI / Deploy (pull_request) Skipped
2026-10-09 17:53:47 +03:00
bsodfather e93de7015a feat(import): complete OpenAPI onboarding and verify delivery
CI / Rust Checks (pull_request) Failing after 1s
CI / UI Checks (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Deploy (pull_request) Skipped
2026-10-09 17:50:38 +03:00
bsodfather bb3e376767 Исправить уязвимость образов и восстановить развёртывание Crank (#85)
CI / Rust Checks (push) Successful in 15m29s
CI / UI Checks (push) Successful in 6s
CI / Community Image Smoke (push) Successful in 1m51s
CI / Frontend E2E (push) Successful in 5m10s
CI / Deploy (push) Successful in 1m38s
2026-10-05 05:14:42 +00:00
bsodfather 3f4ea248c7 fix(deploy): update vulnerable pcre2 in UI image
CI / Rust Checks (pull_request) Successful in 15m33s
CI / UI Checks (pull_request) Successful in 6s
CI / Community Image Smoke (pull_request) Successful in 7m18s
CI / Frontend E2E (pull_request) Successful in 7m53s
CI / Deploy (pull_request) Skipped
2026-10-05 07:47:51 +03:00
bsodfather 7c9bc079da fix(deploy): update vulnerable Debian library before image scan
CI / Rust Checks (pull_request) Successful in 16m40s
CI / UI Checks (pull_request) Successful in 6s
CI / Frontend E2E (pull_request) Successful in 9m3s
CI / Community Image Smoke (pull_request) Failing after 11m0s
CI / Deploy (pull_request) Skipped
2026-10-05 07:15:21 +03:00
bsodfather c7cd4d8ef2 Исправления эпика 1 после ретроспективы (#83)
CI / Rust Checks (push) Successful in 15m25s
CI / UI Checks (push) Successful in 5s
CI / Community Image Smoke (push) Successful in 1m19s
CI / Frontend E2E (push) Successful in 5m7s
CI / Deploy (push) Failing after 26s
2026-10-04 23:38:00 +00:00
bsodfather eacfab0dd8 test: stabilize browser session setup and OpenAPI assertions
CI / Rust Checks (pull_request) Successful in 15m33s
CI / UI Checks (pull_request) Successful in 5s
CI / Community Image Smoke (pull_request) Successful in 6m28s
CI / Frontend E2E (pull_request) Successful in 7m27s
CI / Deploy (pull_request) Skipped
2026-10-05 02:13:18 +03:00
bsodfather ca32d3767f docs: annotate onboarding key disclosure test in archived report
CI / Rust Checks (pull_request) Successful in 16m32s
CI / UI Checks (pull_request) Successful in 6s
CI / Frontend E2E (pull_request) Failing after 10m8s
CI / Community Image Smoke (pull_request) Successful in 15m33s
CI / Deploy (pull_request) Skipped
2026-10-05 01:06:28 +03:00
bsodfather e959f094e3 docs: normalize archived verification output whitespace
CI / Rust Checks (pull_request) Failing after 5m28s
CI / UI Checks (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Deploy (pull_request) Skipped
2026-10-05 00:58:53 +03:00
bsodfather a36f3e2181 docs: preserve isolated epic 1 PR verification evidence
CI / Rust Checks (pull_request) Canceled after 2m31s
CI / UI Checks (pull_request) Canceled after 0s
CI / Frontend E2E (pull_request) Canceled after 0s
CI / Community Image Smoke (pull_request) Canceled after 0s
CI / Deploy (pull_request) Canceled after 0s
2026-10-05 00:56:56 +03:00
bsodfather fa320c34cb fix: copy vendored SQLx into Docker build stages
CI / Rust Checks (pull_request) Canceled after 15m29s
CI / UI Checks (pull_request) Canceled after 0s
CI / Frontend E2E (pull_request) Canceled after 0s
CI / Community Image Smoke (pull_request) Canceled after 0s
CI / Deploy (pull_request) Canceled after 0s
2026-10-05 00:19:54 +03:00
bsodfather 18e27f3994 docs(evidence): normalize sanitized diagnostic whitespace
CI / Rust Checks (pull_request) Failing after 16m45s
CI / UI Checks (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Deploy (pull_request) Skipped
2026-10-05 00:01:04 +03:00
bsodfather ce5b9af5ab docs(epic-1): record verified retrospective completion evidence 2026-10-05 00:01:04 +03:00
bsodfather c6805e2345 test(ui): preserve auto-opened onboarding panel in recovery check 2026-10-05 00:01:04 +03:00
bsodfather 45f3cabe44 fix(tooling): exclude upstream SQLx from Community marker scan 2026-10-04 23:57:40 +03:00
bsodfather 284eb8180e fix(deps): bound SQLx pool maintenance and reserve idle counters 2026-10-04 23:57:40 +03:00
bsodfather 7aa854de26 fix(epic-1): repair atomic agent edits and dispatch certainty 2026-10-04 23:57:40 +03:00
bsodfather 82b215dd74 Merge pull request 'Bmad loop/20260927 234647 43b3/2 10 security schemes secrets' (#5) from bmad-loop/20260927-234647-43b3/2-10-security-schemes-secrets into main
CI / Rust Checks (push) Successful in 22m6s
CI / UI Checks (push) Successful in 11s
CI / Frontend E2E (push) Failing after 9m34s
CI / Community Image Smoke (push) Successful in 16m5s
CI / Deploy (push) Has been skipped
Reviewed-on: #5
2026-09-27 21:25:01 +00:00
bsodfather 028d8183c3 docs(story): complete 2.10 auto run result
CI / Rust Checks (pull_request) Successful in 22m34s
CI / UI Checks (pull_request) Successful in 8s
CI / Frontend E2E (pull_request) Failing after 23s
CI / Community Image Smoke (pull_request) Successful in 16m20s
CI / Deploy (pull_request) Has been skipped
2026-09-27 23:45:07 +03:00
bsodfather 41a0e46110 feat(import): bind OpenAPI security schemes safely 2026-09-27 23:42:11 +03:00
bsodfather c562d5368d docs: resolve secret lifecycle scope for story 2.10 2026-09-13 11:58:16 +03:00
bsodfather f8ee7c9bfe feat(import): preserve supported HTTP contracts 2026-09-13 03:30:31 +03:00
bsodfather 083974c27f fix(smoke): report sanitized operation failure diagnostics
CI / Rust Checks (push) Successful in 14m15s
CI / UI Checks (push) Successful in 5s
CI / Community Image Smoke (push) Successful in 1m15s
CI / Frontend E2E (push) Successful in 4m54s
CI / Deploy (push) Successful in 1m18s
2026-09-05 14:00:39 +03:00
bsodfather bbb9394911 fix(migrations): normalize legacy column order
CI / Rust Checks (push) Successful in 14m21s
CI / UI Checks (push) Successful in 6s
CI / Community Image Smoke (push) Successful in 6m14s
CI / Frontend E2E (push) Successful in 7m10s
CI / Deploy (push) Successful in 1m12s
2026-09-02 01:01:58 +03:00
bsodfather 2759fde81f fix(migrations): adopt published ledgerless baseline
CI / Rust Checks (push) Successful in 13m55s
CI / UI Checks (push) Successful in 5s
CI / Community Image Smoke (push) Successful in 6m14s
CI / Frontend E2E (push) Successful in 7m11s
CI / Deploy (push) Failing after 44s
2026-09-01 23:01:33 +03:00
bsodfather 527efb510f fix(deploy): distinguish core migration drift
CI / Rust Checks (push) Successful in 12m35s
CI / UI Checks (push) Successful in 6s
CI / Community Image Smoke (push) Successful in 6m14s
CI / Frontend E2E (push) Successful in 7m6s
CI / Deploy (push) Failing after 40s
2026-09-01 20:30:09 +03:00
bsodfather 66b9bfd875 fix(ci): scope OpenAPI evidence to required tests
CI / Rust Checks (push) Successful in 12m40s
CI / UI Checks (push) Successful in 16m21s
CI / Community Image Smoke (push) Successful in 1m15s
CI / Frontend E2E (push) Successful in 5m8s
CI / Deploy (push) Failing after 46s
2026-09-01 12:59:04 +03:00
bsodfather 6c5d0c62d0 fix(deploy): back up artifacts after failed rollout
CI / Rust Checks (push) Successful in 12m34s
CI / UI Checks (push) Successful in 6s
CI / Community Image Smoke (push) Successful in 1m12s
CI / Frontend E2E (push) Failing after 5m42s
CI / Deploy (push) Has been skipped
2026-09-01 01:38:15 +03:00
bsodfather dd5dbba1f6 fix(deploy): preserve rollback diagnostics
CI / Rust Checks (push) Successful in 12m38s
CI / UI Checks (push) Successful in 6s
CI / Community Image Smoke (push) Successful in 1m12s
CI / Frontend E2E (push) Successful in 4m55s
CI / Deploy (push) Failing after 31s
2026-09-01 00:13:27 +03:00
bsodfather 5818255cf5 fix(deploy): skip bundled postgres for external databases
CI / Rust Checks (push) Successful in 12m39s
CI / UI Checks (push) Successful in 5s
CI / Community Image Smoke (push) Successful in 1m11s
CI / Frontend E2E (push) Successful in 4m53s
CI / Deploy (push) Failing after 46s
2026-08-31 23:33:48 +03:00
bsodfather 343bb99bff fix(security): refresh community UI runtime
CI / Rust Checks (push) Successful in 12m46s
CI / UI Checks (push) Successful in 5s
CI / Community Image Smoke (push) Successful in 6m17s
CI / Frontend E2E (push) Successful in 7m12s
CI / Deploy (push) Failing after 1m10s
2026-08-31 23:06:16 +03:00
bsodfather dc8171d052 fix(ci): align OpenAPI evidence test title
CI / Rust Checks (push) Successful in 12m33s
CI / UI Checks (push) Successful in 6s
CI / Community Image Smoke (push) Successful in 1m11s
CI / Frontend E2E (push) Successful in 4m52s
CI / Deploy (push) Failing after 28s
2026-08-31 22:42:20 +03:00
bsodfather d92f817e73 fix(ci): harden community image smoke
CI / Rust Checks (push) Successful in 12m30s
CI / UI Checks (push) Successful in 5s
CI / Frontend E2E (push) Failing after 9m30s
CI / Community Image Smoke (push) Successful in 11m18s
CI / Deploy (push) Has been skipped
2026-08-30 22:22:13 +03:00
bsodfather ad8390e297 fix(ui): keep onboarding clear of wizard actions 2026-08-30 22:22:05 +03:00
bsodfather 466659c506 fix(ci): isolate e2e orchestration environment
CI / Rust Checks (push) Successful in 12m35s
CI / UI Checks (push) Successful in 6s
CI / Frontend E2E (push) Failing after 17m19s
CI / Community Image Smoke (push) Failing after 3h13m33s
CI / Deploy (push) Has been skipped
2026-08-30 13:13:35 +03:00
bsodfather 8375c7bbf0 test(admin): disable ANSI in telemetry capture
CI / Rust Checks (push) Successful in 12m30s
CI / UI Checks (push) Successful in 5s
CI / Frontend E2E (push) Failing after 15m39s
CI / Community Image Smoke (push) Failing after 3h13m56s
CI / Deploy (push) Has been skipped
2026-08-30 12:03:14 +03:00
bsodfather 2ccaba897d test(admin): stabilize telemetry outcome assertions
CI / Rust Checks (push) Failing after 9m31s
CI / UI Checks (push) Has been skipped
CI / Frontend E2E (push) Has been skipped
CI / Community Image Smoke (push) Has been skipped
CI / Deploy (push) Has been skipped
2026-08-30 01:49:00 +03:00
bsodfather 619394f509 test(admin): stabilize telemetry failure fixture
CI / Rust Checks (push) Failing after 9m33s
CI / UI Checks (push) Has been skipped
CI / Frontend E2E (push) Has been skipped
CI / Community Image Smoke (push) Has been skipped
CI / Deploy (push) Has been skipped
2026-08-30 01:28:48 +03:00
bsodfather 501e0931c5 fix(observability): preserve inbound trace context
CI / Rust Checks (push) Failing after 9m30s
CI / UI Checks (push) Has been skipped
CI / Frontend E2E (push) Has been skipped
CI / Community Image Smoke (push) Has been skipped
CI / Deploy (push) Has been skipped
2026-08-30 00:43:46 +03:00
bsodfather c3188637b3 refactor(artifacts): split oversized modules
CI / Rust Checks (push) Failing after 9m29s
CI / UI Checks (push) Has been skipped
CI / Frontend E2E (push) Has been skipped
CI / Community Image Smoke (push) Has been skipped
CI / Deploy (push) Has been skipped
2026-08-30 00:15:07 +03:00
bsodfather 32cde544da fix(scope): use workspace terminology
CI / Rust Checks (push) Failing after 3m23s
CI / UI Checks (push) Has been skipped
CI / Frontend E2E (push) Has been skipped
CI / Community Image Smoke (push) Has been skipped
CI / Deploy (push) Has been skipped
2026-08-29 22:14:25 +03:00
bsodfather 10641faf43 fix(migrations): publish canonical v13 contract
CI / Rust Checks (push) Failing after 3m20s
CI / UI Checks (push) Has been skipped
CI / Frontend E2E (push) Has been skipped
CI / Community Image Smoke (push) Has been skipped
CI / Deploy (push) Has been skipped
2026-08-29 21:34:23 +03:00
bsodfather c2556ec236 fix(docker): use HTTPS Debian package mirrors
CI / Rust Checks (push) Failing after 3m12s
CI / UI Checks (push) Has been skipped
CI / Frontend E2E (push) Has been skipped
CI / Community Image Smoke (push) Has been skipped
CI / Deploy (push) Has been skipped
2026-08-29 21:10:00 +03:00
bsodfather 55209a9bbc feat(import): resolve references and schema composition 2026-08-29 08:54:26 +03:00
bsodfather 6c2a3712d8 feat(import): add deterministic OpenAPI normalized IR
CI / Rust Checks (push) Failing after 3m12s
CI / UI Checks (push) Has been skipped
CI / Frontend E2E (push) Has been skipped
CI / Community Image Smoke (push) Has been skipped
CI / Deploy (push) Has been skipped
2026-08-29 04:15:37 +03:00
bsodfather bc03c33387 fix(openapi): harden story 2.1 production lifecycle
CI / Rust Checks (push) Failing after 4m6s
CI / UI Checks (push) Has been skipped
CI / Frontend E2E (push) Has been skipped
CI / Community Image Smoke (push) Has been skipped
CI / Deploy (push) Has been skipped
2026-08-29 00:48:22 +03:00
bsodfather 2c94af6791 feat(openapi): complete upload preview and UI evidence 2026-08-28 15:38:14 +03:00
bsodfather d2849ea3fe feat(artifacts): add fenced reconciliation recovery 2026-08-28 00:44:27 +03:00
bsodfather 8784964fb2 fix(artifacts): harden reconciliation scanner 2026-08-27 12:48:49 +03:00
bsodfather 889b1bdb57 feat(artifacts): add bounded reconciliation 2026-08-27 07:04:22 +03:00
bsodfather 38ba898b72 merge: complete story 2.1b artifact source metadata 2026-08-27 02:18:16 +03:00
bsodfather 2eb185b14a feat(registry): add workspace-scoped artifact metadata 2026-08-27 02:18:09 +03:00
bsodfather 497e1b740f feat(artifacts): add immutable artifact store 2026-08-25 18:06:33 +03:00
bsodfather 182bde8ac0 feat: complete Epic 1 production foundation 2026-08-25 01:24:11 +03:00
bsodfather 767428436d docs: freeze story 1.6 review evidence 2026-08-14 13:59:30 +03:00
bsodfather b7face0e94 fix: harden typed metrics review findings 2026-08-14 13:50:04 +03:00
bsodfather 996a5461de docs: refresh metrics evidence provenance 2026-08-14 12:27:30 +03:00
bsodfather 5fa5f6efe4 test: measure metrics allocation overhead 2026-08-14 12:14:05 +03:00
bsodfather 62d98a8846 docs: freeze metrics foundation evidence 2026-08-14 12:12:11 +03:00
bsodfather 8e709acea9 feat: freeze typed metrics registry and bounded exemplars 2026-08-14 11:52:00 +03:00
bsodfather 17c8e3a8f0 docs: freeze capability evidence for story 1.5 2026-08-14 00:39:12 +03:00
bsodfather f6fc2e5c9b feat: harden community production foundation through story 1.5 2026-08-14 00:21:59 +03:00
1112 changed files with 308355 additions and 11188 deletions

No files matched your search

+42 -38
View File
@@ -1,46 +1,41 @@
POSTGRES_DB=crank
POSTGRES_USER=crank
POSTGRES_PASSWORD=change-me
# Deployment-only image and publication settings.
CRANK_ADMIN_API_IMAGE=crank/admin-api:dev
CRANK_MCP_SERVER_IMAGE=crank/mcp-server:dev
CRANK_UI_IMAGE=crank/ui:dev
CRANK_PUBLISH_BIND=127.0.0.1
# BEGIN GENERATED CRANK RUNTIME CONFIG
CRANK_DATABASE_URL=
POSTGRES_HOST=postgres
POSTGRES_PORT=5432
POSTGRES_DB=crank
POSTGRES_USER=crank
POSTGRES_PASSWORD=
POSTGRES_MAX_CONNECTIONS=20
POSTGRES_MIN_CONNECTIONS=2
POSTGRES_ACQUIRE_TIMEOUT_MS=5000
POSTGRES_IDLE_TIMEOUT_MS=600000
POSTGRES_MAX_LIFETIME_MS=1800000
CRANK_ADMIN_API_IMAGE=crank/admin-api:dev
CRANK_MCP_SERVER_IMAGE=crank/mcp-server:dev
CRANK_UI_IMAGE=crank/ui:dev
CRANK_STORAGE_ROOT=/var/lib/crank/storage
CRANK_PUBLISH_BIND=127.0.0.1
CRANK_ADMIN_BIND=0.0.0.0:3001
CRANK_ADMIN_RATE_LIMIT_RPS=30
CRANK_ADMIN_RATE_LIMIT_BURST=60
CRANK_MCP_BIND=0.0.0.0:3002
CRANK_MCP_REFRESH_MS=5000
CRANK_MCP_RATE_LIMIT_RPS=60
CRANK_MCP_RATE_LIMIT_BURST=120
CRANK_MASTER_KEY=
CRANK_BASE_URL=http://localhost:3000
CRANK_RUNTIME_MAX_CONCURRENT_UNARY=64
CRANK_RUNTIME_MAX_CONCURRENT_WINDOW=16
CRANK_RUNTIME_MAX_CONCURRENT_SESSIONS=16
CRANK_RUNTIME_MAX_CONCURRENT_JOBS=16
# Публичные узлы разрешены по умолчанию. Для внутренних API перечислите
# допустимые имена или IP через запятую.
CRANK_CACHE_BACKEND=memory
CRANK_CACHE_URL=
CRANK_OUTBOUND_ALLOWED_HOSTS=
CRANK_OUTBOUND_DENIED_HOSTS=
CRANK_OUTBOUND_MAX_REQUEST_BYTES=4194304
CRANK_OUTBOUND_MAX_RESPONSE_BYTES=4194304
CRANK_IMPORT_EXTERNAL_REFERENCE_ALLOWED_URL_PREFIXES=
CRANK_IMPORT_EXTERNAL_REFERENCE_MAX_DEPTH=8
CRANK_IMPORT_EXTERNAL_REFERENCE_MAX_DOCUMENTS=32
CRANK_IMPORT_EXTERNAL_REFERENCE_MAX_FETCH_BYTES=262144
CRANK_IMPORT_EXTERNAL_REFERENCE_FETCH_TIMEOUT_MS=10000
CRANK_IMPORT_EXTERNAL_REFERENCE_MAX_EXPANDED_NODES=10000
CRANK_ENVIRONMENT=development
CRANK_LOG_LEVEL=info
# Пустое значение отключает канал критических ошибок.
CRANK_LOG_LEVEL=
CRANK_SENTRY_DSN=
# Prometheus endpoints use separate listeners and stay on loopback by default.
CRANK_METRICS_ENABLED=true
CRANK_ADMIN_METRICS_BIND=127.0.0.1:9464
CRANK_MCP_METRICS_BIND=127.0.0.1:9465
# Required when either metrics listener uses a non-loopback address.
CRANK_METRICS_BEARER_TOKEN=
CRANK_INVOCATION_LOG_RETENTION_DAYS=30
# Пустой endpoint полностью отключает экспорт трасс.
OTEL_EXPORTER_OTLP_ENDPOINT=
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
@@ -53,15 +48,24 @@ OTEL_BSP_MAX_QUEUE_SIZE=2048
OTEL_BSP_MAX_EXPORT_BATCH_SIZE=512
OTEL_BSP_SCHEDULE_DELAY=5000
OTEL_BSP_EXPORT_TIMEOUT=30000
CRANK_MASTER_KEY=change-me-master-key
CRANK_SESSION_SECRET=change-me-session-secret
CRANK_PASSWORD_PEPPER=change-me-password-pepper
CRANK_ADMIN_BIND=0.0.0.0:3001
CRANK_ADMIN_METRICS_BIND=127.0.0.1:9464
CRANK_STORAGE_ROOT=/var/lib/crank/storage
CRANK_ADMIN_RATE_LIMIT_RPS=30
CRANK_ADMIN_RATE_LIMIT_BURST=60
CRANK_INVOCATION_LOG_RETENTION_DAYS=30
CRANK_SESSION_SECRET=
CRANK_PASSWORD_PEPPER=
CRANK_SESSION_TTL_HOURS=24
# Trust X-Real-IP / X-Forwarded-For for client rate limiting. Enable only when
# admin-api runs behind the bundled nginx (or another trusted reverse proxy).
CRANK_TRUST_FORWARDED_HEADERS=true
CRANK_BOOTSTRAP_ADMIN_EMAIL=owner@crank.local
CRANK_BOOTSTRAP_ADMIN_PASSWORD=change-me-admin-password
CRANK_TRUSTED_PROXY_IPS=
CRANK_BOOTSTRAP_ADMIN_EMAIL=
CRANK_BOOTSTRAP_ADMIN_PASSWORD=
CRANK_BOOTSTRAP_ADMIN_DISPLAY_NAME=Crank Owner
CRANK_DEMO_SEED=true
CRANK_BASE_URL=https://crank.example.com
CRANK_DEMO_SEED=false
CRANK_MCP_BIND=0.0.0.0:3002
CRANK_MCP_METRICS_BIND=127.0.0.1:9465
CRANK_MCP_REFRESH_MS=5000
CRANK_MCP_RATE_LIMIT_RPS=60
CRANK_MCP_RATE_LIMIT_BURST=120
CRANK_RUNTIME_MAX_CONCURRENT_SESSIONS=16
# END GENERATED CRANK RUNTIME CONFIG
+1
View File
@@ -0,0 +1 @@
docs/epic-1-completion/evidence/*.patch -whitespace
+273 -35
View File
@@ -2,14 +2,24 @@ name: CI
on:
pull_request:
types: [opened, synchronize, reopened, labeled, unlabeled]
workflow_dispatch:
inputs:
validation_level:
description: Validation depth for this run
type: choice
options: [story, feature]
default: story
required: true
push:
branches:
- main
- "feat/**"
env:
CARGO_BUILD_JOBS: "2"
CARGO_INCREMENTAL: "0"
CARGO_PROFILE_DEV_DEBUG: "0"
CARGO_PROFILE_TEST_DEBUG: "0"
RUST_TEST_THREADS: "2"
TESTCONTAINERS_RYUK_DISABLED: "true"
@@ -20,7 +30,9 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v5
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
with:
fetch-depth: 0
- name: Install Rust toolchain
run: |
@@ -55,12 +67,42 @@ jobs:
docker --version
docker info
- name: Install dependency policy tool
run: cargo install cargo-deny --version 0.20.2 --locked
- name: Check typed runtime configuration contract
run: |
cargo run -p crank-config --bin crank-config-contract -- --check
python3 scripts/check-runtime-config.py --root .
python3 scripts/check-config-boundaries.py --root .
- name: Check canonical migration contract
run: cargo run -p admin-api --bin crank-migrate -- plan --check
- name: Check typed metrics contract
run: |
cargo run -p crank-metrics --bin crank-metrics-contract -- --check
python3 scripts/check-metrics-boundaries.py --root .
- name: Run tooling unit tests
run: python3 -m unittest discover -s tests/unit
- name: Check Capability Inventory
run: |
required_args=""
for number in $(seq 1 54); do
required_args="$required_args --required-fr FR-$number"
done
python3 scripts/validate-capability-inventory.py \
--root . \
--inventory docs/capability-inventory.json \
--schema docs/schemas/capability-inventory.schema.json \
$required_args
- name: Check Capability Baseline
run: |
python3 scripts/validate-capability-baseline.py \
--root . \
--manifest docs/capability-baseline/manifest.json \
--schema docs/schemas/capability-baseline.schema.json
- name: Check Community scope
run: scripts/check-community-scope.sh
@@ -70,26 +112,34 @@ jobs:
- name: Check Rust code health
run: scripts/check-rust-code-health.sh
- name: Check dependency licenses and advisories
run: cargo deny --locked check advisories bans licenses sources
- name: Check Rust boundaries
run: scripts/check-rust-boundaries.sh
- name: Run clippy
run: cargo clippy --workspace --all-targets --all-features --jobs "$CARGO_BUILD_JOBS" -- -D warnings
- name: Run tests
run: cargo test --workspace --all-targets --jobs "$CARGO_BUILD_JOBS"
- name: Check OpenAPI matrix and corpus compatibility versions
env:
MATRIX_BASE_REF: ${{ gitea.base_ref }}
MATRIX_EVENT_BEFORE: ${{ gitea.event.before }}
run: |
if [ -n "$MATRIX_BASE_REF" ]; then
base_ref="origin/$MATRIX_BASE_REF"
elif [ -n "$MATRIX_EVENT_BEFORE" ] && ! printf '%s' "$MATRIX_EVENT_BEFORE" | grep -Eq '^0+$'; then
base_ref="$MATRIX_EVENT_BEFORE"
else
base_ref="$(git merge-base HEAD origin/main)"
fi
python3 scripts/check-openapi-matrix-compatibility.py --base "$base_ref"
- name: Run Crank story ATDD
run: scripts/ci-story-checks.sh
ui:
name: UI Checks
runs-on: ubuntu-latest
needs: rust
steps:
- name: Checkout
uses: actions/checkout@v5
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
- name: Verify runner toolchain
run: |
@@ -97,6 +147,7 @@ jobs:
npm --version
docker --version
- name: Install UI dependencies
working-directory: apps/ui
run: npm ci
@@ -109,10 +160,74 @@ jobs:
working-directory: apps/ui
run: npm run build
rust-feature:
needs: rust
name: Rust Feature Validation
if: >-
inputs.validation_level == 'feature' ||
contains(gitea.event.pull_request.labels.*.name, 'feature-ready')
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
with:
fetch-depth: 0
- name: Install Rust toolchain
run: |
set -eu
toolchain="$(sed -n 's/^channel = "\(.*\)"/\1/p' rust-toolchain.toml | head -n1)"
if [ -z "$toolchain" ]; then
echo "Unable to read Rust toolchain channel from rust-toolchain.toml" >&2
exit 1
fi
rustup toolchain install "$toolchain" --profile minimal --component clippy --component rustfmt
rustup default "$toolchain"
host="$(rustc -vV | sed -n 's/^host: //p')"
toolchain_dir="${RUSTUP_HOME:-$HOME/.rustup}/toolchains/${toolchain}-${host}"
toolchain_bin="$toolchain_dir/bin"
if [ ! -x "$toolchain_bin/rustc" ] || [ ! -x "$toolchain_bin/cargo" ]; then
echo "Rust $toolchain was not installed at $toolchain_dir." >&2
exit 1
fi
printf '%s\n' "$toolchain_bin" >> "$GITHUB_PATH"
"$toolchain_bin/rustc" --version
"$toolchain_bin/cargo" --version
"$toolchain_bin/rustfmt" --version
"$toolchain_bin/cargo-clippy" --version
- name: Verify runner toolchain
run: |
python3 --version
rustc --version
cargo --version
rustfmt --version
cargo clippy --version
docker --version
docker info
- name: Install dependency policy tool
run: cargo install cargo-deny --version 0.20.2 --locked
- name: Check dependency licenses and advisories
run: cargo deny --locked check advisories bans licenses sources
- name: Run offline OpenAPI support corpus gate
run: cargo test -p crank-import --test corpus -- --test-threads=1
- name: Save OpenAPI corpus evidence
if: always()
uses: https://github.com/ChristopherHX/gitea-upload-artifact@81f940d004763f986ba3582c007fd842dd5cb0d7 # v4
with:
name: openapi-corpus-evidence
path: crates/crank-import/target/openapi-corpus/evidence-*.json
if-no-files-found: warn
- name: Run clippy
run: cargo clippy --workspace --all-targets --all-features --jobs "$CARGO_BUILD_JOBS" -- -D warnings
- name: Run tests
run: cargo test --workspace --all-targets --jobs "$CARGO_BUILD_JOBS" -- --test-threads=1
frontend-e2e:
needs: [rust, ui]
if: >-
inputs.validation_level == 'feature' ||
contains(gitea.event.pull_request.labels.*.name, 'feature-ready')
name: Frontend E2E
runs-on: ubuntu-latest
needs: ui
services:
crank-e2e-postgres:
image: postgres:16-alpine
@@ -135,7 +250,7 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v5
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
- name: Install Rust toolchain
run: |
@@ -181,7 +296,31 @@ jobs:
- name: Run Playwright e2e
working-directory: apps/ui
run: npx playwright test
run: |
mkdir -p ../../.tmp
rm -f ../../.tmp/openapi-playwright.json ../../.tmp/openapi-ui-evidence.json
PLAYWRIGHT_JSON_OUTPUT=../../.tmp/openapi-playwright.json npx playwright test --retries=0
- name: Collect sanitized OpenAPI UI evidence
working-directory: apps/ui
run: |
trap 'rm -f ../../.tmp/openapi-playwright.json' EXIT
python3 ../../scripts/collect-capability-baseline.py playwright \
--report ../../.tmp/openapi-playwright.json \
--output ../../.tmp/openapi-ui-evidence.json \
--source-revision "$(git -C ../.. rev-parse HEAD)" \
--environment-class ci \
--flow-id openapi-upload-ui \
--required-test 'operations page imports OpenAPI methods as drafts' \
--required-test 'OpenAPI upload rejects invalid files locally and restores focus after Escape' \
--required-test 'OpenAPI upload recovers from pagehide and a preview server error' \
--required-test 'OpenAPI apply preserves job authority after language or workspace changes' \
--required-test 'OpenAPI upload only renders the latest selected file and clears reset or close races' \
--required-test 'OpenAPI upload ignores a stale failure and renders only correlation identifiers'
python3 ../../scripts/validate-capability-run.py \
--schema ../../docs/schemas/capability-baseline.schema.json \
--candidate ../../.tmp/openapi-ui-evidence.json \
--require-accepted
- name: Show Playwright stack logs
if: failure()
@@ -189,24 +328,45 @@ jobs:
find .tmp/ui-e2e/logs -maxdepth 1 -type f -print -exec sed -n '1,220p' {} \; || true
deployment:
needs: [rust, ui]
if: >-
inputs.validation_level == 'feature' ||
contains(gitea.event.pull_request.labels.*.name, 'feature-ready')
name: Community Image Smoke
runs-on: ubuntu-latest
needs:
- rust
- ui
steps:
- name: Checkout
uses: actions/checkout@v5
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
- name: Validate Community deployment manifest
run: docker compose -f deploy/community/docker-compose.yml --env-file deploy/community/.env.example config -q
- name: Validate Community deployment manifests
run: |
docker compose -f docker-compose.yml --env-file .env.example config -q
docker compose -f deploy/community/docker-compose.yml --env-file deploy/community/.env.example config -q
docker compose -f deploy/community/docker-compose.images.yml --env-file deploy/community/.env.images.example --profile local-db config -q
- name: Build Community images
run: |
docker build -f apps/admin-api/Dockerfile -t crank/admin-api:ci .
docker build -f apps/mcp-server/Dockerfile -t crank/mcp-server:ci .
docker build -f apps/ui/Dockerfile -t crank/ui:ci .
docker build --pull --build-arg CRANK_SECURITY_REFRESH=${{ gitea.sha }} \
-f apps/admin-api/Dockerfile -t crank/admin-api:ci .
docker build --pull --build-arg CRANK_SECURITY_REFRESH=${{ gitea.sha }} \
-f apps/mcp-server/Dockerfile -t crank/mcp-server:ci .
docker build --pull -f apps/ui/Dockerfile -t crank/ui:ci .
- name: Scan Community images
run: |
scripts/scan-images.sh \
crank/admin-api:ci \
crank/mcp-server:ci \
crank/ui:ci
- name: Save Community image scan evidence
if: always()
uses: https://github.com/ChristopherHX/gitea-upload-artifact@81f940d004763f986ba3582c007fd842dd5cb0d7 # v4
with:
name: image-scan-evidence
path: .tmp/image-scan-evidence
if-no-files-found: warn
- name: Start Community image stack
run: |
@@ -226,17 +386,44 @@ jobs:
CRANK_SESSION_SECRET=ci-session-secret
CRANK_PASSWORD_PEPPER=ci-password-pepper
CRANK_BOOTSTRAP_ADMIN_EMAIL=owner@crank.test
CRANK_BOOTSTRAP_ADMIN_PASSWORD=ci-admin-password
CRANK_BOOTSTRAP_ADMIN_DISPLAY_NAME=CI Owner
CRANK_BASE_URL=http://127.0.0.1
CRANK_ENVIRONMENT=ci
CRANK_OUTBOUND_ALLOWED_HOSTS=admin-api
CRANK_PUBLISH_BIND=127.0.0.1
CRANK_ADMIN_PUBLISH_PORT=0
CRANK_MCP_PUBLISH_PORT=0
CRANK_UI_PUBLISH_PORT=0
CRANK_DEMO_SEED=true
EOF
docker compose -f deploy/community/docker-compose.images.yml \
--env-file .tmp/community-smoke.env --profile local-db up -d --wait
compose=(docker compose -f deploy/community/docker-compose.images.yml \
--env-file .tmp/community-smoke.env --profile local-db)
"${compose[@]}" up -d --wait postgres
"${compose[@]}" run --rm migrate
bootstrap_json="$("${compose[@]}" run --rm --no-deps \
--entrypoint crank-migrate migrate admin-auth bootstrap-create \
--email owner@crank.test --display-name 'CI Owner')"
bootstrap_token="$(python3 -c \
'import json,sys; print(json.loads(sys.stdin.read())["bootstrap_token"])' \
<<<"$bootstrap_json")"
install -d -m 700 .tmp/community-bootstrap
printf '%s' "$bootstrap_token" > .tmp/community-bootstrap/token
printf '%s' 'ci-admin-password' > .tmp/community-bootstrap/password
printf '%s' 'ci-password-pepper' > .tmp/community-bootstrap/password-pepper
chmod 600 .tmp/community-bootstrap/token \
.tmp/community-bootstrap/password \
.tmp/community-bootstrap/password-pepper
"${compose[@]}" run --rm --no-deps \
-v "$PWD/.tmp/community-bootstrap:/run/bootstrap:ro" \
--entrypoint crank-migrate migrate admin-auth bootstrap-complete \
--token-file /run/bootstrap/token \
--password-file /run/bootstrap/password \
--password-pepper-file /run/bootstrap/password-pepper
rm -f .tmp/community-bootstrap/token \
.tmp/community-bootstrap/password \
.tmp/community-bootstrap/password-pepper
rmdir .tmp/community-bootstrap
"${compose[@]}" up -d --wait
- name: Run authenticated Community image smoke
env:
@@ -265,14 +452,53 @@ jobs:
docker compose -f deploy/community/docker-compose.images.yml \
--env-file .tmp/community-smoke.env --profile local-db down -v --remove-orphans || true
required-checks:
name: Required Checks
runs-on: ubuntu-latest
if: always()
needs:
- rust
- ui
- deployment
- rust-feature
- frontend-e2e
steps:
- name: Require story gates and mode-appropriate feature gates
env:
FEATURE_VALIDATION: ${{ inputs.validation_level == 'feature' || contains(gitea.event.pull_request.labels.*.name, 'feature-ready') }}
RUST_RESULT: ${{ needs.rust.result }}
UI_RESULT: ${{ needs.ui.result }}
RUST_FEATURE_RESULT: ${{ needs.rust-feature.result }}
E2E_RESULT: ${{ needs.frontend-e2e.result }}
IMAGE_RESULT: ${{ needs.deployment.result }}
run: |
set -eu
if [ "$RUST_RESULT" != "success" ] || \
[ "$UI_RESULT" != "success" ]; then
echo "A required story gate did not succeed." >&2
exit 1
fi
if [ "$FEATURE_VALIDATION" = "true" ]; then
if [ "$RUST_FEATURE_RESULT" != "success" ] || \
[ "$E2E_RESULT" != "success" ] || \
[ "$IMAGE_RESULT" != "success" ]; then
echo "A required feature gate did not succeed." >&2
exit 1
fi
else
if [ "$RUST_FEATURE_RESULT" != "skipped" ] || \
[ "$E2E_RESULT" != "skipped" ] || \
[ "$IMAGE_RESULT" != "skipped" ]; then
echo "A feature-only gate did not skip in story mode." >&2
exit 1
fi
fi
deploy:
name: Deploy
runs-on: ubuntu-latest
needs:
- rust
- ui
- frontend-e2e
- deployment
- required-checks
if: ${{ gitea.event_name == 'push' && gitea.ref == 'refs/heads/main' }}
env:
REGISTRY: git.itexp.me
@@ -284,7 +510,7 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v5
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
- name: Verify runner toolchain
run: |
@@ -308,15 +534,19 @@ jobs:
- name: Build and push images
run: |
docker build -f apps/admin-api/Dockerfile \
docker build --pull \
--build-arg CRANK_SECURITY_REFRESH=${{ env.IMAGE_TAG }} \
-f apps/admin-api/Dockerfile \
-t '${{ env.ADMIN_API_IMAGE }}:${{ env.IMAGE_TAG }}' \
-t '${{ env.ADMIN_API_IMAGE }}:main' \
.
docker build -f apps/mcp-server/Dockerfile \
docker build --pull \
--build-arg CRANK_SECURITY_REFRESH=${{ env.IMAGE_TAG }} \
-f apps/mcp-server/Dockerfile \
-t '${{ env.MCP_SERVER_IMAGE }}:${{ env.IMAGE_TAG }}' \
-t '${{ env.MCP_SERVER_IMAGE }}:main' \
.
docker build -f apps/ui/Dockerfile \
docker build --pull -f apps/ui/Dockerfile \
-t '${{ env.UI_IMAGE }}:${{ env.IMAGE_TAG }}' \
-t '${{ env.UI_IMAGE }}:main' \
.
@@ -331,6 +561,14 @@ jobs:
docker push '${{ env.UI_IMAGE }}:${{ env.IMAGE_TAG }}'
docker push '${{ env.UI_IMAGE }}:main'
- name: Save deploy image scan evidence
if: always()
uses: https://github.com/ChristopherHX/gitea-upload-artifact@81f940d004763f986ba3582c007fd842dd5cb0d7 # v4
with:
name: deploy-image-scan-evidence
path: .tmp/image-scan-evidence
if-no-files-found: warn
- name: Configure SSH key
run: |
. "$OPENBAO_ENV_FILE"
+168 -13
View File
@@ -5,6 +5,13 @@ on:
tags:
- "v*"
workflow_dispatch:
inputs:
validation_level:
description: Validation depth for this run
type: choice
options: [story, feature]
default: story
required: true
env:
REGISTRY: git.itexp.me
@@ -17,10 +24,17 @@ env:
jobs:
release:
runs-on: ubuntu-latest
permissions:
code: read
releases: write
steps:
- name: Checkout
uses: actions/checkout@v5
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
- name: Clear optional release evidence
run: rm -f .tmp/openapi-playwright.json .tmp/openapi-ui-evidence.json
- name: Use preinstalled Rust toolchain
run: |
@@ -41,6 +55,7 @@ jobs:
- name: Verify runner toolchain
run: |
python3 --version
rustc --version
cargo --version
node --version
@@ -52,16 +67,66 @@ jobs:
- name: Install dependency policy tool
run: cargo install cargo-deny --version 0.20.2 --locked
- name: Check formatting
run: cargo fmt --all --check
- name: Audit Rust dependencies
run: cargo deny --locked check advisories bans licenses sources
- name: Run tooling unit tests
if: inputs.validation_level == 'feature'
run: python3 -m unittest discover -s tests/unit
- name: Check typed runtime configuration contract
run: |
cargo run -p crank-config --bin crank-config-contract -- --check
python3 scripts/check-runtime-config.py --root .
python3 scripts/check-config-boundaries.py --root .
scripts/check-rust-boundaries.sh
- name: Check canonical migration contract
run: cargo run -p admin-api --bin crank-migrate -- plan --check
- name: Check typed metrics contract
run: |
cargo run -p crank-metrics --bin crank-metrics-contract -- --check
python3 scripts/check-metrics-boundaries.py --root .
- name: Check Capability Inventory
run: |
required_args=""
for number in $(seq 1 54); do
required_args="$required_args --required-fr FR-$number"
done
python3 scripts/validate-capability-inventory.py \
--root . \
--inventory docs/capability-inventory.json \
--schema docs/schemas/capability-inventory.schema.json \
$required_args
- name: Check Capability Baseline
run: |
python3 scripts/validate-capability-baseline.py \
--root . \
--manifest docs/capability-baseline/manifest.json \
--schema docs/schemas/capability-baseline.schema.json
- name: Check Community scope
run: scripts/check-community-scope.sh
- name: Run Crank story ATDD
run: scripts/ci-story-checks.sh
- name: Run release quality gates
if: inputs.validation_level == 'feature'
run: |
cargo fmt --all --check
cargo clippy --workspace --all-targets --all-features -- -D warnings
cargo test --workspace --all-targets
cargo deny --locked check advisories bans licenses sources
cargo test --workspace --all-targets -- --test-threads=1
- name: Build release binaries
run: cargo build --release -p admin-api -p mcp-server
- name: Install UI dependencies
working-directory: apps/ui
run: npm ci
@@ -75,29 +140,68 @@ jobs:
run: npm run build
- name: Install Playwright browser
if: inputs.validation_level == 'feature'
working-directory: apps/ui
run: npx playwright install --with-deps chromium
- name: Run release end-to-end tests
if: inputs.validation_level == 'feature'
working-directory: apps/ui
run: npm run e2e
run: |
mkdir -p ../../.tmp
rm -f ../../.tmp/openapi-playwright.json ../../.tmp/openapi-ui-evidence.json
PLAYWRIGHT_JSON_OUTPUT=../../.tmp/openapi-playwright.json npm run e2e
- name: Validate deployment manifest
run: docker compose -f deploy/community/docker-compose.yml --env-file deploy/community/.env.example config -q
- name: Collect sanitized OpenAPI UI release evidence
if: inputs.validation_level == 'feature'
working-directory: apps/ui
run: |
trap 'rm -f ../../.tmp/openapi-playwright.json' EXIT
python3 ../../scripts/collect-capability-baseline.py playwright \
--report ../../.tmp/openapi-playwright.json \
--output ../../.tmp/openapi-ui-evidence.json \
--source-revision "$(git -C ../.. rev-parse HEAD)" \
--environment-class release \
--flow-id openapi-upload-ui \
--required-test 'operations page imports OpenAPI methods as drafts' \
--required-test 'OpenAPI upload rejects invalid files locally and restores focus after Escape' \
--required-test 'OpenAPI upload recovers from pagehide and a preview server error' \
--required-test 'OpenAPI apply preserves job authority after language or workspace changes' \
--required-test 'OpenAPI upload only renders the latest selected file and clears reset or close races' \
--required-test 'OpenAPI upload ignores a stale failure and renders only correlation identifiers'
python3 ../../scripts/validate-capability-run.py \
--schema ../../docs/schemas/capability-baseline.schema.json \
--candidate ../../.tmp/openapi-ui-evidence.json \
--require-accepted
- name: Validate deployment manifests
run: |
docker compose -f docker-compose.yml --env-file .env.example config -q
docker compose -f deploy/community/docker-compose.yml --env-file deploy/community/.env.example config -q
docker compose -f deploy/community/docker-compose.images.yml --env-file deploy/community/.env.images.example --profile local-db config -q
- name: Package release artifacts
run: |
set -euo pipefail
mkdir -p dist/release
source_revision="$(git rev-parse HEAD)"
evidence_artifact="dist/crank-openapi-ui-evidence-${IMAGE_TAG}-${source_revision}.json"
cp target/release/admin-api dist/release/admin-api
cp target/release/crank-migrate dist/release/crank-migrate
cp target/release/mcp-server dist/release/mcp-server
tar -C dist/release -czf dist/crank-community-admin-api-${IMAGE_TAG}.tar.gz admin-api
tar -C dist/release -czf dist/crank-community-admin-api-${IMAGE_TAG}.tar.gz admin-api crank-migrate
tar -C dist/release -czf dist/crank-community-mcp-server-${IMAGE_TAG}.tar.gz mcp-server
tar -C apps/ui/dist -czf dist/crank-community-ui-${IMAGE_TAG}.tar.gz .
sha256sum \
dist/crank-community-admin-api-${IMAGE_TAG}.tar.gz \
dist/crank-community-mcp-server-${IMAGE_TAG}.tar.gz \
dist/crank-community-ui-${IMAGE_TAG}.tar.gz \
> dist/crank-community-${IMAGE_TAG}-checksums.txt
files=(
"dist/crank-community-admin-api-${IMAGE_TAG}.tar.gz"
"dist/crank-community-mcp-server-${IMAGE_TAG}.tar.gz"
"dist/crank-community-ui-${IMAGE_TAG}.tar.gz"
)
if [ -f .tmp/openapi-ui-evidence.json ]; then
cp .tmp/openapi-ui-evidence.json "$evidence_artifact"
files+=("$evidence_artifact")
fi
sha256sum "${files[@]}" > "dist/crank-community-${IMAGE_TAG}-checksums.txt"
- name: Generate SBOM
run: |
@@ -105,6 +209,31 @@ jobs:
dir:/workspace -o spdx-json \
> dist/crank-community-${IMAGE_TAG}-sbom.spdx.json
- name: Publish release bundles, checksums, evidence, and SBOM to Gitea
if: startsWith(gitea.ref, 'refs/tags/v')
env:
# gitea.server_url inside the job container is the internal http://gitea:3000 origin;
# the publisher only forwards its token to the public HTTPS origin.
GITEA_SERVER_URL: https://git.itexp.me
GITEA_REPOSITORY: ${{ gitea.repository }}
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
RELEASE_TAG: ${{ gitea.ref_name }}
SOURCE_REVISION: ${{ gitea.sha }}
run: |
set -euo pipefail
assets=(
"dist/crank-community-admin-api-${RELEASE_TAG}.tar.gz"
"dist/crank-community-mcp-server-${RELEASE_TAG}.tar.gz"
"dist/crank-community-ui-${RELEASE_TAG}.tar.gz"
"dist/crank-community-${RELEASE_TAG}-checksums.txt"
"dist/crank-community-${RELEASE_TAG}-sbom.spdx.json"
)
evidence="dist/crank-openapi-ui-evidence-${RELEASE_TAG}-${SOURCE_REVISION}.json"
if [ -f "$evidence" ]; then
assets+=("$evidence")
fi
python3 scripts/ci-release-assets.py "${assets[@]}"
- name: Load deployment secrets from OpenBao
env:
BAO_ADDR: ${{ secrets.BAO_ADDR }}
@@ -130,6 +259,32 @@ jobs:
docker build -f apps/ui/Dockerfile \
-t '${{ env.UI_IMAGE }}:${{ env.IMAGE_TAG }}' \
-t '${{ env.UI_IMAGE }}:latest' .
mkdir -p .tmp
cat > .tmp/release-migration-smoke.env <<EOF
COMPOSE_PROJECT_NAME=crank-release-migration-${{ github.run_id }}-${{ github.run_attempt }}
POSTGRES_HOST=postgres
POSTGRES_DB=crank
POSTGRES_USER=crank
POSTGRES_PASSWORD=release-smoke-password
CRANK_ADMIN_API_IMAGE=${{ env.ADMIN_API_IMAGE }}:${{ env.IMAGE_TAG }}
CRANK_MCP_SERVER_IMAGE=${{ env.MCP_SERVER_IMAGE }}:${{ env.IMAGE_TAG }}
CRANK_UI_IMAGE=${{ env.UI_IMAGE }}:${{ env.IMAGE_TAG }}
CRANK_MASTER_KEY=0000000000000000000000000000000000000000000000000000000000000000
CRANK_SESSION_SECRET=release-smoke-session
CRANK_PASSWORD_PEPPER=release-smoke-pepper
CRANK_BOOTSTRAP_ADMIN_EMAIL=owner@crank.test
CRANK_BASE_URL=http://127.0.0.1
CRANK_ENVIRONMENT=release-smoke
CRANK_PUBLISH_BIND=127.0.0.1
CRANK_ADMIN_PUBLISH_PORT=0
CRANK_MCP_PUBLISH_PORT=0
CRANK_UI_PUBLISH_PORT=0
EOF
trap 'docker compose -f deploy/community/docker-compose.images.yml --env-file .tmp/release-migration-smoke.env --profile local-db down -v --remove-orphans || true' EXIT
docker compose -f deploy/community/docker-compose.images.yml \
--env-file .tmp/release-migration-smoke.env --profile local-db up -d --wait
docker compose -f deploy/community/docker-compose.images.yml \
--env-file .tmp/release-migration-smoke.env --profile local-db logs migrate | grep '"status":"applied"'
scripts/scan-images.sh \
'${{ env.ADMIN_API_IMAGE }}:${{ env.IMAGE_TAG }}' \
'${{ env.MCP_SERVER_IMAGE }}:${{ env.IMAGE_TAG }}' \
+7
View File
@@ -23,3 +23,10 @@ __*.md
diploma/
AGENTS.md
TASKS.md
# BMAD workspace data and generated artifacts
**/_bmad*/
.bmad-loop/runs/
.bmad-loop/cache/
.bmad-loop/policy.toml
_bmad/render/
@@ -0,0 +1,328 @@
{
"db_name": "PostgreSQL",
"query": "select\n o.id,\n o.workspace_id,\n ov.name,\n ov.display_name,\n ov.category,\n ov.protocol,\n ov.security_level,\n ov.created_at as \"operation_created_at!: time::OffsetDateTime\",\n ov.created_at as \"operation_updated_at!: time::OffsetDateTime\",\n ov.published_at as \"operation_published_at: time::OffsetDateTime\",\n ov.version,\n ov.status,\n ov.target_json,\n ov.input_schema_json,\n ov.output_schema_json,\n ov.input_mapping_json,\n ov.output_mapping_json,\n ov.execution_config_json,\n ov.tool_description_json,\n ov.samples_json,\n ov.generated_draft_json,\n ov.config_export_json,\n ov.wizard_state_json,\n ov.change_note,\n ov.created_at as \"created_at!: time::OffsetDateTime\",\n ov.created_by\n from published_operations po\n join operation_versions ov\n on ov.operation_id = po.operation_id and ov.version = po.version\n join operations o on o.id = po.operation_id\n where po.operation_id = $1",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "id",
"type_info": "Text",
"origin": {
"Table": {
"table": "operations",
"name": "id"
}
}
},
{
"ordinal": 1,
"name": "workspace_id",
"type_info": "Text",
"origin": {
"Table": {
"table": "operations",
"name": "workspace_id"
}
}
},
{
"ordinal": 2,
"name": "name",
"type_info": "Text",
"origin": {
"Table": {
"table": "operation_versions",
"name": "name"
}
}
},
{
"ordinal": 3,
"name": "display_name",
"type_info": "Text",
"origin": {
"Table": {
"table": "operation_versions",
"name": "display_name"
}
}
},
{
"ordinal": 4,
"name": "category",
"type_info": "Text",
"origin": {
"Table": {
"table": "operation_versions",
"name": "category"
}
}
},
{
"ordinal": 5,
"name": "protocol",
"type_info": "Text",
"origin": {
"Table": {
"table": "operation_versions",
"name": "protocol"
}
}
},
{
"ordinal": 6,
"name": "security_level",
"type_info": "Text",
"origin": {
"Table": {
"table": "operation_versions",
"name": "security_level"
}
}
},
{
"ordinal": 7,
"name": "operation_created_at!: time::OffsetDateTime",
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "operation_versions",
"name": "created_at"
}
}
},
{
"ordinal": 8,
"name": "operation_updated_at!: time::OffsetDateTime",
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "operation_versions",
"name": "created_at"
}
}
},
{
"ordinal": 9,
"name": "operation_published_at: time::OffsetDateTime",
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "operation_versions",
"name": "published_at"
}
}
},
{
"ordinal": 10,
"name": "version",
"type_info": "Int4",
"origin": {
"Table": {
"table": "operation_versions",
"name": "version"
}
}
},
{
"ordinal": 11,
"name": "status",
"type_info": "Text",
"origin": {
"Table": {
"table": "operation_versions",
"name": "status"
}
}
},
{
"ordinal": 12,
"name": "target_json",
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "operation_versions",
"name": "target_json"
}
}
},
{
"ordinal": 13,
"name": "input_schema_json",
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "operation_versions",
"name": "input_schema_json"
}
}
},
{
"ordinal": 14,
"name": "output_schema_json",
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "operation_versions",
"name": "output_schema_json"
}
}
},
{
"ordinal": 15,
"name": "input_mapping_json",
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "operation_versions",
"name": "input_mapping_json"
}
}
},
{
"ordinal": 16,
"name": "output_mapping_json",
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "operation_versions",
"name": "output_mapping_json"
}
}
},
{
"ordinal": 17,
"name": "execution_config_json",
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "operation_versions",
"name": "execution_config_json"
}
}
},
{
"ordinal": 18,
"name": "tool_description_json",
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "operation_versions",
"name": "tool_description_json"
}
}
},
{
"ordinal": 19,
"name": "samples_json",
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "operation_versions",
"name": "samples_json"
}
}
},
{
"ordinal": 20,
"name": "generated_draft_json",
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "operation_versions",
"name": "generated_draft_json"
}
}
},
{
"ordinal": 21,
"name": "config_export_json",
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "operation_versions",
"name": "config_export_json"
}
}
},
{
"ordinal": 22,
"name": "wizard_state_json",
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "operation_versions",
"name": "wizard_state_json"
}
}
},
{
"ordinal": 23,
"name": "change_note",
"type_info": "Text",
"origin": {
"Table": {
"table": "operation_versions",
"name": "change_note"
}
}
},
{
"ordinal": 24,
"name": "created_at!: time::OffsetDateTime",
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "operation_versions",
"name": "created_at"
}
}
},
{
"ordinal": 25,
"name": "created_by",
"type_info": "Text",
"origin": {
"Table": {
"table": "operation_versions",
"name": "created_by"
}
}
}
],
"parameters": {
"Left": [
"Text"
]
},
"nullable": [
false,
false,
false,
false,
false,
false,
false,
false,
false,
true,
false,
false,
false,
false,
false,
false,
false,
false,
false,
true,
true,
true,
true,
true,
false,
true
]
},
"hash": "09a122a463d94b08719b16c221d266b9b2061da3eb19f0b16e1069ca30c4e60b"
}
@@ -6,72 +6,156 @@
{
"ordinal": 0,
"name": "id",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "operations",
"name": "id"
}
}
},
{
"ordinal": 1,
"name": "workspace_id",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "operations",
"name": "workspace_id"
}
}
},
{
"ordinal": 2,
"name": "name",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "operations",
"name": "name"
}
}
},
{
"ordinal": 3,
"name": "display_name",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "operations",
"name": "display_name"
}
}
},
{
"ordinal": 4,
"name": "category",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "operations",
"name": "category"
}
}
},
{
"ordinal": 5,
"name": "protocol",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "operations",
"name": "protocol"
}
}
},
{
"ordinal": 6,
"name": "security_level",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "operations",
"name": "security_level"
}
}
},
{
"ordinal": 7,
"name": "target_json",
"type_info": "Jsonb"
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "operation_versions",
"name": "target_json"
}
}
},
{
"ordinal": 8,
"name": "status",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "operations",
"name": "status"
}
}
},
{
"ordinal": 9,
"name": "current_draft_version",
"type_info": "Int4"
"type_info": "Int4",
"origin": {
"Table": {
"table": "operations",
"name": "current_draft_version"
}
}
},
{
"ordinal": 10,
"name": "latest_published_version",
"type_info": "Int4"
"type_info": "Int4",
"origin": {
"Table": {
"table": "operations",
"name": "latest_published_version"
}
}
},
{
"ordinal": 11,
"name": "created_at!: time::OffsetDateTime",
"type_info": "Timestamptz"
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "operations",
"name": "created_at"
}
}
},
{
"ordinal": 12,
"name": "updated_at!: time::OffsetDateTime",
"type_info": "Timestamptz"
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "operations",
"name": "updated_at"
}
}
},
{
"ordinal": 13,
"name": "published_at: time::OffsetDateTime",
"type_info": "Timestamptz"
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "operations",
"name": "published_at"
}
}
}
],
"parameters": {
@@ -0,0 +1,330 @@
{
"db_name": "PostgreSQL",
"query": "select\n o.id,\n o.workspace_id,\n ov.name,\n ov.display_name,\n ov.category,\n ov.protocol,\n ov.security_level,\n ov.created_at as \"operation_created_at!: time::OffsetDateTime\",\n ov.created_at as \"operation_updated_at!: time::OffsetDateTime\",\n ov.published_at as \"operation_published_at: time::OffsetDateTime\",\n ov.version,\n ov.status,\n ov.target_json,\n ov.input_schema_json,\n ov.output_schema_json,\n ov.input_mapping_json,\n ov.output_mapping_json,\n ov.execution_config_json,\n ov.tool_description_json,\n ov.samples_json,\n ov.generated_draft_json,\n ov.config_export_json,\n ov.wizard_state_json,\n ov.change_note,\n ov.created_at as \"created_at!: time::OffsetDateTime\",\n ov.created_by\n from operation_versions ov\n join operations o on o.id = ov.operation_id\n where o.workspace_id = $1 and ov.operation_id = $2 and ov.version = $3",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "id",
"type_info": "Text",
"origin": {
"Table": {
"table": "operations",
"name": "id"
}
}
},
{
"ordinal": 1,
"name": "workspace_id",
"type_info": "Text",
"origin": {
"Table": {
"table": "operations",
"name": "workspace_id"
}
}
},
{
"ordinal": 2,
"name": "name",
"type_info": "Text",
"origin": {
"Table": {
"table": "operation_versions",
"name": "name"
}
}
},
{
"ordinal": 3,
"name": "display_name",
"type_info": "Text",
"origin": {
"Table": {
"table": "operation_versions",
"name": "display_name"
}
}
},
{
"ordinal": 4,
"name": "category",
"type_info": "Text",
"origin": {
"Table": {
"table": "operation_versions",
"name": "category"
}
}
},
{
"ordinal": 5,
"name": "protocol",
"type_info": "Text",
"origin": {
"Table": {
"table": "operation_versions",
"name": "protocol"
}
}
},
{
"ordinal": 6,
"name": "security_level",
"type_info": "Text",
"origin": {
"Table": {
"table": "operation_versions",
"name": "security_level"
}
}
},
{
"ordinal": 7,
"name": "operation_created_at!: time::OffsetDateTime",
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "operation_versions",
"name": "created_at"
}
}
},
{
"ordinal": 8,
"name": "operation_updated_at!: time::OffsetDateTime",
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "operation_versions",
"name": "created_at"
}
}
},
{
"ordinal": 9,
"name": "operation_published_at: time::OffsetDateTime",
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "operation_versions",
"name": "published_at"
}
}
},
{
"ordinal": 10,
"name": "version",
"type_info": "Int4",
"origin": {
"Table": {
"table": "operation_versions",
"name": "version"
}
}
},
{
"ordinal": 11,
"name": "status",
"type_info": "Text",
"origin": {
"Table": {
"table": "operation_versions",
"name": "status"
}
}
},
{
"ordinal": 12,
"name": "target_json",
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "operation_versions",
"name": "target_json"
}
}
},
{
"ordinal": 13,
"name": "input_schema_json",
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "operation_versions",
"name": "input_schema_json"
}
}
},
{
"ordinal": 14,
"name": "output_schema_json",
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "operation_versions",
"name": "output_schema_json"
}
}
},
{
"ordinal": 15,
"name": "input_mapping_json",
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "operation_versions",
"name": "input_mapping_json"
}
}
},
{
"ordinal": 16,
"name": "output_mapping_json",
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "operation_versions",
"name": "output_mapping_json"
}
}
},
{
"ordinal": 17,
"name": "execution_config_json",
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "operation_versions",
"name": "execution_config_json"
}
}
},
{
"ordinal": 18,
"name": "tool_description_json",
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "operation_versions",
"name": "tool_description_json"
}
}
},
{
"ordinal": 19,
"name": "samples_json",
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "operation_versions",
"name": "samples_json"
}
}
},
{
"ordinal": 20,
"name": "generated_draft_json",
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "operation_versions",
"name": "generated_draft_json"
}
}
},
{
"ordinal": 21,
"name": "config_export_json",
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "operation_versions",
"name": "config_export_json"
}
}
},
{
"ordinal": 22,
"name": "wizard_state_json",
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "operation_versions",
"name": "wizard_state_json"
}
}
},
{
"ordinal": 23,
"name": "change_note",
"type_info": "Text",
"origin": {
"Table": {
"table": "operation_versions",
"name": "change_note"
}
}
},
{
"ordinal": 24,
"name": "created_at!: time::OffsetDateTime",
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "operation_versions",
"name": "created_at"
}
}
},
{
"ordinal": 25,
"name": "created_by",
"type_info": "Text",
"origin": {
"Table": {
"table": "operation_versions",
"name": "created_by"
}
}
}
],
"parameters": {
"Left": [
"Text",
"Text",
"Int4"
]
},
"nullable": [
false,
false,
false,
false,
false,
false,
false,
false,
false,
true,
false,
false,
false,
false,
false,
false,
false,
false,
false,
true,
true,
true,
true,
true,
false,
true
]
},
"hash": "0c0a4e5d7b11cd7df7aaf34fd3b7c294da65804186907e015da9420d594fefda"
}
@@ -1,173 +0,0 @@
{
"db_name": "PostgreSQL",
"query": "select\n o.id,\n o.workspace_id,\n o.name,\n o.display_name,\n o.category,\n o.protocol,\n o.security_level,\n o.created_at as \"operation_created_at!: time::OffsetDateTime\",\n o.updated_at as \"operation_updated_at!: time::OffsetDateTime\",\n o.published_at as \"operation_published_at: time::OffsetDateTime\",\n ov.version,\n ov.status,\n ov.target_json,\n ov.input_schema_json,\n ov.output_schema_json,\n ov.input_mapping_json,\n ov.output_mapping_json,\n ov.execution_config_json,\n ov.tool_description_json,\n ov.samples_json,\n ov.generated_draft_json,\n ov.config_export_json,\n ov.wizard_state_json,\n ov.change_note,\n ov.created_at as \"created_at!: time::OffsetDateTime\",\n ov.created_by\n from operation_versions ov\n join operations o on o.id = ov.operation_id\n where o.workspace_id = $1 and ov.operation_id = $2\n order by ov.version asc",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "id",
"type_info": "Text"
},
{
"ordinal": 1,
"name": "workspace_id",
"type_info": "Text"
},
{
"ordinal": 2,
"name": "name",
"type_info": "Text"
},
{
"ordinal": 3,
"name": "display_name",
"type_info": "Text"
},
{
"ordinal": 4,
"name": "category",
"type_info": "Text"
},
{
"ordinal": 5,
"name": "protocol",
"type_info": "Text"
},
{
"ordinal": 6,
"name": "security_level",
"type_info": "Text"
},
{
"ordinal": 7,
"name": "operation_created_at!: time::OffsetDateTime",
"type_info": "Timestamptz"
},
{
"ordinal": 8,
"name": "operation_updated_at!: time::OffsetDateTime",
"type_info": "Timestamptz"
},
{
"ordinal": 9,
"name": "operation_published_at: time::OffsetDateTime",
"type_info": "Timestamptz"
},
{
"ordinal": 10,
"name": "version",
"type_info": "Int4"
},
{
"ordinal": 11,
"name": "status",
"type_info": "Text"
},
{
"ordinal": 12,
"name": "target_json",
"type_info": "Jsonb"
},
{
"ordinal": 13,
"name": "input_schema_json",
"type_info": "Jsonb"
},
{
"ordinal": 14,
"name": "output_schema_json",
"type_info": "Jsonb"
},
{
"ordinal": 15,
"name": "input_mapping_json",
"type_info": "Jsonb"
},
{
"ordinal": 16,
"name": "output_mapping_json",
"type_info": "Jsonb"
},
{
"ordinal": 17,
"name": "execution_config_json",
"type_info": "Jsonb"
},
{
"ordinal": 18,
"name": "tool_description_json",
"type_info": "Jsonb"
},
{
"ordinal": 19,
"name": "samples_json",
"type_info": "Jsonb"
},
{
"ordinal": 20,
"name": "generated_draft_json",
"type_info": "Jsonb"
},
{
"ordinal": 21,
"name": "config_export_json",
"type_info": "Jsonb"
},
{
"ordinal": 22,
"name": "wizard_state_json",
"type_info": "Jsonb"
},
{
"ordinal": 23,
"name": "change_note",
"type_info": "Text"
},
{
"ordinal": 24,
"name": "created_at!: time::OffsetDateTime",
"type_info": "Timestamptz"
},
{
"ordinal": 25,
"name": "created_by",
"type_info": "Text"
}
],
"parameters": {
"Left": [
"Text",
"Text"
]
},
"nullable": [
false,
false,
false,
false,
false,
false,
false,
false,
false,
true,
false,
false,
false,
false,
false,
false,
false,
false,
false,
true,
true,
true,
true,
true,
false,
true
]
},
"hash": "0fb9fabe7417aa52f0977efa0ad5b3f1975517a60f63bb09028b3acb3506d1eb"
}
@@ -6,32 +6,68 @@
{
"ordinal": 0,
"name": "agent_id",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "agent_versions",
"name": "agent_id"
}
}
},
{
"ordinal": 1,
"name": "version",
"type_info": "Int4"
"type_info": "Int4",
"origin": {
"Table": {
"table": "agent_versions",
"name": "version"
}
}
},
{
"ordinal": 2,
"name": "status",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "agent_versions",
"name": "status"
}
}
},
{
"ordinal": 3,
"name": "instructions_json",
"type_info": "Jsonb"
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "agent_versions",
"name": "instructions_json"
}
}
},
{
"ordinal": 4,
"name": "tool_selection_policy_json",
"type_info": "Jsonb"
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "agent_versions",
"name": "tool_selection_policy_json"
}
}
},
{
"ordinal": 5,
"name": "created_at!: time::OffsetDateTime",
"type_info": "Timestamptz"
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "agent_versions",
"name": "created_at"
}
}
}
],
"parameters": {
@@ -6,42 +6,90 @@
{
"ordinal": 0,
"name": "id",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "operation_samples",
"name": "id"
}
}
},
{
"ordinal": 1,
"name": "operation_id",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "operation_samples",
"name": "operation_id"
}
}
},
{
"ordinal": 2,
"name": "version",
"type_info": "Int4"
"type_info": "Int4",
"origin": {
"Table": {
"table": "operation_samples",
"name": "version"
}
}
},
{
"ordinal": 3,
"name": "sample_kind",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "operation_samples",
"name": "sample_kind"
}
}
},
{
"ordinal": 4,
"name": "storage_ref",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "operation_samples",
"name": "storage_ref"
}
}
},
{
"ordinal": 5,
"name": "content_type",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "operation_samples",
"name": "content_type"
}
}
},
{
"ordinal": 6,
"name": "file_name",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "operation_samples",
"name": "file_name"
}
}
},
{
"ordinal": 7,
"name": "created_at!: time::OffsetDateTime",
"type_info": "Timestamptz"
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "operation_samples",
"name": "created_at"
}
}
}
],
"parameters": {
@@ -6,57 +6,123 @@
{
"ordinal": 0,
"name": "id",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "agents",
"name": "id"
}
}
},
{
"ordinal": 1,
"name": "workspace_id",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "agents",
"name": "workspace_id"
}
}
},
{
"ordinal": 2,
"name": "slug",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "agents",
"name": "slug"
}
}
},
{
"ordinal": 3,
"name": "display_name",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "agents",
"name": "display_name"
}
}
},
{
"ordinal": 4,
"name": "description",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "agents",
"name": "description"
}
}
},
{
"ordinal": 5,
"name": "status",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "agents",
"name": "status"
}
}
},
{
"ordinal": 6,
"name": "current_draft_version",
"type_info": "Int4"
"type_info": "Int4",
"origin": {
"Table": {
"table": "agents",
"name": "current_draft_version"
}
}
},
{
"ordinal": 7,
"name": "latest_published_version",
"type_info": "Int4"
"type_info": "Int4",
"origin": {
"Table": {
"table": "agents",
"name": "latest_published_version"
}
}
},
{
"ordinal": 8,
"name": "created_at!: time::OffsetDateTime",
"type_info": "Timestamptz"
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "agents",
"name": "created_at"
}
}
},
{
"ordinal": 9,
"name": "updated_at!: time::OffsetDateTime",
"type_info": "Timestamptz"
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "agents",
"name": "updated_at"
}
}
},
{
"ordinal": 10,
"name": "published_at: time::OffsetDateTime",
"type_info": "Timestamptz"
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "agents",
"name": "published_at"
}
}
}
],
"parameters": {
@@ -1,174 +0,0 @@
{
"db_name": "PostgreSQL",
"query": "select\n o.id,\n o.workspace_id,\n o.name,\n o.display_name,\n o.category,\n o.protocol,\n o.security_level,\n o.created_at as \"operation_created_at!: time::OffsetDateTime\",\n o.updated_at as \"operation_updated_at!: time::OffsetDateTime\",\n o.published_at as \"operation_published_at: time::OffsetDateTime\",\n ov.version,\n ov.status,\n ov.target_json,\n ov.input_schema_json,\n ov.output_schema_json,\n ov.input_mapping_json,\n ov.output_mapping_json,\n ov.execution_config_json,\n ov.tool_description_json,\n ov.samples_json,\n ov.generated_draft_json,\n ov.config_export_json,\n ov.wizard_state_json,\n ov.change_note,\n ov.created_at as \"created_at!: time::OffsetDateTime\",\n ov.created_by\n from operation_versions ov\n join operations o on o.id = ov.operation_id\n where o.workspace_id = $1 and ov.operation_id = $2 and ov.version = $3",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "id",
"type_info": "Text"
},
{
"ordinal": 1,
"name": "workspace_id",
"type_info": "Text"
},
{
"ordinal": 2,
"name": "name",
"type_info": "Text"
},
{
"ordinal": 3,
"name": "display_name",
"type_info": "Text"
},
{
"ordinal": 4,
"name": "category",
"type_info": "Text"
},
{
"ordinal": 5,
"name": "protocol",
"type_info": "Text"
},
{
"ordinal": 6,
"name": "security_level",
"type_info": "Text"
},
{
"ordinal": 7,
"name": "operation_created_at!: time::OffsetDateTime",
"type_info": "Timestamptz"
},
{
"ordinal": 8,
"name": "operation_updated_at!: time::OffsetDateTime",
"type_info": "Timestamptz"
},
{
"ordinal": 9,
"name": "operation_published_at: time::OffsetDateTime",
"type_info": "Timestamptz"
},
{
"ordinal": 10,
"name": "version",
"type_info": "Int4"
},
{
"ordinal": 11,
"name": "status",
"type_info": "Text"
},
{
"ordinal": 12,
"name": "target_json",
"type_info": "Jsonb"
},
{
"ordinal": 13,
"name": "input_schema_json",
"type_info": "Jsonb"
},
{
"ordinal": 14,
"name": "output_schema_json",
"type_info": "Jsonb"
},
{
"ordinal": 15,
"name": "input_mapping_json",
"type_info": "Jsonb"
},
{
"ordinal": 16,
"name": "output_mapping_json",
"type_info": "Jsonb"
},
{
"ordinal": 17,
"name": "execution_config_json",
"type_info": "Jsonb"
},
{
"ordinal": 18,
"name": "tool_description_json",
"type_info": "Jsonb"
},
{
"ordinal": 19,
"name": "samples_json",
"type_info": "Jsonb"
},
{
"ordinal": 20,
"name": "generated_draft_json",
"type_info": "Jsonb"
},
{
"ordinal": 21,
"name": "config_export_json",
"type_info": "Jsonb"
},
{
"ordinal": 22,
"name": "wizard_state_json",
"type_info": "Jsonb"
},
{
"ordinal": 23,
"name": "change_note",
"type_info": "Text"
},
{
"ordinal": 24,
"name": "created_at!: time::OffsetDateTime",
"type_info": "Timestamptz"
},
{
"ordinal": 25,
"name": "created_by",
"type_info": "Text"
}
],
"parameters": {
"Left": [
"Text",
"Text",
"Int4"
]
},
"nullable": [
false,
false,
false,
false,
false,
false,
false,
false,
false,
true,
false,
false,
false,
false,
false,
false,
false,
false,
false,
true,
true,
true,
true,
true,
false,
true
]
},
"hash": "21011878e679a08b38c588bae6e24f770221be24a289ba6d83ed32b6b051ed2b"
}
@@ -6,27 +6,57 @@
{
"ordinal": 0,
"name": "id",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "users",
"name": "id"
}
}
},
{
"ordinal": 1,
"name": "email",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "users",
"name": "email"
}
}
},
{
"ordinal": 2,
"name": "display_name",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "users",
"name": "display_name"
}
}
},
{
"ordinal": 3,
"name": "status",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "users",
"name": "status"
}
}
},
{
"ordinal": 4,
"name": "created_at!: OffsetDateTime",
"type_info": "Timestamptz"
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "users",
"name": "created_at"
}
}
}
],
"parameters": {
@@ -6,7 +6,8 @@
{
"ordinal": 0,
"name": "allowed!",
"type_info": "Bool"
"type_info": "Bool",
"origin": "Expression"
}
],
"parameters": {
@@ -6,7 +6,8 @@
{
"ordinal": 0,
"name": "present!",
"type_info": "Int4"
"type_info": "Int4",
"origin": "Expression"
}
],
"parameters": {
@@ -1,52 +0,0 @@
{
"db_name": "PostgreSQL",
"query": "select\n id,\n email,\n display_name,\n password_hash as \"password_hash!\",\n status,\n created_at as \"created_at!: OffsetDateTime\"\n from users\n where email = $1\n limit 1",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "id",
"type_info": "Text"
},
{
"ordinal": 1,
"name": "email",
"type_info": "Text"
},
{
"ordinal": 2,
"name": "display_name",
"type_info": "Text"
},
{
"ordinal": 3,
"name": "password_hash!",
"type_info": "Text"
},
{
"ordinal": 4,
"name": "status",
"type_info": "Text"
},
{
"ordinal": 5,
"name": "created_at!: OffsetDateTime",
"type_info": "Timestamptz"
}
],
"parameters": {
"Left": [
"Text"
]
},
"nullable": [
false,
false,
false,
true,
false,
false
]
},
"hash": "38911a904c6d284f5fb80cf2ae9f569de7c0ffd3b8ca73e6c01f0842ace4ad25"
}
@@ -1,52 +0,0 @@
{
"db_name": "PostgreSQL",
"query": "select\n id,\n email,\n display_name,\n password_hash as \"password_hash!\",\n status,\n created_at as \"created_at!: OffsetDateTime\"\n from users\n where id = $1\n limit 1",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "id",
"type_info": "Text"
},
{
"ordinal": 1,
"name": "email",
"type_info": "Text"
},
{
"ordinal": 2,
"name": "display_name",
"type_info": "Text"
},
{
"ordinal": 3,
"name": "password_hash!",
"type_info": "Text"
},
{
"ordinal": 4,
"name": "status",
"type_info": "Text"
},
{
"ordinal": 5,
"name": "created_at!: OffsetDateTime",
"type_info": "Timestamptz"
}
],
"parameters": {
"Left": [
"Text"
]
},
"nullable": [
false,
false,
false,
true,
false,
false
]
},
"hash": "3d647bffe6eaf3b95be589ae2fe006b4aec8f55fa070f9f2ff3859dc7cb96d06"
}
@@ -1,25 +0,0 @@
{
"db_name": "PostgreSQL",
"query": "insert into users (\n id,\n email,\n display_name,\n password_hash,\n status,\n created_at\n ) values (\n $1, $2, $3, $4, 'active', now()\n )\n on conflict (email) do update\n set display_name = excluded.display_name,\n password_hash = excluded.password_hash,\n status = 'active'\n returning id",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "id",
"type_info": "Text"
}
],
"parameters": {
"Left": [
"Text",
"Text",
"Text",
"Text"
]
},
"nullable": [
false
]
},
"hash": "3df72b8a0ac4a76b15909fc78268a1c87d1cd184973bf1cecc0c725691432c08"
}
@@ -0,0 +1,329 @@
{
"db_name": "PostgreSQL",
"query": "select\n o.id,\n o.workspace_id,\n ov.name,\n ov.display_name,\n ov.category,\n ov.protocol,\n ov.security_level,\n ov.created_at as \"operation_created_at!: time::OffsetDateTime\",\n ov.created_at as \"operation_updated_at!: time::OffsetDateTime\",\n ov.published_at as \"operation_published_at: time::OffsetDateTime\",\n ov.version,\n ov.status,\n ov.target_json,\n ov.input_schema_json,\n ov.output_schema_json,\n ov.input_mapping_json,\n ov.output_mapping_json,\n ov.execution_config_json,\n ov.tool_description_json,\n ov.samples_json,\n ov.generated_draft_json,\n ov.config_export_json,\n ov.wizard_state_json,\n ov.change_note,\n ov.created_at as \"created_at!: time::OffsetDateTime\",\n ov.created_by\n from operation_versions ov\n join operations o on o.id = ov.operation_id\n where o.workspace_id = $1 and ov.operation_id = $2\n order by ov.version asc",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "id",
"type_info": "Text",
"origin": {
"Table": {
"table": "operations",
"name": "id"
}
}
},
{
"ordinal": 1,
"name": "workspace_id",
"type_info": "Text",
"origin": {
"Table": {
"table": "operations",
"name": "workspace_id"
}
}
},
{
"ordinal": 2,
"name": "name",
"type_info": "Text",
"origin": {
"Table": {
"table": "operation_versions",
"name": "name"
}
}
},
{
"ordinal": 3,
"name": "display_name",
"type_info": "Text",
"origin": {
"Table": {
"table": "operation_versions",
"name": "display_name"
}
}
},
{
"ordinal": 4,
"name": "category",
"type_info": "Text",
"origin": {
"Table": {
"table": "operation_versions",
"name": "category"
}
}
},
{
"ordinal": 5,
"name": "protocol",
"type_info": "Text",
"origin": {
"Table": {
"table": "operation_versions",
"name": "protocol"
}
}
},
{
"ordinal": 6,
"name": "security_level",
"type_info": "Text",
"origin": {
"Table": {
"table": "operation_versions",
"name": "security_level"
}
}
},
{
"ordinal": 7,
"name": "operation_created_at!: time::OffsetDateTime",
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "operation_versions",
"name": "created_at"
}
}
},
{
"ordinal": 8,
"name": "operation_updated_at!: time::OffsetDateTime",
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "operation_versions",
"name": "created_at"
}
}
},
{
"ordinal": 9,
"name": "operation_published_at: time::OffsetDateTime",
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "operation_versions",
"name": "published_at"
}
}
},
{
"ordinal": 10,
"name": "version",
"type_info": "Int4",
"origin": {
"Table": {
"table": "operation_versions",
"name": "version"
}
}
},
{
"ordinal": 11,
"name": "status",
"type_info": "Text",
"origin": {
"Table": {
"table": "operation_versions",
"name": "status"
}
}
},
{
"ordinal": 12,
"name": "target_json",
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "operation_versions",
"name": "target_json"
}
}
},
{
"ordinal": 13,
"name": "input_schema_json",
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "operation_versions",
"name": "input_schema_json"
}
}
},
{
"ordinal": 14,
"name": "output_schema_json",
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "operation_versions",
"name": "output_schema_json"
}
}
},
{
"ordinal": 15,
"name": "input_mapping_json",
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "operation_versions",
"name": "input_mapping_json"
}
}
},
{
"ordinal": 16,
"name": "output_mapping_json",
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "operation_versions",
"name": "output_mapping_json"
}
}
},
{
"ordinal": 17,
"name": "execution_config_json",
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "operation_versions",
"name": "execution_config_json"
}
}
},
{
"ordinal": 18,
"name": "tool_description_json",
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "operation_versions",
"name": "tool_description_json"
}
}
},
{
"ordinal": 19,
"name": "samples_json",
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "operation_versions",
"name": "samples_json"
}
}
},
{
"ordinal": 20,
"name": "generated_draft_json",
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "operation_versions",
"name": "generated_draft_json"
}
}
},
{
"ordinal": 21,
"name": "config_export_json",
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "operation_versions",
"name": "config_export_json"
}
}
},
{
"ordinal": 22,
"name": "wizard_state_json",
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "operation_versions",
"name": "wizard_state_json"
}
}
},
{
"ordinal": 23,
"name": "change_note",
"type_info": "Text",
"origin": {
"Table": {
"table": "operation_versions",
"name": "change_note"
}
}
},
{
"ordinal": 24,
"name": "created_at!: time::OffsetDateTime",
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "operation_versions",
"name": "created_at"
}
}
},
{
"ordinal": 25,
"name": "created_by",
"type_info": "Text",
"origin": {
"Table": {
"table": "operation_versions",
"name": "created_by"
}
}
}
],
"parameters": {
"Left": [
"Text",
"Text"
]
},
"nullable": [
false,
false,
false,
false,
false,
false,
false,
false,
false,
true,
false,
false,
false,
false,
false,
false,
false,
false,
false,
true,
true,
true,
true,
true,
false,
true
]
},
"hash": "3e015fdc582e6cbbcc0e8d8863873f4e3113fd89f806fb4e0592c0200283f599"
}
@@ -0,0 +1,396 @@
{
"db_name": "PostgreSQL",
"query": "select\n w.id as workspace_id,\n w.slug as workspace_slug,\n a.id as agent_id,\n a.slug as agent_slug,\n b.tool_name,\n b.tool_title,\n coalesce(b.tool_description_override, ov.tool_description_json->>'description') as \"tool_description!\",\n o.id,\n ov.name,\n ov.display_name,\n ov.category,\n ov.protocol,\n ov.security_level,\n ov.created_at as \"operation_created_at!: time::OffsetDateTime\",\n ov.created_at as \"operation_updated_at!: time::OffsetDateTime\",\n ov.published_at as \"operation_published_at: time::OffsetDateTime\",\n ov.version,\n ov.status,\n ov.target_json,\n ov.input_schema_json,\n ov.output_schema_json,\n ov.input_mapping_json,\n ov.output_mapping_json,\n ov.execution_config_json,\n ov.tool_description_json,\n ov.samples_json,\n ov.generated_draft_json,\n ov.config_export_json,\n ov.wizard_state_json,\n ov.change_note,\n ov.created_at as \"created_at!: time::OffsetDateTime\",\n ov.created_by\n from workspaces w\n join agents a on a.workspace_id = w.id\n join published_agents pa on pa.agent_id = a.id\n join agent_operation_bindings b on b.agent_id = a.id and b.agent_version = pa.version\n join operation_versions ov on ov.operation_id = b.operation_id and ov.version = b.operation_version\n join operations o on o.id = ov.operation_id and o.workspace_id = w.id\n where w.slug = $1 and a.slug = $2 and b.enabled = true\n order by b.tool_name asc",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "workspace_id",
"type_info": "Text",
"origin": {
"Table": {
"table": "workspaces",
"name": "id"
}
}
},
{
"ordinal": 1,
"name": "workspace_slug",
"type_info": "Text",
"origin": {
"Table": {
"table": "workspaces",
"name": "slug"
}
}
},
{
"ordinal": 2,
"name": "agent_id",
"type_info": "Text",
"origin": {
"Table": {
"table": "agents",
"name": "id"
}
}
},
{
"ordinal": 3,
"name": "agent_slug",
"type_info": "Text",
"origin": {
"Table": {
"table": "agents",
"name": "slug"
}
}
},
{
"ordinal": 4,
"name": "tool_name",
"type_info": "Text",
"origin": {
"Table": {
"table": "agent_operation_bindings",
"name": "tool_name"
}
}
},
{
"ordinal": 5,
"name": "tool_title",
"type_info": "Text",
"origin": {
"Table": {
"table": "agent_operation_bindings",
"name": "tool_title"
}
}
},
{
"ordinal": 6,
"name": "tool_description!",
"type_info": "Text",
"origin": "Expression"
},
{
"ordinal": 7,
"name": "id",
"type_info": "Text",
"origin": {
"Table": {
"table": "operations",
"name": "id"
}
}
},
{
"ordinal": 8,
"name": "name",
"type_info": "Text",
"origin": {
"Table": {
"table": "operation_versions",
"name": "name"
}
}
},
{
"ordinal": 9,
"name": "display_name",
"type_info": "Text",
"origin": {
"Table": {
"table": "operation_versions",
"name": "display_name"
}
}
},
{
"ordinal": 10,
"name": "category",
"type_info": "Text",
"origin": {
"Table": {
"table": "operation_versions",
"name": "category"
}
}
},
{
"ordinal": 11,
"name": "protocol",
"type_info": "Text",
"origin": {
"Table": {
"table": "operation_versions",
"name": "protocol"
}
}
},
{
"ordinal": 12,
"name": "security_level",
"type_info": "Text",
"origin": {
"Table": {
"table": "operation_versions",
"name": "security_level"
}
}
},
{
"ordinal": 13,
"name": "operation_created_at!: time::OffsetDateTime",
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "operation_versions",
"name": "created_at"
}
}
},
{
"ordinal": 14,
"name": "operation_updated_at!: time::OffsetDateTime",
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "operation_versions",
"name": "created_at"
}
}
},
{
"ordinal": 15,
"name": "operation_published_at: time::OffsetDateTime",
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "operation_versions",
"name": "published_at"
}
}
},
{
"ordinal": 16,
"name": "version",
"type_info": "Int4",
"origin": {
"Table": {
"table": "operation_versions",
"name": "version"
}
}
},
{
"ordinal": 17,
"name": "status",
"type_info": "Text",
"origin": {
"Table": {
"table": "operation_versions",
"name": "status"
}
}
},
{
"ordinal": 18,
"name": "target_json",
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "operation_versions",
"name": "target_json"
}
}
},
{
"ordinal": 19,
"name": "input_schema_json",
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "operation_versions",
"name": "input_schema_json"
}
}
},
{
"ordinal": 20,
"name": "output_schema_json",
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "operation_versions",
"name": "output_schema_json"
}
}
},
{
"ordinal": 21,
"name": "input_mapping_json",
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "operation_versions",
"name": "input_mapping_json"
}
}
},
{
"ordinal": 22,
"name": "output_mapping_json",
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "operation_versions",
"name": "output_mapping_json"
}
}
},
{
"ordinal": 23,
"name": "execution_config_json",
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "operation_versions",
"name": "execution_config_json"
}
}
},
{
"ordinal": 24,
"name": "tool_description_json",
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "operation_versions",
"name": "tool_description_json"
}
}
},
{
"ordinal": 25,
"name": "samples_json",
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "operation_versions",
"name": "samples_json"
}
}
},
{
"ordinal": 26,
"name": "generated_draft_json",
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "operation_versions",
"name": "generated_draft_json"
}
}
},
{
"ordinal": 27,
"name": "config_export_json",
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "operation_versions",
"name": "config_export_json"
}
}
},
{
"ordinal": 28,
"name": "wizard_state_json",
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "operation_versions",
"name": "wizard_state_json"
}
}
},
{
"ordinal": 29,
"name": "change_note",
"type_info": "Text",
"origin": {
"Table": {
"table": "operation_versions",
"name": "change_note"
}
}
},
{
"ordinal": 30,
"name": "created_at!: time::OffsetDateTime",
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "operation_versions",
"name": "created_at"
}
}
},
{
"ordinal": 31,
"name": "created_by",
"type_info": "Text",
"origin": {
"Table": {
"table": "operation_versions",
"name": "created_by"
}
}
}
],
"parameters": {
"Left": [
"Text",
"Text"
]
},
"nullable": [
false,
false,
false,
false,
false,
false,
null,
false,
false,
false,
false,
false,
false,
false,
false,
true,
false,
false,
false,
false,
false,
false,
false,
false,
false,
true,
true,
true,
true,
true,
false,
true
]
},
"hash": "4699ba47e6489b840ada38a7491f071f605dabd061992cd0a567241a8de1fa0c"
}
@@ -6,37 +6,79 @@
{
"ordinal": 0,
"name": "id",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "workspaces",
"name": "id"
}
}
},
{
"ordinal": 1,
"name": "slug",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "workspaces",
"name": "slug"
}
}
},
{
"ordinal": 2,
"name": "display_name",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "workspaces",
"name": "display_name"
}
}
},
{
"ordinal": 3,
"name": "status",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "workspaces",
"name": "status"
}
}
},
{
"ordinal": 4,
"name": "settings_json",
"type_info": "Jsonb"
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "workspaces",
"name": "settings_json"
}
}
},
{
"ordinal": 5,
"name": "created_at!: time::OffsetDateTime",
"type_info": "Timestamptz"
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "workspaces",
"name": "created_at"
}
}
},
{
"ordinal": 6,
"name": "updated_at!: time::OffsetDateTime",
"type_info": "Timestamptz"
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "workspaces",
"name": "updated_at"
}
}
}
],
"parameters": {
@@ -6,42 +6,90 @@
{
"ordinal": 0,
"name": "id",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "descriptors",
"name": "id"
}
}
},
{
"ordinal": 1,
"name": "operation_id",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "descriptors",
"name": "operation_id"
}
}
},
{
"ordinal": 2,
"name": "version",
"type_info": "Int4"
"type_info": "Int4",
"origin": {
"Table": {
"table": "descriptors",
"name": "version"
}
}
},
{
"ordinal": 3,
"name": "descriptor_kind",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "descriptors",
"name": "descriptor_kind"
}
}
},
{
"ordinal": 4,
"name": "storage_ref",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "descriptors",
"name": "storage_ref"
}
}
},
{
"ordinal": 5,
"name": "source_name",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "descriptors",
"name": "source_name"
}
}
},
{
"ordinal": 6,
"name": "package_index_json",
"type_info": "Jsonb"
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "descriptors",
"name": "package_index_json"
}
}
},
{
"ordinal": 7,
"name": "created_at!: time::OffsetDateTime",
"type_info": "Timestamptz"
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "descriptors",
"name": "created_at"
}
}
}
],
"parameters": {
@@ -6,7 +6,8 @@
{
"ordinal": 0,
"name": "present!",
"type_info": "Int4"
"type_info": "Int4",
"origin": "Expression"
}
],
"parameters": {
@@ -1,64 +0,0 @@
{
"db_name": "PostgreSQL",
"query": "select\n w.id,\n w.slug,\n w.display_name,\n w.status,\n w.settings_json,\n w.created_at as \"created_at!: time::OffsetDateTime\",\n w.updated_at as \"updated_at!: time::OffsetDateTime\",\n m.role\n from memberships m\n join workspaces w on w.id = m.workspace_id\n where m.user_id = $1\n order by w.slug asc",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "id",
"type_info": "Text"
},
{
"ordinal": 1,
"name": "slug",
"type_info": "Text"
},
{
"ordinal": 2,
"name": "display_name",
"type_info": "Text"
},
{
"ordinal": 3,
"name": "status",
"type_info": "Text"
},
{
"ordinal": 4,
"name": "settings_json",
"type_info": "Jsonb"
},
{
"ordinal": 5,
"name": "created_at!: time::OffsetDateTime",
"type_info": "Timestamptz"
},
{
"ordinal": 6,
"name": "updated_at!: time::OffsetDateTime",
"type_info": "Timestamptz"
},
{
"ordinal": 7,
"name": "role",
"type_info": "Text"
}
],
"parameters": {
"Left": [
"Text"
]
},
"nullable": [
false,
false,
false,
false,
false,
false,
false,
false
]
},
"hash": "92bd2268efce8ba515b1d502287dea62bdfae0ba66100589ead3f986244b4cbb"
}
@@ -1,172 +0,0 @@
{
"db_name": "PostgreSQL",
"query": "select\n o.id,\n o.workspace_id,\n o.name,\n o.display_name,\n o.category,\n o.protocol,\n o.security_level,\n o.created_at as \"operation_created_at!: time::OffsetDateTime\",\n o.updated_at as \"operation_updated_at!: time::OffsetDateTime\",\n o.published_at as \"operation_published_at: time::OffsetDateTime\",\n ov.version,\n ov.status,\n ov.target_json,\n ov.input_schema_json,\n ov.output_schema_json,\n ov.input_mapping_json,\n ov.output_mapping_json,\n ov.execution_config_json,\n ov.tool_description_json,\n ov.samples_json,\n ov.generated_draft_json,\n ov.config_export_json,\n ov.wizard_state_json,\n ov.change_note,\n ov.created_at as \"created_at!: time::OffsetDateTime\",\n ov.created_by\n from published_operations po\n join operation_versions ov\n on ov.operation_id = po.operation_id and ov.version = po.version\n join operations o on o.id = po.operation_id\n where po.operation_id = $1",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "id",
"type_info": "Text"
},
{
"ordinal": 1,
"name": "workspace_id",
"type_info": "Text"
},
{
"ordinal": 2,
"name": "name",
"type_info": "Text"
},
{
"ordinal": 3,
"name": "display_name",
"type_info": "Text"
},
{
"ordinal": 4,
"name": "category",
"type_info": "Text"
},
{
"ordinal": 5,
"name": "protocol",
"type_info": "Text"
},
{
"ordinal": 6,
"name": "security_level",
"type_info": "Text"
},
{
"ordinal": 7,
"name": "operation_created_at!: time::OffsetDateTime",
"type_info": "Timestamptz"
},
{
"ordinal": 8,
"name": "operation_updated_at!: time::OffsetDateTime",
"type_info": "Timestamptz"
},
{
"ordinal": 9,
"name": "operation_published_at: time::OffsetDateTime",
"type_info": "Timestamptz"
},
{
"ordinal": 10,
"name": "version",
"type_info": "Int4"
},
{
"ordinal": 11,
"name": "status",
"type_info": "Text"
},
{
"ordinal": 12,
"name": "target_json",
"type_info": "Jsonb"
},
{
"ordinal": 13,
"name": "input_schema_json",
"type_info": "Jsonb"
},
{
"ordinal": 14,
"name": "output_schema_json",
"type_info": "Jsonb"
},
{
"ordinal": 15,
"name": "input_mapping_json",
"type_info": "Jsonb"
},
{
"ordinal": 16,
"name": "output_mapping_json",
"type_info": "Jsonb"
},
{
"ordinal": 17,
"name": "execution_config_json",
"type_info": "Jsonb"
},
{
"ordinal": 18,
"name": "tool_description_json",
"type_info": "Jsonb"
},
{
"ordinal": 19,
"name": "samples_json",
"type_info": "Jsonb"
},
{
"ordinal": 20,
"name": "generated_draft_json",
"type_info": "Jsonb"
},
{
"ordinal": 21,
"name": "config_export_json",
"type_info": "Jsonb"
},
{
"ordinal": 22,
"name": "wizard_state_json",
"type_info": "Jsonb"
},
{
"ordinal": 23,
"name": "change_note",
"type_info": "Text"
},
{
"ordinal": 24,
"name": "created_at!: time::OffsetDateTime",
"type_info": "Timestamptz"
},
{
"ordinal": 25,
"name": "created_by",
"type_info": "Text"
}
],
"parameters": {
"Left": [
"Text"
]
},
"nullable": [
false,
false,
false,
false,
false,
false,
false,
false,
false,
true,
false,
false,
false,
false,
false,
false,
false,
false,
false,
true,
true,
true,
true,
true,
false,
true
]
},
"hash": "94c7e7cb5068c56f6caed09c71323f5efe2beba283e317af1157e7b262cf65ce"
}
@@ -6,57 +6,123 @@
{
"ordinal": 0,
"name": "id",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "agents",
"name": "id"
}
}
},
{
"ordinal": 1,
"name": "workspace_id",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "agents",
"name": "workspace_id"
}
}
},
{
"ordinal": 2,
"name": "slug",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "agents",
"name": "slug"
}
}
},
{
"ordinal": 3,
"name": "display_name",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "agents",
"name": "display_name"
}
}
},
{
"ordinal": 4,
"name": "description",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "agents",
"name": "description"
}
}
},
{
"ordinal": 5,
"name": "status",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "agents",
"name": "status"
}
}
},
{
"ordinal": 6,
"name": "current_draft_version",
"type_info": "Int4"
"type_info": "Int4",
"origin": {
"Table": {
"table": "agents",
"name": "current_draft_version"
}
}
},
{
"ordinal": 7,
"name": "latest_published_version",
"type_info": "Int4"
"type_info": "Int4",
"origin": {
"Table": {
"table": "agents",
"name": "latest_published_version"
}
}
},
{
"ordinal": 8,
"name": "created_at!: time::OffsetDateTime",
"type_info": "Timestamptz"
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "agents",
"name": "created_at"
}
}
},
{
"ordinal": 9,
"name": "updated_at!: time::OffsetDateTime",
"type_info": "Timestamptz"
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "agents",
"name": "updated_at"
}
}
},
{
"ordinal": 10,
"name": "published_at: time::OffsetDateTime",
"type_info": "Timestamptz"
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "agents",
"name": "published_at"
}
}
}
],
"parameters": {
@@ -0,0 +1,326 @@
{
"db_name": "PostgreSQL",
"query": "select\n o.id,\n o.workspace_id,\n ov.name,\n ov.display_name,\n ov.category,\n ov.protocol,\n ov.security_level,\n ov.created_at as \"operation_created_at!: time::OffsetDateTime\",\n ov.created_at as \"operation_updated_at!: time::OffsetDateTime\",\n ov.published_at as \"operation_published_at: time::OffsetDateTime\",\n ov.version,\n ov.status,\n ov.target_json,\n ov.input_schema_json,\n ov.output_schema_json,\n ov.input_mapping_json,\n ov.output_mapping_json,\n ov.execution_config_json,\n ov.tool_description_json,\n ov.samples_json,\n ov.generated_draft_json,\n ov.config_export_json,\n ov.wizard_state_json,\n ov.change_note,\n ov.created_at as \"created_at!: time::OffsetDateTime\",\n ov.created_by\n from published_operations po\n join operation_versions ov\n on ov.operation_id = po.operation_id and ov.version = po.version\n join operations o on o.id = po.operation_id\n order by o.name asc",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "id",
"type_info": "Text",
"origin": {
"Table": {
"table": "operations",
"name": "id"
}
}
},
{
"ordinal": 1,
"name": "workspace_id",
"type_info": "Text",
"origin": {
"Table": {
"table": "operations",
"name": "workspace_id"
}
}
},
{
"ordinal": 2,
"name": "name",
"type_info": "Text",
"origin": {
"Table": {
"table": "operation_versions",
"name": "name"
}
}
},
{
"ordinal": 3,
"name": "display_name",
"type_info": "Text",
"origin": {
"Table": {
"table": "operation_versions",
"name": "display_name"
}
}
},
{
"ordinal": 4,
"name": "category",
"type_info": "Text",
"origin": {
"Table": {
"table": "operation_versions",
"name": "category"
}
}
},
{
"ordinal": 5,
"name": "protocol",
"type_info": "Text",
"origin": {
"Table": {
"table": "operation_versions",
"name": "protocol"
}
}
},
{
"ordinal": 6,
"name": "security_level",
"type_info": "Text",
"origin": {
"Table": {
"table": "operation_versions",
"name": "security_level"
}
}
},
{
"ordinal": 7,
"name": "operation_created_at!: time::OffsetDateTime",
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "operation_versions",
"name": "created_at"
}
}
},
{
"ordinal": 8,
"name": "operation_updated_at!: time::OffsetDateTime",
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "operation_versions",
"name": "created_at"
}
}
},
{
"ordinal": 9,
"name": "operation_published_at: time::OffsetDateTime",
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "operation_versions",
"name": "published_at"
}
}
},
{
"ordinal": 10,
"name": "version",
"type_info": "Int4",
"origin": {
"Table": {
"table": "operation_versions",
"name": "version"
}
}
},
{
"ordinal": 11,
"name": "status",
"type_info": "Text",
"origin": {
"Table": {
"table": "operation_versions",
"name": "status"
}
}
},
{
"ordinal": 12,
"name": "target_json",
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "operation_versions",
"name": "target_json"
}
}
},
{
"ordinal": 13,
"name": "input_schema_json",
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "operation_versions",
"name": "input_schema_json"
}
}
},
{
"ordinal": 14,
"name": "output_schema_json",
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "operation_versions",
"name": "output_schema_json"
}
}
},
{
"ordinal": 15,
"name": "input_mapping_json",
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "operation_versions",
"name": "input_mapping_json"
}
}
},
{
"ordinal": 16,
"name": "output_mapping_json",
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "operation_versions",
"name": "output_mapping_json"
}
}
},
{
"ordinal": 17,
"name": "execution_config_json",
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "operation_versions",
"name": "execution_config_json"
}
}
},
{
"ordinal": 18,
"name": "tool_description_json",
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "operation_versions",
"name": "tool_description_json"
}
}
},
{
"ordinal": 19,
"name": "samples_json",
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "operation_versions",
"name": "samples_json"
}
}
},
{
"ordinal": 20,
"name": "generated_draft_json",
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "operation_versions",
"name": "generated_draft_json"
}
}
},
{
"ordinal": 21,
"name": "config_export_json",
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "operation_versions",
"name": "config_export_json"
}
}
},
{
"ordinal": 22,
"name": "wizard_state_json",
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "operation_versions",
"name": "wizard_state_json"
}
}
},
{
"ordinal": 23,
"name": "change_note",
"type_info": "Text",
"origin": {
"Table": {
"table": "operation_versions",
"name": "change_note"
}
}
},
{
"ordinal": 24,
"name": "created_at!: time::OffsetDateTime",
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "operation_versions",
"name": "created_at"
}
}
},
{
"ordinal": 25,
"name": "created_by",
"type_info": "Text",
"origin": {
"Table": {
"table": "operation_versions",
"name": "created_by"
}
}
}
],
"parameters": {
"Left": []
},
"nullable": [
false,
false,
false,
false,
false,
false,
false,
false,
false,
true,
false,
false,
false,
false,
false,
false,
false,
false,
false,
true,
true,
true,
true,
true,
false,
true
]
},
"hash": "b531368c52ef70664dfe75fe801e52e70fc74af8e782989cf4aea3f1170d33e5"
}
@@ -6,37 +6,79 @@
{
"ordinal": 0,
"name": "id",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "auth_profiles",
"name": "id"
}
}
},
{
"ordinal": 1,
"name": "workspace_id",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "auth_profiles",
"name": "workspace_id"
}
}
},
{
"ordinal": 2,
"name": "name",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "auth_profiles",
"name": "name"
}
}
},
{
"ordinal": 3,
"name": "kind",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "auth_profiles",
"name": "kind"
}
}
},
{
"ordinal": 4,
"name": "config_json",
"type_info": "Jsonb"
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "auth_profiles",
"name": "config_json"
}
}
},
{
"ordinal": 5,
"name": "created_at!: OffsetDateTime",
"type_info": "Timestamptz"
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "auth_profiles",
"name": "created_at"
}
}
},
{
"ordinal": 6,
"name": "updated_at!: OffsetDateTime",
"type_info": "Timestamptz"
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "auth_profiles",
"name": "updated_at"
}
}
}
],
"parameters": {
@@ -1,64 +0,0 @@
{
"db_name": "PostgreSQL",
"query": "select\n id,\n protocol_version,\n initialized,\n workspace_slug,\n agent_slug,\n created_at as \"created_at!: OffsetDateTime\",\n updated_at as \"updated_at!: OffsetDateTime\",\n expires_at as \"expires_at: OffsetDateTime\"\n from mcp_transport_sessions\n where id = $1",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "id",
"type_info": "Text"
},
{
"ordinal": 1,
"name": "protocol_version",
"type_info": "Text"
},
{
"ordinal": 2,
"name": "initialized",
"type_info": "Bool"
},
{
"ordinal": 3,
"name": "workspace_slug",
"type_info": "Text"
},
{
"ordinal": 4,
"name": "agent_slug",
"type_info": "Text"
},
{
"ordinal": 5,
"name": "created_at!: OffsetDateTime",
"type_info": "Timestamptz"
},
{
"ordinal": 6,
"name": "updated_at!: OffsetDateTime",
"type_info": "Timestamptz"
},
{
"ordinal": 7,
"name": "expires_at: OffsetDateTime",
"type_info": "Timestamptz"
}
],
"parameters": {
"Left": [
"Text"
]
},
"nullable": [
false,
false,
false,
false,
false,
false,
false,
true
]
},
"hash": "bdae00d03a55ba3b6d121578b081e970ff08427f9a3dd396b649996a9131260c"
}
@@ -6,42 +6,90 @@
{
"ordinal": 0,
"name": "workspace_id",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "memberships",
"name": "workspace_id"
}
}
},
{
"ordinal": 1,
"name": "user_id",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "memberships",
"name": "user_id"
}
}
},
{
"ordinal": 2,
"name": "role",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "memberships",
"name": "role"
}
}
},
{
"ordinal": 3,
"name": "created_at!: time::OffsetDateTime",
"type_info": "Timestamptz"
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "memberships",
"name": "created_at"
}
}
},
{
"ordinal": 4,
"name": "email",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "users",
"name": "email"
}
}
},
{
"ordinal": 5,
"name": "display_name",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "users",
"name": "display_name"
}
}
},
{
"ordinal": 6,
"name": "status",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "users",
"name": "status"
}
}
},
{
"ordinal": 7,
"name": "user_created_at!: time::OffsetDateTime",
"type_info": "Timestamptz"
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "users",
"name": "created_at"
}
}
}
],
"parameters": {
@@ -1,209 +0,0 @@
{
"db_name": "PostgreSQL",
"query": "select\n w.id as workspace_id,\n w.slug as workspace_slug,\n a.id as agent_id,\n a.slug as agent_slug,\n b.tool_name,\n b.tool_title,\n coalesce(b.tool_description_override, ov.tool_description_json->>'description') as \"tool_description!\",\n o.id,\n o.name,\n o.display_name,\n o.category,\n o.protocol,\n o.security_level,\n o.created_at as \"operation_created_at!: time::OffsetDateTime\",\n o.updated_at as \"operation_updated_at!: time::OffsetDateTime\",\n o.published_at as \"operation_published_at: time::OffsetDateTime\",\n ov.version,\n ov.status,\n ov.target_json,\n ov.input_schema_json,\n ov.output_schema_json,\n ov.input_mapping_json,\n ov.output_mapping_json,\n ov.execution_config_json,\n ov.tool_description_json,\n ov.samples_json,\n ov.generated_draft_json,\n ov.config_export_json,\n ov.wizard_state_json,\n ov.change_note,\n ov.created_at as \"created_at!: time::OffsetDateTime\",\n ov.created_by\n from workspaces w\n join agents a on a.workspace_id = w.id\n join published_agents pa on pa.agent_id = a.id\n join agent_operation_bindings b on b.agent_id = a.id and b.agent_version = pa.version\n join operation_versions ov on ov.operation_id = b.operation_id and ov.version = b.operation_version\n join operations o on o.id = ov.operation_id and o.workspace_id = w.id\n where w.slug = $1 and a.slug = $2 and b.enabled = true\n order by b.tool_name asc",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "workspace_id",
"type_info": "Text"
},
{
"ordinal": 1,
"name": "workspace_slug",
"type_info": "Text"
},
{
"ordinal": 2,
"name": "agent_id",
"type_info": "Text"
},
{
"ordinal": 3,
"name": "agent_slug",
"type_info": "Text"
},
{
"ordinal": 4,
"name": "tool_name",
"type_info": "Text"
},
{
"ordinal": 5,
"name": "tool_title",
"type_info": "Text"
},
{
"ordinal": 6,
"name": "tool_description!",
"type_info": "Text"
},
{
"ordinal": 7,
"name": "id",
"type_info": "Text"
},
{
"ordinal": 8,
"name": "name",
"type_info": "Text"
},
{
"ordinal": 9,
"name": "display_name",
"type_info": "Text"
},
{
"ordinal": 10,
"name": "category",
"type_info": "Text"
},
{
"ordinal": 11,
"name": "protocol",
"type_info": "Text"
},
{
"ordinal": 12,
"name": "security_level",
"type_info": "Text"
},
{
"ordinal": 13,
"name": "operation_created_at!: time::OffsetDateTime",
"type_info": "Timestamptz"
},
{
"ordinal": 14,
"name": "operation_updated_at!: time::OffsetDateTime",
"type_info": "Timestamptz"
},
{
"ordinal": 15,
"name": "operation_published_at: time::OffsetDateTime",
"type_info": "Timestamptz"
},
{
"ordinal": 16,
"name": "version",
"type_info": "Int4"
},
{
"ordinal": 17,
"name": "status",
"type_info": "Text"
},
{
"ordinal": 18,
"name": "target_json",
"type_info": "Jsonb"
},
{
"ordinal": 19,
"name": "input_schema_json",
"type_info": "Jsonb"
},
{
"ordinal": 20,
"name": "output_schema_json",
"type_info": "Jsonb"
},
{
"ordinal": 21,
"name": "input_mapping_json",
"type_info": "Jsonb"
},
{
"ordinal": 22,
"name": "output_mapping_json",
"type_info": "Jsonb"
},
{
"ordinal": 23,
"name": "execution_config_json",
"type_info": "Jsonb"
},
{
"ordinal": 24,
"name": "tool_description_json",
"type_info": "Jsonb"
},
{
"ordinal": 25,
"name": "samples_json",
"type_info": "Jsonb"
},
{
"ordinal": 26,
"name": "generated_draft_json",
"type_info": "Jsonb"
},
{
"ordinal": 27,
"name": "config_export_json",
"type_info": "Jsonb"
},
{
"ordinal": 28,
"name": "wizard_state_json",
"type_info": "Jsonb"
},
{
"ordinal": 29,
"name": "change_note",
"type_info": "Text"
},
{
"ordinal": 30,
"name": "created_at!: time::OffsetDateTime",
"type_info": "Timestamptz"
},
{
"ordinal": 31,
"name": "created_by",
"type_info": "Text"
}
],
"parameters": {
"Left": [
"Text",
"Text"
]
},
"nullable": [
false,
false,
false,
false,
false,
false,
null,
false,
false,
false,
false,
false,
false,
false,
false,
true,
false,
false,
false,
false,
false,
false,
false,
false,
false,
true,
true,
true,
true,
true,
false,
true
]
},
"hash": "c570f37d6cbc556f763f8a05d3b225b2ba5e5694c46c84deda01f99751eec3ec"
}
@@ -6,47 +6,101 @@
{
"ordinal": 0,
"name": "id",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "secrets",
"name": "id"
}
}
},
{
"ordinal": 1,
"name": "workspace_id",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "secrets",
"name": "workspace_id"
}
}
},
{
"ordinal": 2,
"name": "name",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "secrets",
"name": "name"
}
}
},
{
"ordinal": 3,
"name": "kind",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "secrets",
"name": "kind"
}
}
},
{
"ordinal": 4,
"name": "status",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "secrets",
"name": "status"
}
}
},
{
"ordinal": 5,
"name": "current_version",
"type_info": "Int4"
"type_info": "Int4",
"origin": {
"Table": {
"table": "secrets",
"name": "current_version"
}
}
},
{
"ordinal": 6,
"name": "created_at!: time::OffsetDateTime",
"type_info": "Timestamptz"
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "secrets",
"name": "created_at"
}
}
},
{
"ordinal": 7,
"name": "updated_at!: time::OffsetDateTime",
"type_info": "Timestamptz"
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "secrets",
"name": "updated_at"
}
}
},
{
"ordinal": 8,
"name": "last_used_at: time::OffsetDateTime",
"type_info": "Timestamptz"
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "secrets",
"name": "last_used_at"
}
}
}
],
"parameters": {
@@ -6,37 +6,79 @@
{
"ordinal": 0,
"name": "id",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "workspaces",
"name": "id"
}
}
},
{
"ordinal": 1,
"name": "slug",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "workspaces",
"name": "slug"
}
}
},
{
"ordinal": 2,
"name": "display_name",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "workspaces",
"name": "display_name"
}
}
},
{
"ordinal": 3,
"name": "status",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "workspaces",
"name": "status"
}
}
},
{
"ordinal": 4,
"name": "settings_json",
"type_info": "Jsonb"
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "workspaces",
"name": "settings_json"
}
}
},
{
"ordinal": 5,
"name": "created_at!: time::OffsetDateTime",
"type_info": "Timestamptz"
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "workspaces",
"name": "created_at"
}
}
},
{
"ordinal": 6,
"name": "updated_at!: time::OffsetDateTime",
"type_info": "Timestamptz"
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "workspaces",
"name": "updated_at"
}
}
}
],
"parameters": {
@@ -6,47 +6,101 @@
{
"ordinal": 0,
"name": "id",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "secrets",
"name": "id"
}
}
},
{
"ordinal": 1,
"name": "workspace_id",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "secrets",
"name": "workspace_id"
}
}
},
{
"ordinal": 2,
"name": "name",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "secrets",
"name": "name"
}
}
},
{
"ordinal": 3,
"name": "kind",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "secrets",
"name": "kind"
}
}
},
{
"ordinal": 4,
"name": "status",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "secrets",
"name": "status"
}
}
},
{
"ordinal": 5,
"name": "current_version",
"type_info": "Int4"
"type_info": "Int4",
"origin": {
"Table": {
"table": "secrets",
"name": "current_version"
}
}
},
{
"ordinal": 6,
"name": "created_at!: time::OffsetDateTime",
"type_info": "Timestamptz"
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "secrets",
"name": "created_at"
}
}
},
{
"ordinal": 7,
"name": "updated_at!: time::OffsetDateTime",
"type_info": "Timestamptz"
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "secrets",
"name": "updated_at"
}
}
},
{
"ordinal": 8,
"name": "last_used_at: time::OffsetDateTime",
"type_info": "Timestamptz"
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "secrets",
"name": "last_used_at"
}
}
}
],
"parameters": {
@@ -6,42 +6,90 @@
{
"ordinal": 0,
"name": "id",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "invitation_tokens",
"name": "id"
}
}
},
{
"ordinal": 1,
"name": "workspace_id",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "invitation_tokens",
"name": "workspace_id"
}
}
},
{
"ordinal": 2,
"name": "email",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "invitation_tokens",
"name": "email"
}
}
},
{
"ordinal": 3,
"name": "role",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "invitation_tokens",
"name": "role"
}
}
},
{
"ordinal": 4,
"name": "status",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "invitation_tokens",
"name": "status"
}
}
},
{
"ordinal": 5,
"name": "token_hash",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "invitation_tokens",
"name": "token_hash"
}
}
},
{
"ordinal": 6,
"name": "expires_at!: time::OffsetDateTime",
"type_info": "Timestamptz"
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "invitation_tokens",
"name": "expires_at"
}
}
},
{
"ordinal": 7,
"name": "created_at!: time::OffsetDateTime",
"type_info": "Timestamptz"
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "invitation_tokens",
"name": "created_at"
}
}
}
],
"parameters": {
@@ -6,72 +6,156 @@
{
"ordinal": 0,
"name": "id",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "operations",
"name": "id"
}
}
},
{
"ordinal": 1,
"name": "workspace_id",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "operations",
"name": "workspace_id"
}
}
},
{
"ordinal": 2,
"name": "name",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "operations",
"name": "name"
}
}
},
{
"ordinal": 3,
"name": "display_name",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "operations",
"name": "display_name"
}
}
},
{
"ordinal": 4,
"name": "category",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "operations",
"name": "category"
}
}
},
{
"ordinal": 5,
"name": "protocol",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "operations",
"name": "protocol"
}
}
},
{
"ordinal": 6,
"name": "security_level",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "operations",
"name": "security_level"
}
}
},
{
"ordinal": 7,
"name": "target_json",
"type_info": "Jsonb"
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "operation_versions",
"name": "target_json"
}
}
},
{
"ordinal": 8,
"name": "status",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "operations",
"name": "status"
}
}
},
{
"ordinal": 9,
"name": "current_draft_version",
"type_info": "Int4"
"type_info": "Int4",
"origin": {
"Table": {
"table": "operations",
"name": "current_draft_version"
}
}
},
{
"ordinal": 10,
"name": "latest_published_version",
"type_info": "Int4"
"type_info": "Int4",
"origin": {
"Table": {
"table": "operations",
"name": "latest_published_version"
}
}
},
{
"ordinal": 11,
"name": "created_at!: time::OffsetDateTime",
"type_info": "Timestamptz"
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "operations",
"name": "created_at"
}
}
},
{
"ordinal": 12,
"name": "updated_at!: time::OffsetDateTime",
"type_info": "Timestamptz"
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "operations",
"name": "updated_at"
}
}
},
{
"ordinal": 13,
"name": "published_at: time::OffsetDateTime",
"type_info": "Timestamptz"
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "operations",
"name": "published_at"
}
}
}
],
"parameters": {
@@ -6,37 +6,79 @@
{
"ordinal": 0,
"name": "id",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "user_sessions",
"name": "id"
}
}
},
{
"ordinal": 1,
"name": "user_id",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "user_sessions",
"name": "user_id"
}
}
},
{
"ordinal": 2,
"name": "current_workspace_id",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "user_sessions",
"name": "current_workspace_id"
}
}
},
{
"ordinal": 3,
"name": "email",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "users",
"name": "email"
}
}
},
{
"ordinal": 4,
"name": "display_name",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "users",
"name": "display_name"
}
}
},
{
"ordinal": 5,
"name": "status",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "users",
"name": "status"
}
}
},
{
"ordinal": 6,
"name": "created_at!: OffsetDateTime",
"type_info": "Timestamptz"
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "users",
"name": "created_at"
}
}
}
],
"parameters": {
@@ -6,37 +6,79 @@
{
"ordinal": 0,
"name": "id",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "auth_profiles",
"name": "id"
}
}
},
{
"ordinal": 1,
"name": "workspace_id",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "auth_profiles",
"name": "workspace_id"
}
}
},
{
"ordinal": 2,
"name": "name",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "auth_profiles",
"name": "name"
}
}
},
{
"ordinal": 3,
"name": "kind",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "auth_profiles",
"name": "kind"
}
}
},
{
"ordinal": 4,
"name": "config_json",
"type_info": "Jsonb"
"type_info": "Jsonb",
"origin": {
"Table": {
"table": "auth_profiles",
"name": "config_json"
}
}
},
{
"ordinal": 5,
"name": "created_at!: OffsetDateTime",
"type_info": "Timestamptz"
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "auth_profiles",
"name": "created_at"
}
}
},
{
"ordinal": 6,
"name": "updated_at!: OffsetDateTime",
"type_info": "Timestamptz"
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "auth_profiles",
"name": "updated_at"
}
}
}
],
"parameters": {
@@ -6,32 +6,68 @@
{
"ordinal": 0,
"name": "secret_id",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "secret_versions",
"name": "secret_id"
}
}
},
{
"ordinal": 1,
"name": "version",
"type_info": "Int4"
"type_info": "Int4",
"origin": {
"Table": {
"table": "secret_versions",
"name": "version"
}
}
},
{
"ordinal": 2,
"name": "ciphertext",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "secret_versions",
"name": "ciphertext"
}
}
},
{
"ordinal": 3,
"name": "key_version",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "secret_versions",
"name": "key_version"
}
}
},
{
"ordinal": 4,
"name": "created_at!: time::OffsetDateTime",
"type_info": "Timestamptz"
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "secret_versions",
"name": "created_at"
}
}
},
{
"ordinal": 5,
"name": "created_by",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "secret_versions",
"name": "created_by"
}
}
}
],
"parameters": {
@@ -6,22 +6,46 @@
{
"ordinal": 0,
"name": "operation_id",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "agent_operation_bindings",
"name": "operation_id"
}
}
},
{
"ordinal": 1,
"name": "agent_id",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "agents",
"name": "id"
}
}
},
{
"ordinal": 2,
"name": "agent_slug",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "agents",
"name": "slug"
}
}
},
{
"ordinal": 3,
"name": "display_name",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "agents",
"name": "display_name"
}
}
}
],
"parameters": {
@@ -1,170 +0,0 @@
{
"db_name": "PostgreSQL",
"query": "select\n o.id,\n o.workspace_id,\n o.name,\n o.display_name,\n o.category,\n o.protocol,\n o.security_level,\n o.created_at as \"operation_created_at!: time::OffsetDateTime\",\n o.updated_at as \"operation_updated_at!: time::OffsetDateTime\",\n o.published_at as \"operation_published_at: time::OffsetDateTime\",\n ov.version,\n ov.status,\n ov.target_json,\n ov.input_schema_json,\n ov.output_schema_json,\n ov.input_mapping_json,\n ov.output_mapping_json,\n ov.execution_config_json,\n ov.tool_description_json,\n ov.samples_json,\n ov.generated_draft_json,\n ov.config_export_json,\n ov.wizard_state_json,\n ov.change_note,\n ov.created_at as \"created_at!: time::OffsetDateTime\",\n ov.created_by\n from published_operations po\n join operation_versions ov\n on ov.operation_id = po.operation_id and ov.version = po.version\n join operations o on o.id = po.operation_id\n order by o.name asc",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "id",
"type_info": "Text"
},
{
"ordinal": 1,
"name": "workspace_id",
"type_info": "Text"
},
{
"ordinal": 2,
"name": "name",
"type_info": "Text"
},
{
"ordinal": 3,
"name": "display_name",
"type_info": "Text"
},
{
"ordinal": 4,
"name": "category",
"type_info": "Text"
},
{
"ordinal": 5,
"name": "protocol",
"type_info": "Text"
},
{
"ordinal": 6,
"name": "security_level",
"type_info": "Text"
},
{
"ordinal": 7,
"name": "operation_created_at!: time::OffsetDateTime",
"type_info": "Timestamptz"
},
{
"ordinal": 8,
"name": "operation_updated_at!: time::OffsetDateTime",
"type_info": "Timestamptz"
},
{
"ordinal": 9,
"name": "operation_published_at: time::OffsetDateTime",
"type_info": "Timestamptz"
},
{
"ordinal": 10,
"name": "version",
"type_info": "Int4"
},
{
"ordinal": 11,
"name": "status",
"type_info": "Text"
},
{
"ordinal": 12,
"name": "target_json",
"type_info": "Jsonb"
},
{
"ordinal": 13,
"name": "input_schema_json",
"type_info": "Jsonb"
},
{
"ordinal": 14,
"name": "output_schema_json",
"type_info": "Jsonb"
},
{
"ordinal": 15,
"name": "input_mapping_json",
"type_info": "Jsonb"
},
{
"ordinal": 16,
"name": "output_mapping_json",
"type_info": "Jsonb"
},
{
"ordinal": 17,
"name": "execution_config_json",
"type_info": "Jsonb"
},
{
"ordinal": 18,
"name": "tool_description_json",
"type_info": "Jsonb"
},
{
"ordinal": 19,
"name": "samples_json",
"type_info": "Jsonb"
},
{
"ordinal": 20,
"name": "generated_draft_json",
"type_info": "Jsonb"
},
{
"ordinal": 21,
"name": "config_export_json",
"type_info": "Jsonb"
},
{
"ordinal": 22,
"name": "wizard_state_json",
"type_info": "Jsonb"
},
{
"ordinal": 23,
"name": "change_note",
"type_info": "Text"
},
{
"ordinal": 24,
"name": "created_at!: time::OffsetDateTime",
"type_info": "Timestamptz"
},
{
"ordinal": 25,
"name": "created_by",
"type_info": "Text"
}
],
"parameters": {
"Left": []
},
"nullable": [
false,
false,
false,
false,
false,
false,
false,
false,
false,
true,
false,
false,
false,
false,
false,
false,
false,
false,
false,
true,
true,
true,
true,
true,
false,
true
]
},
"hash": "ff7cac26067a7845a033db286befb7762b051412ac83c0238fe7ce426c02d4d4"
}
+22
View File
@@ -30,10 +30,30 @@ npx playwright test
## Требования к изменениям
- Не добавляйте функциональность вне границ Community-версии.
- Сверяйте границу и статус flow с
[`docs/capability-inventory.json`](./docs/capability-inventory.json): только
`implemented` считается готовым, а `planned`, `gap` и `blocked` не являются
разрешением заявить незавершённую функцию работающей.
- Если change меняет capability status или evidence, обновите versioned
[`docs/capability-baseline/manifest.json`](./docs/capability-baseline/manifest.json),
sanitized results и SHA-256. Нельзя вручную объявлять pass для failed, flaky,
skipped, not-run или manual-only evidence.
- Для записи результатов этапа и разделения исторического capability baseline
и текущего completion evidence следуйте
[правилам evidence разработки](./docs/development-evidence.md). Перед этапом
фиксируйте полный clean Git baseline; проверенную реализацию указывайте
отдельной ревизией.
- Не добавляйте секреты, токены, приватные адреса и локальные настройки.
- Не коммитьте `AGENTS.md`, `TASKS.md`, `.env` и временные файлы.
- Для изменений SQL-запросов обновляйте `.sqlx`, если это требуется SQLx.
- Изменение PostgreSQL schema начинается с новой append-only migration в
`crank-registry`; baseline v1 не редактируется. Выполните
`just migration-contract-check` и PostgreSQL migration integration suite.
- Для пользовательских изменений обновляйте документацию или примеры.
- Runtime environment contract изменяется только через registry
`crates/crank-config`. После изменения выполните
`just config-contract-check`; generated sections `.env.example`, Compose и
parameter reference не поддерживаются независимыми ручными таблицами.
## Границы проекта
@@ -47,3 +67,5 @@ npx playwright test
- необязательный Valkey или Redis для служебного кэша.
Функции за пределами перечисленного набора не должны попадать в этот репозиторий.
Целевые Resources, Prompts, Tasks и Load Runs допускаются только через
проверяемое изменение Inventory, guardrails, tests и документации.
Generated
+116 -53
View File
@@ -26,10 +26,14 @@ dependencies = [
"axum",
"axum-extra",
"base64",
"crank-artifacts",
"crank-community-auth",
"crank-community-mcp",
"crank-config",
"crank-core",
"crank-import",
"crank-mapping",
"crank-metrics",
"crank-observability",
"crank-registry",
"crank-runtime",
@@ -39,7 +43,9 @@ dependencies = [
"metrics",
"metrics-util",
"opentelemetry",
"opentelemetry-proto",
"opentelemetry_sdk",
"prost",
"rand 0.10.2",
"reqwest 0.12.28",
"serde",
@@ -55,6 +61,7 @@ dependencies = [
"tracing",
"tracing-opentelemetry",
"tracing-subscriber",
"url",
"uuid",
]
@@ -216,9 +223,9 @@ checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53"
[[package]]
name = "aws-lc-rs"
version = "1.17.3"
version = "1.18.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "00bdb5da18dac48ca2cc7cd4a98e533e8635a58e2361d13a1a4ee3888e0d72f1"
checksum = "b281d307588d634de920874890732659e2e7672f72b5e10e81badc1a8a83621e"
dependencies = [
"aws-lc-sys",
"zeroize",
@@ -226,9 +233,9 @@ dependencies = [
[[package]]
name = "aws-lc-sys"
version = "0.43.0"
version = "0.45.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43103168cc76fe62678a375e722fc9cb3a0146159ac5828bc4f0dfd755c2224c"
checksum = "9bff6c3b54fad79a2e60b8102caf565819711497c1f5f092f49508e2f5c31b27"
dependencies = [
"cc",
"cmake",
@@ -256,6 +263,7 @@ dependencies = [
"matchit",
"memchr",
"mime",
"multer",
"percent-encoding",
"pin-project-lite",
"serde_core",
@@ -516,9 +524,9 @@ checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724"
[[package]]
name = "chacha20"
version = "0.10.1"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d524456ba66e72eb8b115ff89e01e497f8e6d11d78b70b1aa13c0fbd97540a81"
checksum = "65c35e4b699c7e15ccbe7ee35c005e4fc0a278d22238a2857e6ce2dadeda1b06"
dependencies = [
"cfg-if",
"cpufeatures 0.3.0",
@@ -576,15 +584,6 @@ dependencies = [
"tokio-util",
]
[[package]]
name = "concurrent-queue"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ca0197aee26d1ae37445ee532fefce43251d24cc7c166799f4d46817f1d3973"
dependencies = [
"crossbeam-utils",
]
[[package]]
name = "cookie"
version = "0.18.1"
@@ -654,6 +653,7 @@ version = "0.3.1"
dependencies = [
"async-trait",
"axum",
"base64",
"crank-core",
"crank-metrics",
"crank-trace",
@@ -670,6 +670,17 @@ dependencies = [
"tracing-subscriber",
]
[[package]]
name = "crank-artifacts"
version = "0.3.1"
dependencies = [
"libc",
"rand 0.10.2",
"sha2 0.10.9",
"thiserror 2.0.18",
"url",
]
[[package]]
name = "crank-community-auth"
version = "0.3.1"
@@ -696,6 +707,7 @@ dependencies = [
"axum",
"base64",
"crank-adapter-rest",
"crank-artifacts",
"crank-core",
"crank-mapping",
"crank-metrics",
@@ -723,6 +735,16 @@ dependencies = [
"uuid",
]
[[package]]
name = "crank-config"
version = "0.3.1"
dependencies = [
"serde",
"serde_json",
"sha2 0.10.9",
"url",
]
[[package]]
name = "crank-core"
version = "0.3.1"
@@ -734,6 +756,7 @@ dependencies = [
"thiserror 2.0.18",
"time",
"tokio",
"uuid",
]
[[package]]
@@ -746,7 +769,9 @@ dependencies = [
"serde",
"serde_json",
"serde_yaml",
"sha2 0.10.9",
"thiserror 2.0.18",
"url",
]
[[package]]
@@ -795,17 +820,18 @@ dependencies = [
"tracing-opentelemetry",
"tracing-subscriber",
"url",
"uuid",
]
[[package]]
name = "crank-registry"
version = "0.3.1"
dependencies = [
"crank-artifacts",
"crank-core",
"crank-mapping",
"crank-schema",
"crank-test-support",
"rand 0.10.2",
"serde",
"serde_json",
"sha2 0.10.9",
@@ -826,6 +852,7 @@ dependencies = [
"base64",
"crank-adapter-rest",
"crank-core",
"crank-import",
"crank-mapping",
"crank-metrics",
"crank-schema",
@@ -874,7 +901,10 @@ dependencies = [
name = "crank-trace"
version = "0.3.1"
dependencies = [
"crank-core",
"opentelemetry",
"tracing",
"tracing-opentelemetry",
"tracing-subscriber",
]
@@ -948,9 +978,9 @@ dependencies = [
[[package]]
name = "ctutils"
version = "0.4.2"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7d5515a3834141de9eafb9717ad39eea8247b5674e6066c404e8c4b365d2a29e"
checksum = "12218662ccac37d9714f2191252553d67585b97df8c839cbe49e0bde278dc865"
dependencies = [
"cmov",
]
@@ -1088,6 +1118,15 @@ dependencies = [
"serde",
]
[[package]]
name = "encoding_rs"
version = "0.8.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "75030f3c4f45dafd7586dd6780965a8c7e8e285a5ecb86713e63a79c5b2766f3"
dependencies = [
"cfg-if",
]
[[package]]
name = "endian-type"
version = "0.1.2"
@@ -1107,7 +1146,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb"
dependencies = [
"libc",
"windows-sys 0.61.2",
"windows-sys 0.52.0",
]
[[package]]
@@ -1122,11 +1161,10 @@ dependencies = [
[[package]]
name = "event-listener"
version = "5.4.1"
version = "5.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e13b66accf52311f30a0db42147dadea9850cb48cd070028831ae5f5d4b856ab"
checksum = "5a23add41df1562121a9393cb065eab5146a1242410f23a644851e90cfd669d2"
dependencies = [
"concurrent-queue",
"parking",
"pin-project-lite",
]
@@ -1384,9 +1422,9 @@ checksum = "e629b9b98ef3dd8afe6ca2bd0f89306cec16d43d907889945bc5d6687f2f13c7"
[[package]]
name = "h2"
version = "0.4.15"
version = "0.4.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6cb093c84e8bd9b188d4c4a8cb6579fc016968d14c99882163cd3ff402a4f155"
checksum = "a9f37a958b41b3b19ee2707c06439c0e9e547e847223eb791ecb0cb821c65e27"
dependencies = [
"atomic-waker",
"bytes",
@@ -1633,7 +1671,7 @@ dependencies = [
"libc",
"percent-encoding",
"pin-project-lite",
"socket2 0.6.4",
"socket2 0.5.10",
"tokio",
"tower-service",
"tracing",
@@ -2018,7 +2056,9 @@ dependencies = [
"async-trait",
"axum",
"base64",
"crank-artifacts",
"crank-community-mcp",
"crank-config",
"crank-core",
"crank-mapping",
"crank-observability",
@@ -2116,6 +2156,16 @@ version = "0.3.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a"
[[package]]
name = "mime_guess"
version = "2.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7c44f8e672c00fe5308fa235f821cb4198414e1c77935c1ab6948d3fd78550e"
dependencies = [
"mime",
"unicase",
]
[[package]]
name = "miniz_oxide"
version = "0.8.9"
@@ -2136,6 +2186,23 @@ dependencies = [
"windows-sys 0.61.2",
]
[[package]]
name = "multer"
version = "3.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "83e87776546dc87511aa5ee218730c92b666d7264ab6ed41f9d215af9cd5224b"
dependencies = [
"bytes",
"encoding_rs",
"futures-util",
"http",
"httparse",
"memchr",
"mime",
"spin",
"version_check",
]
[[package]]
name = "nibble_vec"
version = "0.1.0"
@@ -2568,7 +2635,7 @@ dependencies = [
"quinn-udp",
"rustc-hash",
"rustls",
"socket2 0.6.4",
"socket2 0.5.10",
"thiserror 2.0.18",
"tokio",
"tracing",
@@ -2607,9 +2674,9 @@ dependencies = [
"cfg_aliases",
"libc",
"once_cell",
"socket2 0.6.4",
"socket2 0.5.10",
"tracing",
"windows-sys 0.61.2",
"windows-sys 0.52.0",
]
[[package]]
@@ -2836,6 +2903,7 @@ dependencies = [
"hyper-util",
"js-sys",
"log",
"mime_guess",
"percent-encoding",
"pin-project-lite",
"quinn",
@@ -2934,7 +3002,7 @@ dependencies = [
"errno",
"libc",
"linux-raw-sys 0.4.15",
"windows-sys 0.59.0",
"windows-sys 0.52.0",
]
[[package]]
@@ -2947,14 +3015,14 @@ dependencies = [
"errno",
"libc",
"linux-raw-sys 0.12.1",
"windows-sys 0.61.2",
"windows-sys 0.52.0",
]
[[package]]
name = "rustls"
version = "0.23.41"
version = "0.23.46"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6b92b125634d9b795e7beca796cc790df15a7fb38323bf3196fda83292d06b1f"
checksum = "48e13bd8c0e9365c43cfa5c9e8f9ad49d3c8444926c9aac819e0e4dc503c8fdf"
dependencies = [
"aws-lc-rs",
"log",
@@ -3006,7 +3074,7 @@ dependencies = [
"security-framework",
"security-framework-sys",
"webpki-root-certs",
"windows-sys 0.61.2",
"windows-sys 0.52.0",
]
[[package]]
@@ -3017,9 +3085,9 @@ checksum = "f87165f0995f63a9fbeea62b64d10b4d9d8e78ec6d7d51fb2125fda7bb36788f"
[[package]]
name = "rustls-webpki"
version = "0.103.13"
version = "0.103.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "61c429a8649f110dddef65e2a5ad240f747e85f7758a6bccc7e5777bd33f756e"
checksum = "4443b1b6d39d7c663139942cc4558262562d2c84123bb6282ef177009ca8b281"
dependencies = [
"aws-lc-rs",
"ring",
@@ -3451,9 +3519,9 @@ dependencies = [
[[package]]
name = "spin"
version = "0.9.8"
version = "0.9.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67"
checksum = "3763264f6b73151db08c50ff20d7d8a0b8796e021cdea7ceedad07b80155fa0e"
dependencies = [
"lock_api",
]
@@ -3474,8 +3542,6 @@ dependencies = [
[[package]]
name = "sqlx-core"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05b44e85bf579a8eeb4ceaa77a3a523baf2bf0e9bac7e40f405d537b5d2d5ccb"
dependencies = [
"base64",
"bytes",
@@ -3868,9 +3934,9 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20"
[[package]]
name = "tokio"
version = "1.52.3"
version = "1.53.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8fc7f01b389ac15039e4dc9531aa973a135d7a4135281b12d7c1bc79fd57fffe"
checksum = "ce3335fa71841cda333a58d7615b03901380ecf09d59b3296d21f8bbac0dde4e"
dependencies = [
"bytes",
"libc",
@@ -4104,6 +4170,12 @@ version = "1.20.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6f5e870be6c3b371b77fe0ee0bafb859fa4964b4404c27de1d380043c4dda20"
[[package]]
name = "unicase"
version = "2.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dbc4bc3a9f746d862c45cb89d705aa10f187bb96c76001afab07a0d35ce60142"
[[package]]
name = "unicode-bidi"
version = "0.3.18"
@@ -4369,9 +4441,9 @@ dependencies = [
[[package]]
name = "webpki-roots"
version = "1.0.8"
version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf85cb06032201fa7c6f829d7db5a7e5aa45bcc0655327713065f6f0576731bf"
checksum = "1c1c8e20c8a38fc7d65abfe74eb7280e4aa667e8eec8bc84996d2874810748d9"
dependencies = [
"rustls-pki-types",
]
@@ -4404,7 +4476,7 @@ version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22"
dependencies = [
"windows-sys 0.61.2",
"windows-sys 0.52.0",
]
[[package]]
@@ -4490,15 +4562,6 @@ dependencies = [
"windows-targets 0.52.6",
]
[[package]]
name = "windows-sys"
version = "0.59.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b"
dependencies = [
"windows-targets 0.52.6",
]
[[package]]
name = "windows-sys"
version = "0.61.2"
+8 -2
View File
@@ -4,6 +4,7 @@ members = [
"apps/mcp-server",
"crates/crank-community-auth",
"crates/crank-community-mcp",
"crates/crank-config",
"crates/crank-core",
"crates/crank-import",
"crates/crank-schema",
@@ -15,9 +16,14 @@ members = [
"crates/crank-test-support",
"crates/crank-trace",
"crates/crank-adapter-rest",
"crates/crank-artifacts",
]
exclude = ["vendor/sqlx-core"]
resolver = "3"
[patch.crates-io]
sqlx-core = { path = "vendor/sqlx-core" }
[workspace.package]
edition = "2024"
license = "AGPL-3.0-only"
@@ -28,7 +34,7 @@ publish = false
[workspace.dependencies]
aes-gcm = "0.10"
argon2 = "0.5"
axum = "0.8"
axum = { version = "0.8", features = ["multipart"] }
axum-extra = { version = "0.12", features = ["cookie"] }
base64 = "0.22"
hkdf = "0.12"
@@ -41,7 +47,7 @@ opentelemetry_sdk = { version = "0.32.1", default-features = false, features = [
percent-encoding = "2"
prost = "0.14"
rand = "0.10"
reqwest = { version = "0.12", default-features = false, features = ["cookies", "json", "rustls-tls"] }
reqwest = { version = "0.12", default-features = false, features = ["cookies", "json", "multipart", "rustls-tls"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
serde_yaml = "0.9"
+17
View File
@@ -165,6 +165,20 @@ docker compose up -d --build
- PostgreSQL как основное хранилище;
- необязательный Valkey или Redis для служебного кэша.
Текущие и целевые возможности фиксируются в
[`docs/capability-inventory.json`](docs/capability-inventory.json). Статус
`implemented` означает реализованный flow; `planned`, `gap` и `blocked` не
считаются готовностью. MCP Resources, Prompts, фоновые Tasks и встроенные Load
Runs сейчас перечислены только как planned target и не выдаются за работающие
возможности этой версии.
Проверенный snapshot текущих UI, Admin API и MCP flows находится в
[`docs/capability-baseline/manifest.json`](docs/capability-baseline/manifest.json).
Он связывает inventory, обязательные surfaces, taxonomy, manual checklist и
sanitized results точными SHA-256. Полный baseline pass требует сочетания
`implemented + automated + pass`; skipped, flaky, not-run и manual-only не
становятся pass.
## Структура проекта
```text
@@ -268,7 +282,10 @@ npx playwright test
- [Production checklist](docs/production-checklist.md)
- [Troubleshooting](docs/troubleshooting.md)
- [Настройки запуска](docs/runtime-config.md)
- [Machine schema runtime-конфигурации](docs/schemas/runtime-config.schema.json)
- [Английский README](docs/en/README.md)
- [Capability Inventory](docs/capability-inventory.json)
- [Capability Baseline](docs/capability-baseline/manifest.json)
## Участие в разработке
@@ -0,0 +1,227 @@
---
title: '2.10 — Привязать security schemes через безопасный import API'
type: feature
created: '2026-09-13'
status: done
baseline_revision: c562d5368d810d1730f5d392ae0c6c106143a97c
review_loop_iteration: 0
followup_review_recommended: true
context:
- '_bmad-output/project-context.md'
- '_bmad-output/implementation-artifacts/epic-2-context.md'
- '_bmad-output/implementation-artifacts/1-10-lifecycle-auth-profiles-machine-keys.md'
- '_bmad-output/implementation-artifacts/spec-2-9-http-contract.md'
warnings: [oversized]
deferred: []
---
<intent-contract>
## Intent
**Problem:** Importer не сохраняет security schemes и effective authentication requirements. Import API создаёт Operations с пустым auth binding, а raw upload сохраняется до security analysis.
**Approach:** Сохранить typed security metadata и OR/AND semantics в IR/preview; предоставить workspace-scoped выбор совместимого Auth Profile и повторную проверку binding перед атомарным Apply. Сохранять только ссылки, исключая credential values из artifacts и результатов.
## Boundaries & Constraints
**Always:** Выполнить AC Story 2.10 для OpenAPI 3.0/3.1 и Swagger 2.0; сохранить детерминированность и исторический replay, workspace isolation, typed error/redaction и существующие architectural boundaries. `sprint-status.yaml` принадлежит оркестратору: не записывать и не откатывать его. Статусы спринта не являются доказательством проверки AC.
**Never:** Raw credentials, ciphertext или secret hashes в source snapshot/IR/plan/export/diagnostics; silent collapse OR в AND или выбор первой alternative; создание actionable Draft при unresolved required auth; попутная реализация mapping UI Story 2.11 или полной qualification Story 2.14. Независимый отзыв/status/tombstone lifecycle Auth Profile остаётся вне scope Story 2.10; доступность binding определяется существованием и workspace профиля, а также существующим lifecycle связанных Secrets согласно решению ниже.
<frozen-after-approval>
### Решение пользователя: доступность Auth Profile (2026-09-13)
Для Story 2.10 формулировка «отозванный Auth Profile» означает недоступный binding вследствие недоступности любого необходимого связанного Secret по существующим правилам active/current version. Это не независимый отзыв профиля. Preview проверяет существование профиля, его принадлежность текущему workspace, совместимость и доступность необходимых Secrets; Apply повторяет авторитетную проверку в registry transaction до создания Draft, включая изменение состояния после preview.
При общем Secret профилей A и B отключение Secret отклоняет оба binding. При активном Secret с доступной текущей версией оба binding допустимы при выполнении остальных проверок. Независимое отключение только A при сохранении B не требуется и не реализуется в этой истории: status/tombstone и управление отзывом Auth Profile остаются отложенными. Negative test с disabled Secret доказывает только недоступность credential, а не независимый отзыв профиля. Отсутствующие/удалённые и foreign-workspace profile refs отклоняются без раскрытия foreign metadata.
</frozen-after-approval>
## I/O & Edge-Case Matrix
| Scenario | Input / State | Expected Output / Behavior | Error Handling |
|----------|---------------|---------------------------|----------------|
| Schemes | Bearer/basic/API key header/query/cookie | Typed names, locations и format hints без credentials | Unsupported mapping явно отражён |
| Effective security | Global rule, operation override, explicit no-auth | Сохраняется specification inheritance/override | Invalid requirement не становится actionable |
| Alternatives | OR и AND requirements | Все OR alternatives сохраняются; unsupported AND блокирует affected candidate | Finding объясняет потерянную semantics |
| Binding | Compatible current-workspace profile либо unresolved | Только typed reference; unresolved required auth блокирует Apply | Foreign/missing reference не раскрывает metadata |
| Credential availability | Профиль отсутствует/удалён, принадлежит другому workspace либо любой необходимый связанный Secret недоступен по существующим правилам active/current version | Reference отклоняется при preview и повторно в transaction Apply до Draft | Ошибка не раскрывает metadata foreign profile |
| Shared Secret | Профили A и B используют один Secret | При активном Secret с доступной текущей версией оба binding допустимы при выполнении остальных проверок; отключение общего Secret блокирует оба | Независимый отзыв только A не входит в scope и не заявляется как проверенный |
| Secret source | Literal в example/default/extension/userinfo/header | Redacted finding; raw value отсутствует в persisted/observable output | Проверка до записи source artifact |
| Stale source | Security source изменён после preview | Digest mismatch, новый preview и подтверждение mappings | Нет mutation |
</intent-contract>
## Code Map
- `crates/crank-import/src/rest/model.rs` — `NormalizedIr`, `NormalizedOperation`, `RestImportOperation`, `ImportOperationCandidate` пока не содержат security metadata. `NORMALIZER_VERSION` / `PROJECTION_VERSION` равны v4. `NormalizedIr.source_tree` сохраняет весь canonical document.
- `crates/crank-import/src/rest/{openapi3.rs,swagger2.rs}` — `parse_document_v2` не читает security schemes/requirements; здесь начинается parser → IR путь.
- `crates/crank-import/src/rest/{normalize.rs,normalize_projection.rs,payload.rs}` — `canonicalize`, `operation_from_normalized`, `candidate_from_operation` проводят semantics через IR к candidate.
- `crates/crank-import/src/rest/metadata_findings.rs` — `collect_secret_like_examples` только добавляет warning для example/examples, не удаляя raw value. `normalize_finding_validation.rs` повторяет узкую эвристику.
- `apps/admin-api/src/service/imports.rs:54` — после upload validation вызывается `put_registered` до parse; raw source записывается с sensitivity Internal. `preview_openapi_import` сохраняет job/digests; `create_openapi_import_with_locale` перепроверяет verified source/preview. При создании Operation `auth_profile_ref` задаётся `None`; `openapi_application_key` пока не включает mappings.
- `apps/admin-api/src/service/imports/{job_contract.rs,external_references.rs}` — исторический replay и immutable external snapshots; защита source должна распространяться на external inputs.
- `apps/admin-api/src/{app.rs,dto.rs}` — существующие `POST /imports/openapi/preview` и `POST /imports/openapi/{job_id}/create`, preview/create payloads; выбора auth alternative/profile пока нет.
- `crates/crank-registry/src/postgres/import_job.rs` — `apply_import_job_transaction` является атомарной границей создания Operations; финальная auth revalidation нужна здесь, иначе service lookup оставляет race.
- `crates/crank-core/src/auth.rs` — `AuthProfile` имеет identity/workspace/kind/config/timestamps, но не status. `AuthConfig` поддерживает Bearer/Basic/ApiKeyHeader/ApiKeyQuery, без cookie.
- `crates/crank-core/src/operation.rs` — `ExecutionConfig.auth_profile_ref` допускает один `AuthProfileId`; AND из нескольких schemes не поддержан текущим runtime.
- `crates/crank-runtime/src/auth.rs` — `ResolvedAuth::{from_profile,apply}`; существующие trusted credential injection rules необходимо сохранить.
- `apps/admin-api/src/service.rs` — `validate_registry_operation_in_workspace` и `resolve_operation_auth`; `crates/crank-registry/src/postgres/auth.rs` — workspace-scoped profile lookup.
- `_bmad-output/implementation-artifacts/1-10-lifecycle-auth-profiles-machine-keys.md:223` и `_bmad-output/implementation-artifacts/deferred-work.md:17` — отдельный status/tombstone lifecycle Auth Profile явно отложен; существующая проверка active Secret не равна отзыву отдельного профиля.
## Tasks & Acceptance
**Execution (intent gap разрешён пользователем 2026-09-13; реализация выполняется при повторном запуске оркестратора):**
- [x] `crates/crank-import/src/rest/{model.rs,openapi3.rs,swagger2.rs,normalize.rs,normalize_projection.rs,payload.rs}` — добавить typed schemes и effective requirements, deterministic OR/AND projection, source coverage и blockers без credentials.
- [x] `crates/crank-import/src/rest/{metadata_findings.rs,normalize_finding_validation.rs}`, `apps/admin-api/src/service/imports.rs`, `apps/admin-api/src/service/imports/external_references.rs` — проверять secret-bearing inputs до persistence и исключить raw values из IR/diagnostics.
- [x] `apps/admin-api/src/{app.rs,dto.rs,service/imports.rs}` — предоставить выбор/revalidation alternative и совместимых workspace profiles, включить mappings в application digest, сохранять только profile reference.
- [x] `crates/crank-registry/src/postgres/import_job.rs` — проверять существование и workspace профиля, совместимость binding и доступность всех необходимых связанных Secrets по существующим правилам active/current version в transaction перед mutation; независимый lifecycle профиля не добавлять.
- [x] `apps/admin-api/src/service/imports/job_contract.rs` — сохранить исторический v4 replay при изменении normalizer/projection contract.
- [x] `crates/crank-import/tests/`, `apps/admin-api/tests/integration/openapi_import/`, `crates/crank-runtime/tests/` — покрыть все строки матрицы и AC fixtures/negative tests, в том числе race, foreign refs, redaction canaries и digest mismatch.
- [x] `docs/openapi-import.md` — описать security API, поддерживаемые mappings, blockers и revalidation; не обещать непроверенные semantics.
**Acceptance Criteria (из Story 2.10; семантика доступности binding уточнена решением пользователя 2026-09-13):**
**Given** OpenAPI 3.x `components.securitySchemes` или Swagger 2.0 `securityDefinitions`
**When** importer анализирует schemes
**Then** bearer HTTP, basic HTTP и API key в header/query/cookie преобразуются в typed security metadata
**And** source names, locations и применимые format hints сохраняются без credential values.
**Given** global security requirement
**When** Operation не задаёт собственный override
**Then** candidate наследует global requirement согласно specification semantics
**And** operation-level requirement или explicit no-auth корректно заменяет global rule.
**Given** security requirement с несколькими alternatives
**When** source определяет OR-семантику
**Then** preview сохраняет все допустимые alternatives и позволяет выбрать поддерживаемый вариант
**And** importer не объединяет alternatives в ложное обязательное AND.
**Given** security requirement, требующий несколько schemes одновременно
**When** source определяет AND-семантику
**Then** candidate сохраняет составное требование, если runtime contract его поддерживает
**And** иначе affected Operation получает blocker finding с перечислением несохраняемой semantics.
**Given** bearer, basic или API-key requirement
**When** формируется preview
**Then** администратору предлагается выбрать совместимый Auth Profile из текущего workspace либо оставить явный unresolved binding
**And** unresolved required authentication блокирует actionable Draft/Apply для affected Operation.
**Given** выбранный Auth Profile
**When** создаётся candidate или ImportPlan
**Then** сохраняется только typed `AuthProfileRef` и scheme metadata
**And** raw secret, password, token, API key, hash или encrypted ciphertext не копируется в source snapshot, IR, findings, plan или Operation export.
**Given** OAuth2, OpenID Connect, mutual TLS или неизвестная scheme
**When** release scope не поддерживает автоматическое mapping
**Then** importer создаёт explicit manual-configuration/unsupported finding
**And** не извлекает client secret, token URL credential, example token или vendor-extension secret.
**Given** OpenAPI document содержит literal credential в example, default, extension, URL userinfo или header example
**When** security analysis выполняется
**Then** значение не используется как credential и создаёт redacted secret finding
**And** raw value не появляется в preview UI, logs, traces, metrics или diagnostics.
**Given** Auth Profile отсутствует/удалён, принадлежит другому workspace либо любой необходимый связанный Secret недоступен по существующим правилам active/current version
**When** preview либо Apply проверяет binding
**Then** reference отклоняется до создания Draft
**And** ошибка не раскрывает metadata foreign profile.
**Given** профили A и B используют один активный Secret с доступной текущей версией
**When** проверяются binding этих профилей
**Then** оба binding допустимы при выполнении остальных проверок
**And** после отключения общего Secret оба binding отклоняются до создания Draft; тест с disabled Secret не является доказательством независимого отзыва Auth Profile, который остаётся вне scope.
**Given** исходная security scheme изменилась после preview
**When** старый ImportPlan пытаются применить к новому source
**Then** plan digest mismatch блокирует Apply
**And** требуется новый preview и явное подтверждение Auth Profile mappings.
**Given** security scheme test suite
**When** выполняются fixtures и negative tests
**Then** покрыты global/operation overrides, no-auth, bearer/basic/API-key locations, OR/AND requirements, OAuth/OpenID/unknown schemes, secret-like examples, missing/deleted и cross-workspace profile refs, недоступные Secrets по правилам active/current version и блокировка обоих профилей при отключении общего Secret
**And** redaction tests и `just verify` проходят.
## Spec Change Log
- 2026-09-13 — Пользователь выбрал вариант 1: сохранить существующий lifecycle Secrets; независимый отзыв Auth Profile остаётся отложенным. Уточнены intent, AC, матрица и transaction predicate. Запись Auto Run Result ниже сохранена как историческое свидетельство остановки; требуемое в ней решение принято. Статус переустанавливает оркестратор.
## Review Triage Log
### 2026-09-27 — Review pass
- verdicts: 13 findings — high 9, medium 1, low 2, false 1, maybe-false 0
- findings:
- `[high]` `[patch]` camelCase `clientSecret`/`accessToken` не распознаются — `sensitive_key` приводит регистр, но сравнивает только целые слова или `_`-границы; literal может остаться в source. Исправить распознавание и покрыть canary. В `security_literals.rs` добавлена проверка camelCase; canary-тест проходит.
- `[high]` `[patch]` числовые/boolean credential values пропускаются — `walk` обрабатывает лишь строки, хотя значения под чувствительным ключом тоже являются literal; заменить их безопасным redaction marker и покрыть тестом. В `walk` скаляры под sensitive context заменяются `[REDACTED]`; null оставлен без finding, тест проходит.
- `[high]` `[patch]` JWT в generic example пропускается — путь и значение могут не содержать текстовых маркеров; добавить строгую JWT-эвристику и canary-тест. `is_jwt` проверяет форму и длины сегментов; JWT canary и benign `1.2.3` проверены.
- `[high]` `[patch]` secret в query URL пропускается — проверяется userinfo, но не ключи query вроде `access_token`; отклонять такой source до persistence и проверять отсутствие canary в IR. Добавлена проверка чувствительных query keys с redaction; OAuth URL fixture проверяет и безопасный, и секретный вариант.
- `[high]` `[patch]` Cookie/Set-Cookie example пропускается — response header example без слова `token` не получает sensitive context; покрыть header/cookie примеры. Добавлен контекст Cookie/Set-Cookie и canary-тест.
- `[medium]` `[patch]` credential как ключ объекта example остаётся в pointer/source tree — для распознаваемого token-shaped ключа значение редактируется, а имя ключа нет; редактировать весь example object и указывать безопасный parent pointer. JWT-shaped key приводит к redaction всего example object с parent pointer; canary-тест проходит.
- `[low]` `[patch]` transaction-level auth error не локализуется — `From<RegistryError>` всегда выдаёт английский текст, хотя `create_openapi_import` знает locale; преобразовать этот вариант на границе import API. В import Apply вариант RegistryError преобразуется с locale; Ru integration test проходит.
- `[low]` `[patch]` документация не уточняет zero-based `alternative_index` — код индексирует `Vec` напрямую; добавить одну фразу. В `docs/openapi-import.md` добавлено явное правило нулевой индексации.
- `[high]` `[patch]` AC-12 не проверяет перечисленные literal paths — тесты не содержат camelCase/JWT/query URL/Cookie; добавить canary-fixtures к исправлению сканера. `scanner_redacts_contextual_credentials_non_strings_jwts_and_cookie_examples` и OAuth URL fixture добавлены и прошли.
- `[high]` `[patch]` тест redacted bearer hint не проверяет safe finding — сейчас проверяется только отсутствие canary в IR; добавить проверку source location и сериализованного finding. Проверяются точный safe pointer, код finding и сериализованный report без canary.
- `[high]` `[patch]` edge-case слой подтвердил camelCase/JWT пропуски — тот же `sensitive_key` и `looks_secret_value`, что в строках 1 и 3; покрывается тем же исправлением. Закрыто общей scanner patch и canary-тестами строк 1 и 3.
- `[high]` `[patch]` query API-key parameter с произвольным example пропускается — имя параметра и example обходятся отдельно, `in: query` не включает sensitive context; связать context с именем `api_key` и добавить тест. Сканер использует sibling `name`/`in` для example context; canary-тест проходит.
- `[false]` `[reject]` API rejection вместо redacted preview finding не означает отсутствия finding — `normalize.rs` создаёт `secret_literal_redacted` в IR, а admin API до persistence отклоняет опасный upload с безопасной ошибкой; контракт не требует сохранять небезопасный source ради preview.
## Design Notes
Pure importer хранит schemes/requirements, но не читает Auth Profiles или Secrets. Workspace-safe lookup принадлежит admin service, authoritative проверка непосредственно перед созданием Draft — registry transaction. Текущий единичный profile reference позволяет выдавать blocker для unsupported AND: это прямо предусмотрено AC и не требует расширения runtime до составного auth.
Проверка literal credentials до `put_registered` позволяет отвергать небезопасный source без изменения bytes принятого immutable snapshot; AC не требует успешного preview для документа с literal secret. Новые формы import API и внутреннее представление alternatives сами по себе являются инженерными решениями, не основанием для остановки.
## Verification
- `cargo test -p crank-import --all-targets` — PASS: 99 tests; security metadata, inheritance, OR/AND, Swagger 2.0, redaction, JWT/cookie/query credential canaries.
- `cargo test -p admin-api --test integration -- --test-threads=1` — PASS: 88 integration tests; upload, mappings, workspace isolation, Secret availability, replay и digest drift.
- `cargo test -p crank-runtime --all-targets` — PASS: 83 tests; существующий trusted auth path не менялся.
- `cargo test -p crank-registry --test integration import_auth_binding -- --test-threads=1` — PASS: 5 tests; transaction revalidation и no-partial-Draft race.
- `just rust-code-health`, `just dependencies`, `just fmt-check`, `just clippy` — PASS после разнесения oversized модулей, обновления `rustls` в `Cargo.lock` до 0.23.45 и исправления формата теста.
- `just verify` — PASS: полный workspace gate завершился с exit 0 (через pipe/`tee`). Первый прогон остановился на file-size gate, второй — на устаревшей advisory `RUSTSEC-2026-0285`, третий — на `clippy`, четвёртый — на `rustfmt`, пятый и шестой запуск упали в `crank-artifacts::real_process_write_limit_leaves_no_partial_final_blob` из-за перенаправления stdout в обычный файл: дочерний тест ставит `RLIMIT_FSIZE=1`, и его собственный вывод попал под лимит. Это воспроизведено отдельной командой с редиректом; без него scoped target прошёл 7/7. Финальный полный прогон через pipe/`tee` прошёл; код теста не менялся.
- После review patch повторный `just verify` — PASS (exit 0): все workspace tests, fmt, clippy, code health и dependency gate. Включены новые scanner canaries и исправленная локализация ошибки binding.
**Matrix test audit:** Schemes, Effective security и Alternatives покрыты `crates/crank-import/tests/security_schemes.rs`; Binding, Credential availability, Shared Secret и Stale source — `apps/admin-api/tests/integration/openapi_import/security.rs` и `crates/crank-registry/tests/integration/import_auth_binding.rs`; Secret source — importer scanner tests, primary/external upload integration и telemetry canary tests. Все указанные story tests и полный workspace gate запускались и прошли.
## Auto Run Result
Status: blocked
Blocking condition: intent gap
Планирование остановлено до ATDD и реализации. Код, migration, API и tests не изменены. Обновлён локальный epic context и подготовлен этот spec; прохождение AC не заявляется. `sprint-status.yaml` не изменялся и не откатывался.
### Требуемое решение
Определить, что означает «Auth Profile был отозван» в AC Story 2.10:
- Проверять только существующий lifecycle связанных Secrets (active/current version), считая профиль недоступным при отключении credential, а отдельный lifecycle профиля оставить отложенным.
- Ввести в этой истории независимый отзыв Auth Profile (status/tombstone и соответствующее управление), который запрещает только этот profile ref, сохраняя работоспособность другого профиля с тем же Secret.
Наблюдаемое различие: профили A и B с одним активным Secret. Независимый отзыв A должен отклонить binding A и сохранить binding B; отключение общего Secret отклоняет оба. `AuthProfile` сейчас не имеет status, а отдельный lifecycle явно отложен в Story 1.10 и deferred-work. Поэтому нельзя считать negative test с disabled Secret доказательством независимого отзыва Auth Profile, либо расширить lifecycle/domain/storage scope молча.
Основание остановки: правило `step-02-plan.md`, INSTRUCTIONS 5: “Multiple defensible readings of the intent that lead to observably different outcomes, with nothing in the intent to select between them, are an intent gap — do not resolve one by picking a reading.”
Это пробел нормативного scope, а не внешнее действие оператора. AC не требуют покупки домена, выдачи реального API key или действий в vendor console; `awaiting-operator` к данной остановке не применяется.
### 2026-09-27 — повторный Auto Run Result
Status: done
Реализованы typed security schemes и OR/AND requirements в IR/preview, безопасный выбор Auth Profile при import, повторная проверка Secret/profile в транзакции перед созданием Draft, redaction credential literals и совместимость исторического replay. Небезопасный source отклоняется до сохранения artifacts. Документирован API contract; ATDD сценарии и матрица AC покрыты тестами.
Изменённые файлы:
- `crates/crank-import/src/rest/{model,security,security_literals,normalize*,openapi3*,swagger2*,payload,legacy_payload,mod}.rs` — typed security metadata, effective requirements, безопасный scan/redaction и совместимый replay.
- `apps/admin-api/src/{dto,error,service/imports*}.rs` — API выбора Auth Profile, проверка mapping и безопасная локализованная ошибка.
- `crates/crank-registry/src/{model,error,lib,postgres/import_job}.rs` — повторная transaction-level проверка binding до создания Draft.
- `crates/crank-import/tests/security_schemes.rs`, `apps/admin-api/tests/integration/openapi_import*`, `apps/admin-api/tests/integration/openapi_source*`, `crates/crank-registry/tests/integration*` — acceptance и regression tests, включая race, replay и credential canaries.
- `docs/openapi-import.md` — контракт import API и индексация alternatives.
- `_bmad-output/test-artifacts/atdd-{checklist,summary}-2-10-security-schemes-secrets.*` — RED/GREEN и результаты проверок.
- `Cargo.lock` — обновление `rustls` для dependency gate.
- Этот spec — матрица AC, Review Triage Log, Verification и итоговый статус.
Review: 13 findings; 12 исправлены (9 high, 1 medium, 2 low), отложенных нет. Один finding отклонён как false: он утверждал, что API rejection не показывает redacted preview finding, но pure importer формирует `secret_literal_redacted`, а Admin API намеренно отклоняет опасный upload до сохранения source; AC не требует сохранять небезопасный source ради preview. Открытых intent gaps нет. Повторный `just verify` прошёл с exit 0 после всех review patches; importer 99/99, Admin API integration 88/88, registry auth-binding 5/5, runtime 83/83.
Остаточный риск: распознавание произвольных секретов без явного контекста остаётся эвристическим. Рекомендована независимая follow-up проверка scanner coverage при дальнейшем расширении форматов. Внешних действий оператора для AC этой истории нет.
@@ -0,0 +1,137 @@
---
stepsCompleted: ['step-01-preflight-and-context', 'step-02-generation-mode', 'step-03-test-strategy', 'step-04c-aggregate', 'step-05-validate-and-complete']
lastStep: 'step-05-validate-and-complete'
lastSaved: '2026-09-27'
storyId: '2.10'
storyKey: '2-10-security-schemes-secrets'
storyFile: '_bmad-output/implementation-artifacts/spec-2-10-security-schemes-secrets.md'
atddChecklistPath: '_bmad-output/test-artifacts/atdd-checklist-2-10-security-schemes-secrets.md'
generatedTestFiles:
- 'crates/crank-import/tests/security_schemes.rs'
- 'apps/admin-api/tests/integration/openapi_import/security.rs'
- 'apps/admin-api/tests/integration/openapi_import/external_references.rs'
- 'crates/crank-registry/tests/integration/import_auth_binding.rs'
inputDocuments:
- '_bmad-output/implementation-artifacts/spec-2-10-security-schemes-secrets.md'
- '_bmad-output/implementation-artifacts/epic-2-context.md'
- '_bmad-output/implementation-artifacts/1-10-lifecycle-auth-profiles-machine-keys.md'
- '_bmad-output/implementation-artifacts/spec-2-9-http-contract.md'
- '_bmad-output/test-artifacts/test-design-epic-2.md'
- '_bmad-output/test-artifacts/test-design-qa.md'
- '_bmad/tea/config.yaml'
- 'Cargo.toml'
- 'apps/admin-api/Cargo.toml'
- 'crates/crank-import/Cargo.toml'
- 'crates/crank-runtime/Cargo.toml'
acceptanceCriteria:
- id: AC-1
idSource: generated
text: 'Given OpenAPI 3.x components.securitySchemes или Swagger 2.0 securityDefinitions; When importer анализирует schemes; Then bearer HTTP, basic HTTP и API key в header/query/cookie преобразуются в typed security metadata; And source names, locations и применимые format hints сохраняются без credential values.'
- id: AC-2
idSource: generated
text: 'Given global security requirement; When Operation не задаёт собственный override; Then candidate наследует global requirement согласно specification semantics; And operation-level requirement или explicit no-auth корректно заменяет global rule.'
- id: AC-3
idSource: generated
text: 'Given security requirement с несколькими alternatives; When source определяет OR-семантику; Then preview сохраняет все допустимые alternatives и позволяет выбрать поддерживаемый вариант; And importer не объединяет alternatives в ложное обязательное AND.'
- id: AC-4
idSource: generated
text: 'Given security requirement, требующий несколько schemes одновременно; When source определяет AND-семантику; Then candidate сохраняет составное требование, если runtime contract его поддерживает; And иначе affected Operation получает blocker finding с перечислением несохраняемой semantics.'
- id: AC-5
idSource: generated
text: 'Given bearer, basic или API-key requirement; When формируется preview; Then администратору предлагается выбрать совместимый Auth Profile из текущего workspace либо оставить явный unresolved binding; And unresolved required authentication блокирует actionable Draft/Apply для affected Operation.'
- id: AC-6
idSource: generated
text: 'Given выбранный Auth Profile; When создаётся candidate или ImportPlan; Then сохраняется только typed AuthProfileRef и scheme metadata; And raw secret, password, token, API key, hash или encrypted ciphertext не копируется в source snapshot, IR, findings, plan или Operation export.'
- id: AC-7
idSource: generated
text: 'Given OAuth2, OpenID Connect, mutual TLS или неизвестная scheme; When release scope не поддерживает автоматическое mapping; Then importer создаёт explicit manual-configuration/unsupported finding; And не извлекает client secret, token URL credential, example token или vendor-extension secret.'
- id: AC-8
idSource: generated
text: 'Given OpenAPI document содержит literal credential в example, default, extension, URL userinfo или header example; When security analysis выполняется; Then значение не используется как credential и создаёт redacted secret finding; And raw value не появляется в preview UI, logs, traces, metrics или diagnostics.'
- id: AC-9
idSource: generated
text: 'Given Auth Profile отсутствует/удалён, принадлежит другому workspace либо любой необходимый связанный Secret недоступен по существующим правилам active/current version; When preview либо Apply проверяет binding; Then reference отклоняется до создания Draft; And ошибка не раскрывает metadata foreign profile.'
- id: AC-10
idSource: generated
text: 'Given профили A и B используют один активный Secret с доступной текущей версией; When проверяются binding этих профилей; Then оба binding допустимы при выполнении остальных проверок; And после отключения общего Secret оба binding отклоняются до создания Draft; тест с disabled Secret не является доказательством независимого отзыва Auth Profile, который остаётся вне scope.'
- id: AC-11
idSource: generated
text: 'Given исходная security scheme изменилась после preview; When старый ImportPlan пытаются применить к новому source; Then plan digest mismatch блокирует Apply; And требуется новый preview и явное подтверждение Auth Profile mappings.'
- id: AC-12
idSource: generated
text: 'Given security scheme test suite; When выполняются fixtures и negative tests; Then покрыты global/operation overrides, no-auth, bearer/basic/API-key locations, OR/AND requirements, OAuth/OpenID/unknown schemes, secret-like examples, missing/deleted и cross-workspace profile refs, недоступные Secrets по правилам active/current version и блокировка обоих профилей при отключении общего Secret; And redaction tests и `just verify` проходят.'
---
# ATDD Checklist — Story 2.10: Security Schemes and Secrets
## Preflight
- **Approval/source:** the approved, frozen acceptance contract is the Story 2.10 spec. Its recorded 2026-09-13 decision defines binding availability through the existing active/current Secret rules; it does not add independent Auth Profile revocation.
- **Story identity:** ID `2.10`; canonical key `2-10-security-schemes-secrets` from the spec filename.
- **Stack and harness:** repository is Rust backend plus an existing UI. This story's approved scope is API/parser/runtime, so primary level is Rust unit/integration/API contract; UI/E2E belongs to Story 2.11. `cargo test` and `just verify` are configured.
- **Risk and design:** P0, FR-10, E2-R2 (score 9), seed scenarios `2.10-API-A` and `2.10-API-B`. The epic test design calls for preserving security semantics and rejecting unsupported/foreign bindings, with secret canaries absent from API/IR/plan/export surfaces.
- **Implementation observation:** security metadata now flows through typed IR/preview; Admin API accepts profile mappings, rejects unsafe literals before persistence, and validates binding availability within Apply. Current verification evidence is recorded below and is scoped to the commands actually run.
- **Generation mode:** backend/API source analysis. No browser recording; no Story 2.11 UI scaffolds.
- **Contract boundary:** implementation added focused tests in the existing importer, Admin API and registry harnesses. No skipped or placeholder tests are used as evidence.
## Acceptance Criteria Snapshot and Scenario Mapping
AC IDs below are deterministic IDs assigned in source order because the spec does not declare `AC-<n>` IDs. The exact snapshot is also stored in frontmatter.
| Scenario | AC | Test level / priority | Primary reusable test path | Required scenario and current status |
|---|---|---|---|---|
| 2.10-API-A1 | AC-1 | Importer unit / P0 | `security_alternatives_preserve_or_and_and_preview_exposes_typed_schemes`; `swagger2_security_definitions_project_basic_and_api_key_header_and_query` in `crates/crank-import/tests/security_schemes.rs` | **PASS** in importer suite: OpenAPI typed scheme metadata and Swagger 2.0 basic/API-key locations are projected. |
| 2.10-API-A2 | AC-2 | Importer unit / P0 | `security_requirements_inherit_and_operation_empty_array_disables_auth` in `crates/crank-import/tests/security_schemes.rs` | **PASS** in importer suite: global inheritance, multi-scheme operation requirement, explicit empty override. |
| 2.10-API-A3 | AC-3 | Importer + preview API / P0 | `security_alternatives_preserve_or_and_and_preview_exposes_typed_schemes`; `selected_supported_or_and_explicit_no_auth_alternatives_apply_without_collapsing_semantics` in `apps/admin-api/tests/integration/openapi_import/security.rs` | **PASS** in importer and Admin API integration suites: OR alternatives remain distinct and selected supported option applies. |
| 2.10-API-A4 | AC-4 | Importer unit + preview API / P0 | `security_requirements_inherit_and_operation_empty_array_disables_auth`; `security_alternatives_preserve_or_and_and_preview_exposes_typed_schemes` in `crates/crank-import/tests/security_schemes.rs` | **PASS** in importer suite: AND composition is retained and unsupported composition becomes a blocker. |
| 2.10-API-B1 | AC-5 | Admin API integration / P0 | `auth_profiles_are_workspace_scoped_revalidated_and_shared_secret_availability_is_current`; `selected_supported_or_and_explicit_no_auth_alternatives_apply_without_collapsing_semantics` in `apps/admin-api/tests/integration/openapi_import/security.rs` | **PASS** in Admin API integration suite: choices are workspace-scoped, supported mapping applies, unresolved required auth is blocked. |
| 2.10-API-B2 | AC-6 | Importer + API/registry integration / P0 | `ac_6_never_copies_literal_secret_canaries_into_normalized_ir` in `crates/crank-import/tests/security_schemes.rs`; `auth_profiles_are_workspace_scoped_revalidated_and_shared_secret_availability_is_current` in `apps/admin-api/tests/integration/openapi_import/security.rs`; `no_auth_requires_no_profile_reference` in `crates/crank-registry/tests/integration/import_auth_binding.rs` | **PASS** in importer, Admin API and registry suites. Tests assert canary/ciphertext/hash absence from IR/serialized Operation and no profile ref for no-auth. |
| 2.10-API-A5 | AC-7 | Importer unit / P0 | `unsupported_security_schemes_create_blockers_without_copying_credentials` in `crates/crank-import/tests/security_schemes.rs` | **PASS** in importer suite: OAuth/OIDC/mTLS/unknown produce explicit unsupported/blocker findings without credential extraction. |
| 2.10-API-A6 | AC-8 | Importer + Admin API integration / P0 | `scanner_finds_examples_defaults_extensions_and_url_userinfo_without_values`; `scanner_redacts_contextual_credentials_non_strings_jwts_and_cookie_examples`; `scanner_keeps_version_examples_and_null_credential_defaults`; `unsafe_bearer_format_canary_is_redacted_and_not_projected` in `crates/crank-import/tests/security_schemes.rs`; `literal_credential_is_rejected_before_any_import_artifact_or_job_is_created` in Admin API `security.rs`; `external_literal_credential_is_rejected_before_snapshot_persistence` in `external_references.rs` | **PASS** in importer/Admin API suites: canaries redact from findings and literal credentials are rejected before primary or external source persistence. |
| 2.10-API-B3 | AC-9 | Admin API + registry integration / P0 | `auth_profiles_are_workspace_scoped_revalidated_and_shared_secret_availability_is_current` in Admin API `security.rs`; `missing_and_foreign_profiles_have_the_same_safe_error_and_insert_no_operation`; `apply_rejects_kind_parameter_and_missing_current_version_before_mutation` in `crates/crank-registry/tests/integration/import_auth_binding.rs` | **PASS** in Admin API and registry suites. Foreign/missing errors are indistinguishable and no Draft mutation occurs. |
| 2.10-API-B4 | AC-10 | Admin API + registry integration / P0 | `auth_profiles_are_workspace_scoped_revalidated_and_shared_secret_availability_is_current` in Admin API `security.rs`; `apply_accepts_current_compatible_profile_and_shared_secret_then_rejects_disabled_secret` in `crates/crank-registry/tests/integration/import_auth_binding.rs` | **PASS** in Admin API and registry suites. Shared Secret permits both profiles while active and rejects them when disabled; independent profile revocation is not asserted. |
| 2.10-API-B5 | AC-11 | Admin API integration / P0 | `changed_security_scheme_after_preview_fails_digest_check_without_draft_mutation` in `apps/admin-api/tests/integration/openapi_import/security.rs` | **PASS** in Admin API integration suite: stale digest blocks Apply without creating Draft. |
| 2.10-API-GATE | AC-12 | Suite / P0 | `cargo test -p crank-import --all-targets`; `cargo test -p admin-api --test integration openapi_import -- --test-threads=1`; `cargo test -p crank-registry --test integration import_auth_binding -- --test-threads=1`; `just verify` | Importer, Admin API and registry suites **PASS**; final `just verify` **PASS**. Earlier failed attempts were repaired; the unrelated artifact subprocess failure was caused by redirecting stdout to a regular file and disappeared when the gate ran through a pipe. |
### Reuse inventory (not a claim of AC coverage)
- Importer behavior patterns: `crates/crank-import/tests/security_schemes.rs`, `swagger2_contract.rs`, `unit.rs`, `preview.rs`, and `http_replay.rs`.
- API/import persistence patterns: `apps/admin-api/tests/integration/openapi_import/http_contract.rs` and `external_references.rs`.
- Runtime auth injection patterns: `crates/crank-runtime/tests/stages.rs` and `integration/confirmation.rs`.
- The AC rows point to the actual tests observed in the current implementation. Importer, Admin API and registry test runs are complete; broad `just verify` has now passed.
## Test Strategy and TDD State
- **Primary level:** importer unit tests for pure OpenAPI semantics; Admin API + registry integration for profile scope, secret availability, transaction revalidation, and no-Draft guarantees; runtime tests only for unchanged trusted credential injection behavior.
- **Duplicate coverage:** do not duplicate existing general reference, source digest, redaction, or runtime-auth tests. Add assertions only for the Story 2.10 security contract and its import boundary.
- **Fixture requirements:** OAS 3.0, OAS 3.1, and Swagger 2.0 docs; deterministic secret canaries in example/default/extension/userinfo/header-example positions; workspace A/B profiles sharing one Secret; active/current and disabled Secret states; source mutation after preview. Never use customer credentials.
- **Assertion order:** primary assertion checks the exact promised value/state (typed scheme/effective requirement, blocker, absence of Draft, or digest rejection) before broad serialized-object checks. Setup results from an unimplemented endpoint remain opaque until the criterion assertion.
- **TDD record:** the initial two active tests genuinely failed before implementation (historical RED below); after implementation both now pass. No skipped tests are counted as RED or GREEN evidence.
## Handoff
- Story-level importer, Admin API and registry evidence is recorded above; all scenarios mapped to AC-1..AC-11 passed in those suites.
- AC-12 is satisfied: full `just verify` completed successfully. Earlier gate failures were fixed or traced to the output-redirection harness.
- Runtime suite: root reports `cargo test -p crank-runtime --all-targets` passed (83 tests).
## Historical RED Evidence (Before Implementation)
`cargo test -p crank-import --test normalization_details -- --nocapture` failed as intended: 11 passed, 2 failed, 0 ignored. AC-1 failed because `WorkspaceBearer` was absent from operation projection; AC-6 failed because `atdd-secret-canary-2-10` appeared in normalized IR. These failures were fixed by implementation and the tests now pass.
## Verification Results (Current Worktree)
| Command | Result | Interpretation |
|---|---|---|
| `cargo test -p crank-import --all-targets` | **PASS** — 99 tests passed, 0 failed, 0 ignored. | Includes security normalization, OpenAPI/Swagger mapping and redaction cases after review fixes. |
| `cargo test -p admin-api --test integration openapi_import -- --test-threads=1` | **PASS** — 24 passed, 0 failed, 0 ignored; 64 filtered out. | Includes mapping choices, literal-credential rejection, workspace/profile revalidation, shared Secret availability and digest drift. |
| `cargo test -p crank-registry --test integration import_auth_binding -- --test-threads=1` | **PASS** — 5 passed, 0 failed, 0 ignored; 95 filtered out. | Includes transaction-level profile, Secret state, no-auth and no-partial-mutation checks. |
| `cargo test -p crank-runtime --all-targets` | **PASS** — 83 passed, 0 failed, 0 ignored (result reported by root). | Runtime regression suite is green; no security scheme behavior is implemented in the runtime crate. |
| `just verify` | **PASS** — full workspace gate finished with exit 0, including fmt, clippy, dependencies and all workspace tests. | Earlier failures were corrected; artifact subprocess needed a pipe instead of regular-file stdout. |
| `git diff --check` | **PASS**. | No whitespace errors in current diff. |
## Completion Summary
- Story ID/key: `2.10` / `2-10-security-schemes-secrets`.
- AC snapshot: 12 source criteria mapped one-to-one to scenario IDs.
- Tests: security-specific tests were added to existing importer, Admin API and registry suites. The scenario table maps each AC to exact test names and package-level evidence.
- Current evidence after review fixes: importer 99/99, Admin API integration 88/88, registry auth-binding integration 5/5. Historical RED is retained separately; repeat full `just verify` passed.
@@ -0,0 +1,81 @@
{
"story_id": "2.10",
"story_key": "2-10-security-schemes-secrets",
"story_file": "_bmad-output/implementation-artifacts/spec-2-10-security-schemes-secrets.md",
"checklist": "_bmad-output/test-artifacts/atdd-checklist-2-10-security-schemes-secrets.md",
"tdd_phase": "GREEN",
"acceptance_criteria_total": 12,
"active_red_tests": 0,
"historical_red_tests": [
{
"scenario": "2.10-API-A1",
"acceptance_criterion": "AC-1",
"test": "security_schemes::ac_1_projects_openapi_security_scheme_identity_onto_operation_metadata",
"result": "FAIL_BEFORE_IMPLEMENTATION_THEN_PASS",
"observed": "operation projection does not contain WorkspaceBearer"
},
{
"scenario": "2.10-API-B2",
"acceptance_criterion": "AC-6",
"test": "security_schemes::ac_6_never_copies_literal_secret_canaries_into_normalized_ir",
"result": "FAIL_BEFORE_IMPLEMENTATION_THEN_PASS",
"observed": "normalized IR contains atdd-secret-canary-2-10"
}
],
"pending_acceptance_criteria": [],
"generated_test_files": [
"crates/crank-import/tests/security_schemes.rs",
"apps/admin-api/tests/integration/openapi_import/security.rs",
"apps/admin-api/tests/integration/openapi_import/external_references.rs",
"crates/crank-registry/tests/integration/import_auth_binding.rs"
],
"existing_suite_baseline": {
"command": "cargo test -p crank-import --all-targets",
"result": "PASS",
"passed": 99,
"failed": 0,
"ignored": 0,
"timing": "current implementation"
},
"other_passed_suites": [
{
"command": "cargo test -p admin-api --test integration openapi_import -- --test-threads=1",
"result": "PASS",
"passed": 24,
"failed": 0,
"ignored": 0,
"filtered_out": 64
},
{
"command": "cargo test -p crank-registry --test integration import_auth_binding -- --test-threads=1",
"result": "PASS",
"passed": 5,
"failed": 0,
"ignored": 0,
"filtered_out": 95
},
{
"command": "cargo test -p crank-runtime --all-targets",
"result": "PASS",
"passed": 83,
"failed": 0,
"ignored": 0,
"reported_by": "root"
}
],
"other_checks": [
{
"command": "just verify",
"result": "PASS",
"detail": "Repeat full workspace gate after review fixes exited 0; earlier file-size, advisory, fmt/clippy and stdout-redirection failures were resolved."
},
{
"command": "git diff --check",
"result": "PASS"
}
],
"all_tests_skipped": false,
"expected_to_fail": false,
"e2e_tests": 0,
"note": "Historical RED evidence is preserved; all active acceptance tests and the final just verify gate pass. No skipped scaffold is counted as RED."
}
+13 -2
View File
@@ -10,15 +10,23 @@ version.workspace = true
name = "admin-api"
path = "src/main.rs"
[[bin]]
name = "crank-migrate"
path = "src/bin/crank-migrate.rs"
[dependencies]
async-trait = "0.1"
argon2.workspace = true
axum.workspace = true
axum-extra.workspace = true
base64.workspace = true
crank-community-auth = { path = "../../crates/crank-community-auth" }
crank-artifacts = { path = "../../crates/crank-artifacts" }
crank-config = { path = "../../crates/crank-config" }
crank-core = { path = "../../crates/crank-core" }
crank-import = { path = "../../crates/crank-import" }
crank-mapping = { path = "../../crates/crank-mapping" }
crank-metrics = { path = "../../crates/crank-metrics" }
crank-observability = { path = "../../crates/crank-observability" }
crank-registry = { path = "../../crates/crank-registry" }
crank-runtime = { path = "../../crates/crank-runtime" }
@@ -32,18 +40,21 @@ sha2.workspace = true
sqlx.workspace = true
thiserror.workspace = true
time.workspace = true
tokio = { workspace = true, features = ["fs"] }
tokio = { workspace = true, features = ["fs", "time"] }
tracing.workspace = true
tracing-subscriber.workspace = true
url.workspace = true
uuid.workspace = true
[dev-dependencies]
async-trait = "0.1"
crank-community-mcp = { path = "../../crates/crank-community-mcp" }
crank-test-support = { path = "../../crates/crank-test-support" }
metrics.workspace = true
metrics-util = "0.20.4"
opentelemetry.workspace = true
opentelemetry-proto.workspace = true
opentelemetry_sdk.workspace = true
prost.workspace = true
reqwest.workspace = true
serial_test = "3"
tower.workspace = true
+49 -50
View File
@@ -1,68 +1,67 @@
FROM rust:1.96.1-bookworm AS deps
WORKDIR /app
COPY Cargo.toml Cargo.lock ./
COPY .sqlx ./.sqlx
COPY apps/admin-api/Cargo.toml apps/admin-api/Cargo.toml
COPY apps/mcp-server/Cargo.toml apps/mcp-server/Cargo.toml
COPY crates/crank-core/Cargo.toml crates/crank-core/Cargo.toml
COPY crates/crank-schema/Cargo.toml crates/crank-schema/Cargo.toml
COPY crates/crank-mapping/Cargo.toml crates/crank-mapping/Cargo.toml
COPY crates/crank-registry/Cargo.toml crates/crank-registry/Cargo.toml
COPY crates/crank-runtime/Cargo.toml crates/crank-runtime/Cargo.toml
COPY crates/crank-adapter-rest/Cargo.toml crates/crank-adapter-rest/Cargo.toml
RUN mkdir -p \
apps/admin-api/src \
apps/mcp-server/src \
crates/crank-core/src \
crates/crank-schema/src \
crates/crank-mapping/src \
crates/crank-registry/src \
crates/crank-runtime/src \
crates/crank-adapter-rest/src \
&& printf 'fn main() {}\n' > apps/admin-api/src/main.rs \
&& printf 'fn main() {}\n' > apps/mcp-server/src/main.rs \
&& printf 'pub fn placeholder() {}\n' > crates/crank-core/src/lib.rs \
&& printf 'pub fn placeholder() {}\n' > crates/crank-schema/src/lib.rs \
&& printf 'pub fn placeholder() {}\n' > crates/crank-mapping/src/lib.rs \
&& printf 'pub fn placeholder() {}\n' > crates/crank-registry/src/lib.rs \
&& printf 'pub fn placeholder() {}\n' > crates/crank-runtime/src/lib.rs \
&& printf 'pub fn placeholder() {}\n' > crates/crank-adapter-rest/src/lib.rs
RUN --mount=type=cache,target=/usr/local/cargo/registry \
--mount=type=cache,target=/usr/local/cargo/git/db \
--mount=type=cache,target=/app/target \
SQLX_OFFLINE=true cargo build --release -p admin-api
FROM rust:1.96.1-bookworm AS builder
WORKDIR /app
COPY Cargo.toml Cargo.lock ./
COPY vendor ./vendor
COPY .sqlx ./.sqlx
COPY apps ./apps
COPY crates ./crates
RUN --mount=type=cache,target=/usr/local/cargo/registry \
--mount=type=cache,target=/usr/local/cargo/git/db \
--mount=type=cache,target=/app/target \
RUN --mount=type=cache,id=crank-admin-cargo-registry,target=/usr/local/cargo/registry \
--mount=type=cache,id=crank-admin-cargo-git,target=/usr/local/cargo/git/db \
--mount=type=cache,id=crank-admin-target,target=/app/target \
SQLX_OFFLINE=true cargo build --release -p admin-api \
&& cp /app/target/release/admin-api /tmp/admin-api
&& cp /app/target/release/admin-api /tmp/admin-api \
&& cp /app/target/release/crank-migrate /tmp/crank-migrate
FROM debian:bookworm-slim
# Trusted CA bundle source for the package stage. The slim image ships no CA
# certificates, so the HTTPS Debian mirror can only be reached after seeding
# them; the Rust base image provides the bundle without rebuilding the app.
FROM rust:1.96.1-bookworm AS ca-bundle
RUN apt-get update \
&& apt-get install -y --no-install-recommends ca-certificates curl \
&& rm -rf /var/lib/apt/lists/*
RUN test -s /etc/ssl/certs/ca-certificates.crt
# Package stage: install the security-refreshed Debian packages, then assemble
# the minimal runtime rootfs. Kept independent of the Rust builder so it can be
# built and reviewed on its own.
FROM debian:bookworm-slim AS runtime-rootfs
ARG CRANK_SECURITY_REFRESH
COPY --from=ca-bundle /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
COPY scripts/prepare-runtime-rootfs.sh /usr/local/bin/prepare-runtime-rootfs
RUN sed -i 's|http://deb.debian.org|https://deb.debian.org|g' /etc/apt/sources.list.d/debian.sources \
&& apt-get update \
&& apt-get install -y --no-install-recommends --only-upgrade \
base-files libc6 libgcc-s1 gcc-12-base bash dash libtinfo6 libselinux1 libpcre2-8-0 coreutils \
&& rm -rf /var/lib/apt/lists/* \
&& dpkg --compare-versions "$(dpkg-query -W -f='${Version}' libpcre2-8-0)" ge '10.42-1+deb12u2' \
&& sh /usr/local/bin/prepare-runtime-rootfs /runtime \
&& echo "crank-security-refresh=${CRANK_SECURITY_REFRESH}"
COPY apps/admin-api/docker-entrypoint.sh /runtime/usr/local/bin/crank-admin-entrypoint
COPY scripts/docker-http-healthcheck.sh /runtime/usr/local/bin/crank-http-healthcheck
RUN chmod 0755 /runtime/usr/local/bin/crank-admin-entrypoint \
&& chmod 0755 /runtime/usr/local/bin/crank-http-healthcheck
FROM scratch
COPY --from=ca-bundle /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
COPY --from=runtime-rootfs /runtime /
COPY --from=builder /tmp/admin-api /usr/local/bin/admin-api
COPY --from=builder /tmp/crank-migrate /usr/local/bin/crank-migrate
ENV PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
ENV CRANK_ADMIN_BIND=0.0.0.0:3001
ENV CRANK_STORAGE_ROOT=/var/lib/crank/storage
WORKDIR /app
COPY --from=builder /tmp/admin-api /usr/local/bin/admin-api
ENV CRANK_ADMIN_BIND=0.0.0.0:3001
EXPOSE 3001
ENTRYPOINT ["/usr/local/bin/crank-admin-entrypoint"]
CMD ["admin-api"]
+36
View File
@@ -0,0 +1,36 @@
#!/bin/sh
set -eu
# Docker creates a fresh named volume as root:root 0755. The artifact store
# deliberately rejects that mode: before starting the API, provision exactly
# the private root its pinned-directory checks require.
provision_storage_root() {
storage_root="${CRANK_STORAGE_ROOT:-/var/lib/crank/storage}"
case "$storage_root" in
/*) ;;
*)
echo "CRANK_STORAGE_ROOT must be an absolute path" >&2
exit 64
;;
esac
if [ -L "$storage_root" ] || { [ -e "$storage_root" ] && [ ! -d "$storage_root" ]; }; then
echo "CRANK_STORAGE_ROOT must be a directory, not a symlink or file" >&2
exit 64
fi
umask 077
mkdir -p -- "$storage_root"
chmod 0700 -- "$storage_root"
}
# The dedicated init service calls this entrypoint as `crank-storage-init`; it
# provisions the artifact root and exits without running a server binary.
if [ "${1:-}" = "crank-storage-init" ]; then
provision_storage_root
exit 0
fi
if [ "${1:-}" = "admin-api" ]; then
provision_storage_root
fi
exec "$@"
@@ -0,0 +1,49 @@
//! Read-only database observation for the local Playwright harness.
//! This example is never packaged as an application endpoint or executable.
#[tokio::main]
async fn main() {
let workspace = std::env::args().nth(1).expect("test workspace argument");
assert!(
!workspace.is_empty()
&& workspace.len() <= 128
&& workspace
.bytes()
.all(|byte| byte.is_ascii_alphanumeric() || b"_-".contains(&byte)),
"invalid test workspace argument"
);
let config = crank_config::parse_migrator(
crank_config::ConfigSource::from_os_for_migrator().expect("test configuration source"),
)
.expect("test database configuration");
let database = config.database;
let options = if let Some(url) = &database.url {
url.expose_secret()
.parse::<sqlx::postgres::PgConnectOptions>()
.expect("test database URL")
} else {
sqlx::postgres::PgConnectOptions::new()
.host(&database.host)
.port(database.port)
.database(&database.database)
.username(&database.username)
.password(database.password.expose_secret())
};
let pool = sqlx::postgres::PgPoolOptions::new()
.max_connections(1)
.connect_with(options)
.await
.expect("test database connection");
let (sources, operations, versions): (i64, i64, i64) = sqlx::query_as(
"select
(select count(*) from artifact_sources where workspace_id=$1),
(select count(*) from operations where workspace_id=$1),
(select count(*) from operation_versions v
join operations o on o.id=v.operation_id where o.workspace_id=$1)",
)
.bind(workspace)
.fetch_one(&pool)
.await
.expect("test storage observation");
println!("{{\"sources\":{sources},\"operations\":{operations},\"versions\":{versions}}}");
}
+143 -9
View File
@@ -1,10 +1,16 @@
use axum::{
Router, middleware,
Router,
extract::DefaultBodyLimit,
middleware,
routing::{delete, get, post},
};
use crank_artifacts::MAX_ARTIFACT_BYTES;
use crate::{
auth::{require_session, require_workspace_session},
auth::{
enforce_browser_security, require_csrf_for_browser_mutations, require_session,
require_workspace_session,
},
rate_limit::apply_api_rate_limit,
request_context::apply_request_context,
routes::{
@@ -15,13 +21,23 @@ use crate::{
list_agent_platform_api_keys, list_agents, preview_tool_search, publish_agent,
revoke_agent_platform_api_key, save_agent_bindings, unpublish_agent, update_agent,
},
auth::{change_password, get_profile, get_session, login, logout, update_profile},
auth::{
bootstrap_status, change_password, complete_bootstrap, get_profile, get_session, login,
logout, refresh_session_csrf, update_profile,
},
auth_profiles::{create_auth_profile, get_auth_profile, list_auth_profiles},
capabilities::get_capabilities,
imports::{create_openapi_import, preview_openapi_import},
imports::{
apply_openapi_import_plan, create_openapi_import, create_openapi_import_plan,
preview_openapi_import, revalidate_openapi_import_plan,
},
observability::{
get_agent_usage, get_approval, get_log, get_operation_usage, get_usage, list_approvals,
list_logs,
approve_approval, deny_approval, export_logs_csv, export_usage_csv, get_agent_usage,
get_approval, get_log, get_operation_usage, get_usage, list_approvals, list_logs,
},
onboarding::{
get_onboarding, record_onboarding_event, register_onboarding_import_context,
reset_onboarding_selection, select_onboarding_import_draft,
},
operations::{
analyze_operation_quality, archive_operation, create_operation, create_version,
@@ -29,6 +45,17 @@ use crate::{
get_operation_version, list_operations, publish_operation, run_test, update_operation,
upload_input_json, upload_output_json,
},
prompts::{
create_prompt_draft, export_prompt_draft, export_prompt_version, get_prompt_draft,
import_prompt, list_prompt_drafts, preview_prompt_draft, prompt_action,
update_prompt_draft,
},
resources::{
archive_resource, create_resource, download_resource, download_resource_version,
export_resource, export_resource_version, get_resource, get_resource_version,
import_resource, list_resources, preview_resource, publish_resource, revoke_resource,
test_resource, update_resource,
},
secrets::{create_secret, delete_secret, get_secret, list_secrets, rotate_secret},
upstreams::{create_upstream, list_upstreams, update_upstream},
workspaces::{get_workspace, list_workspaces, update_workspace},
@@ -39,16 +66,89 @@ use crate::{
pub fn build_app(state: AppState) -> Router {
let workspace_router = Router::new()
.route("/operations", get(list_operations).post(create_operation))
.route("/imports/openapi/preview", post(preview_openapi_import))
.route(
"/resources",
get(list_resources)
.post(create_resource)
.layer(DefaultBodyLimit::max(MAX_ARTIFACT_BYTES + 32 * 1024)),
)
.route(
"/prompts",
get(list_prompt_drafts).post(create_prompt_draft),
)
.route(
"/prompts/{prompt_id}",
get(get_prompt_draft)
.put(update_prompt_draft)
.post(prompt_action),
)
.route("/prompts/{prompt_id}/preview", post(preview_prompt_draft))
.route("/prompts/import", post(import_prompt))
.route("/prompts/{prompt_id}/export", get(export_prompt_draft))
.route(
"/prompts/{prompt_id}/versions/{version}/export",
get(export_prompt_version),
)
.route(
"/resources/{resource_id}",
get(get_resource)
.put(update_resource)
.layer(DefaultBodyLimit::max(MAX_ARTIFACT_BYTES + 32 * 1024)),
)
.route("/resources/{resource_id}/preview", get(preview_resource))
.route("/resources/{resource_id}/test-runs", post(test_resource))
.route("/resources/{resource_id}/content", get(download_resource))
.route("/resources/{resource_id}/export", get(export_resource))
.route(
"/resources/import",
post(import_resource).layer(DefaultBodyLimit::max(
crate::service::resources::portability::MAX_IMPORT_BODY_BYTES,
)),
)
.route("/resources/{resource_id}/publish", post(publish_resource))
.route("/resources/{resource_id}/archive", post(archive_resource))
.route("/resources/{resource_id}/revoke", post(revoke_resource))
.route(
"/resources/{resource_id}/versions/{version}",
get(get_resource_version),
)
.route(
"/resources/{resource_id}/versions/{version}/content",
get(download_resource_version),
)
.route(
"/resources/{resource_id}/versions/{version}/export",
get(export_resource_version),
)
.route(
"/imports/openapi/preview",
post(preview_openapi_import)
.layer(DefaultBodyLimit::max(MAX_ARTIFACT_BYTES + 32 * 1024)),
)
.route(
"/imports/openapi/{job_id}/create",
post(create_openapi_import),
)
.route(
"/imports/openapi/{job_id}/plans",
post(create_openapi_import_plan).layer(DefaultBodyLimit::max(16 * 1024 * 1024)),
)
.route(
"/imports/openapi/plans/{plan_id}",
get(revalidate_openapi_import_plan),
)
.route(
"/imports/openapi/{plan_id}/apply",
post(apply_openapi_import_plan).layer(DefaultBodyLimit::max(16 * 1024)),
)
.route(
"/operations/analyze-quality",
post(analyze_operation_quality),
)
.route("/operations/import", post(import_operation))
.route(
"/operations/import",
post(import_operation).layer(DefaultBodyLimit::max(256 * 1024)),
)
.route(
"/operations/{operation_id}",
get(get_operation)
@@ -126,12 +226,30 @@ pub fn build_app(state: AppState) -> Router {
.route("/secrets/{secret_id}/rotate", post(rotate_secret))
.route("/export", get(export_workspace))
.route("/logs", get(list_logs))
.route("/logs/export.csv", get(export_logs_csv))
.route("/logs/{log_id}", get(get_log))
.route("/approvals", get(list_approvals))
.route("/approvals/{approval_id}", get(get_approval))
.route("/approvals/{approval_id}/approve", post(approve_approval))
.route("/approvals/{approval_id}/deny", post(deny_approval))
.route("/usage/export.csv", get(export_usage_csv))
.route("/usage", get(get_usage))
.route("/usage/operations/{operation_id}", get(get_operation_usage))
.route("/usage/agents/{agent_id}", get(get_agent_usage));
.route("/usage/agents/{agent_id}", get(get_agent_usage))
.route("/onboarding", get(get_onboarding))
.route("/onboarding/events", post(record_onboarding_event))
.route(
"/onboarding/reset-selection",
post(reset_onboarding_selection),
)
.route(
"/onboarding/import-context",
post(register_onboarding_import_context).layer(DefaultBodyLimit::max(16 * 1024)),
)
.route(
"/onboarding/select-draft",
post(select_onboarding_import_draft).layer(DefaultBodyLimit::max(16 * 1024)),
);
let workspace_root_router = Router::new()
.route("/capabilities", get(get_capabilities))
@@ -157,6 +275,7 @@ pub fn build_app(state: AppState) -> Router {
let protected_auth_router = Router::new()
.route("/logout", post(logout))
.route("/session", get(get_session))
.route("/session/csrf", post(refresh_session_csrf))
.route("/profile", get(get_profile).patch(update_profile))
.route("/password", post(change_password))
.layer(middleware::from_fn_with_state(
@@ -170,10 +289,20 @@ pub fn build_app(state: AppState) -> Router {
.nest(
"/api/auth",
Router::new()
.route("/bootstrap/status", get(bootstrap_status))
.route("/bootstrap/complete", post(complete_bootstrap))
.route("/login", post(login))
.merge(protected_auth_router),
)
.nest("/api/admin", admin_router)
.layer(middleware::from_fn_with_state(
state.clone(),
require_csrf_for_browser_mutations,
))
.layer(middleware::from_fn_with_state(
state.clone(),
enforce_browser_security,
))
.layer(middleware::from_fn_with_state(
state.clone(),
apply_api_rate_limit,
@@ -186,3 +315,8 @@ pub fn build_app(state: AppState) -> Router {
}
use crate::routes::operations::import_operation;
/// AT-3.6-02/06: приватная тестовая composition fixture (FINAL-DISPOSITION run99:47).
/// Собирается только в `cfg(test)`: non-test сборка `admin-api` модуль не видит.
#[cfg(test)]
pub(crate) mod resource_test_support;
@@ -0,0 +1,960 @@
//! AT-3.6-02/06: приватная `#[cfg(test)]` composition fixture для HTTP guard-путей Story 3.6.
//!
//! Источник полномочий: FINAL-DISPOSITION run99:47 (Q1) — принимается рекомендованный ARC
//! test-only composition fixture **вне** сервиса: точный путь
//! `apps/admin-api/src/app/resource_test_support.rs`, регистрация `#[cfg(test)] pub(crate)
//! mod resource_test_support;` в `app.rs`. TL review run101 R-A требует: fixture возвращает
//! готовый `PostgresRegistry`/изолированный router context (не raw pool в сервисе), а
//! драйвер/миграция/SQL-наблюдения состояния живут только здесь, в composition root.
//!
//! Модуль собирается исключительно для `cfg(test)`: в non-test сборке `admin-api` его нет
//! (регистрация в `app.rs` под тем же `cfg`), поэтому production API и boundary-проверки
//! не меняются. Пять исходных service-счётчиков `service::resources::tests` остаются
//! отдельным, неизменённым набором; этот файл даёт **реальный HTTP** поверх настоящих
//! middleware-ов (`require_session`, `require_workspace_session`,
//! `require_csrf_for_browser_mutations`, `enforce_browser_security`), поэтому отказ,
//! случившийся до сервиса, наблюдается как нулевые счётчики put/read физического хранилища.
//!
//! Проверки, которые опираются на этот fixture, лежат рядом с ним:
//! `crate::service::resources::tests::guard_path`.
#![allow(dead_code)]
use std::{
net::SocketAddr,
path::PathBuf,
sync::Arc,
time::{SystemTime, UNIX_EPOCH},
};
use axum::Router;
use crank_core::{MembershipRole, Workspace, WorkspaceId, WorkspaceStatus};
use crank_registry::{
CreateWorkspaceRequest, MASTER_KEY_CIPHER_CONTRACT, MasterKeyIdentityCandidate,
PostgresRegistry,
};
use crank_runtime::SecretCrypto;
use reqwest::StatusCode;
use reqwest::header::{HeaderMap, HeaderValue};
use reqwest::multipart::{Form, Part};
use serde_json::{Value, json};
use sqlx::Row as _;
use tokio::net::{TcpListener, TcpStream};
use crate::{
app::build_app,
auth::{AuthSettings, BootstrapAdminConfig, hash_password},
service::AdminServiceBuilder,
state::AppState,
};
pub(crate) const DEFAULT_WORKSPACE_ID: &str = "ws_default";
pub(crate) const OWNER_EMAIL: &str = "owner@crank.local";
pub(crate) const OWNER_PASSWORD: &str = "test-password";
/// Синтетическое WorkspaceB без членства вызывающего пользователя (workspace-негатив).
pub(crate) const FOREIGN_WORKSPACE_ID: &str = "ws_story36_guard_foreign";
/// Synthetic Viewer в `ws_default`: доступ на чтение есть, мутация запрещена (Owner-only).
pub(crate) const VIEWER_EMAIL: &str = "story36_guard_viewer@crank.local";
pub(crate) const VIEWER_PASSWORD: &str = "story36-guard-viewer-password";
/// Owner чужого WorkspaceB: создаёт и читает существующий ResourceB в своём workspace.
pub(crate) const FOREIGN_OWNER_EMAIL: &str = "story36_guard_foreign_owner@crank.local";
pub(crate) const FOREIGN_OWNER_PASSWORD: &str = "story36-guard-foreign-password";
const PASSWORD_PEPPER: &str = "test-password-pepper";
const SESSION_SECRET: &str = "test-session-secret";
const MASTER_KEY: &str = "test-master-key-00000000000000000000000000000000";
const FOREIGN_ORIGIN: &str = "http://evil.example.test";
const FOREIGN_HOST: &str = "evil.example.test";
/// Однофакторные вариации стенда: срок жизни сессии, Viewer-членство, чужое workspace,
/// настраиваемый предел содержимого.
#[derive(Clone, Copy, Debug)]
pub(crate) struct GuardOptions {
pub session_ttl_hours: i64,
pub viewer_membership: bool,
pub foreign_workspace: bool,
/// Owner-членство отдельного аккаунта в чужом WorkspaceB (существующий ResourceB).
pub foreign_workspace_owner: bool,
/// Настроенный предел содержимого Resource (`ResourceLimits::uniform`); `None` — default.
pub resource_limit: Option<usize>,
}
impl Default for GuardOptions {
fn default() -> Self {
Self {
session_ttl_hours: 24,
viewer_membership: false,
foreign_workspace: false,
foreign_workspace_owner: false,
resource_limit: None,
}
}
}
/// Изолированный стенд: собственная PG-схема на тест, приватный artifact root, реальный router.
pub(crate) struct GuardHarness {
pub registry: PostgresRegistry,
pub auth: AuthSettings,
/// Корень API без workspace-префикса — точка входа `/api/auth/login`.
pub root_url: String,
/// Workspace-scoped база `ws_default` — точка входа `/resources...`.
pub base_url: String,
/// Loopback-адрес стенда: нужен для настоящих фрагментированных HTTP-запросов
/// (chunked/absent Content-Length), которые нельзя выразить `reqwest`-клиентом.
pub addr: SocketAddr,
/// Приватный artifact root стенда: по нему проверяются физические отказы чтения.
pub storage_root: PathBuf,
shutdown: Option<tokio::sync::oneshot::Sender<()>>,
handle: Option<tokio::task::JoinHandle<()>>,
}
impl Drop for GuardHarness {
fn drop(&mut self) {
let shutdown = self.shutdown.take();
let handle = self.handle.take();
tokio::task::block_in_place(|| {
if let Some(shutdown) = shutdown {
let _ = shutdown.send(());
}
if let Some(handle) = handle {
let _ = tokio::runtime::Handle::current().block_on(handle);
}
});
}
}
impl GuardHarness {
/// Стенд по умолчанию: 24-часовые сессии, только Owner-членство, без чужого workspace.
pub(crate) async fn spawn(name: &str) -> Self {
Self::spawn_with(name, GuardOptions::default()).await
}
pub(crate) async fn spawn_with(name: &str, options: GuardOptions) -> Self {
let (database_url, pool) = resource_schema(&format!("story36_guard_{name}")).await;
crank_registry::MigrationAuthority::apply(&pool)
.await
.expect("real V16 migrations must apply to the isolated schema");
drop(pool);
let registry = PostgresRegistry::connect(&database_url)
.await
.expect("fixture registry must connect");
let secret_crypto = SecretCrypto::new(MASTER_KEY).expect("static master key must be valid");
registry
.verify_or_register_master_key_identity(MasterKeyIdentityCandidate {
epoch: secret_crypto.master_key_epoch(),
fingerprint: secret_crypto.master_key_fingerprint(),
cipher_contract: MASTER_KEY_CIPHER_CONTRACT,
observed_at: &time::OffsetDateTime::now_utc(),
})
.await
.expect("master key identity must be registered");
seed_owner(&registry).await;
if options.viewer_membership {
seed_viewer(&registry).await;
}
if options.foreign_workspace {
create_foreign_workspace(&registry).await;
}
if options.foreign_workspace_owner {
seed_foreign_owner(&registry).await;
}
let auth = AuthSettings {
session_secret: SESSION_SECRET.to_owned(),
password_pepper: PASSWORD_PEPPER.to_owned(),
session_ttl_hours: options.session_ttl_hours,
cookie_secure: false,
bootstrap_admin: BootstrapAdminConfig {
email: OWNER_EMAIL.to_owned(),
password: OWNER_PASSWORD.to_owned(),
display_name: "Story 3.6 Guard Owner".to_owned(),
},
};
let storage_root = private_storage_root(name);
let runtime = crank_runtime::community_with_outbound_policy(
crank_runtime::OutboundHttpPolicy::allowing_hosts(["127.0.0.1"]),
)
.build();
let mut builder = AdminServiceBuilder::new(
registry.clone(),
storage_root.clone(),
auth.clone(),
secret_crypto,
runtime,
);
if let Some(limit) = options.resource_limit {
builder = builder.with_resource_limits(
crank_core::resource::ResourceLimits::uniform(limit)
.expect("the configured test limit must stay inside the storage ceiling"),
);
}
let service = builder.build();
let app = build_app(AppState {
service,
api_rate_limiter: crank_runtime::RequestRateLimiter::new(
crank_runtime::RequestRateLimitConfig::new(10_000, 10_000)
.expect("static rate limit config"),
),
trusted_proxy_ips: Vec::new(),
});
let (root_url, base_url, addr, shutdown, handle) = serve(app).await;
Self {
registry,
auth,
root_url,
base_url,
addr,
storage_root,
shutdown: Some(shutdown),
handle: Some(handle),
}
}
/// Session-клиент реального synthetic-аккаунта без CSRF-заголовка.
pub(crate) async fn session_client(&self, email: &str, password: &str) -> reqwest::Client {
login_client(&self.root_url, email, password, LoginOptions::default()).await
}
/// Адрес workspace-сокращённого Resource-маршрута.
pub(crate) fn resources_url(&self) -> String {
format!("{}/resources", self.base_url)
}
pub(crate) fn resource_url(&self, resource_id: &str) -> String {
format!("{}/resources/{resource_id}", self.base_url)
}
pub(crate) fn nested_url(&self, resource_id: &str, suffix: &str) -> String {
format!("{}/resources/{resource_id}/{suffix}", self.base_url)
}
/// URL чужого workspace в том же router: доступ к маршруту есть, прав нет.
pub(crate) fn foreign_resources_url(&self) -> String {
let root = self
.base_url
.split("/api/admin/workspaces/")
.next()
.expect("workspace base url keeps the admin prefix");
format!("{root}/api/admin/workspaces/{FOREIGN_WORKSPACE_ID}/resources")
}
/// Принятые черновики `ws_default` через публичное чтение реестра (composition root).
pub(crate) async fn accepted_drafts(
&self,
) -> Vec<crank_registry::model::resource::ResourceDraftRecord> {
self.registry
.list_resource_drafts(DEFAULT_WORKSPACE_ID, 100)
.await
.expect("accepted drafts must be readable")
}
}
/// Параметры логина: наличие CSRF-заголовка и подменённый `Origin`/`Host`.
#[derive(Clone, Copy, Default)]
pub(crate) struct LoginOptions {
pub csrf: bool,
/// Явное значение заголовка `x-csrf-token` вместо настоящего токена сессии
/// (R-A: недостающий **и чужой** CSRF проверяются отдельно).
pub csrf_token: Option<&'static str>,
pub foreign_origin: bool,
/// `None` — валидный Origin реального адреса стенда (`http://127.0.0.1:<port>`).
pub origin: Option<&'static str>,
/// `Some(value)` — подменённый `Host`, с которым сравнивается `Origin`.
pub host: Option<&'static str>,
}
impl LoginOptions {
pub(crate) fn with_csrf() -> Self {
Self {
csrf: true,
..Self::default()
}
}
/// Тот же аккаунт, но с заведомо чужим значением CSRF-заголовка.
pub(crate) fn with_wrong_csrf() -> Self {
Self {
csrf: true,
csrf_token: Some("story36-not-the-session-token"),
..Self::default()
}
}
/// Cross-origin запрос: чужой `Origin` при настоящем `Host` стенда.
///
/// `Host` намеренно НЕ подменяется: иначе authority совпала бы с `Origin` и запрос
/// перестал бы быть cross-origin (проверка `enforce_browser_security`).
pub(crate) fn cross_origin() -> Self {
Self {
csrf: true,
origin: Some(FOREIGN_ORIGIN),
host: None,
..Self::default()
}
}
}
/// Логин реального аккаунта через `/api/auth/login` и сборка клиента с cookie jar.
pub(crate) async fn login_client(
root_url: &str,
email: &str,
password: &str,
options: LoginOptions,
) -> reqwest::Client {
let cookie_jar = Arc::new(reqwest::cookie::Jar::default());
let login_client = reqwest::Client::builder()
.cookie_provider(Arc::clone(&cookie_jar))
.build()
.expect("login client");
let response = login_client
.post(format!("{root_url}/api/auth/login"))
.json(&json!({ "email": email, "password": password }))
.send()
.await
.expect("login request must complete");
let status = response.status();
let body: Value = response.json().await.unwrap_or(Value::Null);
assert_eq!(
status,
StatusCode::OK,
"synthetic account `{email}` must log in: status={status} body={body}"
);
let mut headers = HeaderMap::new();
if options.csrf {
let token = match options.csrf_token {
Some(token) => token,
None => body["csrf_token"]
.as_str()
.expect("successful login must return csrf_token"),
};
headers.insert(
"x-csrf-token",
HeaderValue::from_str(token).expect("csrf header value"),
);
}
if let Some(origin) = options.origin {
headers.insert(
reqwest::header::ORIGIN,
HeaderValue::from_str(origin).expect("origin header value"),
);
}
if let Some(host) = options.host {
headers.insert(
reqwest::header::HOST,
HeaderValue::from_str(host).expect("host header value"),
);
}
reqwest::Client::builder()
.cookie_provider(cookie_jar)
.default_headers(headers)
.build()
.expect("session client")
}
/// Клиент без сессии (реальный anonymous HTTP-запрос через те же middleware).
pub(crate) fn anonymous_client() -> reqwest::Client {
reqwest::Client::builder()
.build()
.expect("anonymous client")
}
// ---------------------------------------------------------------------------
// Multipart Draft flow (та же форма, что у UI и integration-набора)
// ---------------------------------------------------------------------------
pub(crate) fn draft_metadata(
uri: &str,
name: &str,
kind: &str,
mime: &str,
sensitivity: &str,
) -> Value {
json!({
"uri": uri,
"name": name,
"description": "",
"kind": kind,
"mime_type": mime,
"sensitivity": sensitivity,
})
}
pub(crate) fn draft_form(metadata: &Value, content: &[u8], filename: &str, mime: &str) -> Form {
Form::new()
.part(
"metadata",
Part::bytes(serde_json::to_vec(metadata).expect("metadata must serialize"))
.file_name("metadata.json".to_owned())
.mime_str("application/json")
.expect("json part mime"),
)
.part(
"content",
Part::bytes(content.to_vec())
.file_name(filename.to_owned())
.mime_str(mime)
.expect("content part mime"),
)
}
/// Метаданные обновления Draft: изменяемые поля + precondition, без immutable-ключей (D-16).
pub(crate) fn update_metadata(
description: &str,
mime: &str,
sensitivity: &str,
expected_updated_at: &str,
) -> Value {
json!({
"description": description,
"mime_type": mime,
"sensitivity": sensitivity,
"expected_updated_at": expected_updated_at,
})
}
/// Значение precondition `expected_updated_at` из карточки Draft.
pub(crate) fn draft_updated_at(card: &Value) -> String {
card.pointer("/resource/updated_at")
.or_else(|| card.pointer("/version/updated_at"))
.and_then(Value::as_str)
.expect("a Draft card must expose the precondition timestamp")
.to_owned()
}
/// POST Draft: статус, наличие `x-trace-id`, текст тела, разобранный JSON.
pub(crate) async fn post_draft(
client: &reqwest::Client,
url: &str,
metadata: &Value,
content: &[u8],
mime: &str,
) -> (StatusCode, bool, String, Value) {
let response = client
.post(url)
.multipart(draft_form(metadata, content, "draft.bin", mime))
.send()
.await
.expect("draft POST must complete");
let status = response.status();
let traceable = response.headers().contains_key("x-trace-id");
let text = response.text().await.unwrap_or_default();
let body = serde_json::from_str::<Value>(&text).unwrap_or(Value::Null);
(status, traceable, text, body)
}
/// PUT Draft: статус, заголовки, текст тела, разобранный JSON.
pub(crate) async fn put_draft(
client: &reqwest::Client,
url: &str,
metadata: &Value,
content: &[u8],
mime: &str,
) -> (StatusCode, HeaderMap, String, Value) {
let response = client
.put(url)
.multipart(draft_form(metadata, content, "draft.bin", mime))
.send()
.await
.expect("draft PUT must complete");
let status = response.status();
let headers = response.headers().clone();
let text = response.text().await.unwrap_or_default();
let body = serde_json::from_str::<Value>(&text).unwrap_or(Value::Null);
(status, headers, text, body)
}
/// GET: статус, заголовки, текст тела, разобранный JSON.
pub(crate) async fn get_raw(
client: &reqwest::Client,
url: &str,
) -> (StatusCode, HeaderMap, String, Value) {
let response = client.get(url).send().await.expect("GET must complete");
let status = response.status();
let headers = response.headers().clone();
let text = response.text().await.unwrap_or_default();
let body = serde_json::from_str::<Value>(&text).unwrap_or(Value::Null);
(status, headers, text, body)
}
/// Полная подпись отказа: статус + стабильные `error.code`/`error.context.field`.
pub(crate) fn error_signature(status: StatusCode, body: &Value) -> String {
let code = body["error"]["code"].as_str().unwrap_or("<absent>");
let field = body["error"]["context"]["field"]
.as_str()
.unwrap_or("<absent>");
let message = body["error"]["message"].as_str().unwrap_or("<absent>");
format!("status={status} code={code} field={field} message={message}")
}
/// Идентификатор созданного Draft в ответе 201.
pub(crate) fn created_resource_id(body: &Value) -> String {
body["resource"]["resource_id"]
.as_str()
.or_else(|| body["resource"]["id"].as_str())
.expect("201 response must carry the created Resource identifier")
.to_owned()
}
// ---------------------------------------------------------------------------
// Сев аккаунтов и приватных корней
// ---------------------------------------------------------------------------
async fn seed_owner(registry: &PostgresRegistry) {
let password_hash = hash_password(OWNER_PASSWORD, PASSWORD_PEPPER).expect("owner hash");
let user_id = registry
.upsert_bootstrap_user(OWNER_EMAIL, "Story 3.6 Guard Owner", &password_hash)
.await
.expect("owner account");
registry
.ensure_membership(
&WorkspaceId::new(DEFAULT_WORKSPACE_ID),
&user_id,
MembershipRole::Owner,
)
.await
.expect("owner membership in ws_default");
}
async fn seed_viewer(registry: &PostgresRegistry) {
let password_hash = hash_password(VIEWER_PASSWORD, PASSWORD_PEPPER).expect("viewer hash");
let user_id = registry
.upsert_bootstrap_user(VIEWER_EMAIL, "Story 3.6 Guard Viewer", &password_hash)
.await
.expect("viewer account");
registry
.ensure_membership(
&WorkspaceId::new(DEFAULT_WORKSPACE_ID),
&user_id,
MembershipRole::Viewer,
)
.await
.expect("viewer membership in ws_default");
}
/// Owner чужого WorkspaceB: создаёт и читает существующий ResourceB в своём workspace.
async fn seed_foreign_owner(registry: &PostgresRegistry) {
let password_hash =
hash_password(FOREIGN_OWNER_PASSWORD, PASSWORD_PEPPER).expect("foreign owner hash");
let user_id = registry
.upsert_bootstrap_user(
FOREIGN_OWNER_EMAIL,
"Story 3.6 Guard Foreign Owner",
&password_hash,
)
.await
.expect("foreign owner account");
registry
.ensure_membership(
&WorkspaceId::new(FOREIGN_WORKSPACE_ID),
&user_id,
MembershipRole::Owner,
)
.await
.expect("foreign owner membership in WorkspaceB");
}
async fn create_foreign_workspace(registry: &PostgresRegistry) {
let now = time::OffsetDateTime::now_utc();
registry
.create_workspace(CreateWorkspaceRequest {
workspace: &Workspace {
id: WorkspaceId::new(FOREIGN_WORKSPACE_ID),
slug: "story36-guard-foreign".to_owned(),
display_name: "Story 3.6 Guard Foreign".to_owned(),
status: WorkspaceStatus::Active,
settings: json!({}),
created_at: now,
updated_at: now,
},
})
.await
.expect("foreign workspace must be created");
}
fn private_storage_root(name: &str) -> PathBuf {
let root = std::env::temp_dir().join(format!(
"crank_admin_api_guard_{name}_{}_{}",
std::process::id(),
SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("clock after epoch")
.as_nanos()
));
std::fs::create_dir_all(&root).expect("guard artifact root must be creatable");
#[cfg(unix)]
std::fs::set_permissions(&root, std::os::unix::fs::PermissionsExt::from_mode(0o700))
.expect("guard artifact root must be private");
root
}
// ---------------------------------------------------------------------------
// Настоящий фрагментированный запрос (chunked, без Content-Length) и наблюдения
// durable-артефакта: обычным `reqwest`-клиентом это не выражается, а SQL и файловые
// наблюдения обязаны жить в composition root, а не в сервисных тестах (R-A/R-D).
// ---------------------------------------------------------------------------
/// Сессия стенда в виде, пригодном для рукописного HTTP-запроса на сокете.
pub(crate) struct RawSession {
pub cookie: String,
pub csrf: String,
}
/// Логин без cookie-jar: cookie и CSRF-токен извлекаются напрямую.
pub(crate) async fn raw_session(root_url: &str, email: &str, password: &str) -> RawSession {
let client = reqwest::Client::builder()
.build()
.expect("raw login client");
let response = client
.post(format!("{root_url}/api/auth/login"))
.json(&json!({ "email": email, "password": password }))
.send()
.await
.expect("raw login must complete");
assert_eq!(
response.status(),
StatusCode::OK,
"the raw session login must succeed"
);
let cookie = response
.headers()
.get_all(reqwest::header::SET_COOKIE)
.iter()
.filter_map(|value| value.to_str().ok())
.filter_map(|value| value.split(';').next())
.collect::<Vec<_>>()
.join("; ");
let body: Value = response.json().await.unwrap_or(Value::Null);
let csrf = body["csrf_token"].as_str().unwrap_or_default().to_owned();
assert!(
!cookie.is_empty() && !csrf.is_empty(),
"a raw session must carry both the cookie and the CSRF token"
);
RawSession { cookie, csrf }
}
/// Ответ рукописного HTTP-запроса: статус-код и тело.
pub(crate) struct RawHttpResponse {
pub status: u16,
/// Голова ответа как её прислал сервер (нужна для диагностики отказов);
/// значения чувствительных заголовков отредактированы.
pub head: String,
pub body: String,
/// Отправленная голова запроса: без неё отказ не отличить от ошибки харнесса.
/// Хранится уже с отредактированными чувствительными заголовками (F5).
pub request_head: String,
/// Ошибка записи фрагмента (сервер мог ответить и закрыть соединение) — наблюдение.
pub write_error: Option<String>,
/// Ошибка дочитывания ответа; пустое тело при `Some` — не «нет ответа», а обрыв.
pub read_error: Option<String>,
}
impl RawHttpResponse {
/// Компактная подпись для сообщений об отказе.
pub(crate) fn signature(&self) -> String {
format!(
"status={} write_error={:?} read_error={:?} response_head={} request_head={} body={}",
self.status,
self.write_error,
self.read_error,
self.head.replace('\r', "").replace('\n', " | "),
self.request_head.replace('\r', "").replace('\n', " | "),
self.body
)
}
}
/// Настоящий chunked-запрос: тело уходит несколькими фрагментами, `Content-Length`
/// отсутствует, `Transfer-Encoding: chunked` объявлен явно.
pub(crate) async fn send_chunked_multipart(
addr: SocketAddr,
path: &str,
session: &RawSession,
boundary: &str,
chunks: &[Vec<u8>],
) -> RawHttpResponse {
use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _};
let mut stream = TcpStream::connect(addr)
.await
.expect("the raw loopback socket must connect");
let head_request = format!(
"POST {path} HTTP/1.1\r\nHost: {addr}\r\nCookie: {}\r\nX-CSRF-Token: {}\r\n\
Content-Type: multipart/form-data; boundary={boundary}\r\n\
Transfer-Encoding: chunked\r\nConnection: close\r\n\r\n",
session.cookie, session.csrf
);
stream
.write_all(head_request.as_bytes())
.await
.expect("the request head must be written");
// Запись фрагментов может прерваться, если сервер уже ответил и закрыл соединение
// (например, ранний отказ guard-а или отказ по лимиту). Это наблюдение, а не ошибка
// теста: ответ всё равно дочитывается ниже и попадает в assertions.
let mut write_error: Option<std::io::Error> = None;
for chunk in chunks {
let size = format!("{:x}\r\n", chunk.len());
let fragment = [
stream.write_all(size.as_bytes()).await,
stream.write_all(chunk).await,
stream.write_all(b"\r\n").await,
];
if let Some(error) = fragment.into_iter().find_map(Result::err) {
write_error = Some(error);
break;
}
stream.flush().await.expect("a fragment must be flushed");
tokio::time::sleep(std::time::Duration::from_millis(5)).await;
}
if write_error.is_none() {
let _ = stream.write_all(b"0\r\n\r\n").await;
}
let _ = stream.flush().await;
let mut raw = Vec::new();
let read_error = stream.read_to_end(&mut raw).await.err();
let text = String::from_utf8_lossy(&raw).into_owned();
let (head, body) = text.clone().split_once("\r\n\r\n").map_or_else(
|| (String::new(), String::new()),
|(head, body)| (head.to_owned(), body.to_owned()),
);
let status = head
.split_whitespace()
.nth(1)
.and_then(|code| code.parse::<u16>().ok())
.unwrap_or(0);
RawHttpResponse {
status,
// Обе головы сохраняются уже отредактированными: ни request-, ни response-голова
// не должны уносить session/CSRF-значения в авторский raw-журнал (F5).
head: redact_sensitive_headers(&head),
body,
request_head: redact_sensitive_headers(&head_request),
write_error: write_error.map(|error| error.to_string()),
read_error: read_error.map(|error| error.to_string()),
}
}
/// Наблюдаемая идентичность durable-артефакта версии Resource (blob + версия + owning ref).
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct ArtifactIdentity {
pub blob_digest: String,
pub blob_size_bytes: i64,
pub blob_artifact_ref: String,
pub blob_lifecycle: String,
pub version_digest: String,
pub version_size_bytes: i64,
pub version_mime_type: String,
pub version_sensitivity: String,
pub source_id: String,
pub source_mime_type: String,
pub source_sensitivity: String,
pub source_lifecycle: String,
pub source_detached_at: Option<time::OffsetDateTime>,
}
/// Однофакторные мутации metadata, от которых зависит сверка чтения Resource (§6).
///
/// Расхождение `artifact_blobs.artifact_ref` с digest не входит в матрицу: его
/// структурно запрещает CHECK `artifact_blobs_ref_check (artifact_ref = 'sha256:' || digest)`,
/// поэтому такой вектор был бы недостижимым, а не «пропущенным дефектом».
#[derive(Clone, Copy, Debug)]
pub(crate) enum ArtifactMutation {
BlobSize,
BlobStorageLifecycle,
VersionDigest,
VersionSize,
VersionMimeType,
VersionSensitivity,
SourceMimeType,
SourceLifecycle,
}
impl ArtifactMutation {
pub(crate) const ALL: [Self; 8] = [
Self::BlobSize,
Self::BlobStorageLifecycle,
Self::VersionDigest,
Self::VersionSize,
Self::VersionMimeType,
Self::VersionSensitivity,
Self::SourceMimeType,
Self::SourceLifecycle,
];
pub(crate) fn label(self) -> &'static str {
match self {
Self::BlobSize => "blob-size",
Self::BlobStorageLifecycle => "blob-storage-lifecycle",
Self::VersionDigest => "version-digest",
Self::VersionSize => "version-size",
Self::VersionMimeType => "version-mime-type",
Self::VersionSensitivity => "version-sensitivity",
Self::SourceMimeType => "source-mime-type",
Self::SourceLifecycle => "source-lifecycle",
}
}
fn statement(self) -> &'static str {
match self {
Self::BlobSize => "update artifact_blobs set size_bytes = size_bytes + 1",
Self::BlobStorageLifecycle => {
"update artifact_blobs set storage_lifecycle = 'unavailable'"
}
Self::VersionDigest => "update resource_versions set digest = repeat('0', 64)",
Self::VersionSize => "update resource_versions set size_bytes = size_bytes + 1",
Self::VersionMimeType => "update resource_versions set mime_type = 'text/html'",
Self::VersionSensitivity => "update resource_versions set sensitivity = 'public'",
Self::SourceMimeType => "update artifact_sources set mime_type = 'text/html'",
Self::SourceLifecycle => {
"update artifact_sources set lifecycle = 'detached', detached_at = updated_at"
}
}
}
}
/// Текущая идентичность артефакта workspace (ровно одна текущая версия в тестах).
pub(crate) async fn observe_artifact_identity(
pool: &sqlx::PgPool,
workspace_id: &str,
) -> ArtifactIdentity {
let row = sqlx::query(
"select b.digest as blob_digest, b.size_bytes as blob_size_bytes,
b.artifact_ref as blob_artifact_ref, b.storage_lifecycle as blob_lifecycle,
v.digest as version_digest, v.size_bytes as version_size_bytes,
v.mime_type as version_mime_type, v.sensitivity as version_sensitivity,
s.source_id, s.mime_type as source_mime_type,
s.sensitivity as source_sensitivity, s.lifecycle as source_lifecycle,
s.detached_at as source_detached_at
from artifact_blobs b
join artifact_sources s on s.blob_digest = b.digest
join resource_versions v
on v.workspace_id = s.workspace_id and v.artifact_source_id = s.source_id
where s.workspace_id = $1
order by s.source_id
limit 1",
)
.bind(workspace_id)
.fetch_one(pool)
.await
.expect("the durable artifact identity must be observable");
ArtifactIdentity {
blob_digest: row.get("blob_digest"),
blob_size_bytes: row.get("blob_size_bytes"),
blob_artifact_ref: row.get("blob_artifact_ref"),
blob_lifecycle: row.get("blob_lifecycle"),
version_digest: row.get("version_digest"),
version_size_bytes: row.get("version_size_bytes"),
version_mime_type: row.get("version_mime_type"),
version_sensitivity: row.get("version_sensitivity"),
source_id: row.get("source_id"),
source_mime_type: row.get("source_mime_type"),
source_sensitivity: row.get("source_sensitivity"),
source_lifecycle: row.get("source_lifecycle"),
source_detached_at: row.get("source_detached_at"),
}
}
/// Применяет одну однофакторную мутацию metadata (SQL только в composition root).
pub(crate) async fn apply_artifact_mutation(
pool: &sqlx::PgPool,
mutation: ArtifactMutation,
) -> Result<(), sqlx::Error> {
sqlx::raw_sql(sqlx::AssertSqlSafe(mutation.statement().to_owned()))
.execute(pool)
.await
.map(|_| ())
}
/// Возвращает зафиксированные значения байт-в-байт (не «похожие»).
pub(crate) async fn restore_artifact_identity(
pool: &sqlx::PgPool,
workspace_id: &str,
identity: &ArtifactIdentity,
) -> Result<(), sqlx::Error> {
sqlx::query(
"update artifact_blobs
set digest = $1, size_bytes = $2, artifact_ref = $3, storage_lifecycle = $4
where digest = $1",
)
.bind(&identity.blob_digest)
.bind(identity.blob_size_bytes)
.bind(&identity.blob_artifact_ref)
.bind(&identity.blob_lifecycle)
.execute(pool)
.await?;
sqlx::query(
"update resource_versions
set digest = $1, size_bytes = $2, mime_type = $3, sensitivity = $4
where workspace_id = $5 and artifact_source_id = $6",
)
.bind(&identity.version_digest)
.bind(identity.version_size_bytes)
.bind(&identity.version_mime_type)
.bind(&identity.version_sensitivity)
.bind(workspace_id)
.bind(&identity.source_id)
.execute(pool)
.await?;
sqlx::query(
"update artifact_sources
set mime_type = $1, sensitivity = $2, lifecycle = $3, detached_at = $4
where workspace_id = $5 and source_id = $6",
)
.bind(&identity.source_mime_type)
.bind(&identity.source_sensitivity)
.bind(&identity.source_lifecycle)
.bind(identity.source_detached_at)
.bind(workspace_id)
.bind(&identity.source_id)
.execute(pool)
.await?;
Ok(())
}
// ---------------------------------------------------------------------------
// Подмодуль наблюдений: редактирование заголовков (F5), composition-стенд сервисных
// проверок чтения (F3), SQL/файловые наблюдения. Разделён по health-правилу 1000 строк;
// границы и регистрация модуля не меняются.
// ---------------------------------------------------------------------------
mod observations;
pub(crate) use observations::{
ResourceServiceHarness, artifact_files, redact_sensitive_headers, resource_row_counts,
resource_schema,
};
/// Поднимает реальный router на loopback и возвращает URL-ы, адрес и ручки остановки.
async fn serve(
app: Router,
) -> (
String,
String,
SocketAddr,
tokio::sync::oneshot::Sender<()>,
tokio::task::JoinHandle<()>,
) {
let listener = TcpListener::bind("127.0.0.1:0")
.await
.expect("ephemeral loopback port");
let address: SocketAddr = listener.local_addr().expect("listener address");
let (shutdown_tx, shutdown_rx) = tokio::sync::oneshot::channel();
let handle = tokio::spawn(async move {
axum::serve(listener, app)
.with_graceful_shutdown(async move {
let _ = shutdown_rx.await;
})
.await
.expect("guard harness server must serve");
});
(
format!("http://{address}"),
format!("http://{address}/api/admin/workspaces/{DEFAULT_WORKSPACE_ID}"),
address,
shutdown_tx,
handle,
)
}
@@ -0,0 +1,136 @@
//! Наблюдения и composition-часть приватного ATDD-стенда Story 3.6.
//!
//! Подмодуль того же приватного `#[cfg(test)]` фикстура
//! `crate::app::resource_test_support` (регистрация в `app.rs` не меняется): физическое
//! разделение выполнено только потому, что health-правило репозитория ограничивает файл
//! 1000 строками (`scripts/check-rust-code-health.sh`), а базовая часть модуля уже
//! содержит HTTP-стенд guard-проверок. Здесь находятся:
//!
//! * редактирование чувствительных заголовков рукописной диагностики (F5, TL run105);
//! * composition-стенд сервисных проверок чтения Resource (F3/F4, TL run105);
//! * SQL-наблюдение итогов строк V16 и файловые наблюдения содержимого.
//!
//! Границы те же, что у корневого файла: `cfg(test)`-only, никакого production API.
use std::{path::PathBuf, sync::Arc};
use crank_artifacts::ArtifactStore;
use crank_core::resource::ResourceLimits;
use crank_registry::PostgresRegistry;
use crate::service::resources::ResourcesService;
// ---------------------------------------------------------------------------
// F5: диагностика не несёт значения чувствительных заголовков
// ---------------------------------------------------------------------------
/// Значения этих заголовков не попадают в диагностику даже синтетического стенда.
const SENSITIVE_HEADERS: [&str; 4] = ["cookie", "set-cookie", "x-csrf-token", "authorization"];
/// Редактирует значения чувствительных заголовков (AGENTS: credentials не в журналах).
///
/// Редактирование выполняется **до** сохранения головы, а не при печати (TL run105 F5);
/// метод, путь, структура заголовков и статус сохраняются, иначе отказ не отличить от
/// ошибки харнесса.
pub(crate) fn redact_sensitive_headers(head: &str) -> String {
head.split("\r\n")
.map(|line| match line.split_once(':') {
Some((name, _value))
if SENSITIVE_HEADERS.contains(&name.trim().to_ascii_lowercase().as_str()) =>
{
format!("{name}: <redacted-sensitive-header>")
}
_ => line.to_owned(),
})
.collect::<Vec<_>>()
.join("\r\n")
}
// ---------------------------------------------------------------------------
// F3/F4: composition-стенд сервисных проверок чтения Resource
// ---------------------------------------------------------------------------
/// Изолированная PG-схема вместе с соединением к ней.
///
/// Конкретный драйвер допускается только в composition root (`cfg(test)`-фикстура
/// вне `service/**`): сервисные тест-модули получают готовые registry/service и
/// не называют адаптер хранилища сами (TL run105 F3, FINAL-DISPOSITION run99:47).
pub(crate) async fn resource_schema(prefix: &str) -> (String, sqlx::PgPool) {
let url = crank_test_support::postgres_schema_url(prefix).await;
let pool = sqlx::postgres::PgPoolOptions::new()
.max_connections(2)
.connect(&url)
.await
.expect("the isolated test schema must accept connections");
(url, pool)
}
/// Приватный стенд сервисных проверок чтения Resource: изолированная PG-схема V16,
/// приватный artifact root и **готовый** `ResourcesService`.
///
/// Драйвер, миграция и сборка store/service живут в composition root, а не в сервисном
/// тест-модуле (TL run105 F3; FINAL-DISPOSITION run99:47).
pub(crate) struct ResourceServiceHarness {
pub service: ResourcesService,
pub registry: PostgresRegistry,
/// Приватный root содержимого: по нему наблюдаются физические отказы чтения.
pub root: PathBuf,
}
impl ResourceServiceHarness {
pub(crate) async fn spawn(name: &str, limit: usize) -> Self {
let (database_url, pool) = resource_schema(&format!("story36_integrity_{name}")).await;
crank_registry::MigrationAuthority::apply(&pool)
.await
.expect("V16 must be applicable");
drop(pool);
let registry = PostgresRegistry::connect(&database_url)
.await
.expect("fixture registry must connect");
let root = super::private_storage_root(&format!("integrity_{name}"));
let store = ArtifactStore::open(&root).expect("private artifact store must open");
let limits = ResourceLimits::uniform(limit)
.expect("the configured test limit must stay inside the storage ceiling");
Self {
service: ResourcesService::new(registry.clone(), Arc::new(store), limits),
registry,
root,
}
}
}
// ---------------------------------------------------------------------------
// SQL/файловые наблюдения состояния (только composition root)
// ---------------------------------------------------------------------------
/// Итоги строк V16 `(resources, resource_versions, artifact_sources, artifact_blobs)` —
/// наблюдение «отказ без побочных эффектов» (SQL только в composition root).
pub(crate) async fn resource_row_counts(pool: &sqlx::PgPool) -> (i64, i64, i64, i64) {
sqlx::query_as(
"select (select count(*) from resources), (select count(*) from resource_versions),
(select count(*) from artifact_sources), (select count(*) from artifact_blobs)",
)
.fetch_one(pool)
.await
.expect("V16 relations must be countable")
}
/// Физические файлы содержимого под приватным artifact root.
pub(crate) fn artifact_files(root: &std::path::Path) -> Vec<PathBuf> {
let mut files = Vec::new();
let mut pending = vec![root.to_path_buf()];
while let Some(dir) = pending.pop() {
let entries = std::fs::read_dir(&dir).expect("the private artifact root must be readable");
for entry in entries {
let entry = entry.expect("an artifact entry must be readable");
let path = entry.path();
if path.is_dir() {
pending.push(path);
} else {
files.push(path);
}
}
}
files.sort();
files
}
+131 -3
View File
@@ -1,22 +1,24 @@
use axum::{
extract::{OriginalUri, Request, State},
http::{HeaderValue, Method, header},
middleware::Next,
response::Response,
};
use axum_extra::extract::cookie::{Cookie, CookieJar};
use crank_community_auth::{
cleared_session_cookie as build_cleared_session_cookie,
cleared_session_cookie as build_cleared_session_cookie, create_csrf_token as build_csrf_token,
create_session_cookie as build_session_cookie, hash_password as community_hash_password,
session_cookie as build_session_cookie_header,
};
use crank_core::{User, UserSessionId, WorkspaceId};
use crank_core::{MembershipRole, User, UserSessionId, WorkspaceId};
use crank_registry::WorkspaceMembershipRecord;
use serde::Serialize;
use crate::{error::ApiError, state::AppState};
pub use crank_community_auth::{
SESSION_COOKIE_NAME, SessionCookie, extract_session_token, hash_session_secret, verify_password,
SESSION_COOKIE_NAME, SessionCookie, create_csrf_token, extract_session_token, hash_csrf_token,
hash_session_secret, verify_password,
};
#[derive(Clone)]
@@ -53,6 +55,10 @@ pub fn create_session_cookie(settings: &AuthSettings) -> Result<SessionCookie, A
.map_err(|error| ApiError::internal(format!("failed to create session cookie: {error}")))
}
pub fn create_csrf_token_value() -> String {
build_csrf_token()
}
pub fn session_cookie(settings: &AuthSettings, token: &str) -> Cookie<'static> {
build_session_cookie_header(token, settings.cookie_secure, settings.session_ttl_hours)
}
@@ -90,11 +96,81 @@ pub async fn require_workspace_session(
if !has_access {
return Err(ApiError::forbidden("workspace access denied"));
}
if !matches!(
request.method(),
&axum::http::Method::GET | &axum::http::Method::HEAD
) && !session.memberships.iter().any(|membership| {
membership.workspace.id == workspace_id && membership.role == MembershipRole::Owner
}) {
return Err(ApiError::forbidden("workspace owner access required"));
}
request.extensions_mut().insert(session);
Ok(next.run(request).await)
}
pub async fn require_csrf_for_browser_mutations(
State(state): State<AppState>,
jar: CookieJar,
request: Request,
next: Next,
) -> Result<Response, ApiError> {
if !requires_csrf(request.method(), request.uri().path()) {
return Ok(next.run(request).await);
}
let (session_id, _session_value) = extract_session_token(&jar)
.ok_or_else(|| ApiError::unauthorized("authentication required"))?;
let token = request
.headers()
.get("x-csrf-token")
.and_then(|value| value.to_str().ok())
.filter(|value| valid_csrf_token(value))
.ok_or_else(|| ApiError::forbidden("csrf validation failed"))?;
let csrf_hash = hash_csrf_token(
&session_id,
token,
&state.service.auth_settings().session_secret,
);
if !state
.service
.verify_session_csrf(&session_id, &csrf_hash)
.await?
{
return Err(ApiError::forbidden("csrf validation failed"));
}
Ok(next.run(request).await)
}
pub async fn enforce_browser_security(
State(state): State<AppState>,
request: Request,
next: Next,
) -> Result<Response, ApiError> {
if let Some(origin) = request.headers().get(header::ORIGIN)
&& is_cross_origin(
origin,
request.headers().get(header::HOST),
if state.service.auth_settings().cookie_secure {
"https"
} else {
"http"
},
)
&& request.uri().path().starts_with("/api/")
{
return Err(ApiError::forbidden("cross-origin admin request denied"));
}
let mut response = next.run(request).await;
let headers = response.headers_mut();
headers.insert(
"x-content-type-options",
HeaderValue::from_static("nosniff"),
);
headers.insert("x-frame-options", HeaderValue::from_static("DENY"));
headers.insert("referrer-policy", HeaderValue::from_static("no-referrer"));
Ok(response)
}
async fn resolve_authenticated_session(
state: AppState,
jar: &CookieJar,
@@ -122,3 +198,55 @@ fn workspace_id_from_path(path: &str) -> Option<WorkspaceId> {
Some(WorkspaceId::new(workspace_id))
}
fn requires_csrf(method: &Method, path: &str) -> bool {
if matches!(method, &Method::GET | &Method::HEAD | &Method::OPTIONS) {
return false;
}
if matches!(
path,
"/api/auth/login" | "/api/auth/bootstrap/complete" | "/api/auth/session/csrf"
) {
return false;
}
path.starts_with("/api/auth/") || path.starts_with("/api/admin/")
}
fn valid_csrf_token(value: &str) -> bool {
(32..=256).contains(&value.len())
&& value
.bytes()
.all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.'))
}
fn is_cross_origin(
origin: &HeaderValue,
host: Option<&HeaderValue>,
expected_scheme: &str,
) -> bool {
let Some(host) = host.and_then(|value| value.to_str().ok()) else {
return true;
};
let Some(origin) = origin.to_str().ok() else {
return true;
};
let Ok(url) = url::Url::parse(origin) else {
return true;
};
url.host_str()
.zip(url.port_or_known_default())
.map(|(origin_host, origin_port)| {
let origin_authority = format!("{}://{origin_host}:{origin_port}", url.scheme());
let request_authority = if host.contains(':') {
format!("{expected_scheme}://{}", host.to_ascii_lowercase())
} else {
let default_port = if expected_scheme == "https" { 443 } else { 80 };
format!(
"{expected_scheme}://{}:{default_port}",
host.to_ascii_lowercase()
)
};
origin_authority.to_ascii_lowercase() != request_authority
})
.unwrap_or(true)
}
+965
View File
@@ -0,0 +1,965 @@
#[path = "crank_migrate/admin_auth_command.rs"]
mod admin_auth_command;
#[path = "crank_migrate/db_connect.rs"]
mod db_connect;
use crank_config::{ConfigSource, parse_migrator};
use crank_registry::{
BackfillPolicy, MASTER_KEY_CIPHER_CONTRACT, MasterKeyIdentityCandidate,
MasterKeyRotationRecord, MigrationApplyResult, MigrationAuthority, MigrationPreflight,
PostgresRegistry, RegistryError, SecretVersionRecord,
};
use crank_runtime::SecretCrypto;
use serde_json::json;
use std::{path::Path, process::ExitCode};
use time::OffsetDateTime;
const MASTER_KEY_ROTATION_PAGE_SIZE: i64 = 1_000;
#[tokio::main]
async fn main() -> ExitCode {
match run().await {
Ok(code) => code,
Err(error) => {
eprintln!(
"{}",
json!({
"status": "error",
"code": error.code,
"stage": error.stage,
"version": error.version,
"recovery": error.recovery,
})
);
ExitCode::FAILURE
}
}
}
#[derive(Clone, Copy)]
struct CliError {
code: &'static str,
stage: &'static str,
recovery: &'static str,
version: Option<i64>,
}
impl CliError {
const fn new(code: &'static str, stage: &'static str, recovery: &'static str) -> Self {
Self {
code,
stage,
recovery,
version: None,
}
}
fn from_migration(error: crank_registry::MigrationError) -> Self {
Self {
code: error.code(),
stage: error.stage(),
recovery: error.recovery(),
version: error.version(),
}
}
fn from_registry(error: RegistryError) -> Self {
match error {
RegistryError::MasterKeyIdentityMismatch { .. } => Self::new(
"master_key_identity_mismatch",
"master_key.identity",
"use_matching_master_key",
),
RegistryError::InvalidMasterKeyIdentity => Self::new(
"master_key_identity_invalid",
"master_key.identity",
"verify_operator_input",
),
RegistryError::MasterKeyRotationInProgress => Self::new(
"master_key_rotation_in_progress",
"master_key.rotation",
"resume_verify_promote_or_abort_rotation",
),
RegistryError::MasterKeyRotationNotFound { .. } => Self::new(
"master_key_rotation_not_found",
"master_key.rotation",
"run_status",
),
RegistryError::MasterKeyRotationConflict => Self::new(
"master_key_rotation_conflict",
"master_key.rotation",
"run_status",
),
RegistryError::MasterKeyRotationVerificationFailed => Self::new(
"master_key_rotation_verification_failed",
"master_key.rotation",
"rerun_rotation_or_abort",
),
RegistryError::AdminBootstrapUnavailable => Self::new(
"admin_bootstrap_unavailable",
"admin_auth.bootstrap",
"use_existing_active_contract_or_wait_until_expired",
),
RegistryError::AdminBootstrapRejected => Self::new(
"admin_bootstrap_rejected",
"admin_auth.bootstrap",
"create_new_local_bootstrap_contract",
),
RegistryError::AdminRecoveryRejected => Self::new(
"admin_recovery_rejected",
"admin_auth.recovery",
"verify_local_inputs_and_master_key",
),
RegistryError::AdminLoginRateLimited { .. } => Self::new(
"admin_login_rate_limited",
"admin_auth.login",
"retry_after_delay",
),
RegistryError::AdminCsrfRejected => {
Self::new("admin_csrf_rejected", "admin_auth.csrf", "refresh_session")
}
RegistryError::Storage(_) => {
Self::new("storage_unavailable", "database.query", "contact_operator")
}
_ => Self::new("registry_error", "registry.operation", "contact_operator"),
}
}
}
async fn run() -> Result<ExitCode, CliError> {
let args = std::env::args().skip(1).collect::<Vec<_>>();
if args.first().map(String::as_str) == Some("master-key") {
return run_master_key(&args[1..]).await;
}
if args.first().map(String::as_str) == Some("admin-auth") {
return admin_auth_command::run_admin_auth(&args[1..]).await;
}
let requested = args.first().map(String::as_str);
let option = args.get(1).map(String::as_str);
if args.len() > 2 {
return Err(CliError::new(
"invalid_command",
"cli.arguments",
"run_preflight",
));
}
let command = match requested {
None | Some("preflight") => "preflight",
Some("plan") => "plan",
Some("apply") => "apply",
Some(_) => {
return Err(CliError::new(
"invalid_command",
"cli.arguments",
"run_preflight",
));
}
};
if command == "plan" {
MigrationAuthority::validate_sequence().map_err(CliError::from_migration)?;
let sequence = MigrationAuthority::sequence()
.into_iter()
.map(|migration| {
let backfill = match migration.backfill {
BackfillPolicy::None => json!({ "kind": "none" }),
BackfillPolicy::Bounded {
max_batch_rows,
max_batch_ms,
resumable,
} => json!({
"kind": "bounded",
"max_batch_rows": max_batch_rows,
"max_batch_ms": max_batch_ms,
"resumable": resumable,
}),
};
json!({
"version": migration.version,
"name": migration.name,
"checksum": migration.checksum,
"source_digest": migration.source_digest,
"phase": migration.phase,
"compatibility": migration.compatibility,
"owner": migration.owner,
"transactional": migration.transactional,
"backfill": backfill,
"readable_schema_min": migration.readable_schema_min,
"readable_schema_max": migration.readable_schema_max,
"contract_evidence": migration.contract_evidence,
})
})
.collect::<Vec<_>>();
let plan = json!({ "schema_version": 1, "sequence": sequence });
if option == Some("--target-version") {
let target_version = plan["sequence"]
.as_array()
.and_then(|sequence| sequence.last())
.and_then(|migration| migration["version"].as_i64())
.ok_or(CliError::new(
"migration_sequence_invalid",
"plan.target_version",
"contact_operator",
))?;
println!("{target_version}");
return Ok(ExitCode::SUCCESS);
}
if option == Some("--check") {
let bytes = std::fs::read("docs/schemas/migration-sequence.json")
.map_err(|_| CliError::new("contract_drift", "plan.read", "contact_operator"))?;
if bytes.len() > 65_536 {
return Err(CliError::new(
"contract_drift",
"plan.size",
"contact_operator",
));
}
let committed: serde_json::Value = serde_json::from_slice(&bytes)
.map_err(|_| CliError::new("contract_drift", "plan.parse", "contact_operator"))?;
if committed != plan {
return Err(CliError::new(
"contract_drift",
"plan.compare",
"contact_operator",
));
}
println!("{}", json!({ "status": "contract_current" }));
return Ok(ExitCode::SUCCESS);
}
if option.is_some() {
return Err(CliError::new(
"invalid_command",
"cli.arguments",
"run_preflight",
));
}
println!("{plan}");
return Ok(ExitCode::SUCCESS);
}
if option.is_some() {
return Err(CliError::new(
"invalid_command",
"cli.arguments",
"run_preflight",
));
}
let config = parse_migrator(
ConfigSource::from_os_for_migrator()
.map_err(|_| CliError::new("config_invalid", "config.source", "run_preflight"))?,
)
.map_err(|_| CliError::new("config_invalid", "config.validate", "run_preflight"))?;
let pool = db_connect::connect(&config.database).await?;
if command == "apply" {
let result = MigrationAuthority::apply(&pool)
.await
.map_err(CliError::from_migration)?;
let (status, from, to) = match result {
MigrationApplyResult::Applied { from, to } => ("applied", from, to),
MigrationApplyResult::AlreadyCurrent { version } => {
("already_current", version, version)
}
};
println!(
"{}",
json!({ "status": status, "from_version": from, "to_version": to })
);
return Ok(ExitCode::SUCCESS);
}
match MigrationAuthority::preflight(&pool)
.await
.map_err(CliError::from_migration)?
{
MigrationPreflight::Current { version } => {
println!("{}", json!({ "status": "current", "version": version }));
Ok(ExitCode::SUCCESS)
}
MigrationPreflight::MigrationRequired { current, target } => {
println!(
"{}",
json!({
"status": "migration_required",
"current_version": current,
"target_version": target,
"recovery": "run_controlled_migration",
})
);
Ok(ExitCode::from(2))
}
}
}
async fn run_master_key(arguments: &[String]) -> Result<ExitCode, CliError> {
let Some(command) = arguments.first().map(String::as_str) else {
return Err(CliError::new(
"invalid_command",
"cli.arguments",
"run_master_key_status",
));
};
let options = MasterKeyOptions::parse(&arguments[1..])?;
let config = parse_migrator(
ConfigSource::from_os_for_migrator()
.map_err(|_| CliError::new("config_invalid", "config.source", "run_preflight"))?,
)
.map_err(|_| CliError::new("config_invalid", "config.validate", "run_preflight"))?;
let registry = db_connect::connect_registry(&config.database).await?;
match command {
"status" => {
ensure_no_options(&options)?;
let status = registry
.master_key_rotation_status()
.await
.map_err(CliError::from_registry)?;
println!(
"{}",
json!({
"status": "ok",
"active_epoch": status.active_identity.as_ref().map(|identity| identity.epoch),
"rotation_count": status.rotations.len(),
"rotations": status.rotations.iter().map(rotation_json).collect::<Vec<_>>(),
})
);
}
"preflight" => {
let preflight = master_key_preflight(&registry, &options).await?;
println!(
"{}",
json!({
"status": "preflight_ok",
"source_epoch": preflight.source_epoch,
"target_epoch": preflight.target_epoch,
"affected_secret_versions": preflight.affected_secret_versions,
"backup_ref": preflight.backup_ref.map(|_| "configured"),
})
);
}
"rotate" => {
let (rotation, current_crypto, target_crypto) =
if let Some(rotation) = active_rotation(&registry, &["running"]).await? {
let (current_crypto, target_crypto) =
rotation_crypto_for_resume(&registry, &options, &rotation).await?;
(rotation, current_crypto, target_crypto)
} else {
let preflight = master_key_preflight(&registry, &options).await?;
let now = OffsetDateTime::now_utc();
let rotation = registry
.begin_master_key_rotation(
preflight.source_epoch,
preflight.target_epoch,
preflight.target_crypto.master_key_fingerprint(),
preflight.backup_ref.as_deref(),
&now,
)
.await
.map_err(CliError::from_registry)?;
(rotation, preflight.current_crypto, preflight.target_crypto)
};
let processed = process_rotation_batch(
&registry,
&rotation,
&current_crypto,
&target_crypto,
options.max_versions,
)
.await?;
let status = if rotation.processed_secret_versions + processed
>= rotation.total_secret_versions
{
let now = OffsetDateTime::now_utc();
registry
.finish_master_key_rotation_batches(&rotation.id, &now)
.await
.map_err(CliError::from_registry)?
} else {
registry
.master_key_rotation_status()
.await
.map_err(CliError::from_registry)?
.rotations
.into_iter()
.find(|candidate| candidate.id == rotation.id)
.ok_or_else(|| {
CliError::new(
"master_key_rotation_not_found",
"master_key.rotation",
"run_status",
)
})?
};
println!(
"{}",
json!({
"status": status.state,
"rotation_id": status.id,
"source_epoch": status.source_epoch,
"target_epoch": status.target_epoch,
"processed_secret_versions": status.processed_secret_versions,
"total_secret_versions": status.total_secret_versions,
})
);
}
"verify" => {
let target_key = read_required_key_file(options.target_key_file.as_deref())?;
let rotation = active_rotation(&registry, &["verifying"])
.await?
.ok_or_else(|| {
CliError::new(
"master_key_rotation_conflict",
"master_key.rotation",
"run_rotate",
)
})?;
let target_crypto = SecretCrypto::with_epoch(&target_key, rotation.target_epoch)
.map_err(|_| {
CliError::new(
"master_key_invalid",
"master_key.input",
"verify_operator_input",
)
})?;
if target_crypto.master_key_fingerprint() != rotation.target_fingerprint {
return Err(CliError::new(
"master_key_identity_mismatch",
"master_key.identity",
"use_matching_target_key",
));
}
let verified = verify_staged_targets(&registry, &rotation, &target_crypto).await?;
let now = OffsetDateTime::now_utc();
let status = registry
.verify_master_key_rotation(&rotation.id, verified, &now)
.await
.map_err(CliError::from_registry)?;
println!(
"{}",
json!({
"status": status.state,
"rotation_id": status.id,
"verified_secret_versions": status.verified_secret_versions,
"total_secret_versions": status.total_secret_versions,
})
);
}
"promote" => {
let target_key = read_required_key_file(options.target_key_file.as_deref())?;
let rotation = active_rotation(&registry, &["verified"])
.await?
.ok_or_else(|| {
CliError::new(
"master_key_rotation_conflict",
"master_key.rotation",
"run_verify",
)
})?;
let target_crypto = SecretCrypto::with_epoch(&target_key, rotation.target_epoch)
.map_err(|_| {
CliError::new(
"master_key_invalid",
"master_key.input",
"verify_operator_input",
)
})?;
if target_crypto.master_key_fingerprint() != rotation.target_fingerprint {
return Err(CliError::new(
"master_key_identity_mismatch",
"master_key.identity",
"use_matching_target_key",
));
}
verify_staged_targets(&registry, &rotation, &target_crypto).await?;
let now = OffsetDateTime::now_utc();
let status = registry
.promote_master_key_rotation(
&rotation.id,
MasterKeyIdentityCandidate {
epoch: rotation.target_epoch,
fingerprint: target_crypto.master_key_fingerprint(),
cipher_contract: MASTER_KEY_CIPHER_CONTRACT,
observed_at: &now,
},
&now,
)
.await
.map_err(CliError::from_registry)?;
println!(
"{}",
json!({
"status": status.state,
"rotation_id": status.id,
"active_epoch": status.target_epoch,
})
);
}
"abort" => {
let rotation_id = options
.rotation_id
.as_deref()
.ok_or_else(|| CliError::new("invalid_command", "cli.arguments", "run_status"))?;
let now = OffsetDateTime::now_utc();
let status = registry
.abort_master_key_rotation(rotation_id, &now)
.await
.map_err(CliError::from_registry)?;
println!(
"{}",
json!({
"status": status.state,
"rotation_id": status.id,
"active_epoch": status.source_epoch,
})
);
}
_ => {
return Err(CliError::new(
"invalid_command",
"cli.arguments",
"run_master_key_status",
));
}
}
Ok(ExitCode::SUCCESS)
}
#[derive(Default)]
struct MasterKeyOptions {
current_key_file: Option<String>,
target_key_file: Option<String>,
backup_ref: Option<String>,
rotation_id: Option<String>,
max_versions: Option<usize>,
}
impl MasterKeyOptions {
fn parse(arguments: &[String]) -> Result<Self, CliError> {
let mut options = Self::default();
let mut index = 0;
while index < arguments.len() {
let key = arguments[index].as_str();
let Some(value) = arguments.get(index + 1) else {
return Err(CliError::new(
"invalid_command",
"cli.arguments",
"run_master_key_status",
));
};
match key {
"--current-key-file" => options.current_key_file = Some(value.clone()),
"--target-key-file" => options.target_key_file = Some(value.clone()),
"--backup-ref" => options.backup_ref = Some(value.clone()),
"--rotation-id" => options.rotation_id = Some(value.clone()),
"--max-versions" => {
let parsed = value.parse::<usize>().map_err(|_| {
CliError::new("invalid_command", "cli.arguments", "run_master_key_status")
})?;
if parsed == 0 || parsed > 10_000 {
return Err(CliError::new(
"invalid_command",
"cli.arguments",
"run_master_key_status",
));
}
options.max_versions = Some(parsed);
}
_ => {
return Err(CliError::new(
"invalid_command",
"cli.arguments",
"run_master_key_status",
));
}
}
index += 2;
}
Ok(options)
}
}
struct MasterKeyPreflight {
source_epoch: i64,
target_epoch: i64,
affected_secret_versions: usize,
backup_ref: Option<String>,
current_crypto: SecretCrypto,
target_crypto: SecretCrypto,
}
async fn master_key_preflight(
registry: &PostgresRegistry,
options: &MasterKeyOptions,
) -> Result<MasterKeyPreflight, CliError> {
let current_key = read_required_key_file(options.current_key_file.as_deref())?;
let target_key = read_required_key_file(options.target_key_file.as_deref())?;
let status = registry
.master_key_rotation_status()
.await
.map_err(CliError::from_registry)?;
if status.rotations.iter().any(|rotation| {
matches!(
rotation.state.as_str(),
"running" | "verifying" | "verified"
)
}) {
return Err(CliError::new(
"master_key_rotation_in_progress",
"master_key.rotation",
"resume_verify_promote_or_abort_rotation",
));
}
let active = status.active_identity.ok_or_else(|| {
CliError::new(
"master_key_identity_missing",
"master_key.identity",
"start_secret_process_once",
)
})?;
let current_crypto = SecretCrypto::with_epoch(&current_key, active.epoch).map_err(|_| {
CliError::new(
"master_key_invalid",
"master_key.input",
"verify_operator_input",
)
})?;
if current_crypto.master_key_fingerprint() != active.fingerprint {
return Err(CliError::new(
"master_key_identity_mismatch",
"master_key.identity",
"use_matching_current_key",
));
}
let target_epoch = active.epoch + 1;
let target_crypto = SecretCrypto::with_epoch(&target_key, target_epoch).map_err(|_| {
CliError::new(
"master_key_invalid",
"master_key.input",
"verify_operator_input",
)
})?;
if target_crypto.master_key_fingerprint() == current_crypto.master_key_fingerprint()
|| registry
.master_key_fingerprint_exists(target_crypto.master_key_fingerprint())
.await
.map_err(CliError::from_registry)?
{
return Err(CliError::new(
"master_key_rotation_conflict",
"master_key.rotation",
"choose_new_target_key",
));
}
let affected_secret_versions =
count_and_verify_current_versions(registry, active.epoch, &current_crypto).await?;
Ok(MasterKeyPreflight {
source_epoch: active.epoch,
target_epoch,
affected_secret_versions,
backup_ref: options.backup_ref.clone(),
current_crypto,
target_crypto,
})
}
async fn rotation_crypto_for_resume(
registry: &PostgresRegistry,
options: &MasterKeyOptions,
rotation: &MasterKeyRotationRecord,
) -> Result<(SecretCrypto, SecretCrypto), CliError> {
let current_key = read_required_key_file(options.current_key_file.as_deref())?;
let target_key = read_required_key_file(options.target_key_file.as_deref())?;
let active = registry
.active_master_key_identity()
.await
.map_err(CliError::from_registry)?
.ok_or_else(|| {
CliError::new(
"master_key_identity_missing",
"master_key.identity",
"start_secret_process_once",
)
})?;
let current_crypto =
SecretCrypto::with_epoch(&current_key, rotation.source_epoch).map_err(|_| {
CliError::new(
"master_key_invalid",
"master_key.input",
"verify_operator_input",
)
})?;
if active.epoch != rotation.source_epoch
|| active.fingerprint != current_crypto.master_key_fingerprint()
{
return Err(CliError::new(
"master_key_identity_mismatch",
"master_key.identity",
"use_matching_current_key",
));
}
let target_crypto =
SecretCrypto::with_epoch(&target_key, rotation.target_epoch).map_err(|_| {
CliError::new(
"master_key_invalid",
"master_key.input",
"verify_operator_input",
)
})?;
if target_crypto.master_key_fingerprint() != rotation.target_fingerprint {
return Err(CliError::new(
"master_key_identity_mismatch",
"master_key.identity",
"use_matching_target_key",
));
}
Ok((current_crypto, target_crypto))
}
async fn process_rotation_batch(
registry: &PostgresRegistry,
rotation: &MasterKeyRotationRecord,
current_crypto: &SecretCrypto,
target_crypto: &SecretCrypto,
max_versions: Option<usize>,
) -> Result<i64, CliError> {
let mut processed = 0_i64;
let mut after_secret_id: Option<String> = None;
let mut after_version: Option<u32> = None;
loop {
let versions = registry
.list_secret_versions_for_master_key_epoch_page(
rotation.source_epoch,
after_secret_id.as_deref(),
after_version,
MASTER_KEY_ROTATION_PAGE_SIZE,
)
.await
.map_err(CliError::from_registry)?;
if versions.is_empty() {
break;
}
for version in versions {
after_secret_id = Some(version.secret_version.secret_id.as_str().to_owned());
after_version = Some(version.secret_version.version);
if version.target_master_key_epoch == Some(rotation.target_epoch) {
continue;
}
if max_versions.is_some_and(|limit| processed as usize >= limit) {
return Ok(processed);
}
let plaintext = decrypt_current(&version, current_crypto)?;
let target_ciphertext = target_crypto.encrypt(&plaintext).map_err(|_| {
CliError::new(
"master_key_rotation_verification_failed",
"master_key.rotation",
"rerun_rotation_or_abort",
)
})?;
let now = OffsetDateTime::now_utc();
registry
.stage_master_key_rotation_ciphertext(
&rotation.id,
&version.secret_version.secret_id,
version.secret_version.version,
rotation.source_epoch,
&target_ciphertext,
target_crypto.key_version(),
rotation.target_epoch,
&now,
)
.await
.map_err(CliError::from_registry)?;
processed += 1;
}
}
Ok(processed)
}
async fn verify_staged_targets(
registry: &PostgresRegistry,
rotation: &MasterKeyRotationRecord,
target_crypto: &SecretCrypto,
) -> Result<i64, CliError> {
let mut verified = 0_i64;
let mut after_secret_id: Option<String> = None;
let mut after_version: Option<u32> = None;
loop {
let versions = registry
.list_target_secret_versions_for_master_key_rotation_page(
rotation.target_epoch,
after_secret_id.as_deref(),
after_version,
MASTER_KEY_ROTATION_PAGE_SIZE,
)
.await
.map_err(CliError::from_registry)?;
if versions.is_empty() {
break;
}
for version in versions {
after_secret_id = Some(version.secret_version.secret_id.as_str().to_owned());
after_version = Some(version.secret_version.version);
let ciphertext = version.target_ciphertext.as_deref().ok_or_else(|| {
CliError::new(
"master_key_rotation_verification_failed",
"master_key.rotation",
"rerun_rotation_or_abort",
)
})?;
let key_version = version.target_key_version.as_deref().ok_or_else(|| {
CliError::new(
"master_key_rotation_verification_failed",
"master_key.rotation",
"rerun_rotation_or_abort",
)
})?;
target_crypto
.decrypt_for_epoch(key_version, rotation.target_epoch, ciphertext)
.map_err(|_| {
CliError::new(
"master_key_rotation_verification_failed",
"master_key.rotation",
"rerun_rotation_or_abort",
)
})?;
verified += 1;
}
}
if verified != rotation.total_secret_versions {
return Err(CliError::new(
"master_key_rotation_verification_failed",
"master_key.rotation",
"rerun_rotation_or_abort",
));
}
Ok(verified)
}
async fn count_and_verify_current_versions(
registry: &PostgresRegistry,
epoch: i64,
current_crypto: &SecretCrypto,
) -> Result<usize, CliError> {
let mut count = 0_usize;
let mut after_secret_id: Option<String> = None;
let mut after_version: Option<u32> = None;
loop {
let versions = registry
.list_secret_versions_for_master_key_epoch_page(
epoch,
after_secret_id.as_deref(),
after_version,
MASTER_KEY_ROTATION_PAGE_SIZE,
)
.await
.map_err(CliError::from_registry)?;
if versions.is_empty() {
break;
}
for version in versions {
after_secret_id = Some(version.secret_version.secret_id.as_str().to_owned());
after_version = Some(version.secret_version.version);
decrypt_current(&version, current_crypto)?;
count += 1;
}
}
Ok(count)
}
fn decrypt_current(
version: &SecretVersionRecord,
current_crypto: &SecretCrypto,
) -> Result<serde_json::Value, CliError> {
current_crypto
.decrypt_for_epoch(
&version.secret_version.key_version,
version.master_key_epoch,
&version.secret_version.ciphertext,
)
.map_err(|_| {
CliError::new(
"master_key_rotation_verification_failed",
"master_key.rotation",
"rerun_rotation_or_abort",
)
})
}
async fn active_rotation(
registry: &PostgresRegistry,
allowed_states: &[&str],
) -> Result<Option<MasterKeyRotationRecord>, CliError> {
let status = registry
.master_key_rotation_status()
.await
.map_err(CliError::from_registry)?;
Ok(status
.rotations
.into_iter()
.find(|rotation| allowed_states.contains(&rotation.state.as_str())))
}
fn rotation_json(rotation: &MasterKeyRotationRecord) -> serde_json::Value {
json!({
"id": rotation.id,
"source_epoch": rotation.source_epoch,
"target_epoch": rotation.target_epoch,
"state": rotation.state,
"backup_ref": rotation.backup_ref.as_ref().map(|_| "configured"),
"checkpoint_secret_id": rotation.checkpoint_secret_id,
"total_secret_versions": rotation.total_secret_versions,
"processed_secret_versions": rotation.processed_secret_versions,
"verified_secret_versions": rotation.verified_secret_versions,
"failure_code": rotation.failure_code,
})
}
fn read_required_key_file(path: Option<&str>) -> Result<String, CliError> {
let path = path.ok_or_else(|| {
CliError::new("invalid_command", "cli.arguments", "run_master_key_status")
})?;
if path.len() > 512 || path.bytes().any(|byte| byte.is_ascii_control()) {
return Err(CliError::new(
"master_key_input_invalid",
"master_key.input",
"verify_operator_input",
));
}
let metadata = std::fs::metadata(Path::new(path)).map_err(|_| {
CliError::new(
"master_key_input_invalid",
"master_key.input",
"verify_operator_input",
)
})?;
if !metadata.is_file() || metadata.len() > 16_384 {
return Err(CliError::new(
"master_key_input_invalid",
"master_key.input",
"verify_operator_input",
));
}
let value = std::fs::read_to_string(Path::new(path)).map_err(|_| {
CliError::new(
"master_key_input_invalid",
"master_key.input",
"verify_operator_input",
)
})?;
if value.trim().is_empty() {
return Err(CliError::new(
"master_key_input_invalid",
"master_key.input",
"verify_operator_input",
));
}
Ok(value)
}
fn ensure_no_options(options: &MasterKeyOptions) -> Result<(), CliError> {
if options.current_key_file.is_some()
|| options.target_key_file.is_some()
|| options.backup_ref.is_some()
|| options.rotation_id.is_some()
|| options.max_versions.is_some()
{
return Err(CliError::new(
"invalid_command",
"cli.arguments",
"run_master_key_status",
));
}
Ok(())
}
@@ -0,0 +1,342 @@
use base64::{Engine as _, engine::general_purpose::URL_SAFE_NO_PAD};
use crank_community_auth::hash_password;
use crank_config::{ConfigSource, parse_migrator};
use crank_registry::{
CreateAdminBootstrapContractRequest, MASTER_KEY_CIPHER_CONTRACT, PostgresRegistry,
RecoverAdminPasswordRequest,
};
use crank_runtime::SecretCrypto;
use rand::RngExt;
use serde_json::json;
use sha2::{Digest, Sha256};
use std::{
path::{Path, PathBuf},
process::ExitCode,
};
use time::{Duration as TimeDuration, OffsetDateTime};
use super::{CliError, db_connect::connect_registry};
pub(super) async fn run_admin_auth(arguments: &[String]) -> Result<ExitCode, CliError> {
let Some(command) = arguments.first().map(String::as_str) else {
return Err(CliError::new(
"invalid_command",
"cli.arguments",
"run_admin_auth_bootstrap_create",
));
};
let options = AdminAuthOptions::parse(&arguments[1..])?;
let config = parse_migrator(
ConfigSource::from_os_for_migrator()
.map_err(|_| CliError::new("config_invalid", "config.source", "run_preflight"))?,
)
.map_err(|_| CliError::new("config_invalid", "config.validate", "run_preflight"))?;
let registry = connect_registry(&config.database).await?;
match command {
"bootstrap-create" => create_bootstrap_contract(&registry, options).await?,
"bootstrap-complete" => complete_bootstrap_contract(&registry, options).await?,
"recover" => recover_admin_password(&registry, options).await?,
_ => {
return Err(CliError::new(
"invalid_command",
"cli.arguments",
"run_admin_auth_bootstrap_create",
));
}
}
Ok(ExitCode::SUCCESS)
}
async fn complete_bootstrap_contract(
registry: &PostgresRegistry,
options: AdminAuthOptions,
) -> Result<(), CliError> {
let token_path = options
.token_file
.as_deref()
.ok_or_else(|| CliError::new("invalid_command", "cli.arguments", "provide_token_file"))?;
let password_path = options.password_file.as_deref().ok_or_else(|| {
CliError::new("invalid_command", "cli.arguments", "provide_password_file")
})?;
let pepper_path = options.password_pepper_file.as_deref().ok_or_else(|| {
CliError::new(
"invalid_command",
"cli.arguments",
"provide_password_pepper_file",
)
})?;
let token = read_secret_file(token_path, "admin_auth.bootstrap_token")?;
let password = read_secret_file(password_path, "admin_auth.password")?;
let pepper = read_secret_file(pepper_path, "admin_auth.password_pepper")?;
if !(32..=256).contains(&token.len())
|| !(12..=256).contains(&password.len())
|| pepper.is_empty()
|| pepper.len() > 1_024
{
return Err(CliError::new(
"invalid_command",
"cli.arguments",
"provide_bounded_secret_files",
));
}
let token_hash = admin_auth_hash("bootstrap", &token);
let password_hash = hash_password(&password, &pepper).map_err(|_| {
CliError::new(
"admin_bootstrap_rejected",
"admin_auth.bootstrap",
"verify_local_inputs",
)
})?;
let now = OffsetDateTime::now_utc();
let user_id = registry
.consume_admin_bootstrap_contract(crank_registry::ConsumeAdminBootstrapContractRequest {
token_hash: &token_hash,
password_hash: &password_hash,
now: &now,
})
.await
.map_err(CliError::from_registry)?;
println!(
"{}",
json!({
"status": "bootstrap_completed",
"user_id": user_id.as_str()
})
);
Ok(())
}
async fn recover_admin_password(
registry: &PostgresRegistry,
options: AdminAuthOptions,
) -> Result<(), CliError> {
let email = options
.email
.as_deref()
.ok_or_else(|| CliError::new("invalid_command", "cli.arguments", "provide_email"))?;
let password_path = options.password_file.as_deref().ok_or_else(|| {
CliError::new("invalid_command", "cli.arguments", "provide_password_file")
})?;
let pepper_path = options.password_pepper_file.as_deref().ok_or_else(|| {
CliError::new(
"invalid_command",
"cli.arguments",
"provide_password_pepper_file",
)
})?;
let master_key_path = options.master_key_file.as_deref().ok_or_else(|| {
CliError::new(
"invalid_command",
"cli.arguments",
"provide_master_key_file",
)
})?;
let master_key = read_secret_file(master_key_path, "master_key.input")?;
let active = registry
.active_master_key_identity()
.await
.map_err(CliError::from_registry)?
.ok_or_else(|| {
CliError::new(
"master_key_identity_missing",
"master_key.identity",
"start_service_once_with_current_master_key",
)
})?;
let crypto = SecretCrypto::with_epoch(&master_key, active.epoch).map_err(|_| {
CliError::new(
"master_key_invalid",
"master_key.input",
"provide_current_master_key_file",
)
})?;
if active.cipher_contract != MASTER_KEY_CIPHER_CONTRACT
|| active.fingerprint != crypto.master_key_fingerprint()
{
return Err(CliError::new(
"master_key_identity_mismatch",
"master_key.identity",
"use_matching_master_key",
));
}
let password = read_secret_file(password_path, "admin_auth.password")?;
let pepper = read_secret_file(pepper_path, "admin_auth.password_pepper")?;
if !(12..=256).contains(&password.len()) || pepper.is_empty() || pepper.len() > 1_024 {
return Err(CliError::new(
"invalid_command",
"cli.arguments",
"provide_bounded_secret_files",
));
}
let password_hash = hash_password(&password, &pepper).map_err(|_| {
CliError::new(
"admin_recovery_rejected",
"admin_auth.recovery",
"verify_local_inputs",
)
})?;
let audit_id = format!("audit_{}", uuid::Uuid::now_v7().simple());
let user_id = registry
.recover_admin_password(RecoverAdminPasswordRequest {
email,
password_hash: &password_hash,
audit_id: &audit_id,
})
.await
.map_err(CliError::from_registry)?;
println!(
"{}",
json!({
"status": "admin_recovered",
"user_id": user_id.as_str(),
"sessions_revoked": true,
"audit_id": audit_id
})
);
Ok(())
}
async fn create_bootstrap_contract(
registry: &PostgresRegistry,
options: AdminAuthOptions,
) -> Result<(), CliError> {
let email = options
.email
.as_deref()
.ok_or_else(|| CliError::new("invalid_command", "cli.arguments", "provide_email"))?;
let display_name = options.display_name.as_deref().unwrap_or("Crank Owner");
let ttl_seconds = options.ttl_seconds.unwrap_or(900);
if !(60..=86_400).contains(&ttl_seconds) {
return Err(CliError::new(
"invalid_command",
"cli.arguments",
"set_ttl_between_60_and_86400",
));
}
let token = random_token();
let contract_id = format!("boot_{}", uuid::Uuid::now_v7().simple());
let token_hash = admin_auth_hash("bootstrap", &token);
let now = OffsetDateTime::now_utc();
let expires_at = now
.checked_add(TimeDuration::seconds(ttl_seconds))
.ok_or_else(|| CliError::new("invalid_command", "cli.arguments", "reduce_ttl"))?;
let contract = registry
.create_admin_bootstrap_contract(CreateAdminBootstrapContractRequest {
id: &contract_id,
token_hash: &token_hash,
email,
display_name,
expires_at: &expires_at,
})
.await
.map_err(CliError::from_registry)?;
println!(
"{}",
json!({
"status": "bootstrap_created",
"contract_id": contract.id,
"expires_at": contract.expires_at,
"bootstrap_token": token,
"warning": "copy_once_token_not_logged_by_services"
})
);
Ok(())
}
#[derive(Default)]
struct AdminAuthOptions {
email: Option<String>,
display_name: Option<String>,
ttl_seconds: Option<i64>,
token_file: Option<PathBuf>,
password_file: Option<PathBuf>,
password_pepper_file: Option<PathBuf>,
master_key_file: Option<PathBuf>,
}
impl AdminAuthOptions {
fn parse(arguments: &[String]) -> Result<Self, CliError> {
let mut options = Self::default();
let mut index = 0;
while index < arguments.len() {
let key = arguments[index].as_str();
let Some(value) = arguments.get(index + 1) else {
return Err(CliError::new(
"invalid_command",
"cli.arguments",
"run_admin_auth_bootstrap_create",
));
};
match key {
"--email" => options.email = Some(value.clone()),
"--display-name" => options.display_name = Some(value.clone()),
"--ttl-seconds" => {
options.ttl_seconds = Some(value.parse::<i64>().map_err(|_| {
CliError::new("invalid_command", "cli.arguments", "set_ttl_seconds")
})?);
}
"--password-file" => options.password_file = Some(PathBuf::from(value)),
"--token-file" => options.token_file = Some(PathBuf::from(value)),
"--password-pepper-file" => {
options.password_pepper_file = Some(PathBuf::from(value));
}
"--master-key-file" => options.master_key_file = Some(PathBuf::from(value)),
_ => {
return Err(CliError::new(
"invalid_command",
"cli.arguments",
"run_admin_auth_bootstrap_create",
));
}
}
index += 2;
}
Ok(options)
}
}
fn read_secret_file(path: &Path, stage: &'static str) -> Result<String, CliError> {
let metadata = std::fs::metadata(path)
.map_err(|_| CliError::new("invalid_command", stage, "provide_readable_secret_file"))?;
if !metadata.is_file() || metadata.len() > 8_192 {
return Err(CliError::new(
"invalid_command",
stage,
"provide_bounded_secret_file",
));
}
let bytes = std::fs::read(path)
.map_err(|_| CliError::new("invalid_command", stage, "provide_readable_secret_file"))?;
if bytes.len() > 8_192 {
return Err(CliError::new(
"invalid_command",
stage,
"provide_bounded_secret_file",
));
}
let mut value = String::from_utf8(bytes)
.map_err(|_| CliError::new("invalid_command", stage, "provide_utf8_secret_file"))?;
if value.ends_with("\r\n") {
value.truncate(value.len() - 2);
} else if value.ends_with('\n') {
value.truncate(value.len() - 1);
}
Ok(value)
}
fn random_token() -> String {
let mut bytes = [0_u8; 32];
rand::rng().fill(&mut bytes);
URL_SAFE_NO_PAD.encode(bytes)
}
fn admin_auth_hash(scope: &str, token: &str) -> String {
let mut digest = Sha256::new();
digest.update(scope.as_bytes());
digest.update(b":");
digest.update(token.as_bytes());
URL_SAFE_NO_PAD.encode(digest.finalize())
}
@@ -0,0 +1,78 @@
use crank_config::DatabaseSettings;
use crank_registry::{PostgresPoolConfig, PostgresRegistry, RegistryError};
use sqlx::{
PgPool,
postgres::{PgConnectOptions, PgPoolOptions},
};
use std::time::Duration;
use super::CliError;
pub(super) async fn connect(config: &DatabaseSettings) -> Result<PgPool, CliError> {
let options = connect_options(config)?;
for attempt in 1..=10 {
let result = PgPoolOptions::new()
.max_connections(config.pool.max_connections)
.min_connections(config.pool.min_connections)
.acquire_timeout(Duration::from_millis(config.pool.acquire_timeout_ms))
.idle_timeout(Duration::from_millis(config.pool.idle_timeout_ms))
.max_lifetime(Duration::from_millis(config.pool.max_lifetime_ms))
.connect_with(options.clone())
.await;
match result {
Ok(pool) => return Ok(pool),
Err(_) if attempt < 10 => tokio::time::sleep(Duration::from_secs(1)).await,
Err(_) => break,
}
}
Err(CliError::new(
"storage_unavailable",
"database.connect",
"contact_operator",
))
}
pub(super) async fn connect_registry(
config: &DatabaseSettings,
) -> Result<PostgresRegistry, CliError> {
let options = connect_options(config)?;
let pool_config = PostgresPoolConfig {
max_connections: config.pool.max_connections,
min_connections: config.pool.min_connections,
acquire_timeout_ms: config.pool.acquire_timeout_ms,
idle_timeout_ms: config.pool.idle_timeout_ms,
max_lifetime_ms: config.pool.max_lifetime_ms,
};
for attempt in 1..=10 {
let result =
PostgresRegistry::connect_with_options_and_pool_config(options.clone(), pool_config)
.await;
match result {
Ok(registry) => return Ok(registry),
Err(RegistryError::Storage(_)) if attempt < 10 => {
tokio::time::sleep(Duration::from_secs(1)).await;
}
Err(error) => return Err(CliError::from_registry(error)),
}
}
Err(CliError::new(
"storage_unavailable",
"database.connect",
"contact_operator",
))
}
fn connect_options(config: &DatabaseSettings) -> Result<PgConnectOptions, CliError> {
if let Some(url) = &config.url {
url.expose_secret()
.parse::<PgConnectOptions>()
.map_err(|_| CliError::new("config_invalid", "database.source", "run_preflight"))
} else {
Ok(PgConnectOptions::new()
.host(&config.host)
.port(config.port)
.database(&config.database)
.username(&config.username)
.password(config.password.expose_secret()))
}
}
+385 -14
View File
@@ -1,13 +1,15 @@
use crank_core::{
AgentId, AgentStatus, ApprovalRequestStatus, AuthConfig, AuthKind, ExecutionMode, ExportMode,
GeneratedDraft, InvocationLevel, InvocationSource, InvocationStatus, OperationSecurityLevel,
OperationStatus, PlatformApiKeyKind, PlatformApiKeyScope, Protocol, SecretKind, Target,
ToolSelectionPolicy, UsagePeriod, WizardState, WorkspaceId, WorkspaceStatus,
GeneratedDraft, InvocationLevel, InvocationSource, InvocationStatus, OperationAvailability,
OperationSecurityLevel, OperationStatus, OperationVersionState, PlatformApiKeyKind,
PlatformApiKeyScope, Protocol, SecretKind, Target, ToolSelectionPolicy, UsagePeriod,
WizardState, WorkspaceId, WorkspaceStatus,
};
use crank_mapping::MappingSet;
use crank_registry::{
PlatformApiKeyRecord, RegistryOperation, UsageAgentBreakdown, UsageOperationBreakdown,
UsageSummary, UsageTimelinePoint, WorkspaceMembershipRecord, WorkspaceRecord,
InvocationLogRecord, PlatformApiKeyRecord, RegistryOperation, UsageAgentBreakdown,
UsageOperationBreakdown, UsageOutcomeGroup, UsageSummary, UsageTimelinePoint,
WorkspaceMembershipRecord, WorkspaceRecord,
};
use crank_schema::Schema;
use serde::{Deserialize, Serialize};
@@ -19,11 +21,23 @@ pub struct LoginPayload {
pub password: String,
}
#[derive(Clone, Debug, Deserialize)]
pub struct CompleteBootstrapPayload {
pub token: String,
pub password: String,
}
#[derive(Clone, Debug, Serialize)]
pub struct BootstrapStatusResponse {
pub bootstrap_required: bool,
}
#[derive(Clone, Debug, Serialize)]
pub struct SessionResponse {
pub user: crank_core::User,
pub memberships: Vec<WorkspaceMembershipRecord>,
pub current_workspace_id: Option<String>,
pub csrf_token: String,
}
#[derive(Clone, Debug, Deserialize)]
@@ -80,12 +94,19 @@ pub struct PublishPayload {
pub struct TestRunPayload {
pub version: u32,
pub input: Value,
#[serde(default)]
pub confirmation_token: Option<String>,
#[serde(default)]
pub locale: Option<String>,
}
#[derive(Clone, Debug, Serialize)]
pub struct TestRunResult {
pub ok: bool,
pub mode: ExecutionMode,
pub tested_version: u32,
pub request_id: String,
pub trace_id: String,
pub request_preview: Value,
pub response_preview: Value,
pub errors: Vec<Value>,
@@ -174,6 +195,22 @@ pub struct ToolSearchPreviewPayload {
pub tool_selection_policy: ToolSelectionPolicy,
}
/// Точная Published Resource Version в Config-форме `POST .../bindings`
/// (Story 3.7 AC04): положительный exact integer, не `latest`.
#[derive(Clone, Debug, Deserialize)]
pub struct AgentResourceBindingPayload {
pub resource_id: String,
pub resource_version: u32,
}
/// Точная Published Prompt Version в Config-форме `POST .../bindings`
/// (Story 3.13 AC2): положительный exact integer, не `latest`.
#[derive(Clone, Debug, Deserialize)]
pub struct AgentPromptBindingPayload {
pub prompt_id: String,
pub prompt_version: u32,
}
#[derive(Clone, Debug, Deserialize)]
#[serde(untagged)]
pub enum AgentCatalogPayload {
@@ -181,17 +218,39 @@ pub enum AgentCatalogPayload {
Config {
bindings: Vec<AgentBindingPayload>,
tool_selection_policy: ToolSelectionPolicy,
/// Отсутствие поля сохраняет прежние Resource bindings, `[]` очищает,
/// список заменяет их в Draft (TL interface decision 1).
#[serde(default)]
resource_bindings: Option<Vec<AgentResourceBindingPayload>>,
/// Tri-state exact Prompt bindings (Story 3.13): absent сохраняет,
/// `[]` очищает, список заменяет.
#[serde(default)]
prompt_bindings: Option<Vec<AgentPromptBindingPayload>>,
},
}
type AgentCatalogParts = (
Vec<AgentBindingPayload>,
Option<ToolSelectionPolicy>,
Option<Vec<AgentResourceBindingPayload>>,
Option<Vec<AgentPromptBindingPayload>>,
);
impl AgentCatalogPayload {
pub fn into_parts(self) -> (Vec<AgentBindingPayload>, Option<ToolSelectionPolicy>) {
pub fn into_parts(self) -> AgentCatalogParts {
match self {
Self::Bindings(bindings) => (bindings, None),
Self::Bindings(bindings) => (bindings, None, None, None),
Self::Config {
bindings,
tool_selection_policy,
} => (bindings, Some(tool_selection_policy)),
resource_bindings,
prompt_bindings,
} => (
bindings,
Some(tool_selection_policy),
resource_bindings,
prompt_bindings,
),
}
}
}
@@ -228,6 +287,7 @@ pub struct AgentSummaryView {
pub status: AgentStatus,
pub current_draft_version: u32,
pub latest_published_version: Option<u32>,
pub catalog_revision: i64,
pub created_at: String,
pub updated_at: String,
pub published_at: Option<String>,
@@ -266,6 +326,152 @@ fn default_platform_api_key_kind() -> PlatformApiKeyKind {
pub struct CreatedPlatformApiKeyResponse {
pub api_key: PlatformApiKeyRecord,
pub secret: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub connection: Option<EphemeralMcpConnection>,
}
#[derive(Clone, Debug, Serialize)]
pub struct EphemeralMcpClientConfig {
pub client: String,
pub config: Value,
}
#[derive(Clone, Debug, Serialize)]
pub struct EphemeralMcpConnection {
pub endpoint: String,
pub clients: Vec<EphemeralMcpClientConfig>,
pub secret_display: &'static str,
}
#[derive(Clone, Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct OnboardingEventPayload {
pub event: String,
pub idempotency_key: String,
pub expected_revision: i64,
#[serde(default)]
pub completed_steps: Option<Value>,
}
#[derive(Clone, Debug, Serialize)]
pub struct OnboardingStepView {
pub id: crank_core::OnboardingStepId,
pub completed: bool,
pub status: String,
pub action_code: String,
pub reason_code: String,
}
#[derive(Clone, Debug, Serialize)]
pub struct OnboardingFirstCallEvidence {
pub log_id: String,
pub agent_id: String,
pub key_id: String,
pub operation_id: String,
pub operation_version: u32,
pub tool_name: String,
pub occurred_at: String,
pub request_id: Option<String>,
pub trace_id: Option<String>,
}
#[derive(Clone, Debug, Serialize)]
pub struct OnboardingResponse {
pub schema_version: u16,
pub workspace_id: String,
pub revision: i64,
pub status: String,
pub completed: bool,
pub eligible_since: Option<String>,
pub steps: Vec<OnboardingStepView>,
pub operation_id: Option<String>,
pub operation_version: Option<u32>,
pub agent_id: Option<String>,
pub catalog_revision: Option<i64>,
pub platform_api_key_id: Option<String>,
pub mcp_endpoint: Option<String>,
pub first_call: Option<OnboardingFirstCallEvidence>,
pub import_context: Option<OnboardingImportContextView>,
}
#[derive(Clone, Debug, Serialize)]
pub struct OnboardingImportChoiceView {
pub key: String,
pub name: String,
pub outcome: String,
pub operation_id: Option<String>,
pub version: Option<u32>,
pub reason: Option<String>,
pub selectable: bool,
}
#[derive(Clone, Debug, Serialize)]
pub struct OnboardingImportDriftView {
pub reason: String,
pub current_operation_version: Option<u32>,
}
#[derive(Clone, Debug, Serialize)]
pub struct OnboardingImportContextView {
pub application_idempotency_key: String,
pub plan_digest: String,
pub report: Value,
pub choices: Vec<OnboardingImportChoiceView>,
pub selected_operation_id: Option<String>,
pub selected_operation_version: Option<u32>,
pub drift: Option<OnboardingImportDriftView>,
}
#[derive(Clone, Debug, Serialize)]
pub struct OnboardingEventResponse {
pub accepted: bool,
#[serde(flatten)]
pub onboarding: OnboardingResponse,
}
#[derive(Clone, Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ResetOnboardingSelectionPayload {
pub expected_revision: i64,
}
#[derive(Clone, Debug, Serialize)]
pub struct ResetOnboardingSelectionResponse {
pub selection_reset: bool,
#[serde(flatten)]
pub onboarding: OnboardingResponse,
}
#[derive(Clone, Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct RegisterOnboardingImportContextPayload {
pub application_idempotency_key: String,
pub plan_digest: String,
pub expected_revision: i64,
}
#[derive(Clone, Debug, Serialize)]
pub struct RegisterOnboardingImportContextResponse {
pub registered: bool,
#[serde(flatten)]
pub onboarding: OnboardingResponse,
}
#[derive(Clone, Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct SelectOnboardingImportDraftPayload {
pub application_idempotency_key: String,
pub plan_digest: String,
pub operation_id: String,
pub operation_version: u32,
pub expected_revision: i64,
}
#[derive(Clone, Debug, Serialize)]
pub struct SelectOnboardingImportDraftResponse {
pub selected: bool,
#[serde(flatten)]
pub onboarding: OnboardingResponse,
}
#[derive(Clone, Debug, Serialize)]
@@ -285,11 +491,16 @@ pub struct WorkspaceCatalogSnapshotResponse {
#[derive(Clone, Debug, Deserialize)]
pub struct LogsQuery {
pub level: Option<InvocationLevel>,
pub status: Option<InvocationStatus>,
pub outcome_group: Option<UsageOutcomeGroup>,
pub search: Option<String>,
pub source: Option<InvocationSource>,
pub operation_id: Option<String>,
pub agent_id: Option<String>,
pub period: Option<UsagePeriod>,
pub created_after: Option<String>,
pub created_before: Option<String>,
pub cursor: Option<String>,
pub limit: Option<u32>,
}
@@ -299,10 +510,19 @@ pub struct ApprovalsQuery {
pub limit: Option<u32>,
}
#[derive(Clone, Debug, Deserialize)]
pub struct ApprovalDecisionPayload {
pub approve: String,
#[serde(default)]
pub note: Option<String>,
}
#[derive(Clone, Debug, Deserialize)]
pub struct UsageRequestQuery {
pub period: Option<UsagePeriod>,
pub source: Option<InvocationSource>,
pub created_after: Option<String>,
pub created_before: Option<String>,
}
#[derive(Clone, Debug, Serialize)]
@@ -311,6 +531,13 @@ pub struct UsageOverviewResponse {
pub timeline: Vec<UsageTimelinePoint>,
pub operations: Vec<UsageOperationBreakdown>,
pub agents: Vec<UsageAgentBreakdown>,
pub outcomes: Vec<crank_registry::UsageOutcomeBreakdown>,
}
#[derive(Clone, Debug, Serialize)]
pub struct LogsListResponse {
pub items: Vec<InvocationLogRecord>,
pub next_cursor: Option<String>,
}
#[derive(Clone, Debug, Serialize)]
@@ -362,15 +589,51 @@ pub struct ExportQuery {
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct YamlOperationDocument {
pub format_version: String,
pub kind: String,
pub operation: PortableOperation,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct PortableOperation {
pub name: String,
pub display_name: String,
#[serde(default = "default_operation_category")]
pub category: String,
pub protocol: Protocol,
#[serde(default)]
pub security_level: OperationSecurityLevel,
pub target: Target,
pub input_schema: Schema,
pub output_schema: Schema,
pub input_mapping: MappingSet,
pub output_mapping: MappingSet,
pub execution_config: crank_core::ExecutionConfig,
pub tool_description: crank_core::ToolDescription,
}
#[derive(Clone, Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct LegacyYamlOperationDocument {
pub format_version: String,
pub kind: String,
pub operation: RegistryOperation,
}
#[derive(Clone, Debug, Deserialize)]
pub struct OpenApiImportPreviewPayload {
pub document: String,
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum OpenApiUploadLocale {
En,
Ru,
}
#[derive(Clone, Debug)]
pub struct OpenApiUpload {
pub bytes: Vec<u8>,
pub mime_type: String,
pub locale: OpenApiUploadLocale,
}
#[derive(Clone, Debug, Serialize)]
@@ -378,6 +641,23 @@ pub struct OpenApiImportPreviewResponse {
pub job_id: String,
pub expires_at: String,
pub preview: crank_import::rest::ImportPreview,
#[serde(default)]
pub security_profile_choices: Vec<OpenApiImportSecurityProfileChoice>,
}
#[derive(Clone, Debug, Serialize)]
pub struct OpenApiImportSecurityProfileChoice {
pub operation_key: String,
pub alternative_index: usize,
pub schemes: Vec<String>,
pub profiles: Vec<OpenApiImportAuthProfileChoice>,
}
#[derive(Clone, Debug, Serialize)]
pub struct OpenApiImportAuthProfileChoice {
pub id: String,
pub name: String,
pub kind: crank_core::AuthKind,
}
#[derive(Clone, Debug, Deserialize)]
@@ -396,20 +676,104 @@ pub struct OpenApiImportCreateResponse {
pub findings: Vec<crank_import::rest::ImportFinding>,
}
#[derive(Clone, Debug, Serialize)]
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
pub struct OpenApiImportCreatedOperation {
pub operation_key: String,
pub operation_id: String,
pub name: String,
pub version: u32,
}
#[derive(Clone, Debug, Serialize)]
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
pub struct OpenApiImportSkippedOperation {
pub operation_key: String,
pub name: String,
pub reason: String,
}
#[derive(Clone, Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct OpenApiImportPlanCreatePayload {
#[serde(default = "default_openapi_plan_selection_mode")]
pub selection_mode: String,
#[serde(default)]
pub selected_operation_keys: Vec<String>,
#[serde(default)]
pub server_url: Option<String>,
#[serde(default)]
pub decisions: Vec<OpenApiImportPlanDecision>,
}
#[derive(Clone, Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct OpenApiImportPlanApplyPayload {
pub plan_digest: String,
pub idempotency_key: String,
}
fn default_openapi_plan_selection_mode() -> String {
"full".to_owned()
}
#[derive(Clone, Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct OpenApiImportPlanDecision {
pub operation_key: String,
pub action: String,
#[serde(default)]
pub target_operation_id: Option<String>,
#[serde(default)]
pub confirmed_name: Option<String>,
#[serde(default)]
pub auth_profile_id: Option<String>,
#[serde(default)]
pub security_alternative_index: Option<usize>,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
pub struct OpenApiImportPlanCounts {
pub selected: usize,
pub excluded: usize,
pub actionable: usize,
pub blocked: usize,
}
#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct OpenApiImportPlanCandidateView {
pub key: String,
pub name: String,
pub state: String,
pub action: Option<String>,
pub target_operation_id: Option<String>,
pub confirmed_name: Option<String>,
pub auth_profile_id: Option<String>,
pub security_alternative_index: Option<usize>,
pub reason: Option<String>,
}
#[derive(Clone, Debug, Serialize)]
pub struct OpenApiImportPlanCreateResponse {
pub plan_reference: String,
pub plan_digest: String,
pub catalog_revision: String,
pub support_matrix_version: String,
pub expires_at: String,
pub selection_mode: String,
pub counts: OpenApiImportPlanCounts,
pub candidates: Vec<OpenApiImportPlanCandidateView>,
}
#[derive(Clone, Debug, Serialize)]
pub struct OpenApiImportPlanRevalidationResponse {
pub plan_reference: String,
pub plan_digest: String,
pub revalidation_status: String,
pub revalidation_required: bool,
pub reason: Option<String>,
pub counts: OpenApiImportPlanCounts,
pub candidates: Vec<OpenApiImportPlanCandidateView>,
}
#[derive(Clone, Debug, Serialize)]
pub struct CreatedOperationResponse {
pub operation_id: String,
@@ -473,6 +837,7 @@ pub struct OperationSummaryView {
pub status: OperationStatus,
pub current_draft_version: u32,
pub latest_published_version: Option<u32>,
pub can_delete: bool,
pub created_at: String,
pub updated_at: String,
pub published_at: Option<String>,
@@ -490,6 +855,7 @@ pub struct OperationDetailView {
pub protocol: Protocol,
pub security_level: OperationSecurityLevel,
pub status: OperationStatus,
pub availability: OperationAvailability,
pub current_draft_version: u32,
pub latest_published_version: Option<u32>,
pub created_at: String,
@@ -503,7 +869,7 @@ pub struct OperationDetailView {
#[derive(Clone, Debug, Serialize)]
pub struct VersionRef {
pub version: u32,
pub status: OperationStatus,
pub status: OperationVersionState,
}
#[derive(Clone, Debug, Serialize)]
@@ -551,12 +917,17 @@ pub(crate) struct InvocationRecordRequest<'a> {
pub agent_id: Option<&'a AgentId>,
pub operation: &'a RegistryOperation,
pub request_id: Option<&'a str>,
pub trace_id: Option<&'a str>,
pub source: InvocationSource,
pub level: InvocationLevel,
pub status: InvocationStatus,
pub message: String,
pub status_code: Option<u16>,
pub error_kind: Option<String>,
pub execution_stage: Option<crank_core::ExecutionStage>,
pub execution_error_code: Option<crank_core::ExecutionErrorCode>,
pub retryability: Option<crank_core::Retryability>,
pub outcome_certainty: Option<crank_core::OutcomeCertainty>,
pub duration_ms: u64,
pub request_preview: Value,
pub response_preview: Value,
+604 -95
View File
@@ -11,6 +11,7 @@ use serde_json::{Value, json};
use thiserror::Error;
use tracing::{error, warn};
use crate::dto::OpenApiUploadLocale;
use crate::storage::StorageError;
#[derive(Debug, Error)]
@@ -31,6 +32,11 @@ pub enum ApiError {
context: Option<Value>,
},
#[error("{message}")]
Unprocessable {
message: String,
context: Option<Value>,
},
#[error("{message}")]
NotFound {
message: String,
context: Option<Value>,
@@ -41,11 +47,26 @@ pub enum ApiError {
context: Option<Value>,
},
#[error("{message}")]
PayloadTooLarge {
message: String,
context: Option<Value>,
},
#[error("{message}")]
PreconditionRequired {
message: String,
context: Option<Value>,
},
#[error("{message}")]
RateLimited {
message: String,
context: Option<Value>,
},
#[error("{message}")]
ServiceUnavailable {
message: String,
context: Option<Value>,
},
#[error("{message}")]
Internal {
message: String,
context: Option<Value>,
@@ -74,13 +95,111 @@ impl ApiError {
}
}
pub fn internal(message: impl Into<String>) -> Self {
pub fn internal(_message: impl Into<String>) -> Self {
Self::Internal {
message: message.into(),
message: "internal server error".to_owned(),
context: None,
}
}
pub(crate) fn openapi_upload(locale: OpenApiUploadLocale, code: &'static str) -> Self {
let russian = locale == OpenApiUploadLocale::Ru;
let message = match (russian, code) {
(_, "file_too_large") => {
if russian {
"файл OpenAPI превышает лимит 256 КиБ"
} else {
"OpenAPI file exceeds the 256 KiB limit"
}
}
(_, "empty_file") => {
if russian {
"файл OpenAPI не должен быть пустым"
} else {
"OpenAPI file must not be empty"
}
}
(_, "invalid_utf8") => {
if russian {
"файл OpenAPI должен быть в UTF-8"
} else {
"OpenAPI file must be UTF-8"
}
}
(_, "invalid_media_type") => {
if russian {
"тип файла OpenAPI не поддерживается"
} else {
"OpenAPI file type is not supported"
}
}
(_, "invalid_document") => {
if russian {
"некорректный или неподдерживаемый документ OpenAPI"
} else {
"OpenAPI document is invalid or unsupported"
}
}
(_, "no_methods") => {
if russian {
"документ OpenAPI не содержит поддерживаемых методов"
} else {
"OpenAPI document contains no supported methods"
}
}
(_, "source_integrity") => {
if russian {
"проверка целостности источника OpenAPI не пройдена"
} else {
"OpenAPI source integrity verification failed"
}
}
(_, "source_unavailable") => {
if russian {
"источник OpenAPI недоступен"
} else {
"OpenAPI source is unavailable"
}
}
(_, "parser_unavailable") | (_, "storage_unavailable") => {
if russian {
"обработка OpenAPI временно недоступна"
} else {
"OpenAPI processing is temporarily unavailable"
}
}
_ => {
if russian {
"некорректная multipart-загрузка OpenAPI"
} else {
"invalid OpenAPI multipart upload"
}
}
};
let context = json!({ "error_code": format!("openapi_upload.{code}") });
if code == "file_too_large" {
Self::PayloadTooLarge {
message: message.to_owned(),
context: Some(context),
}
} else if matches!(code, "storage_unavailable" | "parser_unavailable") {
Self::Internal {
message: message.to_owned(),
context: Some(context),
}
} else if matches!(code, "source_integrity" | "source_unavailable") {
Self::Unprocessable {
message: message.to_owned(),
context: Some(context),
}
} else {
Self::Validation {
message: message.to_owned(),
context: Some(context),
}
}
}
pub(crate) fn rate_limited_with_context(message: impl Into<String>, context: Value) -> Self {
Self::RateLimited {
message: message.into(),
@@ -88,6 +207,16 @@ impl ApiError {
}
}
pub(crate) fn service_unavailable_with_context(
message: impl Into<String>,
context: Value,
) -> Self {
Self::ServiceUnavailable {
message: message.into(),
context: Some(context),
}
}
pub(crate) fn validation_with_context(message: impl Into<String>, context: Value) -> Self {
Self::Validation {
message: message.into(),
@@ -95,6 +224,13 @@ impl ApiError {
}
}
pub(crate) fn unprocessable_with_context(message: impl Into<String>, context: Value) -> Self {
Self::Unprocessable {
message: message.into(),
context: Some(context),
}
}
pub(crate) fn not_found_with_context(message: impl Into<String>, context: Value) -> Self {
Self::NotFound {
message: message.into(),
@@ -109,26 +245,61 @@ impl ApiError {
}
}
pub(crate) fn payload_too_large_with_context(
message: impl Into<String>,
context: Value,
) -> Self {
Self::PayloadTooLarge {
message: message.into(),
context: Some(context),
}
}
pub(crate) fn precondition_required_with_context(
message: impl Into<String>,
context: Value,
) -> Self {
Self::PreconditionRequired {
message: message.into(),
context: Some(context),
}
}
fn status_code(&self) -> StatusCode {
match self {
Self::Unauthorized { .. } => StatusCode::UNAUTHORIZED,
Self::Forbidden { .. } => StatusCode::FORBIDDEN,
Self::Validation { .. } => StatusCode::BAD_REQUEST,
Self::Unprocessable { .. } => StatusCode::UNPROCESSABLE_ENTITY,
Self::NotFound { .. } => StatusCode::NOT_FOUND,
Self::Conflict { .. } => StatusCode::CONFLICT,
Self::PayloadTooLarge { .. } => StatusCode::PAYLOAD_TOO_LARGE,
Self::PreconditionRequired { .. } => StatusCode::PRECONDITION_REQUIRED,
Self::RateLimited { .. } => StatusCode::TOO_MANY_REQUESTS,
Self::ServiceUnavailable { .. } => StatusCode::SERVICE_UNAVAILABLE,
Self::Internal { .. } => StatusCode::INTERNAL_SERVER_ERROR,
}
}
fn code(&self) -> &'static str {
pub(crate) fn code(&self) -> &str {
if let Some(code) = self
.context_ref()
.and_then(|context| context.get("error_code"))
.and_then(Value::as_str)
{
return code;
}
match self {
Self::Unauthorized { .. } => "unauthorized",
Self::Forbidden { .. } => "forbidden",
Self::Validation { .. } => "validation_error",
Self::Unprocessable { .. } => "unprocessable_entity",
Self::NotFound { .. } => "not_found",
Self::Conflict { .. } => "conflict",
Self::PayloadTooLarge { .. } => "payload_too_large",
Self::PreconditionRequired { .. } => "precondition_required",
Self::RateLimited { .. } => "rate_limited",
Self::ServiceUnavailable { .. } => "service_unavailable",
Self::Internal { .. } => "internal_error",
}
}
@@ -147,9 +318,13 @@ impl IntoResponse for ApiError {
Self::Unauthorized { .. }
| Self::Forbidden { .. }
| Self::Validation { .. }
| Self::Unprocessable { .. }
| Self::NotFound { .. }
| Self::Conflict { .. }
| Self::RateLimited { .. } => {
| Self::PayloadTooLarge { .. }
| Self::PreconditionRequired { .. }
| Self::RateLimited { .. }
| Self::ServiceUnavailable { .. } => {
warn!(
name: "admin.response.rejected",
error_code = self.code(),
@@ -165,6 +340,13 @@ impl IntoResponse for ApiError {
if let Some(context) = self.context() {
error["context"] = context;
}
let (request_id, trace_id) = crank_observability::current_request_correlation();
if let Some(request_id) = request_id {
error["request_id"] = Value::String(request_id);
}
if let Some(trace_id) = trace_id {
error["trace_id"] = Value::String(trace_id);
}
let body = Json(json!({
"error": error
@@ -175,19 +357,36 @@ impl IntoResponse for ApiError {
}
impl ApiError {
fn context(&self) -> Option<Value> {
fn context_ref(&self) -> Option<&Value> {
match self {
Self::Unauthorized { context, .. }
| Self::Forbidden { context, .. }
| Self::Validation { context, .. }
| Self::Unprocessable { context, .. }
| Self::NotFound { context, .. }
| Self::Conflict { context, .. }
| Self::PayloadTooLarge { context, .. }
| Self::PreconditionRequired { context, .. }
| Self::RateLimited { context, .. }
| Self::Internal { context, .. } => context.clone(),
| Self::ServiceUnavailable { context, .. }
| Self::Internal { context, .. } => context.as_ref(),
}
}
fn context(&self) -> Option<Value> {
self.context_ref().cloned()
}
}
/// Стабильные коды/поля Resource (D-14) живут в `error/resource.rs`: `error.rs`
/// держим в пределах лимита размера файла.
mod prompt;
mod resource;
pub(crate) use prompt::{
dependency_finding, representation_unsupported, template_finding, unavailable, unused,
};
impl From<RegistryError> for ApiError {
fn from(value: RegistryError) -> Self {
match value {
@@ -221,10 +420,41 @@ impl From<RegistryError> for ApiError {
format!("platform api key {key_id} was not found"),
json!({ "key_id": key_id }),
),
RegistryError::PlatformApiKeyInactive { key_id } => Self::conflict_with_context(
"platform api key is not active",
json!({
"key_id": key_id,
"error_code": "platform_api_key_not_active",
"recovery": "create_replacement_key"
}),
),
RegistryError::SecretNotFound { secret_id } => Self::not_found_with_context(
format!("secret {secret_id} was not found"),
json!({ "secret_id": secret_id }),
),
RegistryError::SecretInactive { secret_id } => Self::conflict_with_context(
"secret is not active",
json!({
"secret_id": secret_id,
"error_code": "secret_not_active",
"recovery": "rotate_or_replace_secret"
}),
),
RegistryError::SecretConcurrentUpdate { secret_id } => Self::conflict_with_context(
"secret was updated concurrently",
json!({
"secret_id": secret_id,
"error_code": "secret_concurrent_update",
"recovery": "reload"
}),
),
RegistryError::MasterKeyRotationInProgress => Self::conflict_with_context(
"master key rotation is in progress",
json!({
"error_code": "master_key_rotation_in_progress",
"recovery": "retry_after_rotation"
}),
),
RegistryError::InvocationLogNotFound { log_id } => Self::not_found_with_context(
format!("invocation log {log_id} was not found"),
json!({ "log_id": log_id }),
@@ -261,6 +491,72 @@ impl From<RegistryError> for ApiError {
json!({ "operation_id": operation_id }),
)
}
RegistryError::OperationArchived { operation_id } => Self::conflict_with_context(
format!("operation {operation_id} is archived"),
json!({ "operation_id": operation_id, "error_code": "operation_archived" }),
),
RegistryError::ResourceArchived { resource_id } => Self::conflict_with_context(
format!("resource {resource_id} is archived"),
json!({
"resource_id": resource_id,
"error_code": "resource_archived",
"recovery": "reload"
}),
),
RegistryError::ResourceRevoked { resource_id } => Self::conflict_with_context(
format!("resource {resource_id} is revoked"),
json!({
"resource_id": resource_id,
"error_code": "resource_revoked"
}),
),
RegistryError::OperationStaleVersion {
operation_id,
expected,
actual,
} => Self::conflict_with_context(
format!("operation {operation_id} has a stale base version"),
json!({
"operation_id": operation_id,
"current_version": expected,
"provided_version": actual,
"error_code": "operation_stale_version",
"recovery": "reload"
}),
),
RegistryError::InvalidOperationTransition {
operation_id,
from,
action,
} => Self::conflict_with_context(
format!("operation {operation_id} cannot perform {action} from {from}"),
json!({
"operation_id": operation_id,
"state": from,
"action": action,
"error_code": "operation_invalid_transition"
}),
),
RegistryError::OperationDeleteForbidden { operation_id } => {
Self::conflict_with_context(
format!(
"operation {operation_id} cannot be deleted because durable history exists"
),
json!({
"operation_id": operation_id,
"error_code": "operation_delete_forbidden"
}),
)
}
RegistryError::OperationAuthProfileUnavailable { operation_id } => {
Self::unprocessable_with_context(
"operation auth profile reference is unavailable",
json!({
"operation_id": operation_id,
"error_code": "operation_auth_profile_invalid"
}),
)
}
RegistryError::AuthProfileNotFound { auth_profile_id } => Self::not_found_with_context(
format!("auth profile {auth_profile_id} was not found"),
json!({ "auth_profile_id": auth_profile_id }),
@@ -269,6 +565,17 @@ impl From<RegistryError> for ApiError {
format!("operation {operation_id} already exists"),
json!({ "operation_id": operation_id }),
),
RegistryError::PlatformApiKeyNameAlreadyExists { workspace_id, name } => {
Self::conflict_with_context(
"platform api key name already exists",
json!({
"workspace_id": workspace_id,
"name": name,
"error_code": "platform_api_key_name_conflict",
"recovery": "choose_different_name"
}),
)
}
RegistryError::WorkspaceSlugAlreadyExists { slug } => Self::conflict_with_context(
format!("workspace with slug {slug} already exists"),
json!({ "slug": slug }),
@@ -279,6 +586,8 @@ impl From<RegistryError> for ApiError {
json!({
"workspace_id": workspace_id,
"name": name,
"error_code": "secret_name_conflict",
"recovery": "choose_different_name"
}),
)
}
@@ -290,12 +599,29 @@ impl From<RegistryError> for ApiError {
json!({
"secret_id": secret_id,
"auth_profile_id": auth_profile_id,
"error_code": "secret_referenced_by_auth_profile",
"recovery": "remove_or_update_auth_profile_reference"
}),
),
RegistryError::UserEmailAlreadyExists { email } => Self::conflict_with_context(
format!("user with email {email} already exists"),
json!({ "email": email }),
),
RegistryError::AdminBootstrapUnavailable | RegistryError::AdminBootstrapRejected => {
Self::unauthorized("bootstrap request is invalid or expired")
}
RegistryError::AdminRecoveryRejected => Self::unauthorized("recovery request rejected"),
RegistryError::AdminLoginRateLimited { retry_after_ms } => {
Self::rate_limited_with_context(
"login temporarily unavailable",
json!({
"retry_after_ms": retry_after_ms.clamp(1, 300_000),
"error_code": "login_throttled",
"recovery": "retry_after_delay"
}),
)
}
RegistryError::AdminCsrfRejected => Self::forbidden("csrf validation failed"),
RegistryError::InvalidInitialVersion {
operation_id,
version,
@@ -330,6 +656,39 @@ impl From<RegistryError> for ApiError {
"actual": actual,
}),
),
RegistryError::ImmutableAgentVersion { agent_id, version } => {
Self::conflict_with_context(
"Agent Version is immutable; reload before editing",
json!({
"agent_id": agent_id,
"version": version,
"error_code": "agent_stale_revision",
"recovery": "reload"
}),
)
}
RegistryError::AgentStaleRevision { agent_id } => Self::conflict_with_context(
"Agent state changed; reload before retrying",
json!({
"agent_id": agent_id,
"error_code": "agent_stale_revision",
"recovery": "reload"
}),
),
RegistryError::InvalidAgentTransition {
agent_id,
from,
action,
} => Self::conflict_with_context(
format!("agent {agent_id} cannot transition from {from} using {action}"),
json!({
"agent_id": agent_id,
"from": from,
"action": action,
"error_code": "agent_invalid_transition",
"recovery": "reload"
}),
),
RegistryError::ImmutableOperationFieldChanged {
operation_id,
field,
@@ -340,6 +699,37 @@ impl From<RegistryError> for ApiError {
"field": field,
}),
),
RegistryError::OnboardingStaleRevision => Self::conflict_with_context(
"onboarding state changed; reload before retrying",
json!({"error_code":"onboarding_stale_revision","recovery":"reload"}),
),
RegistryError::OnboardingIncomplete => Self::unprocessable_with_context(
"onboarding domain steps are not complete",
json!({"error_code":"onboarding_incomplete"}),
),
RegistryError::OnboardingImportDraftUnavailable => Self::unprocessable_with_context(
"the requested operation is not an actionable imported Draft",
json!({
"error_code":"onboarding_import_draft_unavailable",
"recovery":"reload_import_report"
}),
),
RegistryError::OnboardingImportApplicationUnavailable => {
Self::unprocessable_with_context(
"no committed import apply report matches the requested application",
json!({
"error_code":"onboarding_import_application_unavailable",
"recovery":"reload_import_report"
}),
)
}
RegistryError::OnboardingImportContextUnavailable => Self::unprocessable_with_context(
"the workspace has no durable import context for the requested application",
json!({
"error_code":"onboarding_import_context_unavailable",
"recovery":"register_import_context"
}),
),
RegistryError::InvalidEnumRepresentation { field } => Self::validation_with_context(
format!("unsupported enum representation for field {field}"),
json!({ "field": field }),
@@ -363,8 +753,188 @@ impl From<RegistryError> for ApiError {
format!("import job {job_id} was already applied with different parameters"),
json!({ "job_id": job_id }),
),
RegistryError::Storage(_) | RegistryError::Serialization(_) => {
Self::internal(value.to_string())
RegistryError::InvalidImportPlan { field } => Self::validation_with_context(
format!("invalid import plan input for field {field}"),
json!({
"field": field,
"error_code": "import_plan_invalid_input"
}),
),
RegistryError::ImportPlanConflict { plan_id } => Self::conflict_with_context(
format!("import plan {plan_id} already exists with different material"),
json!({
"plan_id": plan_id,
"error_code": "import_plan_conflict",
"recovery": "repreview_required"
}),
),
RegistryError::ImportPlanJobUnavailable { job_id } => Self::not_found_with_context(
format!("import job {job_id} is unavailable for planning"),
json!({ "job_id": job_id, "error_code": "import_plan_job_unavailable" }),
),
RegistryError::ImportPlanJobTerminal { job_id }
| RegistryError::ImportPlanJobExpired { job_id } => Self::conflict_with_context(
format!("import job {job_id} is not in planning state"),
json!({
"job_id": job_id,
"error_code": "import_plan_job_terminal",
"recovery": "repreview_required",
"repreview_required": true
}),
),
RegistryError::ImportPlanNotFound { .. } => Self::not_found_with_context(
"import plan was not found in this workspace",
json!({ "error_code": "import_plan_not_found" }),
),
RegistryError::ImportPlanExpired { .. } => Self::conflict_with_context(
"import plan has expired; create a fresh preview",
json!({
"error_code": "import_plan_expired",
"recovery": "repreview_required",
"repreview_required": true
}),
),
RegistryError::ImportPlanPayloadCorrupt { .. } => Self::conflict_with_context(
"import plan is invalid; create a fresh preview",
json!({
"error_code": "import_plan_payload_invalid",
"recovery": "repreview_required",
"repreview_required": true
}),
),
RegistryError::ImportPlanFingerprintStale { .. } => Self::conflict_with_context(
"import plan no longer matches the verified preview; create a fresh preview",
json!({
"error_code": "import_plan_fingerprint_stale",
"recovery": "repreview_required",
"repreview_required": true
}),
),
RegistryError::ImportPlanKeyDigestConflict { .. } => Self::conflict_with_context(
"idempotency key was already committed for different plan material",
json!({
"error_code": "import_plan_key_digest_conflict",
"recovery": "repreview_required",
"repreview_required": true
}),
),
RegistryError::ImportPlanNameConflict { .. } => Self::conflict_with_context(
"an operation name changed after the plan was created",
json!({
"error_code": "import_plan_name_conflict",
"recovery": "confirm_skip_rename_or_new_version"
}),
),
RegistryError::ImportPlanTargetStale { .. } => Self::conflict_with_context(
"an operation changed after the plan was created; create a fresh preview",
json!({
"error_code": "import_plan_target_stale",
"recovery": "repreview_required",
"repreview_required": true
}),
),
RegistryError::ImportPlanAuthUnavailable { .. } => Self::conflict_with_context(
"an authentication profile is unavailable; create a fresh preview",
json!({
"error_code": "import_plan_auth_unavailable",
"recovery": "repreview_required",
"repreview_required": true
}),
),
RegistryError::PromptNotFound { prompt_id } => Self::not_found_with_context(
format!("prompt {prompt_id} was not found"),
json!({ "prompt_id": prompt_id, "error_code": "prompt_not_found" }),
),
RegistryError::PromptNotDraft { prompt_id } => Self::conflict_with_context(
"only a Draft prompt can be edited",
json!({ "prompt_id": prompt_id, "error_code": "prompt_not_draft", "recovery": "reload" }),
),
RegistryError::PromptStaleRevision { prompt_id } => Self::conflict_with_context(
"prompt changed since it was loaded",
json!({ "prompt_id": prompt_id, "error_code": "prompt_stale_revision", "recovery": "reload" }),
),
RegistryError::PromptArchived { prompt_id } => Self::conflict_with_context(
"archived prompt cannot receive new Agent bindings",
json!({ "prompt_id": prompt_id, "error_code": "prompt_archived", "recovery": "reload" }),
),
RegistryError::PromptPublicationNotFound { prompt_id } => Self::not_found_with_context(
format!("published prompt version for {prompt_id} was not found"),
json!({ "prompt_id": prompt_id, "error_code": "prompt_publication_not_found" }),
),
RegistryError::PromptPublicationConflict { prompt_id } => Self::conflict_with_context(
"prompt version is already published",
json!({ "prompt_id": prompt_id, "error_code": "prompt_publication_conflict", "recovery": "reload" }),
),
RegistryError::PromptVersionOverflow { .. } | RegistryError::PromptCorrupt { .. } => {
Self::internal("registry operation failed")
}
RegistryError::PromptDependencyUnresolved {
position,
resource_id,
version,
..
} => prompt::unresolved(resource_id, version, position),
RegistryError::PromptDependencyRepresentationUnsupported {
position,
resource_id,
version,
..
} => prompt::representation_unsupported(resource_id, version, position),
RegistryError::PromptDependencyUnused {
position,
resource_id,
version,
..
} => prompt::unused(resource_id, version, position),
RegistryError::PromptDependencyUnavailable { position, .. } => {
prompt::unavailable(position)
}
RegistryError::SourceNotFound { .. } => Self::not_found_with_context(
"artifact source was not found",
json!({
"error_code": "artifact_source_not_found"
}),
),
RegistryError::SourceConflict { .. } => Self::conflict_with_context(
"artifact source metadata or lifecycle conflicts with the request",
json!({
"error_code": "artifact_source_conflict",
"recovery": "reload"
}),
),
RegistryError::SourceUnavailable => Self::unprocessable_with_context(
"artifact source is unavailable",
json!({ "error_code": "artifact_source_unavailable" }),
),
RegistryError::SourceIntegrity => Self::unprocessable_with_context(
"artifact source failed integrity verification",
json!({ "error_code": "artifact_source_integrity" }),
),
RegistryError::ArtifactClaimInProgress => Self::conflict_with_context(
"artifact reconciliation is in progress",
json!({
"error_code": "artifact_claim_in_progress",
"recovery": "retry"
}),
),
RegistryError::InvalidArtifactSource { field } => Self::validation_with_context(
"artifact source metadata is invalid",
json!({
"field": field,
"error_code": "artifact_source_invalid"
}),
),
RegistryError::Storage(_) => Self::internal("registry operation failed"),
RegistryError::Migration(_)
| RegistryError::Serialization(_)
| RegistryError::MasterKeyIdentityMismatch { .. }
| RegistryError::InvalidMasterKeyIdentity
| RegistryError::MasterKeyRotationNotFound { .. }
| RegistryError::MasterKeyRotationConflict
| RegistryError::MasterKeyRotationVerificationFailed
| RegistryError::InvalidCorrelationIdentity { .. }
| RegistryError::InvalidExecutionRecord { .. } => {
Self::internal("registry operation failed")
}
}
}
@@ -397,95 +967,34 @@ impl From<StorageError> for ApiError {
}
pub fn runtime_test_failure(error: &RuntimeError) -> Value {
let failure =
crank_runtime::normalize_runtime_error(error, &crank_core::CorrelationContext::generate());
execution_test_failure(&failure)
}
pub fn execution_test_failure(failure: &crank_core::ExecutionFailure) -> Value {
execution_test_failure_localized(failure, crank_core::ExecutionLocale::En)
}
pub fn execution_test_failure_localized(
failure: &crank_core::ExecutionFailure,
locale: crank_core::ExecutionLocale,
) -> Value {
let mut payload = json!({
"code": runtime_test_failure_code(error),
"message": error.to_string()
"code": failure.error_code().as_str(),
"message": failure.error_code().message(locale),
"stage": failure.stage().as_str(),
"retryability": failure.retryability().as_str(),
"outcome_certainty": failure.outcome_certainty().as_str(),
});
if let Some(context) = runtime_error_context(error) {
payload["context"] = context;
if let Some(status) = failure.upstream_status() {
payload["context"] = json!({ "upstream_status": status });
}
if let Some(challenge) = failure.confirmation() {
payload["context"] = json!({
"confirmation_token": challenge.token(),
"expires_in_ms": challenge.expires_in_ms(),
});
}
payload
}
fn runtime_test_failure_code(error: &RuntimeError) -> &'static str {
match error {
RuntimeError::Schema(_) => "runtime_schema_error",
RuntimeError::Mapping(_) => "runtime_mapping_error",
RuntimeError::RestAdapter(_) => "runtime_rest_error",
RuntimeError::ProtocolAdapter(_) => "runtime_adapter_error",
RuntimeError::UnsupportedProtocol { .. } => "runtime_protocol_error",
RuntimeError::ConcurrencyLimitExceeded { .. } => "runtime_overloaded",
RuntimeError::InvalidPreparedRequest { .. } => "runtime_request_error",
RuntimeError::ConfirmationRequired { .. } => "runtime_confirmation_required",
RuntimeError::InvalidConfirmationToken { .. } => "runtime_confirmation_error",
RuntimeError::ConfirmationStoreUnavailable { .. } => "runtime_confirmation_unavailable",
RuntimeError::IdempotencyStoreUnavailable { .. } => "runtime_idempotency_unavailable",
RuntimeError::IdempotencyInProgress { .. } => "runtime_idempotency_in_progress",
RuntimeError::IdempotencyConflict { .. } => "runtime_idempotency_conflict",
RuntimeError::IdempotencyOutcomeUnknown { .. } => "runtime_idempotency_outcome_unknown",
RuntimeError::UnsupportedExecutionMode { .. } => "runtime_streaming_mode_error",
RuntimeError::MissingAuthProfile { .. } => "runtime_auth_profile_error",
RuntimeError::MissingSecret { .. } | RuntimeError::MissingSecretVersion { .. } => {
"runtime_secret_error"
}
RuntimeError::InvalidAuthSecretValue { .. } => "runtime_secret_value_error",
RuntimeError::SecretCrypto { .. } => "runtime_secret_crypto_error",
}
}
pub fn runtime_error_context(error: &RuntimeError) -> Option<Value> {
match error {
RuntimeError::InvalidPreparedRequest { field, reason } => Some(json!({
"field": field,
"reason": reason,
})),
RuntimeError::ConfirmationRequired {
confirmation_token,
expires_in_ms,
safety_class,
..
} => Some(json!({
"confirmation_token": confirmation_token,
"expires_in_ms": expires_in_ms,
"safety_class": safety_class,
})),
RuntimeError::InvalidConfirmationToken { operation_id }
| RuntimeError::ConfirmationStoreUnavailable { operation_id }
| RuntimeError::IdempotencyStoreUnavailable { operation_id }
| RuntimeError::IdempotencyInProgress { operation_id }
| RuntimeError::IdempotencyConflict { operation_id }
| RuntimeError::IdempotencyOutcomeUnknown { operation_id } => Some(json!({
"operation_id": operation_id,
})),
RuntimeError::InvalidAuthSecretValue { secret_id, reason } => Some(json!({
"secret_id": secret_id,
"reason": reason,
})),
RuntimeError::SecretCrypto { operation, details } => Some(json!({
"operation": operation,
"details": details,
})),
RuntimeError::MissingAuthProfile { auth_profile_id } => Some(json!({
"auth_profile_id": auth_profile_id,
})),
RuntimeError::MissingSecret { secret_id } => Some(json!({
"secret_id": secret_id,
})),
RuntimeError::MissingSecretVersion { secret_id, version } => Some(json!({
"secret_id": secret_id,
"version": version,
})),
RuntimeError::UnsupportedExecutionMode { operation_id, mode } => Some(json!({
"operation_id": operation_id,
"mode": mode,
})),
RuntimeError::UnsupportedProtocol { protocol } => Some(json!({
"protocol": protocol,
})),
RuntimeError::ConcurrencyLimitExceeded { kind, limit } => Some(json!({
"kind": kind,
"limit": limit,
})),
_ => None,
}
}
+78
View File
@@ -0,0 +1,78 @@
//! Stable error surface of Story 3.16 (FR-18): pinned Prompt dependencies.
//!
//! Every refusal names only the code, the exact `source_path` of the definition array and the
//! pinned identity (`resource_id`, `version`) — never content, name or workspace.
use serde_json::json;
use super::ApiError;
const UNRESOLVED: &str = "prompt_dependency_unresolved";
const UNSUPPORTED: &str = "prompt_dependency_representation_unsupported";
const UNUSED: &str = "prompt_dependency_unused";
const UNAVAILABLE: &str = "prompt_dependency_unavailable";
pub(crate) fn unresolved(resource_id: String, version: u32, position: usize) -> ApiError {
ApiError::conflict_with_context(
"a pinned prompt dependency cannot be resolved",
json!({
"error_code": UNRESOLVED,
"source_path": source_path(position),
"resource_id": resource_id,
"version": version,
}),
)
}
pub(crate) fn representation_unsupported(
resource_id: String,
version: u32,
position: usize,
) -> ApiError {
ApiError::validation_with_context(
"a pinned prompt dependency has no message representation",
json!({
"error_code": UNSUPPORTED,
"source_path": source_path(position),
"resource_id": resource_id,
"version": version,
}),
)
}
pub(crate) fn unused(resource_id: String, version: u32, position: usize) -> ApiError {
ApiError::validation_with_context(
"a pinned prompt dependency is not referenced by any message",
json!({
"error_code": UNUSED,
"source_path": source_path(position),
"resource_id": resource_id,
"version": version,
}),
)
}
pub(crate) fn unavailable(position: usize) -> ApiError {
ApiError::service_unavailable_with_context(
"a pinned prompt dependency is unavailable",
json!({ "error_code": UNAVAILABLE, "source_path": source_path(position) }),
)
}
/// Exact definition path of one pinned dependency (`dependencies[i]`).
pub(crate) fn source_path(position: usize) -> String {
format!("dependencies[{position}]")
}
/// Template-level finding of the bounded scan (contract §5): the matched text is never carried.
pub(crate) fn template_finding(code: &'static str, source_path: String) -> ApiError {
ApiError::validation_with_context(
"the prompt template text is refused by the bounded scan",
json!({ "error_code": code, "source_path": source_path }),
)
}
/// Read-time finding inside dynamic dependency content (contract §5).
pub(crate) fn dependency_finding(code: &'static str, position: usize) -> ApiError {
template_finding(code, source_path(position))
}
+165
View File
@@ -0,0 +1,165 @@
//! Отображение ошибок Resource в HTTP-ответ admin-api (D-14).
//!
//! Домен отдаёт только стабильные `code`/`field`; HTTP-статус и конверт
//! собираются здесь, поэтому `error.rs` не разрастается и остаётся в пределах
//! лимита размера файла.
use super::ApiError;
use crank_core::resource::ResourceError;
use serde_json::{Value, json};
/// Стабильные коды/поля Resource (D-14): `context.error_code` наблюдаем как
/// `error.code`, `context.field` — как `error.context.field`. Сообщения
/// фиксированные и никогда не содержат bytes содержимого или текст хранилища.
impl From<ResourceError> for ApiError {
fn from(value: ResourceError) -> Self {
let code = value.code();
let mut context = json!({ "error_code": code });
if let Some(field) = value.field() {
context["field"] = Value::String(field.to_owned());
}
if value == ResourceError::Stale {
context["recovery"] = Value::String("reload".to_owned());
}
// Story 3.10: блокирующие findings уходят в существующее поле конверта
// `error.context.findings[]` (`code` + `field`), без текста определения.
if let ResourceError::SourceBlocked { findings } = &value {
context["findings"] = json!(findings);
}
let message = resource_error_message(&value);
match value {
ResourceError::ContentEmpty
| ResourceError::ContentInvalidUtf8
| ResourceError::ContentInvalidJson
| ResourceError::ArtifactIntegrity
| ResourceError::SecretFindings => Self::Unprocessable {
message,
context: Some(context),
},
ResourceError::SourceNotPublished | ResourceError::SourceBlocked { .. } => {
Self::Unprocessable {
message,
context: Some(context),
}
}
ResourceError::ContentTooLarge => Self::PayloadTooLarge {
message,
context: Some(context),
},
ResourceError::UriDuplicate | ResourceError::NameDuplicate => Self::Conflict {
message,
context: Some(context),
},
// Story 3.10: definition-only Resource не переносим (export/content).
ResourceError::SourceNotPortable => Self::Conflict {
message,
context: Some(context),
},
ResourceError::SourceNotFound => Self::NotFound {
message,
context: Some(context),
},
ResourceError::Stale => Self::PreconditionRequired {
message,
context: Some(context),
},
ResourceError::NotFound => Self::NotFound {
message,
context: Some(context),
},
// Story 3.8: отозванный Resource — обычный конфликт с устойчивым кодом;
// сообщение и контекст не раскрывают content/URI/digest.
ResourceError::Revoked => Self::Conflict {
message,
context: Some(context),
},
ResourceError::StorageUnavailable | ResourceError::InvalidLimits => Self::Internal {
message: "internal server error".to_owned(),
context: Some(context),
},
// Lifecycle-отказ — обычный клиентский конфликт, а не внутренняя ошибка:
// код остаётся существующим (`resource_lifecycle_invalid`), сообщение
// фиксированное и не раскрывает from/to.
ResourceError::InvalidLifecycleTransition { .. } => Self::Conflict {
message,
context: Some(context),
},
ResourceError::MimeSyntaxInvalid
| ResourceError::MimeNotAllowed
| ResourceError::KindMismatch
| ResourceError::UriInvalid
| ResourceError::NameInvalid
| ResourceError::DescriptionInvalid
| ResourceError::PortabilityEnvelopeInvalid
| ResourceError::SourceInvalid { .. }
| ResourceError::FieldImmutable { .. } => Self::Validation {
message,
context: Some(context),
},
// D-20 B/E: значение поля вне набора — форма запроса. Отказ выводится как
// generic `validation_error` без Resource-специфичного кода и без поля.
ResourceError::UnknownMetadataValue { .. } => Self::Validation {
message,
context: None,
},
}
}
}
fn resource_error_message(error: &ResourceError) -> String {
match error {
ResourceError::ContentEmpty => "resource content must not be empty".to_owned(),
ResourceError::PortabilityEnvelopeInvalid => {
"resource portability envelope is invalid".to_owned()
}
ResourceError::ContentTooLarge => {
"resource content exceeds the configured limit".to_owned()
}
ResourceError::ContentInvalidUtf8 => "resource content is not valid UTF-8".to_owned(),
ResourceError::ContentInvalidJson => "resource content is not valid JSON".to_owned(),
ResourceError::MimeSyntaxInvalid => "resource mime type syntax is invalid".to_owned(),
ResourceError::MimeNotAllowed => {
"resource mime type is not allowed for this kind".to_owned()
}
ResourceError::KindMismatch => {
"resource mime type is incompatible with the declared kind".to_owned()
}
ResourceError::UriInvalid => "resource uri is invalid".to_owned(),
ResourceError::NameInvalid => "resource name is invalid".to_owned(),
ResourceError::DescriptionInvalid => "resource description is invalid".to_owned(),
ResourceError::UnknownMetadataValue { field } => {
format!("resource metadata field `{field}` has an unknown value")
}
ResourceError::UriDuplicate => "resource uri already exists in this workspace".to_owned(),
ResourceError::NameDuplicate => "resource name already exists in this workspace".to_owned(),
ResourceError::FieldImmutable { field } => {
format!("resource field {field} is immutable")
}
ResourceError::Stale => "resource changed since it was read".to_owned(),
ResourceError::NotFound => "resource was not found in this workspace".to_owned(),
ResourceError::Revoked => "resource has been revoked".to_owned(),
ResourceError::ArtifactIntegrity => {
"resource artifact integrity verification failed".to_owned()
}
ResourceError::SecretFindings => {
"resource content contains secret material or an access-bypassing reference".to_owned()
}
ResourceError::StorageUnavailable | ResourceError::InvalidLimits => {
"internal server error".to_owned()
}
ResourceError::InvalidLifecycleTransition { .. } => {
"resource lifecycle transition is not allowed".to_owned()
}
ResourceError::SourceInvalid { .. } => "resource source definition is invalid".to_owned(),
ResourceError::SourceNotFound => {
"resource source operation was not found in this workspace".to_owned()
}
ResourceError::SourceNotPublished => {
"resource source operation version is not published".to_owned()
}
ResourceError::SourceBlocked { .. } => {
"resource source definition has blocking findings".to_owned()
}
ResourceError::SourceNotPortable => "resource source definition is not portable".to_owned(),
}
}
+6
View File
@@ -134,9 +134,13 @@ mod tests {
method: HttpMethod::Delete,
path_template: "/items/{id}".to_owned(),
static_headers: BTreeMap::new(),
request_contract: None,
response_contract: Vec::new(),
multipart_field_mime_types: BTreeMap::new(),
}),
input_schema: Schema {
kind: SchemaKind::Object,
format: None,
description: None,
required: true,
nullable: false,
@@ -148,6 +152,7 @@ mod tests {
},
output_schema: Schema {
kind: SchemaKind::Object,
format: None,
description: None,
required: false,
nullable: false,
@@ -198,6 +203,7 @@ mod tests {
fn string_schema() -> Schema {
Schema {
kind: SchemaKind::String,
format: None,
description: None,
required: true,
nullable: false,
+1
View File
@@ -5,6 +5,7 @@ pub mod error;
pub mod import_guidance;
pub mod pool_metrics;
pub mod rate_limit;
pub mod reconciliation;
pub mod request_context;
pub mod routes;
pub mod service;
+388 -119
View File
@@ -1,18 +1,26 @@
use std::{env, net::SocketAddr, path::PathBuf, time::Duration};
use std::{io, net::SocketAddr, process::ExitCode, time::Duration};
use admin_api::{
app::build_app,
auth::{AuthSettings, BootstrapAdminConfig},
pool_metrics::spawn_postgres_pool_metrics,
reconciliation::{open_reconciliation_store, spawn_artifact_reconciliation},
service::AdminServiceBuilder,
state::AppState,
};
use crank_community_auth::PasswordIdentityProvider;
use crank_config::{
AdminProcessConfig, CacheBackend as ConfigCacheBackend, ConfigSource, DatabaseSettings,
DiagnosticCode, ObservabilitySettings, ProcessKind, parse_process,
};
use crank_core::CacheBackend;
use crank_observability::{
CriticalErrorCategory, MetricsConfig, ObservabilityConfig, ObservabilityLifecycle,
capture_critical_error,
OtlpTraceConfig, RedactionLimits, SentryConfig, ServiceIdentity, capture_critical_error,
};
use crank_registry::{
MASTER_KEY_CIPHER_CONTRACT, MasterKeyIdentityCandidate, PostgresPoolConfig, PostgresRegistry,
};
use crank_registry::{PostgresPoolConfig, PostgresRegistry};
use crank_runtime::{
RequestRateLimitConfig, RequestRateLimiter, RuntimeCacheConfig, RuntimeCacheStores,
RuntimeLimits, SecretCrypto,
@@ -21,17 +29,107 @@ use sqlx::postgres::PgConnectOptions;
use tokio::net::TcpListener;
use tracing::{info, warn};
const MAX_INVOCATION_LOG_RETENTION_DAYS: i64 = 36_500;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let observability = crank_observability::init(ObservabilityConfig::from_env(
"admin-api",
env!("CARGO_PKG_VERSION"),
"admin_api=info,tower_http=info",
)?)?;
async fn main() -> ExitCode {
match main_result().await {
Ok(()) => ExitCode::SUCCESS,
Err(error) => {
eprintln!("{}", safe_startup_diagnostic(error.as_ref()));
ExitCode::FAILURE
}
}
}
fn safe_startup_diagnostic(error: &(dyn std::error::Error + 'static)) -> String {
let mut current = Some(error);
while let Some(cause) = current {
if let Some(config) = cause.downcast_ref::<crank_config::ConfigError>() {
return config.to_json();
}
if let Some(migration) = cause.downcast_ref::<crank_registry::MigrationError>() {
return serde_json::json!({
"status": "error",
"code": migration.code(),
"stage": migration.stage(),
"version": migration.version(),
"recovery": migration.recovery(),
})
.to_string();
}
if let Some(crank_registry::RegistryError::Migration(migration)) =
cause.downcast_ref::<crank_registry::RegistryError>()
{
return serde_json::json!({
"status": "error",
"code": migration.code(),
"stage": migration.stage(),
"version": migration.version(),
"recovery": migration.recovery(),
})
.to_string();
}
if let Some(crank_registry::RegistryError::MasterKeyIdentityMismatch { epoch }) =
cause.downcast_ref::<crank_registry::RegistryError>()
{
return serde_json::json!({
"status": "error",
"code": "master_key_identity_mismatch",
"stage": "startup.master_key_identity",
"version": epoch,
"recovery": "configure_same_master_key",
})
.to_string();
}
if cause
.downcast_ref::<crank_registry::RegistryError>()
.is_some_and(|error| {
matches!(
error,
crank_registry::RegistryError::InvalidMasterKeyIdentity
)
})
{
return serde_json::json!({
"status": "error",
"code": "master_key_identity_invalid",
"stage": "startup.master_key_identity",
"version": null,
"recovery": "contact_operator",
})
.to_string();
}
current = cause.source();
}
serde_json::json!({
"status": "error",
"code": "startup_failed",
"stage": "startup",
"version": null,
"recovery": "contact_operator",
})
.to_string()
}
async fn main_result() -> Result<(), Box<dyn std::error::Error>> {
let effective = parse_process(ProcessKind::AdminApi, ConfigSource::from_os()?)?;
let config = effective
.admin()
.cloned()
.ok_or_else(|| io::Error::other("admin configuration projection is unavailable"))?;
preflight_config(&config)?;
let observability = init_observability(&config.observability)?;
for deprecation in effective.deprecations() {
warn!(
name: "config.deprecated",
field = deprecation.field,
source_class = deprecation.source_class,
replacement = deprecation.replacement,
removal_window = deprecation.removal_window,
"deprecated configuration accepted"
);
}
let mut startup_completed = false;
let result = run(&observability, &mut startup_completed).await;
let result = run(config, &observability, &mut startup_completed).await;
if result.is_err() {
capture_critical_error(if startup_completed {
CriticalErrorCategory::Internal
@@ -43,54 +141,75 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
}
async fn run(
config: AdminProcessConfig,
observability: &ObservabilityLifecycle,
startup_completed: &mut bool,
) -> Result<(), Box<dyn std::error::Error>> {
let metrics_config =
MetricsConfig::from_env("CRANK_ADMIN_METRICS_BIND", "127.0.0.1:9464".parse()?)?;
let metrics_config = MetricsConfig::new(
config.observability.metrics.enabled,
config.observability.metrics.bind_addr,
config
.observability
.metrics
.bearer_token
.as_ref()
.map(|token| token.expose_secret().to_owned()),
)?;
let metrics_enabled = metrics_config.enabled();
let metrics_server = if metrics_config.enabled() {
let pool_config = postgres_pool_config(&config.database)?;
let registry = PostgresRegistry::connect_with_options_and_pool_config(
database_options(&config.database)?,
pool_config,
)
.await?;
let metrics_server = if metrics_enabled {
Some(observability.metrics_surface(metrics_config).bind().await?)
} else {
None
};
let storage_root = PathBuf::from(
env::var("CRANK_STORAGE_ROOT").unwrap_or_else(|_| "/var/lib/crank/storage".into()),
);
let bind_addr = env::var("CRANK_ADMIN_BIND").unwrap_or_else(|_| "0.0.0.0:3001".into());
let base_url = env::var("CRANK_BASE_URL").unwrap_or_else(|_| "http://localhost:3000".into());
let socket_addr: SocketAddr = bind_addr.parse()?;
let pool_config = PostgresPoolConfig::from_env()?;
let registry = PostgresRegistry::connect_with_options_and_pool_config(
database_options_from_env()?,
pool_config,
)
.await?;
if metrics_enabled {
spawn_postgres_pool_metrics(registry.pool().clone());
}
let base_url = config
.runtime
.base_url
.clone()
.unwrap_or_else(|| "http://localhost:3000".to_owned());
let auth_settings = AuthSettings {
session_secret: env::var("CRANK_SESSION_SECRET")?,
password_pepper: env::var("CRANK_PASSWORD_PEPPER")?,
session_ttl_hours: env::var("CRANK_SESSION_TTL_HOURS")
.ok()
.and_then(|value| value.parse::<i64>().ok())
.unwrap_or(24),
session_secret: config.session_secret.expose_secret().to_owned(),
password_pepper: config.password_pepper.expose_secret().to_owned(),
session_ttl_hours: config.session_ttl_hours,
cookie_secure: base_url.starts_with("https://"),
bootstrap_admin: BootstrapAdminConfig {
email: env::var("CRANK_BOOTSTRAP_ADMIN_EMAIL")?,
password: env::var("CRANK_BOOTSTRAP_ADMIN_PASSWORD")?,
display_name: env::var("CRANK_BOOTSTRAP_ADMIN_DISPLAY_NAME")
.unwrap_or_else(|_| "Crank Owner".into()),
email: config.bootstrap_email.clone(),
password: config
.bootstrap_password
.as_ref()
.map(|password| password.expose_secret().to_owned())
.unwrap_or_default(),
display_name: config.bootstrap_display_name.clone(),
},
};
let runtime_limits = RuntimeLimits::from_env()?;
let cache_config = RuntimeCacheConfig::from_env()?;
let runtime_limits = RuntimeLimits::try_new(
config.runtime.max_concurrent_unary,
config.runtime.max_concurrent_sessions,
)?;
let cache_config = runtime_cache_config(&config)?;
let cache_stores = RuntimeCacheStores::from_config(&cache_config).await?;
let api_rate_limit = admin_api_rate_limit_config_from_env()?;
let secret_crypto = SecretCrypto::new(&env::var("CRANK_MASTER_KEY")?)?;
let outbound_http_policy = crank_runtime::OutboundHttpPolicy::from_env()?;
let api_rate_limit = RequestRateLimitConfig::new(
config.rate_limit.requests_per_second,
config.rate_limit.burst,
)?;
let secret_crypto =
verified_startup_secret_crypto(&registry, config.runtime.master_key.expose_secret())
.await?;
let artifact_store = open_reconciliation_store(config.storage_root.clone()).await?;
let outbound_http_policy = crank_runtime::OutboundHttpPolicy::try_new_with_limits(
config.runtime.outbound.allowed_hosts.clone(),
config.runtime.outbound.denied_hosts.clone(),
config.runtime.outbound.max_request_bytes,
config.runtime.outbound.max_response_bytes,
)?;
let runtime = crank_runtime::community_with_outbound_policy(outbound_http_policy.clone())
.with_limits(runtime_limits)
.with_response_cache(cache_stores.response.clone())
@@ -99,21 +218,23 @@ async fn run(
let identity_provider =
PasswordIdentityProvider::new(registry.clone(), auth_settings.password_pepper.clone());
let service = AdminServiceBuilder::new(
registry,
storage_root,
registry.clone(),
config.storage_root.clone(),
auth_settings,
secret_crypto,
runtime,
)
.with_artifact_store(artifact_store.clone())
.with_public_base_url(base_url)
.with_outbound_http_policy(outbound_http_policy)
.with_external_reference_import(&config.external_references)?
.with_identity_provider(std::sync::Arc::new(identity_provider))
.build();
let invocation_log_retention_days = invocation_log_retention_days_from_env()?;
service.bootstrap_admin_user().await?;
if env_flag("CRANK_DEMO_SEED") {
if config.demo_seed {
service.seed_demo_assets().await?;
}
spawn_invocation_log_cleanup(service.clone(), invocation_log_retention_days);
spawn_artifact_reconciliation(registry, artifact_store).await;
spawn_invocation_log_cleanup(service.clone(), config.invocation_log_retention_days);
let state = AppState {
service,
api_rate_limiter: if cache_config.backend.is_external() {
@@ -121,10 +242,10 @@ async fn run(
} else {
RequestRateLimiter::new(api_rate_limit)
},
trust_forwarded_headers: env_flag("CRANK_TRUST_FORWARDED_HEADERS"),
trusted_proxy_ips: config.trusted_proxy_ips.clone(),
};
let app = build_app(state);
let listener = TcpListener::bind(socket_addr).await?;
let listener = TcpListener::bind(config.bind_addr).await?;
let make_service = app.into_make_service_with_connect_info::<SocketAddr>();
info!(
@@ -138,14 +259,10 @@ async fn run(
acquire_timeout_ms = pool_config.acquire_timeout_ms,
idle_timeout_ms = pool_config.idle_timeout_ms,
max_lifetime_ms = pool_config.max_lifetime_ms,
invocation_log_retention_days,
invocation_log_retention_days = config.invocation_log_retention_days,
"postgres pool configured"
);
info!(
name: "admin.server.listening",
bind_address = %socket_addr,
"admin-api listening"
);
info!(name: "admin.server.listening", bind_address = %config.bind_addr, "admin-api listening");
*startup_completed = true;
if let Some(metrics_server) = metrics_server {
@@ -156,23 +273,211 @@ async fn run(
} else {
axum::serve(listener, make_service).await?;
}
Ok(())
}
fn invocation_log_retention_days_from_env() -> Result<i64, Box<dyn std::error::Error>> {
const NAME: &str = "CRANK_INVOCATION_LOG_RETENTION_DAYS";
let value = match env::var(NAME) {
Ok(raw) => raw.parse::<i64>()?,
Err(env::VarError::NotPresent) => 30,
Err(error) => return Err(error.into()),
fn init_observability(
config: &ObservabilitySettings,
) -> Result<ObservabilityLifecycle, Box<dyn std::error::Error>> {
let identity = ServiceIdentity::try_new(
"admin-api",
env!("CARGO_PKG_VERSION"),
config.environment.clone(),
)?;
let base = ObservabilityConfig::try_new(
identity,
config.log_filter.clone(),
RedactionLimits::default(),
)?;
let sentry = SentryConfig::parse(
config
.sentry_dsn
.as_ref()
.map(|value| value.expose_secret()),
)?;
let otlp = otlp_config(config)?;
Ok(ObservabilityLifecycle::init_with_exporters(
base, sentry, otlp,
)?)
}
fn preflight_config(config: &AdminProcessConfig) -> Result<(), crank_config::ConfigError> {
let invalid = |field| crank_config::ConfigError::single(DiagnosticCode::InvalidType, field);
database_options(&config.database).map_err(|_| invalid("database.source"))?;
postgres_pool_config(&config.database).map_err(|_| invalid("database.pool"))?;
MetricsConfig::new(
config.observability.metrics.enabled,
config.observability.metrics.bind_addr,
config
.observability
.metrics
.bearer_token
.as_ref()
.map(|v| v.expose_secret().to_owned()),
)
.map_err(|_| invalid("observability.metrics"))?;
RuntimeLimits::try_new(
config.runtime.max_concurrent_unary,
config.runtime.max_concurrent_sessions,
)
.map_err(|_| invalid("runtime.limits"))?;
runtime_cache_config(config).map_err(|_| invalid("cache"))?;
RequestRateLimitConfig::new(
config.rate_limit.requests_per_second,
config.rate_limit.burst,
)
.map_err(|_| invalid("admin.rate_limit"))?;
SecretCrypto::new(config.runtime.master_key.expose_secret())
.map_err(|_| invalid("runtime.master_key"))?;
crank_runtime::OutboundHttpPolicy::try_new_with_limits(
config.runtime.outbound.allowed_hosts.clone(),
config.runtime.outbound.denied_hosts.clone(),
config.runtime.outbound.max_request_bytes,
config.runtime.outbound.max_response_bytes,
)
.map_err(|_| invalid("runtime.outbound"))?;
let identity = ServiceIdentity::try_new(
"admin-api",
env!("CARGO_PKG_VERSION"),
config.observability.environment.clone(),
)
.map_err(|_| invalid("observability.environment"))?;
ObservabilityConfig::try_new(
identity,
config.observability.log_filter.clone(),
RedactionLimits::default(),
)
.map_err(|_| invalid("observability.log_filter"))?;
SentryConfig::parse(
config
.observability
.sentry_dsn
.as_ref()
.map(|v| v.expose_secret()),
)
.map_err(|_| invalid("observability.sentry_dsn"))?;
otlp_config(&config.observability).map_err(|_| invalid("observability.otlp"))?;
Ok(())
}
async fn verified_startup_secret_crypto(
registry: &PostgresRegistry,
master_key: &str,
) -> Result<SecretCrypto, Box<dyn std::error::Error>> {
let active = registry.active_master_key_identity().await?;
let secret_crypto = if let Some(identity) = active {
SecretCrypto::with_epoch(master_key, identity.epoch)?
} else {
let crypto = SecretCrypto::new(master_key)?;
let mut after_secret_id: Option<String> = None;
let mut after_version: Option<u32> = None;
loop {
let versions = registry
.list_secret_versions_for_master_key_epoch_page(
1,
after_secret_id.as_deref(),
after_version,
1_000,
)
.await?;
if versions.is_empty() {
break;
}
for version in versions {
after_secret_id = Some(version.secret_version.secret_id.as_str().to_owned());
after_version = Some(version.secret_version.version);
crypto.decrypt_for_epoch(
&version.secret_version.key_version,
version.master_key_epoch,
&version.secret_version.ciphertext,
)?;
}
}
crypto
};
if !(1..=MAX_INVOCATION_LOG_RETENTION_DAYS).contains(&value) {
return Err(
format!("{NAME} must be between 1 and {MAX_INVOCATION_LOG_RETENTION_DAYS}").into(),
);
let master_key_observed_at = time::OffsetDateTime::now_utc();
registry
.verify_or_register_master_key_identity(MasterKeyIdentityCandidate {
epoch: secret_crypto.master_key_epoch(),
fingerprint: secret_crypto.master_key_fingerprint(),
cipher_contract: MASTER_KEY_CIPHER_CONTRACT,
observed_at: &master_key_observed_at,
})
.await?;
Ok(secret_crypto)
}
fn otlp_config(
config: &ObservabilitySettings,
) -> Result<OtlpTraceConfig, crank_observability::OtlpTraceConfigError> {
let values = &config.otlp;
OtlpTraceConfig::from_values(
values.endpoint.clone(),
values.traces_endpoint.clone(),
values.protocol.clone(),
values.traces_protocol.clone(),
values.timeout.clone(),
values.traces_timeout.clone(),
values
.headers
.as_ref()
.map(|value| value.expose_secret().to_owned()),
values
.traces_headers
.as_ref()
.map(|value| value.expose_secret().to_owned()),
values.max_queue_size,
values.max_export_batch_size,
values.schedule_delay.clone(),
values.export_timeout.clone(),
)
}
fn postgres_pool_config(
config: &DatabaseSettings,
) -> Result<PostgresPoolConfig, crank_registry::PostgresPoolConfigError> {
PostgresPoolConfig::try_new(
config.pool.max_connections,
config.pool.min_connections,
config.pool.acquire_timeout_ms,
config.pool.idle_timeout_ms,
config.pool.max_lifetime_ms,
)
}
fn database_options(
config: &DatabaseSettings,
) -> Result<PgConnectOptions, Box<dyn std::error::Error>> {
if let Some(url) = &config.url {
return url
.expose_secret()
.parse::<PgConnectOptions>()
.map_err(|_| io::Error::other("database URL is invalid").into());
}
Ok(value)
Ok(PgConnectOptions::new()
.host(&config.host)
.port(config.port)
.database(&config.database)
.username(&config.username)
.password(config.password.expose_secret()))
}
fn runtime_cache_config(
config: &AdminProcessConfig,
) -> Result<RuntimeCacheConfig, crank_runtime::RuntimeCacheConfigError> {
RuntimeCacheConfig::try_new(
match config.runtime.cache.backend {
ConfigCacheBackend::Memory => CacheBackend::Memory,
ConfigCacheBackend::Valkey => CacheBackend::Valkey,
ConfigCacheBackend::Redis => CacheBackend::Redis,
},
config
.runtime
.cache
.url
.as_ref()
.map(|value| value.expose_secret().to_owned()),
)
}
fn spawn_invocation_log_cleanup(service: admin_api::service::AdminService, retention_days: i64) {
@@ -182,12 +487,23 @@ fn spawn_invocation_log_cleanup(service: admin_api::service::AdminService, reten
interval.tick().await;
let cutoff = time::OffsetDateTime::now_utc() - time::Duration::days(retention_days);
match service.cleanup_invocation_logs_before(cutoff).await {
Ok(removed) if removed > 0 => info!(
Ok(outcome) if outcome.deleted_records > 0 => info!(
name: "admin.invocation_log_cleanup.completed",
removed,
status = ?outcome.status,
removed = outcome.deleted_records,
requested_cutoff = %outcome.policy.requested_cutoff,
effective_cutoff = %outcome.policy.effective_cutoff,
preserved_usage_window_days = outcome.policy.preserved_usage_window_days,
"expired invocation logs removed"
),
Ok(_) => {}
Ok(outcome) => info!(
name: "admin.invocation_log_cleanup.noop",
status = ?outcome.status,
requested_cutoff = %outcome.policy.requested_cutoff,
effective_cutoff = %outcome.policy.effective_cutoff,
preserved_usage_window_days = outcome.policy.preserved_usage_window_days,
"no expired invocation logs removed"
),
Err(_) => warn!(
name: "admin.invocation_log_cleanup.failed",
error_category = "registry_cleanup",
@@ -197,50 +513,3 @@ fn spawn_invocation_log_cleanup(service: admin_api::service::AdminService, reten
}
});
}
fn env_flag(name: &str) -> bool {
matches!(
env::var(name)
.ok()
.as_deref()
.map(str::to_ascii_lowercase)
.as_deref(),
Some("1" | "true" | "yes" | "on")
)
}
fn admin_api_rate_limit_config_from_env()
-> Result<RequestRateLimitConfig, Box<dyn std::error::Error>> {
let requests_per_second = env::var("CRANK_ADMIN_RATE_LIMIT_RPS")
.ok()
.and_then(|value| value.parse::<u32>().ok())
.unwrap_or(30);
let burst = env::var("CRANK_ADMIN_RATE_LIMIT_BURST")
.ok()
.and_then(|value| value.parse::<u32>().ok())
.unwrap_or(60);
Ok(RequestRateLimitConfig::new(requests_per_second, burst)?)
}
fn database_options_from_env() -> Result<PgConnectOptions, Box<dyn std::error::Error>> {
if let Ok(database_url) = env::var("CRANK_DATABASE_URL") {
return Ok(database_url.parse::<PgConnectOptions>()?);
}
let host = env::var("POSTGRES_HOST").unwrap_or_else(|_| "postgres".into());
let port = env::var("POSTGRES_PORT")
.ok()
.and_then(|value| value.parse::<u16>().ok())
.unwrap_or(5432);
let database = env::var("POSTGRES_DB").unwrap_or_else(|_| "crank".into());
let username = env::var("POSTGRES_USER").unwrap_or_else(|_| "crank".into());
let password = env::var("POSTGRES_PASSWORD").unwrap_or_else(|_| "crank".into());
Ok(PgConnectOptions::new()
.host(&host)
.port(port)
.database(&database)
.username(&username)
.password(&password))
}
+39 -15
View File
@@ -10,20 +10,23 @@ use crank_runtime::{RateLimitCheckError, RateLimitRejection};
use crate::{error::ApiError, state::AppState};
#[derive(Clone, Debug)]
pub struct ClientIdentityBucket(pub String);
pub async fn apply_api_rate_limit(
State(state): State<AppState>,
request: Request,
mut request: Request,
next: Next,
) -> Result<Response, ApiError> {
let peer_ip = request
.extensions()
.get::<ConnectInfo<SocketAddr>>()
.map(|ConnectInfo(address)| address.ip());
let key = rate_limit_key(
let key = client_rate_limit_key(
request.headers(),
request.uri().path(),
peer_ip,
state.trust_forwarded_headers,
&state.trusted_proxy_ips,
);
if let Err(error) = state.api_rate_limiter.check(&key).await {
return match error {
@@ -37,6 +40,8 @@ pub async fn apply_api_rate_limit(
};
}
request.extensions_mut().insert(ClientIdentityBucket(key));
Ok(next.run(request).await)
}
@@ -46,13 +51,15 @@ fn rejection_context(rejection: RateLimitRejection) -> serde_json::Value {
})
}
fn rate_limit_key(
pub fn client_rate_limit_key(
headers: &HeaderMap,
path: &str,
peer_ip: Option<IpAddr>,
trust_forwarded_headers: bool,
trusted_proxy_ips: &[IpAddr],
) -> String {
if trust_forwarded_headers && let Some(client_ip) = forwarded_client_ip(headers) {
if peer_ip.is_some_and(|peer_ip| trusted_proxy_ips.contains(&peer_ip))
&& let Some(client_ip) = forwarded_client_ip(headers)
{
return format!("ip:{client_ip}");
}
@@ -63,7 +70,8 @@ fn rate_limit_key(
format!("anonymous:{path}")
}
/// Resolves the client IP from proxy headers, assuming a single trusted proxy.
/// Resolves the client IP from proxy headers after the immediate peer was
/// matched against the trusted-proxy allowlist.
///
/// `X-Real-IP` is preferred because a trusted proxy (e.g. nginx) sets it to the
/// real peer address. For `X-Forwarded-For` the proxy *appends* the observed
@@ -96,7 +104,7 @@ mod tests {
use axum::http::{HeaderMap, HeaderValue, header::COOKIE};
use super::rate_limit_key;
use super::client_rate_limit_key;
fn peer() -> Option<IpAddr> {
Some(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 7)))
@@ -112,7 +120,7 @@ mod tests {
headers.insert("x-forwarded-for", HeaderValue::from_static("10.0.0.5"));
assert_eq!(
rate_limit_key(&headers, "/api/auth/login", peer(), true),
client_rate_limit_key(&headers, "/api/auth/login", peer(), &[peer().unwrap()]),
"ip:10.0.0.5"
);
}
@@ -124,7 +132,23 @@ mod tests {
headers.insert("x-real-ip", HeaderValue::from_static("10.0.0.9"));
assert_eq!(
rate_limit_key(&headers, "/api/auth/login", peer(), false),
client_rate_limit_key(&headers, "/api/auth/login", peer(), &[]),
"ip:203.0.113.7"
);
}
#[test]
fn ignores_forwarded_headers_from_unlisted_peer() {
let mut headers = HeaderMap::new();
headers.insert("x-real-ip", HeaderValue::from_static("10.0.0.9"));
assert_eq!(
client_rate_limit_key(
&headers,
"/api/auth/login",
peer(),
&[IpAddr::V4(Ipv4Addr::new(198, 51, 100, 10))]
),
"ip:203.0.113.7"
);
}
@@ -139,7 +163,7 @@ mod tests {
headers.insert("x-real-ip", HeaderValue::from_static("10.0.0.9"));
assert_eq!(
rate_limit_key(&headers, "/api/auth/login", peer(), true),
client_rate_limit_key(&headers, "/api/auth/login", peer(), &[peer().unwrap()]),
"ip:10.0.0.9"
);
}
@@ -155,7 +179,7 @@ mod tests {
);
assert_eq!(
rate_limit_key(&headers, "/api/auth/login", peer(), true),
client_rate_limit_key(&headers, "/api/auth/login", peer(), &[peer().unwrap()]),
"ip:10.0.0.6"
);
}
@@ -165,7 +189,7 @@ mod tests {
let headers = HeaderMap::new();
assert_eq!(
rate_limit_key(&headers, "/api/auth/login", peer(), true),
client_rate_limit_key(&headers, "/api/auth/login", peer(), &[peer().unwrap()]),
"ip:203.0.113.7"
);
}
@@ -180,7 +204,7 @@ mod tests {
);
assert_eq!(
rate_limit_key(&headers, "/api/auth/login", peer(), true),
client_rate_limit_key(&headers, "/api/auth/login", peer(), &[peer().unwrap()]),
"ip:203.0.113.7"
);
}
@@ -190,7 +214,7 @@ mod tests {
let headers = HeaderMap::new();
assert_eq!(
rate_limit_key(&headers, "/api/auth/login", None, false),
client_rate_limit_key(&headers, "/api/auth/login", None, &[]),
"anonymous:/api/auth/login"
);
}
+944
View File
@@ -0,0 +1,944 @@
use std::{
path::PathBuf,
sync::Arc,
time::{Duration, SystemTime},
};
use async_trait::async_trait;
use crank_artifacts::{
ArtifactError, ArtifactStore, ReconciliationCursor, ReconciliationMutation,
ReconciliationNamespace, ReconciliationPresence, ReconciliationRegistration,
ReconciliationScanStop, TempScanCursor,
};
use crank_registry::{
ArtifactClaimFinalization, ArtifactClaimFinalizeOutcome, ArtifactClaimOutcome,
ArtifactClaimRecheckOutcome, ArtifactClaimToken, ArtifactExpiredClaimProbe,
ClaimArtifactReconciliationRequest, ClaimExpiredArtifactReconciliationRequest,
PostgresRegistry,
};
use time::OffsetDateTime;
use tokio::time::MissedTickBehavior;
use tracing::{info, warn};
pub const RECONCILIATION_INTERVAL: Duration = Duration::from_secs(15 * 60);
pub const RECONCILIATION_GRACE: Duration = Duration::from_secs(24 * 60 * 60);
pub const RECONCILIATION_LEASE: Duration = Duration::from_secs(5 * 60);
pub const RECONCILIATION_TRAVERSAL_LIMIT: usize = 4_096;
pub const RECONCILIATION_CANDIDATE_LIMIT: usize = 32;
pub const RECONCILIATION_MUTATION_LIMIT: usize = 32;
pub const RECONCILIATION_TICK_DEADLINE: Duration = Duration::from_secs(30);
pub const TEMP_CLEANUP_GRACE: Duration = Duration::from_secs(60 * 60);
pub const TEMP_CLEANUP_SCAN_LIMIT: usize = 512;
pub const TEMP_CLEANUP_DELETE_LIMIT: usize = 16;
pub const IMPORT_JOB_CLEANUP_LIMIT: u32 = 128;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum Phase {
Recovery,
Sweep,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
struct StepLimits {
traversal_syscalls: usize,
candidates: usize,
mutations: usize,
}
#[derive(Debug)]
struct Step<C> {
cursor: Option<C>,
scan_complete: bool,
traversal_syscalls: usize,
candidates: usize,
mutation_attempts: usize,
classifications: ReconciliationClassificationCounters,
physical_mutation_possible: bool,
retryable: bool,
}
/// Bounded, identity-free classifications observed during one or more scans.
///
/// The fixed fields are the complete telemetry vocabulary: no digest, path,
/// token, source or workspace value can be attached to an item classification.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct ReconciliationClassificationCounters {
pub scanner_malformed: usize,
pub scanner_unsafe: usize,
pub recovery_present: usize,
pub recovery_safety: usize,
pub recovery_retryable: usize,
pub registration_integrity: usize,
pub registration_safety: usize,
pub registration_retryable: usize,
pub claim_metadata_conflict: usize,
pub mutation_integrity: usize,
pub mutation_safety: usize,
pub mutation_retryable: usize,
}
impl ReconciliationClassificationCounters {
fn checked_add(self, other: Self) -> Option<Self> {
Some(Self {
scanner_malformed: self
.scanner_malformed
.checked_add(other.scanner_malformed)?,
scanner_unsafe: self.scanner_unsafe.checked_add(other.scanner_unsafe)?,
recovery_present: self.recovery_present.checked_add(other.recovery_present)?,
recovery_safety: self.recovery_safety.checked_add(other.recovery_safety)?,
recovery_retryable: self
.recovery_retryable
.checked_add(other.recovery_retryable)?,
registration_integrity: self
.registration_integrity
.checked_add(other.registration_integrity)?,
registration_safety: self
.registration_safety
.checked_add(other.registration_safety)?,
registration_retryable: self
.registration_retryable
.checked_add(other.registration_retryable)?,
claim_metadata_conflict: self
.claim_metadata_conflict
.checked_add(other.claim_metadata_conflict)?,
mutation_integrity: self
.mutation_integrity
.checked_add(other.mutation_integrity)?,
mutation_safety: self.mutation_safety.checked_add(other.mutation_safety)?,
mutation_retryable: self
.mutation_retryable
.checked_add(other.mutation_retryable)?,
})
}
fn record_registration_error(&mut self, error: ArtifactError) -> bool {
match error {
ArtifactError::Integrity
| ArtifactError::EmptySource
| ArtifactError::SourceTooLarge => {
self.registration_integrity += 1;
false
}
ArtifactError::UnsafeRoot
| ArtifactError::InvalidReference
| ArtifactError::NotFound => {
self.registration_safety += 1;
false
}
ArtifactError::Storage => {
self.registration_retryable += 1;
true
}
}
}
fn record_mutation_error(&mut self, error: ArtifactError) -> bool {
match error {
ArtifactError::Integrity
| ArtifactError::EmptySource
| ArtifactError::SourceTooLarge => self.mutation_integrity += 1,
ArtifactError::UnsafeRoot
| ArtifactError::InvalidReference
| ArtifactError::NotFound => self.mutation_safety += 1,
ArtifactError::Storage => self.mutation_retryable += 1,
}
true
}
fn record_recovery_presence(&mut self, presence: ReconciliationPresence) -> bool {
match presence {
ReconciliationPresence::Final | ReconciliationPresence::Quarantine => {
self.recovery_present += 1;
false
}
ReconciliationPresence::Unsafe => {
self.recovery_safety += 1;
false
}
ReconciliationPresence::Retryable => {
self.recovery_retryable += 1;
true
}
ReconciliationPresence::Absent => false,
}
}
fn record_mutation_presence(&mut self, presence: ReconciliationPresence) -> bool {
match presence {
ReconciliationPresence::Absent => false,
ReconciliationPresence::Unsafe => {
self.mutation_safety += 1;
true
}
ReconciliationPresence::Final
| ReconciliationPresence::Quarantine
| ReconciliationPresence::Retryable => {
self.mutation_retryable += 1;
true
}
}
}
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct ReconciliationTickReport {
pub traversal_syscalls: usize,
pub candidates: usize,
pub mutation_attempts: usize,
pub classifications: ReconciliationClassificationCounters,
pub recovery_completed: bool,
pub cycle_completed: bool,
pub retryable: bool,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, thiserror::Error)]
pub enum ReconciliationCoordinatorError {
#[error("artifact reconciliation backend is unavailable")]
Backend,
#[error("artifact reconciliation backend exceeded its bounded contract")]
Bounds,
#[error("artifact reconciliation backend exceeded its deadline")]
Deadline,
}
#[async_trait]
trait ReconciliationBackend: Send + Sync {
type Cursor: Send;
async fn step(
&self,
phase: Phase,
cursor: Option<Self::Cursor>,
limits: StepLimits,
) -> Result<Step<Self::Cursor>, ReconciliationCoordinatorError>;
}
struct ReconciliationCoordinator<B: ReconciliationBackend> {
backend: B,
phase: Phase,
cursor: Option<B::Cursor>,
tick_limits: StepLimits,
}
#[derive(Clone)]
struct PostgresReconciliationBackend {
registry: PostgresRegistry,
store: Arc<ArtifactStore>,
}
enum PostgresReconciliationCursor {
ExpiredClaim(ArtifactExpiredClaimProbe),
Filesystem(ReconciliationCursor),
}
impl std::fmt::Debug for PostgresReconciliationCursor {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.write_str("PostgresReconciliationCursor(..)")
}
}
impl PostgresReconciliationBackend {
fn new(registry: PostgresRegistry, store: Arc<ArtifactStore>) -> Self {
Self { registry, store }
}
async fn database_now(&self) -> Result<OffsetDateTime, ReconciliationCoordinatorError> {
self.registry
.artifact_reconciliation_now()
.await
.map_err(|_| ReconciliationCoordinatorError::Backend)
}
async fn presence(
&self,
artifact_ref: crank_artifacts::ArtifactRef,
) -> Result<ReconciliationPresence, ReconciliationCoordinatorError> {
let store = Arc::clone(&self.store);
let result =
tokio::task::spawn_blocking(move || store.reconciliation_presence(&artifact_ref))
.await
.map_err(|_| ReconciliationCoordinatorError::Backend)?;
Ok(match result {
Ok(presence) => presence,
Err(ArtifactError::Storage) => ReconciliationPresence::Retryable,
Err(_) => ReconciliationPresence::Unsafe,
})
}
}
fn reconciliation_lease_expires_at(claimed_at: OffsetDateTime) -> OffsetDateTime {
claimed_at
+ time::Duration::seconds(
i64::try_from(RECONCILIATION_LEASE.as_secs())
.expect("the fixed reconciliation lease fits i64"),
)
}
impl<B: ReconciliationBackend> ReconciliationCoordinator<B> {
fn new(backend: B) -> Self {
Self {
backend,
phase: Phase::Recovery,
cursor: None,
tick_limits: StepLimits {
traversal_syscalls: RECONCILIATION_TRAVERSAL_LIMIT,
candidates: RECONCILIATION_CANDIDATE_LIMIT,
mutations: RECONCILIATION_MUTATION_LIMIT,
},
}
}
#[cfg(test)]
fn with_tick_limits(backend: B, tick_limits: StepLimits) -> Self {
Self {
backend,
phase: Phase::Recovery,
cursor: None,
tick_limits,
}
}
async fn tick(&mut self) -> Result<ReconciliationTickReport, ReconciliationCoordinatorError> {
let mut report = ReconciliationTickReport::default();
loop {
let limits = StepLimits {
traversal_syscalls: self
.tick_limits
.traversal_syscalls
.saturating_sub(report.traversal_syscalls),
candidates: self
.tick_limits
.candidates
.saturating_sub(report.candidates),
mutations: self
.tick_limits
.mutations
.saturating_sub(report.mutation_attempts),
};
if limits.traversal_syscalls == 0 || limits.candidates == 0 || limits.mutations == 0 {
break;
}
let step = match self
.backend
.step(self.phase, self.cursor.take(), limits)
.await
{
Ok(step) => step,
Err(error) => {
self.cursor = None;
self.phase = Phase::Recovery;
return Err(error);
}
};
if step.traversal_syscalls > limits.traversal_syscalls
|| step.candidates > limits.candidates
|| step.mutation_attempts > limits.mutations
|| step.mutation_attempts > step.candidates
{
self.cursor = None;
self.phase = Phase::Recovery;
return Err(ReconciliationCoordinatorError::Bounds);
}
report.traversal_syscalls += step.traversal_syscalls;
report.candidates += step.candidates;
report.mutation_attempts += step.mutation_attempts;
report.classifications = report
.classifications
.checked_add(step.classifications)
.ok_or_else(|| {
self.cursor = None;
self.phase = Phase::Recovery;
ReconciliationCoordinatorError::Bounds
})?;
report.retryable |= step.retryable;
let made_progress =
step.traversal_syscalls != 0 || step.candidates != 0 || step.mutation_attempts != 0;
if step.physical_mutation_possible {
// A cursor is valid only while the namespace is unchanged. A
// possible rename/unlink includes ambiguous fsync outcomes.
drop(step.cursor);
self.cursor = None;
self.phase = Phase::Recovery;
} else {
self.cursor = step.cursor;
if step.scan_complete {
self.cursor = None;
match self.phase {
Phase::Recovery => {
report.recovery_completed = true;
self.phase = Phase::Sweep;
}
Phase::Sweep => {
report.cycle_completed = true;
self.phase = Phase::Recovery;
break;
}
}
}
}
if step.retryable || (!made_progress && !step.scan_complete) {
break;
}
}
Ok(report)
}
}
#[async_trait]
impl ReconciliationBackend for PostgresReconciliationBackend {
type Cursor = PostgresReconciliationCursor;
async fn step(
&self,
phase: Phase,
cursor: Option<Self::Cursor>,
limits: StepLimits,
) -> Result<Step<Self::Cursor>, ReconciliationCoordinatorError> {
let mut recovered_candidates = 0;
let mut classifications = ReconciliationClassificationCounters::default();
let mut recovery_retryable = false;
let mut filesystem_cursor = None;
if phase == Phase::Recovery {
let after = match cursor {
Some(PostgresReconciliationCursor::ExpiredClaim(probe)) => Some(probe),
Some(PostgresReconciliationCursor::Filesystem(cursor)) => {
filesystem_cursor = Some(cursor);
None
}
None => None,
};
if filesystem_cursor.is_none() {
let database_now = self.database_now().await?;
let probe_limit = u32::try_from(limits.candidates).unwrap_or(u32::MAX);
let probes = self
.registry
.list_expired_artifact_reconciliation_probes_after(
database_now,
after.as_ref(),
probe_limit,
)
.await
.map_err(|_| ReconciliationCoordinatorError::Backend)?;
let page_full = probes.len() == probe_limit as usize;
let mut last_processed = after;
for probe in probes {
recovered_candidates += 1;
last_processed = Some(probe.clone());
let presence = self.presence(probe.artifact_ref().clone()).await?;
if presence != ReconciliationPresence::Absent {
recovery_retryable |= classifications.record_recovery_presence(presence);
continue;
}
let claimed_at = self.database_now().await?;
let claim = self
.registry
.claim_expired_artifact_reconciliation(
&probe,
ClaimExpiredArtifactReconciliationRequest {
token: ArtifactClaimToken::generate(),
claimed_at,
lease_expires_at: reconciliation_lease_expires_at(claimed_at),
},
)
.await
.map_err(|_| ReconciliationCoordinatorError::Backend)?;
let Some(claim) = claim else {
continue;
};
// Recheck after the DB CAS. A concurrent filesystem put
// does not consult PostgreSQL, so the first probe alone
// cannot prove absence at finalization time.
let presence = self.presence(claim.artifact_ref().clone()).await?;
if presence != ReconciliationPresence::Absent {
classifications.record_recovery_presence(presence);
recovery_retryable = true;
continue;
}
let recovered_at = self.database_now().await?;
let result = self
.registry
.recover_artifact_reconciliation_claim(
&claim,
ArtifactClaimFinalization::AlreadyAbsent,
recovered_at,
)
.await;
let recovered = matches!(result, Ok(ArtifactClaimFinalizeOutcome::Unavailable));
if recovered {
let presence = self.presence(claim.artifact_ref().clone()).await?;
if presence != ReconciliationPresence::Absent {
classifications.record_recovery_presence(presence);
recovery_retryable = true;
}
}
return Ok(Step {
cursor: last_processed.map(PostgresReconciliationCursor::ExpiredClaim),
scan_complete: false,
traversal_syscalls: 0,
candidates: recovered_candidates,
mutation_attempts: 1,
classifications,
physical_mutation_possible: false,
retryable: recovery_retryable || !recovered,
});
}
if page_full {
return Ok(Step {
cursor: last_processed.map(PostgresReconciliationCursor::ExpiredClaim),
scan_complete: false,
traversal_syscalls: 0,
candidates: recovered_candidates,
mutation_attempts: 0,
classifications,
physical_mutation_possible: false,
retryable: recovery_retryable,
});
}
}
} else if let Some(PostgresReconciliationCursor::Filesystem(cursor)) = cursor {
filesystem_cursor = Some(cursor);
}
let scan_candidate_limit = limits.candidates.saturating_sub(recovered_candidates);
if scan_candidate_limit == 0 {
return Ok(Step {
cursor: filesystem_cursor.map(PostgresReconciliationCursor::Filesystem),
scan_complete: false,
traversal_syscalls: 0,
candidates: recovered_candidates,
mutation_attempts: 0,
classifications,
physical_mutation_possible: false,
retryable: recovery_retryable,
});
}
let namespace = match phase {
Phase::Recovery => ReconciliationNamespace::Quarantine,
Phase::Sweep => ReconciliationNamespace::Final,
};
let store = Arc::clone(&self.store);
let (mut scan, candidates) = tokio::task::spawn_blocking(move || {
store.scan_reconciliation_namespace(
namespace,
filesystem_cursor,
limits.traversal_syscalls,
scan_candidate_limit,
)
})
.await
.map_err(|_| ReconciliationCoordinatorError::Backend)?
.map_err(|_| ReconciliationCoordinatorError::Backend)?;
let mut step = Step {
cursor: scan
.continuation
.take()
.map(PostgresReconciliationCursor::Filesystem),
scan_complete: scan.stop == ReconciliationScanStop::Complete,
traversal_syscalls: scan.traversal_syscalls,
candidates: recovered_candidates,
mutation_attempts: 0,
classifications: classifications
.checked_add(ReconciliationClassificationCounters {
scanner_malformed: scan.malformed,
scanner_unsafe: scan.unsafe_entries,
..ReconciliationClassificationCounters::default()
})
.ok_or(ReconciliationCoordinatorError::Bounds)?,
physical_mutation_possible: false,
retryable: recovery_retryable || scan.stop == ReconciliationScanStop::Retryable,
};
for candidate in candidates {
if step.candidates == limits.candidates || step.mutation_attempts == limits.mutations {
break;
}
step.candidates += 1;
let registration_grace = match phase {
Phase::Recovery => Duration::ZERO,
Phase::Sweep => RECONCILIATION_GRACE,
};
let store = Arc::clone(&self.store);
let registration = tokio::task::spawn_blocking(move || {
let registration = store.register_reconciliation(
&candidate,
registration_grace,
SystemTime::now(),
);
(candidate, registration)
})
.await
.map_err(|_| ReconciliationCoordinatorError::Backend)?;
let (candidate, registration) = registration;
let artifact = match registration {
Ok(ReconciliationRegistration::Registered(artifact)) => artifact,
Ok(ReconciliationRegistration::NotEligible) => continue,
Err(error) => {
step.retryable |= step.classifications.record_registration_error(error);
continue;
}
};
let claimed_at = self.database_now().await?;
let lease_expires_at = reconciliation_lease_expires_at(claimed_at);
let detached_grace = time::Duration::seconds(
i64::try_from(RECONCILIATION_GRACE.as_secs())
.expect("the fixed reconciliation grace fits i64"),
);
let claim = self
.registry
.claim_artifact_reconciliation(ClaimArtifactReconciliationRequest {
artifact: &artifact,
token: ArtifactClaimToken::generate(),
claimed_at,
lease_expires_at,
detached_grace,
})
.await
.map_err(|_| ReconciliationCoordinatorError::Backend)?;
let claim = match claim {
ArtifactClaimOutcome::Claimed(claim) => claim,
ArtifactClaimOutcome::HeldByOther
| ArtifactClaimOutcome::ActiveReference
| ArtifactClaimOutcome::DetachedReferenceInGrace => continue,
ArtifactClaimOutcome::MetadataConflict => {
step.classifications.claim_metadata_conflict += 1;
continue;
}
};
let rechecked_at = self.database_now().await?;
let recheck = self
.registry
.recheck_artifact_reconciliation_claim(&claim, rechecked_at, detached_grace)
.await
.map_err(|_| ReconciliationCoordinatorError::Backend)?;
if recheck != ArtifactClaimRecheckOutcome::Mutate {
continue;
}
step.mutation_attempts += 1;
step.physical_mutation_possible = true;
let store = Arc::clone(&self.store);
let mutation = match tokio::task::spawn_blocking(move || match phase {
Phase::Recovery => store.delete_quarantined_reconciliation(candidate),
Phase::Sweep => store.quarantine_reconciliation(candidate),
})
.await
{
Ok(mutation) => mutation,
Err(_) => Err(ArtifactError::Storage),
};
let mut observation = match mutation {
Ok(ReconciliationMutation::Quarantined)
| Ok(ReconciliationMutation::AlreadyQuarantined) => {
ArtifactClaimFinalization::Quarantined
}
Ok(ReconciliationMutation::Deleted) => ArtifactClaimFinalization::Deleted,
Ok(ReconciliationMutation::AlreadyAbsent) => {
ArtifactClaimFinalization::AlreadyAbsent
}
Ok(ReconciliationMutation::Retryable) => {
step.classifications.mutation_retryable += 1;
step.retryable = true;
ArtifactClaimFinalization::Retryable
}
Err(error) => {
step.retryable |= step.classifications.record_mutation_error(error);
ArtifactClaimFinalization::Retryable
}
};
if matches!(
observation,
ArtifactClaimFinalization::Deleted | ArtifactClaimFinalization::AlreadyAbsent
) {
// `unavailable` means both canonical locations are confirmed
// absent. A concurrent republish can recreate final bytes
// without consulting PostgreSQL, so unlink alone is not proof.
let presence = self.presence(claim.artifact_ref().clone()).await?;
observation = match presence {
ReconciliationPresence::Absent => ArtifactClaimFinalization::AlreadyAbsent,
presence => {
step.retryable |= step.classifications.record_mutation_presence(presence);
ArtifactClaimFinalization::Retryable
}
};
}
let observed_at = self.database_now().await?;
let finalization = match phase {
Phase::Recovery => {
self.registry
.recover_artifact_reconciliation_claim(&claim, observation, observed_at)
.await
}
Phase::Sweep => {
self.registry
.finalize_artifact_reconciliation_claim(&claim, observation, observed_at)
.await
}
};
let finalized_as_expected =
match observation {
ArtifactClaimFinalization::Deleted
| ArtifactClaimFinalization::AlreadyAbsent => matches!(
finalization.as_ref(),
Ok(ArtifactClaimFinalizeOutcome::Unavailable)
),
ArtifactClaimFinalization::Quarantined
| ArtifactClaimFinalization::Retryable => matches!(
finalization.as_ref(),
Ok(ArtifactClaimFinalizeOutcome::Retained)
),
};
if !finalized_as_expected {
step.retryable = true;
}
if matches!(
finalization.as_ref(),
Ok(ArtifactClaimFinalizeOutcome::Unavailable)
) {
let presence = self.presence(claim.artifact_ref().clone()).await?;
step.retryable |= step.classifications.record_mutation_presence(presence);
}
// The scan cursor and every remaining observation came from the
// pre-mutation namespace. Return immediately so the coordinator
// drops them and restarts a recovery cycle from `None`.
break;
}
Ok(step)
}
}
/// Opens the protected root without running filesystem syscalls on Tokio's
/// async executor.
pub async fn open_reconciliation_store(
root: PathBuf,
) -> Result<Arc<ArtifactStore>, ReconciliationCoordinatorError> {
tokio::task::spawn_blocking(move || ArtifactStore::open(root).map(Arc::new))
.await
.map_err(|_| ReconciliationCoordinatorError::Backend)?
.map_err(|_| ReconciliationCoordinatorError::Backend)
}
/// Runs the immediate bounded startup cycle, then schedules 15-minute ticks.
/// Recovery remains ahead of the final sweep even when it spans several ticks.
pub async fn spawn_artifact_reconciliation(registry: PostgresRegistry, store: Arc<ArtifactStore>) {
let cleanup_registry = registry.clone();
let backend = PostgresReconciliationBackend::new(registry, Arc::clone(&store));
let mut coordinator = ReconciliationCoordinator::new(backend);
let mut temp_cursor = None;
let startup_maintenance = async {
observe_import_job_cleanup(&cleanup_registry).await;
observe_tick(run_bounded_reconciliation_tick(&mut coordinator).await);
temp_cursor = observe_stale_temp_cleanup(Arc::clone(&store), None).await;
};
if tokio::time::timeout(RECONCILIATION_TICK_DEADLINE, startup_maintenance)
.await
.is_err()
{
warn!(
name: "admin.artifact_maintenance.startup_deadline",
"startup maintenance exceeded its shared deadline; remaining work will resume in background"
);
}
tokio::spawn(async move {
let mut interval = tokio::time::interval(RECONCILIATION_INTERVAL);
interval.set_missed_tick_behavior(MissedTickBehavior::Skip);
// The immediate tick was run above as part of startup composition.
interval.tick().await;
loop {
interval.tick().await;
observe_import_job_cleanup(&cleanup_registry).await;
observe_tick(run_bounded_reconciliation_tick(&mut coordinator).await);
temp_cursor = observe_stale_temp_cleanup(Arc::clone(&store), temp_cursor).await;
}
});
}
async fn observe_import_job_cleanup(registry: &PostgresRegistry) {
let cleanup = async {
let mut deleted_jobs = 0_u64;
let mut detached_sources = 0_u64;
loop {
let report = registry
.cleanup_expired_import_jobs(IMPORT_JOB_CLEANUP_LIMIT)
.await?;
deleted_jobs = deleted_jobs.saturating_add(report.deleted_jobs);
detached_sources = detached_sources.saturating_add(report.detached_sources);
if !report.more_work {
return Ok::<_, crank_registry::RegistryError>((deleted_jobs, detached_sources));
}
// Each pass commits independently. Yield before requesting the
// next lock batch so normal import traffic can make progress.
tokio::task::yield_now().await;
}
};
match tokio::time::timeout(RECONCILIATION_TICK_DEADLINE, cleanup).await {
Ok(Ok((deleted_jobs, detached_sources))) => info!(
name: "admin.openapi_import_cleanup.tick",
deleted_jobs,
detached_sources,
more_work = false,
"OpenAPI import cleanup tick completed"
),
Ok(Err(_)) => warn!(
name: "admin.openapi_import_cleanup.failed",
error_category = "registry",
"OpenAPI import cleanup tick will be retried"
),
Err(_) => warn!(
name: "admin.openapi_import_cleanup.failed",
error_category = "deadline",
"OpenAPI import cleanup tick exceeded its deadline"
),
}
}
async fn run_bounded_reconciliation_tick<B: ReconciliationBackend>(
coordinator: &mut ReconciliationCoordinator<B>,
) -> Result<ReconciliationTickReport, ReconciliationCoordinatorError> {
tokio::time::timeout(RECONCILIATION_TICK_DEADLINE, coordinator.tick())
.await
.map_err(|_| ReconciliationCoordinatorError::Deadline)?
}
async fn observe_stale_temp_cleanup(
store: Arc<ArtifactStore>,
cursor: Option<TempScanCursor>,
) -> Option<TempScanCursor> {
let progress = Arc::new(std::sync::Mutex::new(cursor));
let cleanup_progress = Arc::clone(&progress);
let cleanup = async move {
let mut scanned = 0_usize;
let mut omitted = 0_usize;
let mut deleted = 0_usize;
let mut retryable = 0_usize;
loop {
let scanner = Arc::clone(&store);
let cursor = cleanup_progress
.lock()
.map_err(|_| ArtifactError::Storage)?
.clone();
let (scan, candidates, next_cursor) = tokio::task::spawn_blocking(move || {
scanner.scan_stale_temps_after(
TEMP_CLEANUP_GRACE,
cursor,
TEMP_CLEANUP_SCAN_LIMIT,
TEMP_CLEANUP_DELETE_LIMIT,
)
})
.await
.map_err(|_| ArtifactError::Storage)??;
scanned = scanned.saturating_add(scan.scanned);
omitted = omitted.saturating_add(scan.omitted);
*cleanup_progress
.lock()
.map_err(|_| ArtifactError::Storage)? = Some(next_cursor);
for candidate in candidates {
let store = Arc::clone(&store);
match tokio::task::spawn_blocking(move || store.delete_stale_temp(candidate)).await
{
Ok(Ok(())) => deleted += 1,
Ok(Err(_)) | Err(_) => retryable += 1,
}
}
if scan.complete {
return Ok::<_, ArtifactError>((
scanned,
omitted,
deleted,
retryable,
cleanup_progress
.lock()
.map_err(|_| ArtifactError::Storage)?
.clone(),
));
}
tokio::task::yield_now().await;
}
};
match tokio::time::timeout(RECONCILIATION_TICK_DEADLINE, cleanup).await {
Ok(Ok((scanned, omitted, deleted, retryable, next_cursor))) => {
info!(
name: "admin.artifact_temp_cleanup.tick",
scanned,
omitted,
deleted,
retryable,
"artifact temporary-file cleanup tick completed"
);
next_cursor
}
Ok(Err(_)) => {
warn!(
name: "admin.artifact_temp_cleanup.failed",
error_category = "backend",
"artifact temporary-file cleanup tick will be retried"
);
progress.lock().ok().and_then(|cursor| cursor.clone())
}
Err(_) => {
warn!(
name: "admin.artifact_temp_cleanup.failed",
error_category = "deadline",
"artifact temporary-file cleanup tick exceeded its deadline"
);
progress.lock().ok().and_then(|cursor| cursor.clone())
}
}
}
fn observe_tick(result: Result<ReconciliationTickReport, ReconciliationCoordinatorError>) {
match result {
Ok(report) => info!(
name: "admin.artifact_reconciliation.tick",
traversal_syscalls = report.traversal_syscalls,
candidates = report.candidates,
mutation_attempts = report.mutation_attempts,
scanner_malformed = report.classifications.scanner_malformed,
scanner_unsafe = report.classifications.scanner_unsafe,
recovery_present = report.classifications.recovery_present,
recovery_safety = report.classifications.recovery_safety,
recovery_retryable = report.classifications.recovery_retryable,
registration_integrity = report.classifications.registration_integrity,
registration_safety = report.classifications.registration_safety,
registration_retryable = report.classifications.registration_retryable,
claim_metadata_conflict = report.classifications.claim_metadata_conflict,
mutation_integrity = report.classifications.mutation_integrity,
mutation_safety = report.classifications.mutation_safety,
mutation_retryable = report.classifications.mutation_retryable,
recovery_completed = report.recovery_completed,
cycle_completed = report.cycle_completed,
retryable = report.retryable,
"artifact reconciliation tick completed"
),
Err(error) => warn!(
name: "admin.artifact_reconciliation.failed",
error_category = match error {
ReconciliationCoordinatorError::Backend => "backend",
ReconciliationCoordinatorError::Bounds => "bounds",
ReconciliationCoordinatorError::Deadline => "deadline",
},
"artifact reconciliation tick will be retried"
),
}
}
#[cfg(test)]
#[path = "reconciliation/tests.rs"]
mod tests;
+694
View File
@@ -0,0 +1,694 @@
use std::{
collections::VecDeque,
fs,
os::unix::fs::{PermissionsExt, symlink},
path::{Path, PathBuf},
sync::{
Arc, Mutex,
atomic::{AtomicUsize, Ordering},
},
};
use async_trait::async_trait;
use crank_artifacts::{ReconciliationRegistration, RegisteredArtifact};
use crank_registry::MigrationAuthority;
use sqlx::Row;
use super::*;
static NEXT_TEST_ROOT: AtomicUsize = AtomicUsize::new(0);
struct TestRoot(PathBuf);
impl TestRoot {
fn new() -> Self {
let path = std::env::temp_dir().join(format!(
"crank-admin-reconciliation-{}-{}",
std::process::id(),
NEXT_TEST_ROOT.fetch_add(1, Ordering::Relaxed)
));
fs::create_dir(&path).unwrap();
fs::set_permissions(&path, fs::Permissions::from_mode(0o700)).unwrap();
Self(path)
}
}
impl Drop for TestRoot {
fn drop(&mut self) {
let _ = fs::set_permissions(&self.0, fs::Permissions::from_mode(0o700));
let _ = fs::remove_dir_all(&self.0);
}
}
fn artifact_path(root: &Path, artifact: &RegisteredArtifact) -> PathBuf {
let digest = artifact.artifact_ref().digest_hex();
root.join("sha256").join(&digest[..2]).join(digest)
}
fn age_artifact(root: &Path, artifact: &RegisteredArtifact) {
fs::File::open(artifact_path(root, artifact))
.unwrap()
.set_modified(SystemTime::now() - RECONCILIATION_GRACE - Duration::from_secs(60))
.unwrap();
}
fn remove_registered_artifact(store: &ArtifactStore, artifact: &RegisteredArtifact) {
let (_, candidates) = store
.scan_reconciliation_namespace(ReconciliationNamespace::Final, None, 4_096, 32)
.unwrap();
let candidate = candidates
.into_iter()
.find(|candidate| {
matches!(
store.register_reconciliation(candidate, Duration::ZERO, SystemTime::now()),
Ok(ReconciliationRegistration::Registered(found)) if found == *artifact
)
})
.expect("the final artifact is discoverable");
assert!(matches!(
store.quarantine_reconciliation(candidate),
Ok(ReconciliationMutation::Quarantined | ReconciliationMutation::AlreadyQuarantined)
));
let (_, candidates) = store
.scan_reconciliation_namespace(ReconciliationNamespace::Quarantine, None, 4_096, 32)
.unwrap();
let candidate = candidates
.into_iter()
.find(|candidate| {
matches!(
store.register_reconciliation(candidate, Duration::ZERO, SystemTime::now()),
Ok(ReconciliationRegistration::Registered(found)) if found == *artifact
)
})
.expect("the quarantined artifact is discoverable");
assert!(matches!(
store.delete_quarantined_reconciliation(candidate),
Ok(ReconciliationMutation::Deleted | ReconciliationMutation::AlreadyAbsent)
));
}
#[derive(Clone, Copy)]
struct StepTemplate {
scan_complete: bool,
traversal_syscalls: usize,
candidates: usize,
mutation_attempts: usize,
classifications: ReconciliationClassificationCounters,
physical_mutation_possible: bool,
retryable: bool,
return_cursor: bool,
error: bool,
}
impl StepTemplate {
fn complete() -> Self {
Self {
scan_complete: true,
traversal_syscalls: 1,
candidates: 0,
mutation_attempts: 0,
classifications: ReconciliationClassificationCounters::default(),
physical_mutation_possible: false,
retryable: false,
return_cursor: false,
error: false,
}
}
}
struct CursorEvidence {
alive: Arc<AtomicUsize>,
}
impl CursorEvidence {
fn new(alive: Arc<AtomicUsize>, maximum: &AtomicUsize) -> Self {
let current = alive.fetch_add(1, Ordering::SeqCst) + 1;
maximum.fetch_max(current, Ordering::SeqCst);
Self { alive }
}
}
impl Drop for CursorEvidence {
fn drop(&mut self) {
self.alive.fetch_sub(1, Ordering::SeqCst);
}
}
struct ScriptedBackend {
steps: Mutex<VecDeque<StepTemplate>>,
calls: Mutex<Vec<(Phase, bool, StepLimits)>>,
alive: Arc<AtomicUsize>,
maximum: AtomicUsize,
}
impl ScriptedBackend {
fn new(steps: impl IntoIterator<Item = StepTemplate>) -> Self {
Self {
steps: Mutex::new(steps.into_iter().collect()),
calls: Mutex::new(Vec::new()),
alive: Arc::new(AtomicUsize::new(0)),
maximum: AtomicUsize::new(0),
}
}
}
#[async_trait]
impl ReconciliationBackend for Arc<ScriptedBackend> {
type Cursor = CursorEvidence;
async fn step(
&self,
phase: Phase,
cursor: Option<Self::Cursor>,
limits: StepLimits,
) -> Result<Step<Self::Cursor>, ReconciliationCoordinatorError> {
self.calls
.lock()
.unwrap()
.push((phase, cursor.is_some(), limits));
drop(cursor);
let template = self.steps.lock().unwrap().pop_front().unwrap();
if template.error {
return Err(ReconciliationCoordinatorError::Backend);
}
let cursor = template
.return_cursor
.then(|| CursorEvidence::new(self.alive.clone(), &self.maximum));
Ok(Step {
cursor,
scan_complete: template.scan_complete,
traversal_syscalls: template.traversal_syscalls,
candidates: template.candidates,
mutation_attempts: template.mutation_attempts,
classifications: template.classifications,
physical_mutation_possible: template.physical_mutation_possible,
retryable: template.retryable,
})
}
}
#[tokio::test]
async fn recovery_always_completes_before_final_sweep() {
let backend = Arc::new(ScriptedBackend::new([
StepTemplate::complete(),
StepTemplate::complete(),
]));
let mut coordinator = ReconciliationCoordinator::new(backend.clone());
let report = coordinator.tick().await.unwrap();
assert!(report.recovery_completed);
assert!(report.cycle_completed);
let phases = backend
.calls
.lock()
.unwrap()
.iter()
.map(|call| call.0)
.collect::<Vec<_>>();
assert_eq!(phases, vec![Phase::Recovery, Phase::Sweep]);
}
#[tokio::test]
async fn possible_mutation_discards_cursor_and_restarts_recovery() {
let paged = StepTemplate {
scan_complete: false,
traversal_syscalls: 2,
candidates: 1,
mutation_attempts: 0,
classifications: ReconciliationClassificationCounters::default(),
physical_mutation_possible: false,
retryable: true,
return_cursor: true,
error: false,
};
let mutation = StepTemplate {
scan_complete: false,
traversal_syscalls: 1,
candidates: 1,
mutation_attempts: 1,
classifications: ReconciliationClassificationCounters::default(),
physical_mutation_possible: true,
retryable: false,
return_cursor: true,
error: false,
};
let backend = Arc::new(ScriptedBackend::new([
StepTemplate::complete(),
paged,
mutation,
StepTemplate::complete(),
StepTemplate::complete(),
]));
let mut coordinator = ReconciliationCoordinator::new(backend.clone());
let first = coordinator.tick().await.unwrap();
assert!(first.retryable);
assert_eq!(backend.alive.load(Ordering::SeqCst), 1);
let second = coordinator.tick().await.unwrap();
assert!(second.cycle_completed);
let calls = backend.calls.lock().unwrap();
assert_eq!(
calls
.iter()
.map(|(phase, had_cursor, _)| (*phase, *had_cursor))
.collect::<Vec<_>>(),
vec![
(Phase::Recovery, false),
(Phase::Sweep, false),
(Phase::Sweep, true),
(Phase::Recovery, false),
(Phase::Sweep, false),
]
);
assert_eq!(backend.alive.load(Ordering::SeqCst), 0);
assert_eq!(backend.maximum.load(Ordering::SeqCst), 1);
}
#[tokio::test]
async fn backend_error_discards_cursor_and_restarts_recovery() {
let paged = StepTemplate {
scan_complete: false,
traversal_syscalls: 1,
candidates: 1,
mutation_attempts: 0,
classifications: ReconciliationClassificationCounters::default(),
physical_mutation_possible: false,
retryable: true,
return_cursor: true,
error: false,
};
let failure = StepTemplate {
error: true,
..StepTemplate::complete()
};
let backend = Arc::new(ScriptedBackend::new([
StepTemplate::complete(),
paged,
failure,
StepTemplate::complete(),
StepTemplate::complete(),
]));
let mut coordinator = ReconciliationCoordinator::new(backend.clone());
assert!(coordinator.tick().await.unwrap().retryable);
assert_eq!(backend.alive.load(Ordering::SeqCst), 1);
assert_eq!(
coordinator.tick().await,
Err(ReconciliationCoordinatorError::Backend)
);
assert_eq!(backend.alive.load(Ordering::SeqCst), 0);
assert!(coordinator.tick().await.unwrap().cycle_completed);
let calls = backend.calls.lock().unwrap();
assert_eq!(
calls
.iter()
.map(|(phase, had_cursor, _)| (*phase, *had_cursor))
.collect::<Vec<_>>(),
vec![
(Phase::Recovery, false),
(Phase::Sweep, false),
(Phase::Sweep, true),
(Phase::Recovery, false),
(Phase::Sweep, false),
]
);
}
#[tokio::test]
async fn backend_cannot_exceed_tick_limits() {
let backend = Arc::new(ScriptedBackend::new([StepTemplate {
scan_complete: false,
traversal_syscalls: RECONCILIATION_TRAVERSAL_LIMIT + 1,
candidates: 0,
mutation_attempts: 0,
classifications: ReconciliationClassificationCounters::default(),
physical_mutation_possible: false,
retryable: false,
return_cursor: false,
error: false,
}]));
let mut coordinator = ReconciliationCoordinator::new(backend);
assert_eq!(
coordinator.tick().await,
Err(ReconciliationCoordinatorError::Bounds)
);
}
#[tokio::test]
async fn tick_stops_at_the_exact_candidate_and_mutation_limits() {
let backend = Arc::new(ScriptedBackend::new(
(0..RECONCILIATION_MUTATION_LIMIT).map(|_| StepTemplate {
scan_complete: false,
traversal_syscalls: 1,
candidates: 1,
mutation_attempts: 1,
classifications: ReconciliationClassificationCounters::default(),
physical_mutation_possible: true,
retryable: false,
return_cursor: true,
error: false,
}),
));
let mut coordinator = ReconciliationCoordinator::new(backend.clone());
let report = coordinator.tick().await.unwrap();
assert_eq!(report.candidates, RECONCILIATION_CANDIDATE_LIMIT);
assert_eq!(report.mutation_attempts, RECONCILIATION_MUTATION_LIMIT);
assert_eq!(report.traversal_syscalls, RECONCILIATION_MUTATION_LIMIT);
assert_eq!(
backend.calls.lock().unwrap().len(),
RECONCILIATION_MUTATION_LIMIT
);
assert_eq!(backend.alive.load(Ordering::SeqCst), 0);
assert_eq!(backend.maximum.load(Ordering::SeqCst), 1);
}
#[tokio::test]
async fn coordinator_retains_at_most_one_cursor_between_ticks() {
let backend = Arc::new(ScriptedBackend::new((0..128).map(|_| StepTemplate {
scan_complete: false,
traversal_syscalls: 1,
candidates: 0,
mutation_attempts: 0,
classifications: ReconciliationClassificationCounters::default(),
physical_mutation_possible: false,
retryable: true,
return_cursor: true,
error: false,
})));
let mut coordinator = ReconciliationCoordinator::new(backend.clone());
for _ in 0..128 {
coordinator.tick().await.unwrap();
assert_eq!(backend.alive.load(Ordering::SeqCst), 1);
}
assert_eq!(backend.maximum.load(Ordering::SeqCst), 1);
drop(coordinator);
assert_eq!(backend.alive.load(Ordering::SeqCst), 0);
}
#[test]
fn registration_failures_have_a_closed_redacted_classification() {
let mut counters = ReconciliationClassificationCounters::default();
assert!(!counters.record_registration_error(ArtifactError::Integrity));
assert!(!counters.record_registration_error(ArtifactError::UnsafeRoot));
assert!(counters.record_registration_error(ArtifactError::Storage));
assert!(!counters.record_registration_error(ArtifactError::NotFound));
assert!(!counters.record_registration_error(ArtifactError::InvalidReference));
assert!(!counters.record_registration_error(ArtifactError::EmptySource));
assert!(!counters.record_registration_error(ArtifactError::SourceTooLarge));
assert_eq!(
counters,
ReconciliationClassificationCounters {
scanner_malformed: 0,
scanner_unsafe: 0,
recovery_present: 0,
recovery_safety: 0,
recovery_retryable: 0,
registration_integrity: 3,
registration_safety: 3,
registration_retryable: 1,
claim_metadata_conflict: 0,
mutation_integrity: 0,
mutation_safety: 0,
mutation_retryable: 0,
}
);
let debug = format!("{counters:?}");
assert!(!debug.contains("sha256:"));
assert!(!debug.contains('/'));
}
#[test]
fn mutation_failures_are_classified_and_always_retryable() {
let mut counters = ReconciliationClassificationCounters::default();
assert!(counters.record_mutation_error(ArtifactError::Integrity));
assert!(counters.record_mutation_error(ArtifactError::UnsafeRoot));
assert!(counters.record_mutation_error(ArtifactError::Storage));
assert!(counters.record_mutation_error(ArtifactError::NotFound));
assert!(counters.record_mutation_error(ArtifactError::InvalidReference));
assert!(counters.record_mutation_error(ArtifactError::EmptySource));
assert!(counters.record_mutation_error(ArtifactError::SourceTooLarge));
assert_eq!(counters.mutation_integrity, 3);
assert_eq!(counters.mutation_safety, 3);
assert_eq!(counters.mutation_retryable, 1);
}
#[tokio::test]
async fn production_backend_recovers_absence_before_sweep_and_restarts_after_mutation() {
let database_url =
crank_test_support::postgres_schema_url("admin_reconciliation_backend").await;
let pool = sqlx::PgPool::connect(&database_url).await.unwrap();
MigrationAuthority::apply(&pool).await.unwrap();
let registry = PostgresRegistry::connect(&database_url).await.unwrap();
let root = TestRoot::new();
let store = ArtifactStore::open(&root.0).unwrap();
let now = OffsetDateTime::now_utc();
let expired = store.put_registered(b"expired physical absence\n").unwrap();
let expired_claim = registry
.claim_artifact_reconciliation(ClaimArtifactReconciliationRequest {
artifact: &expired,
token: ArtifactClaimToken::generate(),
claimed_at: now - time::Duration::minutes(10),
lease_expires_at: now - time::Duration::minutes(5),
detached_grace: time::Duration::hours(24),
})
.await
.unwrap();
assert!(matches!(expired_claim, ArtifactClaimOutcome::Claimed(_)));
remove_registered_artifact(&store, &expired);
sqlx::query(
"update artifact_blobs
set claim_expires_at = clock_timestamp() - interval '1 second'
where digest = $1",
)
.bind(expired.artifact_ref().digest_hex())
.execute(registry.pool())
.await
.unwrap();
let corrupted = store
.put_registered(b"integrity-blocked-candidate\n")
.unwrap();
let corrupted_path = artifact_path(&root.0, &corrupted);
let corrupted_size = fs::metadata(&corrupted_path).unwrap().len() as usize;
fs::set_permissions(&corrupted_path, fs::Permissions::from_mode(0o600)).unwrap();
fs::write(&corrupted_path, vec![b'x'; corrupted_size]).unwrap();
fs::set_permissions(&corrupted_path, fs::Permissions::from_mode(0o400)).unwrap();
age_artifact(&root.0, &corrupted);
let later = store.put_registered(b"later-valid-candidate\n").unwrap();
age_artifact(&root.0, &later);
let metadata_conflict = store
.put_registered(b"metadata-conflict-candidate\n")
.unwrap();
age_artifact(&root.0, &metadata_conflict);
sqlx::query(
"insert into artifact_blobs
(digest, artifact_ref, size_bytes, storage_lifecycle, created_at, updated_at)
values ($1, $2, $3, 'available', $4, $4)",
)
.bind(metadata_conflict.artifact_ref().digest_hex())
.bind(metadata_conflict.artifact_ref().as_str())
.bind(i64::try_from(metadata_conflict.size_bytes()).unwrap() + 1)
.bind(now)
.execute(registry.pool())
.await
.unwrap();
let shard = artifact_path(&root.0, &later)
.parent()
.unwrap()
.to_path_buf();
fs::write(shard.join("not-a-digest"), b"malformed").unwrap();
let unsafe_name = format!(
"{}{}",
&later.artifact_ref().digest_hex()[..2],
"f".repeat(62)
);
assert_ne!(unsafe_name, later.artifact_ref().digest_hex());
symlink("not-a-digest", shard.join(unsafe_name)).unwrap();
let backend = PostgresReconciliationBackend::new(registry.clone(), Arc::new(store.clone()));
let mut coordinator = ReconciliationCoordinator::with_tick_limits(
backend,
StepLimits {
traversal_syscalls: RECONCILIATION_TRAVERSAL_LIMIT,
candidates: RECONCILIATION_CANDIDATE_LIMIT,
mutations: 1,
},
);
// The single mutation slot is consumed by expired-claim recovery. The old
// final candidate proves Sweep did not run ahead of Recovery.
let first = coordinator.tick().await.unwrap();
assert_eq!(first.mutation_attempts, 1);
assert!(!first.recovery_completed);
assert_eq!(
store.reconciliation_presence(later.artifact_ref()).unwrap(),
ReconciliationPresence::Final
);
let lifecycle: String =
sqlx::query_scalar("select storage_lifecycle from artifact_blobs where digest = $1")
.bind(expired.artifact_ref().digest_hex())
.fetch_one(registry.pool())
.await
.unwrap();
assert_eq!(lifecycle, "unavailable");
// The real final sweep classifies blocked entries and still reaches the
// later valid item. Its physical mutation invalidates the final cursor.
let second = coordinator.tick().await.unwrap();
assert!(second.recovery_completed);
assert_eq!(second.mutation_attempts, 1);
assert_eq!(
store.reconciliation_presence(later.artifact_ref()).unwrap(),
ReconciliationPresence::Quarantine
);
// Model the next scheduled/restarted lease window. Because the coordinator
// reset to Recovery and discarded its final cursor, quarantine is handled
// before another final sweep.
sqlx::query("update artifact_blobs set claim_expires_at = $1 where digest = $2")
.bind(OffsetDateTime::now_utc() - time::Duration::seconds(1))
.bind(later.artifact_ref().digest_hex())
.execute(registry.pool())
.await
.unwrap();
let third = coordinator.tick().await.unwrap();
assert_eq!(third.mutation_attempts, 1);
assert_eq!(
store.reconciliation_presence(later.artifact_ref()).unwrap(),
ReconciliationPresence::Absent
);
let fourth = coordinator.tick().await.unwrap();
assert!(fourth.cycle_completed);
let classifications = first
.classifications
.checked_add(second.classifications)
.and_then(|value| value.checked_add(third.classifications))
.and_then(|value| value.checked_add(fourth.classifications))
.unwrap();
assert!(classifications.scanner_malformed > 0);
assert!(classifications.scanner_unsafe > 0);
assert!(classifications.registration_integrity > 0);
assert!(classifications.claim_metadata_conflict > 0);
assert_eq!(
store
.reconciliation_presence(corrupted.artifact_ref())
.unwrap(),
ReconciliationPresence::Unsafe
);
assert_eq!(classifications.registration_safety, 0);
assert_eq!(classifications.registration_retryable, 0);
}
#[tokio::test]
async fn expired_claim_cursor_prevents_unsafe_prefix_starvation_across_ticks() {
let database_url =
crank_test_support::postgres_schema_url("admin_reconciliation_starvation").await;
let pool = sqlx::PgPool::connect(&database_url).await.unwrap();
MigrationAuthority::apply(&pool).await.unwrap();
let registry = PostgresRegistry::connect(&database_url).await.unwrap();
let root = TestRoot::new();
let store = ArtifactStore::open(&root.0).unwrap();
let now = OffsetDateTime::now_utc();
for index in 0..=RECONCILIATION_CANDIDATE_LIMIT {
let artifact = store
.put_registered(format!("unsafe expired claim {index}\n").as_bytes())
.unwrap();
let outcome = registry
.claim_artifact_reconciliation(ClaimArtifactReconciliationRequest {
artifact: &artifact,
token: ArtifactClaimToken::generate(),
claimed_at: now - time::Duration::minutes(20),
lease_expires_at: now - time::Duration::minutes(15),
detached_grace: time::Duration::hours(24),
})
.await
.unwrap();
assert!(matches!(outcome, ArtifactClaimOutcome::Claimed(_)));
sqlx::query(
"update artifact_blobs
set claim_expires_at = clock_timestamp() - interval '10 minutes'
where digest = $1",
)
.bind(artifact.artifact_ref().digest_hex())
.execute(registry.pool())
.await
.unwrap();
fs::set_permissions(
artifact_path(&root.0, &artifact),
fs::Permissions::from_mode(0o600),
)
.unwrap();
}
let absent = store
.put_registered(b"absent after unsafe prefix\n")
.unwrap();
let outcome = registry
.claim_artifact_reconciliation(ClaimArtifactReconciliationRequest {
artifact: &absent,
token: ArtifactClaimToken::generate(),
claimed_at: now - time::Duration::minutes(15),
lease_expires_at: now - time::Duration::minutes(10),
detached_grace: time::Duration::hours(24),
})
.await
.unwrap();
assert!(matches!(outcome, ArtifactClaimOutcome::Claimed(_)));
sqlx::query(
"update artifact_blobs
set claim_expires_at = clock_timestamp() - interval '5 minutes'
where digest = $1",
)
.bind(absent.artifact_ref().digest_hex())
.execute(registry.pool())
.await
.unwrap();
remove_registered_artifact(&store, &absent);
let backend = PostgresReconciliationBackend::new(registry.clone(), Arc::new(store));
let mut coordinator = ReconciliationCoordinator::new(backend);
let first = coordinator.tick().await.unwrap();
assert_eq!(first.candidates, RECONCILIATION_CANDIDATE_LIMIT);
assert_eq!(first.mutation_attempts, 0);
assert_eq!(
first.classifications.recovery_safety,
RECONCILIATION_CANDIDATE_LIMIT
);
let first_lifecycle: String =
sqlx::query_scalar("select storage_lifecycle from artifact_blobs where digest = $1")
.bind(absent.artifact_ref().digest_hex())
.fetch_one(registry.pool())
.await
.unwrap();
assert_eq!(first_lifecycle, "available");
let second = coordinator.tick().await.unwrap();
assert!(second.classifications.recovery_safety > 0);
assert!(second.mutation_attempts > 0);
let second_lifecycle: String =
sqlx::query_scalar("select storage_lifecycle from artifact_blobs where digest = $1")
.bind(absent.artifact_ref().digest_hex())
.fetch_one(registry.pool())
.await
.unwrap();
assert_eq!(second_lifecycle, "unavailable");
}
#[path = "tests/commit_failure.rs"]
mod commit_failure;
@@ -0,0 +1,149 @@
use super::*;
#[tokio::test]
async fn finalize_commit_failure_stays_retryable_and_recovers_on_a_fresh_cycle() {
let database_url =
crank_test_support::postgres_schema_url("admin_reconciliation_commit_failure").await;
let pool = sqlx::PgPool::connect(&database_url).await.unwrap();
MigrationAuthority::apply(&pool).await.unwrap();
let registry = PostgresRegistry::connect(&database_url).await.unwrap();
let root = TestRoot::new();
let store = ArtifactStore::open(&root.0).unwrap();
let now = OffsetDateTime::now_utc();
let artifact = store
.put_registered(b"commit-failure physical absence\n")
.unwrap();
let outcome = registry
.claim_artifact_reconciliation(ClaimArtifactReconciliationRequest {
artifact: &artifact,
token: ArtifactClaimToken::generate(),
claimed_at: now - time::Duration::minutes(10),
lease_expires_at: now - time::Duration::minutes(5),
detached_grace: time::Duration::hours(24),
})
.await
.unwrap();
assert!(matches!(outcome, ArtifactClaimOutcome::Claimed(_)));
remove_registered_artifact(&store, &artifact);
sqlx::query(
"update artifact_blobs
set claim_expires_at = clock_timestamp() - interval '1 second'
where digest = $1",
)
.bind(artifact.artifact_ref().digest_hex())
.execute(registry.pool())
.await
.unwrap();
// A deferred constraint trigger fails the COMMIT that records
// `unavailable`, modelling a real failure after the physical unlink that
// removed the last bytes already succeeded. It lives only in this isolated
// test schema.
sqlx::raw_sql(
"create function crank_test_fail_unavailable() returns trigger language plpgsql as $$
begin
if new.storage_lifecycle = 'unavailable' then
raise exception 'injected finalize commit failure';
end if;
return new;
end $$;
create constraint trigger crank_test_fail_unavailable_guard
after update on artifact_blobs
deferrable initially deferred
for each row execute function crank_test_fail_unavailable();",
)
.execute(registry.pool())
.await
.unwrap();
let backend = PostgresReconciliationBackend::new(registry.clone(), Arc::new(store.clone()));
let mut coordinator = ReconciliationCoordinator::new(backend);
let failed = coordinator.tick().await.unwrap();
// A real commit failure must surface as retryable work, never as idle.
assert!(failed.retryable);
assert_eq!(failed.mutation_attempts, 1);
assert_eq!(
store
.reconciliation_presence(artifact.artifact_ref())
.unwrap(),
ReconciliationPresence::Absent
);
let retained = sqlx::query(
"select claim_token, claim_expires_at, storage_lifecycle
from artifact_blobs where digest = $1",
)
.bind(artifact.artifact_ref().digest_hex())
.fetch_one(registry.pool())
.await
.unwrap();
assert!(
retained
.try_get::<Option<String>, _>("claim_token")
.unwrap()
.is_some(),
"the failed commit must roll back the claim clear"
);
assert!(
retained
.try_get::<Option<time::OffsetDateTime>, _>("claim_expires_at")
.unwrap()
.is_some()
);
assert_eq!(
retained.get::<String, _>("storage_lifecycle"),
"available",
"the failed commit must roll back the unavailable transition"
);
// Removing the injected trigger and retrying a fresh coordinator clears the
// retained claim and records the confirmed absence as `unavailable`.
sqlx::raw_sql(
"drop trigger crank_test_fail_unavailable_guard on artifact_blobs;
drop function crank_test_fail_unavailable();",
)
.execute(registry.pool())
.await
.unwrap();
sqlx::query(
"update artifact_blobs
set claim_expires_at = clock_timestamp() - interval '1 second'
where digest = $1",
)
.bind(artifact.artifact_ref().digest_hex())
.execute(registry.pool())
.await
.unwrap();
let mut recovered_coordinator = ReconciliationCoordinator::new(
PostgresReconciliationBackend::new(registry.clone(), Arc::new(store.clone())),
);
let recovered = recovered_coordinator.tick().await.unwrap();
assert!(!recovered.retryable);
assert_eq!(
store
.reconciliation_presence(artifact.artifact_ref())
.unwrap(),
ReconciliationPresence::Absent
);
let cleared = sqlx::query(
"select claim_token, claim_expires_at, storage_lifecycle
from artifact_blobs where digest = $1",
)
.bind(artifact.artifact_ref().digest_hex())
.fetch_one(registry.pool())
.await
.unwrap();
assert_eq!(
cleared.try_get::<Option<String>, _>("claim_token").unwrap(),
None
);
assert_eq!(
cleared
.try_get::<Option<time::OffsetDateTime>, _>("claim_expires_at")
.unwrap(),
None
);
assert_eq!(cleared.get::<String, _>("storage_lifecycle"), "unavailable");
}
+109 -27
View File
@@ -4,64 +4,146 @@ use axum::{
middleware::Next,
response::Response,
};
use crank_observability::{RequestId, set_remote_trace_parent, with_request_correlation};
use crank_core::{CorrelationContext, RequestId, TraceContext};
use crank_metrics::ExemplarTraceId;
use crank_observability::with_request_correlation;
use tracing::{Instrument, info, info_span};
pub const REQUEST_ID_HEADER: HeaderName = HeaderName::from_static("x-request-id");
pub const TRACE_ID_HEADER: HeaderName = HeaderName::from_static("x-trace-id");
#[derive(Clone, Debug)]
pub struct RequestContext {
pub request_id: String,
pub correlation: CorrelationContext,
}
impl RequestContext {
pub fn request_id(&self) -> &str {
self.correlation.request_id().as_str()
}
pub fn trace_id(&self) -> &str {
self.correlation.trace_id().as_str()
}
}
pub async fn apply_request_context(mut request: Request, next: Next) -> Response {
let context = RequestContext {
request_id: RequestId::resolve_from_headers(request.headers()).into_string(),
};
let (request_id, remote_parent) = resolve_correlation(request.headers());
let method = request.method().clone();
let route = request
.extensions()
.get::<MatchedPath>()
.map_or("unmatched", MatchedPath::as_str)
.to_owned();
// Explicit root: a contextual span would inherit whatever OTel context happens to be
// current on this worker thread, and a telemetry-suppressed ambient context makes the
// span non-recording, so the request trace is silently dropped before the exporter.
// The inbound `traceparent` parent is installed right below on the span builder.
let span = info_span!(
target: "crank::trace",
parent: None,
"http.request",
request_id = %context.request_id,
request_id = %request_id,
trace_id = tracing::field::Empty,
);
set_remote_trace_parent(&span, request.headers());
if let Some(remote_parent) = remote_parent.as_ref() {
set_canonical_parent(&span, remote_parent);
}
let trace_context = crank_trace::trace_context_for_span(&span).unwrap_or_else(|| {
remote_parent
.as_ref()
.map_or_else(TraceContext::generate, TraceContext::continue_local)
});
span.record("trace_id", trace_context.trace_id().as_str());
let context = RequestContext {
correlation: CorrelationContext::new(request_id, trace_context),
};
request.extensions_mut().insert(context.clone());
with_request_correlation(context.request_id.clone(), async move {
let mut response = next.run(request).instrument(span).await;
info!(
name: "admin.request.completed",
request_id = %context.request_id,
method = %method,
route,
status = response.status().as_u16(),
"admin request completed"
);
if let Ok(value) = HeaderValue::from_str(&context.request_id) {
response.headers_mut().insert(REQUEST_ID_HEADER, value);
}
response
})
with_request_correlation(
context.correlation.request_id().to_string(),
context.correlation.trace_id().to_string(),
async move {
let mut response = next.run(request).instrument(span).await;
info!(
name: "admin.request.completed",
request_id = %context.correlation.request_id(),
trace_id = %context.correlation.trace_id(),
method = %method,
route,
status = response.status().as_u16(),
"admin request completed"
);
if let Ok(value) = HeaderValue::from_str(context.correlation.request_id().as_str()) {
response.headers_mut().insert(REQUEST_ID_HEADER, value);
}
if let Ok(value) = HeaderValue::from_str(context.correlation.trace_id().as_str()) {
response.headers_mut().insert(TRACE_ID_HEADER, value);
}
if context.correlation.trace_context().is_sampled()
&& let Some(exemplar) =
ExemplarTraceId::parse(context.correlation.trace_id().as_str())
{
response.extensions_mut().insert(exemplar);
}
response
},
)
.await
}
fn resolve_correlation(headers: &axum::http::HeaderMap) -> (RequestId, Option<TraceContext>) {
let _tracestate_accepted = one_auxiliary_header_within_budget(
headers,
"tracestate",
TraceContext::tracestate_within_budget,
);
let _baggage_accepted =
one_auxiliary_header_within_budget(headers, "baggage", TraceContext::baggage_within_budget);
let mut request_ids = headers.get_all(REQUEST_ID_HEADER).iter();
let request_id = request_ids.next().and_then(|value| value.to_str().ok());
let request_id = if request_ids.next().is_some() {
RequestId::generate()
} else {
RequestId::resolve(request_id)
};
let mut traceparents = headers.get_all("traceparent").iter();
let traceparent = traceparents.next().and_then(|value| value.to_str().ok());
let remote_parent = if traceparents.next().is_some() {
None
} else {
traceparent.and_then(|value| TraceContext::parse(value).ok())
};
(request_id, remote_parent)
}
fn one_auxiliary_header_within_budget(
headers: &axum::http::HeaderMap,
name: &'static str,
validate: fn(&str) -> bool,
) -> bool {
let mut values = headers.get_all(name).iter();
let value = values.next().and_then(|value| value.to_str().ok());
values.next().is_none() && value.is_some_and(validate)
}
fn set_canonical_parent(span: &tracing::Span, context: &TraceContext) {
crank_trace::set_parent_from_trace_context(span, context);
}
#[cfg(test)]
mod tests {
#[test]
fn accepts_visible_ascii_request_ids() {
assert!(crank_observability::RequestId::is_valid("req_test_123"));
assert!(crank_observability::RequestId::is_valid("trace-123/abc"));
assert!(crank_core::RequestId::is_valid("req_test_123"));
assert!(crank_core::RequestId::is_valid("trace-123/abc"));
}
#[test]
fn rejects_empty_or_control_request_ids() {
assert!(!crank_observability::RequestId::is_valid(""));
assert!(!crank_observability::RequestId::is_valid("bad value"));
assert!(!crank_observability::RequestId::is_valid("bad\nvalue"));
assert!(!crank_core::RequestId::is_valid(""));
assert!(!crank_core::RequestId::is_valid("bad value"));
assert!(!crank_core::RequestId::is_valid("bad\nvalue"));
}
}
+77 -8
View File
@@ -5,7 +5,10 @@ pub mod auth_profiles;
pub mod capabilities;
pub mod imports;
pub mod observability;
pub mod onboarding;
pub mod operations;
pub mod prompts;
pub mod resources;
pub mod secrets;
pub mod upstreams;
pub mod workspaces;
@@ -23,23 +26,89 @@ pub async fn health() -> Json<serde_json::Value> {
}
pub async fn readiness(State(state): State<AppState>) -> impl IntoResponse {
match state.service.readiness().await {
Ok(()) => (
let checks = state.service.readiness().await;
let postgres = if checks.postgres {
"ready"
} else {
"not_ready"
};
let artifact_storage = if checks.artifact_storage {
"ready"
} else {
"not_ready"
};
if checks.is_ready() {
(
StatusCode::OK,
Json(json!({
"service": "admin-api",
"status": "ready",
"checks": { "postgres": "ready" }
"checks": { "postgres": postgres, "artifact_storage": artifact_storage }
})),
),
Err(error) => (
)
} else {
(
StatusCode::SERVICE_UNAVAILABLE,
Json(json!({
"service": "admin-api",
"status": "not_ready",
"checks": { "postgres": "not_ready" },
"error": error.to_string()
"checks": { "postgres": postgres, "artifact_storage": artifact_storage }
})),
),
)
}
}
#[cfg(test)]
mod tests {
use crate::service::ReadinessChecks;
#[test]
fn readiness_checks_identify_a_postgres_failure() {
let checks = ReadinessChecks {
postgres: false,
artifact_storage: true,
};
assert!(!checks.is_ready());
assert_eq!(
if checks.postgres {
"ready"
} else {
"not_ready"
},
"not_ready"
);
assert_eq!(
if checks.artifact_storage {
"ready"
} else {
"not_ready"
},
"ready"
);
}
#[test]
fn readiness_checks_identify_an_artifact_storage_failure() {
let checks = ReadinessChecks {
postgres: true,
artifact_storage: false,
};
assert!(!checks.is_ready());
assert_eq!(
if checks.postgres {
"ready"
} else {
"not_ready"
},
"ready"
);
assert_eq!(
if checks.artifact_storage {
"ready"
} else {
"not_ready"
},
"not_ready"
);
}
}
+92 -5
View File
@@ -1,15 +1,19 @@
use axum::{
Json,
Extension, Json,
extract::{Path, State},
http::{HeaderMap, header},
response::IntoResponse,
};
use serde::Deserialize;
use serde_json::{Value, json};
use crate::{
auth::AuthenticatedSession,
error::ApiError,
request_context::RequestContext,
service::{
AgentCatalogPayload, AgentPayload, PlatformApiKeyPayload, PublishPayload,
ToolSearchPreviewPayload, UpdateAgentPayload,
AdminAuditContext, AgentCatalogPayload, AgentPayload, PlatformApiKeyPayload,
PublishPayload, ToolSearchPreviewPayload, UpdateAgentPayload,
},
state::AppState,
};
@@ -77,7 +81,7 @@ pub async fn create_agent(
pub async fn get_agent(
Path(path): Path<WorkspaceAgentPath>,
State(state): State<AppState>,
) -> Result<Json<Value>, ApiError> {
) -> Result<impl IntoResponse, ApiError> {
let agent = state
.service
.get_agent(
@@ -85,20 +89,30 @@ pub async fn get_agent(
&path.agent_id.as_str().into(),
)
.await?;
Ok(Json(json!(agent)))
let etag = crate::service::AdminService::agent_state_etag(&agent);
let mut headers = HeaderMap::new();
headers.insert(
header::ETAG,
header::HeaderValue::from_str(&etag)
.map_err(|_| ApiError::internal("invalid agent etag"))?,
);
Ok((headers, Json(json!(agent))))
}
pub async fn update_agent(
Path(path): Path<WorkspaceAgentPath>,
State(state): State<AppState>,
headers: HeaderMap,
Json(payload): Json<UpdateAgentPayload>,
) -> Result<Json<Value>, ApiError> {
let expected_state = require_agent_precondition(&state, &path, &headers).await?;
let updated = state
.service
.update_agent(
&path.workspace_id.as_str().into(),
&path.agent_id.as_str().into(),
payload,
Some(&expected_state),
)
.await?;
Ok(Json(json!(updated)))
@@ -107,12 +121,15 @@ pub async fn update_agent(
pub async fn delete_agent(
Path(path): Path<WorkspaceAgentPath>,
State(state): State<AppState>,
headers: HeaderMap,
) -> Result<Json<Value>, ApiError> {
let expected_state = require_agent_precondition(&state, &path, &headers).await?;
let deleted = state
.service
.delete_agent(
&path.workspace_id.as_str().into(),
&path.agent_id.as_str().into(),
Some(&expected_state),
)
.await?;
Ok(Json(json!(deleted)))
@@ -136,30 +153,79 @@ pub async fn get_agent_version(
pub async fn save_agent_bindings(
Path(path): Path<WorkspaceAgentPath>,
State(state): State<AppState>,
headers: HeaderMap,
Json(payload): Json<AgentCatalogPayload>,
) -> Result<Json<Value>, ApiError> {
let expected_state = require_agent_precondition(&state, &path, &headers).await?;
let record = state
.service
.save_agent_bindings(
&path.workspace_id.as_str().into(),
&path.agent_id.as_str().into(),
payload,
Some(&expected_state),
)
.await?;
Ok(Json(json!(record)))
}
async fn require_agent_precondition(
state: &AppState,
path: &WorkspaceAgentPath,
headers: &HeaderMap,
) -> Result<crank_registry::AgentStateExpectation, ApiError> {
let agent = state
.service
.get_agent(
&path.workspace_id.as_str().into(),
&path.agent_id.as_str().into(),
)
.await?;
let expected = crate::service::AdminService::agent_state_etag(&agent);
let provided = headers
.get(header::IF_MATCH)
.and_then(|value| value.to_str().ok())
.ok_or_else(|| {
ApiError::precondition_required_with_context(
"If-Match is required for Agent mutation",
json!({
"error_code": "agent_precondition_required",
"current_version": agent.current_draft_version,
"latest_published_version": agent.latest_published_version,
"catalog_revision": agent.catalog_revision,
"recovery": "reload"
}),
)
})?;
if provided != expected {
return Err(ApiError::conflict_with_context(
"Agent state changed; reload before retrying",
json!({
"error_code": "agent_stale_revision",
"current_version": agent.current_draft_version,
"latest_published_version": agent.latest_published_version,
"catalog_revision": agent.catalog_revision,
"recovery": "reload"
}),
));
}
crate::service::AdminService::agent_state_expectation(&agent)
}
pub async fn publish_agent(
Path(path): Path<WorkspaceAgentPath>,
State(state): State<AppState>,
headers: HeaderMap,
Json(payload): Json<PublishPayload>,
) -> Result<Json<Value>, ApiError> {
let expected_state = require_agent_precondition(&state, &path, &headers).await?;
let published = state
.service
.publish_agent(
&path.workspace_id.as_str().into(),
&path.agent_id.as_str().into(),
payload.version,
Some(&expected_state),
)
.await?;
Ok(Json(json!(published)))
@@ -168,12 +234,15 @@ pub async fn publish_agent(
pub async fn unpublish_agent(
Path(path): Path<WorkspaceAgentPath>,
State(state): State<AppState>,
headers: HeaderMap,
) -> Result<Json<Value>, ApiError> {
let expected_state = require_agent_precondition(&state, &path, &headers).await?;
let updated = state
.service
.unpublish_agent(
&path.workspace_id.as_str().into(),
&path.agent_id.as_str().into(),
Some(&expected_state),
)
.await?;
Ok(Json(json!(updated)))
@@ -182,12 +251,15 @@ pub async fn unpublish_agent(
pub async fn archive_agent(
Path(path): Path<WorkspaceAgentPath>,
State(state): State<AppState>,
headers: HeaderMap,
) -> Result<Json<Value>, ApiError> {
let expected_state = require_agent_precondition(&state, &path, &headers).await?;
let updated = state
.service
.archive_agent(
&path.workspace_id.as_str().into(),
&path.agent_id.as_str().into(),
Some(&expected_state),
)
.await?;
Ok(Json(json!(updated)))
@@ -210,14 +282,19 @@ pub async fn list_agent_platform_api_keys(
pub async fn create_agent_platform_api_key(
Path(path): Path<WorkspaceAgentPath>,
State(state): State<AppState>,
Extension(session): Extension<AuthenticatedSession>,
Extension(request_context): Extension<RequestContext>,
Json(payload): Json<PlatformApiKeyPayload>,
) -> Result<Json<Value>, ApiError> {
let audit_context =
AdminAuditContext::from_session_and_correlation(&session, &request_context.correlation);
let created = state
.service
.create_agent_platform_api_key(
&path.workspace_id.as_str().into(),
&path.agent_id.as_str().into(),
payload,
Some(&audit_context),
)
.await?;
Ok(Json(json!(created)))
@@ -226,13 +303,18 @@ pub async fn create_agent_platform_api_key(
pub async fn revoke_agent_platform_api_key(
Path(path): Path<WorkspaceAgentPlatformApiKeyPath>,
State(state): State<AppState>,
Extension(session): Extension<AuthenticatedSession>,
Extension(request_context): Extension<RequestContext>,
) -> Result<axum::http::StatusCode, ApiError> {
let audit_context =
AdminAuditContext::from_session_and_correlation(&session, &request_context.correlation);
state
.service
.revoke_agent_platform_api_key(
&path.workspace_id.as_str().into(),
&path.agent_id.as_str().into(),
&path.key_id.as_str().into(),
Some(&audit_context),
)
.await?;
Ok(axum::http::StatusCode::NO_CONTENT)
@@ -241,13 +323,18 @@ pub async fn revoke_agent_platform_api_key(
pub async fn delete_agent_platform_api_key(
Path(path): Path<WorkspaceAgentPlatformApiKeyPath>,
State(state): State<AppState>,
Extension(session): Extension<AuthenticatedSession>,
Extension(request_context): Extension<RequestContext>,
) -> Result<axum::http::StatusCode, ApiError> {
let audit_context =
AdminAuditContext::from_session_and_correlation(&session, &request_context.correlation);
state
.service
.delete_agent_platform_api_key(
&path.workspace_id.as_str().into(),
&path.agent_id.as_str().into(),
&path.key_id.as_str().into(),
Some(&audit_context),
)
.await?;
Ok(axum::http::StatusCode::NO_CONTENT)
+53 -2
View File
@@ -5,18 +5,52 @@ use serde_json::json;
use crate::{
auth::{AuthenticatedSession, cleared_session_cookie, extract_session_token, session_cookie},
error::ApiError,
rate_limit::ClientIdentityBucket,
service::{
ChangePasswordPayload, LoginPayload, UpdateCurrentWorkspacePayload, UpdateProfilePayload,
ChangePasswordPayload, CompleteBootstrapPayload, LoginPayload,
UpdateCurrentWorkspacePayload, UpdateProfilePayload,
},
state::AppState,
};
pub async fn bootstrap_status(
State(state): State<AppState>,
) -> Result<Json<serde_json::Value>, ApiError> {
Ok(Json(serde_json::json!(
state.service.bootstrap_status().await?
)))
}
pub async fn complete_bootstrap(
State(state): State<AppState>,
jar: CookieJar,
Json(payload): Json<CompleteBootstrapPayload>,
) -> Result<impl IntoResponse, ApiError> {
let (session_data, session) = state.service.complete_bootstrap(payload).await?;
let cookie_value = format!(
"{}.{}",
session_data.session_id.as_str(),
session_data.value
);
let jar = jar.add(session_cookie(state.service.auth_settings(), &cookie_value));
Ok((jar, Json(serde_json::json!(session))))
}
pub async fn login(
State(state): State<AppState>,
jar: CookieJar,
client_bucket: Option<Extension<ClientIdentityBucket>>,
Json(payload): Json<LoginPayload>,
) -> Result<impl IntoResponse, ApiError> {
let (session_data, session) = state.service.login(payload).await?;
let client_bucket = client_bucket
.as_ref()
.map(|Extension(bucket)| bucket.0.as_str())
.unwrap_or("anonymous:/api/auth/login");
let (session_data, session) = state
.service
.login_with_client_bucket(payload, client_bucket)
.await?;
let cookie_value = format!(
"{}.{}",
session_data.session_id.as_str(),
@@ -54,6 +88,22 @@ pub async fn get_session(
Ok(Json(serde_json::json!(session)))
}
pub async fn refresh_session_csrf(
State(state): State<AppState>,
jar: CookieJar,
) -> Result<Json<serde_json::Value>, ApiError> {
let (session_id, session_value) = extract_session_token(&jar)
.ok_or_else(|| ApiError::unauthorized("authentication required"))?;
state
.service
.get_session(&session_id, &session_value)
.await?
.ok_or_else(|| ApiError::unauthorized("session is invalid or expired"))?;
let csrf_token = state.service.rotate_session_csrf_token(&session_id).await?;
Ok(Json(json!({ "csrf_token": csrf_token })))
}
pub async fn get_profile(
Extension(session): Extension<AuthenticatedSession>,
) -> Result<Json<serde_json::Value>, ApiError> {
@@ -73,6 +123,7 @@ pub async fn update_profile(
.service
.update_profile(
&session.user.id,
&session.session_id,
session.current_workspace_id.as_ref(),
payload,
)
+17 -3
View File
@@ -1,11 +1,17 @@
use axum::{
Json,
Extension, Json,
extract::{Path, State},
};
use serde::Deserialize;
use serde_json::{Value, json};
use crate::{error::ApiError, service::AuthProfilePayload, state::AppState};
use crate::{
auth::AuthenticatedSession,
error::ApiError,
request_context::RequestContext,
service::{AdminAuditContext, AuthProfilePayload},
state::AppState,
};
#[derive(Deserialize)]
pub struct WorkspacePath {
@@ -32,11 +38,19 @@ pub async fn list_auth_profiles(
pub async fn create_auth_profile(
Path(path): Path<WorkspacePath>,
State(state): State<AppState>,
Extension(session): Extension<AuthenticatedSession>,
Extension(request_context): Extension<RequestContext>,
Json(payload): Json<AuthProfilePayload>,
) -> Result<Json<Value>, ApiError> {
let audit_context =
AdminAuditContext::from_session_and_correlation(&session, &request_context.correlation);
let profile = state
.service
.create_auth_profile(&path.workspace_id.as_str().into(), payload)
.create_auth_profile(
&path.workspace_id.as_str().into(),
payload,
Some(&audit_context),
)
.await?;
Ok(Json(json!(profile)))
}
+247 -5
View File
@@ -1,13 +1,19 @@
use axum::{
Json,
extract::{Path, State},
extract::{Multipart, Path, State, multipart::MultipartRejection},
http::HeaderMap,
response::IntoResponse,
};
use crank_artifacts::MAX_ARTIFACT_BYTES;
use serde::Deserialize;
use serde_json::{Value, json};
use crate::{
error::ApiError,
service::{OpenApiImportCreatePayload, OpenApiImportPreviewPayload},
service::{
OpenApiImportCreatePayload, OpenApiImportPlanApplyPayload, OpenApiImportPlanCreatePayload,
OpenApiUpload, OpenApiUploadLocale,
},
state::AppState,
};
@@ -22,30 +28,266 @@ pub struct WorkspaceImportPath {
pub job_id: String,
}
#[derive(Deserialize)]
pub struct WorkspacePlanPath {
pub workspace_id: String,
pub plan_id: String,
}
pub async fn preview_openapi_import(
Path(path): Path<WorkspacePath>,
State(state): State<AppState>,
Json(payload): Json<OpenApiImportPreviewPayload>,
headers: HeaderMap,
multipart: Result<Multipart, MultipartRejection>,
) -> Result<Json<Value>, ApiError> {
let locale = openapi_upload_locale(&headers);
let upload = parse_openapi_upload(
multipart.map_err(|rejection| multipart_rejection(locale, rejection))?,
locale,
)
.await?;
let preview = state
.service
.preview_openapi_import(&path.workspace_id.as_str().into(), payload)
.preview_openapi_import(&path.workspace_id.as_str().into(), upload)
.await?;
Ok(Json(json!(preview)))
}
fn multipart_rejection(locale: OpenApiUploadLocale, rejection: MultipartRejection) -> ApiError {
if rejection.into_response().status() == axum::http::StatusCode::PAYLOAD_TOO_LARGE {
ApiError::openapi_upload(locale, "file_too_large")
} else {
ApiError::openapi_upload(locale, "malformed_multipart")
}
}
async fn parse_openapi_upload(
mut multipart: Multipart,
locale: OpenApiUploadLocale,
) -> Result<OpenApiUpload, ApiError> {
let mut upload = None;
while let Some(field) = multipart
.next_field()
.await
.map_err(|_| ApiError::openapi_upload(locale, "malformed_multipart"))?
{
if upload.is_some() || field.name() != Some("file") {
return Err(ApiError::openapi_upload(locale, "malformed_multipart"));
}
let filename = field
.file_name()
.ok_or_else(|| ApiError::openapi_upload(locale, "invalid_filename"))?;
let mime_type = field
.content_type()
// Axum exposes the raw header value here. Persist only the MIME
// essence so parameters and case cannot make route and service
// validation disagree.
.map(|mime| {
mime.split(';')
.next()
.unwrap_or_default()
.trim()
.to_ascii_lowercase()
})
.ok_or_else(|| ApiError::openapi_upload(locale, "invalid_media_type"))?;
if !valid_upload_type(filename, &mime_type) {
return Err(ApiError::openapi_upload(locale, "invalid_media_type"));
}
let mut bytes = Vec::with_capacity(8 * 1024);
let mut field = field;
while let Some(chunk) = field
.chunk()
.await
.map_err(|_| ApiError::openapi_upload(locale, "malformed_multipart"))?
{
if bytes.len().saturating_add(chunk.len()) > MAX_ARTIFACT_BYTES {
return Err(ApiError::openapi_upload(locale, "file_too_large"));
}
bytes.extend_from_slice(&chunk);
}
if bytes.is_empty() {
return Err(ApiError::openapi_upload(locale, "empty_file"));
}
if std::str::from_utf8(&bytes).is_err() {
return Err(ApiError::openapi_upload(locale, "invalid_utf8"));
}
upload = Some(OpenApiUpload {
bytes,
mime_type,
locale,
});
}
upload.ok_or_else(|| ApiError::openapi_upload(locale, "missing_file"))
}
fn valid_upload_type(filename: &str, mime_type: &str) -> bool {
let filename = filename.to_ascii_lowercase();
let mime_type = mime_type.to_ascii_lowercase();
match filename.rsplit_once('.') {
Some((_, "yaml" | "yml")) => matches!(
mime_type.as_str(),
"application/yaml"
| "application/x-yaml"
| "text/yaml"
| "text/x-yaml"
| "application/octet-stream"
),
Some((_, "json")) => matches!(
mime_type.as_str(),
"application/json" | "application/openapi+json" | "application/octet-stream"
),
_ => false,
}
}
fn openapi_upload_locale(headers: &HeaderMap) -> OpenApiUploadLocale {
let Some(value) = headers
.get("accept-language")
.and_then(|value| value.to_str().ok())
else {
return OpenApiUploadLocale::En;
};
// RFC 9110: highest q wins; ties preserve the header's order. Only the
// locales served by this endpoint participate in negotiation.
let mut preferred = (0_u16, usize::MAX, OpenApiUploadLocale::En);
for (index, range) in value.split(',').enumerate() {
let mut parts = range.split(';');
let language = parts.next().unwrap_or_default().trim();
let locale = if language.eq_ignore_ascii_case("ru")
|| language.to_ascii_lowercase().starts_with("ru-")
{
Some(OpenApiUploadLocale::Ru)
} else if language.eq_ignore_ascii_case("en")
|| language.to_ascii_lowercase().starts_with("en-")
|| language == "*"
{
Some(OpenApiUploadLocale::En)
} else {
None
};
let Some(locale) = locale else { continue };
let quality = match parts
.filter_map(|parameter| {
let (name, value) = parameter.trim().split_once('=')?;
name.eq_ignore_ascii_case("q").then_some(value.trim())
})
.next()
{
None => Some(1_000_u16),
Some(value) => value
.parse::<f32>()
.ok()
.filter(|quality| (0.0..=1.0).contains(quality) && *quality > 0.0)
.map(|quality| (quality * 1_000.0).round() as u16),
};
let Some(quality) = quality else { continue };
if quality > preferred.0 || (quality == preferred.0 && index < preferred.1) {
preferred = (quality, index, locale);
}
}
preferred.2
}
pub async fn create_openapi_import(
Path(path): Path<WorkspaceImportPath>,
State(state): State<AppState>,
headers: HeaderMap,
Json(payload): Json<OpenApiImportCreatePayload>,
) -> Result<Json<Value>, ApiError> {
let imported = state
.service
.create_openapi_import(
.create_openapi_import_with_locale(
&path.workspace_id.as_str().into(),
&path.job_id.as_str().into(),
payload,
openapi_upload_locale(&headers),
)
.await?;
Ok(Json(json!(imported)))
}
pub async fn create_openapi_import_plan(
Path(path): Path<WorkspaceImportPath>,
State(state): State<AppState>,
headers: HeaderMap,
Json(payload): Json<OpenApiImportPlanCreatePayload>,
) -> Result<Json<Value>, ApiError> {
let plan = state
.service
.create_openapi_import_plan_with_locale(
&path.workspace_id.as_str().into(),
&path.job_id.as_str().into(),
payload,
openapi_upload_locale(&headers),
)
.await?;
Ok(Json(json!(plan)))
}
pub async fn revalidate_openapi_import_plan(
Path(path): Path<WorkspacePlanPath>,
State(state): State<AppState>,
headers: HeaderMap,
) -> Result<Json<Value>, ApiError> {
let plan = state
.service
.revalidate_openapi_import_plan_with_locale(
&path.workspace_id.as_str().into(),
&path.plan_id.as_str().into(),
openapi_upload_locale(&headers),
)
.await?;
Ok(Json(json!(plan)))
}
pub async fn apply_openapi_import_plan(
Path(path): Path<WorkspacePlanPath>,
State(state): State<AppState>,
headers: HeaderMap,
Json(payload): Json<OpenApiImportPlanApplyPayload>,
) -> Result<impl IntoResponse, ApiError> {
let report = state
.service
.apply_openapi_import_plan_with_locale(
&path.workspace_id.as_str().into(),
&path.plan_id.as_str().into(),
payload,
openapi_upload_locale(&headers),
)
.await?;
let status = if report.status == crank_registry::ImportPlanApplyStatus::Rejected {
axum::http::StatusCode::CONFLICT
} else {
axum::http::StatusCode::OK
};
Ok((
status,
Json(
serde_json::to_value(report)
.map_err(|_| ApiError::internal("could not serialize import plan report"))?,
),
))
}
#[cfg(test)]
mod tests {
use axum::http::{HeaderMap, HeaderValue};
use super::*;
fn locale(value: &str) -> OpenApiUploadLocale {
let mut headers = HeaderMap::new();
headers.insert("accept-language", HeaderValue::from_str(value).unwrap());
openapi_upload_locale(&headers)
}
#[test]
fn accept_language_honours_quality_zero_and_header_order() {
assert_eq!(locale("ru;q=0, en;q=0.5"), OpenApiUploadLocale::En);
assert_eq!(locale("en;q=0.5, ru;q=0.5"), OpenApiUploadLocale::En);
assert_eq!(locale("ru-RU;q=0.9, en;q=1"), OpenApiUploadLocale::En);
assert_eq!(locale("ru, en;q=0.5"), OpenApiUploadLocale::Ru);
assert_eq!(locale("ru;q=bad, en;q=0.5"), OpenApiUploadLocale::En);
}
}
+81 -2
View File
@@ -1,13 +1,18 @@
use axum::{
Json,
extract::{Path, Query, State},
http::{
HeaderValue, StatusCode,
header::{CONTENT_DISPOSITION, CONTENT_TYPE},
},
response::{IntoResponse, Response},
};
use serde_json::{Value, json};
use crate::{
error::ApiError,
routes::access::WorkspacePath,
service::{ApprovalsQuery, LogsQuery, UsageRequestQuery},
service::{ApprovalDecisionPayload, ApprovalsQuery, LogsQuery, UsageRequestQuery},
state::AppState,
};
@@ -44,7 +49,49 @@ pub async fn list_logs(
.service
.list_logs(&path.workspace_id.as_str().into(), query)
.await?;
Ok(Json(json!({ "items": items })))
Ok(Json(json!(items)))
}
pub async fn export_logs_csv(
Path(path): Path<WorkspacePath>,
Query(query): Query<LogsQuery>,
State(state): State<AppState>,
) -> Result<Response, ApiError> {
let csv = state
.service
.export_logs_csv(&path.workspace_id.as_str().into(), query)
.await?;
let mut response = (StatusCode::OK, csv).into_response();
response.headers_mut().insert(
CONTENT_TYPE,
HeaderValue::from_static("text/csv; charset=utf-8"),
);
response.headers_mut().insert(
CONTENT_DISPOSITION,
HeaderValue::from_static("attachment; filename=\"crank-invocation-history.csv\""),
);
Ok(response)
}
pub async fn export_usage_csv(
Path(path): Path<WorkspacePath>,
Query(query): Query<UsageRequestQuery>,
State(state): State<AppState>,
) -> Result<Response, ApiError> {
let csv = state
.service
.export_usage_csv(&path.workspace_id.as_str().into(), query)
.await?;
let mut response = (StatusCode::OK, csv).into_response();
response.headers_mut().insert(
CONTENT_TYPE,
HeaderValue::from_static("text/csv; charset=utf-8"),
);
response.headers_mut().insert(
CONTENT_DISPOSITION,
HeaderValue::from_static("attachment; filename=\"crank-usage.csv\""),
);
Ok(response)
}
pub async fn get_log(
@@ -87,6 +134,38 @@ pub async fn get_approval(
Ok(Json(json!(item)))
}
pub async fn approve_approval(
Path(path): Path<WorkspaceApprovalPath>,
State(state): State<AppState>,
Json(payload): Json<ApprovalDecisionPayload>,
) -> Result<Json<Value>, ApiError> {
let item = state
.service
.approve_approval(
&path.workspace_id.as_str().into(),
&path.approval_id.as_str().into(),
payload,
)
.await?;
Ok(Json(json!(item)))
}
pub async fn deny_approval(
Path(path): Path<WorkspaceApprovalPath>,
State(state): State<AppState>,
Json(payload): Json<ApprovalDecisionPayload>,
) -> Result<Json<Value>, ApiError> {
let item = state
.service
.deny_approval(
&path.workspace_id.as_str().into(),
&path.approval_id.as_str().into(),
payload,
)
.await?;
Ok(Json(json!(item)))
}
pub async fn get_usage(
Path(path): Path<WorkspacePath>,
Query(query): Query<UsageRequestQuery>,
+87
View File
@@ -0,0 +1,87 @@
use axum::{
Json,
extract::{Path, State},
};
use crate::{
error::ApiError,
service::{
OnboardingEventPayload, OnboardingEventResponse, OnboardingResponse,
RegisterOnboardingImportContextPayload, RegisterOnboardingImportContextResponse,
ResetOnboardingSelectionPayload, ResetOnboardingSelectionResponse,
SelectOnboardingImportDraftPayload, SelectOnboardingImportDraftResponse,
},
state::AppState,
};
#[derive(serde::Deserialize)]
pub struct WorkspaceOnboardingPath {
pub workspace_id: String,
}
pub async fn reset_onboarding_selection(
Path(path): Path<WorkspaceOnboardingPath>,
State(state): State<AppState>,
Json(payload): Json<ResetOnboardingSelectionPayload>,
) -> Result<Json<ResetOnboardingSelectionResponse>, ApiError> {
Ok(Json(
state
.service
.reset_onboarding_selection(
&path.workspace_id.as_str().into(),
payload.expected_revision,
)
.await?,
))
}
pub async fn get_onboarding(
Path(path): Path<WorkspaceOnboardingPath>,
State(state): State<AppState>,
) -> Result<Json<OnboardingResponse>, ApiError> {
Ok(Json(
state
.service
.get_onboarding(&path.workspace_id.as_str().into())
.await?,
))
}
pub async fn record_onboarding_event(
Path(path): Path<WorkspaceOnboardingPath>,
State(state): State<AppState>,
Json(payload): Json<OnboardingEventPayload>,
) -> Result<Json<OnboardingEventResponse>, ApiError> {
Ok(Json(
state
.service
.record_onboarding_event(&path.workspace_id.as_str().into(), payload)
.await?,
))
}
pub async fn register_onboarding_import_context(
Path(path): Path<WorkspaceOnboardingPath>,
State(state): State<AppState>,
Json(payload): Json<RegisterOnboardingImportContextPayload>,
) -> Result<Json<RegisterOnboardingImportContextResponse>, ApiError> {
Ok(Json(
state
.service
.register_onboarding_import_context(&path.workspace_id.as_str().into(), payload)
.await?,
))
}
pub async fn select_onboarding_import_draft(
Path(path): Path<WorkspaceOnboardingPath>,
State(state): State<AppState>,
Json(payload): Json<SelectOnboardingImportDraftPayload>,
) -> Result<Json<SelectOnboardingImportDraftResponse>, ApiError> {
Ok(Json(
state
.service
.select_onboarding_import_draft(&path.workspace_id.as_str().into(), payload)
.await?,
))
}
+103 -8
View File
@@ -1,9 +1,10 @@
use axum::{
Json,
extract::{Extension, Path, Query, State},
extract::{Extension, Path, Query, State, rejection::StringRejection},
http::{HeaderMap, StatusCode, header},
response::IntoResponse,
};
use crank_registry::OperationStateExpectation;
use serde::Deserialize;
use serde_json::{Value, json};
@@ -79,7 +80,7 @@ pub async fn analyze_operation_quality(
pub async fn get_operation(
Path(path): Path<WorkspaceOperationPath>,
State(state): State<AppState>,
) -> Result<Json<Value>, ApiError> {
) -> Result<impl IntoResponse, ApiError> {
let operation = state
.service
.get_operation(
@@ -87,20 +88,30 @@ pub async fn get_operation(
&path.operation_id.as_str().into(),
)
.await?;
Ok(Json(json!(operation)))
let etag = crate::service::AdminService::operation_state_etag(&operation);
let mut headers = HeaderMap::new();
headers.insert(
header::ETAG,
header::HeaderValue::from_str(&etag)
.map_err(|_| ApiError::internal("invalid operation etag"))?,
);
Ok((headers, Json(json!(operation))))
}
pub async fn update_operation(
Path(path): Path<WorkspaceOperationPath>,
State(state): State<AppState>,
headers: HeaderMap,
Json(payload): Json<UpdateOperationPayload>,
) -> Result<Json<Value>, ApiError> {
let expected = require_operation_precondition(&state, &path, &headers).await?;
let result = state
.service
.update_operation(
&path.workspace_id.as_str().into(),
&path.operation_id.as_str().into(),
payload,
&expected,
)
.await?;
Ok(Json(json!(result)))
@@ -109,12 +120,15 @@ pub async fn update_operation(
pub async fn delete_operation(
Path(path): Path<WorkspaceOperationPath>,
State(state): State<AppState>,
headers: HeaderMap,
) -> Result<Json<Value>, ApiError> {
let expected = require_operation_precondition(&state, &path, &headers).await?;
let result = state
.service
.delete_operation(
&path.workspace_id.as_str().into(),
&path.operation_id.as_str().into(),
&expected,
)
.await?;
Ok(Json(json!(result)))
@@ -123,7 +137,7 @@ pub async fn delete_operation(
pub async fn get_operation_version(
Path(path): Path<WorkspaceOperationVersionPath>,
State(state): State<AppState>,
) -> Result<Json<Value>, ApiError> {
) -> Result<impl IntoResponse, ApiError> {
let version = state
.service
.get_operation_version(
@@ -132,20 +146,30 @@ pub async fn get_operation_version(
path.version,
)
.await?;
Ok(Json(json!(version)))
let etag = crate::service::AdminService::operation_version_etag(&version)?;
let mut headers = HeaderMap::new();
headers.insert(
header::ETAG,
header::HeaderValue::from_str(&etag)
.map_err(|_| ApiError::internal("invalid operation version etag"))?,
);
Ok((headers, Json(json!(version))))
}
pub async fn create_version(
Path(path): Path<WorkspaceOperationPath>,
State(state): State<AppState>,
headers: HeaderMap,
Json(payload): Json<NewVersionPayload>,
) -> Result<Json<Value>, ApiError> {
let expected = require_operation_precondition(&state, &path, &headers).await?;
let created = state
.service
.create_version(
&path.workspace_id.as_str().into(),
&path.operation_id.as_str().into(),
payload,
&expected,
)
.await?;
Ok(Json(json!(created)))
@@ -154,14 +178,17 @@ pub async fn create_version(
pub async fn publish_operation(
Path(path): Path<WorkspaceOperationPath>,
State(state): State<AppState>,
headers: HeaderMap,
Json(payload): Json<PublishPayload>,
) -> Result<Json<Value>, ApiError> {
let expected = require_operation_precondition(&state, &path, &headers).await?;
let published = state
.service
.publish_operation(
&path.workspace_id.as_str().into(),
&path.operation_id.as_str().into(),
payload.version,
&expected,
)
.await?;
Ok(Json(json!(published)))
@@ -170,17 +197,63 @@ pub async fn publish_operation(
pub async fn archive_operation(
Path(path): Path<WorkspaceOperationPath>,
State(state): State<AppState>,
headers: HeaderMap,
) -> Result<Json<Value>, ApiError> {
let expected = require_operation_precondition(&state, &path, &headers).await?;
let archived = state
.service
.archive_operation(
&path.workspace_id.as_str().into(),
&path.operation_id.as_str().into(),
&expected,
)
.await?;
Ok(Json(json!(archived)))
}
async fn require_operation_precondition(
state: &AppState,
path: &WorkspaceOperationPath,
headers: &HeaderMap,
) -> Result<OperationStateExpectation, ApiError> {
let detail = state
.service
.get_operation(
&path.workspace_id.as_str().into(),
&path.operation_id.as_str().into(),
)
.await?;
let expected = crate::service::AdminService::operation_state_etag(&detail);
let provided = headers
.get(header::IF_MATCH)
.and_then(|value| value.to_str().ok())
.ok_or_else(|| {
ApiError::precondition_required_with_context(
"If-Match is required for Operation mutation",
json!({
"error_code": "operation_precondition_required",
"current_version": detail.current_draft_version,
"recovery": "reload"
}),
)
})?;
if provided != expected {
return Err(ApiError::conflict_with_context(
"Operation state changed; reload before retrying",
json!({
"error_code": "operation_stale_version",
"current_version": detail.current_draft_version,
"recovery": "reload"
}),
));
}
Ok(OperationStateExpectation {
current_draft_version: detail.current_draft_version,
status: detail.status,
latest_published_version: detail.latest_published_version,
})
}
pub async fn run_test(
Path(path): Path<WorkspaceOperationPath>,
State(state): State<AppState>,
@@ -193,7 +266,7 @@ pub async fn run_test(
&path.workspace_id.as_str().into(),
&path.operation_id.as_str().into(),
payload,
&request_context.request_id,
&request_context.correlation,
)
.await?;
Ok(Json(json!(result)))
@@ -285,11 +358,33 @@ pub async fn import_operation(
Path(path): Path<WorkspacePath>,
Query(query): Query<ImportQuery>,
State(state): State<AppState>,
body: String,
headers: HeaderMap,
body: Result<String, StringRejection>,
) -> Result<Json<Value>, ApiError> {
let body = body.map_err(|rejection| match rejection {
StringRejection::InvalidUtf8(_) => ApiError::unprocessable_with_context(
"operation yaml is not valid UTF-8",
json!({ "error_code": "operation_yaml_invalid" }),
),
StringRejection::FailedToBufferBody(_) => ApiError::payload_too_large_with_context(
"operation yaml exceeds the 256 KiB limit",
json!({ "error_code": "operation_yaml_too_large" }),
),
_ => ApiError::unprocessable_with_context(
"operation yaml body is invalid",
json!({ "error_code": "operation_yaml_invalid" }),
),
})?;
let imported = state
.service
.import_operation(&path.workspace_id.as_str().into(), query, &body)
.import_operation(
&path.workspace_id.as_str().into(),
query,
&body,
headers
.get(header::IF_MATCH)
.and_then(|value| value.to_str().ok()),
)
.await?;
Ok(Json(json!(imported)))
}
+214
View File
@@ -0,0 +1,214 @@
use axum::{
Extension, Json,
extract::{Path, Query, State, rejection::JsonRejection},
http::{HeaderMap, HeaderValue, StatusCode, header},
response::{IntoResponse, Response},
};
use crank_core::{WorkspaceId, ids::PromptId};
use serde::Deserialize;
use serde_json::Value;
use crate::{error::ApiError, request_context::RequestContext, state::AppState};
#[derive(Deserialize)]
pub struct WorkspacePath {
workspace_id: String,
}
#[derive(Deserialize)]
pub struct PromptPath {
workspace_id: String,
prompt_id: String,
}
#[derive(Deserialize)]
pub struct PromptVersionPath {
workspace_id: String,
prompt_id: String,
version: u32,
}
#[derive(Deserialize)]
pub struct PromptQuery {
version: Option<u32>,
}
pub async fn list_prompt_drafts(
Path(path): Path<WorkspacePath>,
State(state): State<AppState>,
) -> Result<Json<Value>, ApiError> {
Ok(Json(
state
.service
.list_prompt_drafts(&WorkspaceId::new(path.workspace_id))
.await?,
))
}
pub async fn create_prompt_draft(
Path(path): Path<WorkspacePath>,
State(state): State<AppState>,
Json(body): Json<Value>,
) -> Result<(StatusCode, Json<Value>), ApiError> {
let prompt = state
.service
.create_prompt_draft(&WorkspaceId::new(path.workspace_id), body)
.await?;
Ok((StatusCode::CREATED, Json(prompt)))
}
pub async fn get_prompt_draft(
Path(path): Path<PromptPath>,
Query(query): Query<PromptQuery>,
State(state): State<AppState>,
) -> Result<Json<Value>, ApiError> {
Ok(Json(
state
.service
.get_prompt_draft(
&WorkspaceId::new(path.workspace_id),
&PromptId::new(path.prompt_id),
query.version,
)
.await?,
))
}
pub async fn prompt_action(
Path(path): Path<PromptPath>,
State(state): State<AppState>,
Json(body): Json<Value>,
) -> Result<Json<Value>, ApiError> {
Ok(Json(
state
.service
.prompt_action(
&WorkspaceId::new(path.workspace_id),
&PromptId::new(path.prompt_id),
body,
)
.await?,
))
}
pub async fn update_prompt_draft(
Path(path): Path<PromptPath>,
State(state): State<AppState>,
Json(body): Json<Value>,
) -> Result<Json<Value>, ApiError> {
Ok(Json(
state
.service
.update_prompt_draft(
&WorkspaceId::new(path.workspace_id),
&PromptId::new(path.prompt_id),
body,
)
.await?,
))
}
pub async fn preview_prompt_draft(
Path(path): Path<PromptPath>,
State(state): State<AppState>,
Extension(request_context): Extension<RequestContext>,
Json(body): Json<Value>,
) -> Result<Json<Value>, ApiError> {
Ok(Json(
state
.service
.preview_prompt_draft(
&WorkspaceId::new(path.workspace_id),
&PromptId::new(path.prompt_id),
body,
&request_context.correlation,
)
.await?,
))
}
/// Story 3.15: экспорт текущего Draft. Ответ всегда JSON + `attachment` + `nosniff`,
/// поэтому переносимый definition никогда не рендерится как документ браузера.
pub async fn export_prompt_draft(
Path(path): Path<PromptPath>,
State(state): State<AppState>,
) -> Result<Response, ApiError> {
let (envelope, filename) = state
.service
.export_prompt_draft_envelope(
&WorkspaceId::new(path.workspace_id),
&PromptId::new(path.prompt_id),
)
.await?;
portable_response(&envelope, &filename)
}
/// Story 3.15: экспорт точной Published Version (immutable snapshot этой ревизии).
pub async fn export_prompt_version(
Path(path): Path<PromptVersionPath>,
State(state): State<AppState>,
) -> Result<Response, ApiError> {
let (envelope, filename) = state
.service
.export_published_prompt_envelope(
&WorkspaceId::new(path.workspace_id),
&PromptId::new(path.prompt_id),
path.version,
)
.await?;
portable_response(&envelope, &filename)
}
/// Story 3.15: импорт envelope как **нового** Draft текущего владельца (201 + item view).
///
/// Target workspace берётся только из URL, владелец — только из session; envelope не
/// может задать ни ownership, ни lifecycle, ни идентичность Prompt.
pub async fn import_prompt(
Path(path): Path<WorkspacePath>,
State(state): State<AppState>,
body: Result<Json<Value>, JsonRejection>,
) -> Result<Response, ApiError> {
let envelope = body.map_err(import_body_error)?;
let imported = state
.service
.import_prompt_envelope(&WorkspaceId::new(path.workspace_id), envelope.0)
.await?;
Ok((StatusCode::CREATED, Json(imported)).into_response())
}
fn portable_response(envelope: &Value, filename: &str) -> Result<Response, ApiError> {
let mut headers = HeaderMap::new();
headers.insert(
header::CONTENT_TYPE,
HeaderValue::from_static("application/json"),
);
headers.insert(
header::HeaderName::from_static("x-content-type-options"),
HeaderValue::from_static("nosniff"),
);
// Filename выведен из `name` (ASCII alnum/-/_) с фиксированным суффиксом: CR/LF и
// traversal структурно невозможны.
headers.insert(
header::CONTENT_DISPOSITION,
HeaderValue::from_str(&format!("attachment; filename=\"{filename}\""))
.map_err(|_| ApiError::internal("invalid prompt portability export filename"))?,
);
let body = serde_json::to_vec(envelope)
.map_err(|_| ApiError::internal("invalid prompt portability envelope"))?;
Ok((headers, body).into_response())
}
/// Отказ разбора тела импорта: не JSON — 400 стабильный `prompt_portability_envelope_invalid`,
/// а не generic framework rejection.
fn import_body_error(rejection: JsonRejection) -> ApiError {
if rejection.into_response().status() == StatusCode::PAYLOAD_TOO_LARGE {
ApiError::PayloadTooLarge {
message: "prompt portability import body is too large".to_owned(),
context: Some(serde_json::json!({
"error_code": "prompt_portability_envelope_invalid"
})),
}
} else {
ApiError::validation_with_context(
"prompt portability envelope is invalid",
serde_json::json!({ "error_code": "prompt_portability_envelope_invalid" }),
)
}
}
+704
View File
@@ -0,0 +1,704 @@
//! Admin API Story 3.6: typed static Resource Drafts (D-16).
//!
//! Один режим просмотра (D-6): `GET .../preview` отдаёт metadata + findings +
//! инертный `text` только для text/JSON; `GET .../content` отдаёт bytes **всегда**
//! как `application/octet-stream` + `attachment` + CSP `sandbox`, поэтому
//! заявленный MIME никогда не становится HTTP content type.
//!
//! Порядок проверок D-20 обеспечивается разбором частей multipart и порядком
//! вызовов сервиса: метаданные (E) и разрешение Resource (C) обрабатываются до
//! содержимого (G) — содержимое только буферизуется, но не оценивается.
use axum::{
Extension, Json,
extract::{Multipart, Path, State, multipart::MultipartRejection, rejection::JsonRejection},
http::{HeaderMap, HeaderValue, StatusCode, header},
response::{IntoResponse, Response},
};
use crank_core::resource::ResourceKind;
use crank_registry::model::resource::RevokeResourceRequest;
use serde::Deserialize;
use serde_json::{Map, Value, json};
use crate::{
auth::AuthenticatedSession,
error::ApiError,
request_context::RequestContext,
service::resources::{
ArchiveResource, DraftMetadata, ImmutableField, NewDraft, PublishResource, UpdateDraft,
portability,
template::{NewTemplateDraft, TemplateIdentity, UpdateTemplateDraft},
},
state::AppState,
};
/// Тело ответа Draft нельзя превысить: часть содержимого — bounded bytes.
#[derive(Deserialize)]
pub struct ResourceWorkspacePath {
pub workspace_id: String,
}
#[derive(Deserialize)]
pub struct ResourcePath {
pub workspace_id: String,
pub resource_id: String,
}
#[derive(Deserialize)]
pub struct ResourceVersionPath {
pub workspace_id: String,
pub resource_id: String,
pub version: u32,
}
pub async fn create_resource(
Path(path): Path<ResourceWorkspacePath>,
State(state): State<AppState>,
multipart: Result<Multipart, MultipartRejection>,
) -> Result<Response, ApiError> {
let (metadata, content) = read_draft_multipart(multipart).await?;
// Story 3.10: `metadata.source` ⇒ template Draft без части `content`.
if metadata.contains_key("source") {
let source = content_absent(metadata.get("source"), content)?;
let created = state
.service
.resources()
.create_template_draft(NewTemplateDraft {
workspace_id: path.workspace_id.as_str().into(),
metadata: create_metadata(&metadata)?,
source,
})
.await?;
let card = state
.service
.resources()
.card(&path.workspace_id.as_str().into(), &created.resource_id)
.await?;
return Ok((StatusCode::CREATED, Json(json!(card))).into_response());
}
let metadata = create_metadata(&metadata)?;
let bytes = content.ok_or_else(malformed_multipart)?;
let created = state
.service
.resources()
.create_draft(NewDraft {
workspace_id: path.workspace_id.as_str().into(),
metadata,
bytes,
})
.await?;
let card = state
.service
.resources()
.card(&path.workspace_id.as_str().into(), &created.resource_id)
.await?;
Ok((StatusCode::CREATED, Json(json!(card))).into_response())
}
pub async fn list_resources(
Path(path): Path<ResourceWorkspacePath>,
State(state): State<AppState>,
) -> Result<Json<Value>, ApiError> {
let listed = state
.service
.resources()
.list(&path.workspace_id.as_str().into())
.await?;
Ok(Json(json!(listed)))
}
pub async fn get_resource(
Path(path): Path<ResourcePath>,
State(state): State<AppState>,
) -> Result<Json<Value>, ApiError> {
let card = state
.service
.resources()
.card(
&path.workspace_id.as_str().into(),
&path.resource_id.as_str().into(),
)
.await?;
Ok(Json(json!(card)))
}
pub async fn update_resource(
Path(path): Path<ResourcePath>,
State(state): State<AppState>,
multipart: Result<Multipart, MultipartRejection>,
) -> Result<Json<Value>, ApiError> {
let (metadata, content) = read_draft_multipart(multipart).await?;
// Story 3.10: template Draft редактируется определением, без части `content`.
if metadata.contains_key("source") {
let source = content_absent(metadata.get("source"), content)?;
let update =
update_template_draft(&path.workspace_id, &path.resource_id, &metadata, source)?;
let card = state
.service
.resources()
.update_template_draft(update)
.await?;
return Ok(Json(json!(card)));
}
if state
.service
.resources()
.is_template_draft(
&path.workspace_id.as_str().into(),
&path.resource_id.as_str().into(),
)
.await?
{
// Сохранённый template Draft без нового определения: не превращаем его в static.
return Err(crank_core::resource::ResourceError::SourceInvalid { field: "source" }.into());
}
let bytes = content.ok_or_else(malformed_multipart)?;
let update = update_draft(&path.workspace_id, &path.resource_id, &metadata, bytes)?;
let card = state.service.resources().update_draft(update).await?;
Ok(Json(json!(card)))
}
pub async fn test_resource(
Path(path): Path<ResourcePath>,
State(state): State<AppState>,
Extension(request_context): Extension<RequestContext>,
Json(body): Json<Value>,
) -> Result<Json<Value>, ApiError> {
let result = state
.service
.test_resource_read(
&path.workspace_id.as_str().into(),
&path.resource_id.as_str().into(),
body,
&request_context.correlation,
)
.await?;
Ok(Json(result))
}
pub async fn preview_resource(
Path(path): Path<ResourcePath>,
State(state): State<AppState>,
) -> Result<Json<Value>, ApiError> {
let preview = state
.service
.resources()
.preview(
&path.workspace_id.as_str().into(),
&path.resource_id.as_str().into(),
)
.await?;
Ok(Json(json!(preview)))
}
/// Публикация проверенного Draft в immutable Published Version (Story 3.7).
///
/// Тело — `{ "expected_updated_at": "<точное значение карточки>" }`. Отсутствие и
/// `null` означают «precondition не задан» и дают 428 `resource_stale` (как PUT);
/// значение другого JSON-типа — отказ формы запроса (400).
pub async fn publish_resource(
Path(path): Path<ResourcePath>,
State(state): State<AppState>,
Json(body): Json<Value>,
) -> Result<Json<Value>, ApiError> {
let expected_updated_at = lifecycle_precondition(&body)?;
let published = state
.service
.resources()
.publish(PublishResource {
workspace_id: path.workspace_id.as_str().into(),
resource_id: path.resource_id.as_str().into(),
expected_updated_at,
})
.await?;
Ok(Json(json!(published)))
}
/// Архив проверенного Resource (Story 3.7 AC05/AC06).
///
/// Тело — `{ "expected_updated_at": "<точное значение карточки>" }`; семантика
/// precondition/428 полностью совпадает с publish, а archived-отказ отдаётся
/// существующим domain-кодом `resource_lifecycle_invalid` (409).
pub async fn archive_resource(
Path(path): Path<ResourcePath>,
State(state): State<AppState>,
Json(body): Json<Value>,
) -> Result<Json<Value>, ApiError> {
let expected_updated_at = lifecycle_precondition(&body)?;
let archived = state
.service
.resources()
.archive(ArchiveResource {
workspace_id: path.workspace_id.as_str().into(),
resource_id: path.resource_id.as_str().into(),
expected_updated_at,
})
.await?;
Ok(Json(json!(archived)))
}
/// Отзыв Resource (Story 3.8, owner decision A): 204 без тела, без payload.
///
/// Actor берётся только из authenticated session, request/trace — из серверного
/// RequestContext; middleware workspace_router уже обеспечивает session/Owner/
/// CSRF/origin/rate guards. Повтор существующего отзыва — снова 204 без нового
/// события. Archive/immutable bytes/административное чтение не меняются.
pub async fn revoke_resource(
Path(path): Path<ResourcePath>,
State(state): State<AppState>,
Extension(session): Extension<AuthenticatedSession>,
Extension(request_context): Extension<RequestContext>,
) -> Result<StatusCode, ApiError> {
state
.service
.resources()
.revoke(RevokeResourceRequest {
workspace_id: path.workspace_id.as_str().into(),
resource_id: path.resource_id.as_str().into(),
actor_user_id: session.user.id.as_str().to_owned(),
request_id: request_context.request_id().to_owned(),
trace_id: request_context.trace_id().to_owned(),
revoked_at: time::OffsetDateTime::now_utc(),
})
.await?;
Ok(StatusCode::NO_CONTENT)
}
/// Story 3.9: экспорт текущего Draft как bounded portability envelope.
///
/// Ответ всегда `application/json` + `attachment` + `nosniff`: заявленный MIME
/// Resource остаётся внутри envelope и никогда не становится HTTP content type.
pub async fn export_resource(
Path(path): Path<ResourcePath>,
State(state): State<AppState>,
) -> Result<Response, ApiError> {
let envelope = state
.service
.resources()
.export_draft_envelope(
&path.workspace_id.as_str().into(),
&path.resource_id.as_str().into(),
)
.await?;
portable_response(&envelope)
}
/// Story 3.9: экспорт точной Published Version (immutable snapshot этой ревизии).
pub async fn export_resource_version(
Path(path): Path<ResourceVersionPath>,
State(state): State<AppState>,
) -> Result<Response, ApiError> {
let envelope = state
.service
.resources()
.export_published_envelope(
&path.workspace_id.as_str().into(),
&path.resource_id.as_str().into(),
path.version,
)
.await?;
portable_response(&envelope)
}
/// Story 3.9: импорт envelope как **нового** Draft текущего владельца (201 + карточка).
///
/// Target workspace берётся только из URL, владелец — только из session; envelope не
/// может задать ни ownership, ни lifecycle. Превышение тела — тот же безопасный JSON-конверт.
pub async fn import_resource(
Path(path): Path<ResourceWorkspacePath>,
State(state): State<AppState>,
body: Result<Json<Value>, JsonRejection>,
) -> Result<Response, ApiError> {
let envelope = body.map_err(import_body_error)?;
let import = portability::parse_portable_envelope(&envelope.0)?;
let card = state
.service
.resources()
.import_envelope(path.workspace_id.as_str().into(), import)
.await?;
Ok((StatusCode::CREATED, Json(json!(card))).into_response())
}
fn portable_response(envelope: &portability::PortableEnvelope) -> Result<Response, ApiError> {
let mut headers = HeaderMap::new();
headers.insert(
header::CONTENT_TYPE,
HeaderValue::from_static("application/json"),
);
headers.insert(
header::HeaderName::from_static("x-content-type-options"),
HeaderValue::from_static("nosniff"),
);
// Filename выведен из `name` (ASCII alnum/-/_) с фиксированным суффиксом: CR/LF и
// traversal структурно невозможны.
headers.insert(
header::CONTENT_DISPOSITION,
HeaderValue::from_str(&format!(
"attachment; filename=\"{}\"",
portability::export_filename(&envelope.resource.name)
))
.map_err(|_| ApiError::internal("invalid portability export filename"))?,
);
let body = serde_json::to_vec(envelope)
.map_err(|_| ApiError::internal("invalid portability envelope"))?;
Ok((headers, body).into_response())
}
/// Отказ разбора тела импорта: реальное превышение размера — 413 с Resource-кодом,
/// остальное — 400 `resource_portability_envelope_invalid`, а не generic framework rejection.
fn import_body_error(rejection: JsonRejection) -> ApiError {
if rejection.into_response().status() == StatusCode::PAYLOAD_TOO_LARGE {
ApiError::from(crank_core::resource::ResourceError::ContentTooLarge)
} else {
ApiError::from(crank_core::resource::ResourceError::PortabilityEnvelopeInvalid)
}
}
/// Точная Published Version: девять публичных metadata полей.
pub async fn get_resource_version(
Path(path): Path<ResourceVersionPath>,
State(state): State<AppState>,
) -> Result<Json<Value>, ApiError> {
let version = state
.service
.resources()
.published_version(
&path.workspace_id.as_str().into(),
&path.resource_id.as_str().into(),
path.version,
)
.await?;
Ok(Json(json!(version)))
}
/// Байты точной Published Version: та же inert-политика, что и у Draft download.
pub async fn download_resource_version(
Path(path): Path<ResourceVersionPath>,
State(state): State<AppState>,
) -> Result<Response, ApiError> {
let download = state
.service
.resources()
.read_published_content_for_download(
&path.workspace_id.as_str().into(),
&path.resource_id.as_str().into(),
path.version,
)
.await?;
Ok(download_response(&download))
}
fn download_response(download: &crate::service::resources::ResourceDownload) -> Response {
let filename = crate::service::resources::derived_filename(&download.name, &download.mime_type);
let mut headers = HeaderMap::new();
headers.insert(
header::CONTENT_TYPE,
HeaderValue::from_static("application/octet-stream"),
);
headers.insert(
header::CONTENT_SECURITY_POLICY,
HeaderValue::from_static("default-src 'none'; sandbox"),
);
// `filename` выведен из `name` и заявленного MIME: raw upload filename не
// сохраняется вообще, поэтому CR/LF и traversal структурно невозможны (D-12).
match HeaderValue::from_str(&format!(
"attachment; filename=\"{filename}\"; filename*=UTF-8''{filename}"
)) {
Ok(value) => {
headers.insert(header::CONTENT_DISPOSITION, value);
(headers, download.bytes.clone()).into_response()
}
Err(_) => ApiError::internal("invalid resource download filename").into_response(),
}
}
fn lifecycle_precondition(body: &Value) -> Result<Option<String>, ApiError> {
let object = body
.as_object()
.ok_or_else(|| ApiError::validation("resource lifecycle body must be a JSON object"))?;
match object.get("expected_updated_at") {
None | Some(Value::Null) => Ok(None),
Some(Value::String(value)) => Ok(Some(value.clone())),
Some(_) => Err(ApiError::validation(
"resource field `expected_updated_at` must be a JSON string",
)),
}
}
pub async fn download_resource(
Path(path): Path<ResourcePath>,
State(state): State<AppState>,
) -> Result<Response, ApiError> {
let download = state
.service
.resources()
.read_content_for_download(
&path.workspace_id.as_str().into(),
&path.resource_id.as_str().into(),
)
.await?;
let filename = crate::service::resources::derived_filename(&download.name, &download.mime_type);
let mut headers = HeaderMap::new();
headers.insert(
header::CONTENT_TYPE,
HeaderValue::from_static("application/octet-stream"),
);
headers.insert(
header::CONTENT_SECURITY_POLICY,
HeaderValue::from_static("default-src 'none'; sandbox"),
);
// `filename` выведен из `name` и заявленного MIME: raw upload filename не
// сохраняется вообще, поэтому CR/LF и traversal структурно невозможны (D-12).
headers.insert(
header::CONTENT_DISPOSITION,
HeaderValue::from_str(&format!(
"attachment; filename=\"{filename}\"; filename*=UTF-8''{filename}"
))
.map_err(|_| ApiError::internal("invalid resource download filename"))?,
);
Ok((headers, download.bytes).into_response())
}
/// Разбор multipart: часть `metadata` (JSON-объект) + часть `content` (сырые bytes).
///
/// Содержимое **не** оценивается здесь: превышение предела фиксируется лимитом
/// фреймворка, а стабильные коды (метаданные → precondition → содержимое)
/// назначает сервис в порядке D-20. Оборванное тело внутри окна лимита — отказ
/// формы запроса (400), а не «превышение размера»: иначе клиент получал бы
/// неверный код за запрос, который просто не дочитан (P2).
async fn read_draft_multipart(
multipart: Result<Multipart, MultipartRejection>,
) -> Result<(Map<String, Value>, Option<Vec<u8>>), ApiError> {
let mut multipart = multipart.map_err(multipart_rejection)?;
let mut metadata: Option<Map<String, Value>> = None;
let mut content: Option<Vec<u8>> = None;
while let Some(field) = multipart.next_field().await.map_err(multipart_error)? {
match field.name() {
Some("metadata") => {
if metadata.is_some() {
return Err(malformed_multipart());
}
let bytes = field.bytes().await.map_err(multipart_error)?;
let value: Value = serde_json::from_slice(&bytes)
.map_err(|_| ApiError::validation("invalid resource metadata"))?;
metadata = Some(value.as_object().cloned().ok_or_else(|| {
ApiError::validation("resource metadata must be a JSON object")
})?);
}
Some("content") => {
if content.is_some() {
return Err(malformed_multipart());
}
let bytes = field.bytes().await.map_err(multipart_error)?;
content = Some(bytes.to_vec());
}
_ => return Err(malformed_multipart()),
}
}
let metadata = metadata.ok_or_else(malformed_multipart)?;
// Story 3.10: часть `content` необязательна только для template Draft; обязательность
// для static Resource проверяет вызывающий маршрут.
Ok((metadata, content))
}
/// `metadata.source` присутствует ⇒ часть `content` обязана отсутствовать.
fn content_absent(source: Option<&Value>, content: Option<Vec<u8>>) -> Result<Value, ApiError> {
if content.is_some() {
return Err(crank_core::resource::ResourceError::SourceInvalid { field: "content" }.into());
}
source.cloned().ok_or_else(|| {
crank_core::resource::ResourceError::SourceInvalid { field: "source" }.into()
})
}
/// Обновление template Draft: `source` адресует pinned `input_schema`, а
/// identity-поля (`uri`/`name`/`kind`) принимаются только без изменения значения.
fn update_template_draft(
workspace_id: &str,
resource_id: &str,
metadata: &Map<String, Value>,
source: Value,
) -> Result<UpdateTemplateDraft, ApiError> {
Ok(UpdateTemplateDraft {
workspace_id: workspace_id.into(),
resource_id: resource_id.into(),
description: default_text(metadata, "description")?,
mime_type: required_text(metadata, "mime_type")?,
sensitivity: required_text(metadata, "sensitivity")?,
expected_updated_at: optional_timestamp(metadata, "expected_updated_at")?,
identity: template_identity(metadata)?,
source: Some(source),
})
}
fn template_identity(metadata: &Map<String, Value>) -> Result<TemplateIdentity, ApiError> {
let kind = optional_text(metadata, "kind")?;
if kind
.as_deref()
.is_some_and(|value| ResourceKind::parse(value).is_none())
{
return Err(unknown_enum("kind"));
}
Ok(TemplateIdentity {
uri: optional_text(metadata, "uri")?,
name: optional_text(metadata, "name")?,
kind,
})
}
fn optional_text(metadata: &Map<String, Value>, key: &str) -> Result<Option<String>, ApiError> {
match metadata.get(key) {
None => Ok(None),
Some(Value::String(value)) => Ok(Some(value.clone())),
Some(_) => Err(wrong_json_type(key)),
}
}
/// Метаданные создания: шаг B (форма запроса) + шаг E (поля) в порядке D-20.
///
/// Здесь проверяется только то, что относится к форме запроса (шаг B): неизвестный
/// `kind` и типы полей. Поля метаданных и значение `sensitivity` проверяются сервисом
/// на их собственном шаге E в порядке `uri → name → description → kind → mime_type →
/// sensitivity`, поэтому неизвестная `sensitivity` не может опередить более раннее поле.
fn create_metadata(metadata: &Map<String, Value>) -> Result<DraftMetadata, ApiError> {
let kind = required_text(metadata, "kind")?;
if ResourceKind::parse(&kind).is_none() {
return Err(unknown_enum("kind"));
}
Ok(DraftMetadata {
uri: required_text(metadata, "uri")?,
name: required_text(metadata, "name")?,
description: default_text(metadata, "description")?,
kind,
mime_type: required_text(metadata, "mime_type")?,
sensitivity: required_text(metadata, "sensitivity")?,
})
}
/// Обновление Draft: `uri`/`name`/`kind` не принимаются как изменяемые значения —
/// их **присутствие** уже нарушение неизменяемости (D-16), включая `null`.
///
/// Здесь проверяется форма запроса (шаг B): типы immutable-ключей и обязательных
/// полей, а неизвестный `kind` — тоже B. Само присутствие immutable-ключа — шаг D,
/// который исполняет сервис. Значение `sensitivity` (шаг E, последнее поле) сервис
/// проверяет уже после разрешения Resource (C) и шага D, поэтому неизвестная
/// `sensitivity` не может ни опередить C/D, ни превратиться во внутреннюю ошибку.
fn update_draft(
workspace_id: &str,
resource_id: &str,
metadata: &Map<String, Value>,
bytes: Vec<u8>,
) -> Result<UpdateDraft, ApiError> {
let description = default_text(metadata, "description")?;
let mime_type = required_text(metadata, "mime_type")?;
let sensitivity = required_text(metadata, "sensitivity")?;
let expected_updated_at = optional_timestamp(metadata, "expected_updated_at")?;
let immutable_fields = immutable_fields(metadata)?;
Ok(UpdateDraft {
workspace_id: workspace_id.into(),
resource_id: resource_id.into(),
description,
mime_type,
sensitivity,
expected_updated_at,
immutable_fields,
bytes,
})
}
/// Присутствие immutable-ключей (D-16) с шагом B для их неверного JSON-типа.
fn immutable_fields(metadata: &Map<String, Value>) -> Result<Vec<ImmutableField>, ApiError> {
let mut fields = Vec::new();
for (key, field) in [
("uri", ImmutableField::Uri),
("name", ImmutableField::Name),
("kind", ImmutableField::Kind),
] {
match metadata.get(key) {
// Ключа нет — валидное обновление изменяемых полей.
None => {}
// Присутствие ключа — уже нарушение неизменяемости, значение не важно.
Some(Value::Null) => fields.push(field),
Some(Value::String(value)) => {
// Неизвестный `kind` — отказ формы запроса (D-20 B), а не шаг D;
// известный `kind` остаётся нарушением неизменяемости (D).
if field == ImmutableField::Kind && ResourceKind::parse(value).is_none() {
return Err(unknown_enum("kind"));
}
fields.push(field);
}
// Значение другого JSON-типа — форма запроса не распознана (D-20 B).
Some(_) => return Err(wrong_json_type(key)),
}
}
Ok(fields)
}
/// Обязательное поле метаданных: отсутствующее или не-строковое — шаг B.
fn required_text(metadata: &Map<String, Value>, key: &str) -> Result<String, ApiError> {
match metadata.get(key) {
Some(Value::String(value)) => Ok(value.clone()),
_ => Err(wrong_json_type(key)),
}
}
/// Необязательное текстовое поле: отсутствие — значение по умолчанию `""`.
fn default_text(metadata: &Map<String, Value>, key: &str) -> Result<String, ApiError> {
match metadata.get(key) {
None => Ok(String::new()),
Some(Value::String(value)) => Ok(value.clone()),
Some(_) => Err(wrong_json_type(key)),
}
}
/// Необязательный precondition: отсутствие и `null` — «не задан» (шаг F даёт 428).
fn optional_timestamp(
metadata: &Map<String, Value>,
key: &str,
) -> Result<Option<String>, ApiError> {
match metadata.get(key) {
None | Some(Value::Null) => Ok(None),
Some(Value::String(value)) => Ok(Some(value.clone())),
Some(_) => Err(wrong_json_type(key)),
}
}
fn unknown_enum(key: &str) -> ApiError {
ApiError::validation(format!(
"resource metadata field `{key}` has an unknown value"
))
}
fn wrong_json_type(key: &str) -> ApiError {
ApiError::validation(format!(
"resource metadata field `{key}` must be a JSON string"
))
}
fn malformed_multipart() -> ApiError {
// Шаг B порядка D-20: расхождение формы запроса — generic validation_error
// без Resource-специфичного кода.
ApiError::validation("invalid resource multipart upload")
}
fn content_too_large() -> ApiError {
ApiError::from(crank_core::resource::ResourceError::ContentTooLarge)
}
/// Отказ чтения части multipart: реальное превышение размера остаётся 413,
/// оборванное или нечитаемое тело — клиентская ошибка формы (400).
fn multipart_error(error: axum::extract::multipart::MultipartError) -> ApiError {
if error.status() == StatusCode::PAYLOAD_TOO_LARGE {
content_too_large()
} else {
malformed_multipart()
}
}
fn multipart_rejection(rejection: MultipartRejection) -> ApiError {
if rejection.into_response().status() == StatusCode::PAYLOAD_TOO_LARGE {
content_too_large()
} else {
malformed_multipart()
}
}
+15 -1
View File
@@ -8,7 +8,8 @@ use serde_json::{Value, json};
use crate::{
auth::AuthenticatedSession,
error::ApiError,
service::{RotateSecretPayload, SecretPayload},
request_context::RequestContext,
service::{AdminAuditContext, RotateSecretPayload, SecretPayload},
state::AppState,
};
@@ -38,14 +39,18 @@ pub async fn create_secret(
Path(path): Path<WorkspacePath>,
State(state): State<AppState>,
Extension(session): Extension<AuthenticatedSession>,
Extension(request_context): Extension<RequestContext>,
Json(payload): Json<SecretPayload>,
) -> Result<Json<Value>, ApiError> {
let audit_context =
AdminAuditContext::from_session_and_correlation(&session, &request_context.correlation);
let secret = state
.service
.create_secret(
&path.workspace_id.as_str().into(),
Some(&session.user.id),
payload,
Some(&audit_context),
)
.await?;
Ok(Json(json!(secret)))
@@ -69,8 +74,11 @@ pub async fn rotate_secret(
Path(path): Path<WorkspaceSecretPath>,
State(state): State<AppState>,
Extension(session): Extension<AuthenticatedSession>,
Extension(request_context): Extension<RequestContext>,
Json(payload): Json<RotateSecretPayload>,
) -> Result<Json<Value>, ApiError> {
let audit_context =
AdminAuditContext::from_session_and_correlation(&session, &request_context.correlation);
let secret = state
.service
.rotate_secret(
@@ -78,6 +86,7 @@ pub async fn rotate_secret(
&path.secret_id.as_str().into(),
Some(&session.user.id),
payload,
Some(&audit_context),
)
.await?;
Ok(Json(json!(secret)))
@@ -86,12 +95,17 @@ pub async fn rotate_secret(
pub async fn delete_secret(
Path(path): Path<WorkspaceSecretPath>,
State(state): State<AppState>,
Extension(session): Extension<AuthenticatedSession>,
Extension(request_context): Extension<RequestContext>,
) -> Result<Json<Value>, ApiError> {
let audit_context =
AdminAuditContext::from_session_and_correlation(&session, &request_context.correlation);
state
.service
.delete_secret(
&path.workspace_id.as_str().into(),
&path.secret_id.as_str().into(),
Some(&audit_context),
)
.await?;
Ok(Json(json!({ "ok": true })))
+515 -101
View File
@@ -3,24 +3,30 @@ use std::path::PathBuf;
use std::sync::Arc;
use base64::{Engine as _, engine::general_purpose::URL_SAFE_NO_PAD};
use crank_artifacts::ArtifactStore;
use crank_config::ExternalReferenceSettings;
use crank_core::{
AuditSink, AuthProfile, CapabilityProfile, CommunityCapabilityProfile, EditionCapabilities,
AuditActor, AuditEvent, AuditEventId, AuditSink, AuditTarget, AuditTargetKind, AuthProfile,
CapabilityProfile, CommunityCapabilityProfile, CorrelationContext, EditionCapabilities,
ExecutionMode, IdentityError, IdentityProvider, InvocationLog, InvocationLogId,
InvocationSource, NoopAuditSink, OperationSecurityLevel, OwnerOnlyPolicyEngine, PolicyEngine,
ProductEdition, Protocol, ToolQualityMappingRule, ToolQualityMappingSet, ToolQualitySchemaKind,
ToolQualitySchemaNode, UsagePeriod, WorkspaceId,
ProductEdition, Protocol, SecretStatus, ToolQualityMappingRule, ToolQualityMappingSet,
ToolQualitySchemaKind, ToolQualitySchemaNode, UsagePeriod, WorkspaceId,
};
use crank_mapping::{MappingRule, MappingSet};
use crank_registry::{
AgentSummary, CreateInvocationLogRequest, InvocationHistoryWriteOutcome, OperationAgentRef,
OperationSummary, OperationUsageSummary, PostgresRegistry, RegistryOperation, UsageBucket,
OperationSummary, OperationUsageSummary, PostgresRegistry, RegistryError, RegistryOperation,
UsageBucket,
};
use crank_runtime::{
OutboundHttpPolicy, PreparedRequest, ResolvedAuth, RuntimeError, RuntimeExecutor, SecretCrypto,
ExternalReferenceFetcher, OutboundHttpPolicy, ResolvedAuth, RuntimeError, RuntimeExecutor,
SecretCrypto,
};
use crank_schema::{Schema, SchemaKind};
use crank_trace::{DbOperation, ErrorCategory, Stage, StageOutcome, observe_db_query};
use serde_json::{Value, json};
use rand::RngExt;
use serde_json::json;
use sha2::{Digest, Sha256};
use time::{OffsetDateTime, format_description::well_known::Rfc3339};
use tracing::Instrument;
@@ -33,14 +39,21 @@ mod demo;
mod import_export;
mod imports;
mod observability;
mod onboarding;
mod operation_validation;
mod operations;
mod prompts;
pub(crate) mod resources;
mod samples;
mod secrets;
mod upstreams;
mod workspaces;
use crate::{auth::AuthSettings, error::ApiError, storage::LocalArtifactStorage};
use crate::{
auth::{AuthSettings, AuthenticatedSession},
error::ApiError,
storage::LocalArtifactStorage,
};
use operation_validation::{
validate_approval_policy, validate_execution_timeout, validate_idempotency_policy,
validate_protocol_target, validate_response_cache_policy,
@@ -49,6 +62,8 @@ use operation_validation::{
#[derive(Clone)]
pub struct AdminService {
registry: PostgresRegistry,
artifact_store: Arc<ArtifactStore>,
resources: resources::ResourcesService,
runtime: RuntimeExecutor,
storage: LocalArtifactStorage,
auth_settings: AuthSettings,
@@ -58,11 +73,17 @@ pub struct AdminService {
audit_sink: Arc<dyn AuditSink>,
capability_profile: Arc<dyn CapabilityProfile>,
outbound_http_policy: OutboundHttpPolicy,
external_reference_fetcher: ExternalReferenceFetcher,
external_reference_normalization: crank_import::rest::NormalizationConfig,
external_reference_materialization_timeout: std::time::Duration,
public_base_url: String,
}
pub struct AdminServiceBuilder {
registry: PostgresRegistry,
storage_root: PathBuf,
artifact_store: Option<Arc<ArtifactStore>>,
resource_limits: crank_core::resource::ResourceLimits,
auth_settings: AuthSettings,
secret_crypto: SecretCrypto,
runtime: RuntimeExecutor,
@@ -71,14 +92,72 @@ pub struct AdminServiceBuilder {
audit_sink: Option<Arc<dyn AuditSink>>,
capability_profile: Option<Arc<dyn CapabilityProfile>>,
outbound_http_policy: OutboundHttpPolicy,
external_reference_fetcher: ExternalReferenceFetcher,
external_reference_normalization: crank_import::rest::NormalizationConfig,
external_reference_settings: ExternalReferenceSettings,
public_base_url: String,
}
pub use crate::dto::*;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ReadinessChecks {
pub postgres: bool,
pub artifact_storage: bool,
}
impl ReadinessChecks {
pub const fn is_ready(self) -> bool {
self.postgres && self.artifact_storage
}
}
#[derive(Clone, Debug)]
pub struct AdminAuditContext {
actor: AuditActor,
request_id: String,
trace_id: String,
}
impl AdminAuditContext {
pub fn from_session_and_correlation(
session: &AuthenticatedSession,
correlation: &CorrelationContext,
) -> Self {
Self {
actor: AuditActor {
user_id: session.user.id.clone(),
email: session.user.email.clone(),
session_id: Some(session.session_id.clone()),
},
request_id: correlation.request_id().as_str().to_owned(),
trace_id: correlation.trace_id().as_str().to_owned(),
}
}
}
#[derive(Clone, Copy, Debug)]
pub(super) struct CredentialAuditRecord<'a> {
pub action: &'static str,
pub target_kind: AuditTargetKind,
pub workspace_id: &'a WorkspaceId,
pub target_id: &'a str,
pub credential_type: &'static str,
pub outcome: &'static str,
pub reason: &'a str,
}
impl AdminService {
pub async fn readiness(&self) -> Result<(), ApiError> {
self.registry.ping().await?;
Ok(())
pub async fn readiness(&self) -> ReadinessChecks {
let postgres = self.registry.ping().await.is_ok();
let store = Arc::clone(&self.artifact_store);
let artifact_storage = tokio::task::spawn_blocking(move || store.check_health())
.await
.is_ok_and(|result| result.is_ok());
ReadinessChecks {
postgres,
artifact_storage,
}
}
#[cfg(test)]
@@ -130,6 +209,37 @@ impl AdminService {
pub fn capability_profile(&self) -> &Arc<dyn CapabilityProfile> {
&self.capability_profile
}
/// Сервис typed static Resource Drafts (Story 3.6): create/list/get/update/
/// preview/content внутри выделенного модуля.
pub(crate) fn resources(&self) -> &resources::ResourcesService {
&self.resources
}
pub(crate) async fn test_resource_read(
&self,
workspace_id: &WorkspaceId,
resource_id: &crank_core::ids::ResourceId,
body: serde_json::Value,
correlation: &crank_core::CorrelationContext,
) -> Result<serde_json::Value, ApiError> {
self.resources
.execute_template_test(self, workspace_id, resource_id, body, correlation)
.await
}
}
fn runtime_credential_error_code(error: &RuntimeError) -> &'static str {
match error {
RuntimeError::AuthorizationStoreUnavailable => "authorization_store_unavailable",
RuntimeError::MissingAuthProfile { .. } => "auth_profile_not_found",
RuntimeError::InvalidAuthProfileConfig { .. } => "auth_profile_invalid",
RuntimeError::MissingSecret { .. } => "secret_not_found",
RuntimeError::MissingSecretVersion { .. } => "secret_version_not_found",
RuntimeError::InvalidAuthSecretValue { .. } => "secret_invalid",
RuntimeError::SecretCrypto { .. } => "secret_crypto_failed",
_ => "credential_resolution_failed",
}
}
impl AdminServiceBuilder {
@@ -140,9 +250,27 @@ impl AdminServiceBuilder {
secret_crypto: SecretCrypto,
runtime: RuntimeExecutor,
) -> Self {
let outbound_http_policy = OutboundHttpPolicy::default();
let external_reference_settings = ExternalReferenceSettings {
allowed_url_prefixes: Vec::new(),
max_depth: 8,
max_documents: 32,
max_fetch_bytes: 256 * 1024,
fetch_timeout_ms: 5_000,
max_expanded_nodes: 10_000,
};
let external_reference_fetcher = ExternalReferenceFetcher::try_new(
outbound_http_policy.clone(),
external_reference_settings.allowed_url_prefixes.clone(),
external_reference_settings.max_fetch_bytes,
std::time::Duration::from_millis(external_reference_settings.fetch_timeout_ms),
)
.expect("static external reference defaults are valid");
Self {
registry,
storage_root,
artifact_store: None,
resource_limits: crank_core::resource::ResourceLimits::default(),
auth_settings,
secret_crypto,
runtime,
@@ -150,7 +278,11 @@ impl AdminServiceBuilder {
policy_engine: None,
audit_sink: None,
capability_profile: None,
outbound_http_policy: OutboundHttpPolicy::default(),
outbound_http_policy,
external_reference_fetcher,
external_reference_normalization: Default::default(),
external_reference_settings,
public_base_url: "http://localhost:3000".to_owned(),
}
}
@@ -160,10 +292,61 @@ impl AdminServiceBuilder {
}
pub fn with_outbound_http_policy(mut self, policy: OutboundHttpPolicy) -> Self {
self.external_reference_fetcher = ExternalReferenceFetcher::try_new(
policy.clone(),
self.external_reference_settings
.allowed_url_prefixes
.clone(),
self.external_reference_settings.max_fetch_bytes,
std::time::Duration::from_millis(self.external_reference_settings.fetch_timeout_ms),
)
.expect("validated external reference settings remain valid");
self.outbound_http_policy = policy;
self
}
pub fn with_external_reference_import(
mut self,
settings: &ExternalReferenceSettings,
) -> Result<Self, crank_runtime::ExternalReferenceFetchError> {
self.external_reference_fetcher = ExternalReferenceFetcher::try_new(
self.outbound_http_policy.clone(),
settings.allowed_url_prefixes.clone(),
settings.max_fetch_bytes,
std::time::Duration::from_millis(settings.fetch_timeout_ms),
)?;
self.external_reference_normalization.max_reference_depth = settings.max_depth;
self.external_reference_normalization
.max_reference_documents = settings.max_documents;
self.external_reference_normalization
.max_external_document_bytes = settings.max_fetch_bytes;
self.external_reference_normalization.max_expanded_nodes = settings.max_expanded_nodes;
self.external_reference_normalization
.external_references_enabled = !settings.allowed_url_prefixes.is_empty();
self.external_reference_settings = settings.clone();
Ok(self)
}
/// Reuses the process-wide immutable artifact authority for OpenAPI
/// ingress and reconciliation. Tests may omit this and use their private
/// storage root instead.
pub fn with_artifact_store(mut self, artifact_store: Arc<ArtifactStore>) -> Self {
self.artifact_store = Some(artifact_store);
self
}
/// Пределы содержимого Resource (D-3): значение выше storage ceiling отвергается,
/// нового env-ключа не вводится.
pub fn with_resource_limits(mut self, limits: crank_core::resource::ResourceLimits) -> Self {
self.resource_limits = limits;
self
}
pub fn with_public_base_url(mut self, public_base_url: String) -> Self {
self.public_base_url = public_base_url.trim_end_matches('/').to_owned();
self
}
#[allow(dead_code)]
pub fn with_policy_engine(mut self, policy_engine: Arc<dyn PolicyEngine>) -> Self {
self.policy_engine = Some(policy_engine);
@@ -186,8 +369,18 @@ impl AdminServiceBuilder {
}
pub fn build(self) -> AdminService {
let artifact_store = self
.artifact_store
.unwrap_or_else(|| Arc::new(ArtifactStore::new(&self.storage_root)));
let resources = resources::ResourcesService::new(
self.registry.clone(),
Arc::clone(&artifact_store),
self.resource_limits,
);
AdminService {
registry: self.registry,
artifact_store,
resources,
runtime: self.runtime,
storage: LocalArtifactStorage::new(self.storage_root),
auth_settings: self.auth_settings,
@@ -201,6 +394,12 @@ impl AdminServiceBuilder {
.capability_profile
.unwrap_or_else(|| Arc::new(CommunityCapabilityProfile)),
outbound_http_policy: self.outbound_http_policy,
external_reference_fetcher: self.external_reference_fetcher,
external_reference_normalization: self.external_reference_normalization,
external_reference_materialization_timeout: std::time::Duration::from_millis(
self.external_reference_settings.fetch_timeout_ms,
),
public_base_url: self.public_base_url,
}
}
}
@@ -254,10 +453,56 @@ impl AdminService {
self.capability_profile().capabilities()
}
async fn record_credential_audit(
&self,
audit_context: Option<&AdminAuditContext>,
record: CredentialAuditRecord<'_>,
) {
let Some(audit_context) = audit_context else {
return;
};
let event = AuditEvent {
id: AuditEventId::new(new_prefixed_id("audit")),
occurred_at: OffsetDateTime::now_utc(),
actor: audit_context.actor.clone(),
action: record.action.to_owned(),
target: AuditTarget {
workspace_id: record.workspace_id.clone(),
kind: record.target_kind,
id: record.target_id.to_owned(),
},
payload: json!({
"credential_type": record.credential_type,
"outcome": record.outcome,
"reason": record.reason,
"request_id": audit_context.request_id,
"trace_id": audit_context.trace_id
}),
source_ip: None,
user_agent: None,
};
if self.audit_sink.record(event).await.is_err() {
tracing::warn!(
name: "admin.credential_audit.lost",
action = record.action,
credential_type = record.credential_type,
outcome = record.outcome,
reason = record.reason,
request_id = %audit_context.request_id,
trace_id = %audit_context.trace_id,
error_code = "audit_sink_failed",
"credential audit event was not recorded"
);
}
}
async fn resolve_operation_auth(
&self,
workspace_id: &WorkspaceId,
execution_config: &crank_core::ExecutionConfig,
audit_context: Option<&AdminAuditContext>,
) -> Result<Option<ResolvedAuth>, RuntimeError> {
let Some(auth_profile_id) = execution_config.auth_profile_ref.as_ref() else {
return Ok(None);
@@ -271,15 +516,14 @@ impl AdminService {
.get_auth_profile(workspace_id, auth_profile_id),
)
.await
.map_err(|error| RuntimeError::SecretCrypto {
operation: "load auth profile",
details: error.to_string(),
})?
.ok_or_else(|| RuntimeError::MissingAuthProfile {
auth_profile_id: auth_profile_id.as_str().to_owned(),
})?;
.map_err(|_| RuntimeError::AuthorizationStoreUnavailable)?;
let Some(auth_profile) = auth_profile else {
return Err(RuntimeError::MissingAuthProfile {
auth_profile_id: auth_profile_id.as_str().to_owned(),
});
};
self.resolve_auth_profile(workspace_id, &auth_profile)
self.resolve_auth_profile(workspace_id, &auth_profile, audit_context)
.await
.map(Some)
}
@@ -292,6 +536,17 @@ impl AdminService {
ErrorCategory::Configuration.record(&span);
}
}
if let Err(error) = &result {
self.record_credential_resolution_failure(
audit_context,
workspace_id,
crank_core::AuditTargetKind::AuthProfile,
auth_profile_id.as_str(),
"auth_profile",
runtime_credential_error_code(error),
)
.await;
}
result
}
@@ -299,6 +554,7 @@ impl AdminService {
&self,
workspace_id: &WorkspaceId,
auth_profile: &AuthProfile,
audit_context: Option<&AdminAuditContext>,
) -> Result<ResolvedAuth, RuntimeError> {
let mut secrets = BTreeMap::new();
let used_at = OffsetDateTime::now_utc();
@@ -309,45 +565,156 @@ impl AdminService {
self.registry.get_secret(workspace_id, secret_id),
)
.await
.map_err(|error| RuntimeError::SecretCrypto {
operation: "load secret",
details: error.to_string(),
})?
.ok_or_else(|| RuntimeError::MissingSecret {
secret_id: secret_id.as_str().to_owned(),
})?;
.map_err(|_| RuntimeError::AuthorizationStoreUnavailable)?;
let Some(secret) = secret else {
let error = RuntimeError::MissingSecret {
secret_id: secret_id.as_str().to_owned(),
};
self.record_credential_resolution_failure(
audit_context,
workspace_id,
crank_core::AuditTargetKind::Secret,
secret_id.as_str(),
"secret",
runtime_credential_error_code(&error),
)
.await;
return Err(error);
};
if secret.secret.status != SecretStatus::Active {
let error = RuntimeError::InvalidAuthSecretValue {
secret_id: secret_id.as_str().to_owned(),
reason: "secret is not active".to_owned(),
};
self.record_credential_resolution_failure(
audit_context,
workspace_id,
crank_core::AuditTargetKind::Secret,
secret_id.as_str(),
"secret",
runtime_credential_error_code(&error),
)
.await;
return Err(error);
}
let version = observe_db_query(
DbOperation::SecretRead,
self.registry
.get_current_secret_version(workspace_id, secret_id),
)
.await
.map_err(|error| RuntimeError::SecretCrypto {
operation: "load current secret version",
details: error.to_string(),
})?
.ok_or_else(|| RuntimeError::MissingSecretVersion {
secret_id: secret_id.as_str().to_owned(),
version: secret.secret.current_version,
})?;
let plaintext = self.secret_crypto.decrypt(
.map_err(|_| RuntimeError::AuthorizationStoreUnavailable)?;
let Some(version) = version else {
let error = RuntimeError::MissingSecretVersion {
secret_id: secret_id.as_str().to_owned(),
version: secret.secret.current_version,
};
self.record_credential_resolution_failure(
audit_context,
workspace_id,
crank_core::AuditTargetKind::Secret,
secret_id.as_str(),
"secret",
runtime_credential_error_code(&error),
)
.await;
return Err(error);
};
let plaintext = self.secret_crypto.decrypt_for_epoch(
&version.secret_version.key_version,
version.master_key_epoch,
&version.secret_version.ciphertext,
)?;
);
let plaintext = match plaintext {
Ok(plaintext) => plaintext,
Err(error) => {
self.record_credential_resolution_failure(
audit_context,
workspace_id,
crank_core::AuditTargetKind::Secret,
secret_id.as_str(),
"secret",
runtime_credential_error_code(&error),
)
.await;
return Err(error);
}
};
observe_db_query(
DbOperation::SecretTouch,
self.registry
.touch_secret(workspace_id, secret_id, &used_at),
)
.await
.map_err(|error| RuntimeError::SecretCrypto {
operation: "touch secret",
details: error.to_string(),
})?;
.unwrap_or_else(|_| {
tracing::warn!(
name: "admin.secret.touch_failed",
secret_id = %secret_id.as_str(),
request_id = audit_context
.map(|context| context.request_id.as_str())
.unwrap_or("unavailable"),
trace_id = audit_context
.map(|context| context.trace_id.as_str())
.unwrap_or("unavailable"),
error_code = "secret_touch_failed",
"secret last-used metadata was not updated"
);
});
secrets.insert(secret_id.clone(), plaintext);
}
ResolvedAuth::from_profile(auth_profile, &secrets)
let result = ResolvedAuth::from_profile(auth_profile, &secrets);
if let Err(error) = &result {
self.record_credential_resolution_failure(
audit_context,
workspace_id,
crank_core::AuditTargetKind::AuthProfile,
auth_profile.id.as_str(),
"auth_profile",
runtime_credential_error_code(error),
)
.await;
}
result
}
async fn record_credential_resolution_failure(
&self,
audit_context: Option<&AdminAuditContext>,
workspace_id: &WorkspaceId,
target_kind: crank_core::AuditTargetKind,
target_id: &str,
credential_type: &'static str,
reason: &'static str,
) {
self.record_credential_audit(
audit_context,
CredentialAuditRecord {
action: "credential.resolve_denied",
target_kind,
workspace_id,
target_id,
credential_type,
outcome: "failure",
reason,
},
)
.await;
tracing::warn!(
name: "admin.credential.resolve_denied",
credential_type,
target_kind = ?target_kind,
target_id,
outcome = "failure",
reason,
request_id = audit_context
.map(|context| context.request_id.as_str())
.unwrap_or("unavailable"),
trace_id = audit_context
.map(|context| context.trace_id.as_str())
.unwrap_or("unavailable"),
"credential resolution was denied"
);
}
fn validate_operation_payload(&self, payload: &OperationPayload) -> Result<(), ApiError> {
@@ -376,6 +743,70 @@ impl AdminService {
Ok(())
}
async fn validate_registry_operation_in_workspace(
&self,
workspace_id: &WorkspaceId,
operation: &RegistryOperation,
) -> Result<(), ApiError> {
self.validate_registry_operation(operation)?;
if let Some(auth_profile_id) = operation.execution_config.auth_profile_ref.as_ref() {
let Some(auth_profile) = self
.registry
.get_auth_profile(workspace_id, auth_profile_id)
.await?
else {
return Err(ApiError::unprocessable_with_context(
"operation auth profile reference is unavailable",
json!({ "error_code": "operation_auth_profile_invalid" }),
));
};
self.validate_auth_profile_secret_refs_for_execution(workspace_id, &auth_profile)
.await?;
}
Ok(())
}
async fn validate_auth_profile_secret_refs_for_execution(
&self,
workspace_id: &WorkspaceId,
auth_profile: &AuthProfile,
) -> Result<(), ApiError> {
if auth_profile.kind != auth_profile.config.kind() {
return Err(ApiError::unprocessable_with_context(
"operation auth profile kind and config do not match",
json!({ "error_code": "operation_auth_profile_invalid" }),
));
}
for secret_id in auth_profile.config.secret_ids() {
let Some(secret) = self.registry.get_secret(workspace_id, secret_id).await? else {
return Err(ApiError::unprocessable_with_context(
"operation auth profile secret reference is unavailable",
json!({ "error_code": "operation_auth_profile_invalid" }),
));
};
if secret.secret.status != SecretStatus::Active {
return Err(RegistryError::SecretInactive {
secret_id: secret_id.as_str().to_owned(),
}
.into());
}
if self
.registry
.get_current_secret_version(workspace_id, secret_id)
.await?
.is_none()
{
return Err(ApiError::unprocessable_with_context(
"operation auth profile secret has no current version",
json!({ "error_code": "operation_auth_profile_invalid" }),
));
}
}
Ok(())
}
fn validate_outbound_target(&self, target: &crank_core::Target) -> Result<(), ApiError> {
match target {
crank_core::Target::Rest(rest) => self
@@ -462,16 +893,23 @@ impl AdminService {
id: InvocationLogId::new(new_prefixed_id("log")),
workspace_id: request.workspace_id.clone(),
agent_id: request.agent_id.cloned(),
platform_api_key_id: None,
operation_id: request.operation.id.clone(),
operation_version: Some(request.operation.version),
source: request.source,
level: request.level,
status: request.status,
tool_name: request.operation.name.clone(),
message: request.message,
request_id: request.request_id.map(ToOwned::to_owned),
trace_id: request.trace_id.map(ToOwned::to_owned),
status_code: request.status_code,
duration_ms: request.duration_ms,
error_kind: request.error_kind,
execution_stage: request.execution_stage,
execution_error_code: request.execution_error_code,
retryability: request.retryability,
outcome_certainty: request.outcome_certainty,
request_preview: request.request_preview,
response_preview: request.response_preview,
created_at: OffsetDateTime::now_utc(),
@@ -506,6 +944,7 @@ impl AdminService {
observe_invocation_history_outcome(
outcome,
request.request_id,
request.trace_id,
request.status,
request.source,
);
@@ -516,6 +955,7 @@ impl AdminService {
fn observe_invocation_history_outcome(
outcome: InvocationHistoryWriteOutcome,
request_id: Option<&str>,
trace_id: Option<&str>,
status: crank_core::InvocationStatus,
source: InvocationSource,
) {
@@ -528,6 +968,7 @@ fn observe_invocation_history_outcome(
tracing::warn!(
name: "admin.invocation_history.lost",
request_id = request_id.unwrap_or_default(),
trace_id = trace_id.unwrap_or_default(),
source = invocation_source_label(source),
invocation_status = invocation_status_label(status),
error_category = loss.category.as_str(),
@@ -549,29 +990,6 @@ fn invocation_status_label(status: crank_core::InvocationStatus) -> &'static str
}
}
fn build_request_preview(
mapping: &MappingSet,
input: &Value,
) -> Result<Value, crank_mapping::MappingError> {
let prepared = if mapping.is_empty() {
PreparedRequest::default()
} else {
let mapped = mapping.apply(&json!({ "mcp": input }))?;
PreparedRequest::from_mapping_output(&mapped).map_err(|error| {
crank_mapping::MappingError::InvalidJsonPath {
path: error.to_string(),
}
})?
};
Ok(json!({
"path": prepared.path_params,
"query": prepared.query_params,
"headers": prepared.headers,
"body": prepared.body.unwrap_or(Value::Null)
}))
}
fn validate_profile_display_name(value: &str) -> Result<String, ApiError> {
let display_name = value.trim();
if display_name.is_empty() {
@@ -612,7 +1030,9 @@ fn new_prefixed_id(prefix: &str) -> String {
}
fn generate_access_secret(marker: &str) -> String {
let random = URL_SAFE_NO_PAD.encode(Uuid::now_v7().as_bytes());
let mut secret_bytes = [0_u8; 32];
rand::rng().fill(&mut secret_bytes);
let random = URL_SAFE_NO_PAD.encode(secret_bytes);
format!("{marker}{random}")
}
@@ -636,7 +1056,7 @@ fn format_timestamp(timestamp: OffsetDateTime) -> String {
fn map_identity_error(error: IdentityError) -> ApiError {
match error {
IdentityError::BadCredentials => ApiError::unauthorized("invalid email or password"),
IdentityError::AccountDisabled => ApiError::forbidden("account is disabled"),
IdentityError::AccountDisabled => ApiError::unauthorized("invalid email or password"),
IdentityError::NotSupportedForProvider => ApiError::internal(
"password login is not supported by the configured identity provider",
),
@@ -644,32 +1064,6 @@ fn map_identity_error(error: IdentityError) -> ApiError {
}
}
fn runtime_error_code(error: &RuntimeError) -> &'static str {
match error {
RuntimeError::Schema(_) => "schema_error",
RuntimeError::Mapping(_) => "mapping_error",
RuntimeError::InvalidPreparedRequest { .. } => "invalid_request",
RuntimeError::ConfirmationRequired { .. } => "confirmation_required",
RuntimeError::InvalidConfirmationToken { .. } => "invalid_confirmation_token",
RuntimeError::ConfirmationStoreUnavailable { .. } => "confirmation_unavailable",
RuntimeError::IdempotencyStoreUnavailable { .. } => "idempotency_unavailable",
RuntimeError::IdempotencyInProgress { .. } => "idempotency_in_progress",
RuntimeError::IdempotencyConflict { .. } => "idempotency_conflict",
RuntimeError::IdempotencyOutcomeUnknown { .. } => "idempotency_outcome_unknown",
RuntimeError::RestAdapter(_) => "rest_error",
RuntimeError::ProtocolAdapter(_) => "adapter_error",
RuntimeError::UnsupportedProtocol { .. } => "unsupported_protocol",
RuntimeError::ConcurrencyLimitExceeded { .. } => "runtime_overloaded",
RuntimeError::UnsupportedExecutionMode { .. } => "streaming_mode_error",
RuntimeError::MissingAuthProfile { .. } => "auth_profile_not_found",
RuntimeError::MissingSecret { .. } | RuntimeError::MissingSecretVersion { .. } => {
"secret_not_found"
}
RuntimeError::InvalidAuthSecretValue { .. } => "secret_value_error",
RuntimeError::SecretCrypto { .. } => "secret_crypto_error",
}
}
fn protocol_capability_view(
protocol: Protocol,
_edition: ProductEdition,
@@ -694,7 +1088,9 @@ fn protocol_capability_view(
}
}
fn usage_window(period: UsagePeriod) -> Result<(UsagePeriod, String, UsageBucket), ApiError> {
fn usage_window(
period: UsagePeriod,
) -> Result<(UsagePeriod, String, String, UsageBucket), ApiError> {
let now = OffsetDateTime::now_utc();
let (start, bucket) = match period {
UsagePeriod::Last30Minutes => (
@@ -742,13 +1138,14 @@ fn usage_window(period: UsagePeriod) -> Result<(UsagePeriod, String, UsageBucket
),
};
Ok((
period,
start
.format(&Rfc3339)
.map_err(|error| ApiError::internal(error.to_string()))?,
bucket,
))
let created_after = start
.format(&Rfc3339)
.map_err(|error| ApiError::internal(error.to_string()))?;
let created_before = now
.format(&Rfc3339)
.map_err(|error| ApiError::internal(error.to_string()))?;
Ok((period, created_after, created_before, bucket))
}
fn today_start_utc() -> Result<String, ApiError> {
@@ -806,6 +1203,7 @@ fn map_agent_summary_view(summary: AgentSummary) -> AgentSummaryView {
status: summary.status,
current_draft_version: summary.current_draft_version,
latest_published_version: summary.latest_published_version,
catalog_revision: summary.catalog_revision,
created_at: format_timestamp(summary.created_at),
updated_at: format_timestamp(summary.updated_at),
published_at: summary.published_at.map(format_timestamp),
@@ -818,10 +1216,24 @@ fn map_agent_summary_view(summary: AgentSummary) -> AgentSummaryView {
}
}
fn agent_mcp_endpoint(workspace_slug: &str, agent_slug: &str) -> String {
pub(crate) fn agent_mcp_endpoint(workspace_slug: &str, agent_slug: &str) -> String {
format!("/mcp/v1/{workspace_slug}/{agent_slug}")
}
impl AdminService {
pub(crate) fn public_agent_mcp_endpoint(
&self,
workspace_slug: &str,
agent_slug: &str,
) -> String {
format!(
"{}{}",
self.public_base_url,
agent_mcp_endpoint(workspace_slug, agent_slug)
)
}
}
fn tool_quality_schema_node(schema: &Schema) -> ToolQualitySchemaNode {
ToolQualitySchemaNode {
kind: tool_quality_schema_kind(&schema.kind),
@@ -935,6 +1347,7 @@ mod tests {
category: InvocationHistoryLossCategory::InvalidRecord,
}),
Some("req_admin_dc08"),
Some("0af7651916cd43dd8448eb211c80319c"),
InvocationStatus::Error,
InvocationSource::AgentToolCall,
);
@@ -1008,6 +1421,7 @@ fn enrich_operation_summary(
status: summary.status,
current_draft_version: summary.current_draft_version,
latest_published_version: summary.latest_published_version,
can_delete: summary.can_delete,
created_at: format_timestamp(summary.created_at),
updated_at: format_timestamp(summary.updated_at),
published_at: summary.published_at.map(format_timestamp),
+312 -118
View File
@@ -1,15 +1,18 @@
use std::collections::BTreeMap;
use std::collections::{BTreeMap, BTreeSet};
use crank_core::ids::{PromptId, ResourceId};
use crank_core::{
Agent, AgentId, AgentOperationBinding, AgentStatus, AgentVersion, OperationId, SearchableTool,
ToolAccessMode, ToolSelectionPolicy, UsagePeriod, WorkspaceId, search_tool_catalog,
};
use crank_registry::{
AgentVersionRecord, CreateAgentDraftVersionRequest, CreateAgentRequest, PublishAgentRequest,
SaveAgentBindingsRequest, SaveAgentCatalogConfigRequest, UsageBucket, UsageQuery,
AgentResourceBinding, AgentStateExpectation, AgentVersionRecord, CreateAgentRequest,
PublishAgentRequest, SaveAgentCatalogConfigRequest, UpdateAgentSummaryRequest, UsageBucket,
UsageQuery, model::AgentPromptBinding,
};
use serde_json::json;
use time::OffsetDateTime;
use sha2::{Digest, Sha256};
use time::{OffsetDateTime, format_description::well_known::Rfc3339};
use tracing::{info, instrument};
use crate::{
@@ -17,12 +20,50 @@ use crate::{
service::{
AdminService, AgentCatalogPayload, AgentMutationResult, AgentPayload, AgentSummaryView,
CreatedAgentResponse, PublishAgentResponse, ToolSearchPreviewPayload, UpdateAgentPayload,
agent_mcp_endpoint, format_timestamp, map_agent_summary_view, new_prefixed_id,
agent_mcp_endpoint, format_timestamp, map_agent_summary_view, new_prefixed_id, now_string,
today_start_utc,
},
};
impl AdminService {
pub fn agent_state_etag(agent: &AgentSummaryView) -> String {
let policy =
serde_json::to_string(&agent.tool_selection_policy).unwrap_or_else(|_| "{}".to_owned());
let operation_ids = agent.operation_ids.join(",");
let material = format!(
"agent-state-v2\0{}\0{}\0{}\0{}\0{}\0{}\0{}\0{:?}\0{:?}\0{}\0{}\0{}\0{}\0{}",
agent.workspace_id,
agent.id,
agent.slug,
agent.display_name,
agent.description,
agent.updated_at,
agent.current_draft_version,
agent.status,
agent.latest_published_version,
agent.catalog_revision,
agent.operation_count,
operation_ids,
policy,
agent.key_count
);
format!("\"{:x}\"", Sha256::digest(material.as_bytes()))
}
pub fn agent_state_expectation(
agent: &AgentSummaryView,
) -> Result<AgentStateExpectation, ApiError> {
let updated_at = OffsetDateTime::parse(&agent.updated_at, &Rfc3339)
.map_err(|_| ApiError::internal("invalid agent state timestamp"))?;
Ok(AgentStateExpectation {
status: agent.status,
current_draft_version: agent.current_draft_version,
latest_published_version: agent.latest_published_version,
catalog_revision: agent.catalog_revision,
updated_at,
})
}
#[instrument(skip(self, payload), fields(workspace_id = %workspace_id.as_str()))]
pub async fn preview_tool_search(
&self,
@@ -110,13 +151,16 @@ impl AdminService {
self.ensure_workspace_exists(workspace_id).await?;
let workspace = self.get_workspace(workspace_id).await?;
let summaries = self.registry.list_agents(workspace_id).await?;
let usage_start = today_start_utc()?;
let usage_end = now_string()?;
let usage = self
.registry
.list_usage_by_agent(UsageQuery {
workspace_id,
period: UsagePeriod::Last24Hours,
source: None,
created_after: &today_start_utc()?,
created_after: &usage_start,
created_before: &usage_end,
bucket: UsageBucket::Hour,
})
.await?;
@@ -189,6 +233,8 @@ impl AdminService {
.iter()
.map(|binding| binding.operation_id.as_str().to_owned())
.collect::<Vec<_>>();
let usage_start = today_start_utc()?;
let usage_end = now_string()?;
let usage = self
.registry
.get_usage_for_agent(
@@ -196,7 +242,8 @@ impl AdminService {
workspace_id,
period: UsagePeriod::Last24Hours,
source: None,
created_after: &today_start_utc()?,
created_after: &usage_start,
created_before: &usage_end,
bucket: UsageBucket::Hour,
},
agent_id,
@@ -317,6 +364,7 @@ impl AdminService {
workspace_id: &WorkspaceId,
agent_id: &AgentId,
payload: UpdateAgentPayload,
expected_state: Option<&AgentStateExpectation>,
) -> Result<AgentMutationResult, ApiError> {
let existing = self
.registry
@@ -328,6 +376,20 @@ impl AdminService {
json!({ "agent_id": agent_id.as_str() }),
)
})?;
if existing.status == AgentStatus::Published
&& (payload.slug != existing.slug
|| payload.display_name != existing.display_name
|| payload.description != existing.description)
{
return Err(ApiError::conflict_with_context(
"published Agent summary is immutable; unpublish before editing",
json!({
"agent_id": agent_id.as_str(),
"error_code": "agent_published_summary_immutable",
"recovery": "unpublish_edit_publish"
}),
));
}
if payload.slug != existing.slug
&& self
@@ -346,10 +408,13 @@ impl AdminService {
.update_agent_summary(
workspace_id,
agent_id,
&payload.slug,
&payload.display_name,
&payload.description,
&updated_at,
UpdateAgentSummaryRequest {
slug: &payload.slug,
display_name: &payload.display_name,
description: &payload.description,
updated_at: &updated_at,
expected_state,
},
)
.await?;
@@ -365,6 +430,7 @@ impl AdminService {
&self,
workspace_id: &WorkspaceId,
agent_id: &AgentId,
expected_state: Option<&AgentStateExpectation>,
) -> Result<AgentMutationResult, ApiError> {
let existing = self
.registry
@@ -376,8 +442,20 @@ impl AdminService {
json!({ "agent_id": agent_id.as_str() }),
)
})?;
if existing.latest_published_version.is_some() {
return Err(ApiError::conflict_with_context(
"published Agent cannot be deleted; archive or unpublish it instead",
json!({
"agent_id": agent_id.as_str(),
"error_code": "agent_delete_forbidden",
"recovery": "archive_or_unpublish"
}),
));
}
self.registry.delete_agent(workspace_id, agent_id).await?;
self.registry
.delete_agent(workspace_id, agent_id, expected_state)
.await?;
Ok(AgentMutationResult {
agent_id: agent_id.as_str().to_owned(),
@@ -392,11 +470,14 @@ impl AdminService {
workspace_id: &WorkspaceId,
agent_id: &AgentId,
payload: AgentCatalogPayload,
expected_state: Option<&AgentStateExpectation>,
) -> Result<AgentVersionRecord, ApiError> {
let agent = self.get_agent(workspace_id, agent_id).await?;
let current_version = self
.ensure_editable_agent_version(workspace_id, agent_id)
.get_agent_version(workspace_id, agent_id, agent.current_draft_version)
.await?;
let (payload, requested_policy) = payload.into_parts();
let (payload, requested_policy, requested_resource_bindings, requested_prompt_bindings) =
payload.into_parts();
let bindings = payload
.into_iter()
.map(|binding| AgentOperationBinding {
@@ -408,63 +489,68 @@ impl AdminService {
enabled: binding.enabled,
})
.collect::<Vec<_>>();
// Tri-state additive-поля: отсутствие сохраняет прежние Resource bindings,
// `[]` очищает, список заменяет их в Draft.
let resource_bindings = match requested_resource_bindings {
Some(items) => items
.into_iter()
.map(|binding| AgentResourceBinding {
resource_id: ResourceId::new(binding.resource_id),
resource_version: binding.resource_version,
})
.collect::<Vec<_>>(),
None => current_version.resource_bindings.clone(),
};
validate_resource_binding_shape(&resource_bindings)?;
// Tri-state exact Prompt bindings: отсутствие сохраняет, `[]` очищает,
// список заменяет (Story 3.13 AC2).
let prompt_bindings = match requested_prompt_bindings {
Some(items) => items
.into_iter()
.map(|binding| AgentPromptBinding {
prompt_id: PromptId::new(binding.prompt_id),
prompt_version: binding.prompt_version,
})
.collect::<Vec<_>>(),
None => current_version.prompt_bindings.clone(),
};
validate_prompt_binding_shape(&prompt_bindings)?;
self.validate_exact_published_agent_bindings(workspace_id, &bindings)
.await?;
self.validate_exact_published_resource_bindings(workspace_id, &resource_bindings)
.await?;
self.validate_exact_published_prompt_bindings(workspace_id, &prompt_bindings)
.await?;
let tool_selection_policy = requested_policy
.unwrap_or_else(|| current_version.snapshot.tool_selection_policy.clone());
validate_tool_selection_policy(&tool_selection_policy, &bindings)?;
self.registry
.save_agent_catalog_config(SaveAgentCatalogConfigRequest {
workspace_id,
agent_id,
agent_version: current_version.version,
bindings: &bindings,
tool_selection_policy: &tool_selection_policy,
})
let saved_version = self
.registry
.save_agent_catalog_config_editable_with_prompts(
SaveAgentCatalogConfigRequest {
workspace_id,
agent_id,
agent_version: current_version.version,
bindings: &bindings,
tool_selection_policy: &tool_selection_policy,
resource_bindings: &resource_bindings,
expected_state,
},
&prompt_bindings,
)
.await?;
info!(
name: "admin.agent.bindings_saved",
agent_id = %agent_id.as_str(),
version = current_version.version,
version = saved_version,
binding_count = bindings.len(),
resource_binding_count = resource_bindings.len(),
prompt_binding_count = prompt_bindings.len(),
"agent bindings saved"
);
self.get_agent_version(workspace_id, agent_id, current_version.version)
.await
}
async fn ensure_editable_agent_version(
&self,
workspace_id: &WorkspaceId,
agent_id: &AgentId,
) -> Result<AgentVersionRecord, ApiError> {
let agent = self.get_agent(workspace_id, agent_id).await?;
let current = self
.get_agent_version(workspace_id, agent_id, agent.current_draft_version)
.await?;
if agent.latest_published_version != Some(agent.current_draft_version) {
return Ok(current);
}
let now = OffsetDateTime::now_utc();
let draft = AgentVersion {
agent_id: agent_id.clone(),
version: current.version + 1,
status: AgentStatus::Draft,
instructions: current.snapshot.instructions.clone(),
tool_selection_policy: current.snapshot.tool_selection_policy.clone(),
created_at: now,
};
self.registry
.create_agent_draft_version(CreateAgentDraftVersionRequest {
workspace_id,
agent_id,
version: &draft,
bindings: &current.bindings,
updated_at: &now,
})
.await?;
self.get_agent_version(workspace_id, agent_id, draft.version)
self.get_agent_version(workspace_id, agent_id, saved_version)
.await
}
@@ -474,59 +560,48 @@ impl AdminService {
workspace_id: &WorkspaceId,
agent_id: &AgentId,
version: u32,
expected_state: Option<&AgentStateExpectation>,
) -> Result<PublishAgentResponse, ApiError> {
let agent_version = self
.get_agent_version(workspace_id, agent_id, version)
.await?;
let published_bindings = self
.published_agent_bindings(workspace_id, &agent_version.bindings)
.await?;
validate_tool_selection_policy(
&agent_version.snapshot.tool_selection_policy,
&published_bindings,
&agent_version.bindings,
)?;
if published_bindings.is_empty() {
// Resource-only Agent валиден: публикация требует хотя бы один enabled Tool
// ИЛИ хотя бы один exact Resource binding.
if agent_version
.bindings
.iter()
.filter(|binding| binding.enabled)
.count()
== 0
&& agent_version.resource_bindings.is_empty()
{
return Err(ApiError::conflict_with_context(
"agent cannot be published without published enabled tools",
"agent cannot be published without published enabled tools or Resource bindings",
json!({
"agent_id": agent_id.as_str(),
"version": version,
"binding_count": agent_version.bindings.len()
"binding_count": agent_version.bindings.len(),
"resource_binding_count": agent_version.resource_bindings.len()
}),
));
}
self.validate_exact_published_agent_bindings(workspace_id, &agent_version.bindings)
.await?;
self.validate_exact_published_resource_bindings(
workspace_id,
&agent_version.resource_bindings,
)
.await?;
self.validate_exact_published_prompt_bindings(workspace_id, &agent_version.prompt_bindings)
.await?;
let published_at = OffsetDateTime::now_utc();
if published_bindings != agent_version.bindings {
let draft_version = AgentVersion {
agent_id: agent_id.clone(),
version: agent_version.version + 1,
status: AgentStatus::Draft,
instructions: agent_version.snapshot.instructions.clone(),
tool_selection_policy: agent_version.snapshot.tool_selection_policy.clone(),
created_at: published_at,
};
self.registry
.create_agent_draft_version(CreateAgentDraftVersionRequest {
workspace_id,
agent_id,
version: &draft_version,
bindings: &agent_version.bindings,
updated_at: &published_at,
})
.await?;
self.registry
.save_agent_bindings(SaveAgentBindingsRequest {
workspace_id,
agent_id,
agent_version: agent_version.version,
bindings: &published_bindings,
})
.await?;
}
self.registry
.publish_agent(PublishAgentRequest {
@@ -535,6 +610,7 @@ impl AdminService {
version,
published_at: &published_at,
published_by: None,
expected_state,
})
.await?;
info!(
@@ -552,41 +628,106 @@ impl AdminService {
})
}
async fn published_agent_bindings(
async fn validate_exact_published_agent_bindings(
&self,
workspace_id: &WorkspaceId,
bindings: &[AgentOperationBinding],
) -> Result<Vec<AgentOperationBinding>, ApiError> {
let mut published = Vec::new();
) -> Result<(), ApiError> {
for binding in bindings {
if !binding.enabled {
continue;
}
let Some(summary) = self
.registry
.get_operation_summary(workspace_id, &binding.operation_id)
.await?
else {
continue;
return Err(ApiError::not_found_with_context(
"operation was not found for Agent binding",
json!({
"error_code": "agent_binding_scope_denied",
"operation_id": binding.operation_id.as_str()
}),
));
};
if summary.status == crank_core::OperationStatus::Archived {
return Err(ApiError::unprocessable_with_context(
"archived operation cannot be added to an Agent binding",
json!({
"error_code": "agent_binding_archived_operation",
"operation_id": binding.operation_id.as_str()
}),
));
}
let Some(operation_version) = summary.latest_published_version else {
continue;
let Some(version) = self
.registry
.get_operation_version(
workspace_id,
&binding.operation_id,
binding.operation_version,
)
.await?
else {
return Err(ApiError::unprocessable_with_context(
"operation version is not published for Agent binding",
json!({
"error_code": "agent_binding_not_published",
"operation_id": binding.operation_id.as_str(),
"operation_version": binding.operation_version
}),
));
};
published.push(AgentOperationBinding {
operation_id: summary.id,
operation_version,
tool_name: binding.tool_name.clone(),
tool_title: binding.tool_title.clone(),
tool_description_override: binding.tool_description_override.clone(),
enabled: true,
});
if !version.snapshot.is_published() {
return Err(ApiError::unprocessable_with_context(
"operation version is not published for Agent binding",
json!({
"error_code": "agent_binding_not_published",
"operation_id": binding.operation_id.as_str(),
"operation_version": binding.operation_version
}),
));
}
}
Ok(())
}
Ok(published)
/// Точная published Resource Version обязана существовать в этом workspace.
/// Чужая/неопубликованная/отсутствующая версии дают один и тот же sanitized 404
/// без раскрытия существования.
async fn validate_exact_published_resource_bindings(
&self,
workspace_id: &WorkspaceId,
bindings: &[AgentResourceBinding],
) -> Result<(), ApiError> {
let unresolved = self
.registry
.unresolved_published_resource_versions(workspace_id, bindings)
.await?;
if unresolved.is_empty() {
return Ok(());
}
Err(ApiError::not_found_with_context(
"published resource version was not found for Agent binding",
json!({ "error_code": "agent_resource_binding_not_found" }),
))
}
/// Exact Published Prompt Version обязана существовать в этом workspace;
/// отсутствующая, чужая и неопубликованная версии неразличимы снаружи.
async fn validate_exact_published_prompt_bindings(
&self,
workspace_id: &WorkspaceId,
bindings: &[AgentPromptBinding],
) -> Result<(), ApiError> {
let unresolved = self
.registry
.unresolved_published_prompt_versions(workspace_id, bindings)
.await?;
if unresolved.is_empty() {
return Ok(());
}
Err(ApiError::not_found_with_context(
"published prompt version was not found for Agent binding",
json!({ "error_code": "agent_prompt_binding_not_found" }),
))
}
#[instrument(skip(self), fields(workspace_id = %workspace_id.as_str(), agent_id = %agent_id.as_str()))]
@@ -594,11 +735,12 @@ impl AdminService {
&self,
workspace_id: &WorkspaceId,
agent_id: &AgentId,
expected_state: Option<&AgentStateExpectation>,
) -> Result<AgentMutationResult, ApiError> {
self.ensure_workspace_exists(workspace_id).await?;
let updated_at = OffsetDateTime::now_utc();
self.registry
.unpublish_agent(workspace_id, agent_id, &updated_at)
.unpublish_agent(workspace_id, agent_id, &updated_at, expected_state)
.await?;
info!(
name: "admin.agent.unpublished",
@@ -618,11 +760,12 @@ impl AdminService {
&self,
workspace_id: &WorkspaceId,
agent_id: &AgentId,
expected_state: Option<&AgentStateExpectation>,
) -> Result<AgentMutationResult, ApiError> {
self.ensure_workspace_exists(workspace_id).await?;
let updated_at = OffsetDateTime::now_utc();
self.registry
.archive_agent(workspace_id, agent_id, &updated_at)
.archive_agent(workspace_id, agent_id, &updated_at, expected_state)
.await?;
info!(
name: "admin.agent.archived",
@@ -638,6 +781,57 @@ impl AdminService {
}
}
fn validate_resource_binding_shape(bindings: &[AgentResourceBinding]) -> Result<(), ApiError> {
let mut seen = BTreeSet::new();
for binding in bindings {
if binding.resource_version == 0 {
return Err(ApiError::validation_with_context(
"resource_version must be a positive exact integer",
json!({
"field": "resource_bindings.resource_version",
"resource_id": binding.resource_id.as_str(),
}),
));
}
if !seen.insert((binding.resource_id.as_str(), binding.resource_version)) {
return Err(ApiError::validation_with_context(
"duplicate exact Resource binding is not allowed",
json!({
"field": "resource_bindings",
"resource_id": binding.resource_id.as_str(),
"resource_version": binding.resource_version,
}),
));
}
}
Ok(())
}
fn validate_prompt_binding_shape(bindings: &[AgentPromptBinding]) -> Result<(), ApiError> {
let mut seen = BTreeSet::new();
for binding in bindings {
if binding.prompt_version == 0 {
return Err(ApiError::validation_with_context(
"prompt_version must be a positive exact integer",
json!({
"field": "prompt_bindings.prompt_version",
"prompt_id": binding.prompt_id.as_str(),
}),
));
}
if !seen.insert(binding.prompt_id.as_str()) {
return Err(ApiError::validation_with_context(
"duplicate exact Prompt binding is not allowed",
json!({
"field": "prompt_bindings",
"prompt_id": binding.prompt_id.as_str(),
}),
));
}
}
Ok(())
}
fn validate_tool_selection_policy(
policy: &ToolSelectionPolicy,
bindings: &[AgentOperationBinding],
+300 -28
View File
@@ -6,12 +6,14 @@ use crank_registry::{CreatePlatformApiKeyRequest, PlatformApiKeyRecord};
use serde_json::json;
use time::{OffsetDateTime, format_description::well_known::Rfc3339};
use tracing::instrument;
use url::Url;
use crate::{
error::ApiError,
service::{
AdminService, CreatedPlatformApiKeyResponse, PlatformApiKeyPayload, generate_access_secret,
hash_access_secret, new_prefixed_id,
AdminAuditContext, AdminService, CreatedPlatformApiKeyResponse, CredentialAuditRecord,
EphemeralMcpClientConfig, EphemeralMcpConnection, PlatformApiKeyPayload,
generate_access_secret, hash_access_secret, new_prefixed_id,
},
};
@@ -38,32 +40,84 @@ impl AdminService {
.await?)
}
#[instrument(skip(self, payload), fields(workspace_id = %workspace_id.as_str(), agent_id = %agent_id.as_str(), key_name = %payload.name))]
#[instrument(skip(self, payload, audit_context), fields(workspace_id = %workspace_id.as_str(), agent_id = %agent_id.as_str(), key_name = %payload.name))]
pub async fn create_agent_platform_api_key(
&self,
workspace_id: &WorkspaceId,
agent_id: &AgentId,
payload: PlatformApiKeyPayload,
mut payload: PlatformApiKeyPayload,
audit_context: Option<&AdminAuditContext>,
) -> Result<CreatedPlatformApiKeyResponse, ApiError> {
self.ensure_workspace_exists(workspace_id).await?;
self.registry
let workspace = match self.get_workspace(workspace_id).await {
Ok(workspace) => workspace,
Err(error) => {
self.record_credential_audit(
audit_context,
CredentialAuditRecord {
action: "credential.platform_api_key.create_failed",
target_kind: crank_core::AuditTargetKind::PlatformApiKey,
workspace_id,
target_id: "pending",
credential_type: "platform_api_key",
outcome: "failure",
reason: error.code(),
},
)
.await;
return Err(error);
}
};
let agent_result = match self
.registry
.get_agent_summary(workspace_id, agent_id)
.await?
.ok_or_else(|| {
.await
{
Ok(agent) => agent.ok_or_else(|| {
ApiError::not_found_with_context(
format!("agent {} was not found", agent_id.as_str()),
json!({ "agent_id": agent_id.as_str() }),
)
})?;
}),
Err(error) => Err(ApiError::from(error)),
};
let agent = match agent_result {
Ok(agent) => agent,
Err(error) => {
self.record_credential_audit(
audit_context,
CredentialAuditRecord {
action: "credential.platform_api_key.create_failed",
target_kind: crank_core::AuditTargetKind::PlatformApiKey,
workspace_id,
target_id: "pending",
credential_type: "platform_api_key",
outcome: "failure",
reason: error.code(),
},
)
.await;
return Err(error);
}
};
validate_platform_api_key_payload(&payload)?;
let expires_at = match payload.expires_at.as_deref() {
Some(value) => Some(
OffsetDateTime::parse(value, &Rfc3339)
.map_err(|_| ApiError::validation("expires_at must be RFC3339 timestamp"))?,
),
None => None,
let expires_at = match validate_platform_api_key_payload(&mut payload) {
Ok(expires_at) => expires_at,
Err(error) => {
self.record_credential_audit(
audit_context,
CredentialAuditRecord {
action: "credential.platform_api_key.create_failed",
target_kind: crank_core::AuditTargetKind::PlatformApiKey,
workspace_id,
target_id: "pending",
credential_type: payload.key_kind.audit_credential_type(),
outcome: "failure",
reason: error.code(),
},
)
.await;
return Err(error);
}
};
let secret = generate_access_secret(payload.key_kind.secret_marker());
let api_key = PlatformApiKeyRecord {
@@ -83,55 +137,213 @@ impl AdminService {
},
};
self.registry
if let Err(error) = self
.registry
.create_platform_api_key(CreatePlatformApiKeyRequest {
api_key: &api_key.api_key,
secret_hash: &hash_access_secret(&secret),
})
.await?;
.await
{
let error = ApiError::from(error);
self.record_credential_audit(
audit_context,
CredentialAuditRecord {
action: "credential.platform_api_key.create_failed",
target_kind: crank_core::AuditTargetKind::PlatformApiKey,
workspace_id,
target_id: api_key.api_key.id.as_str(),
credential_type: api_key.api_key.key_kind.audit_credential_type(),
outcome: "failure",
reason: error.code(),
},
)
.await;
return Err(error);
}
self.record_credential_audit(
audit_context,
CredentialAuditRecord {
action: "credential.platform_api_key.created",
target_kind: crank_core::AuditTargetKind::PlatformApiKey,
workspace_id,
target_id: api_key.api_key.id.as_str(),
credential_type: api_key.api_key.key_kind.audit_credential_type(),
outcome: "success",
reason: "credential_created",
},
)
.await;
Ok(CreatedPlatformApiKeyResponse { api_key, secret })
let connection = (api_key.api_key.key_kind == PlatformApiKeyKind::McpClient).then(|| {
let endpoint = self.public_agent_mcp_endpoint(&workspace.workspace.slug, &agent.slug);
EphemeralMcpConnection {
endpoint: endpoint.clone(),
clients: ephemeral_client_configs(&endpoint, &secret),
secret_display: "once",
}
});
Ok(CreatedPlatformApiKeyResponse {
api_key,
secret,
connection,
})
}
#[instrument(skip(self), fields(workspace_id = %workspace_id.as_str(), agent_id = %agent_id.as_str(), key_id = %key_id.as_str()))]
#[instrument(skip(self, audit_context), fields(workspace_id = %workspace_id.as_str(), agent_id = %agent_id.as_str(), key_id = %key_id.as_str()))]
pub async fn revoke_agent_platform_api_key(
&self,
workspace_id: &WorkspaceId,
agent_id: &AgentId,
key_id: &PlatformApiKeyId,
audit_context: Option<&AdminAuditContext>,
) -> Result<(), ApiError> {
self.registry
if let Err(error) = self
.registry
.revoke_platform_api_key_for_agent(
workspace_id,
agent_id,
key_id,
&OffsetDateTime::now_utc(),
)
.await?;
.await
{
let error = ApiError::from(error);
self.record_credential_audit(
audit_context,
CredentialAuditRecord {
action: "credential.platform_api_key.revoke_failed",
target_kind: crank_core::AuditTargetKind::PlatformApiKey,
workspace_id,
target_id: key_id.as_str(),
credential_type: "platform_api_key",
outcome: "failure",
reason: error.code(),
},
)
.await;
return Err(error);
}
self.record_credential_audit(
audit_context,
CredentialAuditRecord {
action: "credential.platform_api_key.revoked",
target_kind: crank_core::AuditTargetKind::PlatformApiKey,
workspace_id,
target_id: key_id.as_str(),
credential_type: "platform_api_key",
outcome: "success",
reason: "credential_revoked",
},
)
.await;
Ok(())
}
#[instrument(skip(self), fields(workspace_id = %workspace_id.as_str(), agent_id = %agent_id.as_str(), key_id = %key_id.as_str()))]
#[instrument(skip(self, audit_context), fields(workspace_id = %workspace_id.as_str(), agent_id = %agent_id.as_str(), key_id = %key_id.as_str()))]
pub async fn delete_agent_platform_api_key(
&self,
workspace_id: &WorkspaceId,
agent_id: &AgentId,
key_id: &PlatformApiKeyId,
audit_context: Option<&AdminAuditContext>,
) -> Result<(), ApiError> {
self.registry
if let Err(error) = self
.registry
.delete_platform_api_key_for_agent(workspace_id, agent_id, key_id)
.await?;
.await
{
let error = ApiError::from(error);
self.record_credential_audit(
audit_context,
CredentialAuditRecord {
action: "credential.platform_api_key.delete_failed",
target_kind: crank_core::AuditTargetKind::PlatformApiKey,
workspace_id,
target_id: key_id.as_str(),
credential_type: "platform_api_key",
outcome: "failure",
reason: error.code(),
},
)
.await;
return Err(error);
}
self.record_credential_audit(
audit_context,
CredentialAuditRecord {
action: "credential.platform_api_key.deleted",
target_kind: crank_core::AuditTargetKind::PlatformApiKey,
workspace_id,
target_id: key_id.as_str(),
credential_type: "platform_api_key",
outcome: "success",
reason: "credential_deleted",
},
)
.await;
Ok(())
}
}
fn validate_platform_api_key_payload(payload: &PlatformApiKeyPayload) -> Result<(), ApiError> {
fn ephemeral_client_configs(endpoint: &str, secret: &str) -> Vec<EphemeralMcpClientConfig> {
["claude_desktop", "cursor", "vscode"]
.into_iter()
.map(|client| EphemeralMcpClientConfig {
client: client.to_owned(),
config: json!({
"transport": "streamable_http",
"url": endpoint,
"headers": {"Authorization": format!("Bearer {secret}")}
}),
})
.collect()
}
trait PlatformApiKeyKindAuditExt {
fn audit_credential_type(self) -> &'static str;
}
impl PlatformApiKeyKindAuditExt for PlatformApiKeyKind {
fn audit_credential_type(self) -> &'static str {
match self {
PlatformApiKeyKind::McpClient => "mcp_client_key",
PlatformApiKeyKind::Approval => "approval_key",
}
}
}
fn validate_platform_api_key_payload(
payload: &mut PlatformApiKeyPayload,
) -> Result<Option<OffsetDateTime>, ApiError> {
const MAX_KEY_NAME_CHARS: usize = 128;
const MAX_SCOPES: usize = 8;
payload.name = payload.name.trim().to_owned();
if payload.name.trim().is_empty() {
return Err(ApiError::validation("key name is required"));
}
if payload.name.chars().count() > MAX_KEY_NAME_CHARS {
return Err(ApiError::validation(
"key name must be at most 128 characters",
));
}
if payload.scopes.is_empty() {
return Err(ApiError::validation("at least one key scope is required"));
}
if payload.scopes.len() > MAX_SCOPES {
return Err(ApiError::validation("too many key scopes"));
}
if payload
.scopes
.iter()
.enumerate()
.any(|(index, scope)| payload.scopes[..index].contains(scope))
{
return Err(ApiError::validation(
"key scopes must not contain duplicates",
));
}
let valid = payload.scopes.iter().all(|scope| match payload.key_kind {
PlatformApiKeyKind::McpClient => matches!(
@@ -156,6 +368,66 @@ fn validate_platform_api_key_payload(payload: &PlatformApiKeyPayload) -> Result<
"approval key can contain at most 20 allowed origins",
));
}
if payload.key_kind == PlatformApiKeyKind::Approval {
let mut normalized_origins = Vec::with_capacity(payload.allowed_origins.len());
for origin in &payload.allowed_origins {
normalized_origins.push(validate_approval_origin(origin)?);
}
if normalized_origins
.iter()
.enumerate()
.any(|(index, origin)| normalized_origins[..index].contains(origin))
{
return Err(ApiError::validation(
"allowed origins must not contain duplicates",
));
}
payload.allowed_origins = normalized_origins;
} else if !payload.allowed_origins.is_empty() {
return Err(ApiError::validation(
"allowed origins are only supported for approval keys",
));
}
Ok(())
let expires_at = payload
.expires_at
.as_deref()
.map(|value| {
OffsetDateTime::parse(value, &Rfc3339)
.map_err(|_| ApiError::validation("expires_at must be RFC3339 timestamp"))
})
.transpose()?;
if expires_at.is_some_and(|value| value <= OffsetDateTime::now_utc()) {
return Err(ApiError::validation("expires_at must be in the future"));
}
Ok(expires_at)
}
fn validate_approval_origin(origin: &str) -> Result<String, ApiError> {
const MAX_ORIGIN_LEN: usize = 2048;
if origin.is_empty() || origin.len() > MAX_ORIGIN_LEN {
return Err(ApiError::validation("allowed origin is invalid"));
}
if origin
.bytes()
.any(|byte| byte.is_ascii_control() || byte.is_ascii_whitespace())
{
return Err(ApiError::validation("allowed origin is invalid"));
}
let parsed =
Url::parse(origin).map_err(|_| ApiError::validation("allowed origin is invalid"))?;
if !matches!(parsed.scheme(), "http" | "https")
|| parsed.host_str().is_none()
|| !parsed.username().is_empty()
|| parsed.password().is_some()
|| parsed.path() != "/"
|| parsed.query().is_some()
|| parsed.fragment().is_some()
{
return Err(ApiError::validation("allowed origin is invalid"));
}
Ok(parsed.origin().ascii_serialization())
}
+238 -40
View File
@@ -1,20 +1,101 @@
use crank_core::{LoginOutcome, MembershipRole, UserSessionId, WorkspaceId};
use crank_core::{
LoginOutcome, MembershipRole, User, UserId, UserSessionId, UserStatus, WorkspaceId,
};
use crank_registry::{AdminSecurityAuditRequest, ConsumeAdminBootstrapContractRequest};
use serde_json::json;
use time::OffsetDateTime;
use tracing::instrument;
use crate::{
auth::{
AuthenticatedSession, SessionCookie, create_session_cookie, hash_password,
hash_session_secret, verify_password,
AuthenticatedSession, SessionCookie, create_csrf_token_value, create_session_cookie,
hash_csrf_token, hash_password, hash_session_secret, verify_password,
},
error::ApiError,
service::{
AdminService, ChangePasswordPayload, LoginPayload, SessionResponse, UpdateProfilePayload,
map_identity_error, validate_profile_display_name, validate_profile_email,
AdminService, BootstrapStatusResponse, ChangePasswordPayload, CompleteBootstrapPayload,
LoginPayload, SessionResponse, UpdateProfilePayload, hash_access_secret,
map_identity_error, new_prefixed_id, validate_profile_display_name, validate_profile_email,
},
};
impl AdminService {
pub async fn bootstrap_status(&self) -> Result<BootstrapStatusResponse, ApiError> {
Ok(BootstrapStatusResponse {
bootstrap_required: !self.registry.has_password_admin().await?,
})
}
pub async fn complete_bootstrap(
&self,
payload: CompleteBootstrapPayload,
) -> Result<(SessionCookie, SessionResponse), ApiError> {
if payload.token.len() < 32 || payload.token.len() > 256 {
self.record_admin_security_audit(None, "bootstrap_rejected", "rejected", "bootstrap")
.await;
return Err(Self::constant_bootstrap_error());
}
if payload.password.len() < 12 || payload.password.len() > 256 {
self.record_admin_security_audit(None, "bootstrap_rejected", "rejected", "bootstrap")
.await;
return Err(Self::constant_bootstrap_error());
}
let now = OffsetDateTime::now_utc();
let bootstrap_token_hash = hash_access_secret(&format!("bootstrap:{}", payload.token));
if !self
.registry
.admin_bootstrap_contract_is_consumable(&bootstrap_token_hash, &now)
.await?
{
self.record_admin_security_audit(None, "bootstrap_rejected", "rejected", "bootstrap")
.await;
return Err(Self::constant_bootstrap_error());
}
let password_hash = hash_password(&payload.password, &self.auth_settings.password_pepper)?;
let user_id = self
.registry
.consume_admin_bootstrap_contract(ConsumeAdminBootstrapContractRequest {
token_hash: &bootstrap_token_hash,
password_hash: &password_hash,
now: &now,
})
.await
.map_err(|_| Self::constant_bootstrap_error());
let user_id = match user_id {
Ok(user_id) => user_id,
Err(error) => {
self.record_admin_security_audit(
None,
"bootstrap_rejected",
"rejected",
"bootstrap",
)
.await;
return Err(error);
}
};
self.record_admin_security_audit(
Some(&user_id),
"bootstrap_completed",
"success",
"bootstrap",
)
.await;
let user = self
.registry
.get_auth_user_by_id(&user_id)
.await?
.ok_or_else(|| ApiError::internal("bootstrap user was not found"))?
.user;
self.create_session_for_user(user).await
}
fn constant_bootstrap_error() -> ApiError {
ApiError::unauthorized("bootstrap request is invalid or expired")
}
pub async fn bootstrap_admin_user(&self) -> Result<(), ApiError> {
let password_hash = hash_password(
&self.auth_settings.bootstrap_admin.password,
@@ -83,36 +164,99 @@ impl AdminService {
Ok(())
}
pub async fn verify_session_csrf(
&self,
session_id: &UserSessionId,
csrf_hash: &str,
) -> Result<bool, ApiError> {
Ok(self
.registry
.verify_session_csrf(session_id, csrf_hash)
.await?)
}
pub async fn login(
&self,
payload: LoginPayload,
) -> Result<(SessionCookie, SessionResponse), ApiError> {
let authenticated = self.authenticate_login(&payload).await?;
self.login_with_client_bucket(payload, "anonymous").await
}
pub async fn login_with_client_bucket(
&self,
payload: LoginPayload,
client_bucket: &str,
) -> Result<(SessionCookie, SessionResponse), ApiError> {
let scope_hash = hash_access_secret(&format!(
"login:{}:{}",
client_bucket,
payload.email.trim().to_ascii_lowercase()
));
if let Some(locked_until) = self
.registry
.login_backoff_locked_until(&scope_hash)
.await?
{
let retry_after_ms = (locked_until - OffsetDateTime::now_utc())
.whole_milliseconds()
.clamp(1, 300_000);
return Err(ApiError::rate_limited_with_context(
"login temporarily unavailable",
json!({
"retry_after_ms": retry_after_ms,
"error_code": "login_throttled",
"recovery": "retry_after_delay"
}),
));
}
let authenticated = match self.authenticate_login(&payload).await {
Ok(authenticated) => authenticated,
Err(error) => {
let _ = self
.registry
.record_login_failure(&scope_hash, &OffsetDateTime::now_utc())
.await;
self.record_admin_security_audit(None, "login_rejected", "rejected", "login")
.await;
return Err(error);
}
};
self.registry.reset_login_backoff(&scope_hash).await?;
self.record_admin_security_audit(
Some(&authenticated.user.id),
"login_succeeded",
"success",
"login",
)
.await;
self.create_session_for_user(authenticated.user).await
}
async fn create_session_for_user(
&self,
user: User,
) -> Result<(SessionCookie, SessionResponse), ApiError> {
let session_cookie = create_session_cookie(&self.auth_settings)?;
let csrf_token = create_csrf_token_value();
let secret_hash = hash_session_secret(
&session_cookie.session_id,
&session_cookie.value,
&self.auth_settings.session_secret,
);
let memberships = self
.registry
.list_workspaces_for_user(&authenticated.user.id)
.await?;
let csrf_hash = hash_csrf_token(
&session_cookie.session_id,
&csrf_token,
&self.auth_settings.session_secret,
);
let memberships = self.registry.list_workspaces_for_user(&user.id).await?;
let default_workspace_id = memberships
.iter()
.find(|membership| membership.workspace.id.as_str() == "ws_default")
.map(|membership| membership.workspace.id.as_str().to_owned());
let current_workspace_id = default_workspace_id.or_else(|| {
authenticated
.current_workspace_id
.as_ref()
.map(|workspace_id| workspace_id.as_str().to_owned())
.or_else(|| {
memberships
.first()
.map(|membership| membership.workspace.id.as_str().to_owned())
})
memberships
.first()
.map(|membership| membership.workspace.id.as_str().to_owned())
});
let current_workspace_ref = current_workspace_id
.as_ref()
@@ -120,9 +264,10 @@ impl AdminService {
self.registry
.create_user_session(
&session_cookie.session_id,
&authenticated.user.id,
&user.id,
current_workspace_ref.as_ref(),
&secret_hash,
Some(&csrf_hash),
&session_cookie.expires_at,
)
.await?;
@@ -130,9 +275,10 @@ impl AdminService {
Ok((
session_cookie,
SessionResponse {
user: authenticated.user,
user,
memberships,
current_workspace_id,
csrf_token,
},
))
}
@@ -158,7 +304,19 @@ impl AdminService {
.registry
.get_auth_user_by_email(&payload.email)
.await?
.ok_or_else(|| ApiError::unauthorized("invalid email or password"))?;
.ok_or_else(|| {
let _ = hash_password(&payload.password, &self.auth_settings.password_pepper);
ApiError::unauthorized("invalid email or password")
})?;
if user.user.status != UserStatus::Active {
let _ = verify_password(
&payload.password,
&self.auth_settings.password_pepper,
&user.password_hash,
);
return Err(ApiError::unauthorized("invalid email or password"));
}
if !verify_password(
&payload.password,
@@ -181,6 +339,8 @@ impl AdminService {
_session_value: &str,
) -> Result<(), ApiError> {
self.registry.revoke_user_session(session_id).await?;
self.record_admin_security_audit(None, "logout", "success", "session")
.await;
Ok(())
}
@@ -190,21 +350,23 @@ impl AdminService {
session_id: &UserSessionId,
session_value: &str,
) -> Result<Option<SessionResponse>, ApiError> {
Ok(self
.get_session(session_id, session_value)
.await?
.map(|session| SessionResponse {
user: session.user,
memberships: session.memberships,
current_workspace_id: session
.current_workspace_id
.map(|id| id.as_str().to_owned()),
}))
let Some(session) = self.get_session(session_id, session_value).await? else {
return Ok(None);
};
Ok(Some(SessionResponse {
user: session.user,
memberships: session.memberships,
current_workspace_id: session
.current_workspace_id
.map(|id| id.as_str().to_owned()),
csrf_token: String::new(),
}))
}
pub async fn update_profile(
&self,
user_id: &crank_core::UserId,
session_id: &UserSessionId,
current_workspace_id: Option<&WorkspaceId>,
payload: UpdateProfilePayload,
) -> Result<SessionResponse, ApiError> {
@@ -221,18 +383,19 @@ impl AdminService {
user,
memberships,
current_workspace_id: current_workspace_id.map(|id| id.as_str().to_owned()),
csrf_token: self.rotate_session_csrf_token(session_id).await?,
})
}
pub async fn change_password(
&self,
user_id: &crank_core::UserId,
current_session_id: &UserSessionId,
_current_session_id: &UserSessionId,
payload: ChangePasswordPayload,
) -> Result<(), ApiError> {
if payload.new_password.len() < 12 {
if payload.new_password.len() < 12 || payload.new_password.len() > 256 {
return Err(ApiError::validation(
"new password must be at least 12 characters long",
"new password must be between 12 and 256 characters long",
));
}
@@ -258,13 +421,48 @@ impl AdminService {
let password_hash =
hash_password(&payload.new_password, &self.auth_settings.password_pepper)?;
self.registry
.update_user_password_and_revoke_other_sessions(
user_id,
current_session_id,
&password_hash,
)
.update_user_password_and_revoke_all_sessions(user_id, &password_hash)
.await?;
self.record_admin_security_audit(Some(user_id), "password_rotated", "success", "password")
.await;
Ok(())
}
async fn record_admin_security_audit(
&self,
actor_user_id: Option<&UserId>,
action: &'static str,
outcome: &'static str,
source: &'static str,
) {
let (request_id, trace_id) = crank_observability::current_request_correlation();
let audit_id = new_prefixed_id("audit");
let _ = self
.registry
.record_admin_security_audit(AdminSecurityAuditRequest {
id: &audit_id,
action,
outcome,
actor_user_id,
session_id: None,
request_id: request_id.as_deref(),
trace_id: trace_id.as_deref(),
source,
})
.await;
}
pub async fn rotate_session_csrf_token(
&self,
session_id: &UserSessionId,
) -> Result<String, ApiError> {
let csrf_token = create_csrf_token_value();
let csrf_hash =
hash_csrf_token(session_id, &csrf_token, &self.auth_settings.session_secret);
self.registry
.update_user_session_csrf(session_id, &csrf_hash)
.await?;
Ok(csrf_token)
}
}
+67 -12
View File
@@ -1,9 +1,9 @@
use std::collections::BTreeMap;
use crank_core::{
AgentId, InvocationLevel, InvocationSource, InvocationStatus, MembershipRole, OperationId,
OperationSecurityLevel, PlatformApiKeyKind, PlatformApiKeyScope, PlatformApiKeyStatus,
Protocol, Target, WizardState, WorkspaceId,
AgentId, AgentOperationBinding, InvocationLevel, InvocationSource, InvocationStatus,
MembershipRole, OperationId, OperationSecurityLevel, PlatformApiKeyKind, PlatformApiKeyScope,
PlatformApiKeyStatus, Protocol, Target, WizardState, WorkspaceId,
};
use crank_mapping::{JsonPathRoot, infer_mapping_from_samples};
use crank_mapping::{MappingRule, MappingSet};
@@ -87,8 +87,12 @@ impl AdminService {
async fn cleanup_legacy_demo_assets(&self, workspace_id: &WorkspaceId) -> Result<(), ApiError> {
for slug in ["revops-copilot", "support-triage"] {
if let Some(agent) = self.find_agent_by_slug(workspace_id, slug).await? {
self.delete_agent(workspace_id, &AgentId::new(agent.id.as_str().to_owned()))
.await?;
self.delete_agent(
workspace_id,
&AgentId::new(agent.id.as_str().to_owned()),
None,
)
.await?;
}
}
@@ -129,7 +133,10 @@ impl AdminService {
.await
{
Ok(()) => Ok(()),
Err(RegistryError::OperationHasPublishedAgentBindings { .. }) => {
Err(
RegistryError::OperationHasPublishedAgentBindings { .. }
| RegistryError::OperationDeleteForbidden { .. },
) => {
tracing::warn!(
name: "admin.demo_operation.cleanup_skipped",
operation_id = %operation_id.as_str(),
@@ -168,6 +175,7 @@ impl AdminService {
expires_at: None,
allowed_origins: Vec::new(),
},
None,
)
.await?
.api_key
@@ -175,7 +183,7 @@ impl AdminService {
};
if revoke && key.api_key.status != PlatformApiKeyStatus::Revoked {
self.revoke_agent_platform_api_key(workspace_id, agent_id, &key.api_key.id)
self.revoke_agent_platform_api_key(workspace_id, agent_id, &key.api_key.id, None)
.await?;
}
@@ -210,8 +218,17 @@ impl AdminService {
return Ok(());
}
self.publish_operation(workspace_id, &summary.id, summary.current_draft_version)
.await?;
self.publish_operation(
workspace_id,
&summary.id,
summary.current_draft_version,
&crank_registry::OperationStateExpectation {
current_draft_version: summary.current_draft_version,
status: summary.status,
latest_published_version: summary.latest_published_version,
},
)
.await?;
Ok(())
}
@@ -273,10 +290,15 @@ impl AdminService {
publish: bool,
) -> Result<(), ApiError> {
let summary = self.get_agent(workspace_id, agent_id).await?;
self.save_agent_bindings(workspace_id, agent_id, bindings.into())
let current = self
.get_agent_version(workspace_id, agent_id, summary.current_draft_version)
.await?;
if !demo_agent_bindings_match(&current.bindings, &bindings) {
self.save_agent_bindings(workspace_id, agent_id, bindings.into(), None)
.await?;
}
if publish && summary.latest_published_version.is_none() {
self.publish_agent(workspace_id, agent_id, summary.current_draft_version)
self.publish_agent(workspace_id, agent_id, summary.current_draft_version, None)
.await?;
}
@@ -294,11 +316,16 @@ impl AdminService {
.list_invocation_logs(ListInvocationLogsQuery {
workspace_id,
level: None,
status: None,
outcome_group: None,
search_text: None,
source: None,
operation_id: None,
agent_id: None,
created_after: None,
created_before: None,
cursor_created_at: None,
cursor_id: None,
limit: 1,
})
.await?
@@ -316,17 +343,23 @@ impl AdminService {
.current_draft_version,
)
.await?;
let correlation = crank_core::CorrelationContext::generate();
self.record_invocation(InvocationRecordRequest {
workspace_id,
agent_id: Some(currency_agent_id),
operation: &rest_operation.snapshot,
request_id: None,
request_id: Some(correlation.request_id().as_str()),
trace_id: Some(correlation.trace_id().as_str()),
source: InvocationSource::AgentToolCall,
level: InvocationLevel::Info,
status: InvocationStatus::Ok,
message: "Frankfurter returned latest exchange rate".to_owned(),
status_code: Some(200),
error_kind: None,
execution_stage: Some(crank_core::ExecutionStage::Runtime),
execution_error_code: None,
retryability: Some(crank_core::Retryability::Never),
outcome_certainty: Some(crank_core::OutcomeCertainty::Certain),
duration_ms: 124,
request_preview: json!({
"path": {},
@@ -341,6 +374,25 @@ impl AdminService {
}
}
fn demo_agent_bindings_match(
existing: &[AgentOperationBinding],
desired: &[AgentBindingPayload],
) -> bool {
if existing.len() != desired.len() {
return false;
}
let mut desired = desired.iter().collect::<Vec<_>>();
desired.sort_by(|left, right| left.tool_name.cmp(&right.tool_name));
existing.iter().zip(desired).all(|(existing, desired)| {
existing.operation_id.as_str() == desired.operation_id
&& existing.operation_version == desired.operation_version
&& existing.tool_name == desired.tool_name
&& existing.tool_title == desired.tool_title
&& existing.tool_description_override == desired.tool_description_override
&& existing.enabled == desired.enabled
})
}
fn demo_currency_agent_payload() -> AgentPayload {
AgentPayload {
slug: "currency-rates".to_owned(),
@@ -369,6 +421,9 @@ fn demo_rest_operation_payload() -> OperationPayload {
method: crank_core::HttpMethod::Get,
path_template: "/v1/latest".to_owned(),
static_headers: BTreeMap::from([("Accept".to_owned(), "application/json".to_owned())]),
request_contract: None,
response_contract: Vec::new(),
multipart_field_mime_types: BTreeMap::new(),
}),
input_schema: Schema::from_json_sample(&input),
output_schema: Schema::from_json_sample(&output),
+497 -45
View File
@@ -1,16 +1,26 @@
use crank_core::{ConfigExport, WorkspaceId};
use crank_registry::RegistryOperation;
use crank_core::{OperationStatus, Target, WorkspaceId};
use crank_registry::OperationStateExpectation;
use serde::de::{self, Deserialize, Deserializer, MapAccess, SeqAccess, Visitor};
use serde_yaml::Value as YamlValue;
use std::fmt;
use tracing::{info, instrument};
use crate::{
error::ApiError,
import_guidance::import_guidance_warnings,
service::{
AdminService, ExportQuery, ImportMode, ImportQuery, ImportResponse, NewVersionPayload,
OperationPayload, YamlOperationDocument,
AdminService, ExportQuery, ImportMode, ImportQuery, ImportResponse,
LegacyYamlOperationDocument, NewVersionPayload, OperationPayload, PortableOperation,
YamlOperationDocument,
},
};
const MAX_YAML_BYTES: usize = 256 * 1024;
const MAX_YAML_LINE_OR_SCALAR_BYTES: usize = 64 * 1024;
const MAX_YAML_DEPTH: usize = 64;
const MAX_YAML_NODES: usize = 20_000;
const MAX_YAML_COLLECTION_ITEMS: usize = 4_096;
impl AdminService {
#[instrument(skip(self), fields(operation_id = %operation_id.as_str(), version = query.version.unwrap_or_default(), mode = ?query.mode))]
pub async fn export_operation(
@@ -30,19 +40,30 @@ impl AdminService {
let record = self
.get_operation_version(workspace_id, operation_id, version)
.await?;
let document = YamlOperationDocument {
format_version: "1".to_owned(),
kind: "operation".to_owned(),
operation: RegistryOperation {
config_export: Some(ConfigExport {
format_version: "1".to_owned(),
export_mode: query.mode,
let aggregate = self.get_operation(workspace_id, operation_id).await?;
let exportable = record.status == OperationStatus::Published
|| (aggregate.status != OperationStatus::Archived
&& record.status == OperationStatus::Draft
&& aggregate.current_draft_version == version);
if !exportable {
return Err(ApiError::conflict_with_context(
"only the active current Draft or a Published Version can be exported",
serde_json::json!({
"error_code": "operation_invalid_transition",
"version": version
}),
..record.snapshot
},
));
}
let operation = PortableOperation::from_registry(&record.snapshot);
reject_credential_material(&operation)?;
let document = YamlOperationDocument {
format_version: "2".to_owned(),
kind: "operation".to_owned(),
operation,
};
serde_yaml::to_string(&document).map_err(|error| ApiError::internal(error.to_string()))
serde_yaml::to_string(&document)
.map_err(|_| ApiError::internal("portable yaml serialization failed"))
}
#[instrument(skip(self, yaml_document), fields(mode = ?query.mode))]
@@ -51,29 +72,41 @@ impl AdminService {
workspace_id: &WorkspaceId,
query: ImportQuery,
yaml_document: &str,
if_match: Option<&str>,
) -> Result<ImportResponse, ApiError> {
let document: YamlOperationDocument = serde_yaml::from_str(yaml_document)
.map_err(|error| ApiError::validation(error.to_string()))?;
if document.kind != "operation" {
return Err(ApiError::validation("yaml kind must be operation"));
}
reject_yaml_anchors_and_aliases(yaml_document)?;
let parsed = parse_bounded_yaml(yaml_document)?;
let format_version = parsed
.as_mapping()
.and_then(|mapping| mapping.get(YamlValue::String("format_version".to_owned())))
.and_then(YamlValue::as_str)
.ok_or_else(yaml_invalid)?;
let payload = OperationPayload {
name: document.operation.name.clone(),
display_name: document.operation.display_name.clone(),
category: document.operation.category.clone(),
protocol: document.operation.protocol,
security_level: document.operation.security_level,
target: document.operation.target.clone(),
input_schema: document.operation.input_schema.clone(),
output_schema: document.operation.output_schema.clone(),
input_mapping: document.operation.input_mapping.clone(),
output_mapping: document.operation.output_mapping.clone(),
execution_config: document.operation.execution_config.clone(),
tool_description: document.operation.tool_description.clone(),
wizard_state: document.operation.wizard_state.clone(),
let (operation, warnings) = match format_version {
"2" => {
let document: YamlOperationDocument =
serde_yaml::from_value(parsed).map_err(|_| yaml_invalid())?;
if document.kind != "operation" || document.format_version != "2" {
return Err(yaml_unsupported());
}
(document.operation, Vec::new())
}
"1" => {
let document: LegacyYamlOperationDocument =
serde_yaml::from_value(parsed).map_err(|_| yaml_invalid())?;
if document.kind != "operation" || document.format_version != "1" {
return Err(yaml_unsupported());
}
let warnings = import_guidance_warnings(&document.operation);
(
PortableOperation::from_registry(&document.operation),
warnings,
)
}
_ => return Err(yaml_unsupported()),
};
let warnings = import_guidance_warnings(&document.operation);
reject_credential_material(&operation)?;
let payload = operation.to_payload();
match query.mode {
ImportMode::Create => {
@@ -88,9 +121,66 @@ impl AdminService {
}
ImportMode::Upsert => {
if let Some(existing) = self
.find_operation_by_name(workspace_id, &document.operation.name)
.find_operation_by_name(workspace_id, &operation.name)
.await?
{
if existing.status == OperationStatus::Archived {
return Err(crank_registry::RegistryError::OperationArchived {
operation_id: existing.id.as_str().to_owned(),
}
.into());
}
let current = self
.get_operation_version(
workspace_id,
&existing.id,
existing.current_draft_version,
)
.await?;
if operation.matches_registry(&current.snapshot) {
let expected_state = OperationStateExpectation {
current_draft_version: existing.current_draft_version,
status: existing.status,
latest_published_version: existing.latest_published_version,
};
self.registry
.verify_operation_state(workspace_id, &existing.id, &expected_state)
.await?;
return Ok(ImportResponse {
operation_id: existing.id.as_str().to_owned(),
workspace_id: workspace_id.as_str().to_owned(),
version: existing.current_draft_version,
import_mode: ImportMode::Upsert,
warnings,
});
}
let current_detail = self.get_operation(workspace_id, &existing.id).await?;
let expected_state = OperationStateExpectation {
current_draft_version: current_detail.current_draft_version,
status: current_detail.status,
latest_published_version: current_detail.latest_published_version,
};
let expected_etag = Self::operation_state_etag(&current_detail);
let provided_etag = if_match.ok_or_else(|| {
ApiError::precondition_required_with_context(
"If-Match is required when YAML upsert changes an existing Operation",
serde_json::json!({
"error_code": "operation_precondition_required",
"current_version": existing.current_draft_version,
"recovery": "reload"
}),
)
})?;
if provided_etag != expected_etag {
return Err(ApiError::conflict_with_context(
"Operation state changed; reload before retrying YAML upsert",
serde_json::json!({
"error_code": "operation_stale_version",
"current_version": existing.current_draft_version,
"recovery": "reload"
}),
));
}
let created = self
.create_version(
workspace_id,
@@ -99,9 +189,9 @@ impl AdminService {
operation: payload,
change_note: Some("yaml upsert".to_owned()),
},
&expected_state,
)
.await?;
let response = ImportResponse {
operation_id: created.operation_id,
workspace_id: created.workspace_id,
@@ -118,22 +208,384 @@ impl AdminService {
Ok(response)
} else {
let created = self.create_operation(workspace_id, payload).await?;
let response = ImportResponse {
Ok(ImportResponse {
operation_id: created.operation_id,
workspace_id: created.workspace_id,
version: created.version,
import_mode: ImportMode::Upsert,
warnings,
};
info!(
name: "admin.operation.imported",
operation_id = %response.operation_id,
version = response.version,
"operation imported by upsert"
);
Ok(response)
})
}
}
}
}
}
impl PortableOperation {
fn from_registry(operation: &crank_registry::RegistryOperation) -> Self {
Self {
name: operation.name.clone(),
display_name: operation.display_name.clone(),
category: operation.category.clone(),
protocol: operation.protocol,
security_level: operation.security_level,
target: operation.target.clone(),
input_schema: operation.input_schema.clone(),
output_schema: operation.output_schema.clone(),
input_mapping: operation.input_mapping.clone(),
output_mapping: operation.output_mapping.clone(),
execution_config: operation.execution_config.clone(),
tool_description: operation.tool_description.clone(),
}
}
fn to_payload(&self) -> OperationPayload {
OperationPayload {
name: self.name.clone(),
display_name: self.display_name.clone(),
category: self.category.clone(),
protocol: self.protocol,
security_level: self.security_level,
target: self.target.clone(),
input_schema: self.input_schema.clone(),
output_schema: self.output_schema.clone(),
input_mapping: self.input_mapping.clone(),
output_mapping: self.output_mapping.clone(),
execution_config: self.execution_config.clone(),
tool_description: self.tool_description.clone(),
wizard_state: None,
}
}
fn matches_registry(&self, operation: &crank_registry::RegistryOperation) -> bool {
self.name == operation.name
&& self.display_name == operation.display_name
&& self.category == operation.category
&& self.protocol == operation.protocol
&& self.security_level == operation.security_level
&& self.target == operation.target
&& self.input_schema == operation.input_schema
&& self.output_schema == operation.output_schema
&& self.input_mapping == operation.input_mapping
&& self.output_mapping == operation.output_mapping
&& self.execution_config == operation.execution_config
&& self.tool_description == operation.tool_description
}
}
fn parse_bounded_yaml(document: &str) -> Result<YamlValue, ApiError> {
if document.is_empty() {
return Err(yaml_invalid());
}
if document.len() > MAX_YAML_BYTES {
return Err(ApiError::payload_too_large_with_context(
"operation yaml exceeds the 256 KiB limit",
serde_json::json!({ "error_code": "operation_yaml_too_large" }),
));
}
if document
.lines()
.any(|line| line.len() > MAX_YAML_LINE_OR_SCALAR_BYTES)
{
return Err(yaml_unsupported());
}
let separators = document.lines().filter(|line| line.trim() == "---").count();
if separators > 1 || document.lines().any(|line| line.trim() == "...") {
return Err(yaml_unsupported());
}
DuplicateChecked::deserialize(serde_yaml::Deserializer::from_str(document))
.map_err(|_| yaml_invalid())?;
let parsed: YamlValue = serde_yaml::from_str(document).map_err(|_| yaml_invalid())?;
let mut nodes = 0;
validate_yaml_value(&parsed, 0, &mut nodes)?;
Ok(parsed)
}
/// A bounded preflight pass which rejects duplicate mapping keys before
/// `serde_yaml::Value` can collapse them. Values are deliberately discarded:
/// the authoritative typed parse follows only after this structural check.
struct DuplicateChecked;
impl<'de> Deserialize<'de> for DuplicateChecked {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_any(DuplicateCheckedVisitor)
}
}
struct DuplicateCheckedVisitor;
impl<'de> Visitor<'de> for DuplicateCheckedVisitor {
type Value = DuplicateChecked;
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("a YAML value without duplicate mapping keys")
}
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
where
A: MapAccess<'de>,
{
let mut keys = Vec::<YamlValue>::new();
while let Some(key) = map.next_key::<YamlValue>()? {
if keys.iter().any(|existing| existing == &key) {
return Err(de::Error::custom("duplicate mapping key"));
}
keys.push(key);
map.next_value::<DuplicateChecked>()?;
}
Ok(DuplicateChecked)
}
fn visit_seq<A>(self, mut sequence: A) -> Result<Self::Value, A::Error>
where
A: SeqAccess<'de>,
{
while sequence.next_element::<DuplicateChecked>()?.is_some() {}
Ok(DuplicateChecked)
}
fn visit_bool<E>(self, _: bool) -> Result<Self::Value, E> {
Ok(DuplicateChecked)
}
fn visit_i64<E>(self, _: i64) -> Result<Self::Value, E> {
Ok(DuplicateChecked)
}
fn visit_u64<E>(self, _: u64) -> Result<Self::Value, E> {
Ok(DuplicateChecked)
}
fn visit_f64<E>(self, _: f64) -> Result<Self::Value, E> {
Ok(DuplicateChecked)
}
fn visit_str<E>(self, _: &str) -> Result<Self::Value, E> {
Ok(DuplicateChecked)
}
fn visit_string<E>(self, _: String) -> Result<Self::Value, E> {
Ok(DuplicateChecked)
}
fn visit_none<E>(self) -> Result<Self::Value, E> {
Ok(DuplicateChecked)
}
fn visit_unit<E>(self) -> Result<Self::Value, E> {
Ok(DuplicateChecked)
}
fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
where
D: Deserializer<'de>,
{
DuplicateChecked::deserialize(deserializer)
}
fn visit_newtype_struct<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
where
D: Deserializer<'de>,
{
DuplicateChecked::deserialize(deserializer)
}
}
fn validate_yaml_value(value: &YamlValue, depth: usize, nodes: &mut usize) -> Result<(), ApiError> {
if depth > MAX_YAML_DEPTH {
return Err(yaml_unsupported());
}
*nodes = nodes.saturating_add(1);
if *nodes > MAX_YAML_NODES {
return Err(yaml_unsupported());
}
match value {
YamlValue::Sequence(values) => {
if values.len() > MAX_YAML_COLLECTION_ITEMS {
return Err(yaml_unsupported());
}
for value in values {
validate_yaml_value(value, depth + 1, nodes)?;
}
}
YamlValue::Mapping(values) => {
if values.len() > MAX_YAML_COLLECTION_ITEMS {
return Err(yaml_unsupported());
}
for (key, value) in values {
validate_yaml_value(key, depth + 1, nodes)?;
validate_yaml_value(value, depth + 1, nodes)?;
}
}
YamlValue::String(value) if value.len() > MAX_YAML_LINE_OR_SCALAR_BYTES => {
return Err(yaml_unsupported());
}
YamlValue::Tagged(_) => return Err(yaml_unsupported()),
_ => {}
}
Ok(())
}
fn reject_credential_material(operation: &PortableOperation) -> Result<(), ApiError> {
let sensitive_name = |name: &str| {
let normalized = name.to_ascii_lowercase();
let segments = normalized.split(['-', '_', '.']).collect::<Vec<_>>();
matches!(
normalized.as_str(),
"authorization" | "proxy-authorization" | "cookie" | "set-cookie"
) || segments
.iter()
.any(|segment| matches!(*segment, "auth" | "token" | "secret" | "cookie"))
|| segments.windows(2).any(|parts| parts == ["api", "key"])
};
let sensitive_value = |value: &str| {
let normalized = value.trim().to_ascii_lowercase();
normalized.starts_with("bearer ")
|| normalized.starts_with("basic ")
|| normalized.starts_with("digest ")
|| normalized.contains("api_key=")
|| normalized.contains("access_token=")
};
let target_headers = match &operation.target {
Target::Rest(target) => &target.static_headers,
};
if target_headers
.iter()
.any(|(name, value)| sensitive_name(name) || sensitive_value(value))
|| operation
.execution_config
.headers
.iter()
.any(|(name, value)| sensitive_name(name) || sensitive_value(value))
{
return Err(ApiError::unprocessable_with_context(
"portable yaml contains credential-bearing headers",
serde_json::json!({ "error_code": "operation_yaml_invalid" }),
));
}
Ok(())
}
fn reject_yaml_anchors_and_aliases(document: &str) -> Result<(), ApiError> {
let mut single_quote = false;
let mut double_quote = false;
let mut escaped = false;
let mut comment = false;
let mut previous = '\n';
let chars = document.chars().collect::<Vec<_>>();
for (index, character) in chars.iter().copied().enumerate() {
if character == '\n' {
comment = false;
previous = character;
continue;
}
if comment {
previous = character;
continue;
}
if double_quote {
if escaped {
escaped = false;
} else if character == '\\' {
escaped = true;
} else if character == '"' {
double_quote = false;
}
previous = character;
continue;
}
if single_quote {
if character == '\'' {
single_quote = false;
}
previous = character;
continue;
}
match character {
'#' => comment = true,
'"' => double_quote = true,
'\'' => single_quote = true,
'&' | '*'
if (previous.is_whitespace() || matches!(previous, ':' | '[' | '{' | ','))
&& chars.get(index + 1).is_some_and(|next| {
next.is_ascii_alphanumeric() || matches!(next, '_' | '-')
}) =>
{
return Err(yaml_unsupported());
}
_ => {}
}
previous = character;
}
Ok(())
}
fn yaml_invalid() -> ApiError {
ApiError::unprocessable_with_context(
"operation yaml is invalid",
serde_json::json!({ "error_code": "operation_yaml_invalid" }),
)
}
fn yaml_unsupported() -> ApiError {
ApiError::unprocessable_with_context(
"operation yaml uses an unsupported construct",
serde_json::json!({ "error_code": "operation_yaml_unsupported" }),
)
}
#[cfg(test)]
mod tests {
use super::parse_bounded_yaml;
use serde_json::Value;
#[test]
fn portable_v2_schema_freezes_the_closed_top_level_contract() {
let schema: Value = serde_json::from_str(include_str!(
"../../../../docs/schemas/operation-export-v2.schema.json"
))
.unwrap();
assert_eq!(schema["properties"]["format_version"]["const"], "2");
assert_eq!(schema["properties"]["kind"]["const"], "operation");
assert_eq!(schema["additionalProperties"], false);
assert_eq!(
schema["$defs"]["portable_operation"]["additionalProperties"],
false
);
assert_eq!(
schema["$defs"]["portable_operation"]["properties"]["security_level"]["enum"],
serde_json::json!(["standard"])
);
for name in [
"rest_target",
"schema",
"mapping_set",
"mapping_rule",
"execution_config",
"tool_description",
] {
assert_eq!(schema["$defs"][name]["additionalProperties"], false);
}
assert_eq!(
schema["$defs"]["portable_operation"]["properties"]["target"]["$ref"],
"#/$defs/rest_target"
);
}
#[test]
fn bounded_yaml_rejects_duplicate_keys_before_typed_parsing() {
let document =
"format_version: '2'\nkind: operation\nkind: attacker_override\noperation: {}\n";
let error = parse_bounded_yaml(document).expect_err("duplicate key must fail closed");
match error {
crate::error::ApiError::Unprocessable { context, .. } => assert_eq!(
context.and_then(|value| value["error_code"].as_str().map(str::to_owned)),
Some("operation_yaml_invalid".to_owned())
),
other => panic!("unexpected error: {other:?}"),
}
}
}
+611 -80
View File
@@ -1,17 +1,23 @@
use std::collections::{BTreeMap, BTreeSet};
use crank_artifacts::{ArtifactError, MAX_ARTIFACT_BYTES};
use crank_core::{
ExecutionConfig, OperationSecurityLevel, Protocol, ToolQualityFinding, ToolQualitySeverity,
WorkspaceId,
AuthProfileId, ExecutionConfig, OperationId, OperationSecurityLevel, OperationStatus, Protocol,
SecretStatus, ToolQualityFinding, ToolQualitySeverity, WorkspaceId,
};
use crank_import::rest::{
ImportFinding, ImportFindingSeverity, ImportOperationCandidate, operation_draft_from_candidate,
ImportFinding, ImportFindingSeverity, ImportNameConflict, ImportOperationCandidate,
NORMALIZER_VERSION, PROJECTION_VERSION, canonical_external_document_uri,
operation_draft_from_candidate, validate_document_source_safe,
};
use crank_registry::{
ApplyImportJobRequest, CreateImportJobRequest, ImportConflictMode, ImportJobId, ImportJobKind,
ImportJobStatus, ImportOperationDraft,
ApplyImportJobRequest, ArtifactSourceId, ArtifactSourceSensitivity,
CreateArtifactSourceRequest, CreateImportJobRequest, CreateImportPlanRequest,
FinishImportJobRequest, ImportConflictMode, ImportJob, ImportJobApplyResult, ImportJobId,
ImportJobKind, ImportJobSourceEnvelope, ImportJobStatus, ImportOperationDraft, ImportPlanId,
RegistryError, RegistryOperation,
};
use serde_json::json;
use serde_json::{Value, json};
use sha2::{Digest, Sha256};
use time::{Duration, OffsetDateTime, format_description::well_known::Rfc3339};
use tracing::{info, instrument};
@@ -19,64 +25,486 @@ use tracing::{info, instrument};
use crate::{
error::ApiError,
service::{
AdminService, OpenApiImportCreatePayload, OpenApiImportCreateResponse,
OpenApiImportCreatedOperation, OpenApiImportPreviewPayload, OpenApiImportPreviewResponse,
OpenApiImportSkippedOperation, OperationPayload, new_prefixed_id,
AdminService, OpenApiImportAuthProfileChoice, OpenApiImportCreatePayload,
OpenApiImportCreateResponse, OpenApiImportCreatedOperation, OpenApiImportPlanApplyPayload,
OpenApiImportPlanCandidateView, OpenApiImportPlanCounts, OpenApiImportPlanCreatePayload,
OpenApiImportPlanCreateResponse, OpenApiImportPlanDecision,
OpenApiImportPlanRevalidationResponse, OpenApiImportPreviewResponse,
OpenApiImportSecurityProfileChoice, OpenApiImportSkippedOperation, OpenApiUpload,
OpenApiUploadLocale, OperationPayload, new_prefixed_id,
},
};
mod external_references;
mod import_plan;
mod job_contract;
mod plan_apply;
mod verified_preview;
use external_references::ImportReplayContext;
use import_plan::import_plan_repreview_error;
use job_contract::{
SourceDetachGuard, import_job_dependencies, import_job_normalization_config, import_job_source,
};
use verified_preview::{
ImportPreviewResolution, artifact_error, openapi_source_error, parse_verified_preview,
preview_digest, preview_has_blocker,
};
const IMPORT_JOB_TTL_HOURS: i64 = 24;
const OPENAPI_PARSE_DEADLINE: std::time::Duration = std::time::Duration::from_secs(30);
const OPENAPI_PARSE_CONCURRENCY: usize = 4;
const MAX_IMPORT_PLAN_KEYS: usize = 2_000;
const OPENAPI_SUPPORT_MATRIX_VERSION: &str = "1.0.4";
const MAX_IMPORT_PLAN_KEY_BYTES: usize = 4_096;
const MAX_IMPORT_PLAN_SERVER_URL_BYTES: usize = 8_192;
const MAX_IMPORT_PLAN_NAME_BYTES: usize = 256;
const MAX_IMPORT_PLAN_ID_REF_BYTES: usize = 128;
static OPENAPI_PARSE_SLOTS: tokio::sync::Semaphore =
tokio::sync::Semaphore::const_new(OPENAPI_PARSE_CONCURRENCY);
fn preview_name_conflicts(
preview: &crank_import::rest::ImportPreview,
catalog_names: &[String],
) -> Vec<ImportNameConflict> {
let operations = preview
.groups
.iter()
.flat_map(|group| group.operations.iter())
.collect::<Vec<_>>();
let mut operation_ids = BTreeMap::<&str, usize>::new();
for candidate in &operations {
if let Some(id) = candidate.operation_id.as_deref() {
*operation_ids.entry(id).or_default() += 1;
}
}
let occupied = catalog_names
.iter()
.map(String::as_str)
.collect::<BTreeSet<_>>();
let mut proposals = catalog_names.iter().cloned().collect::<BTreeSet<_>>();
proposals.extend(
operations
.iter()
.map(|candidate| candidate.suggested_name.clone()),
);
let mut conflicts = Vec::new();
for candidate in operations {
let duplicate = candidate
.operation_id
.as_deref()
.is_some_and(|id| operation_ids.get(id).is_some_and(|count| *count > 1));
let invalid = candidate.operation_id.as_deref().is_some_and(|id| {
id.is_empty()
|| id.len() > MAX_IMPORT_PLAN_NAME_BYTES
|| !id.bytes().enumerate().all(|(index, ch)| {
ch.is_ascii_alphabetic() || ch == b'_' || (index > 0 && ch.is_ascii_digit())
})
});
let taken = occupied.contains(candidate.suggested_name.as_str());
if !duplicate && !invalid && !taken {
continue;
}
let base = candidate
.suggested_name
.chars()
.take(240)
.collect::<String>();
let proposal = next_available_name(&base, &mut proposals);
for conflict_type in [
duplicate.then_some("duplicate_name"),
invalid.then_some("invalid_name"),
taken.then_some("taken_name"),
]
.into_iter()
.flatten()
{
conflicts.push(ImportNameConflict {
operation_key: candidate.key.clone(),
conflict_type: conflict_type.to_owned(),
proposed_name: proposal.clone(),
source_location: candidate.source_location.as_ref().map(|location| {
crank_import::rest::SourceLocation {
pointer: if candidate.operation_id.is_some() {
format!("{}/operationId", location.pointer)
} else {
location.pointer.clone()
},
}
}),
});
}
}
conflicts
}
fn next_available_name(base: &str, occupied: &mut BTreeSet<String>) -> String {
// A catalog may contain more names than the preview candidate cap. With
// occupied.len() + 1 suffixes, at least one candidate must be free.
(2..=occupied.len() + 2)
.map(|index| format!("{base}_{index}"))
.find(|name| occupied.insert(name.clone()))
.expect("finite occupied names always leave a free suffix")
}
fn attach_preview_readiness(
preview: &mut crank_import::rest::ImportPreview,
choices: &[OpenApiImportSecurityProfileChoice],
) {
let document_blocked = preview.findings.iter().any(|finding| {
finding.severity == ImportFindingSeverity::Error && finding.operation_key.is_none()
});
for candidate in preview
.groups
.iter_mut()
.flat_map(|group| group.operations.iter_mut())
{
let blocked = document_blocked
|| candidate
.findings
.iter()
.any(|finding| finding.severity == ImportFindingSeverity::Error);
let alternatives = &candidate.security_requirements.alternatives;
let public = alternatives.is_empty()
|| alternatives
.iter()
.any(|alternative| alternative.supported && alternative.schemes.is_empty());
candidate.status = if blocked {
"blocked"
} else if public {
"actionable"
} else if alternatives.iter().enumerate().any(|(index, alternative)| {
alternative.supported
&& choices.iter().any(|choice| {
choice.operation_key == candidate.key
&& choice.alternative_index == index
&& !choice.profiles.is_empty()
})
}) {
"unresolved"
} else {
"blocked"
}
.to_owned();
}
}
impl AdminService {
#[instrument(skip(self, payload), fields(workspace_id = %workspace_id.as_str()))]
#[instrument(skip(self, upload), fields(workspace_id = %workspace_id.as_str()))]
pub async fn preview_openapi_import(
&self,
workspace_id: &WorkspaceId,
payload: OpenApiImportPreviewPayload,
upload: OpenApiUpload,
) -> Result<OpenApiImportPreviewResponse, ApiError> {
self.ensure_workspace_exists(workspace_id).await?;
let _ = self.registry.delete_expired_import_jobs().await;
let preview = crank_import::rest::preview_document(&payload.document)
.map_err(|error| ApiError::validation(error.to_string()))?;
validate_openapi_upload(&upload)?;
let OpenApiUpload {
bytes: raw_bytes,
mime_type,
locale,
} = upload;
let raw_document = std::str::from_utf8(&raw_bytes)
.map_err(|_| ApiError::openapi_upload(locale, "invalid_utf8"))?;
validate_document_source_safe(raw_document, &self.external_reference_normalization)
.map_err(|_| ApiError::openapi_upload(locale, "invalid_document"))?;
let now = OffsetDateTime::now_utc();
let mut dependencies = self
.materialize_external_reference_snapshots(workspace_id, &raw_bytes, now)
.await;
if dependencies.unsafe_source {
return Err(ApiError::openapi_upload(locale, "invalid_document"));
}
let source_digest =
crank_import::rest::SourceDigest::parse(format!("{:x}", Sha256::digest(&raw_bytes)))
.map_err(|_| ApiError::openapi_upload(locale, "invalid_document"))?;
if crank_import::rest::validate_resolved_bundle_source_safe(
raw_document,
source_digest,
&dependencies.snapshots,
&self.external_reference_normalization,
)
.is_err()
{
dependencies.detach_all().await;
return Err(ApiError::openapi_upload(locale, "invalid_document"));
}
let bytes = raw_bytes;
let store = self.artifact_store.clone();
let artifact_result = tokio::task::spawn_blocking(move || store.put_registered(&bytes))
.await
.map_err(|_| ApiError::openapi_upload(locale, "storage_unavailable"))
.and_then(|result| result.map_err(|error| artifact_error(locale, error)));
let artifact = match artifact_result {
Ok(artifact) => artifact,
Err(error) => {
dependencies.detach_all().await;
return Err(error);
}
};
let expires_at = now + Duration::hours(IMPORT_JOB_TTL_HOURS);
let job_id = ImportJobId::new(new_prefixed_id("imp"));
let preview_payload = serde_json::to_value(&preview)
let source_id = ArtifactSourceId::new(new_prefixed_id("src_openapi"));
let source_result = self
.registry
.create_artifact_source(CreateArtifactSourceRequest {
workspace_id,
source_id: &source_id,
artifact: &artifact,
mime_type: &mime_type,
sensitivity: ArtifactSourceSensitivity::Internal,
created_at: now,
})
.await;
let source = match source_result {
Ok(source) => source,
Err(error) => {
dependencies.detach_all().await;
return Err(ApiError::from(error));
}
};
let mut detach_guard = SourceDetachGuard::new(
self.registry.clone(),
workspace_id.clone(),
source_id.clone(),
source.updated_at,
);
let verified = match self
.registry
.read_artifact_source(
std::sync::Arc::clone(&self.artifact_store),
workspace_id,
&source_id,
)
.await
{
Ok(verified) => verified,
Err(error) => {
detach_guard.detach_now().await;
return Err(ApiError::from(error));
}
};
if verified.source.blob.artifact_ref != *artifact.artifact_ref() {
detach_guard.detach_now().await;
return Err(ApiError::openapi_upload(locale, "source_integrity"));
}
let parsed = match parse_verified_preview(
verified.bytes,
artifact.artifact_ref().digest_hex().to_owned(),
dependencies.snapshots.clone(),
self.external_reference_normalization.clone(),
locale,
false,
)
.await
{
Ok(preview) => preview,
Err(error) => {
dependencies.detach_all().await;
detach_guard.detach_now().await;
return Err(error);
}
};
if parsed
.preview
.groups
.iter()
.all(|group| group.operations.is_empty())
{
dependencies.detach_all().await;
detach_guard.detach_now().await;
return Err(ApiError::openapi_upload(locale, "no_methods"));
}
let candidate_count: usize = parsed
.preview
.groups
.iter()
.map(|group| group.operations.len())
.sum();
if candidate_count > MAX_IMPORT_PLAN_KEYS {
dependencies.detach_all().await;
detach_guard.detach_now().await;
return Err(ApiError::validation_with_context(
"OpenAPI preview has too many candidates",
json!({ "error_code": "openapi_preview_too_many_candidates", "limit": MAX_IMPORT_PLAN_KEYS }),
));
}
let security_profile_choices = match self
.security_profile_choices(workspace_id, &parsed.preview)
.await
{
Ok(choices) => choices,
Err(error) => {
dependencies.detach_all().await;
detach_guard.detach_now().await;
return Err(error);
}
};
let catalog_names = match self.registry.list_import_catalog_names(workspace_id).await {
Ok(names) => names,
Err(error) => {
dependencies.detach_all().await;
detach_guard.detach_now().await;
return Err(ApiError::from(error));
}
};
let source_envelope = ImportJobSourceEnvelope {
source_id,
digest: artifact.artifact_ref().clone(),
};
let preview_value = serde_json::to_value(&parsed.preview)
.map_err(|error| ApiError::internal(error.to_string()))?;
let preview_digest = preview_digest(&preview_value)?;
let preview_payload = json!({
"source": {
"source_id": source_envelope.source_id.as_str(),
"digest": source_envelope.digest.as_str(),
},
"dependencies": dependencies.payload(),
"dependency_snapshots": dependencies.snapshot_payload(),
"preview": preview_value,
"preview_digest": preview_digest,
"normalization": {
"normalizer_version": NORMALIZER_VERSION,
"projection_version": PROJECTION_VERSION,
"ir_fingerprint": parsed.ir_fingerprint,
"config": self.external_reference_normalization,
},
});
self.registry
if let Err(error) = self
.registry
.create_import_job(CreateImportJobRequest {
id: &job_id,
workspace_id,
kind: ImportJobKind::OpenApi,
source_format: &preview.source.format,
source_version: preview.source.version.as_deref(),
source_format: &parsed.preview.source.format,
source_version: parsed.preview.source.version.as_deref(),
status: ImportJobStatus::Pending,
source: &source_envelope,
preview_payload: &preview_payload,
created_at: &now,
expires_at: &expires_at,
})
.await?;
.await
{
dependencies.detach_all().await;
detach_guard.detach_now().await;
return Err(ApiError::from(error));
}
dependencies.disarm();
detach_guard.disarm();
let mut response_preview = parsed.preview;
response_preview.conflicts = preview_name_conflicts(&response_preview, &catalog_names);
attach_preview_readiness(&mut response_preview, &security_profile_choices);
Ok(OpenApiImportPreviewResponse {
job_id: job_id.as_str().to_owned(),
expires_at: expires_at
.format(&Rfc3339)
.map_err(|error| ApiError::internal(error.to_string()))?,
preview,
preview: response_preview,
security_profile_choices,
})
}
async fn security_profile_choices(
&self,
workspace_id: &WorkspaceId,
preview: &crank_import::rest::ImportPreview,
) -> Result<Vec<OpenApiImportSecurityProfileChoice>, ApiError> {
let profiles = self.registry.list_auth_profiles(workspace_id).await?;
let mut available = Vec::new();
for profile in profiles {
if profile.kind != profile.config.kind() {
continue;
}
let mut usable = true;
for secret_id in profile.config.secret_ids() {
let Some(secret) = self.registry.get_secret(workspace_id, secret_id).await? else {
usable = false;
break;
};
if secret.secret.status != SecretStatus::Active
|| self
.registry
.get_current_secret_version(workspace_id, secret_id)
.await?
.is_none()
{
usable = false;
break;
}
}
if usable {
available.push(profile);
}
}
let mut choices = Vec::new();
for operation in preview
.groups
.iter()
.flat_map(|group| group.operations.iter())
{
for (alternative_index, alternative) in operation
.security_requirements
.alternatives
.iter()
.enumerate()
{
if !alternative.supported || alternative.schemes.len() != 1 {
continue;
}
let Some(scheme) = operation.security_schemes.get(&alternative.schemes[0]) else {
continue;
};
let profiles = available
.iter()
.filter(|profile| {
import_plan::auth_config_matches_scheme(&profile.config, scheme)
})
.map(|profile| OpenApiImportAuthProfileChoice {
id: profile.id.as_str().to_owned(),
name: profile.name.clone(),
kind: profile.kind,
})
.collect::<Vec<_>>();
choices.push(OpenApiImportSecurityProfileChoice {
operation_key: operation.key.clone(),
alternative_index,
schemes: alternative.schemes.clone(),
profiles,
});
}
}
Ok(choices)
}
#[instrument(skip(self, payload), fields(workspace_id = %workspace_id.as_str(), job_id = %job_id.as_str()))]
pub async fn create_openapi_import(
&self,
workspace_id: &WorkspaceId,
job_id: &ImportJobId,
payload: OpenApiImportCreatePayload,
) -> Result<OpenApiImportCreateResponse, ApiError> {
self.create_openapi_import_with_locale(
workspace_id,
job_id,
payload,
OpenApiUploadLocale::En,
)
.await
}
pub async fn create_openapi_import_with_locale(
&self,
workspace_id: &WorkspaceId,
job_id: &ImportJobId,
payload: OpenApiImportCreatePayload,
locale: OpenApiUploadLocale,
) -> Result<OpenApiImportCreateResponse, ApiError> {
self.ensure_workspace_exists(workspace_id).await?;
let _ = self.registry.delete_expired_import_jobs().await;
if !matches!(payload.conflict_mode.as_str(), "skip" | "rename") {
return Err(ApiError::validation(
@@ -95,19 +523,16 @@ impl AdminService {
)
})?;
if job.expires_at <= OffsetDateTime::now_utc() {
return Err(ApiError::validation("import preview has expired"));
let message = match locale {
OpenApiUploadLocale::Ru => "срок действия preview импорта истёк",
OpenApiUploadLocale::En => "import preview has expired",
};
return Err(ApiError::validation(message));
}
if job.kind != ImportJobKind::OpenApi {
return Err(ApiError::validation("import job kind is not openapi"));
}
let stored_preview = job
.preview_payload
.get("preview")
.cloned()
.unwrap_or_else(|| job.preview_payload.clone());
let preview: crank_import::rest::ImportPreview = serde_json::from_value(stored_preview)
.map_err(|error| ApiError::internal(error.to_string()))?;
let selected = payload
.selected_operation_keys
.iter()
@@ -118,7 +543,58 @@ impl AdminService {
"selected_operation_keys must contain at least one operation",
));
}
let finished_at = OffsetDateTime::now_utc();
let application_key = openapi_application_key(&payload)?;
let conflict_mode = if payload.conflict_mode == "skip" {
ImportConflictMode::Skip
} else {
ImportConflictMode::Rename
};
let replay_context = ImportReplayContext {
workspace_id,
job_id,
application_key: &application_key,
conflict_mode,
finished_at: &finished_at,
};
if job.status == ImportJobStatus::Completed {
let applied = self
.registry
.apply_import_job(ApplyImportJobRequest {
id: job_id,
workspace_id,
application_key: &application_key,
conflict_mode,
operations: &[],
pre_skipped: &[],
finished_at: &finished_at,
})
.await?;
return Ok(openapi_import_response(applied));
}
let resolved_preview = match self
.resolve_verified_openapi_preview(workspace_id, &job, locale)
.await
{
Ok(resolved) => resolved,
Err(ImportPreviewResolution::Fatal(error_code, error)) => {
return self
.fail_openapi_import_or_replay(&replay_context, error_code, error)
.await;
}
Err(ImportPreviewResolution::NonFatal(error)) => return Err(error),
};
let preview = resolved_preview.preview;
if preview_has_blocker(&preview, &selected) {
return self
.fail_openapi_import_or_replay(
&replay_context,
"reference_resolution_blocked",
ApiError::openapi_upload(locale, "invalid_document"),
)
.await;
}
let mut candidates = BTreeMap::new();
for group in &preview.groups {
for operation in &group.operations {
@@ -131,7 +607,7 @@ impl AdminService {
for operation_key in selected {
let Some(candidate) = candidates.get(&operation_key) else {
skipped.push(OpenApiImportSkippedOperation {
skipped.push(crank_registry::SkippedImportOperation {
operation_key,
name: String::new(),
reason: "operation was not found in import preview".to_owned(),
@@ -171,13 +647,6 @@ impl AdminService {
});
}
let finished_at = OffsetDateTime::now_utc();
let application_key = openapi_application_key(&payload)?;
let conflict_mode = if payload.conflict_mode == "skip" {
ImportConflictMode::Skip
} else {
ImportConflictMode::Rename
};
let applied = self
.registry
.apply_import_job(ApplyImportJobRequest {
@@ -186,24 +655,34 @@ impl AdminService {
application_key: &application_key,
conflict_mode,
operations: &operations,
pre_skipped: &skipped,
finished_at: &finished_at,
})
.await?;
let created = applied
.created
.iter()
.map(|operation| OpenApiImportCreatedOperation {
operation_id: operation.operation_id.as_str().to_owned(),
name: operation.name.clone(),
version: operation.version,
})
.collect::<Vec<_>>();
let mut findings = applied
.created
.iter()
.filter_map(|operation| {
operation.renamed_from.as_ref().map(|previous_name| ImportFinding {
Ok(openapi_import_response(applied))
}
}
fn openapi_import_response(applied: ImportJobApplyResult) -> OpenApiImportCreateResponse {
let created = applied
.created
.iter()
.map(|operation| OpenApiImportCreatedOperation {
operation_key: operation.operation_key.clone(),
operation_id: operation.operation_id.as_str().to_owned(),
name: operation.name.clone(),
version: operation.version,
})
.collect::<Vec<_>>();
let mut findings = applied
.created
.iter()
.filter_map(|operation| {
operation
.renamed_from
.as_ref()
.map(|previous_name| ImportFinding {
code: "operation_name_renamed".to_owned(),
severity: ImportFindingSeverity::Info,
message: format!(
@@ -211,40 +690,75 @@ impl AdminService {
operation.name
),
operation_key: Some(operation.operation_key.clone()),
source_location: None,
})
})
.collect::<Vec<_>>();
for operation in applied.skipped {
skipped.push(OpenApiImportSkippedOperation {
operation_key: operation.operation_key.clone(),
name: operation.name.clone(),
reason: "operation with this name already exists".to_owned(),
});
findings.push(ImportFinding {
code: operation.reason,
severity: ImportFindingSeverity::Warning,
message: format!(
"Операция {} уже существует и была пропущена.",
operation.name
),
operation_key: Some(operation.operation_key),
});
}
info!(
name: "admin.openapi_import.completed",
created = created.len(),
skipped = skipped.len(),
"openapi import created drafts"
);
Ok(OpenApiImportCreateResponse {
created,
skipped,
findings,
})
.collect::<Vec<_>>();
let skipped = applied
.skipped
.into_iter()
.map(|operation| {
let reason = operation.reason.clone();
let name = operation.name.clone();
findings.push(ImportFinding {
code: reason.clone(),
severity: ImportFindingSeverity::Warning,
message: if name.is_empty() {
"Выбранная операция отсутствует в исходном preview и была пропущена.".to_owned()
} else {
format!("Операция {name} уже существует и была пропущена.")
},
operation_key: Some(operation.operation_key.clone()),
source_location: None,
});
OpenApiImportSkippedOperation {
operation_key: operation.operation_key.clone(),
name: operation.name,
reason,
}
})
.collect::<Vec<_>>();
info!(
name: "admin.openapi_import.completed",
created = created.len(),
skipped = skipped.len(),
"openapi import created drafts"
);
OpenApiImportCreateResponse {
created,
skipped,
findings,
}
}
fn validate_openapi_upload(upload: &OpenApiUpload) -> Result<(), ApiError> {
if upload.bytes.is_empty() {
return Err(ApiError::openapi_upload(upload.locale, "empty_file"));
}
if upload.bytes.len() > MAX_ARTIFACT_BYTES {
return Err(ApiError::openapi_upload(upload.locale, "file_too_large"));
}
if !matches!(
upload.mime_type.as_str(),
"application/yaml"
| "application/x-yaml"
| "text/yaml"
| "text/x-yaml"
| "application/json"
| "application/openapi+json"
| "application/octet-stream"
) {
return Err(ApiError::openapi_upload(
upload.locale,
"invalid_media_type",
));
}
if std::str::from_utf8(&upload.bytes).is_err() {
return Err(ApiError::openapi_upload(upload.locale, "invalid_utf8"));
}
Ok(())
}
fn openapi_application_key(payload: &OpenApiImportCreatePayload) -> Result<String, ApiError> {
let selected_operation_keys = payload
.selected_operation_keys
@@ -294,3 +808,20 @@ fn tool_quality_finding_from_import(finding: &ImportFinding) -> ToolQualityFindi
field_path: finding.operation_key.clone(),
}
}
#[cfg(test)]
mod preview_name_tests {
use super::next_available_name;
use std::collections::BTreeSet;
#[test]
fn crowded_catalog_still_gets_a_free_deterministic_proposal() {
let mut occupied = (2..=2_002)
.map(|index| format!("shared_{index}"))
.collect::<BTreeSet<_>>();
occupied.insert("shared".to_owned());
assert_eq!(next_available_name("shared", &mut occupied), "shared_2003");
assert_eq!(next_available_name("shared", &mut occupied), "shared_2004");
}
}
@@ -0,0 +1,460 @@
use super::*;
use std::collections::VecDeque;
use tracing::warn;
pub(super) struct MaterializedDependencies {
pub(super) snapshots: Vec<crank_import::rest::ExternalDocumentSnapshot>,
envelopes: Vec<ImportJobSourceEnvelope>,
canonical_uris: Vec<String>,
guards: Vec<SourceDetachGuard>,
pub(super) unsafe_source: bool,
}
struct StagedDependency {
canonical_uri: String,
bytes: Vec<u8>,
document: String,
}
pub(super) struct ImportReplayContext<'a> {
pub(super) workspace_id: &'a WorkspaceId,
pub(super) job_id: &'a ImportJobId,
pub(super) application_key: &'a str,
pub(super) conflict_mode: ImportConflictMode,
pub(super) finished_at: &'a OffsetDateTime,
}
impl MaterializedDependencies {
fn empty() -> Self {
Self {
snapshots: Vec::new(),
envelopes: Vec::new(),
canonical_uris: Vec::new(),
guards: Vec::new(),
unsafe_source: false,
}
}
pub(super) fn payload(&self) -> Vec<serde_json::Value> {
self.envelopes
.iter()
.zip(&self.canonical_uris)
.map(|(dependency, canonical_uri)| {
json!({
"source_id": dependency.source_id.as_str(),
"digest": dependency.digest.as_str(),
"canonical_uri": canonical_uri,
})
})
.collect()
}
pub(super) fn snapshot_payload(&self) -> Vec<serde_json::Value> {
self.snapshots
.iter()
.filter_map(|snapshot| {
self.envelopes
.iter()
.find(|dependency| dependency.digest.digest_hex() == snapshot.digest.as_str())
.map(|dependency| {
json!({
"source_id": dependency.source_id.as_str(),
"digest": dependency.digest.as_str(),
"canonical_uri": snapshot.canonical_uri,
})
})
})
.collect()
}
pub(super) fn disarm(&mut self) {
for guard in &mut self.guards {
guard.disarm();
}
}
pub(super) async fn detach_all(&mut self) {
for guard in &mut self.guards {
guard.detach_now().await;
}
}
}
impl AdminService {
pub(super) async fn fail_openapi_import_or_replay(
&self,
context: &ImportReplayContext<'_>,
error_text: &'static str,
error: ApiError,
) -> Result<OpenApiImportCreateResponse, ApiError> {
let empty = json!([]);
self.registry
.finish_import_job(FinishImportJobRequest {
id: context.job_id,
status: ImportJobStatus::Failed,
created_operation_ids: &empty,
error_text: Some(error_text),
finished_at: context.finished_at,
})
.await?;
// `finish_import_job` preserves an already Completed row under its
// lock. This read distinguishes that race from the terminal Failed
// transition and returns the immutable canonical application result.
let latest = self
.registry
.get_import_job(context.workspace_id, context.job_id)
.await?;
if latest.is_some_and(|job| job.status == ImportJobStatus::Completed) {
let applied = self
.registry
.apply_import_job(ApplyImportJobRequest {
id: context.job_id,
workspace_id: context.workspace_id,
application_key: context.application_key,
conflict_mode: context.conflict_mode,
operations: &[],
pre_skipped: &[],
finished_at: context.finished_at,
})
.await?;
return Ok(openapi_import_response(applied));
}
Err(error)
}
pub(super) async fn materialize_external_reference_snapshots(
&self,
workspace_id: &WorkspaceId,
primary_bytes: &[u8],
created_at: OffsetDateTime,
) -> MaterializedDependencies {
let materialized_count = std::sync::atomic::AtomicUsize::new(0);
match tokio::time::timeout(
self.external_reference_materialization_timeout,
self.materialize_external_reference_snapshots_inner(
workspace_id,
primary_bytes,
created_at,
&materialized_count,
),
)
.await
{
Ok(dependencies) => dependencies,
Err(_) => {
materialization_failure(
"chain",
"timeout",
materialized_count.load(std::sync::atomic::Ordering::Relaxed),
);
MaterializedDependencies::empty()
}
}
}
async fn materialize_external_reference_snapshots_inner(
&self,
workspace_id: &WorkspaceId,
primary_bytes: &[u8],
created_at: OffsetDateTime,
materialized_count: &std::sync::atomic::AtomicUsize,
) -> MaterializedDependencies {
if !self
.external_reference_normalization
.external_references_enabled
{
return MaterializedDependencies::empty();
}
let Ok(primary) = std::str::from_utf8(primary_bytes) else {
materialization_failure("primary_decode", "invalid_utf8", 0);
return MaterializedDependencies::empty();
};
let Ok(primary_references) =
crank_import::rest::reference_uris(primary, &self.external_reference_normalization)
else {
materialization_failure("primary_scan", "invalid_document", 0);
return MaterializedDependencies::empty();
};
let mut queue = VecDeque::new();
for reference in primary_references {
if let Some(uri) = canonical_external_document_uri(None, &reference) {
queue.push_back((uri, 1usize));
}
}
let mut seen = BTreeSet::new();
let mut staged = Vec::<StagedDependency>::new();
let mut staged_documents = vec![primary.to_owned()];
while let Some((canonical_uri, depth)) = queue.pop_front() {
if !seen.insert(canonical_uri.clone())
|| depth > self.external_reference_normalization.max_reference_depth
|| seen.len()
> self
.external_reference_normalization
.max_reference_documents
{
continue;
}
let bytes = match self.external_reference_fetcher.get(&canonical_uri).await {
Ok(bytes) => bytes,
Err(error) => {
materialization_failure(
"fetch",
external_fetch_error_code(&error),
staged.len(),
);
continue;
}
};
let Ok(document) = std::str::from_utf8(&bytes).map(str::to_owned) else {
materialization_failure("dependency_decode", "invalid_utf8", staged.len());
continue;
};
let Ok(references) = crank_import::rest::external_reference_uris(
&document,
&self.external_reference_normalization,
) else {
materialization_failure("dependency_scan", "invalid_document", staged.len());
continue;
};
staged_documents.push(document.clone());
staged.push(StagedDependency {
canonical_uri: canonical_uri.clone(),
bytes,
document,
});
for reference in references {
if let Some(uri) = canonical_external_document_uri(Some(&canonical_uri), &reference)
{
queue.push_back((uri, depth.saturating_add(1)));
}
}
}
let staged_snapshots = staged
.iter()
.filter_map(|dependency| {
let digest = format!("{:x}", Sha256::digest(&dependency.bytes));
let digest = crank_import::rest::SourceDigest::parse(digest).ok()?;
Some(crank_import::rest::ExternalDocumentSnapshot {
canonical_uri: dependency.canonical_uri.clone(),
digest,
document: dependency.document.clone(),
})
})
.collect::<Vec<_>>();
let primary_digest =
crank_import::rest::SourceDigest::parse(format!("{:x}", Sha256::digest(primary_bytes)))
.expect("SHA-256 digest is valid");
let raw_bundle_is_unsafe = crank_import::rest::validate_document_bundle_source_safe(
&staged_documents
.iter()
.map(String::as_str)
.collect::<Vec<_>>(),
&self.external_reference_normalization,
)
.is_err();
let resolved_bundle_is_unsafe = crank_import::rest::validate_resolved_bundle_source_safe(
primary,
primary_digest,
&staged_snapshots,
&self.external_reference_normalization,
)
.is_err();
if raw_bundle_is_unsafe || resolved_bundle_is_unsafe {
materialization_failure("dependency_redaction", "invalid_document", staged.len());
let mut rejected = MaterializedDependencies::empty();
rejected.unsafe_source = true;
return rejected;
}
let mut result = MaterializedDependencies::empty();
let mut dependency_by_digest = BTreeMap::<String, usize>::new();
for dependency in staged {
let store = self.artifact_store.clone();
let artifact_bytes = dependency.bytes.clone();
let Ok(Ok(artifact)) =
tokio::task::spawn_blocking(move || store.put_registered(&artifact_bytes)).await
else {
materialization_failure(
"artifact_store",
"storage_unavailable",
result.snapshots.len(),
);
continue;
};
let digest_key = artifact.artifact_ref().as_str().to_owned();
let envelope_index = if let Some(index) = dependency_by_digest.get(&digest_key) {
*index
} else {
let source_id = ArtifactSourceId::new(new_prefixed_id("src_openapi_dep"));
let Ok(source) = self
.registry
.create_artifact_source(CreateArtifactSourceRequest {
workspace_id,
source_id: &source_id,
artifact: &artifact,
mime_type: "application/octet-stream",
sensitivity: ArtifactSourceSensitivity::Internal,
created_at,
})
.await
else {
materialization_failure(
"source_create",
"registry_unavailable",
result.snapshots.len(),
);
continue;
};
let mut guard = SourceDetachGuard::new(
self.registry.clone(),
workspace_id.clone(),
source_id.clone(),
source.updated_at,
);
let Ok(verified) = self
.registry
.read_artifact_source(
std::sync::Arc::clone(&self.artifact_store),
workspace_id,
&source_id,
)
.await
else {
materialization_failure(
"source_verify",
"source_unavailable",
result.snapshots.len(),
);
guard.detach_now().await;
continue;
};
if verified.source.blob.artifact_ref != *artifact.artifact_ref() {
materialization_failure(
"source_verify",
"source_integrity",
result.snapshots.len(),
);
guard.detach_now().await;
continue;
}
let index = result.envelopes.len();
result.envelopes.push(ImportJobSourceEnvelope {
source_id: source_id.clone(),
digest: artifact.artifact_ref().clone(),
});
result.guards.push(guard);
dependency_by_digest.insert(digest_key, index);
index
};
let dependency_envelope = &result.envelopes[envelope_index];
let Ok(snapshot_digest) = crank_import::rest::SourceDigest::parse(
dependency_envelope.digest.digest_hex().to_owned(),
) else {
materialization_failure("snapshot", "source_integrity", result.snapshots.len());
continue;
};
result
.snapshots
.push(crank_import::rest::ExternalDocumentSnapshot {
canonical_uri: dependency.canonical_uri.clone(),
digest: snapshot_digest,
document: dependency.document,
});
materialized_count.store(result.snapshots.len(), std::sync::atomic::Ordering::Relaxed);
result.canonical_uris.push(dependency.canonical_uri);
}
// Registry ownership is one envelope per immutable digest, while the
// resolver may map several canonical URIs to that same snapshot. Keep
// payload rows aligned with envelopes and add aliases separately.
if result.envelopes.len() != result.canonical_uris.len() {
let mut canonical_by_digest = BTreeMap::new();
for snapshot in &result.snapshots {
canonical_by_digest
.entry(snapshot.digest.as_str().to_owned())
.or_insert_with(|| snapshot.canonical_uri.clone());
}
result.canonical_uris = result
.envelopes
.iter()
.map(|envelope| {
canonical_by_digest
.get(envelope.digest.digest_hex())
.cloned()
.unwrap_or_default()
})
.collect();
}
result
}
pub(super) async fn read_import_job_dependencies(
&self,
workspace_id: &WorkspaceId,
payload: &serde_json::Value,
locale: OpenApiUploadLocale,
) -> Result<Vec<crank_import::rest::ExternalDocumentSnapshot>, ApiError> {
let dependencies = import_job_dependencies(payload, locale)?;
let mut snapshots = Vec::with_capacity(dependencies.len());
for (canonical_uri, dependency) in dependencies {
let verified = self
.registry
.read_artifact_source(
std::sync::Arc::clone(&self.artifact_store),
workspace_id,
&dependency.source_id,
)
.await
.map_err(|error| openapi_source_error(locale, error))?;
if verified.source.blob.artifact_ref != dependency.digest {
return Err(ApiError::openapi_upload(locale, "source_integrity"));
}
let document = String::from_utf8(verified.bytes)
.map_err(|_| ApiError::openapi_upload(locale, "source_integrity"))?;
let digest =
crank_import::rest::SourceDigest::parse(dependency.digest.digest_hex().to_owned())
.map_err(|_| ApiError::openapi_upload(locale, "source_integrity"))?;
snapshots.push(crank_import::rest::ExternalDocumentSnapshot {
canonical_uri,
digest,
document,
});
}
Ok(snapshots)
}
}
fn external_fetch_error_code(error: &crank_runtime::ExternalReferenceFetchError) -> &'static str {
match error {
crank_runtime::ExternalReferenceFetchError::Disabled => "disabled",
crank_runtime::ExternalReferenceFetchError::InvalidUrl => "invalid_url",
crank_runtime::ExternalReferenceFetchError::TargetNotAllowed => "target_not_allowed",
crank_runtime::ExternalReferenceFetchError::RedirectNotAllowed => "redirect_not_allowed",
crank_runtime::ExternalReferenceFetchError::ResponseTooLarge { .. } => "response_too_large",
crank_runtime::ExternalReferenceFetchError::UnexpectedStatus { .. } => "unexpected_status",
crank_runtime::ExternalReferenceFetchError::Transport { timeout: true, .. } => "timeout",
crank_runtime::ExternalReferenceFetchError::Transport { .. } => "transport",
crank_runtime::ExternalReferenceFetchError::InvalidConfiguration => "invalid_configuration",
}
}
fn materialization_failure(stage: &'static str, error_code: &'static str, count: usize) {
warn!(
name: "admin.openapi_import.materialization_failed",
stage,
error_code,
count,
"external OpenAPI materialization failed"
);
}
#[cfg(test)]
mod tests {
use super::external_fetch_error_code;
#[test]
fn unexpected_status_has_a_stable_safe_error_code() {
let error = crank_runtime::ExternalReferenceFetchError::UnexpectedStatus { status: 500 };
assert_eq!(external_fetch_error_code(&error), "unexpected_status");
}
}
@@ -0,0 +1,976 @@
mod validation;
use super::*;
use crank_core::AuthConfig;
use crank_import::rest::SecurityRequirementsSource as SecuritySource;
impl AdminService {
pub async fn create_openapi_import_plan(
&self,
workspace_id: &WorkspaceId,
job_id: &ImportJobId,
payload: OpenApiImportPlanCreatePayload,
) -> Result<OpenApiImportPlanCreateResponse, ApiError> {
self.create_openapi_import_plan_with_locale(
workspace_id,
job_id,
payload,
OpenApiUploadLocale::En,
)
.await
}
#[instrument(skip(self, payload), fields(workspace_id = %workspace_id.as_str(), job_id = %job_id.as_str()))]
pub async fn create_openapi_import_plan_with_locale(
&self,
workspace_id: &WorkspaceId,
job_id: &ImportJobId,
payload: OpenApiImportPlanCreatePayload,
locale: OpenApiUploadLocale,
) -> Result<OpenApiImportPlanCreateResponse, ApiError> {
self.ensure_workspace_exists(workspace_id).await?;
let now = OffsetDateTime::now_utc();
let job = self
.registry
.get_import_job(workspace_id, job_id)
.await?
.ok_or_else(|| {
ApiError::not_found_with_context(
"import job was not found",
json!({ "job_id": job_id.as_str() }),
)
})?;
if job.kind != ImportJobKind::OpenApi {
return Err(ApiError::validation("import job kind is not openapi"));
}
if job.expires_at <= now {
let message = match locale {
OpenApiUploadLocale::Ru => "срок действия preview импорта истёк",
OpenApiUploadLocale::En => "import preview has expired",
};
return Err(ApiError::validation(message));
}
if job.status != ImportJobStatus::Pending {
return Err(import_plan_repreview_error(
locale,
"import_plan_job_terminal",
));
}
if payload.decisions.len() > MAX_IMPORT_PLAN_KEYS {
return Err(ApiError::validation(
"too many decision blocks for this import plan",
));
}
if payload.server_url.as_deref().is_some_and(|server_url| {
server_url.is_empty() || server_url.len() > MAX_IMPORT_PLAN_SERVER_URL_BYTES
}) {
return Err(ApiError::validation_with_context(
"server_url is empty or too long",
json!({ "error_code": "import_plan_invalid_input" }),
));
}
// The plan is anchored to the verified job snapshots. A stale or
// corrupt preview is reported as bounded re-preview guidance and
// never mutates the job: other stored plans may still reference the
// same job and its replay must remain readable.
let resolved = self
.resolve_verified_openapi_preview(workspace_id, &job, locale)
.await
.map_err(|resolution| match resolution {
ImportPreviewResolution::Fatal(error_code, _) => {
import_plan_repreview_error(locale, error_code)
}
ImportPreviewResolution::NonFatal(error) => error,
})?;
let preview = &resolved.preview;
let catalog_revision = self.registry.import_catalog_revision(workspace_id).await?;
// Stored external dependency snapshots are re-verified for every
// replay variant (including historically persisted ones) without a
// network fetch: detached or altered snapshots are re-preview
// material and no plan is created.
self.read_import_job_dependencies(workspace_id, &job.preview_payload, locale)
.await
.map_err(|_| {
import_plan_repreview_error(locale, "import_dependency_verification_failed")
})?;
let candidates: BTreeMap<String, &ImportOperationCandidate> = preview
.groups
.iter()
.flat_map(|group| group.operations.iter())
.map(|operation| (operation.key.clone(), operation))
.collect();
let all_set: BTreeSet<String> = candidates.keys().cloned().collect();
if all_set.len() > MAX_IMPORT_PLAN_KEYS {
return Err(ApiError::validation_with_context(
"too many candidates for an import plan",
json!({ "error_code": "import_plan_invalid_input" }),
));
}
let selected = resolve_plan_selection(
&payload.selection_mode,
&payload.selected_operation_keys,
&all_set,
)?;
if selected.len() > MAX_IMPORT_PLAN_KEYS
|| all_set.len() - selected.len() > MAX_IMPORT_PLAN_KEYS
{
return Err(ApiError::validation_with_context(
"too many candidates for an import plan",
json!({ "error_code": "import_plan_invalid_input" }),
));
}
let selected_keys: Vec<String> = selected.iter().cloned().collect();
let excluded: Vec<String> = all_set
.iter()
.filter(|key| !selected.contains(*key))
.cloned()
.collect();
let excluded_reasons = excluded
.iter()
.map(|key| {
let name = candidates
.get(key)
.map(|candidate| candidate.draft.name.clone())
.unwrap_or_else(|| key.clone());
let reason = preview
.findings
.iter()
.find(|finding| {
finding.severity == ImportFindingSeverity::Error
&& finding.operation_key.is_none()
})
.map(|finding| finding.code.clone())
.or_else(|| {
candidates.get(key).and_then(|candidate| {
candidate
.findings
.iter()
.find(|finding| finding.severity == ImportFindingSeverity::Error)
.map(|finding| finding.code.clone())
})
})
.unwrap_or_else(|| "not_selected".to_owned());
(key.clone(), (name, reason))
})
.collect::<BTreeMap<_, _>>();
let decisions = validation::validate_plan_decisions(&payload.decisions, &selected)?;
let global_blocker = preview
.findings
.iter()
.find(|finding| {
finding.severity == ImportFindingSeverity::Error && finding.operation_key.is_none()
})
.map(|finding| finding.code.clone());
let mut planned = Vec::with_capacity(selected_keys.len());
for key in &selected_keys {
let Some(candidate) = candidates.get(key) else {
unreachable!("selection is validated against the candidate set");
};
let blocker = global_blocker.clone().or_else(|| {
candidate
.findings
.iter()
.find(|finding| finding.severity == ImportFindingSeverity::Error)
.map(|finding| finding.code.clone())
});
let candidate_plan = self
.plan_import_candidate(
workspace_id,
payload.server_url.as_deref(),
candidate,
decisions.get(key).copied(),
blocker,
locale,
)
.await?;
planned.push(candidate_plan);
}
let counts = OpenApiImportPlanCounts {
selected: planned.len(),
excluded: excluded.len(),
actionable: planned
.iter()
.filter(|candidate| !candidate.blocked)
.count(),
blocked: planned.iter().filter(|candidate| candidate.blocked).count(),
};
if payload.selection_mode == "partial" && counts.blocked != 0 {
return Err(ApiError::validation_with_context(
"partial selection includes blocked candidates",
json!({ "error_code": "import_plan_partial_blocked" }),
));
}
if self.registry.import_catalog_revision(workspace_id).await? != catalog_revision {
return Err(import_plan_repreview_error(
locale,
"import_plan_catalog_stale",
));
}
// Server-authored immutable plan. Client-supplied candidate material,
// digests or payload fields are not read: every value below derives
// from the verified job preview and the revalidated decisions.
let all_keys: Vec<String> = all_set.iter().cloned().collect();
let plan_counts_value = json!({
"selected": counts.selected,
"excluded": counts.excluded,
"actionable": counts.actionable,
"blocked": counts.blocked,
});
let plan_reference = new_prefixed_id("plan");
let plan_payload = json!({
"schema_version": "import-plan-v2",
"plan_reference": plan_reference,
"job_id": job_id.as_str(),
"server_url": payload.server_url,
"source": {
"source_id": resolved.source.source_id.as_str(),
"digest": resolved.source.digest.as_str(),
},
"dependencies": job.preview_payload.get("dependencies").cloned().unwrap_or(json!([])),
"fingerprint": resolved.fingerprint,
"catalog_revision": catalog_revision,
"support_matrix_version": OPENAPI_SUPPORT_MATRIX_VERSION,
"findings": {
"document": preview.findings,
"candidates": candidates.iter().map(|(key, candidate)| json!({
"key": key,
"findings": candidate.findings,
})).collect::<Vec<_>>(),
},
"selection": {
"mode": payload.selection_mode,
"all": all_keys,
"selected": selected_keys,
"excluded": excluded,
},
"excluded_reasons": excluded_reasons.iter().map(|(key, (name, reason))| json!({
"key": key,
"name": name,
"reason": reason,
})).collect::<Vec<_>>(),
"counts": plan_counts_value,
"candidates": planned.iter().map(plan_candidate_payload).collect::<Result<Vec<_>, _>>()?,
});
let plan_reference_id = ImportPlanId::new(&plan_reference);
let created = self
.registry
.create_import_plan(CreateImportPlanRequest {
id: &plan_reference_id,
workspace_id,
job_id,
payload: &plan_payload,
created_at: &now,
expires_at: &job.expires_at,
})
.await
.map_err(ApiError::from)?;
let mut candidate_views: Vec<OpenApiImportPlanCandidateView> = planned
.iter()
.map(|candidate| OpenApiImportPlanCandidateView {
key: candidate.key.clone(),
name: candidate.name.clone(),
state: if candidate.blocked {
"blocked"
} else {
"actionable"
}
.to_owned(),
action: Some(candidate.action.clone()),
target_operation_id: candidate.target_operation_id.clone(),
confirmed_name: candidate.confirmed_name.clone(),
auth_profile_id: candidate.auth_profile_id.clone(),
security_alternative_index: candidate.security_alternative_index,
reason: candidate.reason.clone(),
})
.collect();
candidate_views.extend(excluded.iter().map(|key| {
let name = excluded_reasons
.get(key)
.map(|(name, _)| name.clone())
.unwrap_or_else(|| key.clone());
OpenApiImportPlanCandidateView {
key: key.clone(),
name,
state: "excluded".to_owned(),
action: None,
target_operation_id: None,
confirmed_name: None,
auth_profile_id: None,
security_alternative_index: None,
reason: excluded_reasons.get(key).map(|(_, reason)| reason.clone()),
}
}));
Ok(OpenApiImportPlanCreateResponse {
plan_reference: created.id.as_str().to_owned(),
plan_digest: created.digest,
catalog_revision,
support_matrix_version: OPENAPI_SUPPORT_MATRIX_VERSION.to_owned(),
expires_at: created
.expires_at
.format(&Rfc3339)
.map_err(|error| ApiError::internal(error.to_string()))?,
selection_mode: payload.selection_mode,
counts,
candidates: candidate_views,
})
}
/// Materializes one selected candidate of the plan: the confirmed
/// decision, the exact authorized target with its expected state and the
/// full Draft snapshot (create/rename/new_version) or the bounded
/// redacted rejection reason (blocked).
async fn plan_import_candidate(
&self,
workspace_id: &WorkspaceId,
server_url: Option<&str>,
candidate: &ImportOperationCandidate,
decision: Option<&OpenApiImportPlanDecision>,
blocker_reason: Option<String>,
locale: OpenApiUploadLocale,
) -> Result<PlannedCandidate, ApiError> {
let action = decision
.map(|decision| decision.action.as_str())
.unwrap_or("create");
let target_operation_id =
decision.and_then(|decision| decision.target_operation_id.clone());
let confirmed_name = decision.and_then(|decision| decision.confirmed_name.clone());
let auth_profile_id = decision.and_then(|decision| decision.auth_profile_id.clone());
let requested_security_alternative =
decision.and_then(|decision| decision.security_alternative_index);
let mut blocker_reason = blocker_reason;
let candidate_name = candidate.draft.name.clone();
let mut blocked = blocker_reason.is_some();
// Explicit skip/new_version targets are always resolved in the
// workspace: an unknown target, a name mismatch, or an incompatible
// protocol is a request error even when the candidate is already
// blocked.
let mut target = None;
let mut target_summary = None;
let mut next_version: Option<u32> = None;
if matches!(action, "skip" | "new_version") {
let target_id = target_operation_id
.as_ref()
.expect("target_operation_id is validated for skip/new_version")
.clone();
let target_operation = OperationId::new(target_id.clone());
let summary = self
.registry
.get_operation_summary(workspace_id, &target_operation)
.await?
.ok_or_else(|| {
ApiError::validation_with_context(
"decision target operation was not found in this workspace",
json!({
"target_operation_id": target_id,
"error_code": "import_plan_target_invalid"
}),
)
})?;
if summary.name != candidate_name {
return Err(ApiError::validation_with_context(
"decision target does not match the candidate name in this workspace",
json!({
"target_operation_id": target_id,
"error_code": "import_plan_target_invalid"
}),
));
}
if action == "new_version"
&& (summary.protocol != Protocol::Rest
|| summary.status == OperationStatus::Archived)
{
return Err(ApiError::validation_with_context(
"new_version target must be an active REST operation",
json!({
"target_operation_id": target_id,
"error_code": "import_plan_target_invalid"
}),
));
}
target = Some(json!({
"operation_id": target_id,
"name": summary.name,
"expected_state": {
"status": serde_json::to_value(summary.status)
.map_err(|error| ApiError::internal(error.to_string()))?,
"current_draft_version": summary.current_draft_version,
"latest_published_version": summary.latest_published_version,
},
}));
if action == "new_version" {
let version = summary
.current_draft_version
.checked_add(1)
.filter(|value| i32::try_from(*value).is_ok())
.ok_or_else(|| {
ApiError::conflict_with_context(
"operation revision limit reached",
json!({ "error_code": "operation_revision_exhausted" }),
)
})?;
next_version = Some(version);
}
target_summary = Some(summary);
}
// create/rename must land on a name that is free now; the server
// never auto-selects a replacement when the confirmed name is taken.
if !blocked && matches!(action, "create" | "rename") {
let final_name = confirmed_name.as_deref().unwrap_or(candidate_name.as_str());
if self
.find_operation_by_name(workspace_id, final_name)
.await?
.is_some()
{
if action == "create" && decision.is_none() {
return Ok(PlannedCandidate {
key: candidate.key.clone(),
name: candidate_name,
blocked: true,
reason: Some("operation_name_conflict".to_owned()),
action: action.to_owned(),
target_operation_id,
confirmed_name,
auth_profile_id,
security_alternative_index: requested_security_alternative,
auth_requirement: None,
auth_snapshot: None,
target: None,
operation: None,
});
}
let message = match locale {
OpenApiUploadLocale::Ru => {
format!("операция с именем {final_name} уже существует")
}
OpenApiUploadLocale::En => {
format!("operation with name {final_name} already exists")
}
};
return Err(ApiError::conflict_with_context(
message,
json!({
"name": final_name,
"error_code": "import_plan_name_conflict",
"recovery": "confirm_skip_rename_or_new_version"
}),
));
}
}
// Use an explicit decision or inherit the target reference for new_version.
let mut auth_profile_ref = auth_profile_id
.as_deref()
.map(|id| AuthProfileId::new(id.to_owned()));
let mut inherited_auth_profile = false;
if let Some(summary) = target_summary.as_ref()
&& !blocked
&& action == "new_version"
&& auth_profile_ref.is_none()
&& (candidate.security_requirements.source == SecuritySource::None
|| !candidate.security_requirements.alternatives.is_empty())
{
let record = self
.registry
.get_operation_version(workspace_id, &summary.id, summary.current_draft_version)
.await?
.ok_or_else(|| ApiError::internal("current operation version is unavailable"))?;
auth_profile_ref = record.snapshot.execution_config.auth_profile_ref;
inherited_auth_profile = auth_profile_ref.is_some();
}
let auth_profile_id = auth_profile_ref.as_ref().map(|id| id.as_str().to_owned());
let inherited_unconstrained_auth = inherited_auth_profile
&& action == "new_version"
&& candidate.security_requirements.source
== crank_import::rest::SecurityRequirementsSource::None
&& candidate.security_requirements.alternatives.is_empty()
&& requested_security_alternative.is_none();
let security_alternative_index = if blocked {
requested_security_alternative
} else if inherited_unconstrained_auth {
None
} else {
resolve_security_binding(
self,
workspace_id,
candidate,
auth_profile_ref.as_ref(),
requested_security_alternative,
&mut blocker_reason,
)
.await?
};
blocked = blocker_reason.is_some();
let auth_requirement = if !blocked && inherited_unconstrained_auth {
Some(json!({ "kind": "unconstrained" }))
} else if !blocked && auth_profile_ref.is_some() {
security_alternative_index
.and_then(|index| candidate.security_requirements.alternatives.get(index))
.and_then(|alternative| {
(alternative.schemes.len() == 1).then(|| &alternative.schemes[0])
})
.and_then(|scheme_name| candidate.security_schemes.get(scheme_name))
.and_then(|scheme| match &scheme.kind {
crank_import::rest::NormalizedSecuritySchemeKind::Http { scheme, .. } => {
match scheme.to_ascii_lowercase().as_str() {
"bearer" => Some(json!({ "kind": "bearer" })),
"basic" => Some(json!({ "kind": "basic" })),
_ => None,
}
}
crank_import::rest::NormalizedSecuritySchemeKind::ApiKey {
location: crank_import::rest::SecurityParameterLocation::Header,
parameter_name,
..
} => {
Some(json!({ "kind": "api_key_header", "parameter_name": parameter_name }))
}
crank_import::rest::NormalizedSecuritySchemeKind::ApiKey {
location: crank_import::rest::SecurityParameterLocation::Query,
parameter_name,
..
} => Some(json!({ "kind": "api_key_query", "parameter_name": parameter_name })),
_ => None,
})
} else {
None
};
if blocked {
return Ok(PlannedCandidate {
key: candidate.key.clone(),
name: candidate_name,
blocked: true,
reason: blocker_reason,
action: action.to_owned(),
target_operation_id,
confirmed_name,
auth_profile_id,
security_alternative_index,
auth_requirement,
auth_snapshot: None,
target,
operation: None,
});
}
if action == "skip" {
return Ok(PlannedCandidate {
key: candidate.key.clone(),
name: candidate_name,
blocked: false,
reason: None,
action: action.to_owned(),
target_operation_id,
confirmed_name,
auth_profile_id,
security_alternative_index,
auth_requirement: None,
auth_snapshot: None,
target,
operation: None,
});
}
let mut draft = operation_draft_from_candidate(candidate, server_url);
if action == "rename" {
draft.name = confirmed_name
.clone()
.expect("rename decisions carry the exact confirmed_name");
}
attach_import_findings(&mut draft, candidate);
let operation = self.new_operation_snapshot(OperationPayload {
name: draft.name.clone(),
display_name: draft.display_name.clone(),
category: draft.category,
protocol: Protocol::Rest,
security_level: OperationSecurityLevel::Standard,
target: crank_core::Target::Rest(draft.target),
input_schema: draft.input_schema,
output_schema: draft.output_schema,
input_mapping: draft.input_mapping,
output_mapping: draft.output_mapping,
execution_config: ExecutionConfig {
timeout_ms: 10_000,
retry_policy: None,
response_cache: None,
idempotency: None,
safety: None,
approval_policy: None,
auth_profile_ref,
headers: BTreeMap::new(),
},
tool_description: draft.tool_description,
wizard_state: draft.wizard_state,
})?;
// The same workspace/auth secret validator the operation creation
// path uses: the referenced profile and its active secret refs must
// resolve in this workspace before the plan is accepted.
self.validate_registry_operation_in_workspace(workspace_id, &operation)
.await?;
let mut operation = operation;
if action == "new_version" {
let target_id = target_operation_id
.clone()
.expect("new_version decisions carry target_operation_id");
operation.id = OperationId::new(target_id);
operation.version = next_version.expect("new_version computes the next Draft version");
}
let auth_snapshot = match operation.execution_config.auth_profile_ref.as_ref() {
Some(profile_id) => Some(self.import_auth_snapshot(workspace_id, profile_id).await?),
None => None,
};
Ok(PlannedCandidate {
key: candidate.key.clone(),
name: draft.name,
blocked: false,
reason: None,
action: action.to_owned(),
target_operation_id,
confirmed_name,
auth_profile_id,
security_alternative_index,
auth_requirement,
auth_snapshot,
target,
operation: Some(operation),
})
}
/// Captures only profile configuration and secret metadata. Credential
/// values never enter the plan; Apply compares this snapshot under locks.
async fn import_auth_snapshot(
&self,
workspace_id: &WorkspaceId,
profile_id: &AuthProfileId,
) -> Result<Value, ApiError> {
let unavailable = || {
ApiError::unprocessable_with_context(
"The selected authentication profile is unavailable for this import.",
json!({ "error_code": "import_plan_auth_unavailable" }),
)
};
let profile = self
.registry
.get_auth_profile(workspace_id, profile_id)
.await?
.ok_or_else(unavailable)?;
let mut secret_ids: Vec<String> = profile
.config
.secret_ids()
.into_iter()
.map(|id| id.as_str().to_owned())
.collect();
secret_ids.sort();
secret_ids.dedup();
let mut secrets = Vec::with_capacity(secret_ids.len());
for id in secret_ids {
let secret = self
.registry
.get_secret(workspace_id, &crank_core::SecretId::new(id.clone()))
.await?
.ok_or_else(unavailable)?
.secret;
secrets.push(json!({
"id": id,
"status": secret.status,
"current_version": secret.current_version,
"updated_at": secret.updated_at.format(&Rfc3339)
.map_err(|error| ApiError::internal(error.to_string()))?,
}));
}
Ok(json!({
"profile_id": profile_id.as_str(),
"name": profile.name,
"kind": profile.kind,
"config": profile.config,
"updated_at": profile.updated_at.format(&Rfc3339)
.map_err(|error| ApiError::internal(error.to_string()))?,
"secrets": secrets,
}))
}
}
struct PlannedCandidate {
key: String,
name: String,
blocked: bool,
reason: Option<String>,
action: String,
target_operation_id: Option<String>,
confirmed_name: Option<String>,
auth_profile_id: Option<String>,
security_alternative_index: Option<usize>,
auth_requirement: Option<Value>,
auth_snapshot: Option<Value>,
target: Option<Value>,
operation: Option<RegistryOperation>,
}
pub(super) fn import_plan_repreview_error(
locale: OpenApiUploadLocale,
error_code: &'static str,
) -> ApiError {
let message = match locale {
OpenApiUploadLocale::Ru => {
"не удалось повторно проверить план импорта; создайте новый preview"
}
OpenApiUploadLocale::En => "import plan revalidation failed; create a fresh preview",
};
ApiError::conflict_with_context(
message,
json!({
"error_code": error_code,
"recovery": "repreview_required",
"repreview_required": true
}),
)
}
/// Closes the selection against the current preview: `full` keeps every
/// candidate (an explicit list must equal the whole candidate set), `partial`
/// requires a non-empty exact subset. Unknown or duplicate keys are rejected
/// before any plan material exists.
fn resolve_plan_selection(
selection_mode: &str,
requested: &[String],
all: &BTreeSet<String>,
) -> Result<BTreeSet<String>, ApiError> {
if !matches!(selection_mode, "full" | "partial") {
return Err(ApiError::validation_with_context(
"selection_mode must be full or partial",
json!({ "error_code": "import_plan_invalid_input" }),
));
}
if requested.len() > MAX_IMPORT_PLAN_KEYS {
return Err(ApiError::validation_with_context(
"too many selected_operation_keys for this import plan",
json!({ "error_code": "import_plan_invalid_input" }),
));
}
for key in requested {
if key.is_empty() || key.len() > MAX_IMPORT_PLAN_KEY_BYTES {
return Err(ApiError::validation_with_context(
"selected_operation_keys entries are empty or too long",
json!({ "error_code": "import_plan_invalid_input" }),
));
}
}
let requested_set: BTreeSet<&String> = requested.iter().collect();
if requested_set.len() != requested.len() {
return Err(ApiError::validation_with_context(
"selected_operation_keys contains duplicates",
json!({ "error_code": "import_plan_invalid_input" }),
));
}
let all_set: BTreeSet<&String> = all.iter().collect();
match selection_mode {
"full" => {
if !requested_set.is_empty() && requested_set != all_set {
return Err(ApiError::validation_with_context(
"full selection covers the entire candidate set; selected_operation_keys does not match the preview",
json!({ "error_code": "import_plan_selection_mismatch" }),
));
}
Ok(all.clone())
}
_ => {
if requested_set.is_empty() {
return Err(ApiError::validation_with_context(
"partial selection requires at least one selected_operation_key",
json!({ "error_code": "import_plan_invalid_input" }),
));
}
if !requested_set.is_subset(&all_set) {
return Err(ApiError::validation_with_context(
"selected_operation_keys reference operations absent from the preview",
json!({ "error_code": "import_plan_unknown_operation_key" }),
));
}
Ok(requested.iter().cloned().collect())
}
}
}
async fn resolve_security_binding(
service: &AdminService,
workspace_id: &WorkspaceId,
candidate: &ImportOperationCandidate,
profile_ref: Option<&AuthProfileId>,
requested_index: Option<usize>,
blocker_reason: &mut Option<String>,
) -> Result<Option<usize>, ApiError> {
let alternatives = &candidate.security_requirements.alternatives;
if alternatives.is_empty() {
if profile_ref.is_some() || requested_index.is_some() {
return Err(ApiError::validation_with_context(
"security binding was supplied for an operation with no security requirement",
json!({ "operation_key": candidate.key, "error_code": "import_plan_auth_incompatible" }),
));
}
return Ok(None);
}
let public_index = alternatives
.iter()
.position(|alternative| alternative.supported && alternative.schemes.is_empty());
let compatible = if let Some(profile_ref) = profile_ref {
let profile = service
.registry
.get_auth_profile(workspace_id, profile_ref)
.await?
.ok_or_else(|| {
ApiError::unprocessable_with_context(
"selected Auth Profile is unavailable in this workspace",
json!({ "error_code": "import_plan_auth_unavailable" }),
)
})?;
let compatible = alternatives
.iter()
.enumerate()
.filter(|(_, alternative)| {
alternative.supported
&& alternative.schemes.len() == 1
&& candidate
.security_schemes
.get(&alternative.schemes[0])
.is_some_and(|scheme| auth_config_matches_scheme(&profile.config, scheme))
})
.map(|(index, _)| index)
.collect::<Vec<_>>();
Some((profile, compatible))
} else {
None
};
if let Some((_, compatible_indices)) = compatible.as_ref() {
let selected = match requested_index {
Some(index) if compatible_indices.contains(&index) => index,
Some(_) => {
return Err(ApiError::validation_with_context(
"selected Auth Profile is incompatible with the selected security alternative",
json!({ "operation_key": candidate.key, "error_code": "import_plan_auth_incompatible" }),
));
}
None if compatible_indices.len() == 1 => compatible_indices[0],
None if compatible_indices.is_empty() => {
return Err(ApiError::validation_with_context(
"selected Auth Profile is incompatible with the source security schemes",
json!({ "operation_key": candidate.key, "error_code": "import_plan_auth_incompatible" }),
));
}
None => {
return Err(ApiError::validation_with_context(
"security_alternative_index is required when a profile matches multiple alternatives",
json!({ "operation_key": candidate.key, "error_code": "import_plan_security_alternative_required" }),
));
}
};
return Ok(Some(selected));
}
if let Some(index) = requested_index {
if alternatives
.get(index)
.is_none_or(|alternative| !alternative.supported)
{
return Err(ApiError::validation_with_context(
"selected security alternative is unsupported",
json!({ "operation_key": candidate.key, "error_code": "import_plan_auth_incompatible" }),
));
}
if alternatives[index].schemes.is_empty() {
return Ok(Some(index));
}
}
if let Some(public_index) = public_index
&& requested_index.is_none()
{
return Ok(Some(public_index));
}
*blocker_reason = Some("auth_profile_required".to_owned());
Ok(requested_index)
}
pub(super) fn auth_config_matches_scheme(
config: &AuthConfig,
scheme: &crank_import::rest::NormalizedSecurityScheme,
) -> bool {
use crank_import::rest::{NormalizedSecuritySchemeKind, SecurityParameterLocation};
match (&scheme.kind, config) {
(NormalizedSecuritySchemeKind::Http { scheme, .. }, AuthConfig::Bearer(_)) => {
scheme.eq_ignore_ascii_case("bearer")
}
(NormalizedSecuritySchemeKind::Http { scheme, .. }, AuthConfig::Basic(_)) => {
scheme.eq_ignore_ascii_case("basic")
}
(
NormalizedSecuritySchemeKind::ApiKey {
location: SecurityParameterLocation::Header,
parameter_name,
..
},
AuthConfig::ApiKeyHeader(config),
) => parameter_name.eq_ignore_ascii_case(&config.header_name),
(
NormalizedSecuritySchemeKind::ApiKey {
location: SecurityParameterLocation::Query,
parameter_name,
..
},
AuthConfig::ApiKeyQuery(config),
) => parameter_name == &config.param_name,
_ => false,
}
}
/// Serializes one planned candidate into the versioned plan payload: the
/// redacted decision, the bounded rejection reason (if any), the exact
/// authorized target with expected state and, for mutating actions, the full
/// server-authored Draft snapshot.
fn plan_candidate_payload(candidate: &PlannedCandidate) -> Result<Value, ApiError> {
let mut object = json!({
"key": candidate.key,
"name": candidate.name,
"state": if candidate.blocked { "blocked" } else { "actionable" },
"decision": {
"action": candidate.action,
"target_operation_id": candidate.target_operation_id,
"confirmed_name": candidate.confirmed_name,
"auth_profile_id": candidate.auth_profile_id,
"security_alternative_index": candidate.security_alternative_index,
},
});
if let Some(reason) = &candidate.reason {
object["reason"] = json!(reason);
}
if let Some(target) = &candidate.target {
object["target"] = target.clone();
}
if let Some(auth_snapshot) = &candidate.auth_snapshot {
object["auth_snapshot"] = auth_snapshot.clone();
}
if let Some(auth_requirement) = &candidate.auth_requirement {
object["auth_requirement"] = auth_requirement.clone();
}
if let Some(operation) = &candidate.operation {
let value = serde_json::to_value(operation)
.map_err(|error| ApiError::internal(error.to_string()))?;
object["operation"] = value;
}
Ok(object)
}
@@ -0,0 +1,128 @@
use super::*;
/// Binds decisions to selected candidates with action-specific field rules:
/// unknown/duplicate keys, foreign fields and oversized references are
/// rejected before the plan is materialized.
pub(super) fn validate_plan_decisions<'input>(
decisions: &'input [OpenApiImportPlanDecision],
selected: &BTreeSet<String>,
) -> Result<BTreeMap<String, &'input OpenApiImportPlanDecision>, ApiError> {
let invalid = |context: Value| {
Err(ApiError::validation_with_context(
"import plan decision is invalid",
context,
))
};
let mut mapping = BTreeMap::new();
for decision in decisions {
if decision.operation_key.is_empty()
|| decision.operation_key.len() > MAX_IMPORT_PLAN_KEY_BYTES
{
return invalid(json!({
"operation_key": decision.operation_key,
"error_code": "import_plan_invalid_input"
}));
}
if !matches!(
decision.action.as_str(),
"create" | "skip" | "rename" | "new_version"
) {
return invalid(json!({
"operation_key": decision.operation_key,
"error_code": "import_plan_invalid_input"
}));
}
validate_decision_fields(decision)?;
if !selected.contains(&decision.operation_key) {
return invalid(json!({
"operation_key": decision.operation_key,
"error_code": "import_plan_decision_unselected"
}));
}
if mapping
.insert(decision.operation_key.clone(), decision)
.is_some()
{
return invalid(json!({
"operation_key": decision.operation_key,
"error_code": "import_plan_invalid_input"
}));
}
}
Ok(mapping)
}
fn validate_decision_fields(decision: &OpenApiImportPlanDecision) -> Result<(), ApiError> {
let context = json!({
"operation_key": decision.operation_key,
"error_code": "import_plan_invalid_input"
});
let invalid = |message: &str| -> Result<(), ApiError> {
Err(ApiError::validation_with_context(message, context.clone()))
};
if decision
.target_operation_id
.as_deref()
.is_some_and(|value| value.is_empty() || value.len() > MAX_IMPORT_PLAN_ID_REF_BYTES)
{
return invalid("decision target_operation_id is empty or too long");
}
if decision
.confirmed_name
.as_deref()
.is_some_and(|value| value.is_empty() || value.len() > MAX_IMPORT_PLAN_NAME_BYTES)
{
return invalid("decision confirmed_name is empty or too long");
}
if decision
.auth_profile_id
.as_deref()
.is_some_and(|value| value.is_empty() || value.len() > MAX_IMPORT_PLAN_ID_REF_BYTES)
{
return invalid("decision auth_profile_id is empty or too long");
}
if decision
.security_alternative_index
.is_some_and(|index| index > MAX_IMPORT_PLAN_KEYS)
{
return invalid("decision security_alternative_index is too large");
}
match decision.action.as_str() {
"create" => {
if decision.target_operation_id.is_some() || decision.confirmed_name.is_some() {
return invalid(
"target_operation_id and confirmed_name are not allowed for action create",
);
}
}
"skip" => {
if decision.auth_profile_id.is_some() || decision.security_alternative_index.is_some() {
return invalid("security binding is not allowed for action skip");
}
if decision.target_operation_id.is_none() {
return invalid("action skip requires the exact target_operation_id");
}
if decision.confirmed_name.is_some() {
return invalid("confirmed_name is not allowed for action skip");
}
}
"rename" => {
if decision.target_operation_id.is_some() {
return invalid("target_operation_id is not allowed for action rename");
}
if decision.confirmed_name.is_none() {
return invalid("action rename requires the exact confirmed_name");
}
}
"new_version" => {
if decision.target_operation_id.is_none() {
return invalid("action new_version requires the exact target_operation_id");
}
if decision.confirmed_name.is_some() {
return invalid("confirmed_name is not allowed for action new_version");
}
}
_ => unreachable!("decision action is validated before the field rules"),
}
Ok(())
}
@@ -0,0 +1,169 @@
use std::collections::BTreeSet;
use crank_core::WorkspaceId;
use crank_registry::{ArtifactSourceId, DetachArtifactSourceRequest, ImportJobSourceEnvelope};
use time::OffsetDateTime;
use crate::{error::ApiError, service::OpenApiUploadLocale};
use super::canonical_external_document_uri;
const MAX_IMPORT_JOB_DOCUMENTS: usize = 32;
pub(super) fn import_job_source(
payload: &serde_json::Value,
locale: OpenApiUploadLocale,
) -> Result<ImportJobSourceEnvelope, ApiError> {
let source = payload
.get("source")
.ok_or_else(|| ApiError::openapi_upload(locale, "source_unavailable"))?;
let source_id = source
.get("source_id")
.and_then(serde_json::Value::as_str)
.filter(|value| value.len() <= 132)
.ok_or_else(|| ApiError::openapi_upload(locale, "source_unavailable"))?;
let digest = source
.get("digest")
.and_then(serde_json::Value::as_str)
.and_then(|value| value.parse().ok())
.ok_or_else(|| ApiError::openapi_upload(locale, "source_integrity"))?;
Ok(ImportJobSourceEnvelope {
source_id: ArtifactSourceId::new(source_id),
digest,
})
}
pub(super) fn import_job_dependencies(
payload: &serde_json::Value,
locale: OpenApiUploadLocale,
) -> Result<Vec<(String, ImportJobSourceEnvelope)>, ApiError> {
let empty = Vec::new();
let items = payload
.get("dependency_snapshots")
.and_then(serde_json::Value::as_array)
.unwrap_or(&empty);
if items.len() > MAX_IMPORT_JOB_DOCUMENTS {
return Err(ApiError::openapi_upload(locale, "source_integrity"));
}
let mut canonical_uris = BTreeSet::new();
items
.iter()
.map(|item| {
let canonical_uri = item
.get("canonical_uri")
.and_then(serde_json::Value::as_str)
.filter(|value| {
canonical_external_document_uri(None, value).as_deref() == Some(*value)
})
.ok_or_else(|| ApiError::openapi_upload(locale, "source_integrity"))?;
if !canonical_uris.insert(canonical_uri.to_owned()) {
return Err(ApiError::openapi_upload(locale, "source_integrity"));
}
let source_id = item
.get("source_id")
.and_then(serde_json::Value::as_str)
.filter(|value| value.len() <= 132)
.ok_or_else(|| ApiError::openapi_upload(locale, "source_integrity"))?;
let digest = item
.get("digest")
.and_then(serde_json::Value::as_str)
.and_then(|value| value.parse().ok())
.ok_or_else(|| ApiError::openapi_upload(locale, "source_integrity"))?;
Ok((
canonical_uri.to_owned(),
ImportJobSourceEnvelope {
source_id: ArtifactSourceId::new(source_id),
digest,
},
))
})
.collect()
}
pub(super) fn import_job_normalization_config(
payload: &serde_json::Value,
current: &crank_import::rest::NormalizationConfig,
legacy_v1: bool,
locale: OpenApiUploadLocale,
) -> Result<crank_import::rest::NormalizationConfig, ApiError> {
if legacy_v1 {
return Ok(current.clone());
}
payload
.pointer("/normalization/config")
.cloned()
.ok_or_else(|| ApiError::openapi_upload(locale, "source_integrity"))
.and_then(|value| {
serde_json::from_value(value)
.map_err(|_| ApiError::openapi_upload(locale, "source_integrity"))
})
}
pub(super) struct SourceDetachGuard {
registry: crank_registry::PostgresRegistry,
workspace_id: WorkspaceId,
source_id: ArtifactSourceId,
expected_updated_at: OffsetDateTime,
armed: bool,
}
impl SourceDetachGuard {
pub(super) fn new(
registry: crank_registry::PostgresRegistry,
workspace_id: WorkspaceId,
source_id: ArtifactSourceId,
expected_updated_at: OffsetDateTime,
) -> Self {
Self {
registry,
workspace_id,
source_id,
expected_updated_at,
armed: true,
}
}
pub(super) fn disarm(&mut self) {
self.armed = false;
}
pub(super) async fn detach_now(&mut self) {
if !self.armed {
return;
}
self.armed = false;
let _ = self
.registry
.detach_artifact_source(DetachArtifactSourceRequest {
workspace_id: &self.workspace_id,
source_id: &self.source_id,
expected_updated_at: Some(self.expected_updated_at),
detached_at: OffsetDateTime::now_utc(),
})
.await;
}
}
impl Drop for SourceDetachGuard {
fn drop(&mut self) {
if !self.armed {
return;
}
let registry = self.registry.clone();
let workspace_id = self.workspace_id.clone();
let source_id = self.source_id.clone();
let expected_updated_at = Some(self.expected_updated_at);
if let Ok(handle) = tokio::runtime::Handle::try_current() {
handle.spawn(async move {
let _ = registry
.detach_artifact_source(DetachArtifactSourceRequest {
workspace_id: &workspace_id,
source_id: &source_id,
expected_updated_at,
detached_at: OffsetDateTime::now_utc(),
})
.await;
});
}
}
}
@@ -0,0 +1,440 @@
use super::*;
use crank_registry::{ApplyImportPlanRequest, ImportPlanApplyReport};
const MAX_IMPORT_PLAN_IDEMPOTENCY_KEY_BYTES: usize = 128;
impl AdminService {
/// Reads an immutable plan and checks its current authority without Apply mutations.
pub async fn revalidate_openapi_import_plan(
&self,
workspace_id: &WorkspaceId,
plan_id: &ImportPlanId,
) -> Result<OpenApiImportPlanRevalidationResponse, ApiError> {
self.revalidate_openapi_import_plan_with_locale(
workspace_id,
plan_id,
OpenApiUploadLocale::En,
)
.await
}
/// Locale-aware variant used by the HTTP boundary.
pub async fn revalidate_openapi_import_plan_with_locale(
&self,
workspace_id: &WorkspaceId,
plan_id: &ImportPlanId,
locale: OpenApiUploadLocale,
) -> Result<OpenApiImportPlanRevalidationResponse, ApiError> {
self.ensure_workspace_exists(workspace_id).await?;
let plan = self
.registry
.get_import_plan(workspace_id, plan_id)
.await?
.ok_or_else(|| {
ApiError::not_found_with_context(
"import plan was not found in this workspace",
json!({ "error_code": "import_plan_not_found" }),
)
})?;
let mut reason = None;
let now = OffsetDateTime::now_utc();
if plan.expires_at <= now {
reason = Some("import_plan_expired");
} else if let Some(job) = self
.registry
.get_import_job(workspace_id, &plan.job_id)
.await?
{
if job.kind != ImportJobKind::OpenApi
|| job.status != ImportJobStatus::Pending
|| job.expires_at <= now
{
reason = Some("import_plan_job_terminal");
} else {
match self
.resolve_verified_openapi_preview(workspace_id, &job, locale)
.await
{
Ok(resolved) => {
let v2 = plan.payload.get("schema_version").and_then(Value::as_str)
== Some("import-plan-v2");
if self
.read_import_job_dependencies(
workspace_id,
&job.preview_payload,
locale,
)
.await
.is_err()
{
reason = Some("import_dependency_verification_failed");
} else if !v2 {
// Historical plans stay readable for audit, but lack the
// catalog evidence required for current Apply authority.
reason = Some("importer_version_changed");
} else if resolved
.fingerprint
.get("normalizer_version")
.and_then(Value::as_str)
!= Some(NORMALIZER_VERSION)
|| resolved
.fingerprint
.get("projection_version")
.and_then(Value::as_str)
!= Some(PROJECTION_VERSION)
|| plan
.payload
.get("support_matrix_version")
.and_then(Value::as_str)
!= Some(OPENAPI_SUPPORT_MATRIX_VERSION)
{
reason = Some("importer_version_changed");
} else if self
.registry
.revalidate_import_plan(workspace_id, plan_id, &resolved.fingerprint)
.await
.is_err()
{
reason = Some("import_plan_revalidation_failed");
}
}
Err(_) => reason = Some("import_preview_revalidation_failed"),
}
}
} else {
reason = Some("import_plan_job_unavailable");
}
let counts = &plan.payload["counts"];
let count = |key: &str| {
counts
.get(key)
.and_then(Value::as_u64)
.and_then(|value| usize::try_from(value).ok())
.unwrap_or(0)
};
let selected = plan.payload["candidates"]
.as_array()
.into_iter()
.flatten()
.map(|candidate| {
let decision = &candidate["decision"];
let string = |value: &Value| value.as_str().map(str::to_owned);
OpenApiImportPlanCandidateView {
key: string(&candidate["key"]).unwrap_or_default(),
name: string(&candidate["name"]).unwrap_or_default(),
state: string(&candidate["state"]).unwrap_or_default(),
action: string(&decision["action"]),
target_operation_id: string(&decision["target_operation_id"]),
confirmed_name: string(&decision["confirmed_name"]),
auth_profile_id: string(&decision["auth_profile_id"]),
security_alternative_index: decision["security_alternative_index"]
.as_u64()
.and_then(|value| usize::try_from(value).ok()),
reason: string(&candidate["reason"]),
}
});
let excluded = plan
.payload
.pointer("/selection/excluded")
.and_then(Value::as_array)
.into_iter()
.flatten()
.filter_map(Value::as_str)
.map(|key| OpenApiImportPlanCandidateView {
key: key.to_owned(),
name: excluded_candidate_display_name(&plan.payload, key),
state: "excluded".to_owned(),
action: None,
target_operation_id: None,
confirmed_name: None,
auth_profile_id: None,
security_alternative_index: None,
reason: plan.payload["excluded_reasons"]
.as_array()
.and_then(|items| items.iter().find(|item| item["key"].as_str() == Some(key)))
.and_then(|item| item["reason"].as_str())
.map(str::to_owned)
.or_else(|| Some("not_selected".to_owned())),
});
Ok(OpenApiImportPlanRevalidationResponse {
plan_reference: plan.id.as_str().to_owned(),
plan_digest: plan.digest,
revalidation_status: if reason.is_some() { "stale" } else { "current" }.to_owned(),
revalidation_required: reason.is_some(),
reason: reason.map(str::to_owned),
counts: OpenApiImportPlanCounts {
selected: count("selected"),
excluded: count("excluded"),
actionable: count("actionable"),
blocked: count("blocked"),
},
candidates: selected.chain(excluded).collect(),
})
}
pub async fn apply_openapi_import_plan(
&self,
workspace_id: &WorkspaceId,
plan_id: &ImportPlanId,
payload: OpenApiImportPlanApplyPayload,
) -> Result<ImportPlanApplyReport, ApiError> {
self.apply_openapi_import_plan_with_locale(
workspace_id,
plan_id,
payload,
OpenApiUploadLocale::En,
)
.await
}
#[instrument(skip(self, payload), fields(workspace_id = %workspace_id.as_str(), plan_id = %plan_id.as_str()))]
pub async fn apply_openapi_import_plan_with_locale(
&self,
workspace_id: &WorkspaceId,
plan_id: &ImportPlanId,
payload: OpenApiImportPlanApplyPayload,
locale: OpenApiUploadLocale,
) -> Result<ImportPlanApplyReport, ApiError> {
self.ensure_workspace_exists(workspace_id).await?;
validate_apply_payload(&payload)?;
// The replay row is deliberately checked before the plan or job.
// Its report remains authoritative after normal TTL cleanup removes
// those source rows.
if let Some(application) = self
.registry
.get_import_plan_application(workspace_id, &payload.idempotency_key)
.await?
{
if application.plan_digest != payload.plan_digest {
return Err(key_digest_conflict(locale));
}
return Ok(application.result.report);
}
let plan = self
.registry
.get_import_plan(workspace_id, plan_id)
.await?
.ok_or_else(|| {
ApiError::not_found_with_context(
"import plan was not found in this workspace",
json!({ "error_code": "import_plan_not_found" }),
)
})?;
if plan.digest != payload.plan_digest {
return Err(import_plan_repreview_error(
locale,
"import_plan_digest_mismatch",
));
}
let now = OffsetDateTime::now_utc();
if plan.expires_at <= now {
return Err(import_plan_repreview_error(locale, "import_plan_expired"));
}
let job = self
.registry
.get_import_job(workspace_id, &plan.job_id)
.await?
.ok_or_else(|| import_plan_repreview_error(locale, "import_plan_job_unavailable"))?;
if job.kind != ImportJobKind::OpenApi {
return Err(import_plan_repreview_error(
locale,
"import_plan_job_kind_mismatch",
));
}
if job.status != ImportJobStatus::Pending || job.expires_at <= now {
return Err(import_plan_repreview_error(
locale,
"import_plan_job_terminal",
));
}
let resolved = self
.resolve_verified_openapi_preview(workspace_id, &job, locale)
.await
.map_err(|resolution| match resolution {
ImportPreviewResolution::Fatal(error_code, _) => {
import_plan_repreview_error(locale, error_code)
}
ImportPreviewResolution::NonFatal(_) => {
import_plan_repreview_error(locale, "import_preview_revalidation_failed")
}
})?;
// Applying a new plan requires the current normalizer/projection
// contract. Historic jobs remain available to the legacy replay path,
// but a plan created from a historic projection must be previewed
// again before it can authorize mutations.
if resolved
.fingerprint
.get("normalizer_version")
.and_then(Value::as_str)
!= Some(NORMALIZER_VERSION)
|| resolved
.fingerprint
.get("projection_version")
.and_then(Value::as_str)
!= Some(PROJECTION_VERSION)
|| (plan.payload.get("schema_version").and_then(Value::as_str)
== Some("import-plan-v2")
&& plan
.payload
.get("support_matrix_version")
.and_then(Value::as_str)
!= Some(OPENAPI_SUPPORT_MATRIX_VERSION))
{
return Err(import_plan_repreview_error(
locale,
"importer_version_changed",
));
}
// Verify every persisted external snapshot again without making a
// network request. The registry repeats source/dependency checks
// under its transaction before it mutates operations.
self.read_import_job_dependencies(workspace_id, &job.preview_payload, locale)
.await
.map_err(|_| {
import_plan_repreview_error(locale, "import_dependency_verification_failed")
})?;
let result = self
.registry
.apply_import_plan(&ApplyImportPlanRequest {
workspace_id,
plan_id,
plan_digest: &payload.plan_digest,
idempotency_key: &payload.idempotency_key,
verified_fingerprint: &resolved.fingerprint,
applied_at: &now,
})
.await
.map_err(|error| map_apply_registry_error(locale, error))?;
Ok(result.report)
}
}
fn excluded_candidate_display_name(plan: &Value, key: &str) -> String {
plan["excluded_reasons"]
.as_array()
.and_then(|items| items.iter().find(|item| item["key"].as_str() == Some(key)))
.and_then(|item| item["name"].as_str())
.filter(|name| !name.is_empty())
.unwrap_or(key)
.to_owned()
}
fn validate_apply_payload(payload: &OpenApiImportPlanApplyPayload) -> Result<(), ApiError> {
let digest_valid = payload.plan_digest.len() == 64
&& payload
.plan_digest
.bytes()
.all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte));
if !digest_valid {
return Err(ApiError::validation_with_context(
"plan_digest must be 64 lowercase hexadecimal characters",
json!({ "error_code": "import_plan_invalid_input" }),
));
}
if !(1..=MAX_IMPORT_PLAN_IDEMPOTENCY_KEY_BYTES).contains(&payload.idempotency_key.len())
|| payload.idempotency_key.trim() != payload.idempotency_key
|| payload.idempotency_key.chars().any(char::is_control)
{
return Err(ApiError::validation_with_context(
"idempotency_key must be a non-empty bounded printable value",
json!({ "error_code": "import_plan_invalid_input" }),
));
}
Ok(())
}
fn map_apply_registry_error(locale: OpenApiUploadLocale, error: RegistryError) -> ApiError {
match error {
RegistryError::ImportPlanNotFound { .. } => ApiError::not_found_with_context(
"import plan was not found in this workspace",
json!({ "error_code": "import_plan_not_found" }),
),
RegistryError::ImportPlanExpired { .. } => {
import_plan_repreview_error(locale, "import_plan_expired")
}
RegistryError::ImportPlanPayloadCorrupt { .. } => {
import_plan_repreview_error(locale, "import_plan_payload_invalid")
}
RegistryError::ImportPlanFingerprintStale { .. } => {
import_plan_repreview_error(locale, "import_plan_fingerprint_stale")
}
RegistryError::ImportPlanKeyDigestConflict { .. } => key_digest_conflict(locale),
RegistryError::ImportPlanNameConflict { .. } => {
let message = match locale {
OpenApiUploadLocale::Ru => "имя операции изменилось после создания плана",
OpenApiUploadLocale::En => "an operation name changed after the plan was created",
};
ApiError::conflict_with_context(
message,
json!({
"error_code": "import_plan_name_conflict",
"recovery": "confirm_skip_rename_or_new_version"
}),
)
}
RegistryError::ImportPlanTargetStale { .. } => {
import_plan_repreview_error(locale, "import_plan_target_stale")
}
RegistryError::ImportPlanAuthUnavailable { .. } => {
import_plan_repreview_error(locale, "import_plan_auth_unavailable")
}
other => ApiError::from(other),
}
}
fn key_digest_conflict(locale: OpenApiUploadLocale) -> ApiError {
let message = match locale {
OpenApiUploadLocale::Ru => {
"ключ идемпотентности уже зафиксирован для другого материала плана; создайте новый preview"
}
OpenApiUploadLocale::En => {
"idempotency key was already committed for different plan material"
}
};
ApiError::conflict_with_context(
message,
json!({
"error_code": "import_plan_key_digest_conflict",
"recovery": "repreview_required",
"repreview_required": true
}),
)
}
#[cfg(test)]
mod tests {
use super::excluded_candidate_display_name;
use serde_json::json;
#[test]
fn excluded_candidate_name_uses_persisted_display_name_and_legacy_key_fallback() {
let current = json!({
"excluded_reasons": [{
"key": "GET /alpha",
"name": "Alpha display label",
"reason": "not_selected"
}]
});
assert_eq!(
excluded_candidate_display_name(&current, "GET /alpha"),
"Alpha display label"
);
let historic = json!({
"excluded_reasons": [{ "key": "GET /alpha", "reason": "not_selected" }]
});
assert_eq!(
excluded_candidate_display_name(&historic, "GET /alpha"),
"GET /alpha"
);
}
}
@@ -0,0 +1,405 @@
use super::*;
/// Revalidates the verified source/dependency snapshots and the persisted
/// replay contract of a stored job without mutating the job. Both the
/// legacy Apply and the ImportPlan creation derive from this result, so
/// the legacy failure-text code travels with the outcome.
impl AdminService {
pub(super) async fn resolve_verified_openapi_preview(
&self,
workspace_id: &WorkspaceId,
job: &ImportJob,
locale: OpenApiUploadLocale,
) -> Result<ResolvedOpenApiPreview, ImportPreviewResolution> {
let source = match import_job_source(&job.preview_payload, locale) {
Ok(source) => source,
Err(error) => {
return Err(ImportPreviewResolution::Fatal(
"import_source_verification_failed",
error,
));
}
};
let verified = match self
.registry
.read_artifact_source(
std::sync::Arc::clone(&self.artifact_store),
workspace_id,
&source.source_id,
)
.await
{
Ok(verified) => verified,
Err(
error @ (RegistryError::SourceNotFound { .. } | RegistryError::SourceUnavailable),
) => {
return Err(ImportPreviewResolution::Fatal(
"import_source_verification_failed",
openapi_source_error(locale, error),
));
}
Err(error @ RegistryError::SourceIntegrity) => {
return Err(ImportPreviewResolution::Fatal(
"import_source_verification_failed",
openapi_source_error(locale, error),
));
}
Err(error) => {
return Err(ImportPreviewResolution::NonFatal(openapi_source_error(
locale, error,
)));
}
};
if verified.source.blob.artifact_ref != source.digest {
return Err(ImportPreviewResolution::Fatal(
"import_source_verification_failed",
ApiError::openapi_upload(locale, "source_integrity"),
));
}
let replay_contract = match import_replay_contract(&job.preview_payload, locale) {
Ok(contract) => contract,
Err(error) => {
return Err(ImportPreviewResolution::Fatal(
"import_replay_verification_failed",
error,
));
}
};
let (preview, normalizer_version, projection_version, ir_fingerprint) =
match replay_contract {
ImportReplayContract::PersistedV2(replay) => (
replay.preview.preview,
replay.normalizer_version,
replay.projection_version,
replay.preview.ir_fingerprint,
),
ImportReplayContract::LegacyV1 | ImportReplayContract::Current => {
let legacy_v1 = matches!(replay_contract, ImportReplayContract::LegacyV1);
let dependency_snapshots = self
.read_import_job_dependencies(workspace_id, &job.preview_payload, locale)
.await
.map_err(|error| {
ImportPreviewResolution::Fatal(
"import_dependency_verification_failed",
error,
)
})?;
let normalization_config = import_job_normalization_config(
&job.preview_payload,
&self.external_reference_normalization,
legacy_v1,
locale,
)
.map_err(|error| {
ImportPreviewResolution::Fatal("import_replay_verification_failed", error)
})?;
let parsed = parse_verified_preview(
verified.bytes,
source.digest.digest_hex().to_owned(),
dependency_snapshots,
normalization_config,
locale,
legacy_v1,
)
.await
.map_err(|error| {
ImportPreviewResolution::Fatal("import_replay_verification_failed", error)
})?;
if let Err(error) =
verify_preview_contract(&job.preview_payload, &parsed, locale)
{
return Err(ImportPreviewResolution::Fatal(
"import_replay_verification_failed",
error,
));
}
if legacy_v1 {
(parsed.preview, String::new(), String::new(), String::new())
} else {
(
parsed.preview,
NORMALIZER_VERSION.to_owned(),
PROJECTION_VERSION.to_owned(),
parsed.ir_fingerprint,
)
}
}
};
Ok(ResolvedOpenApiPreview {
source,
preview,
fingerprint: json!({
"normalizer_version": if normalizer_version.is_empty() { Value::Null } else { Value::String(normalizer_version) },
"projection_version": if projection_version.is_empty() { Value::Null } else { Value::String(projection_version) },
"ir_fingerprint": if ir_fingerprint.is_empty() { Value::Null } else { Value::String(ir_fingerprint) },
"preview_digest": job.preview_payload.get("preview_digest").and_then(Value::as_str).map(|digest| Value::String(digest.to_owned())).unwrap_or(Value::Null),
}),
})
}
}
pub(super) struct ParsedOpenApiPreview {
pub(super) preview: crank_import::rest::ImportPreview,
pub(super) ir_fingerprint: String,
}
/// Verified job preview material shared by the legacy Apply path and the
/// ImportPlan creation: the replay contract has held, no job mutation
/// happened.
pub(super) struct ResolvedOpenApiPreview {
pub(super) source: ImportJobSourceEnvelope,
pub(super) preview: crank_import::rest::ImportPreview,
pub(super) fingerprint: Value,
}
/// Outcome of a failed preview revalidation. `Fatal` codes are the same
/// stable failure texts the legacy path records on a failed job; the plan
/// path must surface the same material as a bounded re-preview conflict
/// without touching the job, because other stored plans may anchor to it.
pub(super) enum ImportPreviewResolution {
Fatal(&'static str, ApiError),
NonFatal(ApiError),
}
struct PersistedOpenApiReplay {
preview: ParsedOpenApiPreview,
normalizer_version: String,
projection_version: String,
}
enum ImportReplayContract {
LegacyV1,
PersistedV2(Box<PersistedOpenApiReplay>),
Current,
}
fn import_replay_contract(
payload: &serde_json::Value,
locale: OpenApiUploadLocale,
) -> Result<ImportReplayContract, ApiError> {
let Some(normalization) = payload.get("normalization") else {
return Ok(ImportReplayContract::LegacyV1);
};
let normalizer = normalization
.get("normalizer_version")
.and_then(serde_json::Value::as_str)
.ok_or_else(|| ApiError::openapi_upload(locale, "source_integrity"))?;
let projection = normalization
.get("projection_version")
.and_then(serde_json::Value::as_str)
.ok_or_else(|| ApiError::openapi_upload(locale, "source_integrity"))?;
let ir_fingerprint = normalization
.get("ir_fingerprint")
.and_then(serde_json::Value::as_str)
.filter(|value| is_lower_sha256(value))
.ok_or_else(|| ApiError::openapi_upload(locale, "source_integrity"))?;
let expected_preview_digest = payload
.get("preview_digest")
.and_then(serde_json::Value::as_str)
.filter(|value| is_lower_sha256(value))
.ok_or_else(|| ApiError::openapi_upload(locale, "source_integrity"))?;
if normalizer == NORMALIZER_VERSION && projection == PROJECTION_VERSION {
return Ok(ImportReplayContract::Current);
}
// Historical preview payloads are replayed as their persisted projection.
// They must never be parsed again with current normalization semantics.
if !matches!(
(normalizer, projection),
("normalized-ir-v2", "preview-v2")
| ("normalized-ir-v3", "preview-v3")
| ("normalized-ir-v4", "preview-v4")
| ("normalized-ir-v5", "preview-v5")
// The v5/preview-v6 pair predates the shared external-URI identity
// (normalized-ir-v6). Its saved projection is authoritative and is
// replayed verbatim, never reparsed under the current normalizer.
| ("normalized-ir-v5", "preview-v6")
) {
return Err(ApiError::openapi_upload(locale, "source_integrity"));
}
let preview_value = payload
.get("preview")
.cloned()
.ok_or_else(|| ApiError::openapi_upload(locale, "source_integrity"))?;
if preview_digest(&preview_value)? != expected_preview_digest {
return Err(ApiError::openapi_upload(locale, "source_integrity"));
}
let preview = serde_json::from_value(preview_value)
.map_err(|_| ApiError::openapi_upload(locale, "source_integrity"))?;
Ok(ImportReplayContract::PersistedV2(Box::new(
PersistedOpenApiReplay {
preview: ParsedOpenApiPreview {
preview,
ir_fingerprint: ir_fingerprint.to_owned(),
},
normalizer_version: normalizer.to_owned(),
projection_version: projection.to_owned(),
},
)))
}
fn is_lower_sha256(value: &str) -> bool {
value.len() == 64
&& value
.bytes()
.all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte))
}
pub(super) async fn parse_verified_preview(
bytes: Vec<u8>,
digest: String,
snapshots: Vec<crank_import::rest::ExternalDocumentSnapshot>,
normalization_config: crank_import::rest::NormalizationConfig,
locale: OpenApiUploadLocale,
legacy_v1: bool,
) -> Result<ParsedOpenApiPreview, ApiError> {
let started = tokio::time::Instant::now();
let permit = tokio::time::timeout(OPENAPI_PARSE_DEADLINE, OPENAPI_PARSE_SLOTS.acquire())
.await
.map_err(|_| ApiError::openapi_upload(locale, "parser_unavailable"))?
.map_err(|_| ApiError::openapi_upload(locale, "parser_unavailable"))?;
let parsing = tokio::task::spawn_blocking(move || {
// Keep the permit inside the blocking task. Timing out the caller
// cannot cancel CPU work already running, but abandoned parsers remain
// globally bounded and release capacity when they actually finish.
let _permit = permit;
let document = std::str::from_utf8(&bytes)
.map_err(|_| ApiError::openapi_upload(locale, "invalid_utf8"))?;
if legacy_v1 {
return crank_import::rest::preview_document_legacy_v1(document)
.map(|preview| ParsedOpenApiPreview {
preview,
ir_fingerprint: String::new(),
})
.map_err(|_| ApiError::openapi_upload(locale, "invalid_document"));
}
let digest = crank_import::rest::SourceDigest::parse(digest)
.map_err(|_| ApiError::openapi_upload(locale, "source_integrity"))?;
let ir = crank_import::rest::normalize_verified_bundle(
document,
digest,
&snapshots,
&normalization_config,
)
.map_err(|error| match error {
crank_import::rest::ImportParseError::NoMethods => {
ApiError::openapi_upload(locale, "no_methods")
}
_ => ApiError::openapi_upload(locale, "invalid_document"),
})?;
let ir_fingerprint = preview_digest(
&serde_json::to_value(&ir)
.map_err(|_| ApiError::openapi_upload(locale, "invalid_document"))?,
)?;
Ok::<_, ApiError>(ParsedOpenApiPreview {
preview: crank_import::rest::preview_from_ir(&ir),
ir_fingerprint,
})
});
let remaining = OPENAPI_PARSE_DEADLINE
.checked_sub(started.elapsed())
.unwrap_or(std::time::Duration::ZERO);
tokio::time::timeout(remaining, parsing)
.await
.map_err(|_| ApiError::openapi_upload(locale, "parser_unavailable"))?
.map_err(|_| ApiError::openapi_upload(locale, "parser_unavailable"))?
}
pub(super) fn artifact_error(locale: OpenApiUploadLocale, error: ArtifactError) -> ApiError {
match error {
ArtifactError::EmptySource => ApiError::openapi_upload(locale, "empty_file"),
ArtifactError::SourceTooLarge => ApiError::openapi_upload(locale, "file_too_large"),
ArtifactError::Integrity => ApiError::openapi_upload(locale, "source_integrity"),
ArtifactError::Storage
| ArtifactError::NotFound
| ArtifactError::InvalidReference
| ArtifactError::UnsafeRoot => ApiError::openapi_upload(locale, "storage_unavailable"),
}
}
pub(super) fn openapi_source_error(locale: OpenApiUploadLocale, error: RegistryError) -> ApiError {
match error {
RegistryError::SourceNotFound { .. } | RegistryError::SourceUnavailable => {
ApiError::openapi_upload(locale, "source_unavailable")
}
RegistryError::SourceIntegrity => ApiError::openapi_upload(locale, "source_integrity"),
other => ApiError::from(other),
}
}
pub(super) fn preview_digest(preview: &serde_json::Value) -> Result<String, ApiError> {
let canonical =
serde_json::to_vec(preview).map_err(|error| ApiError::internal(error.to_string()))?;
Ok(format!("{:x}", Sha256::digest(canonical)))
}
fn verify_preview_contract(
payload: &serde_json::Value,
parsed: &ParsedOpenApiPreview,
locale: OpenApiUploadLocale,
) -> Result<(), ApiError> {
// Pre-fingerprint jobs are legacy rolling-upgrade records. They retain the
// old reparse behavior; new jobs fail closed if parser output drifts or a
// persisted preview has been changed.
let expected = payload
.get("preview_digest")
.and_then(serde_json::Value::as_str);
if payload.get("normalization").is_some() && expected.is_none() {
return Err(ApiError::openapi_upload(locale, "source_integrity"));
}
let Some(expected) = expected else {
return Ok(());
};
let actual = preview_digest(
&serde_json::to_value(&parsed.preview)
.map_err(|error| ApiError::internal(error.to_string()))?,
)?;
if actual == expected {
if let Some(normalization) = payload.get("normalization") {
let normalizer = normalization
.get("normalizer_version")
.and_then(serde_json::Value::as_str);
let projection = normalization
.get("projection_version")
.and_then(serde_json::Value::as_str);
let fingerprint = normalization
.get("ir_fingerprint")
.and_then(serde_json::Value::as_str);
if normalizer != Some(NORMALIZER_VERSION)
|| projection != Some(PROJECTION_VERSION)
|| fingerprint != Some(parsed.ir_fingerprint.as_str())
{
return Err(ApiError::openapi_upload(locale, "source_integrity"));
}
}
Ok(())
} else {
Err(ApiError::openapi_upload(locale, "source_integrity"))
}
}
pub(super) fn preview_has_blocker(
preview: &crank_import::rest::ImportPreview,
selected_operation_keys: &BTreeSet<String>,
) -> bool {
preview
.findings
.iter()
.any(|finding| finding.severity == ImportFindingSeverity::Error)
|| preview
.groups
.iter()
.flat_map(|group| group.operations.iter())
.filter(|operation| selected_operation_keys.contains(&operation.key))
.any(|operation| {
operation
.findings
.iter()
.any(|finding| finding.severity == ImportFindingSeverity::Error)
|| (!operation.security_requirements.alternatives.is_empty()
&& !operation.security_requirements.alternatives.iter().any(
|alternative| alternative.supported && alternative.schemes.is_empty(),
))
})
}
+466 -21
View File
@@ -1,28 +1,52 @@
use base64::{Engine as _, engine::general_purpose::URL_SAFE_NO_PAD};
use crank_core::{
AgentId, ApprovalRequestId, ApprovalRequestStatus, InvocationLogId, OperationId, UsagePeriod,
WorkspaceId,
};
use crank_registry::{
ApprovalRequestRecord, ExpireApprovalRequest, InvocationLogRecord, ListApprovalRequestsQuery,
ListInvocationLogsQuery, UsageQuery, UsageRollupRecord,
ApprovalRequestRecord, DecideApprovalRequest, ExpireApprovalRequest, InvocationLogRecord,
InvocationRetentionOutcome, ListApprovalRequestsQuery, ListInvocationLogsQuery, UsageQuery,
UsageRollupRecord,
};
use serde_json::json;
use time::OffsetDateTime;
use time::{OffsetDateTime, format_description::well_known::Rfc3339};
use tracing::instrument;
use crate::{
error::ApiError,
service::{
AdminService, ApprovalsQuery, LogsQuery, UsageOverviewResponse, UsageRequestQuery,
usage_window,
AdminService, ApprovalDecisionPayload, ApprovalsQuery, LogsListResponse, LogsQuery,
UsageOverviewResponse, UsageRequestQuery, usage_window,
},
};
const LOG_LIST_DEFAULT_LIMIT: u32 = 100;
const LOG_LIST_MAX_LIMIT: u32 = 200;
const LOG_CSV_MAX_ROWS: u32 = 1_000;
const MAX_EXPLICIT_USAGE_WINDOW_DAYS: i64 = 90;
#[derive(serde::Serialize, serde::Deserialize)]
struct LogsCursor {
created_at: String,
id: String,
}
fn safe_approval_record(mut record: ApprovalRequestRecord) -> ApprovalRequestRecord {
record.approval.request_payload =
crank_core::sanitize_invocation_preview(&record.approval.request_payload);
record.approval.response_payload = record
.approval
.response_payload
.as_ref()
.map(crank_core::sanitize_invocation_preview);
record
}
impl AdminService {
pub async fn cleanup_invocation_logs_before(
&self,
cutoff: OffsetDateTime,
) -> Result<u64, ApiError> {
) -> Result<InvocationRetentionOutcome, ApiError> {
self.registry
.delete_invocation_logs_before(cutoff)
.await
@@ -34,25 +58,45 @@ impl AdminService {
&self,
workspace_id: &WorkspaceId,
query: LogsQuery,
) -> Result<Vec<InvocationLogRecord>, ApiError> {
) -> Result<LogsListResponse, ApiError> {
self.ensure_workspace_exists(workspace_id).await?;
let operation_id = query.operation_id.as_deref().map(OperationId::new);
let agent_id = query.agent_id.as_deref().map(AgentId::new);
let (_, created_after, _) = usage_window(query.period.unwrap_or(UsagePeriod::Last7Days))?;
let (_, created_after, created_before, _) =
resolve_usage_window(query.period.unwrap_or(UsagePeriod::Last7Days), &query)?;
let (cursor_created_at, cursor_id) = decode_logs_cursor(query.cursor.as_deref())?;
let limit = query
.limit
.unwrap_or(LOG_LIST_DEFAULT_LIMIT)
.clamp(1, LOG_LIST_MAX_LIMIT);
self.registry
let mut items = self
.registry
.list_invocation_logs(ListInvocationLogsQuery {
workspace_id,
level: query.level,
status: query.status,
outcome_group: query.outcome_group,
search_text: query.search.as_deref(),
source: query.source,
operation_id: operation_id.as_ref(),
agent_id: agent_id.as_ref(),
created_after: Some(&created_after),
limit: query.limit.unwrap_or(100),
created_before: Some(&created_before),
cursor_created_at: cursor_created_at.as_deref(),
cursor_id: cursor_id.as_ref(),
limit: limit.saturating_add(1),
})
.await
.map_err(ApiError::from)
.await?;
let next_cursor = if items.len() > limit as usize {
items.truncate(limit as usize);
items.last().map(encode_logs_cursor).transpose()?
} else {
None
};
Ok(LogsListResponse { items, next_cursor })
}
#[instrument(skip(self))]
@@ -72,6 +116,69 @@ impl AdminService {
})
}
#[instrument(skip(self))]
pub async fn export_logs_csv(
&self,
workspace_id: &WorkspaceId,
mut query: LogsQuery,
) -> Result<String, ApiError> {
query.limit = Some(
query
.limit
.unwrap_or(LOG_CSV_MAX_ROWS)
.clamp(1, LOG_CSV_MAX_ROWS),
);
query.cursor = None;
let page = self.list_logs(workspace_id, query).await?;
let mut csv = String::from(
"created_at,level,status,source,agent,operation,operation_version,duration_ms,request_id,trace_id,stage,error_code,message,request_preview,response_preview\n",
);
for record in page.items {
let log = record.log;
let row = [
log.created_at
.format(&Rfc3339)
.map_err(|error| ApiError::internal(error.to_string()))?,
serialize_csv_text(log.level)?,
serialize_csv_text(log.status)?,
serialize_csv_text(log.source)?,
record
.agent_display_name
.or(record.agent_slug)
.unwrap_or_default(),
record.operation_display_name,
log.operation_version
.map(|value| value.to_string())
.unwrap_or_default(),
log.duration_ms.to_string(),
log.request_id.unwrap_or_default(),
log.trace_id.unwrap_or_default(),
log.execution_stage
.map(serialize_csv_text)
.transpose()?
.unwrap_or_default(),
log.execution_error_code
.map(serialize_csv_text)
.transpose()?
.unwrap_or_default(),
log.message,
serde_json::to_string(&log.request_preview)
.map_err(|error| ApiError::internal(error.to_string()))?,
serde_json::to_string(&log.response_preview)
.map_err(|error| ApiError::internal(error.to_string()))?,
];
csv.push_str(&row.into_iter().map(csv_cell).collect::<Vec<_>>().join(","));
csv.push('\n');
if csv.len() > 1_048_576 {
return Err(ApiError::unprocessable_with_context(
"logs CSV export exceeded the bounded response size",
json!({ "error_code": "logs_csv_too_large" }),
));
}
}
Ok(csv)
}
#[instrument(skip(self))]
pub async fn list_approvals(
&self,
@@ -92,7 +199,7 @@ impl AdminService {
for record in records {
let record = self.normalize_approval_record(record).await?;
if query.status.is_none() || record.approval.status == query.status.unwrap() {
normalized.push(record);
normalized.push(safe_approval_record(record));
}
}
@@ -117,7 +224,114 @@ impl AdminService {
)
})?;
self.normalize_approval_record(record).await
self.normalize_approval_record(record)
.await
.map(safe_approval_record)
}
#[instrument(skip(self, payload))]
pub async fn approve_approval(
&self,
workspace_id: &WorkspaceId,
approval_id: &ApprovalRequestId,
payload: ApprovalDecisionPayload,
) -> Result<ApprovalRequestRecord, ApiError> {
if !payload.approve.eq_ignore_ascii_case("yes") {
return Err(ApiError::unprocessable_with_context(
"approve must be yes for approval confirmation",
json!({ "error_code": "invalid_decision_payload" }),
));
}
self.decide_admin_approval(
workspace_id,
approval_id,
ApprovalRequestStatus::Approved,
Some(json!({"approve": "yes"})),
payload.note.as_deref(),
)
.await
}
#[instrument(skip(self, payload))]
pub async fn deny_approval(
&self,
workspace_id: &WorkspaceId,
approval_id: &ApprovalRequestId,
payload: ApprovalDecisionPayload,
) -> Result<ApprovalRequestRecord, ApiError> {
if !payload.approve.eq_ignore_ascii_case("no") {
return Err(ApiError::unprocessable_with_context(
"approve must be no for approval denial",
json!({ "error_code": "invalid_decision_payload" }),
));
}
self.decide_admin_approval(
workspace_id,
approval_id,
ApprovalRequestStatus::Denied,
Some(json!({"approve": "no"})),
payload.note.as_deref(),
)
.await
}
async fn decide_admin_approval(
&self,
workspace_id: &WorkspaceId,
approval_id: &ApprovalRequestId,
status: ApprovalRequestStatus,
response_payload: Option<serde_json::Value>,
decision_note: Option<&str>,
) -> Result<ApprovalRequestRecord, ApiError> {
self.ensure_workspace_exists(workspace_id).await?;
let current = self
.registry
.get_approval_request(workspace_id, approval_id)
.await?
.ok_or_else(|| {
ApiError::not_found_with_context(
format!("approval request {} was not found", approval_id.as_str()),
json!({ "approval_id": approval_id.as_str() }),
)
})?;
let normalized = self.normalize_approval_record(current).await?;
if normalized.approval.status != ApprovalRequestStatus::Pending {
return Err(ApiError::conflict_with_context(
"approval request is not pending",
json!({
"approval_id": approval_id.as_str(),
"status": normalized.approval.status,
"error_code": "approval_state_conflict"
}),
));
}
let decided = self
.registry
.decide_approval_request(DecideApprovalRequest {
workspace_id,
agent_id: &normalized.approval.agent_id,
approval_id,
operation_id: &normalized.approval.operation_id,
operation_version: normalized.approval.operation_version,
request_payload: &normalized.approval.request_payload,
status,
decided_at: OffsetDateTime::now_utc(),
decided_by_key_id: None,
response_payload,
decision_note,
})
.await?
.ok_or_else(|| {
ApiError::conflict_with_context(
"approval request state changed before decision was recorded",
json!({
"approval_id": approval_id.as_str(),
"error_code": "approval_state_conflict"
}),
)
})?;
Ok(safe_approval_record(decided))
}
async fn normalize_approval_record(
@@ -149,13 +363,14 @@ impl AdminService {
query: UsageRequestQuery,
) -> Result<UsageOverviewResponse, ApiError> {
self.ensure_workspace_exists(workspace_id).await?;
let (period, created_after, bucket) =
usage_window(query.period.unwrap_or(UsagePeriod::Last7Days))?;
let (period, created_after, created_before, bucket) =
resolve_usage_request_window(query.period.unwrap_or(UsagePeriod::Last7Days), &query)?;
let usage_query = UsageQuery {
workspace_id,
period,
source: query.source,
created_after: &created_after,
created_before: &created_before,
bucket,
};
@@ -168,16 +383,104 @@ impl AdminService {
.registry
.list_usage_by_operation(usage_query.clone())
.await?;
let agents = self.registry.list_usage_by_agent(usage_query).await?;
let agents = self
.registry
.list_usage_by_agent(usage_query.clone())
.await?;
let outcomes = self.registry.list_usage_outcomes(usage_query).await?;
Ok(UsageOverviewResponse {
summary,
timeline,
operations,
agents,
outcomes,
})
}
#[instrument(skip(self))]
pub async fn export_usage_csv(
&self,
workspace_id: &WorkspaceId,
query: UsageRequestQuery,
) -> Result<String, ApiError> {
let usage = self.get_usage_overview(workspace_id, query).await?;
let mut csv = String::from(
"kind,name,group,error_code,calls_total,calls_error,p50_ms,p95_ms,p99_ms\n",
);
for operation in usage.operations {
csv.push_str(
&[
"operation".to_owned(),
operation.operation_display_name,
String::new(),
String::new(),
operation.calls_total.to_string(),
operation.calls_error.to_string(),
operation.p50_ms.to_string(),
operation.p95_ms.to_string(),
operation.p99_ms.to_string(),
]
.into_iter()
.map(csv_cell)
.collect::<Vec<_>>()
.join(","),
);
csv.push('\n');
}
for agent in usage.agents {
csv.push_str(
&[
"agent".to_owned(),
agent.agent_display_name,
String::new(),
String::new(),
agent.calls_total.to_string(),
agent.calls_error.to_string(),
agent.p50_ms.to_string(),
agent.p95_ms.to_string(),
agent.p99_ms.to_string(),
]
.into_iter()
.map(csv_cell)
.collect::<Vec<_>>()
.join(","),
);
csv.push('\n');
}
for outcome in usage.outcomes {
csv.push_str(
&[
"outcome".to_owned(),
String::new(),
serialize_csv_text(outcome.group)?,
outcome
.execution_error_code
.map(serialize_csv_text)
.transpose()?
.unwrap_or_default(),
outcome.calls_total.to_string(),
String::new(),
outcome.p50_ms.to_string(),
outcome.p95_ms.to_string(),
outcome.p99_ms.to_string(),
]
.into_iter()
.map(csv_cell)
.collect::<Vec<_>>()
.join(","),
);
csv.push('\n');
}
if csv.len() > 1_048_576 {
return Err(ApiError::unprocessable_with_context(
"usage CSV export exceeded the bounded response size",
json!({ "error_code": "usage_csv_too_large" }),
));
}
Ok(csv)
}
#[instrument(skip(self))]
pub async fn get_operation_usage(
&self,
@@ -186,8 +489,8 @@ impl AdminService {
query: UsageRequestQuery,
) -> Result<UsageRollupRecord, ApiError> {
self.get_operation(workspace_id, operation_id).await?;
let (period, created_after, bucket) =
usage_window(query.period.unwrap_or(UsagePeriod::Last7Days))?;
let (period, created_after, created_before, bucket) =
resolve_usage_request_window(query.period.unwrap_or(UsagePeriod::Last7Days), &query)?;
self.registry
.get_usage_for_operation(
@@ -196,6 +499,7 @@ impl AdminService {
period,
source: query.source,
created_after: &created_after,
created_before: &created_before,
bucket,
},
operation_id,
@@ -220,8 +524,8 @@ impl AdminService {
query: UsageRequestQuery,
) -> Result<UsageRollupRecord, ApiError> {
self.get_agent(workspace_id, agent_id).await?;
let (period, created_after, bucket) =
usage_window(query.period.unwrap_or(UsagePeriod::Last7Days))?;
let (period, created_after, created_before, bucket) =
resolve_usage_request_window(query.period.unwrap_or(UsagePeriod::Last7Days), &query)?;
self.registry
.get_usage_for_agent(
@@ -230,6 +534,7 @@ impl AdminService {
period,
source: query.source,
created_after: &created_after,
created_before: &created_before,
bucket,
},
agent_id,
@@ -243,3 +548,143 @@ impl AdminService {
})
}
}
fn resolve_usage_window(
period: UsagePeriod,
query: &LogsQuery,
) -> Result<(UsagePeriod, String, String, crank_registry::UsageBucket), ApiError> {
resolve_explicit_or_period_window(
period,
query.created_after.as_deref(),
query.created_before.as_deref(),
)
}
fn resolve_usage_request_window(
period: UsagePeriod,
query: &UsageRequestQuery,
) -> Result<(UsagePeriod, String, String, crank_registry::UsageBucket), ApiError> {
resolve_explicit_or_period_window(
period,
query.created_after.as_deref(),
query.created_before.as_deref(),
)
}
fn resolve_explicit_or_period_window(
period: UsagePeriod,
created_after: Option<&str>,
created_before: Option<&str>,
) -> Result<(UsagePeriod, String, String, crank_registry::UsageBucket), ApiError> {
match (created_after, created_before) {
(None, None) => usage_window(period),
(Some(_), None) | (None, Some(_)) => Err(invalid_usage_window(
"usage window requires both created_after and created_before",
)),
(Some(after), Some(before)) => {
let after = OffsetDateTime::parse(after, &Rfc3339)
.map_err(|_| invalid_usage_window("created_after must be RFC3339 UTC"))?;
let before = OffsetDateTime::parse(before, &Rfc3339)
.map_err(|_| invalid_usage_window("created_before must be RFC3339 UTC"))?;
if after >= before {
return Err(invalid_usage_window(
"created_after must be before created_before",
));
}
let duration = before - after;
if duration > time::Duration::days(MAX_EXPLICIT_USAGE_WINDOW_DAYS) {
return Err(invalid_usage_window("usage window exceeds maximum range"));
}
let bucket = if duration <= time::Duration::days(1) {
crank_registry::UsageBucket::Hour
} else if duration <= time::Duration::days(14) {
crank_registry::UsageBucket::Day
} else if duration <= time::Duration::days(45) {
crank_registry::UsageBucket::Week
} else {
crank_registry::UsageBucket::Month
};
Ok((
period,
after
.format(&Rfc3339)
.map_err(|error| ApiError::internal(error.to_string()))?,
before
.format(&Rfc3339)
.map_err(|error| ApiError::internal(error.to_string()))?,
bucket,
))
}
}
}
fn invalid_usage_window(message: &'static str) -> ApiError {
ApiError::unprocessable_with_context(message, json!({ "error_code": "usage_window_invalid" }))
}
fn decode_logs_cursor(
cursor: Option<&str>,
) -> Result<(Option<String>, Option<InvocationLogId>), ApiError> {
let Some(cursor) = cursor else {
return Ok((None, None));
};
if cursor.len() > 512 {
return Err(invalid_logs_cursor());
}
let bytes = URL_SAFE_NO_PAD
.decode(cursor)
.map_err(|_| invalid_logs_cursor())?;
let decoded: LogsCursor = serde_json::from_slice(&bytes).map_err(|_| invalid_logs_cursor())?;
OffsetDateTime::parse(&decoded.created_at, &Rfc3339).map_err(|_| invalid_logs_cursor())?;
if decoded.id.is_empty()
|| decoded.id.len() > 128
|| decoded.id.contains(';')
|| decoded.id.contains(',')
{
return Err(invalid_logs_cursor());
}
Ok((
Some(decoded.created_at),
Some(InvocationLogId::new(decoded.id)),
))
}
fn encode_logs_cursor(record: &InvocationLogRecord) -> Result<String, ApiError> {
let cursor = LogsCursor {
created_at: record
.log
.created_at
.format(&Rfc3339)
.map_err(|error| ApiError::internal(error.to_string()))?,
id: record.log.id.as_str().to_owned(),
};
let encoded =
serde_json::to_vec(&cursor).map_err(|error| ApiError::internal(error.to_string()))?;
Ok(URL_SAFE_NO_PAD.encode(encoded))
}
fn invalid_logs_cursor() -> ApiError {
ApiError::unprocessable_with_context(
"logs cursor is invalid",
json!({ "error_code": "logs_cursor_invalid" }),
)
}
fn serialize_csv_text<T: serde::Serialize>(value: T) -> Result<String, ApiError> {
serde_json::to_value(value)
.map_err(|error| ApiError::internal(error.to_string()))?
.as_str()
.map(ToOwned::to_owned)
.ok_or_else(|| ApiError::internal("failed to serialize CSV enum value"))
}
fn csv_cell(mut value: String) -> String {
if value
.chars()
.next()
.is_some_and(|ch| matches!(ch, '=' | '+' | '-' | '@' | '\t' | '\r' | '\n'))
{
value.insert(0, '\'');
}
format!("\"{}\"", value.replace('"', "\"\""))
}
+406
View File
@@ -0,0 +1,406 @@
use crank_core::{
OnboardingProjection, OnboardingStepId, OperationId, ProductEventId, ProductEventKind,
WorkspaceId,
};
use crank_registry::{
OnboardingImportContext, OnboardingPresentationMilestone, RecordOnboardingMilestoneRequest,
RegisterOnboardingImportContextRequest, RegistryError, SelectOnboardingImportDraftRequest,
};
use serde_json::json;
use time::OffsetDateTime;
use crate::{
error::ApiError,
service::{
AdminService, OnboardingEventPayload, OnboardingEventResponse, OnboardingFirstCallEvidence,
OnboardingImportChoiceView, OnboardingImportContextView, OnboardingImportDriftView,
OnboardingResponse, OnboardingStepView, RegisterOnboardingImportContextPayload,
RegisterOnboardingImportContextResponse, ResetOnboardingSelectionResponse,
SelectOnboardingImportDraftPayload, SelectOnboardingImportDraftResponse, format_timestamp,
new_prefixed_id,
},
};
impl AdminService {
pub async fn get_onboarding(
&self,
workspace_id: &WorkspaceId,
) -> Result<OnboardingResponse, ApiError> {
self.ensure_workspace_exists(workspace_id).await?;
let snapshot = self
.registry
.ensure_onboarding_eligibility_snapshot(workspace_id, OffsetDateTime::now_utc())
.await?;
self.map_onboarding_response(snapshot.projection, snapshot.import_context)
.await
}
pub async fn record_onboarding_event(
&self,
workspace_id: &WorkspaceId,
payload: OnboardingEventPayload,
) -> Result<OnboardingEventResponse, ApiError> {
self.ensure_workspace_exists(workspace_id).await?;
if payload.completed_steps.is_some() || payload.event == "completed" {
return Err(onboarding_event_not_allowed());
}
if payload.idempotency_key.is_empty() || payload.idempotency_key.len() > 256 {
return Err(ApiError::validation(
"idempotency_key must contain 1..256 bytes",
));
}
if payload.idempotency_key.starts_with("onboarding:") {
return Err(onboarding_event_not_allowed());
}
let (milestone, event_kind) = match payload.event.as_str() {
"started" => (
OnboardingPresentationMilestone::Started,
ProductEventKind::OnboardingStarted,
),
"resumed" => (
OnboardingPresentationMilestone::Resumed,
ProductEventKind::OnboardingResumed,
),
"dismissed" => (
OnboardingPresentationMilestone::Dismissed,
ProductEventKind::OnboardingDismissed,
),
"abandoned" => (
OnboardingPresentationMilestone::Abandoned,
ProductEventKind::OnboardingAbandoned,
),
_ => return Err(onboarding_event_not_allowed()),
};
// Presentation events must never exist outside the server-owned cohort,
// including a direct API call before the first UI snapshot GET.
self.registry
.ensure_onboarding_eligibility_snapshot(workspace_id, OffsetDateTime::now_utc())
.await?;
let outcome = self
.registry
.record_onboarding_milestone_snapshot(RecordOnboardingMilestoneRequest {
workspace_id,
event_id: &ProductEventId::new(new_prefixed_id("pe")),
milestone,
idempotency_key: &payload.idempotency_key,
expected_revision: payload.expected_revision,
occurred_at: OffsetDateTime::now_utc(),
eligible_since: None,
})
.await;
let outcome = match outcome {
Ok(outcome) => outcome,
Err(RegistryError::OnboardingStaleRevision) => {
let replay = self
.registry
.get_product_event_by_idempotency_key(workspace_id, &payload.idempotency_key)
.await?;
if replay
.as_ref()
.is_some_and(|record| record.event.kind == event_kind)
{
let latest = self
.registry
.ensure_onboarding_eligibility_snapshot(
workspace_id,
OffsetDateTime::now_utc(),
)
.await?;
return Ok(OnboardingEventResponse {
accepted: false,
onboarding: self
.map_onboarding_response(latest.projection, latest.import_context)
.await?,
});
}
if replay.is_some() {
return Err(ApiError::conflict_with_context(
"idempotency key was already used for another onboarding event",
json!({"error_code":"onboarding_idempotency_conflict","recovery":"use_original_event"}),
));
}
return Err(ApiError::conflict_with_context(
"onboarding state changed; reload before retrying",
json!({"error_code":"onboarding_stale_revision","recovery":"reload"}),
));
}
Err(RegistryError::InvalidExecutionRecord {
field: "product_event.idempotency_conflict",
}) => {
return Err(ApiError::conflict_with_context(
"idempotency key was already used for another onboarding event",
json!({"error_code":"onboarding_idempotency_conflict","recovery":"use_original_event"}),
));
}
Err(other) => return Err(other.into()),
};
Ok(OnboardingEventResponse {
accepted: outcome.accepted,
onboarding: self
.map_onboarding_response(
outcome.snapshot.projection,
outcome.snapshot.import_context,
)
.await?,
})
}
pub async fn reset_onboarding_selection(
&self,
workspace_id: &WorkspaceId,
expected_revision: i64,
) -> Result<ResetOnboardingSelectionResponse, ApiError> {
self.ensure_workspace_exists(workspace_id).await?;
let snapshot = self
.registry
.reset_onboarding_selection_snapshot(
workspace_id,
expected_revision,
OffsetDateTime::now_utc(),
)
.await
.map_err(|error| match error {
RegistryError::OnboardingStaleRevision => ApiError::conflict_with_context(
"onboarding state changed; reload before resetting the selection",
json!({"error_code":"onboarding_stale_revision","recovery":"reload"}),
),
other => ApiError::from(other),
})?;
Ok(ResetOnboardingSelectionResponse {
selection_reset: true,
onboarding: self
.map_onboarding_response(snapshot.projection, snapshot.import_context)
.await?,
})
}
pub async fn register_onboarding_import_context(
&self,
workspace_id: &WorkspaceId,
payload: RegisterOnboardingImportContextPayload,
) -> Result<RegisterOnboardingImportContextResponse, ApiError> {
self.ensure_workspace_exists(workspace_id).await?;
validate_application_identity(&payload.application_idempotency_key, &payload.plan_digest)?;
let snapshot = self
.registry
.register_onboarding_import_context_snapshot(RegisterOnboardingImportContextRequest {
workspace_id,
application_idempotency_key: &payload.application_idempotency_key,
plan_digest: &payload.plan_digest,
expected_revision: payload.expected_revision,
occurred_at: OffsetDateTime::now_utc(),
})
.await?;
Ok(RegisterOnboardingImportContextResponse {
registered: true,
onboarding: self
.map_onboarding_response(snapshot.projection, snapshot.import_context)
.await?,
})
}
pub async fn select_onboarding_import_draft(
&self,
workspace_id: &WorkspaceId,
payload: SelectOnboardingImportDraftPayload,
) -> Result<SelectOnboardingImportDraftResponse, ApiError> {
self.ensure_workspace_exists(workspace_id).await?;
validate_application_identity(&payload.application_idempotency_key, &payload.plan_digest)?;
if payload.operation_id.is_empty() || payload.operation_id.len() > 128 {
return Err(ApiError::validation(
"operation_id must contain 1..128 bytes",
));
}
if payload.operation_version == 0 {
return Err(ApiError::validation("operation_version must be positive"));
}
let snapshot = self
.registry
.select_onboarding_import_draft_snapshot(SelectOnboardingImportDraftRequest {
workspace_id,
application_idempotency_key: &payload.application_idempotency_key,
plan_digest: &payload.plan_digest,
operation_id: &OperationId::new(payload.operation_id),
operation_version: payload.operation_version,
expected_revision: payload.expected_revision,
occurred_at: OffsetDateTime::now_utc(),
})
.await?;
Ok(SelectOnboardingImportDraftResponse {
selected: true,
onboarding: self
.map_onboarding_response(snapshot.projection, snapshot.import_context)
.await?,
})
}
async fn map_onboarding_response(
&self,
projection: OnboardingProjection,
import_context: Option<OnboardingImportContext>,
) -> Result<OnboardingResponse, ApiError> {
let endpoint = if let Some(agent_id) = projection.agent_id.as_ref() {
let workspace = self.get_workspace(&projection.workspace_id).await?;
let agent = self
.registry
.get_agent_summary(&projection.workspace_id, agent_id)
.await?
.ok_or_else(|| ApiError::internal("onboarding agent disappeared"))?;
Some(self.public_agent_mcp_endpoint(&workspace.workspace.slug, &agent.slug))
} else {
None
};
let first_call = match (
projection.first_call_log_id.as_ref(),
projection.agent_id.as_ref(),
projection.platform_api_key_id.as_ref(),
projection.operation_id.as_ref(),
projection.operation_version,
projection.first_call_tool_name.as_ref(),
projection.first_call_at,
) {
(
Some(log_id),
Some(agent_id),
Some(key_id),
Some(operation_id),
Some(operation_version),
Some(tool_name),
Some(occurred_at),
) => Some(OnboardingFirstCallEvidence {
log_id: log_id.as_str().to_owned(),
agent_id: agent_id.as_str().to_owned(),
key_id: key_id.as_str().to_owned(),
operation_id: operation_id.as_str().to_owned(),
operation_version,
tool_name: tool_name.clone(),
occurred_at: format_timestamp(occurred_at),
request_id: projection.first_call_request_id.clone(),
trace_id: projection.first_call_trace_id.clone(),
}),
_ => None,
};
let first_incomplete = projection.steps.iter().position(|step| !step.completed);
let import_context = import_context.map(map_import_context);
Ok(OnboardingResponse {
schema_version: 1,
workspace_id: projection.workspace_id.as_str().to_owned(),
revision: projection.revision,
status: if projection.completed {
"complete".to_owned()
} else {
"in_progress".to_owned()
},
completed: projection.completed,
eligible_since: projection.eligible_since.map(format_timestamp),
steps: projection
.steps
.iter()
.enumerate()
.map(|(index, step)| {
let status = if step.completed {
"complete"
} else if projection.was_completed {
"regressed"
} else if Some(index) == first_incomplete {
"current"
} else {
"pending"
};
let reason_code = match status {
"complete" => "authoritative_evidence_present",
"regressed" => "authoritative_evidence_regressed",
"current" => "authoritative_evidence_missing",
_ => "prerequisite_incomplete",
};
OnboardingStepView {
id: step.id,
completed: step.completed,
status: status.to_owned(),
action_code: action_code(step.id).to_owned(),
reason_code: reason_code.to_owned(),
}
})
.collect(),
operation_id: projection.operation_id.map(|id| id.as_str().to_owned()),
operation_version: projection.operation_version,
agent_id: projection.agent_id.map(|id| id.as_str().to_owned()),
catalog_revision: projection.catalog_revision,
platform_api_key_id: projection
.platform_api_key_id
.map(|id| id.as_str().to_owned()),
mcp_endpoint: endpoint,
first_call,
import_context,
})
}
}
fn map_import_context(context: OnboardingImportContext) -> OnboardingImportContextView {
OnboardingImportContextView {
application_idempotency_key: context.application_idempotency_key,
plan_digest: context.plan_digest,
report: context.report,
choices: context
.choices
.into_iter()
.map(|choice| OnboardingImportChoiceView {
key: choice.key,
name: choice.name,
outcome: choice.outcome,
operation_id: choice.operation_id,
version: choice.version,
reason: choice.reason,
selectable: choice.selectable,
})
.collect(),
selected_operation_id: context
.selected_operation_id
.map(|id| id.as_str().to_owned()),
selected_operation_version: context.selected_operation_version,
drift: context.drift.map(|drift| OnboardingImportDriftView {
reason: drift.reason,
current_operation_version: drift.current_operation_version,
}),
}
}
fn validate_application_identity(
application_idempotency_key: &str,
plan_digest: &str,
) -> Result<(), ApiError> {
if application_idempotency_key.is_empty() || application_idempotency_key.len() > 128 {
return Err(ApiError::validation(
"application_idempotency_key must contain 1..128 bytes",
));
}
let valid_digest = plan_digest.len() == 64
&& plan_digest
.bytes()
.all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte));
if !valid_digest {
return Err(ApiError::validation(
"plan_digest must be 64 lowercase hex characters",
));
}
Ok(())
}
fn action_code(step: OnboardingStepId) -> &'static str {
match step {
OnboardingStepId::Operation => "create_operation",
OnboardingStepId::Test => "test_operation",
OnboardingStepId::PublishOperation => "publish_operation",
OnboardingStepId::Agent => "publish_agent",
OnboardingStepId::Key => "create_mcp_key",
OnboardingStepId::McpConnection => "connect_mcp_client",
OnboardingStepId::FirstCall => "call_tool",
}
}
fn onboarding_event_not_allowed() -> ApiError {
ApiError::unprocessable_with_context(
"client cannot complete authoritative onboarding steps",
json!({"error_code":"onboarding_event_not_allowed"}),
)
}
@@ -93,7 +93,14 @@ pub(super) fn validate_idempotency_policy(
}
match target {
Target::Rest(rest_target) if rest_target.method != crank_core::HttpMethod::Get => {}
Target::Rest(rest_target)
if matches!(
rest_target.method,
crank_core::HttpMethod::Post
| crank_core::HttpMethod::Put
| crank_core::HttpMethod::Patch
| crank_core::HttpMethod::Delete
) => {}
_ => {
return Err(ApiError::validation_with_context(
"idempotency is supported only for mutating REST operations".to_owned(),
@@ -206,6 +213,9 @@ mod tests {
method: HttpMethod::Get,
path_template: "/catalog".to_owned(),
static_headers: BTreeMap::new(),
request_contract: None,
response_contract: Vec::new(),
multipart_field_mime_types: BTreeMap::new(),
});
let result = validate_response_cache_policy(&target, &cacheable_execution_config());
@@ -233,6 +243,9 @@ mod tests {
method: HttpMethod::Post,
path_template: "/catalog".to_owned(),
static_headers: BTreeMap::new(),
request_contract: None,
response_contract: Vec::new(),
multipart_field_mime_types: BTreeMap::new(),
});
let error =
@@ -252,6 +265,9 @@ mod tests {
method: HttpMethod::Post,
path_template: "/orders".to_owned(),
static_headers: BTreeMap::new(),
request_contract: None,
response_contract: Vec::new(),
multipart_field_mime_types: BTreeMap::new(),
});
let result = validate_idempotency_policy(
@@ -269,6 +285,9 @@ mod tests {
method: HttpMethod::Get,
path_template: "/orders".to_owned(),
static_headers: BTreeMap::new(),
request_contract: None,
response_contract: Vec::new(),
multipart_field_mime_types: BTreeMap::new(),
});
let error = validate_idempotency_policy(
@@ -291,6 +310,9 @@ mod tests {
method: HttpMethod::Post,
path_template: "/orders".to_owned(),
static_headers: BTreeMap::new(),
request_contract: None,
response_contract: Vec::new(),
multipart_field_mime_types: BTreeMap::new(),
});
let mut config = idempotent_execution_config(IdempotencyMode::Required);
let policy = config.idempotency.as_mut().unwrap();
+252 -97
View File
@@ -1,14 +1,17 @@
use crank_core::{
ConfigExport, ExecutionMode, ExportMode, InvocationLevel, InvocationSource, InvocationStatus,
OperationId, OperationStatus, Samples, WorkspaceId,
ConfigExport, ExecutionMode, ExecutionOrigin, ExportMode, InvocationLevel, InvocationSource,
InvocationStatus, OperationAvailability, OperationId, OperationStatus, OperationVersionState,
Samples, WorkspaceId,
};
use crank_registry::{
CreateVersionRequest, OperationVersionRecord, PublishRequest, RegistryOperation,
CreateVersionRequest, OperationStateExpectation, OperationVersionRecord, PublishRequest,
RegistryOperation,
};
use crank_runtime::{
RuntimeError, RuntimeExecutionRequest, RuntimeOperation, RuntimeRequestContext,
ExecutionAuthorization, RuntimeExecutionRequest, RuntimeOperation, RuntimeRequestContext,
};
use serde_json::{Value, json};
use sha2::{Digest, Sha256};
use time::OffsetDateTime;
use tracing::{info, instrument};
@@ -18,13 +21,33 @@ use crate::{
AdminService, CreatedOperationResponse, InvocationRecordRequest, NewVersionPayload,
OperationDetailView, OperationMutationResult, OperationPayload, OperationSummaryView,
PublishResponse, TestRunPayload, TestRunResult, UpdateOperationPayload, VersionRef,
agent_ref_map, build_request_preview, default_usage_summary, enrich_operation_summary,
format_timestamp, new_prefixed_id, now_string, runtime_error_code, today_start_utc,
tool_quality_mapping_set, tool_quality_schema_node, usage_map,
agent_ref_map, default_usage_summary, enrich_operation_summary, format_timestamp,
new_prefixed_id, now_string, today_start_utc, tool_quality_mapping_set,
tool_quality_schema_node, usage_map,
},
};
impl AdminService {
pub fn operation_state_etag(detail: &OperationDetailView) -> String {
let material = format!(
"operation-state-v2\0{}\0{}\0{}\0{:?}\0{:?}",
detail.workspace_id,
detail.id,
detail.current_draft_version,
detail.status,
detail.latest_published_version
);
format!("\"{:x}\"", Sha256::digest(material.as_bytes()))
}
pub fn operation_version_etag(record: &OperationVersionRecord) -> Result<String, ApiError> {
let bytes = serde_json::to_vec(&record.snapshot)
.map_err(|_| ApiError::internal("operation version serialization failed"))?;
let mut digest = Sha256::new();
digest.update(b"operation-version-v2\0");
digest.update(bytes);
Ok(format!("\"{:x}\"", digest.finalize()))
}
pub async fn list_operations(
&self,
workspace_id: &WorkspaceId,
@@ -84,6 +107,11 @@ impl AdminService {
let refs = agent_ref_map(agent_refs)
.remove(operation_id.as_str())
.unwrap_or_default();
let current_version = self
.registry
.get_operation_version(workspace_id, operation_id, summary.current_draft_version)
.await?
.ok_or_else(|| ApiError::internal("current operation version is unavailable"))?;
Ok(OperationDetailView {
id: summary.id.as_str().to_owned(),
@@ -94,6 +122,11 @@ impl AdminService {
protocol: summary.protocol,
security_level: summary.security_level,
status: summary.status,
availability: if summary.status == OperationStatus::Archived {
OperationAvailability::Archived
} else {
OperationAvailability::Active
},
current_draft_version: summary.current_draft_version,
latest_published_version: summary.latest_published_version,
created_at: format_timestamp(summary.created_at),
@@ -101,11 +134,15 @@ impl AdminService {
published_at: summary.published_at.map(format_timestamp),
draft_version_ref: VersionRef {
version: summary.current_draft_version,
status: summary.status,
status: if current_version.status == OperationStatus::Published {
OperationVersionState::Published
} else {
OperationVersionState::Draft
},
},
published_version_ref: summary.latest_published_version.map(|version| VersionRef {
version,
status: OperationStatus::Published,
status: OperationVersionState::Published,
}),
agent_refs: refs,
})
@@ -155,6 +192,8 @@ impl AdminService {
}
let snapshot = self.new_operation_snapshot(payload)?;
self.validate_registry_operation_in_workspace(workspace_id, &snapshot)
.await?;
let operation_id = snapshot.id.clone();
self.registry
@@ -240,6 +279,7 @@ impl AdminService {
workspace_id: &WorkspaceId,
operation_id: &OperationId,
payload: NewVersionPayload,
expected_state: &OperationStateExpectation,
) -> Result<CreatedOperationResponse, ApiError> {
self.validate_operation_payload(&payload.operation)?;
@@ -254,7 +294,27 @@ impl AdminService {
)
})?;
let now = OffsetDateTime::now_utc();
let version = summary.current_draft_version + 1;
if summary.current_draft_version != expected_state.current_draft_version
|| summary.status != expected_state.status
|| summary.latest_published_version != expected_state.latest_published_version
{
return Err(crank_registry::RegistryError::OperationStaleVersion {
operation_id: operation_id.as_str().to_owned(),
expected: expected_state.current_draft_version,
actual: summary.current_draft_version,
}
.into());
}
let version = summary
.current_draft_version
.checked_add(1)
.filter(|value| i32::try_from(*value).is_ok())
.ok_or_else(|| {
ApiError::conflict_with_context(
"operation revision limit reached",
json!({ "error_code": "operation_revision_exhausted" }),
)
})?;
let snapshot = RegistryOperation {
id: operation_id.clone(),
name: payload.operation.name,
@@ -283,13 +343,19 @@ impl AdminService {
published_at: None,
};
self.validate_registry_operation_in_workspace(workspace_id, &snapshot)
.await?;
self.registry
.create_version(CreateVersionRequest {
workspace_id,
snapshot: &snapshot,
change_note: payload.change_note.as_deref(),
created_by: None,
})
.create_version_cas(
CreateVersionRequest {
workspace_id,
snapshot: &snapshot,
change_note: payload.change_note.as_deref(),
created_by: None,
},
expected_state,
)
.await?;
info!(
name: "admin.operation.version_created",
@@ -313,21 +379,20 @@ impl AdminService {
workspace_id: &WorkspaceId,
operation_id: &OperationId,
payload: UpdateOperationPayload,
expected_state: &OperationStateExpectation,
) -> Result<OperationMutationResult, ApiError> {
let existing = self
.get_operation_version(
workspace_id,
operation_id,
self.get_operation(workspace_id, operation_id)
.await?
.current_draft_version,
expected_state.current_draft_version,
)
.await?;
let updated_at = OffsetDateTime::now_utc();
let snapshot = RegistryOperation {
id: operation_id.clone(),
name: existing.snapshot.name,
name: existing.snapshot.name.clone(),
display_name: payload.display_name,
category: payload.category,
protocol: existing.snapshot.protocol,
@@ -341,24 +406,39 @@ impl AdminService {
output_mapping: payload.output_mapping,
execution_config: payload.execution_config,
tool_description: payload.tool_description,
samples: existing.snapshot.samples,
generated_draft: existing.snapshot.generated_draft,
config_export: existing.snapshot.config_export,
samples: existing.snapshot.samples.clone(),
generated_draft: existing.snapshot.generated_draft.clone(),
config_export: existing.snapshot.config_export.clone(),
wizard_state: payload.wizard_state,
created_at: existing.snapshot.created_at,
updated_at,
published_at: existing.snapshot.published_at,
};
self.validate_registry_operation(&snapshot)?;
self.validate_registry_operation_in_workspace(workspace_id, &snapshot)
.await?;
if existing.snapshot.portable_semantically_eq(&snapshot)
&& existing.snapshot.wizard_state == snapshot.wizard_state
{
self.registry
.verify_operation_state(workspace_id, operation_id, expected_state)
.await?;
return Ok(OperationMutationResult {
operation_id: operation_id.as_str().to_owned(),
workspace_id: workspace_id.as_str().to_owned(),
version: existing.version,
status: existing.status,
updated_at: format_timestamp(existing.snapshot.updated_at),
});
}
self.registry
.update_operation_draft(workspace_id, &snapshot)
.update_operation_draft_cas(workspace_id, &snapshot, expected_state)
.await?;
Ok(OperationMutationResult {
operation_id: operation_id.as_str().to_owned(),
workspace_id: workspace_id.as_str().to_owned(),
version: snapshot.version,
version: snapshot.version.saturating_add(1),
status: snapshot.status,
updated_at: format_timestamp(updated_at),
})
@@ -370,16 +450,56 @@ impl AdminService {
workspace_id: &WorkspaceId,
operation_id: &OperationId,
version: u32,
expected_state: &OperationStateExpectation,
) -> Result<PublishResponse, ApiError> {
let record = self
.get_operation_version(workspace_id, operation_id, version)
.await?;
self.validate_registry_operation_in_workspace(workspace_id, &record.snapshot)
.await?;
let mut findings = crank_core::analyze_tool_identity_quality(
&record.snapshot.name,
&record.snapshot.tool_description,
)
.findings;
findings.extend(
crank_core::analyze_tool_schema_quality(
"input_schema",
&tool_quality_schema_node(&record.snapshot.input_schema),
)
.findings,
);
findings.extend(
crank_core::analyze_tool_response_projection_quality(&tool_quality_mapping_set(
&record.snapshot.output_mapping,
))
.findings,
);
let report = crank_core::ToolQualityReport::new(findings);
if report.blocking {
return Err(ApiError::unprocessable_with_context(
"operation publish is blocked by quality findings",
json!({
"error_code": "operation_publish_blocked",
"findings": report.findings
}),
));
}
let published_at = OffsetDateTime::now_utc();
self.registry
.publish_operation(PublishRequest {
workspace_id,
operation_id,
version,
published_at: &published_at,
published_by: None,
})
.publish_operation_cas(
PublishRequest {
workspace_id,
operation_id,
version,
published_at: &published_at,
published_by: None,
},
expected_state,
)
.await?;
let authoritative = self
.get_operation_version(workspace_id, operation_id, version)
.await?;
info!(
name: "admin.operation.published",
@@ -392,7 +512,11 @@ impl AdminService {
operation_id: operation_id.as_str().to_owned(),
workspace_id: workspace_id.as_str().to_owned(),
published_version: version,
published_at: format_timestamp(published_at),
published_at: authoritative
.snapshot
.published_at
.map(format_timestamp)
.unwrap_or_else(|| format_timestamp(published_at)),
})
}
@@ -401,19 +525,21 @@ impl AdminService {
&self,
workspace_id: &WorkspaceId,
operation_id: &OperationId,
expected_state: &OperationStateExpectation,
) -> Result<OperationMutationResult, ApiError> {
let summary = self.get_operation(workspace_id, operation_id).await?;
let updated_at = OffsetDateTime::now_utc();
self.registry
.archive_operation(workspace_id, operation_id, &updated_at)
.archive_operation_cas(workspace_id, operation_id, &updated_at, expected_state)
.await?;
let authoritative = self.get_operation(workspace_id, operation_id).await?;
Ok(OperationMutationResult {
operation_id: operation_id.as_str().to_owned(),
workspace_id: workspace_id.as_str().to_owned(),
version: summary.current_draft_version,
status: OperationStatus::Archived,
updated_at: format_timestamp(updated_at),
updated_at: authoritative.updated_at,
})
}
@@ -422,11 +548,12 @@ impl AdminService {
&self,
workspace_id: &WorkspaceId,
operation_id: &OperationId,
expected_state: &OperationStateExpectation,
) -> Result<OperationMutationResult, ApiError> {
let summary = self.get_operation(workspace_id, operation_id).await?;
let updated_at = now_string()?;
self.registry
.delete_operation(workspace_id, operation_id)
.delete_operation_cas(workspace_id, operation_id, expected_state)
.await?;
Ok(OperationMutationResult {
@@ -444,71 +571,81 @@ impl AdminService {
workspace_id: &WorkspaceId,
operation_id: &OperationId,
payload: TestRunPayload,
request_id: &str,
correlation: &crank_core::CorrelationContext,
) -> Result<TestRunResult, ApiError> {
let runtime_request_context = RuntimeRequestContext::from_request_id(request_id)
let request_id = correlation.request_id().as_str();
let trace_id = correlation.trace_id().as_str();
let mut runtime_request_context = RuntimeRequestContext::from_correlation(correlation)
.with_metering_context(workspace_id.clone(), None, InvocationSource::AdminTestRun);
if let Some(token) = payload.confirmation_token.as_deref() {
runtime_request_context = runtime_request_context.with_confirmation_token(token);
}
let locale = payload
.locale
.as_deref()
.filter(|value| {
value.eq_ignore_ascii_case("ru") || value.to_ascii_lowercase().starts_with("ru-")
})
.map_or(crank_core::ExecutionLocale::En, |_| {
crank_core::ExecutionLocale::Ru
});
let record = self
.get_operation_version(workspace_id, operation_id, payload.version)
.await?;
let summary = self.get_operation(workspace_id, operation_id).await?;
if summary.status == OperationStatus::Archived
|| record.status != OperationStatus::Draft
|| summary.current_draft_version != payload.version
{
return Err(ApiError::conflict_with_context(
"only the current saved Draft can be tested",
json!({
"operation_id": operation_id.as_str(),
"current_version": summary.current_draft_version,
"tested_version": payload.version,
"error_code": "operation_invalid_transition"
}),
));
}
let runtime = RuntimeOperation::from(record.snapshot.clone());
let mode = ExecutionMode::Unary;
let preview_span = crank_trace::Stage::RuntimeArgumentsMap.span();
let preview_result = preview_span
.in_scope(|| build_request_preview(&record.snapshot.input_mapping, &payload.input));
let request_preview = match preview_result {
Ok(preview) => preview,
Err(error) => {
crank_trace::StageOutcome::Error.record(&preview_span);
crank_trace::ErrorCategory::Mapping.record(&preview_span);
drop(preview_span);
self.record_invocation(InvocationRecordRequest {
workspace_id,
agent_id: None,
operation: &record.snapshot,
request_id: Some(request_id),
source: InvocationSource::AdminTestRun,
level: InvocationLevel::Error,
status: InvocationStatus::Error,
message: "mapping preview failed".to_owned(),
status_code: None,
error_kind: Some("mapping".to_owned()),
duration_ms: 0,
request_preview: Value::Null,
response_preview: Value::Null,
})
.await;
return Ok(TestRunResult {
ok: false,
mode,
request_preview: Value::Null,
response_preview: Value::Null,
errors: vec![crate::error::runtime_test_failure(&RuntimeError::Mapping(
error,
))],
});
}
};
crank_trace::StageOutcome::Success.record(&preview_span);
drop(preview_span);
let resolved_auth = self
.resolve_operation_auth(workspace_id, &runtime.execution_config)
.resolve_operation_auth(workspace_id, &runtime.execution_config, None)
.await;
let started_at = std::time::Instant::now();
match match resolved_auth {
let correlation = crank_core::CorrelationContext::new(
runtime_request_context.request_id.clone(),
runtime_request_context.trace_context.clone(),
);
let execution_result = match resolved_auth {
Ok(resolved_auth) => {
self.runtime
.execute_request(
RuntimeExecutionRequest::new(&runtime, &payload.input)
.with_optional_auth(resolved_auth.as_ref())
.with_context(&runtime_request_context),
)
.await
match RuntimeExecutionRequest::try_new(
workspace_id,
ExecutionOrigin::AdminDraft,
None,
&runtime,
&payload.input,
ExecutionAuthorization::Authorized,
resolved_auth.as_ref(),
&runtime_request_context,
std::time::Instant::now()
+ std::time::Duration::from_millis(
runtime.execution_config.timeout_ms.max(1),
),
) {
Ok(request) => self.runtime.execute_outcome(request).await,
Err(_) => Err(crank_core::ExecutionFailure::new(
crank_core::ExecutionErrorCode::RuntimeInternal,
correlation.clone(),
)),
}
}
Err(error) => Err(error),
} {
Ok(response_preview) => {
Err(error) => Err(crank_runtime::normalize_runtime_error(&error, &correlation)),
};
match execution_result {
Ok(success) => {
let response_preview = crank_core::sanitize_invocation_preview(&success.output);
let request_preview = success.request_preview;
let duration_ms =
u64::try_from(started_at.elapsed().as_millis()).unwrap_or(u64::MAX);
self.record_invocation(InvocationRecordRequest {
@@ -516,12 +653,17 @@ impl AdminService {
agent_id: None,
operation: &record.snapshot,
request_id: Some(request_id),
trace_id: Some(trace_id),
source: InvocationSource::AdminTestRun,
level: InvocationLevel::Info,
status: InvocationStatus::Ok,
message: "admin test run completed".to_owned(),
status_code: None,
error_kind: None,
execution_stage: Some(crank_core::ExecutionStage::Runtime),
execution_error_code: None,
retryability: Some(crank_core::Retryability::Never),
outcome_certainty: Some(crank_core::OutcomeCertainty::Certain),
duration_ms,
request_preview: request_preview.clone(),
response_preview: response_preview.clone(),
@@ -530,12 +672,15 @@ impl AdminService {
Ok(TestRunResult {
ok: true,
mode,
tested_version: payload.version,
request_id: request_id.to_owned(),
trace_id: trace_id.to_owned(),
request_preview,
response_preview,
errors: Vec::new(),
})
}
Err(error) => {
Err(failure) => {
let duration_ms =
u64::try_from(started_at.elapsed().as_millis()).unwrap_or(u64::MAX);
self.record_invocation(InvocationRecordRequest {
@@ -543,23 +688,33 @@ impl AdminService {
agent_id: None,
operation: &record.snapshot,
request_id: Some(request_id),
trace_id: Some(trace_id),
source: InvocationSource::AdminTestRun,
level: InvocationLevel::Error,
status: InvocationStatus::Error,
message: error.to_string(),
status_code: None,
error_kind: Some(runtime_error_code(&error).to_owned()),
message: "runtime execution failed".to_owned(),
status_code: failure.upstream_status(),
error_kind: Some(failure.error_code().as_str().to_owned()),
execution_stage: Some(failure.stage()),
execution_error_code: Some(failure.error_code()),
retryability: Some(failure.retryability()),
outcome_certainty: Some(failure.outcome_certainty()),
duration_ms,
request_preview: request_preview.clone(),
request_preview: Value::Null,
response_preview: Value::Null,
})
.await;
Ok(TestRunResult {
ok: false,
mode,
request_preview,
tested_version: payload.version,
request_id: request_id.to_owned(),
trace_id: trace_id.to_owned(),
request_preview: Value::Null,
response_preview: Value::Null,
errors: vec![crate::error::runtime_test_failure(&error)],
errors: vec![crate::error::execution_test_failure_localized(
&failure, locale,
)],
})
}
}
Loaded 100 of 1112 files, more files were not shown because too many files have changed in this diff. Show more