feat(api): the OpenAPI description covers the pentest routes (#1006) (#1024)
CI / Check (push) Skipped
CI / Agent integration (push) Skipped
CI / Detect Changes (push) Successful in 4s
CI / Deploy Dashboard (push) Skipped
CI / Deploy Docs (push) Skipped
CI / Deploy MCP (push) Skipped
CI / Deploy Agent (push) Successful in 6m1s

This commit was merged in pull request #1024.
This commit is contained in:
sharang committed 2026-11-16 22:25:56 +00:00
1 parent 55091fe02e
commit d679f9cbf1
6 files changed
+210 -84

No files matched your search

@@ -21,8 +21,10 @@ use super::super::dto::{collect_cursor_async, tenant_db};
type AgentExt = Extension<Arc<ComplianceAgent>>; type AgentExt = Extension<Arc<ComplianceAgent>>;
#[derive(Deserialize)] /// The body of [`export_session_report`].
#[derive(Deserialize, utoipa::ToSchema)]
pub struct ExportBody { pub struct ExportBody {
/// The password the archive is encrypted with.
pub password: String, pub password: String,
/// Requester display name (from auth) /// Requester display name (from auth)
#[serde(default)] #[serde(default)]
@@ -32,7 +34,20 @@ pub struct ExportBody {
pub requester_email: String, 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))] #[tracing::instrument(skip_all, fields(session_id = %id))]
pub async fn export_session_report( pub async fn export_session_report(
Extension(agent): AgentExt, Extension(agent): AgentExt,
@@ -17,13 +17,18 @@ use super::super::dto::{collect_cursor_async, tenant_db, ApiResponse, Pagination
type AgentExt = Extension<Arc<ComplianceAgent>>; type AgentExt = Extension<Arc<ComplianceAgent>>;
#[derive(Deserialize)] /// The body of [`create_session`].
#[derive(Deserialize, utoipa::ToSchema)]
pub struct CreateSessionRequest { pub struct CreateSessionRequest {
/// The target to test, for a session without `config`.
pub target_id: Option<String>, pub target_id: Option<String>,
/// `quick`, `comprehensive` (default), `targeted` or `aggressive`.
#[serde(default = "default_strategy")] #[serde(default = "default_strategy")]
pub strategy: String, pub strategy: String,
/// The first instruction.
pub message: Option<String>, pub message: Option<String>,
/// Wizard configuration — if present, takes precedence over legacy fields /// Wizard configuration — if present, takes precedence over legacy fields
#[schema(value_type = Option<Object>)]
pub config: Option<PentestConfig>, pub config: Option<PentestConfig>,
} }
@@ -31,17 +36,37 @@ fn default_strategy() -> String {
"comprehensive".to_string() "comprehensive".to_string()
} }
#[derive(Deserialize)] /// The body of [`send_message`].
#[derive(Deserialize, utoipa::ToSchema)]
pub struct SendMessageRequest { pub struct SendMessageRequest {
/// The message.
pub message: String, pub message: String,
} }
#[derive(Deserialize)] /// The query of [`lookup_repo`].
#[derive(Deserialize, utoipa::IntoParams)]
#[into_params(parameter_in = Query)]
pub struct LookupRepoQuery { pub struct LookupRepoQuery {
/// The repository's git URL.
pub url: String, 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)] #[tracing::instrument(skip_all)]
pub async fn create_session( pub async fn create_session(
Extension(agent): AgentExt, 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)] #[tracing::instrument(skip_all)]
pub async fn lookup_repo( pub async fn lookup_repo(
Extension(agent): AgentExt, 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)] #[tracing::instrument(skip_all)]
pub async fn list_sessions( pub async fn list_sessions(
Extension(agent): AgentExt, 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))] #[tracing::instrument(skip_all, fields(session_id = %id))]
pub async fn get_session( pub async fn get_session(
Extension(agent): AgentExt, 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))] #[tracing::instrument(skip_all, fields(session_id = %id))]
pub async fn send_message( pub async fn send_message(
Extension(agent): AgentExt, 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))] #[tracing::instrument(skip_all, fields(session_id = %id))]
pub async fn stop_session( pub async fn stop_session(
Extension(agent): AgentExt, 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))] #[tracing::instrument(skip_all, fields(session_id = %id))]
pub async fn pause_session( pub async fn pause_session(
Extension(agent): AgentExt, 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))] #[tracing::instrument(skip_all, fields(session_id = %id))]
pub async fn resume_session( pub async fn resume_session(
Extension(agent): AgentExt, Extension(agent): AgentExt,
@@ -727,7 +833,18 @@ pub async fn resume_session(
/// being loaded whole into agent memory. /// being loaded whole into agent memory.
const MAX_ATTACK_CHAIN_NODES: i64 = 2_000; 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))] #[tracing::instrument(skip_all, fields(session_id = %id))]
pub async fn get_attack_chain( pub async fn get_attack_chain(
Extension(agent): AgentExt, 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))] #[tracing::instrument(skip_all, fields(session_id = %id))]
pub async fn get_messages( pub async fn get_messages(
Extension(agent): AgentExt, 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))] #[tracing::instrument(skip_all, fields(session_id = %id))]
pub async fn get_session_findings( pub async fn get_session_findings(
Extension(agent): AgentExt, Extension(agent): AgentExt,
@@ -14,7 +14,16 @@ use super::super::dto::{collect_cursor_async, tenant_db, ApiResponse};
type AgentExt = Extension<Arc<ComplianceAgent>>; type AgentExt = Extension<Arc<ComplianceAgent>>;
/// 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)] #[tracing::instrument(skip_all)]
pub async fn pentest_stats( pub async fn pentest_stats(
Extension(agent): AgentExt, Extension(agent): AgentExt,
@@ -19,10 +19,22 @@ use super::super::dto::{collect_cursor_async, tenant_db};
type AgentExt = Extension<Arc<ComplianceAgent>>; type AgentExt = Extension<Arc<ComplianceAgent>>;
/// 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 /// Replays stored messages/nodes as initial burst, then subscribes to the
/// broadcast channel for live updates. Sends keepalive comments every 15s. /// 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))] #[tracing::instrument(skip_all, fields(session_id = %id))]
pub async fn session_stream( pub async fn session_stream(
Extension(agent): AgentExt, Extension(agent): AgentExt,
+17 -53
View File
@@ -1,6 +1,6 @@
use axum::extract::DefaultBodyLimit; use axum::extract::DefaultBodyLimit;
use axum::http::header::CONTENT_TYPE; use axum::http::header::CONTENT_TYPE;
use axum::routing::{get, patch, post}; use axum::routing::{get, patch};
use axum::Router; use axum::Router;
use utoipa::OpenApi; use utoipa::OpenApi;
use utoipa_axum::router::{OpenApiRouter, UtoipaMethodRouterExt}; use utoipa_axum::router::{OpenApiRouter, UtoipaMethodRouterExt};
@@ -223,58 +223,22 @@ fn described_routes() -> OpenApiRouter {
.routes(routes!(handlers::notifications::mark_read)) .routes(routes!(handlers::notifications::mark_read))
.routes(routes!(handlers::notifications::dismiss_notification)) .routes(routes!(handlers::notifications::dismiss_notification))
// Pentest API endpoints // Pentest API endpoints
.route( .routes(routes!(handlers::pentest::lookup_repo))
"/api/v1/pentest/lookup-repo", .routes(routes!(
get(handlers::pentest::lookup_repo), handlers::pentest::list_sessions,
) handlers::pentest::create_session
.route( ))
"/api/v1/pentest/sessions", .routes(routes!(handlers::pentest::get_session))
get(handlers::pentest::list_sessions).post(handlers::pentest::create_session), .routes(routes!(handlers::pentest::send_message))
) .routes(routes!(handlers::pentest::stop_session))
.route( .routes(routes!(handlers::pentest::pause_session))
"/api/v1/pentest/sessions/{id}", .routes(routes!(handlers::pentest::resume_session))
get(handlers::pentest::get_session), .routes(routes!(handlers::pentest::session_stream))
) .routes(routes!(handlers::pentest::get_attack_chain))
.route( .routes(routes!(handlers::pentest::get_messages))
"/api/v1/pentest/sessions/{id}/chat", .routes(routes!(handlers::pentest::get_session_findings))
post(handlers::pentest::send_message), .routes(routes!(handlers::pentest::export_session_report))
) .routes(routes!(handlers::pentest::pentest_stats))
.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),
)
// Webhook routes live on the separate webhook server (port 3002, // Webhook routes live on the separate webhook server (port 3002,
// see crate::webhooks::server). The M7.2-C tenant-in-URL form is // see crate::webhooks::server). The M7.2-C tenant-in-URL form is
// `/webhook/{tenant_id}/{platform}/{repo_id}` and the handlers // `/webhook/{tenant_id}/{platform}/{repo_id}` and the handlers
@@ -16,16 +16,3 @@
/api/v1/targets/{id}/cve-groups /api/v1/targets/{id}/cve-groups
/api/v1/targets/{id}/cve-groups/vex /api/v1/targets/{id}/cve-groups/vex
/api/v1/targets/{id}/sbom-ecosystems /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