diff --git a/compliance-agent/src/api/handlers/pentest_handlers/export.rs b/compliance-agent/src/api/handlers/pentest_handlers/export.rs index 60f75f0..aa50f8f 100644 --- a/compliance-agent/src/api/handlers/pentest_handlers/export.rs +++ b/compliance-agent/src/api/handlers/pentest_handlers/export.rs @@ -21,8 +21,10 @@ use super::super::dto::{collect_cursor_async, tenant_db}; type AgentExt = Extension>; -#[derive(Deserialize)] +/// The body of [`export_session_report`]. +#[derive(Deserialize, utoipa::ToSchema)] pub struct ExportBody { + /// The password the archive is encrypted with. pub password: String, /// Requester display name (from auth) #[serde(default)] @@ -32,7 +34,20 @@ pub struct ExportBody { pub requester_email: String, } -/// POST /api/v1/pentest/sessions/:id/export — Export an encrypted pentest report archive +/// A session's report as an encrypted archive to download. +#[utoipa::path( + post, + path = "/api/v1/pentest/sessions/{id}/export", + tag = "Pentest", + params(("id" = String, Path, description = "The session's id")), + request_body = ExportBody, + responses( + (status = 200, description = "The archive"), + (status = 400, description = "Not an id, or no password"), + (status = 404, description = "No such session"), + (status = 401, description = "No valid bearer token"), + ) +)] #[tracing::instrument(skip_all, fields(session_id = %id))] pub async fn export_session_report( Extension(agent): AgentExt, diff --git a/compliance-agent/src/api/handlers/pentest_handlers/session.rs b/compliance-agent/src/api/handlers/pentest_handlers/session.rs index 7210a21..353f9c6 100644 --- a/compliance-agent/src/api/handlers/pentest_handlers/session.rs +++ b/compliance-agent/src/api/handlers/pentest_handlers/session.rs @@ -17,13 +17,18 @@ use super::super::dto::{collect_cursor_async, tenant_db, ApiResponse, Pagination type AgentExt = Extension>; -#[derive(Deserialize)] +/// The body of [`create_session`]. +#[derive(Deserialize, utoipa::ToSchema)] pub struct CreateSessionRequest { + /// The target to test, for a session without `config`. pub target_id: Option, + /// `quick`, `comprehensive` (default), `targeted` or `aggressive`. #[serde(default = "default_strategy")] pub strategy: String, + /// The first instruction. pub message: Option, /// Wizard configuration — if present, takes precedence over legacy fields + #[schema(value_type = Option)] pub config: Option, } @@ -31,17 +36,37 @@ fn default_strategy() -> String { "comprehensive".to_string() } -#[derive(Deserialize)] +/// The body of [`send_message`]. +#[derive(Deserialize, utoipa::ToSchema)] pub struct SendMessageRequest { + /// The message. pub message: String, } -#[derive(Deserialize)] +/// The query of [`lookup_repo`]. +#[derive(Deserialize, utoipa::IntoParams)] +#[into_params(parameter_in = Query)] pub struct LookupRepoQuery { + /// The repository's git URL. pub url: String, } -/// POST /api/v1/pentest/sessions — Create a new pentest session and start the orchestrator +/// Create a pentest session and start it. +#[utoipa::path( + post, + path = "/api/v1/pentest/sessions", + tag = "Pentest", + request_body = CreateSessionRequest, + responses( + (status = 200, description = "The session"), + (status = 400, description = "A missing disclaimer, target or id, or a URL the agent may not reach"), + (status = 404, description = "No such target"), + (status = 409, description = "Pentests are not available on this deployment (static analysis only)"), + (status = 429, description = "Too many sessions are running"), + (status = 503, description = "A credential cannot be sealed"), + (status = 401, description = "No valid bearer token"), + ) +)] #[tracing::instrument(skip_all)] pub async fn create_session( Extension(agent): AgentExt, @@ -349,7 +374,17 @@ fn parse_strategy(s: &str) -> PentestStrategy { } } -/// GET /api/v1/pentest/lookup-repo — Look up a tracked repository by git URL +/// Look up a tracked repository by its git URL. +#[utoipa::path( + get, + path = "/api/v1/pentest/lookup-repo", + tag = "Pentest", + params(LookupRepoQuery), + responses( + (status = 200, description = "The repository, or null"), + (status = 401, description = "No valid bearer token"), + ) +)] #[tracing::instrument(skip_all)] pub async fn lookup_repo( Extension(agent): AgentExt, @@ -382,7 +417,17 @@ pub async fn lookup_repo( })) } -/// GET /api/v1/pentest/sessions — List pentest sessions +/// The tenant's pentest sessions, a page at a time. +#[utoipa::path( + get, + path = "/api/v1/pentest/sessions", + tag = "Pentest", + params(PaginationParams), + responses( + (status = 200, description = "A page of sessions"), + (status = 401, description = "No valid bearer token"), + ) +)] #[tracing::instrument(skip_all)] pub async fn list_sessions( Extension(agent): AgentExt, @@ -420,7 +465,19 @@ pub async fn list_sessions( })) } -/// GET /api/v1/pentest/sessions/:id — Get a single pentest session +/// One pentest session. +#[utoipa::path( + get, + path = "/api/v1/pentest/sessions/{id}", + tag = "Pentest", + params(("id" = String, Path, description = "The session's id")), + responses( + (status = 200, description = "The session"), + (status = 400, description = "Not an id"), + (status = 404, description = "No such session"), + (status = 401, description = "No valid bearer token"), + ) +)] #[tracing::instrument(skip_all, fields(session_id = %id))] pub async fn get_session( Extension(agent): AgentExt, @@ -454,7 +511,20 @@ pub async fn get_session( })) } -/// POST /api/v1/pentest/sessions/:id/chat — Send a user message and trigger next orchestrator iteration +/// Send a message to a session, which starts its next iteration. +#[utoipa::path( + post, + path = "/api/v1/pentest/sessions/{id}/chat", + tag = "Pentest", + params(("id" = String, Path, description = "The session's id")), + request_body = SendMessageRequest, + responses( + (status = 200, description = "The stored message"), + (status = 400, description = "Not an id"), + (status = 404, description = "No such session"), + (status = 401, description = "No valid bearer token"), + ) +)] #[tracing::instrument(skip_all, fields(session_id = %id))] pub async fn send_message( Extension(agent): AgentExt, @@ -554,7 +624,19 @@ pub async fn send_message( })) } -/// POST /api/v1/pentest/sessions/:id/stop — Stop a running pentest session +/// Stop a running session. +#[utoipa::path( + post, + path = "/api/v1/pentest/sessions/{id}/stop", + tag = "Pentest", + params(("id" = String, Path, description = "The session's id")), + responses( + (status = 200, description = "The stopped session"), + (status = 400, description = "Not an id, or the session is not running"), + (status = 404, description = "No such session"), + (status = 401, description = "No valid bearer token"), + ) +)] #[tracing::instrument(skip_all, fields(session_id = %id))] pub async fn stop_session( Extension(agent): AgentExt, @@ -630,7 +712,19 @@ pub async fn stop_session( })) } -/// POST /api/v1/pentest/sessions/:id/pause — Pause a running pentest session +/// Pause a running session. +#[utoipa::path( + post, + path = "/api/v1/pentest/sessions/{id}/pause", + tag = "Pentest", + params(("id" = String, Path, description = "The session's id")), + responses( + (status = 200, description = "Paused"), + (status = 400, description = "Not an id, or the session is not running"), + (status = 404, description = "No such session"), + (status = 401, description = "No valid bearer token"), + ) +)] #[tracing::instrument(skip_all, fields(session_id = %id))] pub async fn pause_session( Extension(agent): AgentExt, @@ -676,7 +770,19 @@ pub async fn pause_session( })) } -/// POST /api/v1/pentest/sessions/:id/resume — Resume a paused pentest session +/// Resume a paused session. +#[utoipa::path( + post, + path = "/api/v1/pentest/sessions/{id}/resume", + tag = "Pentest", + params(("id" = String, Path, description = "The session's id")), + responses( + (status = 200, description = "Resumed"), + (status = 400, description = "Not an id, or the session is not paused"), + (status = 404, description = "No such session"), + (status = 401, description = "No valid bearer token"), + ) +)] #[tracing::instrument(skip_all, fields(session_id = %id))] pub async fn resume_session( Extension(agent): AgentExt, @@ -727,7 +833,18 @@ pub async fn resume_session( /// being loaded whole into agent memory. const MAX_ATTACK_CHAIN_NODES: i64 = 2_000; -/// GET /api/v1/pentest/sessions/:id/attack-chain — Get attack chain nodes for a session +/// A session's attack chain. +#[utoipa::path( + get, + path = "/api/v1/pentest/sessions/{id}/attack-chain", + tag = "Pentest", + params(("id" = String, Path, description = "The session's id")), + responses( + (status = 200, description = "The nodes"), + (status = 400, description = "Not an id"), + (status = 401, description = "No valid bearer token"), + ) +)] #[tracing::instrument(skip_all, fields(session_id = %id))] pub async fn get_attack_chain( Extension(agent): AgentExt, @@ -767,7 +884,18 @@ pub async fn get_attack_chain( })) } -/// GET /api/v1/pentest/sessions/:id/messages — Get messages for a session +/// A session's messages, a page at a time. +#[utoipa::path( + get, + path = "/api/v1/pentest/sessions/{id}/messages", + tag = "Pentest", + params(("id" = String, Path, description = "The session's id"), PaginationParams), + responses( + (status = 200, description = "A page of messages"), + (status = 400, description = "Not an id"), + (status = 401, description = "No valid bearer token"), + ) +)] #[tracing::instrument(skip_all, fields(session_id = %id))] pub async fn get_messages( Extension(agent): AgentExt, @@ -807,7 +935,18 @@ pub async fn get_messages( })) } -/// GET /api/v1/pentest/sessions/:id/findings — Get DAST findings for a pentest session +/// A session's findings, a page at a time. +#[utoipa::path( + get, + path = "/api/v1/pentest/sessions/{id}/findings", + tag = "Pentest", + params(("id" = String, Path, description = "The session's id"), PaginationParams), + responses( + (status = 200, description = "A page of findings"), + (status = 400, description = "Not an id"), + (status = 401, description = "No valid bearer token"), + ) +)] #[tracing::instrument(skip_all, fields(session_id = %id))] pub async fn get_session_findings( Extension(agent): AgentExt, diff --git a/compliance-agent/src/api/handlers/pentest_handlers/stats.rs b/compliance-agent/src/api/handlers/pentest_handlers/stats.rs index d849627..9643091 100644 --- a/compliance-agent/src/api/handlers/pentest_handlers/stats.rs +++ b/compliance-agent/src/api/handlers/pentest_handlers/stats.rs @@ -14,7 +14,16 @@ use super::super::dto::{collect_cursor_async, tenant_db, ApiResponse}; type AgentExt = Extension>; -/// GET /api/v1/pentest/stats — Aggregated pentest statistics +/// The tenant's pentest statistics. +#[utoipa::path( + get, + path = "/api/v1/pentest/stats", + tag = "Pentest", + responses( + (status = 200, description = "The statistics"), + (status = 401, description = "No valid bearer token"), + ) +)] #[tracing::instrument(skip_all)] pub async fn pentest_stats( Extension(agent): AgentExt, diff --git a/compliance-agent/src/api/handlers/pentest_handlers/stream.rs b/compliance-agent/src/api/handlers/pentest_handlers/stream.rs index 6080470..855eb68 100644 --- a/compliance-agent/src/api/handlers/pentest_handlers/stream.rs +++ b/compliance-agent/src/api/handlers/pentest_handlers/stream.rs @@ -19,10 +19,22 @@ use super::super::dto::{collect_cursor_async, tenant_db}; type AgentExt = Extension>; -/// GET /api/v1/pentest/sessions/:id/stream — SSE endpoint for real-time events +/// A session's events as they happen (server-sent events). /// /// Replays stored messages/nodes as initial burst, then subscribes to the /// broadcast channel for live updates. Sends keepalive comments every 15s. +#[utoipa::path( + get, + path = "/api/v1/pentest/sessions/{id}/stream", + tag = "Pentest", + params(("id" = String, Path, description = "The session's id")), + responses( + (status = 200, description = "The event stream (text/event-stream)"), + (status = 400, description = "Not an id"), + (status = 404, description = "No such session"), + (status = 401, description = "No valid bearer token"), + ) +)] #[tracing::instrument(skip_all, fields(session_id = %id))] pub async fn session_stream( Extension(agent): AgentExt, diff --git a/compliance-agent/src/api/routes.rs b/compliance-agent/src/api/routes.rs index aff4ade..92199b9 100644 --- a/compliance-agent/src/api/routes.rs +++ b/compliance-agent/src/api/routes.rs @@ -1,6 +1,6 @@ use axum::extract::DefaultBodyLimit; use axum::http::header::CONTENT_TYPE; -use axum::routing::{get, patch, post}; +use axum::routing::{get, patch}; use axum::Router; use utoipa::OpenApi; use utoipa_axum::router::{OpenApiRouter, UtoipaMethodRouterExt}; @@ -223,58 +223,22 @@ fn described_routes() -> OpenApiRouter { .routes(routes!(handlers::notifications::mark_read)) .routes(routes!(handlers::notifications::dismiss_notification)) // Pentest API endpoints - .route( - "/api/v1/pentest/lookup-repo", - get(handlers::pentest::lookup_repo), - ) - .route( - "/api/v1/pentest/sessions", - get(handlers::pentest::list_sessions).post(handlers::pentest::create_session), - ) - .route( - "/api/v1/pentest/sessions/{id}", - get(handlers::pentest::get_session), - ) - .route( - "/api/v1/pentest/sessions/{id}/chat", - post(handlers::pentest::send_message), - ) - .route( - "/api/v1/pentest/sessions/{id}/stop", - post(handlers::pentest::stop_session), - ) - .route( - "/api/v1/pentest/sessions/{id}/pause", - post(handlers::pentest::pause_session), - ) - .route( - "/api/v1/pentest/sessions/{id}/resume", - post(handlers::pentest::resume_session), - ) - .route( - "/api/v1/pentest/sessions/{id}/stream", - get(handlers::pentest::session_stream), - ) - .route( - "/api/v1/pentest/sessions/{id}/attack-chain", - get(handlers::pentest::get_attack_chain), - ) - .route( - "/api/v1/pentest/sessions/{id}/messages", - get(handlers::pentest::get_messages), - ) - .route( - "/api/v1/pentest/sessions/{id}/findings", - get(handlers::pentest::get_session_findings), - ) - .route( - "/api/v1/pentest/sessions/{id}/export", - post(handlers::pentest::export_session_report), - ) - .route( - "/api/v1/pentest/stats", - get(handlers::pentest::pentest_stats), - ) + .routes(routes!(handlers::pentest::lookup_repo)) + .routes(routes!( + handlers::pentest::list_sessions, + handlers::pentest::create_session + )) + .routes(routes!(handlers::pentest::get_session)) + .routes(routes!(handlers::pentest::send_message)) + .routes(routes!(handlers::pentest::stop_session)) + .routes(routes!(handlers::pentest::pause_session)) + .routes(routes!(handlers::pentest::resume_session)) + .routes(routes!(handlers::pentest::session_stream)) + .routes(routes!(handlers::pentest::get_attack_chain)) + .routes(routes!(handlers::pentest::get_messages)) + .routes(routes!(handlers::pentest::get_session_findings)) + .routes(routes!(handlers::pentest::export_session_report)) + .routes(routes!(handlers::pentest::pentest_stats)) // Webhook routes live on the separate webhook server (port 3002, // see crate::webhooks::server). The M7.2-C tenant-in-URL form is // `/webhook/{tenant_id}/{platform}/{repo_id}` and the handlers diff --git a/compliance-agent/src/api/undescribed_routes.txt b/compliance-agent/src/api/undescribed_routes.txt index a28a316..987b6fe 100644 --- a/compliance-agent/src/api/undescribed_routes.txt +++ b/compliance-agent/src/api/undescribed_routes.txt @@ -16,16 +16,3 @@ /api/v1/targets/{id}/cve-groups /api/v1/targets/{id}/cve-groups/vex /api/v1/targets/{id}/sbom-ecosystems -/api/v1/pentest/lookup-repo -/api/v1/pentest/sessions -/api/v1/pentest/sessions/{id} -/api/v1/pentest/sessions/{id}/chat -/api/v1/pentest/sessions/{id}/stop -/api/v1/pentest/sessions/{id}/pause -/api/v1/pentest/sessions/{id}/resume -/api/v1/pentest/sessions/{id}/stream -/api/v1/pentest/sessions/{id}/attack-chain -/api/v1/pentest/sessions/{id}/messages -/api/v1/pentest/sessions/{id}/findings -/api/v1/pentest/sessions/{id}/export -/api/v1/pentest/stats