Skip to content
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

Fix API version sent in wrong header #712

Merged
merged 3 commits into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion mithril-aggregator/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mithril-aggregator"
version = "0.2.10"
version = "0.2.11"
description = "A Mithril Aggregator server"
authors = { workspace = true }
edition = { workspace = true }
Expand Down
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
2 changes: 1 addition & 1 deletion mithril-client/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mithril-client"
version = "0.2.6"
version = "0.2.7"
description = "A Mithril Client"
authors = { workspace = true }
edition = { workspace = true }
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("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 @@ -390,7 +391,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 @@ -456,7 +458,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 @@ -520,7 +523,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 @@ -593,7 +597,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
2 changes: 1 addition & 1 deletion mithril-common/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mithril-common"
version = "0.2.9"
version = "0.2.10"
authors = { workspace = true }
edition = { workspace = true }
documentation = { workspace = true }
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
2 changes: 1 addition & 1 deletion mithril-signer/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mithril-signer"
version = "0.2.7"
version = "0.2.8"
description = "A Mithril Signer"
authors = { workspace = true }
edition = { workspace = true }
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("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 @@ -381,7 +381,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 @@ -431,7 +432,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 @@ -489,7 +491,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 @@ -566,7 +569,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