Files
pruefwerk/compliance-agent/src/api/openapi.rs
T
sharang 32332f65bb
CI / Check (push) Skipped
CI / Agent integration (push) Skipped
CI / Detect Changes (push) Successful in 3s
CI / Deploy Docs (push) Successful in 1m11s
CI / Deploy MCP (push) Successful in 4m56s
CI / Deploy Dashboard (push) Successful in 6m45s
CI / Deploy Agent (push) Successful in 8m52s
feat(api): the agent serves an OpenAPI description, starting with DAST (#1006) (#1012)
2026-11-16 13:10:26 +00:00

74 lines
2.6 KiB
Rust

//! The OpenAPI description of the customer API (#1006).
//!
//! Generated from the handlers: a route registered through
//! [`utoipa_axum::routes!`] takes its path, method, parameters and body from
//! the handler's `#[utoipa::path]`, so the description cannot drift from the
//! route. The document is served at [`SPEC_PATH`], behind the same
//! authentication as the API, for integrators and for DAST, which seeds its
//! endpoints from it (#997).
use utoipa::openapi::security::{HttpAuthScheme, HttpBuilder, SecurityRequirement, SecurityScheme};
use utoipa::OpenApi;
/// Where the agent serves its OpenAPI document. Under the API prefix, so the
/// customer authentication covers it, and one of the paths DAST tries.
pub const SPEC_PATH: &str = "/api/v1/openapi.json";
/// The name of the bearer scheme in the document.
const BEARER: &str = "bearer";
/// The document's frame; the routes add their paths to it.
#[derive(OpenApi)]
#[openapi(info(
title = "Prüfwerk API",
description = "The customer API of the Prüfwerk agent. Every operation but the health \
check needs a Keycloak access token or an API key as a bearer token."
))]
pub struct ApiDoc;
/// `api` with the bearer scheme, required by every operation except the
/// public ones ([`compliance_core::auth::bypasses_customer_auth`]).
///
/// The scheme goes on each operation rather than once for the document: a
/// reader that only looks at operations (the DAST spec parser) still sees it.
///
/// # Arguments
///
/// * `api` - the document with every described route's path
///
/// # Returns
///
/// The finished document.
pub fn with_bearer_auth(mut api: utoipa::openapi::OpenApi) -> utoipa::openapi::OpenApi {
api.components
.get_or_insert_with(Default::default)
.add_security_scheme(
BEARER,
SecurityScheme::Http(
HttpBuilder::new()
.scheme(HttpAuthScheme::Bearer)
.description(Some("A Keycloak access token or an API key"))
.build(),
),
);
for (path, item) in api.paths.paths.iter_mut() {
if compliance_core::auth::bypasses_customer_auth(path) {
continue;
}
let operations = [
&mut item.get,
&mut item.put,
&mut item.post,
&mut item.delete,
&mut item.options,
&mut item.head,
&mut item.patch,
&mut item.trace,
];
for operation in operations.into_iter().flatten() {
operation.security = Some(vec![SecurityRequirement::new(BEARER, Vec::<String>::new())]);
}
}
api
}