-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(service): schema mismatch disables service (#198)
* feat: refactor State to hide inner vars, expose functionality via methods Adds `State::event_db()` which returns an optional handle to the DB connection pool Adds `State::schema_version_check()` which retuns the result of the schema version check query Adds `State::is_schema_version_status(svs)` which compares the inner value with the argument Adds `State::set_schema_version_status(svs)` which sets the inner value with the argument * feat: add SchemaVersionValidation middleware Add middleware type to check the State's schema version status variable, if a mismatch is detected, a `503 Service unavailable` response is returned. * fix: update GET /health/ready endpoint * feat: update endpoint to use 'State::event_db()' method * feat: add SchemaVersionValidation middleware to existing API endpoints * fix: spelling
- Loading branch information
1 parent
63df50d
commit 930c2ce
Showing
9 changed files
with
193 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
//! Custom POEM Middleware for this service. | ||
pub(crate) mod schema_validation; | ||
pub(crate) mod tracing_mw; |
62 changes: 62 additions & 0 deletions
62
catalyst-gateway/bin/src/service/utilities/middleware/schema_validation.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
//! Middleware to verify the status of the last DB schema version validation. | ||
//! | ||
//! If a mismatch is detected, the middleware returns an error with `ServiceUnavailable` | ||
//! status code (503). Otherwise, the middleware calls and returns the wrapped endpoint's | ||
//! response. | ||
//! | ||
//! This middleware checks the `State.schema_version_status` value, if it is Ok, | ||
//! the wrapped endpoint is called and its response is returned. | ||
use std::sync::Arc; | ||
|
||
use poem::{web::Data, Endpoint, EndpointExt, Middleware, Request, Result}; | ||
|
||
use crate::{ | ||
service::common::responses::resp_5xx::ServiceUnavailable, | ||
state::{SchemaVersionStatus, State}, | ||
}; | ||
|
||
/// A middleware that raises an error with `ServiceUnavailable` and 503 status code | ||
/// if a DB schema version mismatch is found the existing `State`. | ||
pub(crate) struct SchemaVersionValidation; | ||
|
||
impl<E: Endpoint> Middleware<E> for SchemaVersionValidation { | ||
type Output = SchemaVersionValidationImpl<E>; | ||
|
||
fn transform(&self, ep: E) -> Self::Output { | ||
SchemaVersionValidationImpl { ep } | ||
} | ||
} | ||
|
||
/// The new endpoint type generated by the `SchemaVersionValidation`. | ||
pub(crate) struct SchemaVersionValidationImpl<E> { | ||
/// Endpoint wrapped by the middleware. | ||
ep: E, | ||
} | ||
|
||
#[poem::async_trait] | ||
impl<E: Endpoint> Endpoint for SchemaVersionValidationImpl<E> { | ||
type Output = E::Output; | ||
|
||
async fn call(&self, req: Request) -> Result<Self::Output> { | ||
if let Some(state) = req.data::<Data<&Arc<State>>>() { | ||
// Check if the inner schema version status is set to `Mismatch`, | ||
// if so, return the `ServiceUnavailable` error, which implements | ||
// `ResponseError`, with status code `503`. | ||
// Otherwise, return the endpoint as usual. | ||
if state.is_schema_version_status(&SchemaVersionStatus::Mismatch) { | ||
return Err(ServiceUnavailable.into()); | ||
} | ||
} | ||
// Calls the endpoint with the request, and returns the response. | ||
self.ep.call(req).await | ||
} | ||
} | ||
|
||
/// A function that wraps an endpoint with the `SchemaVersionValidation`. | ||
/// | ||
/// This function is convenient to use with `poem-openapi` [operation parameters](https://docs.rs/poem-openapi/latest/poem_openapi/attr.OpenApi.html#operation-parameters) via the | ||
/// `transform` attribute. | ||
pub(crate) fn schema_version_validation(ep: impl Endpoint) -> impl Endpoint { | ||
ep.with(SchemaVersionValidation) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters