Files
crank/apps/admin-api/tests/integration/resources/revocation_preservation.rs
T
Hermes DevOps 75fe5a123c [skip ci] wip: preserve Hermes crank development checkpoint
Owner-authorized recovery of accumulated source changes.
Includes accepted work and in-progress changes; NOT a release or acceptance.
Parent baseline 1b6db09bd1. Runtime data, build caches and local credentials excluded.
2026-10-25 10:00:22 +00:00

365 lines
12 KiB
Rust

//! Story 3.8 / AT-3.8-04: revocation is a security transition, not a deletion. The archived
//! Resource keeps its exact immutable publication rows, its exact bytes (including the protected
//! administrative download) and its bindings; a Draft can be revoked and can never be published
//! afterwards. The archive-only positive control stays the unchanged
//! `archive_keeps_the_exact_published_snapshot_readable_for_the_bound_agent` in
//! `mcp-server/tests/integration/static_resources/lifecycle.rs`.
use reqwest::StatusCode;
use serde_json::{Value, json};
use sqlx::Row;
use super::helpers::{
create_draft, created_resource_id, draft_updated_at, get_card, read_response, sha256_hex,
};
use super::revocation::{
KEPT_URI, NOT_AVAILABLE_CODE, NOT_AVAILABLE_MESSAGE, REVOKED_CANARY, REVOKED_CANARY_V2,
REVOKED_URI, mcp_read, read_text, spawn_revocation_stand,
};
#[derive(Debug, PartialEq)]
struct PublicationSnapshot {
version: i32,
uri: String,
name: String,
description: String,
kind: String,
mime_type: String,
size_bytes: i64,
digest: String,
sensitivity: String,
artifact_source_id: String,
published_at: time::OffsetDateTime,
}
async fn publications(pool: &sqlx::PgPool, resource_id: &str) -> Vec<PublicationSnapshot> {
let rows = sqlx::query(
"select version, uri, name, description, kind, mime_type, size_bytes, digest,
sensitivity, artifact_source_id, published_at
from resource_publications
where workspace_id = 'ws_default' and resource_id = $1
order by version asc",
)
.bind(resource_id)
.fetch_all(pool)
.await
.expect("publication rows must be readable");
rows.iter()
.map(|row| PublicationSnapshot {
version: row.try_get("version").unwrap(),
uri: row.try_get("uri").unwrap(),
name: row.try_get("name").unwrap(),
description: row.try_get("description").unwrap(),
kind: row.try_get("kind").unwrap(),
mime_type: row.try_get("mime_type").unwrap(),
size_bytes: row.try_get("size_bytes").unwrap(),
digest: row.try_get("digest").unwrap(),
sensitivity: row.try_get("sensitivity").unwrap(),
artifact_source_id: row.try_get("artifact_source_id").unwrap(),
published_at: row.try_get("published_at").unwrap(),
})
.collect()
}
async fn bindings(pool: &sqlx::PgPool, resource_id: &str) -> Vec<(String, i32, String, i32)> {
sqlx::query_as(
"select agent_id, agent_version, resource_id, resource_version
from agent_resource_bindings
where resource_id = $1
order by agent_id asc",
)
.bind(resource_id)
.fetch_all(pool)
.await
.expect("binding rows must be readable")
}
async fn admin_version(
stand: &super::revocation::RevocationStand,
resource_id: &str,
version: u32,
) -> Value {
let (status, text, body) = read_response(
stand
.admin
.get(format!(
"{}/resources/{resource_id}/versions/{version}",
stand.server
))
.send()
.await
.expect("the administrative version read must complete"),
)
.await;
assert_eq!(
status,
StatusCode::OK,
"the protected administrative read must survive archive+revoke: {text}"
);
body
}
async fn admin_download(
stand: &super::revocation::RevocationStand,
resource_id: &str,
version: u32,
) -> Vec<u8> {
let response = stand
.admin
.get(format!(
"{}/resources/{resource_id}/versions/{version}/content",
stand.server
))
.send()
.await
.expect("the protected administrative download must complete");
assert_eq!(
response.status(),
StatusCode::OK,
"the protected administrative download must survive archive+revoke"
);
assert_eq!(
response.headers()[reqwest::header::CONTENT_TYPE],
"application/octet-stream",
"the download must stay inert bytes, never the published MIME"
);
response.bytes().await.unwrap_or_default().to_vec()
}
/// Archive → revoke: the immutable snapshot, its metadata, its bindings and the protected
/// administrative download are byte-identical, while MCP access and new writes are closed.
#[tokio::test]
async fn story38_archive_then_revoke_preserves_the_exact_snapshot_and_admin_download() {
let stand = spawn_revocation_stand("preservation").await;
let pool = stand.registry.pool();
let publications_before = publications(pool, &stand.revoked_id).await;
assert_eq!(
publications_before.len(),
2,
"the fixture publishes v1 and v2"
);
assert_eq!(
publications_before[0].digest,
sha256_hex(REVOKED_CANARY.as_bytes())
);
assert_eq!(
publications_before[1].digest,
sha256_hex(REVOKED_CANARY_V2.as_bytes())
);
let metadata_before = admin_version(&stand, &stand.revoked_id, 1).await;
let bindings_before = bindings(pool, &stand.revoked_id).await;
assert_eq!(
bindings_before.len(),
2,
"both Agents are bound to the target"
);
let download_v1_before = admin_download(&stand, &stand.revoked_id, 1).await;
let download_v2_before = admin_download(&stand, &stand.revoked_id, 2).await;
assert_eq!(download_v1_before, REVOKED_CANARY.as_bytes());
assert_eq!(download_v2_before, REVOKED_CANARY_V2.as_bytes());
// Archive first (a lifecycle transition), revoke second (the security transition).
let card = get_card(&stand.admin, &stand.server, &stand.revoked_id).await;
let updated_at = draft_updated_at(&card);
let (status, text, _) = read_response(
stand
.admin
.post(format!(
"{}/resources/{}/archive",
stand.server, stand.revoked_id
))
.json(&json!({ "expected_updated_at": updated_at }))
.send()
.await
.expect("the archive request must complete"),
)
.await;
assert_eq!(status, StatusCode::OK, "archive must succeed first: {text}");
let revoked = stand
.admin
.post(format!(
"{}/resources/{}/revoke",
stand.server, stand.revoked_id
))
.send()
.await
.expect("the revoke request must complete");
assert_eq!(revoked.status(), StatusCode::NO_CONTENT);
assert_eq!(
publications(pool, &stand.revoked_id).await,
publications_before,
"revoke must not rewrite any immutable publication field"
);
assert_eq!(
bindings(pool, &stand.revoked_id).await,
bindings_before,
"revoke must not rewrite the existing bindings"
);
assert_eq!(
admin_version(&stand, &stand.revoked_id, 1).await,
metadata_before,
"the protected administrative metadata must stay identical"
);
assert_eq!(
admin_download(&stand, &stand.revoked_id, 1).await,
REVOKED_CANARY.as_bytes(),
"the protected administrative download must return the exact archived bytes"
);
assert_eq!(
admin_download(&stand, &stand.revoked_id, 2).await,
REVOKED_CANARY_V2.as_bytes(),
"the exact second version bytes must stay downloadable for investigation"
);
// MCP access is closed for both Agents/versions and both revisions.
for revision in super::revocation::supported_revisions() {
for (agent, url, key) in [
("first", &stand.mcp_url, &stand.mcp_key),
("second", &stand.mcp_url_second, &stand.mcp_key_second),
] {
let denied = mcp_read(&stand.mcp, url, key, &revision, REVOKED_URI).await;
assert!(
denied.get("result").is_none(),
"{agent}/{revision}: {denied}"
);
assert_eq!(
denied["error"]["code"].as_i64(),
Some(NOT_AVAILABLE_CODE),
"{agent}/{revision}: {denied}"
);
assert_eq!(
denied["error"]["message"].as_str(),
Some(NOT_AVAILABLE_MESSAGE)
);
}
let kept = mcp_read(
&stand.mcp,
&stand.mcp_url,
&stand.mcp_key,
&revision,
KEPT_URI,
)
.await;
assert!(
read_text(&kept).is_some(),
"{revision}: the archive-only control Resource must stay readable: {kept}"
);
}
// A later publication of the archived+revoked Resource is refused.
let card = get_card(&stand.admin, &stand.server, &stand.revoked_id).await;
let updated_at = draft_updated_at(&card);
let (status, text, body) = read_response(
stand
.admin
.post(format!(
"{}/resources/{}/publish",
stand.server, stand.revoked_id
))
.json(&json!({ "expected_updated_at": updated_at }))
.send()
.await
.expect("the post-revoke publish must complete"),
)
.await;
assert_eq!(
status,
StatusCode::CONFLICT,
"an archived+revoked Resource must not publish: {text}"
);
// Both guards are 409 and their priority is deliberately unspecified (TL review B10): an
// archived Resource may report either lifecycle or revoke, but it must never publish.
let code = body["error"]["context"]["error_code"].as_str();
assert!(
matches!(
code,
Some("resource_revoked" | "resource_lifecycle_invalid")
),
"the refusal must be one of the guarded 409 codes: {body}"
);
assert!(
publications(pool, &stand.revoked_id).await == publications_before,
"the refused publication must not add an immutable row"
);
}
/// A Draft can be revoked too: it never becomes publishable afterwards and no publication row is
/// ever created. Drafts have no MCP bytes, so only the write path is observable.
#[tokio::test]
async fn story38_revoke_blocks_a_draft_from_ever_publishing() {
let stand = spawn_revocation_stand("draft_revoke").await;
let pool = stand.registry.pool();
let draft = create_draft(
&stand.admin,
&stand.server,
&super::helpers::draft_metadata(
"crank://resources/story38-draft-revoke",
"story38_draft_revoke",
"text",
"text/plain",
"internal",
),
b"draft revoke v1\n",
"story38-draft-revoke.txt",
"text/plain",
)
.await;
let draft_id = created_resource_id(&draft);
assert_eq!(
publications(pool, &draft_id).await.len(),
0,
"a Draft must start without a publication"
);
let revoked = stand
.admin
.post(format!("{}/resources/{draft_id}/revoke", stand.server))
.send()
.await
.expect("the Draft revoke must reach the real router");
assert_eq!(
revoked.status(),
StatusCode::NO_CONTENT,
"a Draft must be revocable"
);
let card = get_card(&stand.admin, &stand.server, &draft_id).await;
let updated_at = draft_updated_at(&card);
let (status, text, body) = read_response(
stand
.admin
.post(format!("{}/resources/{draft_id}/publish", stand.server))
.json(&json!({ "expected_updated_at": updated_at }))
.send()
.await
.expect("the Draft publish must complete"),
)
.await;
assert_eq!(
status,
StatusCode::CONFLICT,
"a revoked Draft must never publish: {text}"
);
assert_eq!(
body["error"]["context"]["error_code"].as_str(),
Some("resource_revoked"),
"the refusal must use the stable revoked code: {body}"
);
assert_eq!(
publications(pool, &draft_id).await.len(),
0,
"the refused publish must not create a publication row"
);
let tombstone: i64 = sqlx::query_scalar(
"select count(*) from resource_security_revocations where resource_id = $1",
)
.bind(&draft_id)
.fetch_one(pool)
.await
.expect("tombstones must be queryable");
assert_eq!(tombstone, 1, "the Draft revoke must be an audited event");
}