Skip to content

Commit

Permalink
Use a constant for API version header
Browse files Browse the repository at this point in the history
  • Loading branch information
jpraynaud committed Jan 26, 2023
1 parent 5311b29 commit 1653587
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 19 deletions.
14 changes: 8 additions & 6 deletions mithril-aggregator/src/http_server/routes/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use crate::http_server::routes::{
use crate::http_server::SERVER_BASE_PATH;
use crate::DependencyManager;

use mithril_common::{MITHRIL_API_VERSION, MITHRIL_API_VERSION_REQUIREMENT};
use mithril_common::{
MITHRIL_API_VERSION, MITHRIL_API_VERSION_HEADER, MITHRIL_API_VERSION_REQUIREMENT,
};

use reqwest::header::{HeaderMap, HeaderValue};
use reqwest::StatusCode;
Expand Down Expand Up @@ -34,7 +36,7 @@ pub fn routes(
.allow_methods(vec![Method::GET, Method::POST, Method::OPTIONS]);
let mut headers = HeaderMap::new();
headers.insert(
"mithril-api-version",
MITHRIL_API_VERSION_HEADER,
HeaderValue::from_static(MITHRIL_API_VERSION),
);
warp::any()
Expand All @@ -54,7 +56,7 @@ pub fn routes(

/// API Version verification
fn header_must_be() -> impl Filter<Extract = (), Error = Rejection> + Copy {
warp::header::optional("mithril-api-version")
warp::header::optional(MITHRIL_API_VERSION_HEADER)
.and_then(|maybe_header: Option<String>| async move {
match maybe_header {
None => Ok(()),
Expand Down Expand Up @@ -97,7 +99,7 @@ mod tests {
async fn test_parse_version_error() {
let filters = header_must_be();
warp::test::request()
.header("mithril-api-version", "not_a_version")
.header(MITHRIL_API_VERSION_HEADER, "not_a_version")
.path("/aggregator/whatever")
.filter(&filters)
.await
Expand All @@ -110,7 +112,7 @@ mod tests {
async fn test_bad_version() {
let filters = header_must_be();
warp::test::request()
.header("mithril-api-version", "0.0.999")
.header(MITHRIL_API_VERSION_HEADER, "0.0.999")
.path("/aggregator/whatever")
.filter(&filters)
.await
Expand All @@ -121,7 +123,7 @@ mod tests {
async fn test_good_version() {
let filters = header_must_be();
warp::test::request()
.header("mithril-api-version", MITHRIL_API_VERSION)
.header(MITHRIL_API_VERSION_HEADER, MITHRIL_API_VERSION)
.path("/aggregator/whatever")
.filter(&filters)
.await
Expand Down
17 changes: 11 additions & 6 deletions mithril-client/src/aggregator.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use async_trait::async_trait;
use flate2::read::GzDecoder;
use futures::StreamExt;
use mithril_common::MITHRIL_API_VERSION_HEADER;
use reqwest::{self, Response, StatusCode};
use reqwest::{Client, RequestBuilder};
use slog_scope::debug;
Expand Down Expand Up @@ -106,7 +107,7 @@ impl AggregatorHTTPClient {

/// Forge a client request adding protocol version in the headers.
pub fn prepare_request_builder(&self, request_builder: RequestBuilder) -> RequestBuilder {
request_builder.header("mithril-api-version", MITHRIL_API_VERSION)
request_builder.header(MITHRIL_API_VERSION_HEADER, MITHRIL_API_VERSION)
}

/// Download certificate details
Expand Down Expand Up @@ -147,7 +148,7 @@ impl AggregatorHTTPClient {

/// API version error handling
fn handle_api_error(&self, response: &Response) -> AggregatorHandlerError {
if let Some(version) = response.headers().get("mithril-api-version") {
if let Some(version) = response.headers().get(MITHRIL_API_VERSION_HEADER) {
AggregatorHandlerError::ApiVersionMismatch(format!(
"server version: '{}', signer version: '{}'",
version.to_str().unwrap(),
Expand Down Expand Up @@ -392,7 +393,8 @@ mod tests {
let (server, config) = setup_test();
let _snapshots_mock = server.mock(|when, then| {
when.path("/snapshots");
then.status(412).header("mithril-api-version", "0.0.999");
then.status(412)
.header(MITHRIL_API_VERSION_HEADER, "0.0.999");
});
let aggregator_client =
AggregatorHTTPClient::new(config.network, config.aggregator_endpoint);
Expand Down Expand Up @@ -458,7 +460,8 @@ mod tests {
let digest = "digest123";
let _snapshots_mock = server.mock(|when, then| {
when.path(format!("/snapshot/{}", digest));
then.status(412).header("mithril-api-version", "0.0.999");
then.status(412)
.header(MITHRIL_API_VERSION_HEADER, "0.0.999");
});
let aggregator_client =
AggregatorHTTPClient::new(config.network, config.aggregator_endpoint);
Expand Down Expand Up @@ -522,7 +525,8 @@ mod tests {
let url_path = "/download";
let _snapshots_mock = server.mock(|when, then| {
when.path(url_path.to_string());
then.status(412).header("mithril-api-version", "0.0.999");
then.status(412)
.header(MITHRIL_API_VERSION_HEADER, "0.0.999");
});
let aggregator_client =
AggregatorHTTPClient::new(config.network, config.aggregator_endpoint);
Expand Down Expand Up @@ -595,7 +599,8 @@ mod tests {
let certificate_hash = "certificate-hash-123";
let _snapshots_mock = server.mock(|when, then| {
when.path(format!("/certificate/{}", certificate_hash));
then.status(412).header("mithril-api-version", "0.0.999");
then.status(412)
.header(MITHRIL_API_VERSION_HEADER, "0.0.999");
});
let aggregator_client =
AggregatorHTTPClient::new(config.network, config.aggregator_endpoint);
Expand Down
3 changes: 3 additions & 0 deletions mithril-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ use semver::{Version, VersionReq};
/// please also update the entry in the openapi.yml
pub const MITHRIL_API_VERSION: &str = "0.1.1";

/// Mithril API protocol version header name
pub const MITHRIL_API_VERSION_HEADER: &str = "mithril-api-version";

lazy_static! {
/// The [SemVer version requirement][semver::VersionReq] associated with the [MITHRIL_API_VERSION].
///
Expand Down
18 changes: 11 additions & 7 deletions mithril-signer/src/certificate_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use thiserror::Error;
use mithril_common::{
entities::{CertificatePending, EpochSettings, Signer, SingleSignatures},
messages::{CertificatePendingMessage, EpochSettingsMessage},
MITHRIL_API_VERSION,
MITHRIL_API_VERSION, MITHRIL_API_VERSION_HEADER,
};

#[cfg(test)]
Expand Down Expand Up @@ -94,12 +94,12 @@ impl CertificateHandlerHTTPClient {

/// Forge a client request adding protocol version in the headers.
pub fn prepare_request_builder(&self, request_builder: RequestBuilder) -> RequestBuilder {
request_builder.header("mithril-api-version", MITHRIL_API_VERSION)
request_builder.header(MITHRIL_API_VERSION_HEADER, MITHRIL_API_VERSION)
}

/// API version error handling
fn handle_api_error(&self, response: &Response) -> CertificateHandlerError {
if let Some(version) = response.headers().get("mithril-api-version") {
if let Some(version) = response.headers().get(MITHRIL_API_VERSION_HEADER) {
CertificateHandlerError::ApiVersionMismatch(format!(
"server version: '{}', signer version: '{}'",
version.to_str().unwrap(),
Expand Down Expand Up @@ -382,7 +382,8 @@ mod tests {
let (server, config) = setup_test();
let _snapshots_mock = server.mock(|when, then| {
when.path("/epoch-settings");
then.status(412).header("mithril-api-version", "0.0.999");
then.status(412)
.header(MITHRIL_API_VERSION_HEADER, "0.0.999");
});
let certificate_handler = CertificateHandlerHTTPClient::new(config.aggregator_endpoint);
let epoch_settings = certificate_handler
Expand Down Expand Up @@ -432,7 +433,8 @@ mod tests {
let (server, config) = setup_test();
let _snapshots_mock = server.mock(|when, then| {
when.path("/certificate-pending");
then.status(412).header("mithril-api-version", "0.0.999");
then.status(412)
.header(MITHRIL_API_VERSION_HEADER, "0.0.999");
});
let certificate_handler = CertificateHandlerHTTPClient::new(config.aggregator_endpoint);
let error = certificate_handler
Expand Down Expand Up @@ -490,7 +492,8 @@ mod tests {
let (server, config) = setup_test();
let _snapshots_mock = server.mock(|when, then| {
when.method(POST).path("/register-signer");
then.status(412).header("mithril-api-version", "0.0.999");
then.status(412)
.header(MITHRIL_API_VERSION_HEADER, "0.0.999");
});
let single_signers = fake_data::signers(1);
let single_signer = single_signers.first().unwrap();
Expand Down Expand Up @@ -567,7 +570,8 @@ mod tests {
let (server, config) = setup_test();
let _snapshots_mock = server.mock(|when, then| {
when.method(POST).path("/register-signatures");
then.status(412).header("mithril-api-version", "0.0.999");
then.status(412)
.header(MITHRIL_API_VERSION_HEADER, "0.0.999");
});
let single_signatures = fake_data::single_signatures((1..5).collect());
let certificate_handler = CertificateHandlerHTTPClient::new(config.aggregator_endpoint);
Expand Down

0 comments on commit 1653587

Please sign in to comment.