Импорт OpenAPI: закрыть пробелы проверок второго этапа #95
No files matched your search
Generated
+2
@@ -43,7 +43,9 @@ dependencies = [
|
||||
"metrics",
|
||||
"metrics-util",
|
||||
"opentelemetry",
|
||||
"opentelemetry-proto",
|
||||
"opentelemetry_sdk",
|
||||
"prost",
|
||||
"rand 0.10.2",
|
||||
"reqwest 0.12.28",
|
||||
"serde",
|
||||
|
||||
@@ -52,7 +52,9 @@ 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
|
||||
|
||||
@@ -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}}}");
|
||||
}
|
||||
@@ -12,6 +12,7 @@ use std::{
|
||||
use async_trait::async_trait;
|
||||
use crank_artifacts::{ReconciliationRegistration, RegisteredArtifact};
|
||||
use crank_registry::MigrationAuthority;
|
||||
use sqlx::Row;
|
||||
|
||||
use super::*;
|
||||
|
||||
@@ -688,3 +689,6 @@ async fn expired_claim_cursor_prevents_unsafe_prefix_starvation_across_ticks() {
|
||||
.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");
|
||||
}
|
||||
@@ -16,6 +16,13 @@ const REGISTERED_MASTER_KEY: &str = "registered-master-key-CANARY_SECRET_VALUE-0
|
||||
const WRONG_MASTER_KEY: &str = "wrong-master-key-CANARY_SECRET_VALUE-0000000000000";
|
||||
static NEXT_STORAGE_ROOT: AtomicU64 = AtomicU64::new(0);
|
||||
|
||||
fn isolated_metrics_bind() -> String {
|
||||
let listener = std::net::TcpListener::bind("127.0.0.1:0").expect("ephemeral metrics port");
|
||||
let addr = listener.local_addr().unwrap().to_string();
|
||||
drop(listener);
|
||||
addr
|
||||
}
|
||||
|
||||
fn run_with(entries: &[(&str, &str)]) -> String {
|
||||
let mut command = Command::new(env!("CARGO_BIN_EXE_admin-api"));
|
||||
for field in crank_config::field_registry() {
|
||||
@@ -27,6 +34,7 @@ fn run_with(entries: &[(&str, &str)]) -> String {
|
||||
("CRANK_PASSWORD_PEPPER", "pepper"),
|
||||
("CRANK_BOOTSTRAP_ADMIN_EMAIL", "owner@example.test"),
|
||||
("CRANK_BOOTSTRAP_ADMIN_PASSWORD", "password"),
|
||||
("CRANK_ADMIN_METRICS_BIND", isolated_metrics_bind().as_str()),
|
||||
]);
|
||||
for (name, value) in entries {
|
||||
command.env(name, value);
|
||||
@@ -137,6 +145,7 @@ async fn real_admin_startup_runs_immediate_import_maintenance() {
|
||||
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
|
||||
let bind_addr = listener.local_addr().unwrap().to_string();
|
||||
drop(listener);
|
||||
let metrics_bind = isolated_metrics_bind();
|
||||
|
||||
let mut command = Command::new(env!("CARGO_BIN_EXE_admin-api"));
|
||||
for field in crank_config::field_registry() {
|
||||
@@ -151,6 +160,7 @@ async fn real_admin_startup_runs_immediate_import_maintenance() {
|
||||
("CRANK_BOOTSTRAP_ADMIN_EMAIL", "owner@example.test"),
|
||||
("CRANK_BOOTSTRAP_ADMIN_PASSWORD", "password"),
|
||||
("CRANK_ADMIN_BIND", bind_addr.as_str()),
|
||||
("CRANK_ADMIN_METRICS_BIND", metrics_bind.as_str()),
|
||||
("CRANK_STORAGE_ROOT", storage_root.to_str().unwrap()),
|
||||
])
|
||||
.stdout(Stdio::null())
|
||||
|
||||
@@ -6,6 +6,8 @@ use serde_json::{Value, json};
|
||||
use serial_test::serial;
|
||||
|
||||
mod replay;
|
||||
mod telemetry;
|
||||
mod telemetry_startup;
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
#[serial]
|
||||
|
||||
@@ -0,0 +1,301 @@
|
||||
use super::*;
|
||||
use std::{collections::BTreeSet, time::Duration};
|
||||
|
||||
use axum::{
|
||||
Router,
|
||||
body::Bytes,
|
||||
extract::Request,
|
||||
http::StatusCode,
|
||||
middleware::{self, Next},
|
||||
routing::post,
|
||||
};
|
||||
use crank_core::{ProductEventKind, WorkspaceId};
|
||||
use crank_observability::{
|
||||
OtlpBatchConfig, OtlpTraceConfig, ServiceIdentity, build_tracer_provider,
|
||||
};
|
||||
use opentelemetry::trace::TracerProvider as _;
|
||||
use opentelemetry_proto::tonic::{
|
||||
collector::trace::v1::ExportTraceServiceRequest,
|
||||
common::v1::{KeyValue, any_value},
|
||||
};
|
||||
use opentelemetry_sdk::trace::SdkTracerProvider;
|
||||
use prost::Message;
|
||||
use tokio::{net::TcpListener, sync::mpsc};
|
||||
use tracing::instrument::WithSubscriber;
|
||||
use tracing_subscriber::layer::SubscriberExt;
|
||||
|
||||
pub(super) const RAW_DOCUMENT_CANARY: &str = "openapi: 3.1.0 raw-document-canary";
|
||||
pub(super) const USER_ID_CANARY: &str = "user-id-canary-should-not-export";
|
||||
const REQUEST_ID: &str = "req_onboarding_optin_0001";
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
#[serial]
|
||||
async fn onboarding_http_event_keeps_default_trace_export_disabled() {
|
||||
let registry = test_registry().await;
|
||||
let workspace_id = WorkspaceId::new("ws_default");
|
||||
let config = OtlpTraceConfig::default();
|
||||
let identity = service_identity();
|
||||
assert!(!config.is_enabled());
|
||||
assert!(
|
||||
build_tracer_provider(&identity, &config)
|
||||
.expect("disabled OTLP config is valid")
|
||||
.is_none(),
|
||||
"default config must not construct an exporter or batch queue"
|
||||
);
|
||||
|
||||
let (collector_listener, _) = loopback_listener().await;
|
||||
let (dispatch, provider) = local_trace_dispatch();
|
||||
let app = app_with_request_dispatch(
|
||||
build_test_app(
|
||||
registry.clone(),
|
||||
test_storage_root("onboarding_telemetry_default"),
|
||||
),
|
||||
dispatch,
|
||||
);
|
||||
let base_url = spawn_admin_api(app).await;
|
||||
let client = authorized_client(&base_url).await;
|
||||
let revision = onboarding_revision(&client, &base_url).await;
|
||||
|
||||
reject_canary_payload(&client, &base_url, revision).await;
|
||||
let event = post_started_event(&client, &base_url, revision, false).await;
|
||||
assert_eq!(event.status(), reqwest::StatusCode::OK);
|
||||
assert_eq!(event.json::<Value>().await.unwrap()["accepted"], true);
|
||||
assert_event_is_local(®istry, &workspace_id, "ui:telemetry-default-event").await;
|
||||
|
||||
assert!(
|
||||
tokio::time::timeout(Duration::from_millis(300), collector_listener.accept())
|
||||
.await
|
||||
.is_err(),
|
||||
"default telemetry config must make no connection to the external collector"
|
||||
);
|
||||
provider
|
||||
.shutdown()
|
||||
.expect("local trace provider shuts down");
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
#[serial]
|
||||
async fn onboarding_http_event_exports_only_sanitized_trace_when_operator_opts_in() {
|
||||
let registry = test_registry().await;
|
||||
let workspace_id = WorkspaceId::new("ws_default");
|
||||
let (endpoint, mut exported_requests, collector_task) = spawn_otlp_collector().await;
|
||||
let config = OtlpTraceConfig::try_new(
|
||||
Some(endpoint),
|
||||
Some("http/protobuf".to_owned()),
|
||||
Duration::from_secs(2),
|
||||
OtlpBatchConfig::try_new(128, 128, Duration::from_secs(60), Duration::from_secs(2))
|
||||
.unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
let identity = service_identity();
|
||||
assert!(config.is_enabled());
|
||||
let (provider, tracer) = build_tracer_provider(&identity, &config)
|
||||
.expect("explicit OTLP config is valid")
|
||||
.expect("operator endpoint enables the real exporter");
|
||||
let subscriber =
|
||||
tracing_subscriber::registry().with(tracing_opentelemetry::layer().with_tracer(tracer));
|
||||
let dispatch = tracing::Dispatch::new(subscriber);
|
||||
let app = app_with_request_dispatch(
|
||||
build_test_app(
|
||||
registry.clone(),
|
||||
test_storage_root("onboarding_telemetry_optin"),
|
||||
),
|
||||
dispatch,
|
||||
);
|
||||
let base_url = spawn_admin_api(app).await;
|
||||
let client = authorized_client(&base_url).await;
|
||||
let revision = onboarding_revision(&client, &base_url).await;
|
||||
|
||||
reject_canary_payload(&client, &base_url, revision).await;
|
||||
let event = post_started_event(&client, &base_url, revision, true).await;
|
||||
assert_eq!(event.status(), reqwest::StatusCode::OK);
|
||||
assert_eq!(event.json::<Value>().await.unwrap()["accepted"], true);
|
||||
assert_event_is_local(®istry, &workspace_id, "ui:telemetry-optin-event").await;
|
||||
|
||||
provider
|
||||
.force_flush()
|
||||
.expect("flush trace spans to the operator collector");
|
||||
provider.shutdown().expect("exporter shuts down cleanly");
|
||||
let mut request_count = 0;
|
||||
let mut onboarding_request_id_exported = false;
|
||||
while let Ok(body) = exported_requests.try_recv() {
|
||||
request_count += 1;
|
||||
let export = ExportTraceServiceRequest::decode(body.as_ref())
|
||||
.expect("collector receives valid OTLP protobuf");
|
||||
for resource_spans in export.resource_spans {
|
||||
for scope_spans in resource_spans.scope_spans {
|
||||
for span in scope_spans.spans {
|
||||
assert_eq!(span.name, "http.request");
|
||||
assert!(span.events.is_empty());
|
||||
assert!(span.links.is_empty());
|
||||
let attribute_keys: BTreeSet<&str> = span
|
||||
.attributes
|
||||
.iter()
|
||||
.map(|attribute| attribute.key.as_str())
|
||||
.collect();
|
||||
assert!(
|
||||
attribute_keys.iter().all(|key| *key == "request_id"),
|
||||
"request spans may export only the request_id attribute"
|
||||
);
|
||||
if string_attribute(&span.attributes, "request_id") == Some(REQUEST_ID) {
|
||||
onboarding_request_id_exported = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
assert!(!body_contains(&body, RAW_DOCUMENT_CANARY));
|
||||
assert!(!body_contains(&body, USER_ID_CANARY));
|
||||
}
|
||||
assert!(
|
||||
request_count > 0,
|
||||
"explicit opt-in must reach the collector"
|
||||
);
|
||||
assert!(
|
||||
onboarding_request_id_exported,
|
||||
"the collected trace must include the real onboarding HTTP request"
|
||||
);
|
||||
collector_task.abort();
|
||||
}
|
||||
|
||||
fn service_identity() -> ServiceIdentity {
|
||||
ServiceIdentity::try_new("admin-api", "integration-test", "test")
|
||||
.expect("static service identity is valid")
|
||||
}
|
||||
|
||||
fn local_trace_dispatch() -> (tracing::Dispatch, SdkTracerProvider) {
|
||||
let provider = SdkTracerProvider::builder().build();
|
||||
let tracer = provider.tracer("crank");
|
||||
let subscriber =
|
||||
tracing_subscriber::registry().with(tracing_opentelemetry::layer().with_tracer(tracer));
|
||||
(tracing::Dispatch::new(subscriber), provider)
|
||||
}
|
||||
|
||||
fn app_with_request_dispatch(app: Router, dispatch: tracing::Dispatch) -> Router {
|
||||
app.layer(middleware::from_fn(move |request: Request, next: Next| {
|
||||
let dispatch = dispatch.clone();
|
||||
async move { next.run(request).with_subscriber(dispatch).await }
|
||||
}))
|
||||
}
|
||||
|
||||
pub(super) async fn loopback_listener() -> (TcpListener, String) {
|
||||
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let endpoint = format!("http://{}/v1/traces", listener.local_addr().unwrap());
|
||||
(listener, endpoint)
|
||||
}
|
||||
|
||||
pub(super) async fn spawn_otlp_collector() -> (
|
||||
String,
|
||||
mpsc::UnboundedReceiver<Bytes>,
|
||||
tokio::task::JoinHandle<()>,
|
||||
) {
|
||||
let (listener, endpoint) = loopback_listener().await;
|
||||
let (sender, receiver) = mpsc::unbounded_channel();
|
||||
let app = Router::new().route(
|
||||
"/v1/traces",
|
||||
post(move |body: Bytes| {
|
||||
let sender = sender.clone();
|
||||
async move {
|
||||
let _ = sender.send(body);
|
||||
StatusCode::OK
|
||||
}
|
||||
}),
|
||||
);
|
||||
let handle = tokio::spawn(async move {
|
||||
axum::serve(listener, app).await.unwrap();
|
||||
});
|
||||
(endpoint, receiver, handle)
|
||||
}
|
||||
|
||||
pub(super) async fn onboarding_revision(
|
||||
client: &reqwest::Client,
|
||||
base_url: &(impl AsRef<str> + ?Sized),
|
||||
) -> i64 {
|
||||
let base_url = base_url.as_ref();
|
||||
let response = client
|
||||
.get(format!("{base_url}/onboarding"))
|
||||
.send()
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(response.status(), reqwest::StatusCode::OK);
|
||||
response.json::<Value>().await.unwrap()["revision"]
|
||||
.as_i64()
|
||||
.expect("onboarding returns a server revision")
|
||||
}
|
||||
|
||||
pub(super) async fn reject_canary_payload(
|
||||
client: &reqwest::Client,
|
||||
base_url: &(impl AsRef<str> + ?Sized),
|
||||
revision: i64,
|
||||
) {
|
||||
let base_url = base_url.as_ref();
|
||||
let response = client
|
||||
.post(format!("{base_url}/onboarding/events"))
|
||||
.json(&json!({
|
||||
"event": "started",
|
||||
"idempotency_key": "ui:telemetry-canary-rejection",
|
||||
"expected_revision": revision,
|
||||
"user_id": USER_ID_CANARY,
|
||||
"openapi_document": RAW_DOCUMENT_CANARY,
|
||||
}))
|
||||
.send()
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(response.status(), reqwest::StatusCode::UNPROCESSABLE_ENTITY);
|
||||
let body = response.text().await.unwrap();
|
||||
assert!(!body.contains(USER_ID_CANARY));
|
||||
assert!(!body.contains(RAW_DOCUMENT_CANARY));
|
||||
}
|
||||
|
||||
async fn post_started_event(
|
||||
client: &reqwest::Client,
|
||||
base_url: &(impl AsRef<str> + ?Sized),
|
||||
revision: i64,
|
||||
with_request_id: bool,
|
||||
) -> reqwest::Response {
|
||||
let base_url = base_url.as_ref();
|
||||
let mut request = client
|
||||
.post(format!("{base_url}/onboarding/events"))
|
||||
.json(&json!({
|
||||
"event": "started",
|
||||
"idempotency_key": if with_request_id {
|
||||
"ui:telemetry-optin-event"
|
||||
} else {
|
||||
"ui:telemetry-default-event"
|
||||
},
|
||||
"expected_revision": revision,
|
||||
}));
|
||||
if with_request_id {
|
||||
request = request.header("x-request-id", REQUEST_ID);
|
||||
}
|
||||
request.send().await.unwrap()
|
||||
}
|
||||
|
||||
async fn assert_event_is_local(
|
||||
registry: &crank_registry::PostgresRegistry,
|
||||
workspace_id: &WorkspaceId,
|
||||
idempotency_key: &str,
|
||||
) {
|
||||
let record = registry
|
||||
.get_product_event_by_idempotency_key(workspace_id, idempotency_key)
|
||||
.await
|
||||
.unwrap()
|
||||
.expect("successful HTTP event is persisted in the local product event store");
|
||||
assert_eq!(record.event.kind, ProductEventKind::OnboardingStarted);
|
||||
assert_eq!(record.event.idempotency_key, idempotency_key);
|
||||
}
|
||||
|
||||
pub(super) fn string_attribute<'a>(attributes: &'a [KeyValue], key: &str) -> Option<&'a str> {
|
||||
attributes.iter().find_map(|attribute| {
|
||||
(attribute.key == key)
|
||||
.then_some(attribute.value.as_ref()?.value.as_ref()?)
|
||||
.and_then(|value| match value {
|
||||
any_value::Value::StringValue(value) => Some(value.as_str()),
|
||||
_ => None,
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn body_contains(body: &[u8], canary: &str) -> bool {
|
||||
body.windows(canary.len())
|
||||
.any(|window| window == canary.as_bytes())
|
||||
}
|
||||
@@ -0,0 +1,290 @@
|
||||
use super::telemetry::{
|
||||
RAW_DOCUMENT_CANARY, USER_ID_CANARY, body_contains, loopback_listener, onboarding_revision,
|
||||
reject_canary_payload, string_attribute,
|
||||
};
|
||||
use super::*;
|
||||
use axum::{
|
||||
Router,
|
||||
body::{self, Bytes},
|
||||
extract::Request,
|
||||
http::StatusCode,
|
||||
routing::any,
|
||||
};
|
||||
use opentelemetry_proto::tonic::collector::trace::v1::ExportTraceServiceRequest;
|
||||
use prost::Message;
|
||||
use std::{
|
||||
collections::BTreeSet,
|
||||
fs::{self, OpenOptions},
|
||||
net::TcpListener as StdTcpListener,
|
||||
os::unix::fs::{OpenOptionsExt, PermissionsExt},
|
||||
path::PathBuf,
|
||||
process::{Child, Command, Stdio},
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
|
||||
const MASTER_KEY: &str = "test-master-key-00000000000000000000000000000000";
|
||||
const SESSION_SECRET: &str = "test-session-secret";
|
||||
const PASSWORD_PEPPER: &str = "test-password-pepper";
|
||||
const ADMIN_EMAIL: &str = "owner@crank.local";
|
||||
const ADMIN_PASSWORD: &str = "test-password";
|
||||
const REQUEST_ID: &str = "req_admin_process_optin_0001";
|
||||
|
||||
struct AdminProcess {
|
||||
child: Child,
|
||||
log_path: PathBuf,
|
||||
secrets: Vec<String>,
|
||||
}
|
||||
|
||||
impl AdminProcess {
|
||||
fn stop_and_assert_logs_are_safe(&mut self) {
|
||||
let _ = self.child.kill();
|
||||
self.child.wait().expect("admin-api child must be reaped");
|
||||
let logs = fs::read_to_string(&self.log_path).expect("child log must be readable");
|
||||
for secret in &self.secrets {
|
||||
assert!(
|
||||
!logs.contains(secret),
|
||||
"admin-api process log must not contain configured secrets or request canaries"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for AdminProcess {
|
||||
fn drop(&mut self) {
|
||||
let _ = self.child.kill();
|
||||
let _ = self.child.wait();
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
#[serial]
|
||||
async fn production_admin_process_keeps_default_otlp_export_disabled() {
|
||||
let (registry, database_url) = test_registry_with_single_connection().await;
|
||||
let _registry = registry;
|
||||
let storage_root = test_storage_root("onboarding_telemetry_startup_default");
|
||||
let (collector_listener, _) = loopback_listener().await;
|
||||
let (mut process, base_url) = start_admin_process(&database_url, &storage_root, None);
|
||||
wait_until_ready(&mut process, &base_url).await;
|
||||
|
||||
let workspace_url = format!("{base_url}/api/admin/workspaces/ws_default");
|
||||
let client = authorized_client(&workspace_url).await;
|
||||
let revision = onboarding_revision(&client, &workspace_url).await;
|
||||
reject_canary_payload(&client, &workspace_url, revision).await;
|
||||
let response = post_startup_event(&client, &workspace_url, revision, false).await;
|
||||
assert_eq!(response.status(), reqwest::StatusCode::OK);
|
||||
assert_eq!(response.json::<Value>().await.unwrap()["accepted"], true);
|
||||
|
||||
assert!(
|
||||
tokio::time::timeout(Duration::from_secs(2), collector_listener.accept())
|
||||
.await
|
||||
.is_err(),
|
||||
"default production startup must not connect to an OTLP collector"
|
||||
);
|
||||
process.stop_and_assert_logs_are_safe();
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
#[serial]
|
||||
async fn production_admin_process_exports_only_sanitized_request_trace_when_opted_in() {
|
||||
let (registry, database_url) = test_registry_with_single_connection().await;
|
||||
let _registry = registry;
|
||||
let storage_root = test_storage_root("onboarding_telemetry_startup_optin");
|
||||
let (endpoint, mut exported_requests, collector_task) = spawn_startup_collector().await;
|
||||
let (mut process, base_url) =
|
||||
start_admin_process(&database_url, &storage_root, Some(&endpoint));
|
||||
wait_until_ready(&mut process, &base_url).await;
|
||||
|
||||
let workspace_url = format!("{base_url}/api/admin/workspaces/ws_default");
|
||||
let client = authorized_client(&workspace_url).await;
|
||||
let revision = onboarding_revision(&client, &workspace_url).await;
|
||||
reject_canary_payload(&client, &workspace_url, revision).await;
|
||||
let response = post_startup_event(&client, &workspace_url, revision, true).await;
|
||||
assert_eq!(response.status(), reqwest::StatusCode::OK);
|
||||
assert_eq!(response.json::<Value>().await.unwrap()["accepted"], true);
|
||||
|
||||
let mut request_id_exported = false;
|
||||
tokio::time::timeout(Duration::from_secs(12), async {
|
||||
while !request_id_exported {
|
||||
let (path, body) = exported_requests
|
||||
.recv()
|
||||
.await
|
||||
.expect("collector task must remain active");
|
||||
assert_eq!(path, "/v1/traces", "trace-specific endpoint path is exact");
|
||||
let export = ExportTraceServiceRequest::decode(body.as_ref())
|
||||
.expect("production process must send valid OTLP protobuf");
|
||||
for resource_spans in export.resource_spans {
|
||||
for scope_spans in resource_spans.scope_spans {
|
||||
for span in scope_spans.spans {
|
||||
assert_eq!(span.name, "http.request");
|
||||
assert!(span.events.is_empty());
|
||||
assert!(span.links.is_empty());
|
||||
let attribute_keys: BTreeSet<&str> = span
|
||||
.attributes
|
||||
.iter()
|
||||
.map(|attribute| attribute.key.as_str())
|
||||
.collect();
|
||||
assert!(
|
||||
attribute_keys.iter().all(|key| *key == "request_id"),
|
||||
"production request spans may export only the request_id attribute"
|
||||
);
|
||||
if string_attribute(&span.attributes, "request_id") == Some(REQUEST_ID) {
|
||||
request_id_exported = true;
|
||||
}
|
||||
let encoded = format!("{} {:?}", span.name, span.attributes);
|
||||
assert!(!encoded.contains(USER_ID_CANARY));
|
||||
assert!(!encoded.contains(RAW_DOCUMENT_CANARY));
|
||||
}
|
||||
}
|
||||
}
|
||||
assert!(!body_contains(&body, USER_ID_CANARY));
|
||||
assert!(!body_contains(&body, RAW_DOCUMENT_CANARY));
|
||||
}
|
||||
})
|
||||
.await
|
||||
.expect("production exporter must send the onboarding request in a bounded OTLP batch");
|
||||
assert!(
|
||||
request_id_exported,
|
||||
"collector must receive the trace for the onboarding event request"
|
||||
);
|
||||
process.stop_and_assert_logs_are_safe();
|
||||
collector_task.abort();
|
||||
}
|
||||
|
||||
fn start_admin_process(
|
||||
database_url: &str,
|
||||
storage_root: &std::path::Path,
|
||||
otlp_endpoint: Option<&str>,
|
||||
) -> (AdminProcess, String) {
|
||||
fs::create_dir_all(storage_root).expect("isolated storage root must be created");
|
||||
#[cfg(unix)]
|
||||
fs::set_permissions(storage_root, fs::Permissions::from_mode(0o700))
|
||||
.expect("isolated storage root must be private");
|
||||
|
||||
let listener = StdTcpListener::bind("127.0.0.1:0").expect("reserve an ephemeral API port");
|
||||
let address = listener.local_addr().unwrap();
|
||||
drop(listener);
|
||||
let base_url = format!("http://{address}");
|
||||
let log_path = storage_root.join("admin-api.log");
|
||||
let stdout = OpenOptions::new()
|
||||
.write(true)
|
||||
.create_new(true)
|
||||
.mode(0o600)
|
||||
.open(&log_path)
|
||||
.expect("private child log must be created");
|
||||
let stderr = stdout.try_clone().expect("child stderr log handle");
|
||||
|
||||
let mut command = Command::new(env!("CARGO_BIN_EXE_admin-api"));
|
||||
command
|
||||
.env_clear()
|
||||
.env("CRANK_DATABASE_URL", database_url)
|
||||
.env("CRANK_ADMIN_BIND", address.to_string())
|
||||
.env("CRANK_STORAGE_ROOT", storage_root)
|
||||
.env("CRANK_MASTER_KEY", MASTER_KEY)
|
||||
.env("CRANK_SESSION_SECRET", SESSION_SECRET)
|
||||
.env("CRANK_PASSWORD_PEPPER", PASSWORD_PEPPER)
|
||||
.env("CRANK_SESSION_TTL_HOURS", "24")
|
||||
.env("CRANK_BOOTSTRAP_ADMIN_EMAIL", ADMIN_EMAIL)
|
||||
.env("CRANK_BOOTSTRAP_ADMIN_PASSWORD", ADMIN_PASSWORD)
|
||||
.env("CRANK_BOOTSTRAP_ADMIN_DISPLAY_NAME", "Integration Test")
|
||||
.env("CRANK_METRICS_ENABLED", "false")
|
||||
.env("CRANK_BASE_URL", &base_url)
|
||||
.env("CRANK_LOG_LEVEL", "info")
|
||||
.stdout(Stdio::from(stdout))
|
||||
.stderr(Stdio::from(stderr));
|
||||
if let Some(endpoint) = otlp_endpoint {
|
||||
command
|
||||
.env("OTEL_EXPORTER_OTLP_TRACES_ENDPOINT", endpoint)
|
||||
.env("OTEL_EXPORTER_OTLP_TRACES_PROTOCOL", "http/protobuf")
|
||||
.env("OTEL_BSP_MAX_QUEUE_SIZE", "16")
|
||||
.env("OTEL_BSP_MAX_EXPORT_BATCH_SIZE", "1")
|
||||
.env("OTEL_BSP_SCHEDULE_DELAY", "100")
|
||||
.env("OTEL_BSP_EXPORT_TIMEOUT", "2000");
|
||||
}
|
||||
let child = command.spawn().expect("production admin-api binary starts");
|
||||
(
|
||||
AdminProcess {
|
||||
child,
|
||||
log_path,
|
||||
secrets: vec![
|
||||
database_url.to_owned(),
|
||||
MASTER_KEY.to_owned(),
|
||||
SESSION_SECRET.to_owned(),
|
||||
PASSWORD_PEPPER.to_owned(),
|
||||
ADMIN_PASSWORD.to_owned(),
|
||||
USER_ID_CANARY.to_owned(),
|
||||
RAW_DOCUMENT_CANARY.to_owned(),
|
||||
],
|
||||
},
|
||||
base_url,
|
||||
)
|
||||
}
|
||||
|
||||
async fn spawn_startup_collector() -> (
|
||||
String,
|
||||
tokio::sync::mpsc::UnboundedReceiver<(String, Bytes)>,
|
||||
tokio::task::JoinHandle<()>,
|
||||
) {
|
||||
let (listener, endpoint) = loopback_listener().await;
|
||||
let (sender, receiver) = tokio::sync::mpsc::unbounded_channel();
|
||||
let app = Router::new().fallback(any(move |request: Request| {
|
||||
let sender = sender.clone();
|
||||
async move {
|
||||
let path = request.uri().path().to_owned();
|
||||
let body = body::to_bytes(request.into_body(), usize::MAX)
|
||||
.await
|
||||
.expect("collector request body must be readable");
|
||||
let _ = sender.send((path, body));
|
||||
StatusCode::OK
|
||||
}
|
||||
}));
|
||||
let handle = tokio::spawn(async move {
|
||||
axum::serve(listener, app).await.unwrap();
|
||||
});
|
||||
(endpoint, receiver, handle)
|
||||
}
|
||||
|
||||
async fn wait_until_ready(process: &mut AdminProcess, base_url: &str) {
|
||||
let deadline = Instant::now() + Duration::from_secs(45);
|
||||
loop {
|
||||
if let Some(status) = process.child.try_wait().expect("child status is readable") {
|
||||
panic!("production admin-api exited before readiness: {status}");
|
||||
}
|
||||
if reqwest::Client::new()
|
||||
.get(format!("{base_url}/health"))
|
||||
.timeout(Duration::from_millis(500))
|
||||
.send()
|
||||
.await
|
||||
.is_ok_and(|response| response.status().is_success())
|
||||
{
|
||||
return;
|
||||
}
|
||||
assert!(
|
||||
Instant::now() < deadline,
|
||||
"production admin-api readiness deadline"
|
||||
);
|
||||
tokio::time::sleep(Duration::from_millis(100)).await;
|
||||
}
|
||||
}
|
||||
|
||||
async fn post_startup_event(
|
||||
client: &reqwest::Client,
|
||||
workspace_url: &str,
|
||||
revision: i64,
|
||||
with_request_id: bool,
|
||||
) -> reqwest::Response {
|
||||
let mut request = client
|
||||
.post(format!("{workspace_url}/onboarding/events"))
|
||||
.json(&json!({
|
||||
"event": "started",
|
||||
"idempotency_key": if with_request_id {
|
||||
"ui:telemetry-process-optin"
|
||||
} else {
|
||||
"ui:telemetry-process-default"
|
||||
},
|
||||
"expected_revision": revision,
|
||||
}));
|
||||
if with_request_id {
|
||||
request = request.header("x-request-id", REQUEST_ID);
|
||||
}
|
||||
request.send().await.unwrap()
|
||||
}
|
||||
@@ -1,11 +1,17 @@
|
||||
use super::super::common::test_service_with_external_references;
|
||||
mod isolation;
|
||||
use super::*;
|
||||
use axum::{Router, routing::get};
|
||||
use admin_api::error::ApiError;
|
||||
use axum::{Router, body::to_bytes, http::StatusCode, response::IntoResponse, routing::get};
|
||||
use crank_artifacts::ArtifactStore;
|
||||
use crank_core::{Workspace, WorkspaceStatus};
|
||||
use crank_registry::{ArtifactSourceId, CreateWorkspaceRequest, RegistryError};
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::sync::{
|
||||
Arc,
|
||||
atomic::{AtomicUsize, Ordering},
|
||||
};
|
||||
use time::OffsetDateTime;
|
||||
|
||||
// Serialized by the integration harness at baseline f5f40cb167a82b1e3c1ea2475db5cbaed9d9abfa; this v4 payload preserves the then-current security blocker and digest.
|
||||
const BASELINE_V4_SECURITY_PREVIEW_PAYLOAD: &str = r###"{"dependencies":[],"dependency_snapshots":[],"normalization":{"config":{"external_references_enabled":false,"max_aliases":128,"max_bytes":262144,"max_collection_items":10000,"max_depth":64,"max_expanded_nodes":100000,"max_external_document_bytes":262144,"max_nodes":50000,"max_reference_depth":32,"max_reference_documents":32,"max_references":4096,"max_scalar_bytes":262144,"normalizer_version":"normalized-ir-v4","projection_version":"preview-v4"},"ir_fingerprint":"ae2319de76c13d631b26843623092d966e5deea7cda9e3e40e4acc6fa36a25a8","normalizer_version":"normalized-ir-v4","projection_version":"preview-v4"},"preview":{"findings":[{"code":"unsupported_security_requirement","message":"Требование безопасности должно быть привязано к настроенному Auth Profile перед публикацией этой операции; остальные операции не блокируются.","operation_key":"GET /secure","severity":"error"}],"groups":[{"key":"imported_operation","operations":[{"category":"imported","description":"Выполняет GET /public","draft":{"category":"imported","display_name":"Public","input_mapping":{},"input_schema":{"description":"Входные параметры MCP-инструмента","nullable":false,"required":true,"type":"object"},"name":"public","output_mapping":{"rules":[{"required":true,"source":"$.response.body","target":"$.output"}]},"output_schema":{"description":"Успешный ответ без тела","nullable":false,"required":true,"type":"null"},"target":{"base_url":"https://api.example.test","method":"GET","path_template":"/public","response_contract":[{"schema_variant":0,"status_code":200}]},"tool_description":{"description":"Выполняет GET /public","examples":[{"input":{}}],"title":"Public"},"wizard_state":{}},"findings":[{"code":"missing_summary","message":"У метода нет summary, отображаемое имя будет сгенерировано автоматически.","operation_key":"GET /public","severity":"warning"},{"code":"missing_description","message":"У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.","operation_key":"GET /public","severity":"warning"},{"code":"missing_response_schema","message":"У метода не найдена схема успешного ответа, результат будет описан как общий объект.","operation_key":"GET /public","severity":"warning"},{"code":"no_input_fields","message":"У инструмента нет входных параметров. Это нормально для справочных методов, но проверьте, что агенту не нужно передавать фильтры.","operation_key":"GET /public","severity":"info"},{"code":"empty_output_schema","message":"В ответе не найдено отдельных полей. Перед публикацией проверьте схему ответа и маппинг результата.","operation_key":"GET /public","severity":"warning"},{"code":"weak_tool_description","message":"Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.","operation_key":"GET /public","severity":"warning"},{"code":"weak_tool_name","message":"Имя инструмента `public` выглядит слишком общим. Лучше использовать имя с конкретным действием и объектом.","operation_key":"GET /public","severity":"warning"}],"input_fields":0,"key":"GET /public","method":"GET","operation_id":"public","output_fields":0,"path":"/public","server_urls":["https://api.example.test"],"suggested_display_name":"Public","suggested_name":"public"},{"category":"imported","description":"Выполняет GET /secure","draft":{"category":"imported","display_name":"Secure","input_mapping":{},"input_schema":{"description":"Входные параметры MCP-инструмента","nullable":false,"required":true,"type":"object"},"name":"secure","output_mapping":{"rules":[{"required":true,"source":"$.response.body","target":"$.output"}]},"output_schema":{"description":"Успешный ответ без тела","nullable":false,"required":true,"type":"null"},"target":{"base_url":"https://api.example.test","method":"GET","path_template":"/secure","response_contract":[{"schema_variant":0,"status_code":200}]},"tool_description":{"description":"Выполняет GET /secure","examples":[{"input":{}}],"title":"Secure"},"wizard_state":{}},"findings":[{"code":"missing_summary","message":"У мет
Line truncated
|
||||
@@ -43,12 +49,14 @@ async fn external_relative_chain_survives_reversed_builder_order_and_apply_never
|
||||
let origin = format!("http://{address}");
|
||||
|
||||
let registry = test_registry().await;
|
||||
let storage_root = test_storage_root("openapi_import_external_snapshot");
|
||||
let service = test_service_with_external_references(
|
||||
registry.clone(),
|
||||
test_storage_root("openapi_import_external_snapshot"),
|
||||
storage_root.clone(),
|
||||
vec![format!("{origin}/")],
|
||||
);
|
||||
let workspace_id = WorkspaceId::new("ws_default");
|
||||
let foreign_workspace_id = create_workspace(®istry, "ws_external_snapshot_foreign").await;
|
||||
let upload = OpenApiUpload {
|
||||
bytes: format!(
|
||||
r#"
|
||||
@@ -97,6 +105,60 @@ paths:
|
||||
2
|
||||
);
|
||||
|
||||
// The preview response contains only the normalized result. Ownership
|
||||
// envelopes, storage digests, and remote origins stay in the persisted
|
||||
// job payload and are verified through the workspace-scoped registry API.
|
||||
let public_preview = serde_json::to_string(&preview).unwrap();
|
||||
assert!(!public_preview.contains("src_openapi_dep_"));
|
||||
assert!(!public_preview.contains(&origin));
|
||||
let dependencies = job.preview_payload["dependencies"].as_array().unwrap();
|
||||
let store = Arc::new(ArtifactStore::open(&storage_root).unwrap());
|
||||
for dependency in dependencies {
|
||||
let source_id = ArtifactSourceId::new(dependency["source_id"].as_str().unwrap());
|
||||
let digest = dependency["digest"].as_str().unwrap();
|
||||
let canonical_uri = dependency["canonical_uri"].as_str().unwrap();
|
||||
let source = registry
|
||||
.get_artifact_source(&workspace_id, &source_id)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(source.workspace_id, workspace_id);
|
||||
assert_eq!(source.blob.artifact_ref.as_str(), digest);
|
||||
let verified = registry
|
||||
.read_artifact_source(Arc::clone(&store), &workspace_id, &source_id)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(verified.source.blob.artifact_ref.as_str(), digest);
|
||||
let expected_bytes = if canonical_uri.ends_with("/root.yaml") {
|
||||
b"Item: { $ref: './child.yaml#/Item' }".as_slice()
|
||||
} else {
|
||||
assert!(canonical_uri.ends_with("/child.yaml"));
|
||||
b"Item: { type: object, required: [id], properties: { id: { type: string } } }"
|
||||
.as_slice()
|
||||
};
|
||||
assert_eq!(verified.bytes, expected_bytes);
|
||||
|
||||
let foreign_read = registry
|
||||
.read_artifact_source(Arc::clone(&store), &foreign_workspace_id, &source_id)
|
||||
.await
|
||||
.expect_err("foreign workspace must not read an owned snapshot");
|
||||
let unknown_source_id = ArtifactSourceId::new("src_unknown_scope_probe");
|
||||
let unknown_read = registry
|
||||
.read_artifact_source(
|
||||
Arc::clone(&store),
|
||||
&foreign_workspace_id,
|
||||
&unknown_source_id,
|
||||
)
|
||||
.await
|
||||
.expect_err("unknown source must have the same scoped outcome");
|
||||
let foreign_body = source_not_found_body(foreign_read).await;
|
||||
let unknown_body = source_not_found_body(unknown_read).await;
|
||||
assert_eq!(foreign_body, unknown_body);
|
||||
let rendered_body = foreign_body.to_string();
|
||||
assert!(!rendered_body.contains(digest));
|
||||
assert!(!rendered_body.contains(canonical_uri));
|
||||
assert!(!rendered_body.contains(source_id.as_str()));
|
||||
}
|
||||
|
||||
let applied = service
|
||||
.create_openapi_import(
|
||||
&workspace_id,
|
||||
@@ -122,6 +184,59 @@ paths:
|
||||
assert_eq!(active_sources, 0);
|
||||
}
|
||||
|
||||
fn external_upload(origin: &str) -> OpenApiUpload {
|
||||
OpenApiUpload {
|
||||
bytes: format!(
|
||||
r#"
|
||||
openapi: 3.1.0
|
||||
info: {{ title: Scoped external snapshot }}
|
||||
servers: [{{ url: https://api.example.test }}]
|
||||
paths:
|
||||
/items:
|
||||
get:
|
||||
operationId: listItems
|
||||
responses:
|
||||
'200':
|
||||
description: ok
|
||||
content:
|
||||
application/json:
|
||||
schema: {{ $ref: '{origin}/schema.yaml#/Item' }}
|
||||
"#
|
||||
)
|
||||
.into_bytes(),
|
||||
mime_type: "application/yaml".to_owned(),
|
||||
locale: OpenApiUploadLocale::En,
|
||||
}
|
||||
}
|
||||
|
||||
async fn create_workspace(registry: &crank_registry::PostgresRegistry, id: &str) -> WorkspaceId {
|
||||
let workspace_id = WorkspaceId::new(id);
|
||||
let now = OffsetDateTime::now_utc();
|
||||
registry
|
||||
.create_workspace(CreateWorkspaceRequest {
|
||||
workspace: &Workspace {
|
||||
id: workspace_id.clone(),
|
||||
slug: id.replace('_', "-"),
|
||||
display_name: id.to_owned(),
|
||||
status: WorkspaceStatus::Active,
|
||||
settings: serde_json::json!({}),
|
||||
created_at: now,
|
||||
updated_at: now,
|
||||
},
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
workspace_id
|
||||
}
|
||||
|
||||
async fn source_not_found_body(error: RegistryError) -> serde_json::Value {
|
||||
assert!(matches!(&error, RegistryError::SourceNotFound { .. }));
|
||||
let response = ApiError::from(error).into_response();
|
||||
assert_eq!(response.status(), StatusCode::NOT_FOUND);
|
||||
let body = to_bytes(response.into_body(), usize::MAX).await.unwrap();
|
||||
serde_json::from_slice(&body).unwrap()
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[serial]
|
||||
async fn external_secret_document_is_rejected_before_blob_registration() {
|
||||
|
||||
@@ -0,0 +1,287 @@
|
||||
use super::*;
|
||||
use std::time::Duration;
|
||||
use tokio::sync::Notify;
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
#[serial]
|
||||
async fn concurrent_external_previews_keep_snapshot_ownership_workspace_scoped() {
|
||||
let fetches = Arc::new(AtomicUsize::new(0));
|
||||
let fetch_count = Arc::clone(&fetches);
|
||||
let app = Router::new().route(
|
||||
"/schema.yaml",
|
||||
get(move || {
|
||||
let fetch_count = Arc::clone(&fetch_count);
|
||||
async move {
|
||||
fetch_count.fetch_add(1, Ordering::SeqCst);
|
||||
"Item: { type: object, properties: { id: { type: string } } }"
|
||||
}
|
||||
}),
|
||||
);
|
||||
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let address = listener.local_addr().unwrap();
|
||||
let server = tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
|
||||
let origin = format!("http://{address}");
|
||||
let registry = test_registry().await;
|
||||
let storage_root = test_storage_root("openapi_import_external_workspace_isolation");
|
||||
let service = test_service_with_external_references(
|
||||
registry.clone(),
|
||||
storage_root.clone(),
|
||||
vec![format!("{origin}/")],
|
||||
);
|
||||
let workspace_a = create_workspace(®istry, "ws_external_concurrent_a").await;
|
||||
let workspace_b = create_workspace(®istry, "ws_external_concurrent_b").await;
|
||||
|
||||
let (preview_a, preview_b) = tokio::join!(
|
||||
service.preview_openapi_import(&workspace_a, external_upload(&origin)),
|
||||
service.preview_openapi_import(&workspace_b, external_upload(&origin)),
|
||||
);
|
||||
let preview_a = preview_a.unwrap();
|
||||
let preview_b = preview_b.unwrap();
|
||||
server.abort();
|
||||
assert_eq!(
|
||||
fetches.load(Ordering::SeqCst),
|
||||
2,
|
||||
"each workspace fetches its own preview snapshot"
|
||||
);
|
||||
assert_ne!(preview_a.job_id, preview_b.job_id);
|
||||
|
||||
let job_a = registry
|
||||
.get_import_job(&workspace_a, &preview_a.job_id.as_str().into())
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
let job_b = registry
|
||||
.get_import_job(&workspace_b, &preview_b.job_id.as_str().into())
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
let dependency_a = &job_a.preview_payload["dependencies"][0];
|
||||
let dependency_b = &job_b.preview_payload["dependencies"][0];
|
||||
let source_a = ArtifactSourceId::new(dependency_a["source_id"].as_str().unwrap());
|
||||
let source_b = ArtifactSourceId::new(dependency_b["source_id"].as_str().unwrap());
|
||||
let digest_a = dependency_a["digest"].as_str().unwrap();
|
||||
let digest_b = dependency_b["digest"].as_str().unwrap();
|
||||
let canonical_uri = dependency_a["canonical_uri"].as_str().unwrap();
|
||||
assert_eq!(
|
||||
digest_a, digest_b,
|
||||
"identical bytes may share the immutable blob"
|
||||
);
|
||||
assert_eq!(dependency_b["canonical_uri"], dependency_a["canonical_uri"]);
|
||||
assert_ne!(
|
||||
source_a, source_b,
|
||||
"each workspace owns a distinct source relation"
|
||||
);
|
||||
|
||||
let public_a = serde_json::to_string(&preview_a).unwrap();
|
||||
let public_b = serde_json::to_string(&preview_b).unwrap();
|
||||
for response in [&public_a, &public_b] {
|
||||
assert!(!response.contains("src_openapi_dep_"));
|
||||
assert!(!response.contains(digest_a));
|
||||
assert!(!response.contains(canonical_uri));
|
||||
}
|
||||
|
||||
let store = Arc::new(ArtifactStore::open(&storage_root).unwrap());
|
||||
for (workspace_id, source_id) in [(&workspace_a, &source_a), (&workspace_b, &source_b)] {
|
||||
let source = registry
|
||||
.get_artifact_source(workspace_id, source_id)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(source.workspace_id, *workspace_id);
|
||||
assert_eq!(source.blob.artifact_ref.as_str(), digest_a);
|
||||
let verified = registry
|
||||
.read_artifact_source(Arc::clone(&store), workspace_id, source_id)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
verified.bytes,
|
||||
b"Item: { type: object, properties: { id: { type: string } } }"
|
||||
);
|
||||
}
|
||||
|
||||
let foreign_body = source_not_found_body(
|
||||
registry
|
||||
.read_artifact_source(Arc::clone(&store), &workspace_b, &source_a)
|
||||
.await
|
||||
.expect_err("workspace B cannot resolve workspace A source"),
|
||||
)
|
||||
.await;
|
||||
let unknown_source = ArtifactSourceId::new("src_unknown_workspace_probe");
|
||||
let unknown_body = source_not_found_body(
|
||||
registry
|
||||
.read_artifact_source(Arc::clone(&store), &workspace_b, &unknown_source)
|
||||
.await
|
||||
.expect_err("unknown source is indistinguishable from a foreign source"),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(foreign_body, unknown_body);
|
||||
let response = foreign_body.to_string();
|
||||
assert!(!response.contains(digest_a));
|
||||
assert!(!response.contains(canonical_uri));
|
||||
assert!(!response.contains(source_a.as_str()));
|
||||
assert!(!response.contains("exists"));
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
#[serial]
|
||||
async fn existing_foreign_snapshot_does_not_short_circuit_second_workspace_fetch() {
|
||||
let fetches = Arc::new(AtomicUsize::new(0));
|
||||
let fetch_count = Arc::clone(&fetches);
|
||||
let second_fetch_entered = Arc::new(Notify::new());
|
||||
let entered = Arc::clone(&second_fetch_entered);
|
||||
let release_second_fetch = Arc::new(Notify::new());
|
||||
let release = Arc::clone(&release_second_fetch);
|
||||
let app = Router::new().route(
|
||||
"/schema.yaml",
|
||||
get(move || {
|
||||
let fetch_count = Arc::clone(&fetch_count);
|
||||
let entered = Arc::clone(&entered);
|
||||
let release = Arc::clone(&release);
|
||||
async move {
|
||||
let request_number = fetch_count.fetch_add(1, Ordering::SeqCst) + 1;
|
||||
if request_number == 2 {
|
||||
entered.notify_one();
|
||||
release.notified().await;
|
||||
}
|
||||
"Item: { type: object, properties: { id: { type: string } } }"
|
||||
}
|
||||
}),
|
||||
);
|
||||
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let address = listener.local_addr().unwrap();
|
||||
let server = tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
|
||||
let origin = format!("http://{address}");
|
||||
let registry = test_registry().await;
|
||||
let service = test_service_with_external_references(
|
||||
registry.clone(),
|
||||
test_storage_root("openapi_import_external_fetch_oracle"),
|
||||
vec![format!("{origin}/")],
|
||||
);
|
||||
let workspace_a = create_workspace(®istry, "ws_external_oracle_a").await;
|
||||
let workspace_b = create_workspace(®istry, "ws_external_oracle_b").await;
|
||||
|
||||
let preview_a = service
|
||||
.preview_openapi_import(&workspace_a, external_upload(&origin))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(fetches.load(Ordering::SeqCst), 1);
|
||||
|
||||
let preview_b_origin = origin.clone();
|
||||
let preview_b_workspace = workspace_b.clone();
|
||||
let mut preview_b_task = tokio::spawn(async move {
|
||||
service
|
||||
.preview_openapi_import(&preview_b_workspace, external_upload(&preview_b_origin))
|
||||
.await
|
||||
});
|
||||
tokio::time::timeout(Duration::from_secs(10), second_fetch_entered.notified())
|
||||
.await
|
||||
.expect("the second preview must reach its HTTP fetch");
|
||||
|
||||
// The fixture holds the real HTTP response. Poll the preview once after
|
||||
// observing that fetch; it must remain pending until the fixture releases it.
|
||||
tokio::select! {
|
||||
biased;
|
||||
result = &mut preview_b_task => panic!("second preview bypassed its held fetch: {result:?}"),
|
||||
_ = tokio::task::yield_now() => {}
|
||||
}
|
||||
assert_eq!(fetches.load(Ordering::SeqCst), 2);
|
||||
release_second_fetch.notify_one();
|
||||
let preview_b = tokio::time::timeout(Duration::from_secs(10), &mut preview_b_task)
|
||||
.await
|
||||
.expect("the released second preview should complete")
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
server.abort();
|
||||
|
||||
let job_a = registry
|
||||
.get_import_job(&workspace_a, &preview_a.job_id.as_str().into())
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
let job_b = registry
|
||||
.get_import_job(&workspace_b, &preview_b.job_id.as_str().into())
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
let source_a = ArtifactSourceId::new(
|
||||
job_a.preview_payload["dependencies"][0]["source_id"]
|
||||
.as_str()
|
||||
.unwrap(),
|
||||
);
|
||||
let source_b = ArtifactSourceId::new(
|
||||
job_b.preview_payload["dependencies"][0]["source_id"]
|
||||
.as_str()
|
||||
.unwrap(),
|
||||
);
|
||||
assert_ne!(source_a, source_b);
|
||||
assert_eq!(fetches.load(Ordering::SeqCst), 2);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[serial]
|
||||
async fn file_origin_and_relative_chain_are_blocked_without_file_or_network_access() {
|
||||
const PRIVATE_FILE_BYTES: &str = "PrivateCanary: file-origin-secret-must-never-be-read";
|
||||
const HYPOTHETICAL_FILE_ROOT: &str = "Item: { $ref: './private/canary.yaml#/Secret' }";
|
||||
let file_fixture_root = test_storage_root("openapi_import_file_origin_fixture");
|
||||
let private_directory = file_fixture_root.join("private");
|
||||
std::fs::create_dir_all(&private_directory).unwrap();
|
||||
std::fs::write(file_fixture_root.join("root.yaml"), HYPOTHETICAL_FILE_ROOT).unwrap();
|
||||
std::fs::write(
|
||||
private_directory.join("canary.yaml"),
|
||||
format!("Secret: {{ type: string, example: '{PRIVATE_FILE_BYTES}' }}"),
|
||||
)
|
||||
.unwrap();
|
||||
let file_origin = url::Url::from_file_path(file_fixture_root.join("root.yaml")).unwrap();
|
||||
let file_origin_for_response = file_origin.clone();
|
||||
let requests = Arc::new(AtomicUsize::new(0));
|
||||
let request_count = Arc::clone(&requests);
|
||||
let app = Router::new().route(
|
||||
"/schema.yaml",
|
||||
get(move || {
|
||||
let request_count = Arc::clone(&request_count);
|
||||
async move {
|
||||
request_count.fetch_add(1, Ordering::SeqCst);
|
||||
format!("Item: {{ $ref: '{file_origin_for_response}#/Item' }}")
|
||||
}
|
||||
}),
|
||||
);
|
||||
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let address = listener.local_addr().unwrap();
|
||||
let server = tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
|
||||
let origin = format!("http://{address}");
|
||||
let registry = test_registry().await;
|
||||
let service = test_service_with_external_references(
|
||||
registry,
|
||||
test_storage_root("openapi_import_file_origin_blocked"),
|
||||
vec![format!("{origin}/")],
|
||||
);
|
||||
let preview = service
|
||||
.preview_openapi_import(&WorkspaceId::new("ws_default"), external_upload(&origin))
|
||||
.await
|
||||
.unwrap();
|
||||
server.abort();
|
||||
|
||||
assert_eq!(
|
||||
requests.load(Ordering::SeqCst),
|
||||
1,
|
||||
"only the explicitly allowlisted HTTP root is fetched"
|
||||
);
|
||||
let findings = preview
|
||||
.preview
|
||||
.groups
|
||||
.iter()
|
||||
.flat_map(|group| &group.operations)
|
||||
.flat_map(|operation| &operation.findings)
|
||||
.chain(preview.preview.findings.iter())
|
||||
.collect::<Vec<_>>();
|
||||
assert!(
|
||||
findings.iter().any(|finding| {
|
||||
finding.severity == crank_import::rest::ImportFindingSeverity::Error
|
||||
&& finding.code == "reference_target_missing"
|
||||
}),
|
||||
"file origin must produce a blocking unresolved-reference finding"
|
||||
);
|
||||
let report = serde_json::to_string(&preview).unwrap();
|
||||
assert!(!report.contains(file_origin.as_str()));
|
||||
assert!(!report.contains(PRIVATE_FILE_BYTES));
|
||||
assert!(!report.contains(HYPOTHETICAL_FILE_ROOT));
|
||||
}
|
||||
@@ -32,6 +32,9 @@ impl MachineCredentialVerifier for CorpusMcpCredentialVerifier {
|
||||
|
||||
use super::super::common::*;
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
mod crash_recovery;
|
||||
|
||||
const OPENAPI_SINGLE: &str = r#"
|
||||
openapi: 3.0.3
|
||||
info: { title: Apply test }
|
||||
@@ -98,6 +101,7 @@ paths:
|
||||
responses: { '200': { description: ok } }
|
||||
"#;
|
||||
|
||||
#[derive(Clone)]
|
||||
struct CreatedPlan {
|
||||
reference: String,
|
||||
digest: String,
|
||||
|
||||
@@ -0,0 +1,330 @@
|
||||
//! Kill the actual HTTP-serving process at durable Apply boundaries, then
|
||||
//! restart the same production router against the parent's database/storage.
|
||||
|
||||
use super::*;
|
||||
use crank_artifacts::test_support::{FaultAction, set_checkpoint};
|
||||
use std::{
|
||||
path::PathBuf,
|
||||
process::{Child, Command, Stdio},
|
||||
time::Instant,
|
||||
};
|
||||
|
||||
const CHILD_TEST: &str =
|
||||
"integration::openapi_import::import_plan_apply::crash_recovery::http_apply_crash_child";
|
||||
const CHILD_CONFIG: &str = "CRANK_TEST_APPLY_CRASH_CONFIG";
|
||||
|
||||
struct PrivateConfigGuard(PathBuf);
|
||||
impl Drop for PrivateConfigGuard {
|
||||
fn drop(&mut self) {
|
||||
let _ = std::fs::remove_file(&self.0);
|
||||
}
|
||||
}
|
||||
|
||||
struct ChildGuard(Child);
|
||||
impl Drop for ChildGuard {
|
||||
fn drop(&mut self) {
|
||||
let _ = self.0.kill();
|
||||
let _ = self.0.wait();
|
||||
}
|
||||
}
|
||||
|
||||
// Child configuration contains only isolated test fixture credentials. Keep it
|
||||
// in a private file and never include it in assertions, stdout or task logs.
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn http_apply_crash_child() {
|
||||
let Ok(path) = std::env::var(CHILD_CONFIG) else {
|
||||
return;
|
||||
};
|
||||
let config: Value = serde_json::from_slice(&std::fs::read(path).unwrap()).unwrap();
|
||||
let registry = crank_registry::PostgresRegistry::connect(config["database"].as_str().unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
let root = PathBuf::from(config["storage"].as_str().unwrap());
|
||||
let server = spawn_admin_api(build_test_app(registry, root)).await;
|
||||
set_checkpoint(config["stage"].as_str().unwrap(), FaultAction::Exit);
|
||||
std::fs::write(config["ready"].as_str().unwrap(), server.as_ref()).unwrap();
|
||||
std::future::pending::<()>().await;
|
||||
}
|
||||
|
||||
async fn counts(registry: &crank_registry::PostgresRegistry) -> (i64, i64, i64) {
|
||||
sqlx::query_as(
|
||||
"select (select count(*) from operations),
|
||||
(select count(*) from operation_versions),
|
||||
(select count(*) from import_plan_applications)",
|
||||
)
|
||||
.fetch_one(registry.pool())
|
||||
.await
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
async fn crash_then_restart(stage: &str, committed: bool) {
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
let (registry, database_url) = test_registry_with_single_connection().await;
|
||||
let root = test_storage_root(stage);
|
||||
std::fs::create_dir_all(&root).unwrap();
|
||||
std::fs::set_permissions(&root, std::fs::Permissions::from_mode(0o700)).unwrap();
|
||||
let server = spawn_admin_api(build_test_app(registry.clone(), root.clone())).await;
|
||||
let client = authorized_client(&server).await;
|
||||
let plan = create_plan(&client, &server, OPENAPI_SINGLE, full_plan()).await;
|
||||
let store = crank_artifacts::ArtifactStore::open(&root).unwrap();
|
||||
let artifact = store.put_registered(OPENAPI_SINGLE.as_bytes()).unwrap();
|
||||
let key = format!("crash-{stage}");
|
||||
assert_eq!(counts(®istry).await, (0, 0, 0));
|
||||
drop(server);
|
||||
|
||||
let ready = root.join("child.ready");
|
||||
let config_path = root.join("child-config.json");
|
||||
let _private_config = PrivateConfigGuard(config_path.clone());
|
||||
{
|
||||
use std::io::Write;
|
||||
use std::os::unix::fs::OpenOptionsExt;
|
||||
let mut file = std::fs::OpenOptions::new()
|
||||
.write(true)
|
||||
.create_new(true)
|
||||
.mode(0o600)
|
||||
.open(&config_path)
|
||||
.unwrap();
|
||||
let config = json!({"database":database_url,"storage":root,"stage":stage,"ready":ready});
|
||||
file.write_all(&serde_json::to_vec(&config).unwrap())
|
||||
.unwrap();
|
||||
}
|
||||
let mut child = ChildGuard(
|
||||
Command::new(std::env::current_exe().unwrap())
|
||||
.args(["--exact", CHILD_TEST, "--nocapture"])
|
||||
.env(CHILD_CONFIG, &config_path)
|
||||
.stdin(Stdio::null())
|
||||
.stdout(Stdio::null())
|
||||
.stderr(Stdio::null())
|
||||
.spawn()
|
||||
.unwrap(),
|
||||
);
|
||||
let start = Instant::now();
|
||||
let child_url = loop {
|
||||
if let Ok(url) = std::fs::read_to_string(&ready) {
|
||||
break url;
|
||||
}
|
||||
assert!(
|
||||
child.0.try_wait().unwrap().is_none(),
|
||||
"child exited before HTTP readiness"
|
||||
);
|
||||
assert!(
|
||||
start.elapsed() < Duration::from_secs(30),
|
||||
"child HTTP readiness deadline"
|
||||
);
|
||||
tokio::time::sleep(Duration::from_millis(10)).await;
|
||||
};
|
||||
let response = client
|
||||
.post(format!(
|
||||
"{child_url}/imports/openapi/{}/apply",
|
||||
plan.reference
|
||||
))
|
||||
.json(&json!({"plan_digest":plan.digest,"idempotency_key":key}))
|
||||
.timeout(Duration::from_secs(30))
|
||||
.send()
|
||||
.await;
|
||||
assert!(response.is_err(), "crash must prevent an HTTP response");
|
||||
let status = loop {
|
||||
if let Some(status) = child.0.try_wait().unwrap() {
|
||||
break status;
|
||||
}
|
||||
assert!(
|
||||
start.elapsed() < Duration::from_secs(35),
|
||||
"child crash deadline"
|
||||
);
|
||||
tokio::time::sleep(Duration::from_millis(10)).await;
|
||||
};
|
||||
assert_eq!(
|
||||
status.code(),
|
||||
Some(86),
|
||||
"must hit the exact armed commit boundary"
|
||||
);
|
||||
std::fs::remove_file(config_path).unwrap();
|
||||
std::fs::remove_file(ready).unwrap();
|
||||
let expected = if committed { (1, 1, 1) } else { (0, 0, 0) };
|
||||
// An independent connection can observe only fully committed state.
|
||||
assert_eq!(counts(®istry).await, expected);
|
||||
let before: Option<Value> = sqlx::query_scalar(
|
||||
"select report from import_plan_applications where workspace_id='ws_default' and idempotency_key=$1",
|
||||
).bind(&key).fetch_optional(registry.pool()).await.unwrap();
|
||||
assert_eq!(before.is_some(), committed);
|
||||
|
||||
// The source of this nonexpired job remains referenced after process loss;
|
||||
// cleanup cannot make its plan snapshot eligible for orphan deletion.
|
||||
assert_eq!(sqlx::query_scalar::<_, i64>(
|
||||
"select count(*) from artifact_sources where workspace_id='ws_default' and lifecycle='active'",
|
||||
).fetch_one(registry.pool()).await.unwrap(), 1);
|
||||
let cleanup = registry.cleanup_expired_import_jobs(32).await;
|
||||
assert!(cleanup.is_ok());
|
||||
let now = time::OffsetDateTime::now_utc();
|
||||
let claim = registry
|
||||
.claim_artifact_reconciliation(crank_registry::ClaimArtifactReconciliationRequest {
|
||||
artifact: &artifact,
|
||||
token: crank_registry::ArtifactClaimToken::generate(),
|
||||
claimed_at: now,
|
||||
lease_expires_at: now + time::Duration::minutes(5),
|
||||
detached_grace: time::Duration::hours(24),
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(matches!(
|
||||
claim,
|
||||
crank_registry::ArtifactClaimOutcome::ActiveReference
|
||||
));
|
||||
assert_eq!(
|
||||
store.read(artifact.artifact_ref()).unwrap(),
|
||||
OPENAPI_SINGLE.as_bytes()
|
||||
);
|
||||
assert_eq!(sqlx::query_scalar::<_, i64>(
|
||||
"select count(*) from artifact_sources where workspace_id='ws_default' and lifecycle='active'",
|
||||
).fetch_one(registry.pool()).await.unwrap(), 1);
|
||||
|
||||
let restarted = spawn_admin_api(build_test_app(registry.clone(), root)).await;
|
||||
let (status, report) = apply_plan(&client, &restarted, &plan, &key).await;
|
||||
assert_eq!(status, reqwest::StatusCode::OK);
|
||||
if let Some(before) = before {
|
||||
assert_eq!(
|
||||
report, before,
|
||||
"restart must return the committed immutable report"
|
||||
);
|
||||
}
|
||||
assert_eq!(report["plan_digest"], plan.digest);
|
||||
assert_eq!(report["counts"]["created"], 1);
|
||||
let (status, replay) = apply_plan(&client, &restarted, &plan, &key).await;
|
||||
assert_eq!(status, reqwest::StatusCode::OK);
|
||||
assert_eq!(
|
||||
replay, report,
|
||||
"exact retry must be byte-equivalent structured report"
|
||||
);
|
||||
assert_eq!(counts(®istry).await, (1, 1, 1));
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
#[serial]
|
||||
async fn http_process_crash_before_commit_rolls_back_and_retries_after_restart() {
|
||||
crash_then_restart("apply_before_commit", false).await;
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
#[serial]
|
||||
async fn http_process_crash_after_commit_before_response_replays_after_restart() {
|
||||
crash_then_restart("apply_after_commit", true).await;
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
#[serial]
|
||||
async fn concurrent_distinct_overlapping_http_plans_have_one_coherent_commit() {
|
||||
let (seed_registry, database_url) = test_registry_with_single_connection().await;
|
||||
// The ordinary test registry has only one connection and would serialize
|
||||
// both requests before their transactions even start.
|
||||
let registry = crank_registry::PostgresRegistry::connect_with_options_and_pool_config(
|
||||
database_url
|
||||
.parse::<sqlx::postgres::PgConnectOptions>()
|
||||
.unwrap(),
|
||||
crank_registry::PostgresPoolConfig::try_new(4, 0, 5_000, 60_000, 1_800_000).unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
drop(seed_registry);
|
||||
let server = spawn_admin_api(build_test_app(
|
||||
registry.clone(),
|
||||
test_storage_root("distinct-overlap-plans"),
|
||||
))
|
||||
.await;
|
||||
let client = authorized_client(&server).await;
|
||||
let a = create_plan(&client, &server, OPENAPI_SINGLE, full_plan()).await;
|
||||
let b = create_plan(&client, &server, OPENAPI_TWO_OPERATIONS, full_plan()).await;
|
||||
assert_ne!(a.reference, b.reference);
|
||||
assert_ne!(a.digest, b.digest);
|
||||
let a_key = "overlap-distinct-a";
|
||||
let b_key = "overlap-distinct-b";
|
||||
// Hold the production workspace row lock until BOTH independent Apply
|
||||
// transactions are blocked on it. Merely joining requests could otherwise
|
||||
// exercise two sequential transactions.
|
||||
let mut gate = registry.pool().begin().await.unwrap();
|
||||
let gate_pid: i32 = sqlx::query_scalar("select pg_backend_pid()")
|
||||
.fetch_one(&mut *gate)
|
||||
.await
|
||||
.unwrap();
|
||||
sqlx::query("select id from workspaces where id = 'ws_default' for update")
|
||||
.fetch_one(&mut *gate)
|
||||
.await
|
||||
.unwrap();
|
||||
let a_task = tokio::spawn({
|
||||
let (client, server, a) = (client.clone(), server.to_string(), a.clone());
|
||||
async move { apply_plan(&client, &server, &a, a_key).await }
|
||||
});
|
||||
let b_task = tokio::spawn({
|
||||
let (client, server, b) = (client.clone(), server.to_string(), b.clone());
|
||||
async move { apply_plan(&client, &server, &b, b_key).await }
|
||||
});
|
||||
let mut observed_waiting = 0;
|
||||
let barrier_result = tokio::time::timeout(Duration::from_secs(15), async {
|
||||
loop {
|
||||
let waiting: i64 = sqlx::query_scalar(
|
||||
"with recursive blocked(pid) as (
|
||||
select pid from pg_catalog.pg_stat_activity
|
||||
where $1 = any(pg_catalog.pg_blocking_pids(pid))
|
||||
union
|
||||
select activity.pid from pg_catalog.pg_stat_activity activity
|
||||
join blocked on blocked.pid = any(pg_catalog.pg_blocking_pids(activity.pid))
|
||||
)
|
||||
select count(distinct pid) from blocked",
|
||||
)
|
||||
.bind(gate_pid)
|
||||
.fetch_one(registry.pool())
|
||||
.await
|
||||
.unwrap();
|
||||
observed_waiting = waiting;
|
||||
if waiting == 2 {
|
||||
break;
|
||||
}
|
||||
assert!(!a_task.is_finished() && !b_task.is_finished());
|
||||
tokio::time::sleep(Duration::from_millis(10)).await;
|
||||
}
|
||||
})
|
||||
.await;
|
||||
assert!(
|
||||
barrier_result.is_ok(),
|
||||
"both distinct Apply transactions must reach the workspace lock; observed {observed_waiting}"
|
||||
);
|
||||
gate.commit().await.unwrap();
|
||||
let ((a_status, a_report), (b_status, b_report)) =
|
||||
(a_task.await.unwrap(), b_task.await.unwrap());
|
||||
assert!(
|
||||
a_status.is_success() ^ b_status.is_success(),
|
||||
"exactly one overlapping plan commits: {a_status}/{b_status}"
|
||||
);
|
||||
let (winner, loser_status, loser) = if a_status.is_success() {
|
||||
(&a_report, b_status, &b_report)
|
||||
} else {
|
||||
(&b_report, a_status, &a_report)
|
||||
};
|
||||
assert_eq!(loser_status, reqwest::StatusCode::CONFLICT);
|
||||
let error_code = loser
|
||||
.pointer("/error/context/error_code")
|
||||
.and_then(Value::as_str)
|
||||
.unwrap();
|
||||
assert!(
|
||||
matches!(
|
||||
error_code,
|
||||
"import_plan_fingerprint_stale" | "import_plan_name_conflict"
|
||||
),
|
||||
"loser must get the exact typed catalog/name conflict, got {error_code}"
|
||||
);
|
||||
let expected = winner["counts"]["created"].as_i64().unwrap();
|
||||
assert!(expected == 1 || expected == 2);
|
||||
assert_eq!(
|
||||
counts(®istry).await,
|
||||
(expected, expected, 1),
|
||||
"loser cannot leave partial Operations/versions/application"
|
||||
);
|
||||
let (winning_plan, winning_key) = if a_status.is_success() {
|
||||
(&a, a_key)
|
||||
} else {
|
||||
(&b, b_key)
|
||||
};
|
||||
let (status, replay) = apply_plan(&client, &server, winning_plan, winning_key).await;
|
||||
assert_eq!(status, reqwest::StatusCode::OK);
|
||||
assert_eq!(&replay, winner);
|
||||
assert_eq!(counts(®istry).await, (expected, expected, 1));
|
||||
}
|
||||
@@ -10,12 +10,20 @@ const TEST_MASTER_KEY: &str = "test-master-key-00000000000000000000000000000000"
|
||||
const REGISTERED_MASTER_KEY: &str = "registered-master-key-CANARY_SECRET_VALUE-00000000";
|
||||
const WRONG_MASTER_KEY: &str = "wrong-master-key-CANARY_SECRET_VALUE-0000000000000";
|
||||
|
||||
fn isolated_metrics_bind() -> String {
|
||||
let listener = std::net::TcpListener::bind("127.0.0.1:0").expect("ephemeral metrics port");
|
||||
let addr = listener.local_addr().unwrap().to_string();
|
||||
drop(listener);
|
||||
addr
|
||||
}
|
||||
|
||||
fn run_with(entries: &[(&str, &str)]) -> String {
|
||||
let mut command = Command::new(env!("CARGO_BIN_EXE_mcp-server"));
|
||||
for field in crank_config::field_registry() {
|
||||
command.env_remove(field.env_name);
|
||||
}
|
||||
command.env("CRANK_MASTER_KEY", TEST_MASTER_KEY);
|
||||
command.env("CRANK_MCP_METRICS_BIND", isolated_metrics_bind());
|
||||
for (name, value) in entries {
|
||||
command.env(name, value);
|
||||
}
|
||||
|
||||
@@ -2247,6 +2247,7 @@ Object.assign(TRANSLATIONS.en, {
|
||||
'openapi.finding.missing_servers': 'No base URL was found in the document.',
|
||||
'openapi.finding.multiple_servers': 'Several base URLs are available; choose one before creating drafts.',
|
||||
'openapi.finding.unsupported_request_body': 'This request body needs review in the wizard.',
|
||||
'openapi.finding.external_reference_disabled': 'External references are blocked by default. Check the reference at this source location. A server administrator must add trusted addresses to the server allowlist. For offline use, prepare a self-contained OpenAPI file with its referenced schemas included before uploading; this screen accepts one file and does not upload separate reference files.',
|
||||
'openapi.finding.operation_name_renamed': 'The operation name was adjusted to keep it unique.',
|
||||
'openapi.finding.operation_name_conflict': 'An operation with this name already exists.',
|
||||
'openapi.finding.weak_tool_description': 'The tool description is too short.',
|
||||
@@ -2449,6 +2450,7 @@ Object.assign(TRANSLATIONS.ru, {
|
||||
'openapi.finding.missing_servers': 'В документе не найден base URL.',
|
||||
'openapi.finding.multiple_servers': 'В документе несколько base URL; выберите один перед созданием черновиков.',
|
||||
'openapi.finding.unsupported_request_body': 'Тело этого запроса нужно проверить в мастере.',
|
||||
'openapi.finding.external_reference_disabled': 'Внешние ссылки по умолчанию запрещены. Проверьте ссылку по указанному пути. Администратор сервера может добавить доверенные адреса в список разрешённых адресов. Для работы без сети заранее подготовьте самодостаточный OpenAPI-файл, включив в него схемы из внешних ссылок; этот экран принимает один файл и не загружает отдельные файлы зависимостей.',
|
||||
'openapi.finding.operation_name_renamed': 'Имя операции изменено, чтобы сохранить уникальность.',
|
||||
'openapi.finding.operation_name_conflict': 'Операция с таким именем уже существует.',
|
||||
'openapi.finding.weak_tool_description': 'Описание инструмента слишком короткое.',
|
||||
|
||||
@@ -724,6 +724,7 @@
|
||||
multiple_servers: 'openapi.finding.multiple_servers',
|
||||
unsupported_request_body: 'openapi.finding.unsupported_request_body',
|
||||
unsupported_security_scheme: 'openapi.finding.unsupported_security_scheme',
|
||||
external_reference_disabled: 'openapi.finding.external_reference_disabled',
|
||||
unsupported_security_and_requirement: 'openapi.finding.unsupported_security_and_requirement',
|
||||
auth_profile_required: 'openapi.error.auth_profile_required',
|
||||
operation_name_renamed: 'openapi.finding.operation_name_renamed',
|
||||
|
||||
@@ -101,6 +101,7 @@ trap cleanup EXIT
|
||||
trap 'exit 130' INT TERM
|
||||
|
||||
cleanup
|
||||
rm -f "$TMP_DIR/admin-control.request" "$TMP_DIR/admin-control.ack"
|
||||
|
||||
show_startup_log() {
|
||||
local log_file="$1"
|
||||
@@ -235,7 +236,8 @@ chmod 700 "$CRANK_STORAGE_ROOT"
|
||||
|
||||
(
|
||||
cd "$ROOT_DIR/apps/ui"
|
||||
exec node scripts/playwright-http-fixture.js >"$LOG_DIR/http-fixture.log" 2>&1
|
||||
exec env CRANK_E2E_STREAM_FIXTURE_PORT="$STREAM_FIXTURE_PORT" \
|
||||
node scripts/playwright-http-fixture.js >"$LOG_DIR/http-fixture.log" 2>&1
|
||||
) &
|
||||
echo $! > "$TMP_DIR/http-fixture.pid"
|
||||
|
||||
@@ -292,12 +294,27 @@ if ! (
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Launch the built executable directly so the test lifecycle controls the
|
||||
# actual server PID, rather than a Cargo wrapper. This control exists only in
|
||||
# the local Playwright harness and adds no application endpoint/configuration.
|
||||
(
|
||||
cd "$ROOT_DIR"
|
||||
exec env -u CRANK_MCP_BIND -u CRANK_MCP_REFRESH_MS CRANK_DEMO_SEED=true \
|
||||
cargo run -p admin-api --bin admin-api >>"$LOG_DIR/admin-api.log" 2>&1
|
||||
) &
|
||||
echo $! > "$TMP_DIR/admin-api.pid"
|
||||
cargo build -p admin-api --bin admin-api --example e2e_storage_counts >>"$LOG_DIR/admin-api.log" 2>&1
|
||||
)
|
||||
ADMIN_BINARY="${CARGO_TARGET_DIR:-$ROOT_DIR/target}/debug/admin-api"
|
||||
if [[ "$ADMIN_BINARY" != /* ]]; then
|
||||
ADMIN_BINARY="$ROOT_DIR/$ADMIN_BINARY"
|
||||
fi
|
||||
STORAGE_COUNTS_BINARY="$(dirname "$ADMIN_BINARY")/examples/e2e_storage_counts"
|
||||
start_admin() {
|
||||
(
|
||||
cd "$ROOT_DIR"
|
||||
exec env -u CRANK_MCP_BIND -u CRANK_MCP_REFRESH_MS CRANK_DEMO_SEED=true \
|
||||
"$ADMIN_BINARY" >>"$LOG_DIR/admin-api.log" 2>&1
|
||||
) &
|
||||
echo $! > "$TMP_DIR/admin-api.pid"
|
||||
}
|
||||
start_admin
|
||||
|
||||
wait_for_http \
|
||||
"admin-api" \
|
||||
@@ -322,7 +339,9 @@ wait_for_http \
|
||||
|
||||
(
|
||||
cd "$ROOT_DIR/apps/ui"
|
||||
exec node scripts/playwright-ui-server.js >"$LOG_DIR/ui-server.log" 2>&1
|
||||
exec env CRANK_E2E_UI_PORT="$UI_PORT" CRANK_E2E_ADMIN_PORT="$ADMIN_PORT" \
|
||||
CRANK_E2E_MCP_PORT="$MCP_PORT" \
|
||||
node scripts/playwright-ui-server.js >"$LOG_DIR/ui-server.log" 2>&1
|
||||
) &
|
||||
echo $! > "$TMP_DIR/ui-server.pid"
|
||||
|
||||
@@ -335,5 +354,43 @@ wait_for_http \
|
||||
echo "Crank UI e2e stack is ready on http://127.0.0.1:$UI_PORT"
|
||||
|
||||
while true; do
|
||||
if [[ -f "$TMP_DIR/admin-control.request" ]]; then
|
||||
command=""
|
||||
read -r command < "$TMP_DIR/admin-control.request" || true
|
||||
rm -f "$TMP_DIR/admin-control.request"
|
||||
case "$command" in
|
||||
stop)
|
||||
if [[ -f "$TMP_DIR/admin-api.pid" ]]; then
|
||||
admin_pid="$(cat "$TMP_DIR/admin-api.pid")"
|
||||
kill "$admin_pid" 2>/dev/null || true
|
||||
wait "$admin_pid" || true
|
||||
rm -f "$TMP_DIR/admin-api.pid"
|
||||
fi
|
||||
;;
|
||||
start)
|
||||
if [[ ! -f "$TMP_DIR/admin-api.pid" ]] || \
|
||||
! kill -0 "$(cat "$TMP_DIR/admin-api.pid")" 2>/dev/null; then
|
||||
start_admin
|
||||
fi
|
||||
wait_for_http "restarted admin-api" "http://127.0.0.1:$ADMIN_PORT/health" \
|
||||
"$TMP_DIR/admin-api.pid" "$LOG_DIR/admin-api.log" || exit 1
|
||||
;;
|
||||
counts\ *)
|
||||
workspace="${command#counts }"
|
||||
if [[ ! "$workspace" =~ ^[a-zA-Z0-9_-]{1,128}$ ]]; then
|
||||
echo "Invalid local storage observation scope" >&2
|
||||
exit 1
|
||||
fi
|
||||
"$STORAGE_COUNTS_BINARY" "$workspace" > "$TMP_DIR/storage-counts.json.tmp"
|
||||
mv "$TMP_DIR/storage-counts.json.tmp" "$TMP_DIR/storage-counts.json"
|
||||
;;
|
||||
*)
|
||||
echo "Unknown local Admin API test control command" >&2
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
printf '%s\n' "$command" > "$TMP_DIR/admin-control.ack.tmp"
|
||||
mv "$TMP_DIR/admin-control.ack.tmp" "$TMP_DIR/admin-control.ack"
|
||||
fi
|
||||
sleep 1
|
||||
done
|
||||
@@ -292,6 +292,8 @@ test('authentication recovery returns to an incomplete authoritative onboarding
|
||||
});
|
||||
|
||||
await page.goto('/');
|
||||
// Wait for the initial projection and its automatic panel opening before toggling.
|
||||
await expect(page.getByTestId('onboarding-trigger')).toContainText(/3\s*\/\s*7/);
|
||||
if (!await page.getByTestId('onboarding-checklist').isVisible()) {
|
||||
await page.getByTestId('onboarding-trigger').click();
|
||||
}
|
||||
|
||||
@@ -61,7 +61,7 @@ function planResponse(prefix) {
|
||||
};
|
||||
}
|
||||
|
||||
async function preparePlanFlow(page, locale) {
|
||||
async function preparePlanFlow(page, locale, reportFixture) {
|
||||
const planCalls = [];
|
||||
const applyCalls = [];
|
||||
const prefix = uniqueName('plan_apply');
|
||||
@@ -98,7 +98,7 @@ async function preparePlanFlow(page, locale) {
|
||||
await route.fulfill({
|
||||
status: 200,
|
||||
contentType: 'application/json',
|
||||
body: JSON.stringify({
|
||||
body: JSON.stringify(reportFixture || {
|
||||
plan_digest: 'a'.repeat(64),
|
||||
counts: { selected: 1, excluded: 1, created: 0, versioned: 1 },
|
||||
outcomes: [{ status: 'versioned' }],
|
||||
@@ -196,6 +196,63 @@ test('[P1] AC-13 double-click sends one Apply with the stored reference, digest
|
||||
expect(applyCalls[0].payload.idempotency_key.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
for (const locale of ['en', 'ru']) {
|
||||
test(`[P1] AC-2.13-13 renders every immutable Apply outcome in ${locale}`, async ({ page }) => {
|
||||
const reportFixture = {
|
||||
plan_digest: 'd'.repeat(64),
|
||||
idempotency_key: 'apply-report-full-outcomes',
|
||||
status: 'applied',
|
||||
counts: { selected: 5, excluded: 0, created: 1, versioned: 1, renamed: 1, skipped: 1, rejected: 1 },
|
||||
outcomes: [
|
||||
{ key: 'GET /created', name: 'created_operation', status: 'created', version: 1 },
|
||||
{ key: 'GET /versioned', name: 'versioned_operation', status: 'versioned', version: 2 },
|
||||
{ key: 'GET /renamed', name: 'renamed_operation', status: 'renamed', version: 1 },
|
||||
{ key: 'GET /skipped', name: 'skipped_operation', status: 'skipped', reason: 'not_selected' },
|
||||
{
|
||||
key: 'GET /rejected',
|
||||
name: 'rejected_operation',
|
||||
status: 'rejected',
|
||||
reason: 'unsupported_schema',
|
||||
message: 'raw-report-canary https://report-secret.example.test/internal',
|
||||
},
|
||||
],
|
||||
};
|
||||
const { releaseApplyResponse } = await preparePlanFlow(page, locale, reportFixture);
|
||||
await page.locator('[data-testid="openapi-import-apply-confirmation"]').getByRole('button', {
|
||||
name: localized('Apply import', 'Применить импорт'),
|
||||
}).click();
|
||||
releaseApplyResponse();
|
||||
|
||||
const report = page.locator('[data-testid="openapi-import-apply-result"]');
|
||||
await expect(report).toBeVisible();
|
||||
await expect(report.locator('.openapi-import-plan-summary')).toContainText(
|
||||
localized('Applied: 3; skipped: 1; excluded: 0; rejected: 1.', 'Применено: 3; пропущено: 1; исключено: 0; отклонено: 1.'),
|
||||
);
|
||||
for (const outcome of reportFixture.outcomes) {
|
||||
const row = report.locator(`[data-outcome="${outcome.status}"]`);
|
||||
await expect(row).toHaveCount(1);
|
||||
await expect(row).toContainText(outcome.name);
|
||||
await expect(row).toContainText(outcome.key);
|
||||
if (outcome.version) await expect(row).toContainText(`v${outcome.version}`);
|
||||
if (outcome.reason) {
|
||||
const reason = row.locator('.openapi-import-plan-reason');
|
||||
await expect(reason).toBeVisible();
|
||||
await expect(reason).toContainText(localized(
|
||||
'This imported item needs review in the wizard.',
|
||||
'Этот импортированный элемент нужно проверить в мастере.',
|
||||
));
|
||||
}
|
||||
}
|
||||
await expect(report.locator('[data-outcome="created"]')).toContainText(localized('Created', 'Создано'));
|
||||
await expect(report.locator('[data-outcome="versioned"]')).toContainText(localized('New Draft Version', 'Новая версия черновика'));
|
||||
await expect(report.locator('[data-outcome="renamed"]')).toContainText(localized('Created with confirmed name', 'Создано с подтверждённым именем'));
|
||||
await expect(report.locator('[data-outcome="skipped"]')).toContainText(localized('Skipped', 'Пропущено'));
|
||||
await expect(report.locator('[data-outcome="rejected"]')).toContainText(localized('Rejected', 'Отклонено'));
|
||||
await expect(report).not.toContainText('raw-report-canary');
|
||||
await expect(report).not.toContainText('https://report-secret.example.test/internal');
|
||||
});
|
||||
}
|
||||
|
||||
test('[P1] AC-13 submits the confirmed new_version decision for the exact existing operation', async ({ page }) => {
|
||||
const prefix = uniqueName('plan_version');
|
||||
const targetOperationId = 'op_exact_importplan_target';
|
||||
|
||||
@@ -188,6 +188,44 @@ test('[P1] AC-1 preview renders source locations and stable finding codes for ca
|
||||
await expect(row.locator('[data-testid="openapi-import-conflict-location"]')).toContainText('/operationId');
|
||||
});
|
||||
|
||||
for (const locale of ['en', 'ru']) {
|
||||
test(`[P0] AC-2.8-02 external reference denial gives safe guidance and stays blocked in ${locale}`, async ({ page }) => {
|
||||
const prefix = uniqueName('preview_external_denied');
|
||||
const sourceCanary = 'private-reference-message-canary';
|
||||
const urlCanary = 'https://private.example.test/internal/openapi.yaml';
|
||||
const operations = [candidate(prefix, 'blocked', {
|
||||
status: 'blocked',
|
||||
findings: [{
|
||||
code: 'external_reference_disabled',
|
||||
severity: 'error',
|
||||
message: sourceCanary + ' ' + urlCanary,
|
||||
operation_key: 'GET /' + prefix + '-blocked',
|
||||
source_location: { pointer: '/paths/~1' + prefix + '-blocked/get/schema/$ref' },
|
||||
}],
|
||||
})];
|
||||
const response = previewResponse(prefix, operations);
|
||||
await routeOperationsEmpty(page);
|
||||
await page.route('**/api/admin/workspaces/*/imports/openapi/preview', async (route) => {
|
||||
await route.fulfill({ contentType: 'application/json', body: JSON.stringify(response) });
|
||||
});
|
||||
await openImportModal(page, locale);
|
||||
await uploadAndPreview(page, prefix, [[prefix + '-blocked', prefix + '_blocked']]);
|
||||
|
||||
const row = page.locator('.openapi-import-operation').first();
|
||||
const selection = row.locator('[data-openapi-operation]');
|
||||
await expect(selection).toBeDisabled();
|
||||
await expect(row.locator('[data-testid="openapi-import-operation-readiness"]')).toHaveAttribute('data-state', 'blocked');
|
||||
await expect(page.locator('[data-testid="openapi-import-blockers"]')).toContainText('external_reference_disabled');
|
||||
await row.locator('.openapi-import-findings summary').click();
|
||||
const finding = row.locator('.openapi-import-finding');
|
||||
await expect(finding).toContainText('external_reference_disabled');
|
||||
await expect(finding).toContainText(locale === 'en' ? /server allowlist/i : /список разрешённых адресов/i);
|
||||
await expect(finding).toContainText(locale === 'en' ? /offline/i : /работы без сети/i);
|
||||
await expect(finding).not.toContainText(sourceCanary);
|
||||
await expect(finding).not.toContainText(urlCanary);
|
||||
});
|
||||
}
|
||||
|
||||
// AC-4: duplicate/invalid/taken names must show a deterministic rename
|
||||
// proposal and conflict type, and the snapshot must stay unchanged.
|
||||
test('[P1] AC-4 preview shows conflict type and deterministic rename proposal without mutating the snapshot', async ({ page }) => {
|
||||
|
||||
@@ -0,0 +1,229 @@
|
||||
const fs = require('node:fs/promises');
|
||||
const path = require('node:path');
|
||||
const { test, expect } = require('@playwright/test');
|
||||
const { getCurrentWorkspace, login, localized, uniqueName } = require('./helpers');
|
||||
|
||||
const controlDirectory = path.resolve(__dirname, '../../../../.tmp/ui-e2e');
|
||||
|
||||
async function dismissOnboardingIfOpen(page) {
|
||||
await page.locator('#crank-onboarding-panel').waitFor({ state: 'attached' });
|
||||
await page.locator('[data-testid="onboarding-dismiss"]').waitFor({ state: 'attached' });
|
||||
await page.evaluate(() => document.querySelector('[data-testid="onboarding-dismiss"]').click());
|
||||
await expect(page.locator('#crank-onboarding-panel')).toBeHidden();
|
||||
}
|
||||
|
||||
function openApiDocument(operationId) {
|
||||
return [
|
||||
'openapi: 3.0.3',
|
||||
`info: { title: '${operationId}', version: '1' }`,
|
||||
'servers: [{ url: https://api.example.test }]',
|
||||
'paths:',
|
||||
` /${operationId}:`,
|
||||
' get:',
|
||||
` operationId: ${operationId}`,
|
||||
' summary: Recovery test operation',
|
||||
' responses:',
|
||||
" '200': { description: OK }",
|
||||
'',
|
||||
].join('\n');
|
||||
}
|
||||
|
||||
async function operationSnapshot(page, workspaceId) {
|
||||
return page.evaluate(async (id) => {
|
||||
const response = await window.CrankApi.listOperations(id);
|
||||
if (!response || !Array.isArray(response.items)) throw new Error('Operations API returned no item list');
|
||||
return response.items;
|
||||
}, workspaceId);
|
||||
}
|
||||
|
||||
async function requestAdminControl(command) {
|
||||
const requestFile = path.join(controlDirectory, 'admin-control.request');
|
||||
const ackFile = path.join(controlDirectory, 'admin-control.ack');
|
||||
await fs.mkdir(controlDirectory, { recursive: true });
|
||||
await fs.rm(ackFile, { force: true });
|
||||
const temporaryFile = `${requestFile}.${process.pid}.${Date.now()}.tmp`;
|
||||
await fs.writeFile(temporaryFile, `${command}\n`, { mode: 0o600 });
|
||||
await fs.rename(temporaryFile, requestFile);
|
||||
await expect.poll(async () => {
|
||||
try {
|
||||
return (await fs.readFile(ackFile, 'utf8')).trim();
|
||||
} catch (_error) {
|
||||
return '';
|
||||
}
|
||||
}, { timeout: 30_000, message: `Admin API did not acknowledge ${command}` }).toBe(command);
|
||||
}
|
||||
|
||||
async function adminApiPid() {
|
||||
return (await fs.readFile(path.join(controlDirectory, 'admin-api.pid'), 'utf8')).trim();
|
||||
}
|
||||
|
||||
async function storageCounts(workspaceId) {
|
||||
await requestAdminControl(`counts ${workspaceId}`);
|
||||
return JSON.parse(await fs.readFile(path.join(controlDirectory, 'storage-counts.json'), 'utf8'));
|
||||
}
|
||||
|
||||
async function openImport(page, operationId) {
|
||||
await page.getByRole('button', { name: localized('Import OpenAPI', 'Импорт OpenAPI') }).click();
|
||||
await page.locator('#openapi-import-file').setInputFiles({
|
||||
name: `${operationId}.yaml`,
|
||||
mimeType: 'application/yaml',
|
||||
buffer: Buffer.from(openApiDocument(operationId)),
|
||||
});
|
||||
const previewResponse = page.waitForResponse((response) =>
|
||||
response.request().method() === 'POST'
|
||||
&& response.url().includes('/imports/openapi/preview'));
|
||||
await page.locator('#openapi-import-preview').click();
|
||||
const response = await previewResponse;
|
||||
expect(response.ok()).toBe(true);
|
||||
await expect(page.locator('#openapi-import-preview-panel')).toBeVisible();
|
||||
return response;
|
||||
}
|
||||
|
||||
test('[P1] AC03 real invalid preview shows the exact canonical correlation headers', async ({ page }) => {
|
||||
await login(page);
|
||||
await dismissOnboardingIfOpen(page);
|
||||
await page.getByRole('button', { name: localized('Import OpenAPI', 'Импорт OpenAPI') }).click();
|
||||
await page.locator('#openapi-import-file').setInputFiles({
|
||||
name: 'invalid-openapi.yaml',
|
||||
mimeType: 'application/yaml',
|
||||
buffer: Buffer.from('openapi: [\n'),
|
||||
});
|
||||
|
||||
const rejectedPreview = page.waitForResponse((response) =>
|
||||
response.request().method() === 'POST'
|
||||
&& response.url().includes('/imports/openapi/preview'));
|
||||
await page.locator('#openapi-import-preview').click();
|
||||
const response = await rejectedPreview;
|
||||
expect(response.status()).toBeGreaterThanOrEqual(400);
|
||||
expect(response.status()).toBeLessThan(500);
|
||||
|
||||
const requestId = response.headers()['x-request-id'];
|
||||
const traceId = response.headers()['x-trace-id'];
|
||||
expect(requestId).toBeTruthy();
|
||||
expect(traceId).toMatch(/^[a-f0-9]{32}$/i);
|
||||
const status = page.locator('#openapi-import-status');
|
||||
await expect(status).toBeFocused();
|
||||
await expect(status).toHaveAttribute('role', 'status');
|
||||
await expect(status).toHaveAttribute('aria-live', 'polite');
|
||||
await expect(status).toHaveAttribute('aria-atomic', 'true');
|
||||
await expect(status).toContainText(requestId);
|
||||
await expect(status).toContainText(traceId);
|
||||
await expect(status).toContainText('Request ID:');
|
||||
await expect(status).toContainText('Trace ID:');
|
||||
});
|
||||
|
||||
test('[P1] AC05 real preview and plan do not mutate Operations; reduced-motion Apply replay survives reload and API restart', async ({ page }) => {
|
||||
// This journey includes bounded process stop/start and several database observations.
|
||||
test.setTimeout(120_000);
|
||||
await page.emulateMedia({ reducedMotion: 'reduce' });
|
||||
await login(page);
|
||||
await dismissOnboardingIfOpen(page);
|
||||
expect(await page.evaluate(() => window.matchMedia('(prefers-reduced-motion: reduce)').matches)).toBe(true);
|
||||
|
||||
const workspace = await getCurrentWorkspace(page);
|
||||
expect(workspace && workspace.id).toBeTruthy();
|
||||
const initialOperations = await operationSnapshot(page, workspace.id);
|
||||
const initialCounts = await storageCounts(workspace.id);
|
||||
expect(initialCounts).toMatchObject({
|
||||
sources: expect.any(Number),
|
||||
operations: expect.any(Number),
|
||||
versions: expect.any(Number),
|
||||
});
|
||||
const operationId = uniqueName('recovery_import').replace(/[^a-zA-Z0-9_]/g, '_');
|
||||
const applyRequests = [];
|
||||
const apiRequests = { preview: 0, createPlan: 0, apply: 0 };
|
||||
page.on('request', (request) => {
|
||||
if (request.method() !== 'POST') return;
|
||||
const url = request.url();
|
||||
if (url.includes('/imports/openapi/preview')) apiRequests.preview += 1;
|
||||
if (/\/imports\/openapi\/[^/]+\/plans(?:\?|$)/.test(url)) apiRequests.createPlan += 1;
|
||||
if (/\/imports\/openapi\/[^/]+\/apply(?:\?|$)/.test(url)) apiRequests.apply += 1;
|
||||
});
|
||||
await page.route('**/imports/openapi/*/apply', async (route) => {
|
||||
applyRequests.push({
|
||||
url: route.request().url(),
|
||||
body: route.request().postData(),
|
||||
});
|
||||
if (applyRequests.length === 1) {
|
||||
// Send the real request to Admin API, then lose only its browser response.
|
||||
const response = await route.fetch();
|
||||
expect(response.ok()).toBe(true);
|
||||
await route.abort('connectionreset');
|
||||
return;
|
||||
}
|
||||
await route.continue();
|
||||
});
|
||||
|
||||
await openImport(page, operationId);
|
||||
expect(await operationSnapshot(page, workspace.id)).toEqual(initialOperations);
|
||||
expect(apiRequests.preview).toBe(1);
|
||||
const afterPreviewCounts = await storageCounts(workspace.id);
|
||||
expect(afterPreviewCounts).toEqual({
|
||||
...initialCounts,
|
||||
sources: initialCounts.sources + 1,
|
||||
});
|
||||
const dialogMotion = await page.locator('.openapi-import-dialog').evaluate((node) => getComputedStyle(node).transitionDuration);
|
||||
expect(dialogMotion.split(',').every((duration) => Number.parseFloat(duration) === 0)).toBe(true);
|
||||
await page.locator('#openapi-import-server-custom').fill('https://api.example.test');
|
||||
await page.locator('#openapi-import-create').click();
|
||||
await expect(page.locator('[data-testid="openapi-import-apply-confirmation"]')).toBeVisible();
|
||||
expect(await operationSnapshot(page, workspace.id)).toEqual(initialOperations);
|
||||
expect(apiRequests.createPlan).toBe(1);
|
||||
expect(await storageCounts(workspace.id)).toEqual(afterPreviewCounts);
|
||||
|
||||
await page.locator('#openapi-import-plan-apply').click();
|
||||
await expect.poll(() => applyRequests.length).toBe(1);
|
||||
expect(apiRequests.apply).toBe(1);
|
||||
const applyStatus = page.locator('#openapi-import-status');
|
||||
await expect(applyStatus).toBeFocused();
|
||||
await expect(applyStatus).toHaveAttribute('role', 'status');
|
||||
await expect(applyStatus).toHaveAttribute('aria-live', 'polite');
|
||||
await expect(applyStatus).toHaveAttribute('aria-atomic', 'true');
|
||||
await expect(applyStatus).toContainText(localized('Apply result is unknown', 'Результат применения неизвестен'));
|
||||
await expect(page.locator('#openapi-import-retry')).toBeVisible();
|
||||
const committedOperations = await operationSnapshot(page, workspace.id);
|
||||
expect(committedOperations).toHaveLength(initialOperations.length + 1);
|
||||
expect(committedOperations.some((operation) => operation.name === operationId
|
||||
|| operation.operation_id === operationId)).toBe(true);
|
||||
const committedCounts = await storageCounts(workspace.id);
|
||||
expect(committedCounts).toEqual({
|
||||
...afterPreviewCounts,
|
||||
operations: afterPreviewCounts.operations + 1,
|
||||
versions: afterPreviewCounts.versions + 1,
|
||||
});
|
||||
|
||||
const pidBeforeRestart = await adminApiPid();
|
||||
let adminMayBeStopped = true;
|
||||
try {
|
||||
await requestAdminControl('stop');
|
||||
await requestAdminControl('start');
|
||||
adminMayBeStopped = false;
|
||||
} finally {
|
||||
if (adminMayBeStopped) await requestAdminControl('start');
|
||||
}
|
||||
expect(await adminApiPid()).not.toBe(pidBeforeRestart);
|
||||
expect(await storageCounts(workspace.id)).toEqual(committedCounts);
|
||||
await page.reload();
|
||||
await page.getByRole('button', { name: localized('Import OpenAPI', 'Импорт OpenAPI') }).click();
|
||||
await expect(page.locator('#openapi-import-retry')).toBeVisible();
|
||||
await expect(page.locator('#openapi-import-status')).toContainText(localized('Apply result is unknown', 'Результат применения неизвестен'));
|
||||
|
||||
const retryResponse = page.waitForResponse((response) =>
|
||||
response.request().method() === 'POST'
|
||||
&& response.url().includes('/imports/openapi/')
|
||||
&& response.url().endsWith('/apply'));
|
||||
await page.locator('#openapi-import-retry').click();
|
||||
const response = await retryResponse;
|
||||
expect(response.status()).toBe(200);
|
||||
await expect(page.locator('#openapi-import-result')).toContainText(operationId);
|
||||
await expect.poll(() => applyRequests.length).toBe(2);
|
||||
expect(applyRequests[1].url).toBe(applyRequests[0].url);
|
||||
expect(applyRequests[1].body).toBe(applyRequests[0].body);
|
||||
expect(apiRequests).toEqual({ preview: 1, createPlan: 1, apply: 2 });
|
||||
|
||||
const finalOperations = await operationSnapshot(page, workspace.id);
|
||||
expect(finalOperations).toHaveLength(initialOperations.length + 1);
|
||||
expect(finalOperations.filter((operation) => operation.name === operationId
|
||||
|| operation.operation_id === operationId)).toHaveLength(1);
|
||||
expect(await storageCounts(workspace.id)).toEqual(committedCounts);
|
||||
});
|
||||
@@ -367,6 +367,8 @@ impl ExternalReferenceFetcher {
|
||||
fn outbound_client(policy: &OutboundHttpPolicy) -> Result<Client, Arc<str>> {
|
||||
let resolver = Arc::new(PolicyDnsResolver {
|
||||
policy: policy.clone(),
|
||||
#[cfg(test)]
|
||||
lookup: None,
|
||||
});
|
||||
Client::builder()
|
||||
.redirect(redirect::Policy::none())
|
||||
@@ -611,23 +613,43 @@ impl OutboundHttpPolicy {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
#[derive(Clone)]
|
||||
struct PolicyDnsResolver {
|
||||
policy: OutboundHttpPolicy,
|
||||
// Test-local DNS answers exercise the actual policy resolver and connector.
|
||||
// Neither this field nor an injection API exists in production builds.
|
||||
#[cfg(test)]
|
||||
lookup: Option<Arc<dyn Resolve>>,
|
||||
}
|
||||
|
||||
impl PolicyDnsResolver {
|
||||
fn lookup(&self, name: Name) -> Resolving {
|
||||
#[cfg(test)]
|
||||
if let Some(lookup) = &self.lookup {
|
||||
return lookup.resolve(name);
|
||||
}
|
||||
let host = normalize_host(name.as_str());
|
||||
Box::pin(async move {
|
||||
let addresses = tokio::net::lookup_host((host.as_str(), 0))
|
||||
.await
|
||||
.map_err(|error| Box::new(error) as Box<dyn std::error::Error + Send + Sync>)?;
|
||||
let addresses: Vec<_> = addresses.collect();
|
||||
Ok(Box::new(addresses.into_iter()) as Addrs)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Resolve for PolicyDnsResolver {
|
||||
fn resolve(&self, name: Name) -> Resolving {
|
||||
let host = normalize_host(name.as_str());
|
||||
let policy = self.policy.clone();
|
||||
let resolver = self.clone();
|
||||
Box::pin(async move {
|
||||
policy
|
||||
.validate_host(&host)
|
||||
.map_err(|error| boxed_io_error(error.to_string()))?;
|
||||
let explicitly_allowed = policy.is_explicitly_allowed(&host);
|
||||
let resolved = tokio::net::lookup_host((host.as_str(), 0))
|
||||
.await
|
||||
.map_err(|error| Box::new(error) as Box<dyn std::error::Error + Send + Sync>)?;
|
||||
let resolved = resolver.lookup(name).await?;
|
||||
let addresses = resolved
|
||||
.filter(|address| {
|
||||
!policy.is_denied_ip(address.ip())
|
||||
@@ -969,3 +991,7 @@ fn to_reqwest_method(method: HttpMethod) -> reqwest::Method {
|
||||
HttpMethod::Options => reqwest::Method::OPTIONS,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "client_dns_tests.rs"]
|
||||
mod dns_tests;
|
||||
@@ -0,0 +1,155 @@
|
||||
//! Use the production policy resolver in reqwest's real connector. DNS can
|
||||
//! change immediately after lookup; only the validated returned addresses may
|
||||
//! be connected to, and a subsequent resolution must enforce policy again.
|
||||
|
||||
use super::*;
|
||||
use axum::{Router, routing::get};
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
|
||||
struct ChangingLookup {
|
||||
calls: AtomicUsize,
|
||||
answers: Vec<Vec<SocketAddr>>,
|
||||
}
|
||||
impl Resolve for ChangingLookup {
|
||||
fn resolve(&self, _: Name) -> Resolving {
|
||||
// Advancing before the connector receives this iterator models a DNS
|
||||
// answer changing between validation and the socket connection.
|
||||
let call = self.calls.fetch_add(1, Ordering::SeqCst);
|
||||
let addresses = self.answers[call.min(self.answers.len() - 1)].clone();
|
||||
Box::pin(async move { Ok(Box::new(addresses.into_iter()) as Addrs) })
|
||||
}
|
||||
}
|
||||
|
||||
struct Listeners(Vec<tokio::task::JoinHandle<()>>);
|
||||
impl Drop for Listeners {
|
||||
fn drop(&mut self) {
|
||||
for handle in &self.0 {
|
||||
handle.abort();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn fixture() -> (u16, Arc<AtomicUsize>, Arc<AtomicUsize>, Listeners) {
|
||||
let allowed = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let port = allowed.local_addr().unwrap().port();
|
||||
let denied = tokio::net::TcpListener::bind((Ipv4Addr::new(127, 0, 0, 2), port))
|
||||
.await
|
||||
.unwrap();
|
||||
let allowed_count = Arc::new(AtomicUsize::new(0));
|
||||
let denied_count = Arc::new(AtomicUsize::new(0));
|
||||
let mut handles = Vec::new();
|
||||
for (listener, count) in [
|
||||
(allowed, Arc::clone(&allowed_count)),
|
||||
(denied, Arc::clone(&denied_count)),
|
||||
] {
|
||||
handles.push(tokio::spawn(async move {
|
||||
let app = Router::new().route(
|
||||
"/document",
|
||||
get(move || {
|
||||
let count = Arc::clone(&count);
|
||||
async move {
|
||||
count.fetch_add(1, Ordering::SeqCst);
|
||||
"snapshot"
|
||||
}
|
||||
}),
|
||||
);
|
||||
axum::serve(listener, app).await.unwrap();
|
||||
}));
|
||||
}
|
||||
(port, allowed_count, denied_count, Listeners(handles))
|
||||
}
|
||||
|
||||
fn policy_client(lookup: Arc<ChangingLookup>) -> Client {
|
||||
let policy = OutboundHttpPolicy::try_new(
|
||||
vec!["reference.invalid".into()],
|
||||
vec!["127.0.0.2".into()],
|
||||
1024,
|
||||
)
|
||||
.unwrap();
|
||||
Client::builder()
|
||||
.no_proxy()
|
||||
.redirect(redirect::Policy::none())
|
||||
.pool_max_idle_per_host(0)
|
||||
.timeout(Duration::from_secs(3))
|
||||
.dns_resolver(Arc::new(PolicyDnsResolver {
|
||||
policy,
|
||||
lookup: Some(lookup),
|
||||
}))
|
||||
.build()
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn dns_rebinding_pins_the_validated_connection_then_rejects_changed_denied_answer() {
|
||||
let (port, allowed, denied, _listeners) = fixture().await;
|
||||
let lookup = Arc::new(ChangingLookup {
|
||||
calls: AtomicUsize::new(0),
|
||||
answers: vec![
|
||||
vec!["127.0.0.1:0".parse().unwrap()],
|
||||
vec!["127.0.0.2:0".parse().unwrap()],
|
||||
],
|
||||
});
|
||||
let client = policy_client(Arc::clone(&lookup));
|
||||
let url = format!("http://reference.invalid:{port}/document");
|
||||
let body = client
|
||||
.get(&url)
|
||||
.send()
|
||||
.await
|
||||
.unwrap()
|
||||
.error_for_status()
|
||||
.unwrap()
|
||||
.text()
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(body, "snapshot");
|
||||
assert_eq!(
|
||||
lookup.calls.load(Ordering::SeqCst),
|
||||
1,
|
||||
"one validated answer is pinned for this connection"
|
||||
);
|
||||
assert_eq!(allowed.load(Ordering::SeqCst), 1);
|
||||
assert_eq!(denied.load(Ordering::SeqCst), 0);
|
||||
|
||||
// Disable idle pooling above so a new connection really asks for the new
|
||||
// answer; no timing or external DNS configuration is involved.
|
||||
let error = client.get(&url).send().await.unwrap_err();
|
||||
assert!(error.is_connect());
|
||||
assert!(
|
||||
!error.is_timeout(),
|
||||
"must reject the new DNS answer, not merely time out"
|
||||
);
|
||||
assert_eq!(lookup.calls.load(Ordering::SeqCst), 2);
|
||||
assert_eq!(allowed.load(Ordering::SeqCst), 1);
|
||||
assert_eq!(
|
||||
denied.load(Ordering::SeqCst),
|
||||
0,
|
||||
"deny must win over hostname allowlist"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn mixed_dns_answers_never_pass_denied_address_to_the_connector() {
|
||||
let (port, allowed, denied, _listeners) = fixture().await;
|
||||
let lookup = Arc::new(ChangingLookup {
|
||||
calls: AtomicUsize::new(0),
|
||||
answers: vec![vec![
|
||||
"127.0.0.2:0".parse().unwrap(),
|
||||
"127.0.0.1:0".parse().unwrap(),
|
||||
]],
|
||||
});
|
||||
let client = policy_client(Arc::clone(&lookup));
|
||||
assert_eq!(
|
||||
client
|
||||
.get(format!("http://reference.invalid:{port}/document"))
|
||||
.send()
|
||||
.await
|
||||
.unwrap()
|
||||
.text()
|
||||
.await
|
||||
.unwrap(),
|
||||
"snapshot"
|
||||
);
|
||||
assert_eq!(lookup.calls.load(Ordering::SeqCst), 1);
|
||||
assert_eq!(allowed.load(Ordering::SeqCst), 1);
|
||||
assert_eq!(denied.load(Ordering::SeqCst), 0);
|
||||
}
|
||||
@@ -30,6 +30,18 @@ pub enum FaultAction {
|
||||
Hold,
|
||||
}
|
||||
|
||||
/// Terminates a debug test process at an explicitly armed transaction boundary.
|
||||
/// Release builds contain neither this hook nor its callers. The checkpoint
|
||||
/// is in-memory only; a request or environment variable cannot arm it.
|
||||
#[cfg(debug_assertions)]
|
||||
pub fn crash_if_checkpointed(stage: &str) {
|
||||
if action(stage) == Some(FaultAction::Exit) {
|
||||
// Deliberately skip destructors/atexit, as a lost process would. Test
|
||||
// containers belong to the parent process, not this child.
|
||||
unsafe { libc::_exit(86) }
|
||||
}
|
||||
}
|
||||
|
||||
struct State {
|
||||
checkpoint: Option<(String, FaultAction, usize)>,
|
||||
hits: BTreeMap<String, usize>,
|
||||
|
||||
@@ -643,6 +643,47 @@ fn corrupted_or_symlinked_final_never_returns_bytes() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn truncated_published_blob_is_integrity_failure_with_safe_diagnostics() {
|
||||
let root = TestRoot::new("truncated-final");
|
||||
let store = ArtifactStore::open(&root.0).unwrap();
|
||||
let source = b"published blob truncated tail".as_slice();
|
||||
let stored = store.put(source).unwrap();
|
||||
let path = root
|
||||
.0
|
||||
.join("sha256")
|
||||
.join(&stored.artifact_ref.digest_hex()[..2])
|
||||
.join(stored.artifact_ref.digest_hex());
|
||||
assert_eq!(
|
||||
fs::metadata(&path).unwrap().permissions().mode() & 0o777,
|
||||
0o400
|
||||
);
|
||||
|
||||
// Publishing leaves the blob read-only. Reopen it writable only long enough
|
||||
// to truncate the tail, then restore the exact private mode the verified
|
||||
// reader validates before it compares length and digest.
|
||||
fs::set_permissions(&path, fs::Permissions::from_mode(0o600)).unwrap();
|
||||
fs::File::options()
|
||||
.write(true)
|
||||
.open(&path)
|
||||
.unwrap()
|
||||
.set_len(u64::try_from(source.len() - 4).unwrap())
|
||||
.unwrap();
|
||||
fs::set_permissions(&path, fs::Permissions::from_mode(0o400)).unwrap();
|
||||
assert_eq!(
|
||||
fs::metadata(&path).unwrap().permissions().mode() & 0o777,
|
||||
0o400
|
||||
);
|
||||
|
||||
let error = store.read(&stored.artifact_ref).unwrap_err();
|
||||
assert_eq!(error, ArtifactError::Integrity);
|
||||
assert_eq!(error.to_string(), "artifact integrity verification failed");
|
||||
let diagnostic = format!("{error} {error:?}");
|
||||
assert!(!diagnostic.contains(stored.artifact_ref.digest_hex()));
|
||||
assert!(!diagnostic.contains(root.0.to_string_lossy().as_ref()));
|
||||
assert!(!diagnostic.contains("published"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn directory_at_final_digest_is_integrity_failure() {
|
||||
let root = TestRoot::new("directory-final");
|
||||
|
||||
@@ -355,16 +355,19 @@ fn real_process_write_limit_leaves_no_partial_final_blob() {
|
||||
}
|
||||
|
||||
let root = TestRoot::new("real-write-limit");
|
||||
let status = Command::new(std::env::current_exe().unwrap())
|
||||
// Capture the child's stdout/stderr with pipes: the child lowers
|
||||
// RLIMIT_FSIZE to 1 byte, which would otherwise make libtest's own
|
||||
// result write fail with EFBIG against an inherited regular log file.
|
||||
let output = Command::new(std::env::current_exe().unwrap())
|
||||
.args([
|
||||
"--exact",
|
||||
"real_process_write_limit_leaves_no_partial_final_blob",
|
||||
"--nocapture",
|
||||
])
|
||||
.env("CRANK_ARTIFACTS_CHILD_ROOT", &root.0)
|
||||
.status()
|
||||
.output()
|
||||
.unwrap();
|
||||
assert!(status.success());
|
||||
assert!(output.status.success(), "child status: {}", output.status);
|
||||
|
||||
let store = ArtifactStore::open(&root.0).unwrap();
|
||||
let expected =
|
||||
|
||||
@@ -140,6 +140,51 @@ fn swagger2_projects_basic_and_api_key_header_and_query() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn swagger2_cookie_api_key_is_explicitly_unsupported_at_operation_and_preview() {
|
||||
let document = json!({
|
||||
"swagger": "2.0",
|
||||
"info": { "title": "Unsupported Swagger cookie", "version": "1" },
|
||||
"host": "security.example.test",
|
||||
"schemes": ["https"],
|
||||
"securityDefinitions": {
|
||||
"CookieKey": { "type": "apiKey", "in": "cookie", "name": "session" }
|
||||
},
|
||||
"security": [{ "CookieKey": [] }],
|
||||
"paths": { "/private": { "get": {
|
||||
"responses": { "200": { "description": "ok" } }
|
||||
}}}
|
||||
});
|
||||
let ir = normalize(&document);
|
||||
let operation = operation(&ir, "/private");
|
||||
assert!(matches!(
|
||||
ir.security_schemes["CookieKey"].kind,
|
||||
NormalizedSecuritySchemeKind::Unsupported { .. }
|
||||
));
|
||||
assert!(!operation.security_requirements.alternatives[0].supported);
|
||||
assert!(
|
||||
operation
|
||||
.findings
|
||||
.iter()
|
||||
.any(|finding| finding.code == "unsupported_security_scheme")
|
||||
);
|
||||
assert!(
|
||||
operation
|
||||
.findings
|
||||
.iter()
|
||||
.any(|finding| finding.code == "unsupported_security_requirements")
|
||||
);
|
||||
let preview = candidates(&ir);
|
||||
let candidate = candidate(&preview, "GET /private");
|
||||
assert!(
|
||||
candidate
|
||||
.findings
|
||||
.iter()
|
||||
.any(|finding| finding.code == "unsupported_security_requirements")
|
||||
);
|
||||
assert!(candidate.draft.input_mapping.rules.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn effective_requirements_inherit_override_and_allow_explicit_no_auth() {
|
||||
let document = json!({
|
||||
|
||||
@@ -354,7 +354,11 @@ impl PostgresRegistry {
|
||||
let outcome = apply_import_plan_transaction(&mut transaction, request).await;
|
||||
match outcome {
|
||||
Ok((replayed, report)) => {
|
||||
#[cfg(debug_assertions)]
|
||||
crank_artifacts::test_support::crash_if_checkpointed("apply_before_commit");
|
||||
transaction.commit().await?;
|
||||
#[cfg(debug_assertions)]
|
||||
crank_artifacts::test_support::crash_if_checkpointed("apply_after_commit");
|
||||
Ok(ApplyImportPlanResult { replayed, report })
|
||||
}
|
||||
Err(ApplyTxFailure::KeyConflict) => {
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
use super::*;
|
||||
use sqlx::Row;
|
||||
use std::collections::HashMap;
|
||||
|
||||
mod exhaustive_v12;
|
||||
|
||||
#[tokio::test]
|
||||
async fn healthy_v11_upgrades_to_v13_without_rewriting_prior_ledger() {
|
||||
@@ -216,3 +220,523 @@ async fn v13_guard_rejects_wrong_cleanup_index_order_and_predicate() {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const V12_BLOB_COLUMNS: &[&str] = &[
|
||||
"digest",
|
||||
"artifact_ref",
|
||||
"size_bytes",
|
||||
"storage_lifecycle",
|
||||
"claim_token",
|
||||
"claim_expires_at",
|
||||
"created_at",
|
||||
"updated_at",
|
||||
];
|
||||
const V12_SOURCE_COLUMNS: &[&str] = &[
|
||||
"workspace_id",
|
||||
"source_id",
|
||||
"blob_digest",
|
||||
"mime_type",
|
||||
"sensitivity",
|
||||
"lifecycle",
|
||||
"created_at",
|
||||
"updated_at",
|
||||
"detached_at",
|
||||
];
|
||||
const V12_BLOB_DEFAULTS: &[(&str, Option<&str>)] = &[
|
||||
("digest", None),
|
||||
("artifact_ref", None),
|
||||
("size_bytes", None),
|
||||
("storage_lifecycle", Some("'available'::text")),
|
||||
("claim_token", None),
|
||||
("claim_expires_at", None),
|
||||
("created_at", None),
|
||||
("updated_at", None),
|
||||
];
|
||||
const V12_SOURCE_DEFAULTS: &[(&str, Option<&str>)] = &[
|
||||
("workspace_id", None),
|
||||
("source_id", None),
|
||||
("blob_digest", None),
|
||||
("mime_type", None),
|
||||
("sensitivity", None),
|
||||
("lifecycle", Some("'active'::text")),
|
||||
("created_at", None),
|
||||
("updated_at", None),
|
||||
("detached_at", None),
|
||||
];
|
||||
// PostgreSQL normalizes `set default null` into "no default", so each column
|
||||
// that legitimately has no default needs a concrete typed expression to prove
|
||||
// the guard rejects a newly introduced default.
|
||||
const V12_BLOB_NO_DEFAULT_DRIFT: &[(&str, &str)] = &[
|
||||
("digest", "'crank_drift'::text"),
|
||||
("artifact_ref", "'crank_drift'::text"),
|
||||
("size_bytes", "1"),
|
||||
("claim_token", "'crank_drift'::text"),
|
||||
("claim_expires_at", "'2000-01-01 00:00:00+00'::timestamptz"),
|
||||
("created_at", "'2000-01-01 00:00:00+00'::timestamptz"),
|
||||
("updated_at", "'2000-01-01 00:00:00+00'::timestamptz"),
|
||||
];
|
||||
const V12_SOURCE_NO_DEFAULT_DRIFT: &[(&str, &str)] = &[
|
||||
("workspace_id", "'crank_drift'::text"),
|
||||
("source_id", "'crank_drift'::text"),
|
||||
("blob_digest", "'crank_drift'::text"),
|
||||
("mime_type", "'crank_drift'::text"),
|
||||
("sensitivity", "'crank_drift'::text"),
|
||||
("created_at", "'2000-01-01 00:00:00+00'::timestamptz"),
|
||||
("updated_at", "'2000-01-01 00:00:00+00'::timestamptz"),
|
||||
("detached_at", "'2000-01-01 00:00:00+00'::timestamptz"),
|
||||
];
|
||||
const V12_BLOB_CONSTRAINTS: &[&str] = &[
|
||||
"artifact_blobs_artifact_ref_key",
|
||||
"artifact_blobs_claim_shape_check",
|
||||
"artifact_blobs_claim_token_check",
|
||||
"artifact_blobs_digest_check",
|
||||
"artifact_blobs_lifecycle_check",
|
||||
"artifact_blobs_pkey",
|
||||
"artifact_blobs_ref_check",
|
||||
"artifact_blobs_size_check",
|
||||
"artifact_blobs_timestamps_check",
|
||||
];
|
||||
const V12_SOURCE_CONSTRAINTS: &[&str] = &[
|
||||
"artifact_sources_blob_digest_fkey",
|
||||
"artifact_sources_id_check",
|
||||
"artifact_sources_lifecycle_check",
|
||||
"artifact_sources_mime_type_check",
|
||||
"artifact_sources_pkey",
|
||||
"artifact_sources_sensitivity_check",
|
||||
"artifact_sources_state_check",
|
||||
"artifact_sources_timestamps_check",
|
||||
"artifact_sources_workspace_id_fkey",
|
||||
];
|
||||
const V12_OWNED_RELATIONS: &[(&str, &str)] = &[
|
||||
("artifact_blobs", "r"),
|
||||
("artifact_blobs_artifact_ref_key", "i"),
|
||||
("artifact_blobs_expired_claim_idx", "i"),
|
||||
("artifact_blobs_pkey", "i"),
|
||||
("artifact_sources", "r"),
|
||||
("artifact_sources_blob_lifecycle_detached_idx", "i"),
|
||||
("artifact_sources_openapi_dangling_idx", "i"),
|
||||
("artifact_sources_pkey", "i"),
|
||||
("artifact_sources_workspace_created_idx", "i"),
|
||||
];
|
||||
|
||||
fn owned_names(values: &[&str]) -> Vec<String> {
|
||||
values.iter().map(|value| (*value).to_owned()).collect()
|
||||
}
|
||||
|
||||
fn owned_defaults(values: &[(&str, Option<&str>)]) -> Vec<(String, Option<String>)> {
|
||||
values
|
||||
.iter()
|
||||
.map(|(name, default)| ((*name).to_owned(), default.map(str::to_owned)))
|
||||
.collect()
|
||||
}
|
||||
|
||||
async fn catalog_columns(pool: &sqlx::PgPool, table: &str) -> Vec<String> {
|
||||
sqlx::query(
|
||||
"select column_name
|
||||
from information_schema.columns
|
||||
where table_schema = current_schema() and table_name = $1
|
||||
order by ordinal_position",
|
||||
)
|
||||
.bind(table)
|
||||
.fetch_all(pool)
|
||||
.await
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.map(|row| row.get::<String, _>("column_name"))
|
||||
.collect()
|
||||
}
|
||||
|
||||
async fn catalog_column_defaults(
|
||||
pool: &sqlx::PgPool,
|
||||
table: &str,
|
||||
) -> Vec<(String, Option<String>)> {
|
||||
sqlx::query(
|
||||
"select column_name, column_default
|
||||
from information_schema.columns
|
||||
where table_schema = current_schema() and table_name = $1
|
||||
order by ordinal_position",
|
||||
)
|
||||
.bind(table)
|
||||
.fetch_all(pool)
|
||||
.await
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.map(|row| {
|
||||
(
|
||||
row.get::<String, _>("column_name"),
|
||||
row.get::<Option<String>, _>("column_default"),
|
||||
)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
async fn catalog_constraints(pool: &sqlx::PgPool, table: &str) -> Vec<String> {
|
||||
sqlx::query(
|
||||
"select c.conname
|
||||
from pg_catalog.pg_constraint c
|
||||
join pg_catalog.pg_class t on t.oid = c.conrelid
|
||||
join pg_catalog.pg_namespace n on n.oid = t.relnamespace
|
||||
where n.nspname = current_schema() and t.relname = $1
|
||||
order by c.conname",
|
||||
)
|
||||
.bind(table)
|
||||
.fetch_all(pool)
|
||||
.await
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.map(|row| row.get::<String, _>("conname"))
|
||||
.collect()
|
||||
}
|
||||
|
||||
async fn catalog_indexes(pool: &sqlx::PgPool) -> Vec<String> {
|
||||
sqlx::query(
|
||||
"select idx.relname
|
||||
from pg_catalog.pg_index i
|
||||
join pg_catalog.pg_class idx on idx.oid = i.indexrelid
|
||||
join pg_catalog.pg_class t on t.oid = i.indrelid
|
||||
join pg_catalog.pg_namespace n on n.oid = t.relnamespace
|
||||
where n.nspname = current_schema()
|
||||
and t.relname in ('artifact_blobs', 'artifact_sources')
|
||||
order by idx.relname",
|
||||
)
|
||||
.fetch_all(pool)
|
||||
.await
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.map(|row| row.get::<String, _>("relname"))
|
||||
.collect()
|
||||
}
|
||||
|
||||
async fn catalog_owned_relations(pool: &sqlx::PgPool) -> Vec<(String, String)> {
|
||||
sqlx::query(
|
||||
r"select c.relname, c.relkind::text as relkind
|
||||
from pg_catalog.pg_class c
|
||||
join pg_catalog.pg_namespace n on n.oid = c.relnamespace
|
||||
where n.nspname = current_schema()
|
||||
and c.relname like 'artifact\_%' escape '\'
|
||||
order by c.relname",
|
||||
)
|
||||
.fetch_all(pool)
|
||||
.await
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.map(|row| {
|
||||
(
|
||||
row.get::<String, _>("relname"),
|
||||
row.get::<String, _>("relkind"),
|
||||
)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
async fn catalog_constraint_definitions(pool: &sqlx::PgPool) -> HashMap<String, String> {
|
||||
sqlx::query(
|
||||
"select c.conname, pg_get_constraintdef(c.oid, true) as definition
|
||||
from pg_catalog.pg_constraint c
|
||||
join pg_catalog.pg_class t on t.oid = c.conrelid
|
||||
join pg_catalog.pg_namespace n on n.oid = t.relnamespace
|
||||
where n.nspname = current_schema()
|
||||
and t.relname in ('artifact_blobs', 'artifact_sources')",
|
||||
)
|
||||
.fetch_all(pool)
|
||||
.await
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.map(|row| {
|
||||
(
|
||||
row.get::<String, _>("conname"),
|
||||
row.get::<String, _>("definition"),
|
||||
)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
async fn assert_v12_drift(pool: &sqlx::PgPool, drift: &str, restore: &str) {
|
||||
sqlx::raw_sql(sqlx::AssertSqlSafe(drift.to_owned()))
|
||||
.execute(pool)
|
||||
.await
|
||||
.unwrap();
|
||||
let error = MigrationAuthority::preflight(pool).await.unwrap_err();
|
||||
assert_eq!(error.code(), "partial_sequence", "drift: {drift}");
|
||||
assert_eq!(error.version(), Some(12), "drift: {drift}");
|
||||
sqlx::raw_sql(sqlx::AssertSqlSafe(restore.to_owned()))
|
||||
.execute(pool)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
MigrationAuthority::preflight(pool).await.unwrap(),
|
||||
MigrationPreflight::Current { version: 15 },
|
||||
"restore: {restore}"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn v12_guard_rejects_exhaustive_catalog_drift_for_both_tables() {
|
||||
let database_url = crank_test_support::postgres_schema_url("test_v12_exhaustive_drift").await;
|
||||
let pool = sqlx::PgPool::connect(&database_url).await.unwrap();
|
||||
MigrationAuthority::apply(&pool).await.unwrap();
|
||||
|
||||
// Fixed catalog snapshots. These are hard assertions over the complete
|
||||
// matrix, so a disappeared object fails the test instead of silently
|
||||
// dropping out of the drift loop below.
|
||||
assert_eq!(
|
||||
catalog_columns(&pool, "artifact_blobs").await,
|
||||
owned_names(V12_BLOB_COLUMNS)
|
||||
);
|
||||
assert_eq!(
|
||||
catalog_columns(&pool, "artifact_sources").await,
|
||||
owned_names(V12_SOURCE_COLUMNS)
|
||||
);
|
||||
assert_eq!(
|
||||
V12_BLOB_COLUMNS.len() + V12_SOURCE_COLUMNS.len(),
|
||||
17,
|
||||
"the V12 column matrix is exactly seventeen columns"
|
||||
);
|
||||
assert_eq!(
|
||||
catalog_column_defaults(&pool, "artifact_blobs").await,
|
||||
owned_defaults(V12_BLOB_DEFAULTS)
|
||||
);
|
||||
assert_eq!(
|
||||
catalog_column_defaults(&pool, "artifact_sources").await,
|
||||
owned_defaults(V12_SOURCE_DEFAULTS)
|
||||
);
|
||||
assert_eq!(
|
||||
V12_BLOB_DEFAULTS.len() + V12_SOURCE_DEFAULTS.len(),
|
||||
17,
|
||||
"the V12 default matrix is exactly seventeen columns"
|
||||
);
|
||||
assert_eq!(
|
||||
V12_BLOB_DEFAULTS
|
||||
.iter()
|
||||
.chain(V12_SOURCE_DEFAULTS)
|
||||
.filter(|(_, default)| default.is_none())
|
||||
.count(),
|
||||
15,
|
||||
"fifteen V12 columns must declare no default"
|
||||
);
|
||||
assert_eq!(
|
||||
catalog_constraints(&pool, "artifact_blobs").await,
|
||||
owned_names(V12_BLOB_CONSTRAINTS)
|
||||
);
|
||||
assert_eq!(
|
||||
catalog_constraints(&pool, "artifact_sources").await,
|
||||
owned_names(V12_SOURCE_CONSTRAINTS)
|
||||
);
|
||||
assert_eq!(
|
||||
V12_BLOB_CONSTRAINTS.len() + V12_SOURCE_CONSTRAINTS.len(),
|
||||
18,
|
||||
"the V12 constraint matrix is exactly eighteen constraints"
|
||||
);
|
||||
assert_eq!(
|
||||
catalog_indexes(&pool).await,
|
||||
owned_names(&[
|
||||
"artifact_blobs_artifact_ref_key",
|
||||
"artifact_blobs_expired_claim_idx",
|
||||
"artifact_blobs_pkey",
|
||||
"artifact_sources_blob_lifecycle_detached_idx",
|
||||
"artifact_sources_openapi_dangling_idx",
|
||||
"artifact_sources_pkey",
|
||||
"artifact_sources_workspace_created_idx",
|
||||
])
|
||||
);
|
||||
assert_eq!(
|
||||
catalog_owned_relations(&pool).await,
|
||||
V12_OWNED_RELATIONS
|
||||
.iter()
|
||||
.map(|(name, kind)| ((*name).to_owned(), (*kind).to_owned()))
|
||||
.collect::<Vec<_>>()
|
||||
);
|
||||
|
||||
// Every column of both tables: rename it away and restore the name.
|
||||
for (table, column) in V12_BLOB_COLUMNS
|
||||
.iter()
|
||||
.map(|column| ("artifact_blobs", *column))
|
||||
.chain(
|
||||
V12_SOURCE_COLUMNS
|
||||
.iter()
|
||||
.map(|column| ("artifact_sources", *column)),
|
||||
)
|
||||
{
|
||||
let drift = format!("alter table {table} rename column {column} to {column}_crank_drift;");
|
||||
let restore =
|
||||
format!("alter table {table} rename column {column}_crank_drift to {column};");
|
||||
assert_v12_drift(&pool, &drift, &restore).await;
|
||||
}
|
||||
|
||||
// Every column of both tables: change its type and restore the original,
|
||||
// then toggle nullability and restore it. The primary-key members are a
|
||||
// special case because PostgreSQL rejects dropping their NOT NULL while the
|
||||
// key exists; that rejection is asserted and the nullable shape is reached
|
||||
// through a dropped/restored primary key.
|
||||
exhaustive_v12::assert_column_type_drift(&pool).await;
|
||||
exhaustive_v12::assert_column_nullability_drift(&pool).await;
|
||||
|
||||
// Every declared default: swap in a different typed default, then restore.
|
||||
for (table, column, replacement, original) in [
|
||||
(
|
||||
"artifact_blobs",
|
||||
"storage_lifecycle",
|
||||
"'unavailable'",
|
||||
"'available'",
|
||||
),
|
||||
("artifact_sources", "lifecycle", "'detached'", "'active'"),
|
||||
] {
|
||||
let drift = format!("alter table {table} alter column {column} set default {replacement};");
|
||||
let restore = format!("alter table {table} alter column {column} set default {original};");
|
||||
assert_v12_drift(&pool, &drift, &restore).await;
|
||||
}
|
||||
|
||||
// Every V12 column that declares no default: introduce a concrete typed
|
||||
// default, prove the guard rejects it, then drop the default and prove the
|
||||
// schema returns to Current. A bare `set default null` would be folded back
|
||||
// into "no default" by PostgreSQL, so the drift list is fixed and compared
|
||||
// against the default inventory to keep a missing field from dropping out.
|
||||
assert_eq!(
|
||||
V12_BLOB_NO_DEFAULT_DRIFT
|
||||
.iter()
|
||||
.map(|(column, _)| *column)
|
||||
.collect::<Vec<_>>(),
|
||||
V12_BLOB_DEFAULTS
|
||||
.iter()
|
||||
.filter(|(_, default)| default.is_none())
|
||||
.map(|(column, _)| *column)
|
||||
.collect::<Vec<_>>(),
|
||||
"every defaultless artifact_blobs column must have a drift case"
|
||||
);
|
||||
assert_eq!(
|
||||
V12_SOURCE_NO_DEFAULT_DRIFT
|
||||
.iter()
|
||||
.map(|(column, _)| *column)
|
||||
.collect::<Vec<_>>(),
|
||||
V12_SOURCE_DEFAULTS
|
||||
.iter()
|
||||
.filter(|(_, default)| default.is_none())
|
||||
.map(|(column, _)| *column)
|
||||
.collect::<Vec<_>>(),
|
||||
"every defaultless artifact_sources column must have a drift case"
|
||||
);
|
||||
for (table, column, drift) in V12_BLOB_NO_DEFAULT_DRIFT
|
||||
.iter()
|
||||
.map(|(column, drift)| ("artifact_blobs", *column, *drift))
|
||||
.chain(
|
||||
V12_SOURCE_NO_DEFAULT_DRIFT
|
||||
.iter()
|
||||
.map(|(column, drift)| ("artifact_sources", *column, *drift)),
|
||||
)
|
||||
{
|
||||
let set = format!("alter table {table} alter column {column} set default {drift};");
|
||||
let restore = format!("alter table {table} alter column {column} drop default;");
|
||||
assert_v12_drift(&pool, &set, &restore).await;
|
||||
}
|
||||
|
||||
// Every constraint: drop it, then restore it from its own catalog
|
||||
// definition. The foreign key that depends on the artifact_blobs primary
|
||||
// key is dropped first and restored after it.
|
||||
let definitions = catalog_constraint_definitions(&pool).await;
|
||||
for (table, constraint) in V12_BLOB_CONSTRAINTS
|
||||
.iter()
|
||||
.map(|constraint| ("artifact_blobs", *constraint))
|
||||
.chain(
|
||||
V12_SOURCE_CONSTRAINTS
|
||||
.iter()
|
||||
.map(|constraint| ("artifact_sources", *constraint)),
|
||||
)
|
||||
{
|
||||
let definition = definitions
|
||||
.get(constraint)
|
||||
.unwrap_or_else(|| panic!("missing definition for {constraint}"));
|
||||
if constraint == "artifact_blobs_pkey" {
|
||||
let fk = definitions
|
||||
.get("artifact_sources_blob_digest_fkey")
|
||||
.expect("the dependent foreign key definition must be captured");
|
||||
let drift = "alter table artifact_sources drop constraint artifact_sources_blob_digest_fkey;\n\
|
||||
alter table artifact_blobs drop constraint artifact_blobs_pkey;";
|
||||
let restore = format!(
|
||||
"alter table artifact_blobs add constraint artifact_blobs_pkey {definition};\n\
|
||||
alter table artifact_sources add constraint artifact_sources_blob_digest_fkey {fk};"
|
||||
);
|
||||
assert_v12_drift(&pool, drift, &restore).await;
|
||||
} else {
|
||||
let drift = format!("alter table {table} drop constraint {constraint};");
|
||||
let restore = format!("alter table {table} add constraint {constraint} {definition};");
|
||||
assert_v12_drift(&pool, &drift, &restore).await;
|
||||
}
|
||||
}
|
||||
|
||||
// Every constraint again, this time keeping its name and `contype` while
|
||||
// changing only its definition: same-name `check (true)`, foreign keys with
|
||||
// a different delete action, and primary key/unique constraints with
|
||||
// rewritten members. Each mutation must be rejected before the original
|
||||
// captured definition is replayed.
|
||||
exhaustive_v12::assert_constraint_definition_drift(&pool).await;
|
||||
|
||||
// The standalone V12 index: change its definition, then remove it.
|
||||
let index_definition: String = sqlx::query_scalar(
|
||||
"select pg_get_indexdef(i.indexrelid)
|
||||
from pg_catalog.pg_index i
|
||||
join pg_catalog.pg_class idx on idx.oid = i.indexrelid
|
||||
join pg_catalog.pg_namespace n on n.oid = idx.relnamespace
|
||||
where n.nspname = current_schema()
|
||||
and idx.relname = 'artifact_sources_workspace_created_idx'",
|
||||
)
|
||||
.fetch_one(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_v12_drift(
|
||||
&pool,
|
||||
"drop index artifact_sources_workspace_created_idx;
|
||||
create index artifact_sources_workspace_created_idx
|
||||
on artifact_sources(workspace_id, source_id, created_at);",
|
||||
&format!("drop index artifact_sources_workspace_created_idx;\n{index_definition};"),
|
||||
)
|
||||
.await;
|
||||
|
||||
// Every standalone index must be rejected when removed and restorable from
|
||||
// its own catalog definition. The V12 guard validates the whole expanded
|
||||
// name set, so each removal is a version 12 partial sequence.
|
||||
for index in [
|
||||
"artifact_blobs_expired_claim_idx",
|
||||
"artifact_sources_blob_lifecycle_detached_idx",
|
||||
"artifact_sources_openapi_dangling_idx",
|
||||
"artifact_sources_workspace_created_idx",
|
||||
] {
|
||||
let definition: String = sqlx::query_scalar(
|
||||
"select pg_get_indexdef(i.indexrelid)
|
||||
from pg_catalog.pg_index i
|
||||
join pg_catalog.pg_class idx on idx.oid = i.indexrelid
|
||||
join pg_catalog.pg_namespace n on n.oid = idx.relnamespace
|
||||
where n.nspname = current_schema()
|
||||
and idx.relname = $1",
|
||||
)
|
||||
.bind(index)
|
||||
.fetch_one(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_v12_drift(
|
||||
&pool,
|
||||
&format!("drop index {index};"),
|
||||
&format!("{definition};"),
|
||||
)
|
||||
.await;
|
||||
}
|
||||
|
||||
// Extra owned objects of every kind must be rejected by the exact inventory.
|
||||
assert_v12_drift(
|
||||
&pool,
|
||||
"create table artifact_crank_drift_owned(id integer);",
|
||||
"drop table artifact_crank_drift_owned;",
|
||||
)
|
||||
.await;
|
||||
assert_v12_drift(
|
||||
&pool,
|
||||
"create index artifact_crank_drift_owned_idx on artifact_blobs(digest);",
|
||||
"drop index artifact_crank_drift_owned_idx;",
|
||||
)
|
||||
.await;
|
||||
assert_v12_drift(
|
||||
&pool,
|
||||
"create view artifact_crank_drift_owned_view as select digest from artifact_blobs;",
|
||||
"drop view artifact_crank_drift_owned_view;",
|
||||
)
|
||||
.await;
|
||||
}
|
||||
+426
@@ -0,0 +1,426 @@
|
||||
use crank_registry::{MigrationAuthority, MigrationPreflight};
|
||||
|
||||
// V12 changed-type coverage. Every column gets a reachable `alter column ...
|
||||
// type` drift whose `information_schema.data_type` differs from the frozen
|
||||
// guard expectation, plus the exact expression that puts the original type
|
||||
// back. `character varying` is chosen for the text columns and `timestamp`
|
||||
// for the timestamptz columns because both are binary-compatible rewrites:
|
||||
// PostgreSQL applies them even while the primary key, unique, foreign key,
|
||||
// default, check and index objects stay in place, so the guard rejection is
|
||||
// genuinely caused by the type drift and not by a missing dependent object.
|
||||
const COLUMN_TYPE_DRIFT: &[(&str, &str, &str, &str)] = &[
|
||||
("artifact_blobs", "digest", "character varying", "text"),
|
||||
(
|
||||
"artifact_blobs",
|
||||
"artifact_ref",
|
||||
"character varying",
|
||||
"text",
|
||||
),
|
||||
("artifact_blobs", "size_bytes", "integer", "bigint"),
|
||||
(
|
||||
"artifact_blobs",
|
||||
"storage_lifecycle",
|
||||
"character varying",
|
||||
"text",
|
||||
),
|
||||
("artifact_blobs", "claim_token", "character varying", "text"),
|
||||
(
|
||||
"artifact_blobs",
|
||||
"claim_expires_at",
|
||||
"timestamp",
|
||||
"timestamp with time zone",
|
||||
),
|
||||
(
|
||||
"artifact_blobs",
|
||||
"created_at",
|
||||
"timestamp",
|
||||
"timestamp with time zone",
|
||||
),
|
||||
(
|
||||
"artifact_blobs",
|
||||
"updated_at",
|
||||
"timestamp",
|
||||
"timestamp with time zone",
|
||||
),
|
||||
(
|
||||
"artifact_sources",
|
||||
"workspace_id",
|
||||
"character varying",
|
||||
"text",
|
||||
),
|
||||
("artifact_sources", "source_id", "character varying", "text"),
|
||||
(
|
||||
"artifact_sources",
|
||||
"blob_digest",
|
||||
"character varying",
|
||||
"text",
|
||||
),
|
||||
("artifact_sources", "mime_type", "character varying", "text"),
|
||||
(
|
||||
"artifact_sources",
|
||||
"sensitivity",
|
||||
"character varying",
|
||||
"text",
|
||||
),
|
||||
("artifact_sources", "lifecycle", "character varying", "text"),
|
||||
(
|
||||
"artifact_sources",
|
||||
"created_at",
|
||||
"timestamp",
|
||||
"timestamp with time zone",
|
||||
),
|
||||
(
|
||||
"artifact_sources",
|
||||
"updated_at",
|
||||
"timestamp",
|
||||
"timestamp with time zone",
|
||||
),
|
||||
(
|
||||
"artifact_sources",
|
||||
"detached_at",
|
||||
"timestamp",
|
||||
"timestamp with time zone",
|
||||
),
|
||||
];
|
||||
|
||||
// Columns that legitimately carry `null` in V12. The remaining non-primary-key
|
||||
// columns are `not null`, and the primary-key members cannot have `not null`
|
||||
// dropped while the key exists.
|
||||
const V12_NULLABLE_COLUMNS: &[(&str, &str)] = &[
|
||||
("artifact_blobs", "claim_token"),
|
||||
("artifact_blobs", "claim_expires_at"),
|
||||
("artifact_sources", "detached_at"),
|
||||
];
|
||||
|
||||
const V12_PRIMARY_KEY_COLUMNS: &[(&str, &str)] = &[
|
||||
("artifact_blobs", "digest"),
|
||||
("artifact_sources", "workspace_id"),
|
||||
("artifact_sources", "source_id"),
|
||||
];
|
||||
|
||||
// V12 changed-definition coverage. Each entry keeps the original constraint
|
||||
// name and `pg_constraint.contype` and only changes the definition: every
|
||||
// check becomes `check (true)`, both foreign keys change their delete action,
|
||||
// and the primary key/unique members are rewritten while staying the same
|
||||
// kind. The original definition is captured from the catalog and replayed to
|
||||
// restore the exact prior shape.
|
||||
const CONSTRAINT_DEFINITION_DRIFT: &[(&str, &str, &str)] = &[
|
||||
(
|
||||
"artifact_blobs",
|
||||
"artifact_blobs_artifact_ref_key",
|
||||
"unique (digest)",
|
||||
),
|
||||
(
|
||||
"artifact_blobs",
|
||||
"artifact_blobs_claim_shape_check",
|
||||
"check (true)",
|
||||
),
|
||||
(
|
||||
"artifact_blobs",
|
||||
"artifact_blobs_claim_token_check",
|
||||
"check (true)",
|
||||
),
|
||||
(
|
||||
"artifact_blobs",
|
||||
"artifact_blobs_digest_check",
|
||||
"check (true)",
|
||||
),
|
||||
(
|
||||
"artifact_blobs",
|
||||
"artifact_blobs_lifecycle_check",
|
||||
"check (true)",
|
||||
),
|
||||
(
|
||||
"artifact_blobs",
|
||||
"artifact_blobs_pkey",
|
||||
"primary key (digest) include (artifact_ref)",
|
||||
),
|
||||
("artifact_blobs", "artifact_blobs_ref_check", "check (true)"),
|
||||
(
|
||||
"artifact_blobs",
|
||||
"artifact_blobs_size_check",
|
||||
"check (true)",
|
||||
),
|
||||
(
|
||||
"artifact_blobs",
|
||||
"artifact_blobs_timestamps_check",
|
||||
"check (true)",
|
||||
),
|
||||
(
|
||||
"artifact_sources",
|
||||
"artifact_sources_blob_digest_fkey",
|
||||
"foreign key (blob_digest) references artifact_blobs(digest) on delete cascade",
|
||||
),
|
||||
(
|
||||
"artifact_sources",
|
||||
"artifact_sources_id_check",
|
||||
"check (true)",
|
||||
),
|
||||
(
|
||||
"artifact_sources",
|
||||
"artifact_sources_lifecycle_check",
|
||||
"check (true)",
|
||||
),
|
||||
(
|
||||
"artifact_sources",
|
||||
"artifact_sources_mime_type_check",
|
||||
"check (true)",
|
||||
),
|
||||
(
|
||||
"artifact_sources",
|
||||
"artifact_sources_pkey",
|
||||
"primary key (source_id, workspace_id)",
|
||||
),
|
||||
(
|
||||
"artifact_sources",
|
||||
"artifact_sources_sensitivity_check",
|
||||
"check (true)",
|
||||
),
|
||||
(
|
||||
"artifact_sources",
|
||||
"artifact_sources_state_check",
|
||||
"check (true)",
|
||||
),
|
||||
(
|
||||
"artifact_sources",
|
||||
"artifact_sources_timestamps_check",
|
||||
"check (true)",
|
||||
),
|
||||
(
|
||||
"artifact_sources",
|
||||
"artifact_sources_workspace_id_fkey",
|
||||
"foreign key (workspace_id) references workspaces(id) on delete restrict",
|
||||
),
|
||||
];
|
||||
|
||||
async fn catalog_nullable(pool: &sqlx::PgPool, table: &str, column: &str) -> bool {
|
||||
sqlx::query_scalar::<_, String>(
|
||||
"select is_nullable
|
||||
from information_schema.columns
|
||||
where table_schema = current_schema() and table_name = $1 and column_name = $2",
|
||||
)
|
||||
.bind(table)
|
||||
.bind(column)
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
.unwrap()
|
||||
== "YES"
|
||||
}
|
||||
|
||||
pub(super) async fn assert_column_type_drift(pool: &sqlx::PgPool) {
|
||||
assert_eq!(
|
||||
COLUMN_TYPE_DRIFT.len(),
|
||||
17,
|
||||
"every V12 column needs a changed-type drift case"
|
||||
);
|
||||
for (table, column, drift_type, restore_type) in COLUMN_TYPE_DRIFT {
|
||||
let drift = format!("alter table {table} alter column {column} type {drift_type};");
|
||||
let restore = format!("alter table {table} alter column {column} type {restore_type};");
|
||||
super::assert_v12_drift(pool, &drift, &restore).await;
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) async fn assert_column_nullability_drift(pool: &sqlx::PgPool) {
|
||||
let mut covered = 0usize;
|
||||
for (table, column) in super::V12_BLOB_COLUMNS
|
||||
.iter()
|
||||
.map(|column| ("artifact_blobs", *column))
|
||||
.chain(
|
||||
super::V12_SOURCE_COLUMNS
|
||||
.iter()
|
||||
.map(|column| ("artifact_sources", *column)),
|
||||
)
|
||||
{
|
||||
covered += 1;
|
||||
let nullable = catalog_nullable(pool, table, column).await;
|
||||
let expected_nullable = V12_NULLABLE_COLUMNS.contains(&(table, column));
|
||||
assert_eq!(
|
||||
nullable, expected_nullable,
|
||||
"nullability inventory drift for {table}.{column}"
|
||||
);
|
||||
if V12_PRIMARY_KEY_COLUMNS.contains(&(table, column)) {
|
||||
assert_primary_key_nullability(pool, table, column).await;
|
||||
} else if nullable {
|
||||
let drift = format!("alter table {table} alter column {column} set not null;");
|
||||
let restore = format!("alter table {table} alter column {column} drop not null;");
|
||||
super::assert_v12_drift(pool, &drift, &restore).await;
|
||||
} else {
|
||||
let drift = format!("alter table {table} alter column {column} drop not null;");
|
||||
let restore = format!("alter table {table} alter column {column} set not null;");
|
||||
super::assert_v12_drift(pool, &drift, &restore).await;
|
||||
}
|
||||
}
|
||||
assert_eq!(covered, 17, "every V12 column needs a nullability case");
|
||||
assert_eq!(
|
||||
V12_PRIMARY_KEY_COLUMNS.len() + V12_NULLABLE_COLUMNS.len(),
|
||||
6,
|
||||
"the V12 nullability exceptions are exactly the three primary-key members and three nullable columns"
|
||||
);
|
||||
}
|
||||
|
||||
// PostgreSQL refuses `drop not null` on a primary-key member outright, so this
|
||||
// case cannot be reached with the key in place. The rejection is asserted
|
||||
// explicitly instead of being skipped, then the nullable shape is reached by
|
||||
// dropping the primary key (and the dependent foreign key for artifact_blobs),
|
||||
// toggling nullability, and replaying the captured definitions in dependency
|
||||
// order.
|
||||
async fn assert_primary_key_nullability(pool: &sqlx::PgPool, table: &str, column: &str) {
|
||||
let direct = format!("alter table {table} alter column {column} drop not null;");
|
||||
let error = sqlx::raw_sql(sqlx::AssertSqlSafe(direct))
|
||||
.execute(pool)
|
||||
.await
|
||||
.expect_err("PostgreSQL must reject dropping NOT NULL on a primary key member");
|
||||
assert!(
|
||||
error.to_string().contains("is in a primary key"),
|
||||
"unexpected rejection for {table}.{column}: {error}"
|
||||
);
|
||||
assert_eq!(
|
||||
MigrationAuthority::preflight(pool).await.unwrap(),
|
||||
MigrationPreflight::Current { version: 15 },
|
||||
"a rejected primary-key nullability change must leave the schema current"
|
||||
);
|
||||
|
||||
let definitions = super::catalog_constraint_definitions(pool).await;
|
||||
let (drift, restore) = if table == "artifact_blobs" {
|
||||
let pkey = definitions
|
||||
.get("artifact_blobs_pkey")
|
||||
.expect("artifact_blobs primary key definition must be captured");
|
||||
let fk = definitions
|
||||
.get("artifact_sources_blob_digest_fkey")
|
||||
.expect("the foreign key that depends on artifact_blobs_pkey must be captured");
|
||||
(
|
||||
"alter table artifact_sources drop constraint artifact_sources_blob_digest_fkey;\n\
|
||||
alter table artifact_blobs drop constraint artifact_blobs_pkey;\n\
|
||||
alter table artifact_blobs alter column digest drop not null;"
|
||||
.to_owned(),
|
||||
format!(
|
||||
"alter table artifact_blobs alter column digest set not null;\n\
|
||||
alter table artifact_blobs add constraint artifact_blobs_pkey {pkey};\n\
|
||||
alter table artifact_sources add constraint artifact_sources_blob_digest_fkey {fk};"
|
||||
),
|
||||
)
|
||||
} else {
|
||||
let pkey = definitions
|
||||
.get("artifact_sources_pkey")
|
||||
.expect("artifact_sources primary key definition must be captured");
|
||||
(
|
||||
format!(
|
||||
"alter table artifact_sources drop constraint artifact_sources_pkey;\n\
|
||||
alter table artifact_sources alter column {column} drop not null;"
|
||||
),
|
||||
format!(
|
||||
"alter table artifact_sources alter column {column} set not null;\n\
|
||||
alter table artifact_sources add constraint artifact_sources_pkey {pkey};"
|
||||
),
|
||||
)
|
||||
};
|
||||
super::assert_v12_drift(pool, &drift, &restore).await;
|
||||
}
|
||||
|
||||
pub(super) async fn assert_constraint_definition_drift(pool: &sqlx::PgPool) {
|
||||
assert_eq!(
|
||||
CONSTRAINT_DEFINITION_DRIFT.len(),
|
||||
18,
|
||||
"every V12 constraint needs a same-name/kind changed-definition case"
|
||||
);
|
||||
let definitions = super::catalog_constraint_definitions(pool).await;
|
||||
for (table, constraint, drift_definition) in CONSTRAINT_DEFINITION_DRIFT {
|
||||
let original = definitions
|
||||
.get(*constraint)
|
||||
.unwrap_or_else(|| panic!("missing original definition for {constraint}"));
|
||||
if *constraint == "artifact_blobs_pkey" {
|
||||
let fk = definitions
|
||||
.get("artifact_sources_blob_digest_fkey")
|
||||
.expect("the foreign key that depends on artifact_blobs_pkey must be captured");
|
||||
let drift = format!(
|
||||
"alter table artifact_sources drop constraint artifact_sources_blob_digest_fkey;\n\
|
||||
alter table artifact_blobs drop constraint artifact_blobs_pkey;\n\
|
||||
alter table artifact_blobs add constraint artifact_blobs_pkey {drift_definition};\n\
|
||||
alter table artifact_sources add constraint artifact_sources_blob_digest_fkey {fk};"
|
||||
);
|
||||
let restore = format!(
|
||||
"alter table artifact_sources drop constraint artifact_sources_blob_digest_fkey;\n\
|
||||
alter table artifact_blobs drop constraint artifact_blobs_pkey;\n\
|
||||
alter table artifact_blobs add constraint artifact_blobs_pkey {original};\n\
|
||||
alter table artifact_sources add constraint artifact_sources_blob_digest_fkey {fk};"
|
||||
);
|
||||
assert_definition_only_drift(pool, constraint, &drift, &restore).await;
|
||||
} else {
|
||||
let drift = format!(
|
||||
"alter table {table} drop constraint {constraint};\n\
|
||||
alter table {table} add constraint {constraint} {drift_definition};"
|
||||
);
|
||||
let restore = format!(
|
||||
"alter table {table} drop constraint {constraint};\n\
|
||||
alter table {table} add constraint {constraint} {original};"
|
||||
);
|
||||
assert_definition_only_drift(pool, constraint, &drift, &restore).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A definition test must not pass merely because a dependent constraint went
|
||||
// missing. Assert that exactly the named definition changed, with the complete
|
||||
// name/kind inventory preserved, before asking the production guard to reject it.
|
||||
async fn assert_definition_only_drift(
|
||||
pool: &sqlx::PgPool,
|
||||
changed: &str,
|
||||
drift: &str,
|
||||
restore: &str,
|
||||
) {
|
||||
let before = super::catalog_constraint_definitions(pool).await;
|
||||
let kinds = constraint_kinds(pool).await;
|
||||
sqlx::raw_sql(sqlx::AssertSqlSafe(drift.to_owned()))
|
||||
.execute(pool)
|
||||
.await
|
||||
.unwrap();
|
||||
let after = super::catalog_constraint_definitions(pool).await;
|
||||
assert_eq!(
|
||||
constraint_kinds(pool).await,
|
||||
kinds,
|
||||
"all constraint names and kinds must survive: {changed}"
|
||||
);
|
||||
assert_ne!(
|
||||
after.get(changed),
|
||||
before.get(changed),
|
||||
"definition must actually change: {changed}"
|
||||
);
|
||||
for (name, definition) in &before {
|
||||
if name != changed {
|
||||
assert_eq!(
|
||||
after.get(name),
|
||||
Some(definition),
|
||||
"unrelated definition changed: {name}"
|
||||
);
|
||||
}
|
||||
}
|
||||
let error = MigrationAuthority::preflight(pool).await.unwrap_err();
|
||||
assert_eq!(
|
||||
error.code(),
|
||||
"partial_sequence",
|
||||
"definition drift: {changed}"
|
||||
);
|
||||
assert_eq!(error.version(), Some(12), "definition drift: {changed}");
|
||||
sqlx::raw_sql(sqlx::AssertSqlSafe(restore.to_owned()))
|
||||
.execute(pool)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(super::catalog_constraint_definitions(pool).await, before);
|
||||
assert_eq!(
|
||||
MigrationAuthority::preflight(pool).await.unwrap(),
|
||||
MigrationPreflight::Current { version: 15 }
|
||||
);
|
||||
}
|
||||
|
||||
async fn constraint_kinds(pool: &sqlx::PgPool) -> Vec<(String, String)> {
|
||||
sqlx::query_as(
|
||||
"select c.conname::text, c.contype::text from pg_catalog.pg_constraint c
|
||||
join pg_catalog.pg_class t on t.oid = c.conrelid
|
||||
join pg_catalog.pg_namespace n on n.oid = t.relnamespace
|
||||
where n.nspname = current_schema()
|
||||
and t.relname in ('artifact_blobs', 'artifact_sources') order by c.conname",
|
||||
)
|
||||
.fetch_all(pool)
|
||||
.await
|
||||
.unwrap()
|
||||
}
|
||||
@@ -784,7 +784,7 @@ Explicit зависимости Epic 8: `8.1 → 8.2`; `8.3,8.4,8.5,8.7,8.8` з
|
||||
|
||||
| Case ID | Проверка (Given / When / Then) | Expected evidence | PASS criterion |
|
||||
|---|---|---|---|
|
||||
| `AT-2.10-01` | **Given** OpenAPI 3.x `components.securitySchemes` или Swagger 2.0 `securityDefinitions`<br>**When** importer анализирует schemes<br>**Then** bearer HTTP, basic HTTP и API key в header/query/cookie преобразуются в typed security metadata<br>**And** source names, locations и применимые format hints сохраняются без credential values. | Отчёт AT-2.10-01: фактический результат указанного When и наблюдения/значения для каждого Then/And; процедура, revision/config/environment, исполнитель/дата и bounded артефакты с digest. Для мутации/отказа — состояние до/после и число side effects. | bearer HTTP, basic HTTP и API key в header/query/cookie преобразуются в typed security metadata; source names, locations и применимые format hints сохраняются без credential values. |
|
||||
| `AT-2.10-01` | **Given** OpenAPI 3.x `components.securitySchemes` или Swagger 2.0 `securityDefinitions`<br>**When** importer анализирует schemes<br>**Then** OpenAPI 3.x bearer/basic и API key в header/query/cookie, а Swagger 2.0 basic и API key в header/query преобразуются в typed security metadata; Swagger 2.0 cookie явно отклоняется как unsupported<br>**And** source names, locations и применимые format hints сохраняются без credential values. | Отчёт AT-2.10-01: фактический результат указанного When и наблюдения/значения для каждого Then/And; процедура, revision/config/environment, исполнитель/дата и bounded артефакты с digest. Для мутации/отказа — состояние до/после и число side effects. | OpenAPI 3.x bearer/basic и API key в header/query/cookie, а Swagger 2.0 basic и API key в header/query преобразуются в typed security metadata; Swagger 2.0 cookie явно отклоняется как unsupported; source names, locations и применимые format hints сохраняются без credential values. |
|
||||
| `AT-2.10-02` | **Given** global security requirement<br>**When** Operation не задаёт собственный override<br>**Then** candidate наследует global requirement согласно specification semantics<br>**And** operation-level requirement или explicit no-auth корректно заменяет global rule. | Отчёт AT-2.10-02: фактический результат указанного When и наблюдения/значения для каждого Then/And; процедура, revision/config/environment, исполнитель/дата и bounded артефакты с digest. Для мутации/отказа — состояние до/после и число side effects. | candidate наследует global requirement согласно specification semantics; operation-level requirement или explicit no-auth корректно заменяет global rule. |
|
||||
| `AT-2.10-03` | **Given** security requirement с несколькими alternatives<br>**When** source определяет OR-семантику<br>**Then** preview сохраняет все допустимые alternatives и позволяет выбрать поддерживаемый вариант<br>**And** importer не объединяет alternatives в ложное обязательное AND. | Отчёт AT-2.10-03: фактический результат указанного When и наблюдения/значения для каждого Then/And; процедура, revision/config/environment, исполнитель/дата и bounded артефакты с digest. Для мутации/отказа — состояние до/после и число side effects. | preview сохраняет все допустимые alternatives и позволяет выбрать поддерживаемый вариант; importer не объединяет alternatives в ложное обязательное AND. |
|
||||
| `AT-2.10-04` | **Given** security requirement, требующий несколько schemes одновременно<br>**When** source определяет AND-семантику<br>**Then** candidate сохраняет составное требование, если runtime contract его поддерживает<br>**And** иначе affected Operation получает blocker finding с перечислением несохраняемой semantics. | Отчёт AT-2.10-04: фактический результат указанного When и наблюдения/значения для каждого Then/And; процедура, revision/config/environment, исполнитель/дата и bounded артефакты с digest. Для мутации/отказа — состояние до/после и число side effects. | candidate сохраняет составное требование, если runtime contract его поддерживает; иначе affected Operation получает blocker finding с перечислением несохраняемой semantics. |
|
||||
@@ -2996,3 +2996,7 @@ Protocol-кейсы (вне построчного AC-реестра): `AT-PROTO
|
||||
---
|
||||
|
||||
_Конец плана._
|
||||
|
||||
### Уточнение AT-2.10-01 — 2026-10-10
|
||||
|
||||
Разделены возможности двух форматов без изменения импортёра: в [OAS 2.0 Security Scheme Object](https://spec.openapis.org/oas/v2.0.html#security-scheme-object) для `apiKey.in` допустимы только `header` и `query`. Cookie поддерживает OpenAPI 3.x; неподдерживаемая Swagger-схема должна оставаться явным blocker, без создания Draft с ослабленной безопасностью. Предыдущее обобщение header/query/cookie на оба формата было ошибкой требований.
|
||||
@@ -0,0 +1,128 @@
|
||||
# Хранение и восстановление исходных документов OpenAPI
|
||||
|
||||
Эта инструкция описывает текущую реализацию Community. Контракт HTTP см.
|
||||
в [Admin API](admin-api.md#openapi-preview-upload), параметры — в
|
||||
[runtime-config.md](runtime-config.md), обновление схемы — в [migrations.md](migrations.md).
|
||||
|
||||
## Корень и ограничения
|
||||
|
||||
`CRANK_STORAGE_ROOT` — абсолютный путь, по умолчанию `/var/lib/crank/storage`.
|
||||
Admin API открывает его при запуске и закрепляет файловый дескриптор. Корень
|
||||
и внутренние каталоги должны принадлежать пользователю процесса и иметь права
|
||||
`0700`; опубликованные blobs — `0400`. Символьные ссылки, чужие владельцы,
|
||||
небезопасные права и hard links отклоняются. В Compose именованный том
|
||||
`artifact_storage` монтируется в этот путь, а `artifact-storage-init`
|
||||
подготавливает права. Не заменяйте root symlink и не исправляйте проблему
|
||||
прав рекурсивным `chmod 777`.
|
||||
|
||||
Production upload — `multipart/form-data` с единственным полем `file`, UTF-8
|
||||
YAML/JSON, непустой файл до 262144 bytes включительно. Расширение и MIME
|
||||
должны согласовываться; для YAML/JSON допустим `application/octet-stream`.
|
||||
Это фиксированный предел текущего импортёра, не отдельная переменная окружения.
|
||||
Дополнительные лимиты normalizer и external reference chain описаны в
|
||||
[openapi-import.md](openapi-import.md). Увеличение HTTP proxy body limit
|
||||
не увеличивает предел импортёра. Не передавайте документ через JSON `{document}`.
|
||||
|
||||
Физический blob адресуется только SHA-256: `sha256/<первые 2 hex>/<digest>`.
|
||||
Запись использует private temp, file fsync, публикацию без перезаписи и
|
||||
fsync каталога. Чтение проверяет digest до возврата bytes. PostgreSQL хранит
|
||||
workspace-scoped ownership и references; один физический digest может
|
||||
принадлежать нескольким workspace. Наличие файла не предоставляет права
|
||||
другому workspace. Preview не создаёт Operations; Apply создаёт их одной
|
||||
транзакцией и сохраняет неизменяемый результат для точного повторного запроса.
|
||||
|
||||
## Резервное копирование и восстановление
|
||||
|
||||
Сохраняйте согласованный комплект PostgreSQL, **всего** artifact storage,
|
||||
Compose, runtime configuration и используемой ревизии образов. Дамп БД
|
||||
без тома не восстанавливает исходники и snapshots; отдельно взятый том
|
||||
не восстанавливает ownership и immutable plans. Конфигурация и исходники
|
||||
могут содержать секретные данные: закрытые права, шифрование копии и ограниченный доступ.
|
||||
|
||||
Перед ручным снимком остановите mutating traffic и процессы `admin-api`/
|
||||
`mcp-server`, включая фоновые reconciliation workers; затем получите дамп
|
||||
PostgreSQL и архив тома. Не удаляйте named volume. Копируйте комплект на другой
|
||||
хост и проверяйте SHA256SUMS. Возобновляйте процессы после получения снимка.
|
||||
|
||||
`scripts/deploy-community.sh` создаёт комплект `backups/<UTC>/` из
|
||||
`postgres.dump`, `artifacts.tar.gz`, `runtime.env`, `docker-compose.yml`,
|
||||
`SHA256SUMS` и проверяет миграцию копии перед обновлением. Дамп и архив
|
||||
получаются последовательно; при продолжающихся записях это **не** доказательство
|
||||
атомарного снимка БД+файлов. Для строгой согласованности оператор должен
|
||||
организовать окно без записей. Скрипт сохраняет пять последних локальных
|
||||
комплектов, поэтому внешняя копия обязательна до следующего обновления.
|
||||
|
||||
Проверяйте восстановление на изолированном стенде с matching image revision,
|
||||
БД и томом: контрольные суммы, compatibility check, `/ready`, чтение
|
||||
сохранённого source, повтор Apply с прежним ключом и отсутствие дубликатов.
|
||||
Существующая команда восстановления изменяет данные назначения:
|
||||
|
||||
```sh
|
||||
CRANK_RESTORE_CONFIRM=restore ./scripts/restore-community.sh DEPLOY_DIR BACKUP_DIR
|
||||
```
|
||||
|
||||
Перед её выполнением сохраните текущее состояние и убедитесь, что выбрано
|
||||
нужное назначение. Не откатывайте только приложение после миграции схемы:
|
||||
используйте совместимый forward image либо проверенное восстановление комплекта.
|
||||
|
||||
## Очистка и восстановление claims
|
||||
|
||||
Координатор запускается в Admin API, проходит Recovery до Sweep и ограничивает
|
||||
каждый тик: 4096 traversal syscalls, 32 candidates, 32 mutation attempts,
|
||||
deadline 30 секунд. Период — 15 минут. Grace финальных/detached blobs — 24 часа,
|
||||
lease claim — 5 минут; cleanup temp — после часа с отдельными лимитами
|
||||
512 scans / 16 deletes. Это константы текущей версии, не конфигурационные ручки.
|
||||
|
||||
Referenced source действующего ImportPlan не является orphan. Старые upload jobs
|
||||
и detached references очищаются по жизненному циклу; нельзя освобождать место
|
||||
ручным удалением digest из storage, пока БД ссылается на него.
|
||||
Sweep сначала помещает eligible blob в quarantine. Recovery проверяет обе
|
||||
канонические локации и только после подтверждённого отсутствия переводит БД
|
||||
в `unavailable`. Неопределённая мутация/fsync/commit сохраняет claim для
|
||||
повторной попытки; перезапуск начинает bounded cycle заново. Старый worker
|
||||
не может финализировать новый fencing token. При сбое диска/БД восстановите
|
||||
ресурс, затем наблюдайте последующие тики; не сбрасывайте claim вручную в idle.
|
||||
|
||||
Legacy read — только из явно настроенного доверенного корня с проверками
|
||||
containment и digest. Это read compatibility, не автоматическая миграция,
|
||||
перезапись или удаление старых файлов. URL документа не выбирает корень.
|
||||
|
||||
## Диагностика и границы доказательств
|
||||
|
||||
Используйте canonical `X-Request-ID` и `X-Trace-ID` из ответа для сопоставления
|
||||
с диагностикой. Ошибки не должны включать raw source, credentials или пути.
|
||||
Reconciliation пишет фиксированные classification counters в события
|
||||
`admin.artifact_reconciliation.tick` / `failed`; это безопасные категории,
|
||||
а не реестр digest/workspace/token. `/ready` проверяет доступность PostgreSQL,
|
||||
но не подтверждает целостность каждого blob: verified read проверяет конкретный файл.
|
||||
|
||||
Для проверки фоновой очистки смотрите последовательность событий
|
||||
`admin.artifact_reconciliation.tick`: `cycle_completed=true` означает завершённый
|
||||
проход, `recovery_completed=false` — Recovery ещё продолжается. Несколько тиков
|
||||
с `retryable=true`, ростом `recovery_retryable`, `registration_retryable` или
|
||||
`mutation_retryable` требуют проверки доступности БД, свободного места и ошибок
|
||||
fsync/permissions. Событие `admin.artifact_reconciliation.failed` содержит
|
||||
категорию `backend` или `deadline`; сопоставьте её с журналами БД и хоста.
|
||||
После восстановления ресурса убедитесь, что следующие тики завершают цикл без
|
||||
retryable. Предел candidates/mutation attempts может означать продолжение
|
||||
ограниченного прохода, а не сбой.
|
||||
|
||||
В текущей версии эти значения — поля структурированных событий, а не
|
||||
Prometheus gauge размера очереди. Runtime не публикует точное число оставшихся
|
||||
blobs: не считайте один незавершённый тик доказательством утечки. Для Compose
|
||||
отберите события из журнала Admin API командой
|
||||
`docker compose logs --since 2h admin-api`; фильтруйте по фиксированным именам
|
||||
событий. Не включайте raw source или digest в диагностические выгрузки.
|
||||
|
||||
Внешние ссылки выключены по умолчанию. Разрешение задаёт оператор allowlist;
|
||||
автономный комплект требует подготовки и согласованной поддержки загрузки,
|
||||
а не произвольного доступа к файловой системе. Документ не может разрешить
|
||||
себе сеть. Изменённое удалённое содержимое требует нового preview.
|
||||
|
||||
Автоматические проверки находятся в `crank-artifacts/tests`, registry
|
||||
`artifact_sources`/`artifact_reconciliation`, Admin API `reconciliation/tests`
|
||||
и `openapi_import`, UI `openapi*.spec.js`. Они проверяют конкретные сбои и
|
||||
состояния; успешный CI не доказывает отсутствие любых аппаратных сбоев,
|
||||
потерь данных при произвольном порядке backup или утечек по времени.
|
||||
Reference performance, длительная эксплуатация и человеческая приёмка
|
||||
выполняются отдельно по [acceptance-test-plan.md](acceptance-test-plan.md).
|
||||
@@ -176,6 +176,9 @@ curl -I http://127.0.0.1:3000/
|
||||
|
||||
## Эксплуатация
|
||||
|
||||
Хранение OpenAPI sources, ограничения загрузки, согласованное резервирование
|
||||
и восстановление claims описаны в [операторской инструкции](artifact-storage-operations.md).
|
||||
|
||||
- `/health` проверяет, что процесс жив; `/ready` дополнительно проверяет доступность PostgreSQL и используется Docker Compose.
|
||||
- CD перед каждым обновлением создаёт согласованный комплект в `backups/<UTC-время>`: дамп PostgreSQL, том артефактов, окружение, Compose и контрольные суммы. Хранятся последние пять локальных комплектов.
|
||||
- Копируйте комплекты резервных копий на отдельный хост или в объектное хранилище.
|
||||
|
||||
Reference in new issue
Block a user