Files
bsodfather 54ce770682
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
feat(epic3): deliver the accepted Epic 3 scope through Story 3.17
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

626 lines
24 KiB
Rust
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! AT-3.6-06: изолированный захват журналов/спанов/метрик на реальном HTTP (TL run101, R-A).
//!
//! Источник: SPEC v4 §6/§7, QA test-design run77:117–119, TL review run101:39.
//! Проверяется то, что раньше было честным GAP: процессы-приёмники (`writer` fmt-слоя,
//! экспортируемые спаны, рекордер метрик) **реально существуют**, получают положительные
//! события от тех же Resource-отказов, и ни в одном из них не появляется содержимое.
//!
//! Изоляция: этот файл — единственная в бинаре `admin-api --lib` точка, которая ставит
//! глобальные подписчик/рекордер. Канонический прогон задаёт точный фильтр, поэтому в
//! процессе исполняется только этот тест:
//! `cargo test -p admin-api --lib
//! service::resources::tests::guard_telemetry::story36_ -- --test-threads=1`.
//! Существующий `integration::openapi_source::telemetry` живёт в другом тестовом бинаре
//! (integration-тесты), поэтому конфликта глобального default между ними нет.
use std::{
io,
sync::{Arc, Mutex},
};
use base64::Engine as _;
use opentelemetry::trace::TracerProvider as _;
use opentelemetry_sdk::{
error::OTelSdkResult,
trace::{SdkTracerProvider, SpanData, SpanExporter},
};
use reqwest::StatusCode;
use serde_json::{Value, json};
use serial_test::serial;
use tracing_subscriber::{fmt::MakeWriter, layer::SubscriberExt};
use crate::app::resource_test_support::{
GuardHarness, GuardOptions, LoginOptions, OWNER_EMAIL, OWNER_PASSWORD, anonymous_client,
draft_metadata, error_signature, login_client, post_draft,
};
use crank_core::resource::{ResourceKind, ResourceLimits};
/// Test-only размещение рекордера метрик: файл лежит в тестовом пути
/// `apps/admin-api/tests/**`, поэтому прямой рекордер не объявляется в
/// production-исходниках (metrics boundary, TL run109). Захват и положительные
/// образцы остаются теми же.
#[path = "../metrics_capture.rs"]
mod metrics_capture;
/// Канарейка содержимого: одна и та же строка во всех кодировках.
const CANARY: &str = "C36-GUARD-CANARY-7f3a";
/// Тот же маркер, но с символами, которые JSON-сериализатор обязан экранировать.
const ESCAPED_CANARY: &str = "C36-GUARD-\"CANARY\"-\t-7f3a";
const ESCAPED_CANARY_JSON: &str = "C36-GUARD-\\\"CANARY\\\"-\\t-7f3a";
/// Отдельная канарейка существующего чужого Resource: её отсутствие во всех приёмниках
/// и ответах доказывает, что отказ доступа не раскрывает содержимое (R-A).
const FOREIGN_CANARY: &str = "C36-FOREIGN-CANARY-b41d";
#[tokio::test(flavor = "multi_thread")]
#[serial]
async fn story36_at06_guard_refusals_never_reach_logs_spans_or_metrics() {
// (1) Приёмники в одном процессе: рекордер метрик, писатель журналов, экспортёр спанов.
let capture = metrics_capture::MetricsCapture::install();
// Story 3.6: recording-пути применяют ownership только к связанному сервису,
// поэтому фикстура обязана явно зафиксировать admin-api до первого запроса.
crank_metrics::bind_metric_service(crank_metrics::MetricService::AdminApi)
.expect("admin-api metrics binding for the guard telemetry fixture");
let writer = SharedLogWriter::default();
let exported = Arc::new(Mutex::new(Vec::new()));
let provider = SdkTracerProvider::builder()
.with_simple_exporter(CapturingExporter(Arc::clone(&exported)))
.build();
let tracer = provider.tracer("admin-resource-guard-test");
let subscriber = tracing_subscriber::registry()
.with(
tracing_subscriber::fmt::layer()
.with_ansi(false)
.with_writer(writer.clone()),
)
.with(tracing_opentelemetry::layer().with_tracer(tracer));
tracing::dispatcher::set_global_default(tracing::Dispatch::new(subscriber))
.expect("isolated Resource guard tracing subscriber");
let harness = GuardHarness::spawn_with(
"telemetry",
GuardOptions {
foreign_workspace: true,
foreign_workspace_owner: true,
..GuardOptions::default()
},
)
.await;
let owner = login_client(
&harness.root_url,
OWNER_EMAIL,
OWNER_PASSWORD,
LoginOptions::with_csrf(),
)
.await;
let anonymous = anonymous_client();
let base64_canary = base64::engine::general_purpose::STANDARD.encode(CANARY);
let mut denial_traces = Vec::new();
// (2) guard-путь: анонимный create с канарейкой в содержимом.
let (status, traceable, text, body) = post_draft(
&anonymous,
&harness.resources_url(),
&draft_metadata(
"crank://guard/tel/anon",
"tel_anon",
"text",
"text/plain",
"internal",
),
CANARY.as_bytes(),
"text/plain",
)
.await;
assert_eq!(
status,
StatusCode::UNAUTHORIZED,
"anonymous create must be refused: {}",
error_signature(status, &body)
);
assert!(traceable, "a refusal must be attributable via x-trace-id");
assert!(!text.contains(CANARY), "the refusal must not echo content");
denial_traces.push(body);
// (3) malformed JSON: канарейка внутри отклоняемого содержимого.
let malformed = format!("{{\"leak\":\"{CANARY}\"");
let (status, _traceable, text, body) = post_draft(
&owner,
&harness.resources_url(),
&draft_metadata(
"crank://guard/tel/json",
"tel_json",
"json",
"application/json",
"internal",
),
malformed.as_bytes(),
"application/json",
)
.await;
assert_eq!(
status,
StatusCode::UNPROCESSABLE_ENTITY,
"malformed JSON must be refused: {}",
error_signature(status, &body)
);
assert!(!text.contains(CANARY));
// Этот отказ используется ниже как коррелированное наблюдение named-события и спана.
let json_refusal = body.clone();
denial_traces.push(body);
// (4) non-UTF8: канарейка + повреждённая последовательность.
let invalid_utf8 = [CANARY.as_bytes(), &[0xC3, 0x28]].concat();
let (status, _traceable, text, body) = post_draft(
&owner,
&harness.resources_url(),
&draft_metadata(
"crank://guard/tel/utf8",
"tel_utf8",
"text",
"text/plain",
"internal",
),
&invalid_utf8,
"text/plain",
)
.await;
assert_eq!(
status,
StatusCode::UNPROCESSABLE_ENTITY,
"invalid UTF-8 must be refused: {}",
error_signature(status, &body)
);
assert!(!text.contains(CANARY));
denial_traces.push(body);
// (5) base64-вариант канарейки в содержимом принятого Draft и в отклонённом повторе.
let accepted = format!("accepted-{base64_canary}");
let (status, _traceable, _text, body) = post_draft(
&owner,
&harness.resources_url(),
&draft_metadata(
"crank://guard/tel/base64",
"tel_base64",
"text",
"text/plain",
"internal",
),
accepted.as_bytes(),
"text/plain",
)
.await;
assert_eq!(
status,
StatusCode::CREATED,
"the base64-carrying Draft must be accepted first: {}",
error_signature(status, &body)
);
let (status, _traceable, text, body) = post_draft(
&owner,
&harness.resources_url(),
&draft_metadata(
"crank://guard/tel/base64",
"tel_base64_duplicate",
"text",
"text/plain",
"internal",
),
CANARY.as_bytes(),
"text/plain",
)
.await;
assert_eq!(
status,
StatusCode::CONFLICT,
"a duplicate uri must be refused: {}",
error_signature(status, &body)
);
assert!(!text.contains(CANARY));
denial_traces.push(body);
// (6) oversized: реальное превышение предела содержимого, канарейка в начале bytes.
let limit = ResourceLimits::default().effective_limit(ResourceKind::Text);
let mut oversized = CANARY.as_bytes().to_vec();
oversized.resize(limit + 1, b'x');
let (status, _traceable, text, body) = post_draft(
&owner,
&harness.resources_url(),
&draft_metadata(
"crank://guard/tel/oversized",
"tel_oversized",
"text",
"text/plain",
"internal",
),
&oversized,
"text/plain",
)
.await;
assert_eq!(
status,
StatusCode::PAYLOAD_TOO_LARGE,
"content beyond the limit must be refused: {}",
error_signature(status, &body)
);
assert!(!text.contains(CANARY));
denial_traces.push(body);
// (7) экранированный вариант канарейки внутри отклоняемого JSON-содержимого:
// незакрытая строка делает тело невалидным JSON, а оба представления маркера
// (с экранированием и без) остаются в отправленных байтах.
let escaped_body = format!("{{\"marker\":\"{ESCAPED_CANARY}\"");
let (status, _traceable, text, body) = post_draft(
&owner,
&harness.resources_url(),
&draft_metadata(
"crank://guard/tel/escaped",
"tel_escaped",
"json",
"application/json",
"internal",
),
escaped_body.as_bytes(),
"application/json",
)
.await;
assert_eq!(
status,
StatusCode::UNPROCESSABLE_ENTITY,
"malformed JSON with an escaped marker must be refused: {}",
error_signature(status, &body)
);
assert!(!text.contains(ESCAPED_CANARY));
assert!(!text.contains(ESCAPED_CANARY_JSON));
denial_traces.push(body);
// (8) foreign existing Resource: отказ доступа не раскрывает ни metadata, ни bytes.
// Содержимое чужого Resource несёт проверяемую канарейку, поэтому «не раскрыто»
// наблюдается во всех приёмниках и ответах (R-A), а не только по метаданным.
let foreign_owner = login_client(
&harness.root_url,
crate::app::resource_test_support::FOREIGN_OWNER_EMAIL,
crate::app::resource_test_support::FOREIGN_OWNER_PASSWORD,
LoginOptions::with_csrf(),
)
.await;
let foreign_base = harness.foreign_resources_url();
let foreign_body = format!("foreign payload {FOREIGN_CANARY}");
let (status, _traceable, _text, body) = post_draft(
&foreign_owner,
&foreign_base,
&draft_metadata(
"crank://guard/tel/foreign",
"tel_foreign",
"text",
"text/plain",
"internal",
),
foreign_body.as_bytes(),
"text/plain",
)
.await;
assert_eq!(
status,
StatusCode::CREATED,
"the foreign owner must seed ResourceB: {}",
error_signature(status, &body)
);
let resource_b = body["resource"]["resource_id"]
.as_str()
.expect("ResourceB id")
.to_owned();
let (status, _headers, text, body) =
crate::app::resource_test_support::get_raw(&owner, &format!("{foreign_base}/{resource_b}"))
.await;
assert_eq!(
status,
StatusCode::FORBIDDEN,
"a foreign Resource must stay unreadable: {}",
error_signature(status, &body)
);
assert!(!text.contains(FOREIGN_CANARY));
assert!(!text.contains("foreign payload"));
denial_traces.push(body);
// Положительный контроль: разрешённое чтение того же ResourceB его владельцем —
// без него «канарейка не найдена» ничего не доказывала бы.
let (status, _headers, bytes, _body) = crate::app::resource_test_support::get_raw(
&foreign_owner,
&format!("{foreign_base}/{resource_b}/content"),
)
.await;
assert_eq!(status, StatusCode::OK, "the owner reads its own ResourceB");
assert_eq!(
bytes.as_bytes(),
foreign_body.as_bytes(),
"the foreign canary must really be the content of ResourceB"
);
let (status, _headers, _text, body) = post_draft(
&owner,
&harness.resources_url(),
&draft_metadata(
"crank://guard/tel/ok",
"tel_ok",
"text",
"text/plain",
"internal",
),
b"safe payload",
"text/plain",
)
.await;
assert_eq!(
status,
StatusCode::CREATED,
"a valid follow-up must succeed: {}",
error_signature(status, &body)
);
provider.force_flush().expect("span export flush");
// (9) Приёмники не пусты: положительные наблюдения, без которых «отсутствие» ничего не значит.
let logs = writer.output();
assert!(
logs.contains("admin request completed"),
"the log sink must receive the completed-request events"
);
assert!(
logs.contains("status=401"),
"the denied anonymous request must appear in the log sink with its outcome"
);
let rendered_spans = format!("{:?}", exported.lock().expect("exported spans"));
assert!(
rendered_spans.contains("http.request"),
"the span sink must receive the request spans"
);
let metrics_samples = capture.samples();
// Положительный контроль метрик: образец именно наших Resource-отказов.
// Story 3.6: четыре Resource-шаблона объявлены точными admin-идентичностями,
// поэтому `route` обязан нести **точный** template, а не `unmatched`.
let http_samples: Vec<_> = metrics_samples
.iter()
.filter(|(key, _, _, _)| key.key().name() == "crank_http_requests_total")
.collect();
assert!(
!http_samples.is_empty(),
"the metrics sink must receive HTTP request samples from the executed requests"
);
let denied_post_sample = http_samples.iter().find(|(key, _, _, _)| {
key.key()
.labels()
.any(|label| label.key() == "method" && label.value() == "POST")
&& key
.key()
.labels()
.any(|label| label.key() == "status_class" && label.value() == "4xx")
});
assert!(
denied_post_sample.is_some(),
"a refused Resource POST must be observable as a 4xx HTTP sample; observed keys: {:?}",
http_samples
.iter()
.map(|(key, _, _, _)| format!("{:?}", key.key()))
.collect::<Vec<_>>()
);
let resource_route_label = http_samples
.iter()
.flat_map(|(key, _, _, _)| key.key().labels())
.filter(|label| label.key() == "route")
.map(|label| label.value().to_owned())
.collect::<Vec<_>>();
assert!(
resource_route_label
.iter()
.any(|route| route == "/api/auth/login"),
"a known route must keep its own label: {resource_route_label:?}"
);
let resource_collection_template = "/api/admin/workspaces/{workspace_id}/resources";
assert!(
resource_route_label
.iter()
.any(|route| route == resource_collection_template),
"the refused Resource POST must keep its exact admin template \
`{resource_collection_template}`: {resource_route_label:?}"
);
assert!(
resource_route_label
.iter()
.all(|route| route != "unmatched"),
"no Resource request may stay `unmatched` now that the four templates are \
admin-owned: {resource_route_label:?}"
);
// (10) Ни в одном приёмнике нет содержимого — во всех кодировках и для чужого Resource.
for needle in [
CANARY,
ESCAPED_CANARY,
ESCAPED_CANARY_JSON,
&base64_canary,
FOREIGN_CANARY,
] {
assert!(
!logs.contains(needle),
"the log sink must not carry content bytes ({needle})"
);
assert!(
!rendered_spans.contains(needle),
"the span sink must not carry content bytes ({needle})"
);
for (key, _, _, _) in &metrics_samples {
assert!(
!key.key().name().contains(needle),
"a metric name must not carry content bytes ({needle})"
);
assert!(
!key.key().labels().any(|label| {
label.key().contains(needle) || label.value().contains(needle)
}),
"metric labels must not carry content bytes ({needle})"
);
}
for body in &denial_traces {
let rendered = body.to_string();
assert!(
!rendered.contains(needle),
"a refusal body must not carry content bytes ({needle})"
);
}
}
// Ответ отказа атрибутирован: trace_id присутствует в теле и в журнале.
let trace_id = denial_traces
.iter()
.find_map(|body| body["error"]["trace_id"].as_str())
.expect("a refusal body must expose its trace_id");
assert!(
logs.contains(trace_id),
"the attributed refusal must be observable in the log sink"
);
// (11) Коррелированный named-отказ (R-A): настоящее событие
// `admin.response.rejected` с точным `error_code`, тем же request_id/trace_id и
// соответствующим экспортированным span-ом запроса. Без этого «отказы видны в журнале»
// остаётся общим утверждением, а не наблюдением конкретного события.
let json_code = json_refusal["error"]["code"]
.as_str()
.expect("the malformed-JSON refusal must carry its stable code")
.to_owned();
assert_eq!(json_code, "resource_content_invalid_json");
let json_request_id = json_refusal["error"]["request_id"]
.as_str()
.expect("the refusal must expose its request_id")
.to_owned();
let json_trace_id = json_refusal["error"]["trace_id"]
.as_str()
.expect("the refusal must expose its trace_id")
.to_owned();
let rejected_line = logs
.lines()
.find(|line| {
// Наблюдение настоящего события отказа: имя объявлено как name-поле события,
// которое fmt-слой не печатает, поэтому корреляция идёт по телу события
// (`error_code`) вместе с request_id/trace_id отказа в одной строке.
line.contains("error_code")
&& line.contains(json_code.as_str())
&& line.contains(json_request_id.as_str())
&& line.contains(json_trace_id.as_str())
})
.unwrap_or_else(|| {
panic!(
"the named rejected event must be logged together with its error_code \
(`{json_code}`) and the correlated request_id/trace_id of the refusal \
(`{json_request_id}`/`{json_trace_id}`); log sink={logs}"
)
});
assert!(
rejected_line.contains("rejected") || rejected_line.contains("admin_api::error"),
"the correlated line must be the rejection event, not an unrelated record: {rejected_line}"
);
let spans = exported.lock().expect("exported spans");
let correlated = spans
.iter()
.find(|span| {
span.name.as_ref() == "http.request"
&& span.attributes.iter().any(|attribute| {
attribute.key.as_str() == "request_id"
&& attribute.value.as_str() == json_request_id.as_str()
})
})
.unwrap_or_else(|| {
let rendered: Vec<String> = spans
.iter()
.map(|span| {
let fields: Vec<String> = span
.attributes
.iter()
.map(|attribute| {
format!("{}={}", attribute.key.as_str(), attribute.value.as_str())
})
.collect();
format!("{}[{}]", span.name, fields.join(","))
})
.collect();
panic!(
"the refusal must have an exported http.request span carrying its request_id \
`{json_request_id}`; exported spans: {rendered:?}"
)
});
assert!(
correlated.attributes.iter().any(|attribute| {
attribute.key.as_str() == "trace_id"
&& attribute.value.as_str() == json_trace_id.as_str()
}),
"the exported span must carry the same trace_id as the refusal body"
);
drop(spans);
provider.shutdown().expect("tracer provider shutdown");
}
#[derive(Clone, Default)]
struct SharedLogWriter {
buffer: Arc<Mutex<Vec<u8>>>,
}
impl SharedLogWriter {
fn output(&self) -> String {
String::from_utf8(self.buffer.lock().expect("log buffer").clone()).unwrap_or_default()
}
}
impl<'a> MakeWriter<'a> for SharedLogWriter {
type Writer = SharedLogGuard;
fn make_writer(&'a self) -> Self::Writer {
SharedLogGuard {
buffer: Arc::clone(&self.buffer),
pending: Vec::new(),
}
}
}
struct SharedLogGuard {
buffer: Arc<Mutex<Vec<u8>>>,
pending: Vec<u8>,
}
impl io::Write for SharedLogGuard {
fn write(&mut self, bytes: &[u8]) -> io::Result<usize> {
self.pending.extend_from_slice(bytes);
Ok(bytes.len())
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
impl Drop for SharedLogGuard {
fn drop(&mut self) {
if let Ok(mut buffer) = self.buffer.lock() {
buffer.extend_from_slice(&self.pending);
}
}
}
#[derive(Clone, Debug)]
struct CapturingExporter(Arc<Mutex<Vec<SpanData>>>);
impl SpanExporter for CapturingExporter {
async fn export(&self, batch: Vec<SpanData>) -> OTelSdkResult {
if let Ok(mut spans) = self.0.lock() {
spans.extend(batch);
}
Ok(())
}
}
/// Компиляционная защита от «пустого» comparison: [Value] не должен молча терять поля.
#[test]
fn denial_body_rendering_keeps_error_fields() {
let body: Value = json!({ "error": { "code": "forbidden", "trace_id": "t-1" } });
assert!(body.to_string().contains("forbidden"));
}