-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
check API version #641
Merged
Merged
check API version #641
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a78b7d2
check API version
ghubertpalo 9bf947a
reject request with bad API version
ghubertpalo 43a876d
add custom HTTP reply
ghubertpalo 2aa5ca3
add header filter tests
ghubertpalo 4a5a989
add signer version error message
ghubertpalo 37bde81
add client version error message
ghubertpalo b1504ae
update protocol description
ghubertpalo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,9 +7,16 @@ use crate::DependencyManager; | |
use mithril_common::MITHRIL_API_VERSION; | ||
|
||
use reqwest::header::{HeaderMap, HeaderValue}; | ||
use reqwest::StatusCode; | ||
use std::sync::Arc; | ||
use warp::http::Method; | ||
use warp::Filter; | ||
use warp::reject::Reject; | ||
use warp::{Filter, Rejection, Reply}; | ||
|
||
#[derive(Debug)] | ||
pub struct VersionMismatchError; | ||
|
||
impl Reject for VersionMismatchError {} | ||
|
||
/// Routes | ||
pub fn routes( | ||
|
@@ -24,14 +31,75 @@ pub fn routes( | |
"mithril-api-version", | ||
HeaderValue::from_static(MITHRIL_API_VERSION), | ||
); | ||
warp::any() | ||
.and(header_must_be()) | ||
.and(warp::path(SERVER_BASE_PATH)) | ||
.and( | ||
certificate_routes::routes(dependency_manager.clone()) | ||
.or(snapshot_routes::routes(dependency_manager.clone())) | ||
.or(signer_routes::routes(dependency_manager.clone())) | ||
.or(signatures_routes::routes(dependency_manager.clone())) | ||
.or(epoch_routes::routes(dependency_manager)) | ||
.with(cors), | ||
) | ||
.recover(handle_custom) | ||
.with(warp::reply::with::headers(headers)) | ||
} | ||
|
||
/// API Version verification | ||
fn header_must_be() -> impl Filter<Extract = (), Error = Rejection> + Copy { | ||
warp::header::optional("mithril-api-version") | ||
.and_then(|maybe_header: Option<String>| async move { | ||
match maybe_header { | ||
None => Ok(()), | ||
Some(version) if version == MITHRIL_API_VERSION => Ok(()), | ||
Some(_version) => Err(warp::reject::custom(VersionMismatchError)), | ||
} | ||
}) | ||
.untuple_one() | ||
} | ||
|
||
pub async fn handle_custom(reject: Rejection) -> Result<impl Reply, Rejection> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. handle custom rejection codes There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not need to be public |
||
if reject.find::<VersionMismatchError>().is_some() { | ||
Ok(StatusCode::PRECONDITION_FAILED) | ||
} else { | ||
Err(reject) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[tokio::test] | ||
async fn test_no_version() { | ||
let filters = header_must_be(); | ||
warp::test::request() | ||
.path("/aggregator/whatever") | ||
.filter(&filters) | ||
.await | ||
.unwrap(); | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_bad_version() { | ||
let filters = header_must_be(); | ||
warp::test::request() | ||
.header("mithril-api-version", "0.0.999") | ||
.path("/aggregator/whatever") | ||
.filter(&filters) | ||
.await | ||
.unwrap_err(); | ||
} | ||
|
||
warp::any().and(warp::path(SERVER_BASE_PATH)).and( | ||
certificate_routes::routes(dependency_manager.clone()) | ||
.or(snapshot_routes::routes(dependency_manager.clone())) | ||
.or(signer_routes::routes(dependency_manager.clone())) | ||
.or(signatures_routes::routes(dependency_manager.clone())) | ||
.or(epoch_routes::routes(dependency_manager)) | ||
.with(cors) | ||
.with(warp::reply::with::headers(headers)), | ||
) | ||
#[tokio::test] | ||
async fn test_good_version() { | ||
let filters = header_must_be(); | ||
warp::test::request() | ||
.header("mithril-api-version", MITHRIL_API_VERSION) | ||
.path("/aggregator/whatever") | ||
.filter(&filters) | ||
.await | ||
.unwrap(); | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
change the comment according to what the function does