diff --git a/compliance-agent/src/api/handlers/evidence.rs b/compliance-agent/src/api/handlers/evidence.rs index 644ce72..abef555 100644 --- a/compliance-agent/src/api/handlers/evidence.rs +++ b/compliance-agent/src/api/handlers/evidence.rs @@ -112,9 +112,19 @@ fn state_counts(controls: &[crate::evidence::compliance::ControlStatus]) -> serd /// Targets whose CRA status is computed at the same time for the summary. const SUMMARY_CONCURRENCY: usize = 4; -/// GET /api/v1/compliance/summary — every target's CRA state counts, for the -/// overview. A target too large to evaluate in memory is listed with -/// `counts: null` rather than failing the whole summary. +/// Every target's CRA control counts per state, for the overview. +/// +/// A target too large to evaluate in memory is listed with `counts: null` +/// rather than failing the whole summary. +#[utoipa::path( + get, + path = "/api/v1/compliance/summary", + tag = "Compliance", + responses( + (status = 200, description = "The counts per target"), + (status = 401, description = "No valid bearer token"), + ) +)] #[tracing::instrument(skip_all)] pub async fn compliance_summary( Extension(agent): AgentExt, @@ -158,8 +168,22 @@ pub async fn compliance_summary( Ok(Json(serde_json::json!({ "data": rows }))) } -/// GET /api/v1/targets/{id}/compliance — the target's state against every CRA -/// Annex I control, with the findings against each and the last scan. +/// A target's state against every CRA Annex I control. +/// +/// With the findings against each and the last scan. +#[utoipa::path( + get, + path = "/api/v1/targets/{id}/compliance", + tag = "Compliance", + params(("id" = String, Path, description = "The target's id")), + responses( + (status = 200, description = "The state per control"), + (status = 400, description = "Not an id"), + (status = 404, description = "No such target"), + (status = 413, description = "Too large to evaluate in memory"), + (status = 401, description = "No valid bearer token"), + ) +)] #[tracing::instrument(skip_all, fields(target_id = %id))] pub async fn compliance_view( Extension(agent): AgentExt, @@ -185,8 +209,22 @@ pub async fn compliance_view( }))) } -/// GET /api/v1/targets/{id}/evidence-pack — 404 for an unknown target, 413 -/// when it is too large to build in memory. +/// A target's evidence pack to download. +/// +/// 404 for an unknown target, 413 when it is too large to build in memory. +#[utoipa::path( + get, + path = "/api/v1/targets/{id}/evidence-pack", + tag = "Compliance", + params(("id" = String, Path, description = "The target's id")), + responses( + (status = 200, description = "The evidence pack"), + (status = 400, description = "Not an id"), + (status = 404, description = "No such target"), + (status = 413, description = "Too large to build in memory"), + (status = 401, description = "No valid bearer token"), + ) +)] #[tracing::instrument(skip_all, fields(target_id = %id))] pub async fn evidence_pack( Extension(agent): AgentExt, @@ -231,14 +269,28 @@ pub async fn evidence_pack( const MAX_CONTROL_LOOKUP: usize = 50; /// The query of [`describe_controls`]: comma-separated control ids. -#[derive(Debug, serde::Deserialize)] +#[derive(Debug, serde::Deserialize, utoipa::IntoParams)] +#[into_params(parameter_in = Query)] pub struct ControlIdsQuery { + /// The master control ids, comma-separated, at most 50. + #[param(example = "mc-1,mc-2")] ids: String, } -/// GET /api/v1/controls?ids=mc-1,mc-2 — what the master controls stamped on -/// a finding require, for the finding page. Unknown ids are left out; at -/// most [`MAX_CONTROL_LOOKUP`] are described. +/// What the master controls stamped on a finding require. +/// +/// For the finding page. Unknown ids are left out; at most +/// [`MAX_CONTROL_LOOKUP`] are described. +#[utoipa::path( + get, + path = "/api/v1/controls", + tag = "Compliance", + params(ControlIdsQuery), + responses( + (status = 200, description = "The controls"), + (status = 401, description = "No valid bearer token"), + ) +)] #[tracing::instrument(skip_all)] pub async fn describe_controls( Extension(agent): AgentExt, diff --git a/compliance-agent/src/api/handlers/finding_breakdown.rs b/compliance-agent/src/api/handlers/finding_breakdown.rs index 86ee76a..f45d08e 100644 --- a/compliance-agent/src/api/handlers/finding_breakdown.rs +++ b/compliance-agent/src/api/handlers/finding_breakdown.rs @@ -18,11 +18,22 @@ fn open_findings_of(target_id: &str) -> mongodb::bson::Document { doc! { "repo_id": target_id, "status": { "$in": open } } } -/// The open findings of target `id`, counted by type and severity and by -/// folder. Tenant-scoped like the findings list, and read-only, so every +/// A target's open findings, counted by type and severity and by folder. +/// +/// Tenant-scoped like the findings list, and read-only, so every /// role that may read findings may read it. A target without open findings /// (or of another tenant, whose findings are not in this tenant's database) /// gets an empty breakdown. +#[utoipa::path( + get, + path = "/api/v1/targets/{id}/finding-breakdown", + tag = "Findings", + params(("id" = String, Path, description = "The target's id")), + responses( + (status = 200, description = "The breakdown"), + (status = 401, description = "No valid bearer token"), + ) +)] #[tracing::instrument(skip_all, fields(target_id = %id))] pub async fn finding_breakdown( Extension(agent): AgentExt, diff --git a/compliance-agent/src/api/handlers/manual_findings.rs b/compliance-agent/src/api/handlers/manual_findings.rs index 5c4a9f8..da224b4 100644 --- a/compliance-agent/src/api/handlers/manual_findings.rs +++ b/compliance-agent/src/api/handlers/manual_findings.rs @@ -46,9 +46,11 @@ impl From for ScanType { } } -/// `POST /api/v1/targets/{id}/findings`. -#[derive(Debug, Deserialize)] +/// The body of [`create_finding`]: a finding from a manual test. +#[derive(Debug, Deserialize, utoipa::ToSchema)] pub struct CreateFindingRequest { + /// `dast` or `pentest`: the manual test the finding comes from. + #[schema(value_type = String, example = "pentest")] pub kind: ManualKind, /// The tool used, if any; `manual` otherwise. #[serde(default)] @@ -66,9 +68,14 @@ pub enum ImportFormat { } /// `?format=&kind=` of an import. -#[derive(Debug, Deserialize)] +#[derive(Debug, Deserialize, utoipa::IntoParams)] +#[into_params(parameter_in = Query)] pub struct ImportParams { + /// `sarif` or `zap`. + #[param(value_type = String, example = "sarif")] pub format: ImportFormat, + /// `dast` or `pentest`. + #[param(value_type = String, example = "dast")] pub kind: ManualKind, } @@ -124,8 +131,26 @@ async fn insert_new(db: &Database, finding: &mut Finding) -> Result Router { fn described_routes() -> OpenApiRouter { OpenApiRouter::with_openapi(openapi::ApiDoc::openapi()) .route("/api/v1/health", get(handlers::health)) - .route("/api/v1/oscal/assess", post(handlers::oscal::assess_target)) + .routes(routes!(handlers::oscal::assess_target)) .route("/api/v1/stats/overview", get(handlers::stats_overview)) // The tenant's projects (owned by the registry), for assigning targets. .route("/api/v1/projects", get(handlers::projects::list_projects)) @@ -154,34 +154,15 @@ fn described_routes() -> OpenApiRouter { "/api/v1/targets/{id}/sbom-ecosystems", get(handlers::sbom_ecosystems::list_sbom_ecosystems), ) - .route( - "/api/v1/targets/{id}/evidence-pack", - get(handlers::evidence::evidence_pack), - ) - .route( - "/api/v1/targets/{id}/compliance", - get(handlers::evidence::compliance_view), - ) - .route( - "/api/v1/targets/{id}/finding-breakdown", - get(handlers::finding_breakdown::finding_breakdown), - ) - .route( - "/api/v1/compliance/summary", - get(handlers::evidence::compliance_summary), - ) - .route( - "/api/v1/controls", - get(handlers::evidence::describe_controls), - ) + .routes(routes!(handlers::evidence::evidence_pack)) + .routes(routes!(handlers::evidence::compliance_view)) + .routes(routes!(handlers::finding_breakdown::finding_breakdown)) + .routes(routes!(handlers::evidence::compliance_summary)) + .routes(routes!(handlers::evidence::describe_controls)) // Findings from a manual DAST run or penetration test. - .route( - "/api/v1/targets/{id}/findings", - post(handlers::manual_findings::create_finding), - ) - .route( - "/api/v1/targets/{id}/findings/import", - post(handlers::manual_findings::import_findings).layer(DefaultBodyLimit::max( + .routes(routes!(handlers::manual_findings::create_finding)) + .routes( + routes!(handlers::manual_findings::import_findings).layer(DefaultBodyLimit::max( handlers::manual_findings::MAX_IMPORT_BYTES, )), ) diff --git a/compliance-agent/src/api/undescribed_routes.txt b/compliance-agent/src/api/undescribed_routes.txt index a2c65cf..4d89590 100644 --- a/compliance-agent/src/api/undescribed_routes.txt +++ b/compliance-agent/src/api/undescribed_routes.txt @@ -4,7 +4,6 @@ # such route, so the list only ever shrinks. Describe a route with # `#[utoipa::path]` and `.routes(routes!(...))`, then delete its line. /api/v1/health -/api/v1/oscal/assess /api/v1/stats/overview /api/v1/projects /api/v1/metering/usage @@ -30,13 +29,6 @@ /api/v1/targets/{id}/cve-groups /api/v1/targets/{id}/cve-groups/vex /api/v1/targets/{id}/sbom-ecosystems -/api/v1/targets/{id}/evidence-pack -/api/v1/targets/{id}/compliance -/api/v1/targets/{id}/finding-breakdown -/api/v1/compliance/summary -/api/v1/controls -/api/v1/targets/{id}/findings -/api/v1/targets/{id}/findings/import /api/v1/pentest/lookup-repo /api/v1/pentest/sessions /api/v1/pentest/sessions/{id} diff --git a/compliance-agent/src/import/mod.rs b/compliance-agent/src/import/mod.rs index 817d25d..65d2caa 100644 --- a/compliance-agent/src/import/mod.rs +++ b/compliance-agent/src/import/mod.rs @@ -18,15 +18,20 @@ const MAX_EVIDENCE_CHARS: usize = 4_000; /// What a finding from a manual test says, whether typed into the form or /// read from a report. -#[derive(Debug, Clone, Deserialize, PartialEq)] +#[derive(Debug, Clone, Deserialize, PartialEq, utoipa::ToSchema)] pub struct ManualFinding { + /// What was found. pub title: String, + /// The finding in detail. #[serde(default)] pub description: String, + /// `info`, `low`, `medium`, `high` or `critical`. + #[schema(value_type = String, example = "high")] pub severity: Severity, /// `CWE-79`, or just `79`. #[serde(default)] pub cwe: Option, + /// The CVSS base score. #[serde(default)] pub cvss_score: Option, /// Where it was found: a URL, an endpoint, a file. @@ -35,8 +40,10 @@ pub struct ManualFinding { /// A request or response excerpt that shows it. #[serde(default)] pub evidence: Option, + /// How to reproduce it. #[serde(default)] pub reproduction: Option, + /// How to fix it. #[serde(default)] pub remediation: Option, /// The tool's rule or plugin id, for an imported finding.