Skip to content

Commit

Permalink
Add: health endpoints to openAPI (#1515)
Browse files Browse the repository at this point in the history
  • Loading branch information
jjnicola authored Oct 24, 2023
1 parent 0899d3f commit a08fa02
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
24 changes: 24 additions & 0 deletions rust/doc/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,30 @@ paths:
type: "string"
description: "Header"

/health/alive:
get:
description: "Get application's health information"
operationId: "get_health_alive"
responses:
"200":
description: "Ok"
/health/ready:
get:
description: "Get application's health information"
operationId: "get_health_ready"
responses:
"200":
description: "Ok"
"503":
description: "Service Unavailable"
/health/started:
get:
description: "Get application's health information"
operationId: "get_health_started"
responses:
"200":
description: "Ok"

/scans:
post:
description: "This request just creates the scan. It can be started afterwards with the scan_action request."
Expand Down
31 changes: 31 additions & 0 deletions rust/openvasd/src/controller/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ use super::context::Context;
use hyper::{Body, Method, Request, Response};

use crate::scan::{self, Error, ScanDeleter, ScanStarter, ScanStopper};

enum HealthOpts {
/// Ready
Ready,
/// Started
Started,
/// Alive
Alive,
}
/// The supported paths of scannerd
enum KnownPaths {
/// /scans/{id}
Expand All @@ -22,6 +31,8 @@ enum KnownPaths {
ScanStatus(String),
/// /vts
Vts,
/// /health
Health(HealthOpts),
/// Not supported
Unknown,
}
Expand All @@ -44,6 +55,12 @@ impl KnownPaths {
None => KnownPaths::Scans(None),
},
Some("vts") => KnownPaths::Vts,
Some("health") => match parts.next() {
Some("ready") => KnownPaths::Health(HealthOpts::Ready),
Some("alive") => KnownPaths::Health(HealthOpts::Alive),
Some("started") => KnownPaths::Health(HealthOpts::Started),
_ => KnownPaths::Unknown,
}
_ => {
tracing::trace!("Unknown path: {path}");
KnownPaths::Unknown
Expand All @@ -64,6 +81,9 @@ impl Display for KnownPaths {
KnownPaths::ScanStatus(id) => write!(f, "/scans/{}/status", id),
KnownPaths::Unknown => write!(f, "Unknown"),
KnownPaths::Vts => write!(f, "/vts"),
KnownPaths::Health(HealthOpts::Alive) => write!(f, "/health/alive"),
KnownPaths::Health(HealthOpts::Ready) => write!(f, "/health/ready"),
KnownPaths::Health(HealthOpts::Started) => write!(f, "/health/started"),
}
}
}
Expand Down Expand Up @@ -113,6 +133,17 @@ where
}

match (req.method(), kp) {
(&Method::GET, Health(HealthOpts::Alive)) |
(&Method::GET, Health(HealthOpts::Started)) =>
Ok(ctx.response.empty(hyper::StatusCode::OK)),
(&Method::GET, Health(HealthOpts::Ready)) => {
let oids = ctx.db.oids().await?;
if oids.count() == 0 {
Ok(ctx.response.empty(hyper::StatusCode::SERVICE_UNAVAILABLE))
} else {
Ok(ctx.response.empty(hyper::StatusCode::OK))
}
}
(&Method::POST, Scans(None)) => {
match crate::request::json_request::<models::Scan>(&ctx.response, req).await {
Ok(mut scan) => {
Expand Down

0 comments on commit a08fa02

Please sign in to comment.