From 8ae561a01a0bc43830c581843535fab5c0c1b1fc Mon Sep 17 00:00:00 2001 From: Sharang Parnerkar Date: Mon, 16 Nov 2026 19:09:51 +0000 Subject: [PATCH] feat(api): the OpenAPI description covers notifications, MCP, tickets and scan runs (#1006) (#1020) --- compliance-agent/src/api/handlers/dto.rs | 10 ++- compliance-agent/src/api/handlers/issues.rs | 13 +++- .../src/api/handlers/mcp_servers.rs | 72 +++++++++++++++--- .../src/api/handlers/mcp_tokens.rs | 48 ++++++++++-- .../src/api/handlers/notifications.rs | 73 +++++++++++++++++-- compliance-agent/src/api/handlers/scans.rs | 15 +++- compliance-agent/src/api/routes.rs | 61 +++++----------- .../src/api/undescribed_routes.txt | 12 --- 8 files changed, 223 insertions(+), 81 deletions(-) diff --git a/compliance-agent/src/api/handlers/dto.rs b/compliance-agent/src/api/handlers/dto.rs index baecd79..debeacf 100644 --- a/compliance-agent/src/api/handlers/dto.rs +++ b/compliance-agent/src/api/handlers/dto.rs @@ -141,20 +141,26 @@ impl TicketState { } /// The query of the tickets list (`GET /api/v1/issues`). -#[derive(Deserialize)] +#[derive(Deserialize, utoipa::IntoParams)] +#[into_params(parameter_in = Query)] pub struct IssuesFilter { /// A word of the title or the ticket number (`913`, `#913`, `PROJ-913`). #[serde(default)] pub q: Option, - /// Only open or only closed tickets; an unknown value is refused (400). + /// `open` or `closed`; an unknown value is refused (400). #[serde(default)] + #[param(value_type = Option, example = "open")] pub status: Option, /// Only the tickets of one target: of its findings and its CVEs. #[serde(default)] pub repo_id: Option, + /// The page, from 1 (default 1, at most 100 000). #[serde(default = "default_page", deserialize_with = "de_page")] + #[param(minimum = 1, maximum = 100_000)] pub page: u64, + /// Entries per page (default 50, at most 200). #[serde(default = "default_limit", deserialize_with = "de_limit")] + #[param(minimum = 1, maximum = 200)] pub limit: i64, } diff --git a/compliance-agent/src/api/handlers/issues.rs b/compliance-agent/src/api/handlers/issues.rs index 373c0be..961d8d2 100644 --- a/compliance-agent/src/api/handlers/issues.rs +++ b/compliance-agent/src/api/handlers/issues.rs @@ -7,10 +7,21 @@ use compliance_core::models::issue::cve_issue_prefix; use compliance_core::models::TrackerIssue; use compliance_core::tenant_ctx::TenantCtx; -/// The tenant's tickets, newest first, searched and filtered by `filter`. +/// The tenant's tickets, newest first, searched and filtered. /// /// A ticket stores no target, only the finding it is about: the target filter /// first looks up the ids of the target's findings that have a ticket. +#[utoipa::path( + get, + path = "/api/v1/issues", + tag = "Tickets", + params(IssuesFilter), + responses( + (status = 200, description = "A page of tickets"), + (status = 400, description = "An unknown status"), + (status = 401, description = "No valid bearer token"), + ) +)] #[tracing::instrument(skip_all, fields(repo_id = ?filter.repo_id, status = ?filter.status))] pub async fn list_issues( Extension(agent): AgentExt, diff --git a/compliance-agent/src/api/handlers/mcp_servers.rs b/compliance-agent/src/api/handlers/mcp_servers.rs index 9db9a4a..51695ce 100644 --- a/compliance-agent/src/api/handlers/mcp_servers.rs +++ b/compliance-agent/src/api/handlers/mcp_servers.rs @@ -49,7 +49,9 @@ const ALL_TOOLS: [&str; 7] = [ "dast_scan_summary", ]; -#[derive(Debug, serde::Deserialize)] +/// The query of [`list_mcp_servers`]. +#[derive(Debug, serde::Deserialize, utoipa::IntoParams)] +#[into_params(parameter_in = Query)] pub struct ListQuery { /// Where the deployment's MCP server answers (the dashboard's /// `MCP_ENDPOINT_URL`), for the default cards of a tenant that has none. @@ -57,18 +59,26 @@ pub struct ListQuery { pub default_endpoint: Option, } -#[derive(Debug, serde::Deserialize)] +/// The body of [`create_mcp_server`]. +#[derive(Debug, serde::Deserialize, utoipa::ToSchema)] pub struct CreateRequest { + /// The card's name. pub name: String, + /// Where the MCP server answers. pub endpoint_url: String, + /// The transport, such as `streamable-http`. #[serde(default)] pub transport: String, + /// The port, when not in the URL. #[serde(default)] pub port: Option, + /// A description. #[serde(default)] pub description: Option, + /// The MongoDB URI the server reads, for a server of its own. #[serde(default)] pub mongodb_uri: Option, + /// The MongoDB database the server reads. #[serde(default)] pub mongodb_database: Option, } @@ -126,8 +136,19 @@ async fn seed_defaults(db: &Database, endpoint: &str) -> Result<(), mongodb::err Ok(()) } -/// `GET /api/v1/mcp-servers` — the tenant's cards, newest first; the access -/// tokens only for roles that may change things. +/// The tenant's MCP server cards, newest first. +/// +/// The access tokens only for roles that may change things. +#[utoipa::path( + get, + path = "/api/v1/mcp-servers", + tag = "MCP", + params(ListQuery), + responses( + (status = 200, description = "The cards"), + (status = 401, description = "No valid bearer token"), + ) +)] #[tracing::instrument(skip_all)] pub async fn list_mcp_servers( Extension(agent): AgentExt, @@ -155,7 +176,18 @@ pub async fn list_mcp_servers( Ok(Json(serde_json::json!({ "data": servers }))) } -/// `POST /api/v1/mcp-servers` — add a card to the tenant. +/// Add an MCP server card. +#[utoipa::path( + post, + path = "/api/v1/mcp-servers", + tag = "MCP", + request_body = CreateRequest, + responses( + (status = 200, description = "The new card"), + (status = 400, description = "No name or no endpoint URL"), + (status = 401, description = "No valid bearer token"), + ) +)] #[tracing::instrument(skip_all)] pub async fn create_mcp_server( Extension(agent): AgentExt, @@ -194,8 +226,19 @@ pub async fn create_mcp_server( Ok(Json(serde_json::json!({ "id": inserted.inserted_id }))) } -/// `DELETE /api/v1/mcp-servers/{id}` — remove one of the tenant's cards; 404 -/// for an id the tenant has not. +/// Remove an MCP server card. +#[utoipa::path( + delete, + path = "/api/v1/mcp-servers/{id}", + tag = "MCP", + params(("id" = String, Path, description = "The card's id")), + responses( + (status = 204, description = "Removed"), + (status = 400, description = "Not an id"), + (status = 404, description = "No such card"), + (status = 401, description = "No valid bearer token"), + ) +)] #[tracing::instrument(skip_all, fields(server_id = %id))] pub async fn delete_mcp_server( Extension(agent): AgentExt, @@ -215,8 +258,19 @@ pub async fn delete_mcp_server( Ok(StatusCode::NO_CONTENT) } -/// `POST /api/v1/mcp-servers/{id}/token` — give one of the tenant's cards a -/// new access token; 404 for an id the tenant has not. +/// Give an MCP server card a new access token. +#[utoipa::path( + post, + path = "/api/v1/mcp-servers/{id}/token", + tag = "MCP", + params(("id" = String, Path, description = "The card's id")), + responses( + (status = 200, description = "The new token"), + (status = 400, description = "Not an id"), + (status = 404, description = "No such card"), + (status = 401, description = "No valid bearer token"), + ) +)] #[tracing::instrument(skip_all, fields(server_id = %id))] pub async fn regenerate_mcp_server_token( Extension(agent): AgentExt, diff --git a/compliance-agent/src/api/handlers/mcp_tokens.rs b/compliance-agent/src/api/handlers/mcp_tokens.rs index 87db849..283d458 100644 --- a/compliance-agent/src/api/handlers/mcp_tokens.rs +++ b/compliance-agent/src/api/handlers/mcp_tokens.rs @@ -33,8 +33,10 @@ const TOKEN_PREFIX: &str = "mcpt_"; /// Combined with `mcpt_` → 48-char tokens. const TOKEN_RAND_BYTES: usize = 32; -#[derive(serde::Deserialize)] +/// The body of [`create_mcp_token`]. +#[derive(serde::Deserialize, utoipa::ToSchema)] pub struct CreateMcpTokenRequest { + /// What the token is for. pub name: String, } @@ -46,7 +48,18 @@ pub struct CreateMcpTokenResponse { pub view: McpTokenView, } -/// `POST /api/v1/mcp-tokens` — mint a new token for the caller's tenant. +/// Mint an MCP token for the tenant; it is shown only in this answer. +#[utoipa::path( + post, + path = "/api/v1/mcp-tokens", + tag = "MCP", + request_body = CreateMcpTokenRequest, + responses( + (status = 200, description = "The token, once"), + (status = 400, description = "No name"), + (status = 401, description = "No valid bearer token"), + ) +)] #[tracing::instrument(skip_all)] pub async fn create_mcp_token( Extension(agent): AgentExt, @@ -85,9 +98,19 @@ pub async fn create_mcp_token( })) } -/// `GET /api/v1/mcp-tokens` — list tokens for the caller's tenant. -/// Hash is never returned; only metadata + the 12-char prefix so the -/// user can identify which row is which. +/// The tenant's MCP tokens, without the tokens. +/// +/// The hash is never returned; only metadata and the 12-character prefix, +/// so the user can tell which row is which. +#[utoipa::path( + get, + path = "/api/v1/mcp-tokens", + tag = "MCP", + responses( + (status = 200, description = "The tokens"), + (status = 401, description = "No valid bearer token"), + ) +)] #[tracing::instrument(skip_all)] pub async fn list_mcp_tokens( Extension(agent): AgentExt, @@ -119,9 +142,22 @@ pub async fn list_mcp_tokens( })) } -/// `DELETE /api/v1/mcp-tokens/{id}` — revoke (soft delete). +/// Revoke an MCP token. +/// /// Scoped to the caller's tenant: a user can't revoke another tenant's /// token even if they guess its id. +#[utoipa::path( + delete, + path = "/api/v1/mcp-tokens/{id}", + tag = "MCP", + params(("id" = String, Path, description = "The token's id")), + responses( + (status = 200, description = "Revoked"), + (status = 400, description = "Not an id"), + (status = 404, description = "No such token"), + (status = 401, description = "No valid bearer token"), + ) +)] #[tracing::instrument(skip_all, fields(id = %id))] pub async fn revoke_mcp_token( Extension(agent): AgentExt, diff --git a/compliance-agent/src/api/handlers/notifications.rs b/compliance-agent/src/api/handlers/notifications.rs index 62011c5..f3b4fef 100644 --- a/compliance-agent/src/api/handlers/notifications.rs +++ b/compliance-agent/src/api/handlers/notifications.rs @@ -9,7 +9,17 @@ use compliance_core::tenant_ctx::TenantCtx; use super::dto::{tenant_db, AgentExt, ApiResponse}; -/// GET /api/v1/notifications — List CVE notifications (newest first) +/// The tenant's CVE notifications, newest first, a page at a time. +#[utoipa::path( + get, + path = "/api/v1/notifications", + tag = "Notifications", + params(NotificationFilter), + responses( + (status = 200, description = "A page of notifications"), + (status = 401, description = "No valid bearer token"), + ) +)] #[tracing::instrument(skip_all)] pub async fn list_notifications( Extension(agent): AgentExt, @@ -85,7 +95,16 @@ pub async fn list_notifications( })) } -/// GET /api/v1/notifications/count — Count of unread notifications +/// The number of unread notifications. +#[utoipa::path( + get, + path = "/api/v1/notifications/count", + tag = "Notifications", + responses( + (status = 200, description = "The count"), + (status = 401, description = "No valid bearer token"), + ) +)] #[tracing::instrument(skip_all)] pub async fn notification_count( Extension(agent): AgentExt, @@ -101,7 +120,19 @@ pub async fn notification_count( Ok(Json(serde_json::json!({ "count": count }))) } -/// PATCH /api/v1/notifications/:id/read — Mark a notification as read +/// Mark a notification as read. +#[utoipa::path( + patch, + path = "/api/v1/notifications/{id}/read", + tag = "Notifications", + params(("id" = String, Path, description = "The notification's id")), + responses( + (status = 200, description = "Marked as read"), + (status = 400, description = "Not an id"), + (status = 404, description = "No such notification"), + (status = 401, description = "No valid bearer token"), + ) +)] #[tracing::instrument(skip_all, fields(id = %id))] pub async fn mark_read( Extension(agent): AgentExt, @@ -129,7 +160,19 @@ pub async fn mark_read( Ok(Json(serde_json::json!({ "status": "read" }))) } -/// PATCH /api/v1/notifications/:id/dismiss — Dismiss a notification +/// Dismiss a notification. +#[utoipa::path( + patch, + path = "/api/v1/notifications/{id}/dismiss", + tag = "Notifications", + params(("id" = String, Path, description = "The notification's id")), + responses( + (status = 200, description = "Dismissed"), + (status = 400, description = "Not an id"), + (status = 404, description = "No such notification"), + (status = 401, description = "No valid bearer token"), + ) +)] #[tracing::instrument(skip_all, fields(id = %id))] pub async fn dismiss_notification( Extension(agent): AgentExt, @@ -154,7 +197,16 @@ pub async fn dismiss_notification( Ok(Json(serde_json::json!({ "status": "dismissed" }))) } -/// POST /api/v1/notifications/read-all — Mark all new notifications as read +/// Mark every new notification as read. +#[utoipa::path( + post, + path = "/api/v1/notifications/read-all", + tag = "Notifications", + responses( + (status = 200, description = "Marked as read"), + (status = 401, description = "No valid bearer token"), + ) +)] #[tracing::instrument(skip_all)] pub async fn mark_all_read( Extension(agent): AgentExt, @@ -178,11 +230,20 @@ pub async fn mark_all_read( )) } -#[derive(Debug, Deserialize)] +/// Which notifications a list shows. +#[derive(Debug, Deserialize, utoipa::IntoParams)] +#[into_params(parameter_in = Query)] pub struct NotificationFilter { + /// `new`, `read`, `dismissed` or `all`; new and read without it. pub status: Option, + /// Only one severity. pub severity: Option, + /// Only one target's notifications (its id). pub repo_id: Option, + /// The page, from 1 (default 1, at most 100 000). + #[param(minimum = 1, maximum = 100_000)] pub page: Option, + /// Entries per page (default 50, at most 200). + #[param(minimum = 1, maximum = 200)] pub limit: Option, } diff --git a/compliance-agent/src/api/handlers/scans.rs b/compliance-agent/src/api/handlers/scans.rs index 9652884..f5c1cf0 100644 --- a/compliance-agent/src/api/handlers/scans.rs +++ b/compliance-agent/src/api/handlers/scans.rs @@ -8,7 +8,8 @@ use compliance_core::models::ScanRun; use compliance_core::tenant_ctx::TenantCtx; /// Which scan runs to list. -#[derive(Debug, Default, Deserialize)] +#[derive(Debug, Default, Deserialize, utoipa::IntoParams)] +#[into_params(parameter_in = Query)] pub struct ScanRunFilter { /// Only the runs of this target (its id), for the target's scan history. #[serde(default)] @@ -25,7 +26,17 @@ impl ScanRunFilter { } } -/// GET /api/v1/scan-runs?target_id=&page=&limit= — newest first. +/// The tenant's scan runs, newest first, a page at a time. +#[utoipa::path( + get, + path = "/api/v1/scan-runs", + tag = "Scans", + params(PaginationParams, ScanRunFilter), + responses( + (status = 200, description = "A page of scan runs"), + (status = 401, description = "No valid bearer token"), + ) +)] #[tracing::instrument(skip_all)] pub async fn list_scan_runs( Extension(agent): AgentExt, diff --git a/compliance-agent/src/api/routes.rs b/compliance-agent/src/api/routes.rs index 0790bb9..472e894 100644 --- a/compliance-agent/src/api/routes.rs +++ b/compliance-agent/src/api/routes.rs @@ -209,30 +209,20 @@ fn described_routes() -> OpenApiRouter { handlers::licenses::withdraw_review )) .routes(routes!(handlers::licenses::notices)) - .route("/api/v1/issues", get(handlers::list_issues)) - .route("/api/v1/scan-runs", get(handlers::list_scan_runs)) + .routes(routes!(handlers::list_issues)) + .routes(routes!(handlers::list_scan_runs)) // MCP token management (per-tenant API tokens for the MCP server) - .route( - "/api/v1/mcp-servers", - get(handlers::mcp_servers::list_mcp_servers) - .post(handlers::mcp_servers::create_mcp_server), - ) - .route( - "/api/v1/mcp-servers/{id}", - delete(handlers::mcp_servers::delete_mcp_server), - ) - .route( - "/api/v1/mcp-servers/{id}/token", - post(handlers::mcp_servers::regenerate_mcp_server_token), - ) - .route( - "/api/v1/mcp-tokens", - get(handlers::mcp_tokens::list_mcp_tokens).post(handlers::mcp_tokens::create_mcp_token), - ) - .route( - "/api/v1/mcp-tokens/{id}", - delete(handlers::mcp_tokens::revoke_mcp_token), - ) + .routes(routes!( + handlers::mcp_servers::list_mcp_servers, + handlers::mcp_servers::create_mcp_server + )) + .routes(routes!(handlers::mcp_servers::delete_mcp_server)) + .routes(routes!(handlers::mcp_servers::regenerate_mcp_server_token)) + .routes(routes!( + handlers::mcp_tokens::list_mcp_tokens, + handlers::mcp_tokens::create_mcp_token + )) + .routes(routes!(handlers::mcp_tokens::revoke_mcp_token)) // Graph API endpoints .routes(routes!(handlers::graph::get_graph)) .routes(routes!(handlers::graph::get_nodes)) @@ -258,26 +248,11 @@ fn described_routes() -> OpenApiRouter { // Help chat (documentation-grounded Q&A) .routes(routes!(handlers::help_chat::help_chat)) // CVE notification endpoints - .route( - "/api/v1/notifications", - get(handlers::notifications::list_notifications), - ) - .route( - "/api/v1/notifications/count", - get(handlers::notifications::notification_count), - ) - .route( - "/api/v1/notifications/read-all", - post(handlers::notifications::mark_all_read), - ) - .route( - "/api/v1/notifications/{id}/read", - patch(handlers::notifications::mark_read), - ) - .route( - "/api/v1/notifications/{id}/dismiss", - patch(handlers::notifications::dismiss_notification), - ) + .routes(routes!(handlers::notifications::list_notifications)) + .routes(routes!(handlers::notifications::notification_count)) + .routes(routes!(handlers::notifications::mark_all_read)) + .routes(routes!(handlers::notifications::mark_read)) + .routes(routes!(handlers::notifications::dismiss_notification)) // Pentest API endpoints .route( "/api/v1/pentest/lookup-repo", diff --git a/compliance-agent/src/api/undescribed_routes.txt b/compliance-agent/src/api/undescribed_routes.txt index 999b710..a2c65cf 100644 --- a/compliance-agent/src/api/undescribed_routes.txt +++ b/compliance-agent/src/api/undescribed_routes.txt @@ -37,18 +37,6 @@ /api/v1/controls /api/v1/targets/{id}/findings /api/v1/targets/{id}/findings/import -/api/v1/issues -/api/v1/scan-runs -/api/v1/mcp-servers -/api/v1/mcp-servers/{id} -/api/v1/mcp-servers/{id}/token -/api/v1/mcp-tokens -/api/v1/mcp-tokens/{id} -/api/v1/notifications -/api/v1/notifications/count -/api/v1/notifications/read-all -/api/v1/notifications/{id}/read -/api/v1/notifications/{id}/dismiss /api/v1/pentest/lookup-repo /api/v1/pentest/sessions /api/v1/pentest/sessions/{id}