160 lines
5.0 KiB
Rust
160 lines
5.0 KiB
Rust
//! MCP server cards per tenant (#432): one tenant's cards are invisible and
|
|
//! untouchable for another, every tenant gets the default cards once, and a
|
|
//! read-only role sees no access tokens. Runs against a live Mongo and skips
|
|
//! cleanly without one.
|
|
|
|
#![allow(clippy::expect_used, clippy::unwrap_used)]
|
|
|
|
mod common;
|
|
|
|
use std::sync::Arc;
|
|
|
|
use axum::extract::{Extension, Path, Query};
|
|
use axum::http::StatusCode;
|
|
use axum::Json;
|
|
|
|
use compliance_agent::agent::ComplianceAgent;
|
|
use compliance_agent::api::handlers::mcp_servers::{
|
|
create_mcp_server, delete_mcp_server, list_mcp_servers, regenerate_mcp_server_token,
|
|
CreateRequest, ListQuery,
|
|
};
|
|
use compliance_agent::database::DatabasePool;
|
|
use compliance_core::tenant_ctx::TenantCtx;
|
|
use compliance_core::{OrgRole, TenantContext, TenantStatus};
|
|
|
|
use common::dev_config;
|
|
|
|
fn tenant(id: &str, role: OrgRole) -> TenantCtx {
|
|
TenantCtx(TenantContext {
|
|
tenant_id: id.to_string(),
|
|
tenant_slug: id.to_string(),
|
|
org_roles: vec![role],
|
|
products: vec!["compliance-scanner".to_string()],
|
|
plan: "starter".to_string(),
|
|
status: TenantStatus::Active,
|
|
user_id: "u-1".to_string(),
|
|
user_name: None,
|
|
staff: None,
|
|
api_key: None,
|
|
})
|
|
}
|
|
|
|
async fn names(agent: &Arc<ComplianceAgent>, who: TenantCtx) -> Vec<(String, String, String)> {
|
|
let Json(body) = list_mcp_servers(
|
|
Extension(agent.clone()),
|
|
who,
|
|
Query(ListQuery {
|
|
default_endpoint: Some("https://pruefwerk.example".into()),
|
|
}),
|
|
)
|
|
.await
|
|
.unwrap();
|
|
body["data"]
|
|
.as_array()
|
|
.unwrap()
|
|
.iter()
|
|
.map(|s| {
|
|
(
|
|
s["name"].as_str().unwrap().to_string(),
|
|
s["_id"]["$oid"].as_str().unwrap_or_default().to_string(),
|
|
s["access_token"].as_str().unwrap_or_default().to_string(),
|
|
)
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn mcp_server_cards_are_per_tenant() {
|
|
let uri = std::env::var("TEST_MONGODB_URI")
|
|
.unwrap_or_else(|_| "mongodb://root:example@localhost:27017/?authSource=admin".into());
|
|
let db_name = format!("mcps_{}", &uuid::Uuid::new_v4().simple().to_string()[..10]);
|
|
let Ok(pool) = DatabasePool::connect(&uri, &db_name).await else {
|
|
eprintln!("SKIP mcp_servers: no MongoDB reachable at {uri}");
|
|
return;
|
|
};
|
|
let agent = Arc::new(ComplianceAgent::new(dev_config(uri, db_name), pool.clone()));
|
|
let (a, b) = ("tenant-a", "tenant-b");
|
|
|
|
// Every tenant starts with the three default cards, once.
|
|
let first = names(&agent, tenant(a, OrgRole::ItAdmin)).await;
|
|
assert_eq!(first.len(), 3);
|
|
assert_eq!(names(&agent, tenant(a, OrgRole::ItAdmin)).await.len(), 3);
|
|
|
|
// Tenant A adds a card.
|
|
let _created = create_mcp_server(
|
|
Extension(agent.clone()),
|
|
tenant(a, OrgRole::ItAdmin),
|
|
Json(CreateRequest {
|
|
name: "A's own".into(),
|
|
endpoint_url: "https://mcp.a.example/mcp".into(),
|
|
transport: "http".into(),
|
|
port: None,
|
|
description: None,
|
|
mongodb_uri: Some("mongodb://secret-a".into()),
|
|
mongodb_database: None,
|
|
}),
|
|
)
|
|
.await
|
|
.unwrap();
|
|
let a_cards = names(&agent, tenant(a, OrgRole::ItAdmin)).await;
|
|
let (_, a_id, a_token) = a_cards
|
|
.iter()
|
|
.find(|(name, _, _)| name == "A's own")
|
|
.cloned()
|
|
.expect("A sees its card");
|
|
assert!(a_token.starts_with("mcp_"));
|
|
|
|
// Tenant B sees only its own defaults, and cannot touch A's card.
|
|
let b_cards = names(&agent, tenant(b, OrgRole::ItAdmin)).await;
|
|
assert_eq!(b_cards.len(), 3);
|
|
assert!(!b_cards
|
|
.iter()
|
|
.any(|(name, id, _)| name == "A's own" || *id == a_id));
|
|
assert_eq!(
|
|
delete_mcp_server(
|
|
Extension(agent.clone()),
|
|
tenant(b, OrgRole::ItAdmin),
|
|
Path(a_id.clone())
|
|
)
|
|
.await
|
|
.unwrap_err(),
|
|
StatusCode::NOT_FOUND
|
|
);
|
|
assert_eq!(
|
|
regenerate_mcp_server_token(
|
|
Extension(agent.clone()),
|
|
tenant(b, OrgRole::ItAdmin),
|
|
Path(a_id.clone())
|
|
)
|
|
.await
|
|
.unwrap_err(),
|
|
StatusCode::NOT_FOUND
|
|
);
|
|
assert!(names(&agent, tenant(a, OrgRole::ItAdmin))
|
|
.await
|
|
.iter()
|
|
.any(|(_, id, token)| *id == a_id && *token == a_token));
|
|
|
|
// A read-only role sees the cards without their tokens.
|
|
let read_only = names(&agent, tenant(a, OrgRole::Cxo)).await;
|
|
assert_eq!(read_only.len(), 4);
|
|
assert!(read_only.iter().all(|(_, _, token)| token.is_empty()));
|
|
|
|
// A deletes its own card.
|
|
assert_eq!(
|
|
delete_mcp_server(
|
|
Extension(agent.clone()),
|
|
tenant(a, OrgRole::ItAdmin),
|
|
Path(a_id)
|
|
)
|
|
.await
|
|
.unwrap(),
|
|
StatusCode::NO_CONTENT
|
|
);
|
|
assert_eq!(names(&agent, tenant(a, OrgRole::ItAdmin)).await.len(), 3);
|
|
|
|
for t in [a, b] {
|
|
pool.drop_tenant(t).await.ok();
|
|
}
|
|
}
|