Files
crank/apps/admin-api/tests/resource_portability_logs.rs
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

307 lines
11 KiB
Rust

//! AT-3.9-03: безопасные журналы переноса Resource на реальном HTTP (Story 3.9, F2 review round1).
//!
//! Проверяется то, что раньше было честным GAP: реальные запросы экспорта, успешного импорта и
//! отказа по integrity порождают события в настоящем `fmt`-приёмнике (`build_subscriber`), и ни
//! в одном из них нет содержимого (raw/base64), внутренних путей хранилища и credential-канареек.
//!
//! Изоляция: это единственная точка этого тестового бинаря, которая ставит глобальный
//! tracing-подписчик. Общий `integration::openapi_source::telemetry` живёт в другом бинаре,
//! поэтому конфликта глобального default нет — тот же приём, что в принятом `tests/dc08.rs`.
//!
//! Запуск: `cargo test -p admin-api --test resource_portability_logs -- --test-threads=1`
use std::io;
use std::sync::{Arc, Mutex};
use base64::{Engine as _, engine::general_purpose::STANDARD as BASE64};
use reqwest::StatusCode;
use reqwest::multipart::{Form, Part};
use serde_json::{Value, json};
use serial_test::serial;
use tracing_subscriber::fmt::MakeWriter;
#[path = "integration/common.rs"]
mod common;
use common::{
authorized_client, build_test_app, spawn_admin_api, test_registry, test_storage_root,
};
const ENVELOPE_FORMAT: &str = "crank-resource-portability";
/// Уникальные канарейки содержимого: строк, которых нет в обычных журналах.
const EXPORT_CANARY: &str = "C39-LOGSINK-EXPORT-4f21";
const IMPORT_CANARY: &str = "C39-LOGSINK-IMPORT-9b07";
const REFUSAL_CANARY: &str = "C39-LOGSINK-REFUSAL-1c63";
/// Положительный контроль: событие, которое приёмник обязан получить.
const SINK_CONTROL: &str = "C39-LOGSINK-POSITIVE-CONTROL";
/// Детектор утечки: одна функция и для реальных логов, и для искусственной канарейки.
fn leaked(logs: &str, needle: &str) -> bool {
logs.contains(needle)
}
fn sha256_hex(bytes: &[u8]) -> String {
use sha2::{Digest as _, Sha256};
use std::fmt::Write as _;
let digest = Sha256::digest(bytes);
let mut hex = String::with_capacity(64);
for byte in digest {
write!(&mut hex, "{byte:02x}").expect("hex formatting");
}
hex
}
fn content_block(bytes: &[u8]) -> Value {
json!({
"size_bytes": bytes.len(),
"digest": sha256_hex(bytes),
"base64": BASE64.encode(bytes),
})
}
fn envelope(name: &str, bytes: &[u8]) -> Value {
json!({
"format": ENVELOPE_FORMAT,
"version": 1,
"resource": {
"uri": format!("crank://e2e/story39-logsink-{name}"),
"name": format!("story39_logsink_{name}"),
"description": "story39 log sink fixture",
"kind": "text",
"mime_type": "text/plain",
"sensitivity": "internal",
},
"source": { "kind": "draft", "version": 1 },
"content": content_block(bytes),
})
}
fn draft_form(bytes: &[u8]) -> Form {
let metadata = json!({
"uri": "crank://e2e/story39-logsink-export",
"name": "story39_logsink_export",
"description": "story39 log sink fixture",
"kind": "text",
"mime_type": "text/plain",
"sensitivity": "internal",
});
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("metadata part mime"),
)
.part(
"content",
Part::bytes(bytes.to_vec())
.file_name("fixture.txt".to_owned())
.mime_str("text/plain")
.expect("content part mime"),
)
}
#[tokio::test(flavor = "multi_thread")]
#[serial]
async fn story39_at03_portability_logs_never_carry_content_or_storage_paths() {
let writer = SharedLogWriter::default();
let subscriber = crank_observability::build_subscriber(
crank_observability::ObservabilityConfig::new(
crank_observability::ServiceIdentity::try_new("admin-api", "test", "test")
.expect("service identity"),
"info",
crank_observability::RedactionLimits::default(),
),
writer.clone(),
)
.expect("admin-api observability subscriber for the portability log sink");
tracing::subscriber::set_global_default(subscriber)
.expect("isolated portability log sink must be the only global subscriber");
// (1) Реальный сервер и сессия. Миграционные NOTICE sqlx и лог входа остаются вне
// окна наблюдения: контракт относится к запросам переноса.
let registry = test_registry().await;
let app = build_test_app(registry, test_storage_root("story39_logsink"));
let server = spawn_admin_api(app).await;
let client = authorized_client(&server).await;
// `TestServer::base_url` уже указывает на `/api/admin/workspaces/{ws_default}`.
let base = server.as_ref().to_owned();
// (2) Положительные контроли: приёмник получает событие, детектор находит искусственную утечку.
tracing::info!(
event = "test.portability.sink_control",
marker = SINK_CONTROL,
"log sink control event before the portability requests"
);
let control = writer.output();
assert!(
control.contains(SINK_CONTROL),
"the log sink must capture a control event; captured={control}"
);
assert!(
leaked(&format!("synthetic leak {EXPORT_CANARY}"), EXPORT_CANARY),
"the leak detector must flag a synthetic canary line"
);
assert!(
!leaked(&control, EXPORT_CANARY),
"a clean control capture must not be flagged as a leak"
);
let baseline = writer.len();
// (3) Реальные запросы: экспорт, успешный импорт, отказ по integrity.
let created = client
.post(format!("{base}/resources"))
.multipart(draft_form(EXPORT_CANARY.as_bytes()))
.send()
.await
.expect("draft create request must complete");
let status = created.status();
let text = created.text().await.expect("draft create body must read");
assert_eq!(
status,
StatusCode::CREATED,
"draft create must succeed: {text}"
);
let created: Value = serde_json::from_str(&text).expect("draft create must return JSON");
let resource_id = created["resource"]["resource_id"]
.as_str()
.expect("201 must carry the created Resource identifier")
.to_owned();
let exported = client
.get(format!("{base}/resources/{resource_id}/export"))
.send()
.await
.expect("draft export request must complete");
let status = exported.status();
let text = exported.text().await.expect("export body must read");
assert_eq!(status, StatusCode::OK, "draft export must succeed: {text}");
let exported: Value = serde_json::from_str(&text).expect("export must return the envelope");
assert_eq!(
exported["content"]["base64"],
json!(BASE64.encode(EXPORT_CANARY.as_bytes())),
"the export must really carry the canary bytes"
);
let imported = client
.post(format!("{base}/resources/import"))
.json(&envelope("import", IMPORT_CANARY.as_bytes()))
.send()
.await
.expect("import request must complete");
let status = imported.status();
let text = imported.text().await.expect("import body must read");
assert_eq!(
status,
StatusCode::CREATED,
"a legitimate import must create the Draft: {text}"
);
let mut tampered = envelope("refusal", REFUSAL_CANARY.as_bytes());
tampered["content"]["digest"] = json!(sha256_hex(b"different body"));
let refused = client
.post(format!("{base}/resources/import"))
.json(&tampered)
.send()
.await
.expect("refusal request must complete");
let status = refused.status();
let text = refused.text().await.expect("refusal body must read");
assert_eq!(
status,
StatusCode::UNPROCESSABLE_ENTITY,
"a digest mismatch must stay 422: {text}"
);
// (3) Приёмник жив после запросов: окно наблюдения непустое, значит негативы не вакуумны.
tracing::info!(
event = "test.portability.sink_control",
marker = SINK_CONTROL,
"log sink control event after the portability requests"
);
let captured = writer.delta_from(baseline);
assert!(
captured.contains(SINK_CONTROL),
"the sink must still capture events after the portability requests"
);
let forbidden = [
EXPORT_CANARY.to_owned(),
IMPORT_CANARY.to_owned(),
REFUSAL_CANARY.to_owned(),
BASE64.encode(EXPORT_CANARY.as_bytes()),
BASE64.encode(IMPORT_CANARY.as_bytes()),
BASE64.encode(REFUSAL_CANARY.as_bytes()),
"/home/".to_owned(),
".tmp/".to_owned(),
"artifact_blobs".to_owned(),
"postgres://".to_owned(),
"test-master-key".to_owned(),
];
for needle in &forbidden {
assert!(
!leaked(&captured, needle),
"portability logs must not expose `{needle}`; captured={captured}"
);
}
// Канарейки уникальны, поэтому их отсутствие проверяется и во всём захвате целиком.
let everything = writer.output();
for needle in [EXPORT_CANARY, IMPORT_CANARY, REFUSAL_CANARY] {
assert!(
!leaked(&everything, needle),
"the whole log capture must stay free of `{needle}`"
);
}
}
#[derive(Clone, Default)]
struct SharedLogWriter {
buffer: Arc<Mutex<Vec<u8>>>,
}
impl SharedLogWriter {
fn output(&self) -> String {
String::from_utf8_lossy(&self.buffer.lock().unwrap()).into_owned()
}
fn len(&self) -> usize {
self.buffer.lock().unwrap().len()
}
/// Захваченный хвост после `offset`: окно наблюдения конкретных запросов.
fn delta_from(&self, offset: usize) -> String {
let buffer = self.buffer.lock().unwrap();
let start = offset.min(buffer.len());
String::from_utf8_lossy(&buffer[start..]).into_owned()
}
}
impl<'a> MakeWriter<'a> for SharedLogWriter {
type Writer = SharedLogGuard;
fn make_writer(&'a self) -> Self::Writer {
SharedLogGuard {
buffer: Arc::clone(&self.buffer),
}
}
}
struct SharedLogGuard {
buffer: Arc<Mutex<Vec<u8>>>,
}
impl io::Write for SharedLogGuard {
fn write(&mut self, bytes: &[u8]) -> io::Result<usize> {
self.buffer.lock().unwrap().extend_from_slice(bytes);
Ok(bytes.len())
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}