diff --git a/compliance-agent/src/api/handlers/dast.rs b/compliance-agent/src/api/handlers/dast.rs index 595d8df..fcd10bf 100644 --- a/compliance-agent/src/api/handlers/dast.rs +++ b/compliance-agent/src/api/handlers/dast.rs @@ -66,7 +66,9 @@ fn default_rate_limit() -> u32 { 10 } -/// List the tenant's DAST targets. Logins are never returned. +/// List the tenant's DAST targets. +/// +/// Logins are never returned. #[utoipa::path( get, path = "/api/v1/dast/targets", @@ -113,8 +115,10 @@ pub async fn list_targets( })) } -/// Add a DAST target. Its login is stored encrypted and never returned; the -/// answer is the new target with its id. +/// Add a DAST target. +/// +/// Its login is stored encrypted and never returned; the answer is the new +/// target with its id. #[utoipa::path( post, path = "/api/v1/dast/targets", @@ -178,8 +182,10 @@ pub async fn add_target( })) } -/// Start a DAST scan of a target. The scan runs in the background; its run -/// and findings appear under the scan-run and finding lists. +/// Start a DAST scan of a target. +/// +/// The scan runs in the background; its run and findings appear under the +/// scan-run and finding lists. #[utoipa::path( post, path = "/api/v1/dast/targets/{id}/scan", diff --git a/compliance-agent/src/api/handlers/dto.rs b/compliance-agent/src/api/handlers/dto.rs index d33d580..08e2258 100644 --- a/compliance-agent/src/api/handlers/dto.rs +++ b/compliance-agent/src/api/handlers/dto.rs @@ -55,20 +55,29 @@ pub(crate) fn de_limit<'de, D: serde::Deserializer<'de>>(d: D) -> Result, /// Only the findings a given scan run found first (the dashboard reveals /// them when the run ends). #[serde(default)] pub scan_run_id: Option, + /// `info`, `low`, `medium`, `high` or `critical`. #[serde(default)] pub severity: Option, + /// Only one kind of scan (`sast`, `cve`, `dast`, …). #[serde(default)] pub scan_type: Option, + /// `open`, `triaged`, `false_positive`, `resolved`, `ignored`, + /// `accepted_risk`, `needs_review` or `wrong_mapping`. #[serde(default)] pub status: Option, + /// Text the title, description or file path contains (at most 128 + /// characters, matched literally). #[serde(default)] pub q: Option, /// Only the findings of one scanner (`Finding::scanner`, e.g. @@ -83,13 +92,20 @@ pub struct FindingsFilter { /// ([`path_regex`]). #[serde(default)] pub path: Option, + /// `created_at` (default), `updated_at`, `severity`, `scan_type`, + /// `status`, `file_path`, `rule_id`, `repo_id`, `title` or `confidence`. #[serde(default)] pub sort_by: Option, + /// `asc`, or newest/highest first (default). #[serde(default)] pub sort_order: 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, } @@ -293,26 +309,37 @@ fn default_branch() -> String { "main".to_string() } -#[derive(Deserialize)] +/// A finding's new status. +#[derive(Deserialize, utoipa::ToSchema)] pub struct UpdateStatusRequest { - /// Typed, so an unknown status is refused (422) instead of being stored - /// and breaking every later read of the finding. + /// `open`, `triaged`, `false_positive`, `resolved`, `ignored`, + /// `accepted_risk`, `needs_review` or `wrong_mapping`. Typed, so an + /// unknown status is refused (422) instead of being stored and breaking + /// every later read of the finding. + #[schema(value_type = String, example = "false_positive")] pub status: compliance_core::models::FindingStatus, /// Why; required for `accepted_risk`. #[serde(default)] pub reason: Option, } -#[derive(Deserialize)] +/// The same new status for many findings. +#[derive(Deserialize, utoipa::ToSchema)] pub struct BulkUpdateStatusRequest { + /// The findings' ids. pub ids: Vec, + /// The status, as for one finding. + #[schema(value_type = String, example = "resolved")] pub status: compliance_core::models::FindingStatus, + /// Why; required for `accepted_risk` and `wrong_mapping`. #[serde(default)] pub reason: Option, } -#[derive(Deserialize)] +/// A developer's feedback on a finding. +#[derive(Deserialize, utoipa::ToSchema)] pub struct UpdateFeedbackRequest { + /// The feedback, as text. pub feedback: String, } diff --git a/compliance-agent/src/api/handlers/finding_groups.rs b/compliance-agent/src/api/handlers/finding_groups.rs index 70736cf..55521bd 100644 --- a/compliance-agent/src/api/handlers/finding_groups.rs +++ b/compliance-agent/src/api/handlers/finding_groups.rs @@ -22,7 +22,8 @@ use compliance_core::models::{Finding, FindingStatus}; use compliance_core::tenant_ctx::TenantCtx; /// The query of [`list_finding_groups`]. -#[derive(Debug, Deserialize)] +#[derive(Debug, Deserialize, utoipa::IntoParams)] +#[into_params(parameter_in = Query)] pub struct GroupsQuery { /// Only one target's findings (`Finding::repo_id`). #[serde(default)] @@ -30,11 +31,13 @@ pub struct GroupsQuery { } /// The body of [`decide_finding_group`]. -#[derive(Debug, Deserialize)] +#[derive(Debug, Deserialize, utoipa::ToSchema)] pub struct DecideGroupRequest { /// The group, as [`list_finding_groups`] named it. + #[schema(value_type = Object)] pub key: GroupKey, - /// The decision for every member. + /// The decision for every member, a finding status. + #[schema(value_type = String, example = "false_positive")] pub status: FindingStatus, /// Why; required for an accepted risk or a wrong mapping. #[serde(default)] @@ -71,9 +74,19 @@ async fn undecided_findings( Ok(collect_cursor_async(cursor).await) } -/// GET /api/v1/finding-groups[?target=] — the undecided findings, grouped -/// by rule and file (see [`compliance_core::models::finding_group`]), most -/// severe first. +/// List the undecided findings, grouped by rule and file. +/// +/// Most severe first (see [`compliance_core::models::finding_group`]). +#[utoipa::path( + get, + path = "/api/v1/finding-groups", + tag = "Findings", + params(GroupsQuery), + responses( + (status = 200, description = "The groups, most severe first"), + (status = 401, description = "No valid bearer token"), + ) +)] #[tracing::instrument(skip_all, fields(target = ?query.target))] pub async fn list_finding_groups( Extension(agent): AgentExt, @@ -134,12 +147,25 @@ fn candidates_filter(key: &GroupKey) -> Document { } } -/// POST /api/v1/finding-groups/decide — give every undecided member of a -/// group the same status, as if each were changed on its own. +/// Decide every undecided finding of a group at once. +/// +/// Each member gets the same status, as if each were changed on its own. /// /// The members are found again from the key, so a member someone decided /// in the meantime keeps that decision. 404 when the group has no undecided /// member (any more), 422 as [`decision_update`]. +#[utoipa::path( + post, + path = "/api/v1/finding-groups/decide", + tag = "Findings", + request_body = DecideGroupRequest, + responses( + (status = 200, description = "The members were decided"), + (status = 404, description = "The group has no undecided member"), + (status = 422, description = "An unknown status, or no reason for one that needs it"), + (status = 401, description = "No valid bearer token"), + ) +)] #[tracing::instrument(skip_all, fields(rule = %req.key.rule, scope = ?req.key.scope))] pub async fn decide_finding_group( Extension(agent): AgentExt, diff --git a/compliance-agent/src/api/handlers/findings.rs b/compliance-agent/src/api/handlers/findings.rs index 769bba8..c5eae81 100644 --- a/compliance-agent/src/api/handlers/findings.rs +++ b/compliance-agent/src/api/handlers/findings.rs @@ -7,6 +7,17 @@ use super::dto::*; use compliance_core::models::{Finding, FindingStatus, StatusChange}; use compliance_core::tenant_ctx::TenantCtx; +/// List the tenant's findings, filtered, sorted and a page at a time. +#[utoipa::path( + get, + path = "/api/v1/findings", + tag = "Findings", + params(FindingsFilter), + responses( + (status = 200, description = "A page of findings"), + (status = 401, description = "No valid bearer token"), + ) +)] #[tracing::instrument(skip_all, fields(repo_id = ?filter.repo_id, severity = ?filter.severity, scan_type = ?filter.scan_type))] pub async fn list_findings( Extension(agent): AgentExt, @@ -144,15 +155,27 @@ fn severity_rank() -> mongodb::bson::Document { } /// The query of [`list_finding_variants`]. -#[derive(Debug, serde::Deserialize)] +#[derive(Debug, serde::Deserialize, utoipa::IntoParams)] +#[into_params(parameter_in = Query)] pub struct VariantsQuery { /// Only one target's variants. #[serde(default)] pub repo_id: Option, } -/// The app variants the tenant's findings are labelled with (theirs and their -/// groups' places), sorted, for the findings filter. +/// List the app variants the tenant's findings are in. +/// +/// Their own and their groups' places, sorted, for the findings filter. +#[utoipa::path( + get, + path = "/api/v1/findings/variants", + tag = "Findings", + params(VariantsQuery), + responses( + (status = 200, description = "The app variant names, sorted"), + (status = 401, description = "No valid bearer token"), + ) +)] #[tracing::instrument(skip_all, fields(repo_id = ?query.repo_id))] pub async fn list_finding_variants( Extension(agent): AgentExt, @@ -183,6 +206,19 @@ pub async fn list_finding_variants( })) } +/// One finding. +#[utoipa::path( + get, + path = "/api/v1/findings/{id}", + tag = "Findings", + params(("id" = String, Path, description = "The finding's id")), + responses( + (status = 200, description = "The finding"), + (status = 400, description = "Not an id"), + (status = 404, description = "No such finding"), + (status = 401, description = "No valid bearer token"), + ) +)] #[tracing::instrument(skip_all, fields(finding_id = %id))] pub async fn get_finding( Extension(agent): AgentExt, @@ -273,8 +309,23 @@ fn issue_sync( } } -/// PATCH /api/v1/findings/{id}/status — change the status, recording who, -/// when and why. 404 when there is no such finding. +/// Change a finding's status. +/// +/// Records who, when and why. 404 when there is no such finding. +#[utoipa::path( + patch, + path = "/api/v1/findings/{id}/status", + tag = "Findings", + params(("id" = String, Path, description = "The finding's id")), + request_body = UpdateStatusRequest, + responses( + (status = 200, description = "The status was changed"), + (status = 400, description = "Not an id"), + (status = 404, description = "No such finding"), + (status = 422, description = "An unknown status, or no reason for one that needs it"), + (status = 401, description = "No valid bearer token"), + ) +)] #[tracing::instrument(skip_all, fields(finding_id = %id))] pub async fn update_finding_status( Extension(agent): AgentExt, @@ -299,7 +350,19 @@ pub async fn update_finding_status( Ok(Json(serde_json::json!({ "status": "updated" }))) } -/// PATCH /api/v1/findings/bulk-status — the same change on many findings. +/// Change the status of many findings at once. +#[utoipa::path( + patch, + path = "/api/v1/findings/bulk-status", + tag = "Findings", + request_body = BulkUpdateStatusRequest, + responses( + (status = 200, description = "How many findings changed"), + (status = 400, description = "An id that is not one"), + (status = 422, description = "An unknown status, or no reason for one that needs it"), + (status = 401, description = "No valid bearer token"), + ) +)] #[tracing::instrument(skip_all)] pub async fn bulk_update_finding_status( Extension(agent): AgentExt, @@ -354,8 +417,22 @@ pub(crate) async fn apply_status( Ok(result.modified_count) } -/// PATCH /api/v1/findings/{id}/feedback — store the developer's feedback on a -/// finding. 404 when there is no such finding. +/// Store a developer's feedback on a finding. +/// +/// 404 when there is no such finding. +#[utoipa::path( + patch, + path = "/api/v1/findings/{id}/feedback", + tag = "Findings", + params(("id" = String, Path, description = "The finding's id")), + request_body = UpdateFeedbackRequest, + responses( + (status = 200, description = "The feedback was stored"), + (status = 400, description = "Not an id"), + (status = 404, description = "No such finding"), + (status = 401, description = "No valid bearer token"), + ) +)] #[tracing::instrument(skip_all, fields(finding_id = %id))] pub async fn update_finding_feedback( Extension(agent): AgentExt, diff --git a/compliance-agent/src/api/routes.rs b/compliance-agent/src/api/routes.rs index d9cd0e2..b0c7426 100644 --- a/compliance-agent/src/api/routes.rs +++ b/compliance-agent/src/api/routes.rs @@ -119,33 +119,15 @@ fn described_routes() -> OpenApiRouter { "/api/v1/targets/{id}/billing/swap", post(handlers::onboarding::swap_target_billing), ) - .route("/api/v1/findings", get(handlers::list_findings)) - .route( - "/api/v1/findings/variants", - get(handlers::list_finding_variants), - ) - .route("/api/v1/findings/{id}", get(handlers::get_finding)) - .route( - "/api/v1/findings/{id}/status", - patch(handlers::update_finding_status), - ) - .route( - "/api/v1/findings/bulk-status", - patch(handlers::bulk_update_finding_status), - ) - .route( - "/api/v1/findings/{id}/feedback", - patch(handlers::update_finding_feedback), - ) + .routes(routes!(handlers::list_findings)) + .routes(routes!(handlers::list_finding_variants)) + .routes(routes!(handlers::get_finding)) + .routes(routes!(handlers::update_finding_status)) + .routes(routes!(handlers::bulk_update_finding_status)) + .routes(routes!(handlers::update_finding_feedback)) // Undecided findings grouped by rule and file; one decision per group (#951). - .route( - "/api/v1/finding-groups", - get(handlers::finding_groups::list_finding_groups), - ) - .route( - "/api/v1/finding-groups/decide", - post(handlers::finding_groups::decide_finding_group), - ) + .routes(routes!(handlers::finding_groups::list_finding_groups)) + .routes(routes!(handlers::finding_groups::decide_finding_group)) // The "Heute" inbox (#950): open decisions in urgency buckets. .route("/api/v1/inbox", get(handlers::inbox::get_inbox)) .route("/api/v1/inbox/decide", post(handlers::inbox::decide)) @@ -498,6 +480,47 @@ mod tests { ); } + /// Every described operation has a one-line summary (the first paragraph + /// of the handler's doc comment, without the route), and every `{name}` + /// in its path a path parameter of that name: a reader can call it from + /// the description alone. + #[test] + fn every_described_operation_is_complete() { + let (_, api) = described_routes().split_for_parts(); + let doc: serde_json::Value = serde_json::from_str(&api.to_json().unwrap()).unwrap(); + let placeholder = regex::Regex::new(r"\{([^}]+)\}").unwrap(); + + for (path, item) in doc["paths"].as_object().unwrap() { + for (method, operation) in item.as_object().unwrap() { + let summary = operation["summary"].as_str().unwrap_or_default(); + assert!( + !summary.is_empty() + && !summary.contains('\n') + && !["GET ", "POST ", "PUT ", "PATCH ", "DELETE "] + .iter() + .any(|m| summary.starts_with(m)) + && !summary.contains("/api/"), + "{method} {path}: the summary is the doc comment's first line, \ + a short sentence without the route: {summary:?}" + ); + let in_path: Vec<&str> = operation["parameters"] + .as_array() + .into_iter() + .flatten() + .filter(|p| p["in"] == "path") + .filter_map(|p| p["name"].as_str()) + .collect(); + for name in placeholder.captures_iter(path) { + assert!( + in_path.contains(&&name[1]), + "{method} {path} has no path parameter {}", + &name[1] + ); + } + } + } + } + #[test] fn the_description_is_behind_the_customer_authentication() { assert!(!compliance_core::auth::bypasses_customer_auth( diff --git a/compliance-agent/src/api/undescribed_routes.txt b/compliance-agent/src/api/undescribed_routes.txt index 0675471..61a0469 100644 --- a/compliance-agent/src/api/undescribed_routes.txt +++ b/compliance-agent/src/api/undescribed_routes.txt @@ -23,14 +23,6 @@ /api/v1/targets/{id}/detect /api/v1/targets/{id}/scan /api/v1/targets/{id}/billing/swap -/api/v1/findings -/api/v1/findings/variants -/api/v1/findings/{id} -/api/v1/findings/{id}/status -/api/v1/findings/bulk-status -/api/v1/findings/{id}/feedback -/api/v1/finding-groups -/api/v1/finding-groups/decide /api/v1/inbox /api/v1/inbox/decide /api/v1/inbox/undo