From cd899abed9ca324247ea2235b4e26f423a5be5df Mon Sep 17 00:00:00 2001 From: Quentin Gliech Date: Tue, 2 Jul 2024 10:38:05 +0200 Subject: [PATCH] Move tower-http dep to the workspace and adapt mas-axum-utils We removed here the Timeout layer on the HTTP client service, because it required the body to be Default, which isn't the case anymore. Not sure what to do about it. --- Cargo.lock | 48 +++++++------------ Cargo.toml | 5 ++ crates/axum-utils/Cargo.toml | 3 ++ crates/axum-utils/src/client_authorization.rs | 16 +++---- crates/axum-utils/src/fancy_error.rs | 3 +- crates/axum-utils/src/http_client_factory.rs | 7 +-- crates/axum-utils/src/jwt.rs | 6 +-- crates/axum-utils/src/user_authorization.rs | 17 ++++--- crates/cli/Cargo.toml | 2 +- crates/handlers/Cargo.toml | 2 +- crates/http/Cargo.toml | 3 +- crates/http/src/layers/client.rs | 10 +--- crates/listener/Cargo.toml | 2 +- 13 files changed, 56 insertions(+), 68 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2543501a9..6c18ea3be 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2369,9 +2369,9 @@ dependencies = [ [[package]] name = "http-range-header" -version = "0.3.1" +version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "add0ab9360ddbd88cfeb3bd9574a1d85cfdfa14db10b3e21d3700dbc4328758f" +checksum = "08a397c49fec283e3d6211adbe480be95aae5f304cfb923e9970e08956d5168a" [[package]] name = "httparse" @@ -3155,12 +3155,15 @@ dependencies = [ "async-trait", "axum", "axum-extra", + "bytes", "chrono", "data-encoding", "futures-util", "headers", "http 1.1.0", "http-body 1.0.0", + "http-body-util", + "hyper-util", "icu_locid", "mas-data-model", "mas-http", @@ -3243,7 +3246,7 @@ dependencies = [ "sqlx", "tokio", "tower", - "tower-http 0.4.4", + "tower-http", "tracing", "tracing-appender", "tracing-opentelemetry", @@ -3367,7 +3370,7 @@ dependencies = [ "time", "tokio", "tower", - "tower-http 0.4.4", + "tower-http", "tracing", "tracing-subscriber", "ulid", @@ -3392,6 +3395,7 @@ dependencies = [ "mas-tower", "opentelemetry", "opentelemetry-semantic-conventions", + "pin-project-lite", "rustls 0.23.10", "rustls-platform-verifier", "serde", @@ -3400,7 +3404,7 @@ dependencies = [ "thiserror", "tokio", "tower", - "tower-http 0.5.2", + "tower-http", "tracing", "tracing-opentelemetry", ] @@ -3547,7 +3551,7 @@ dependencies = [ "tokio-rustls", "tokio-test", "tower", - "tower-http 0.5.2", + "tower-http", "tracing", "tracing-subscriber", ] @@ -6404,31 +6408,6 @@ dependencies = [ "tracing", ] -[[package]] -name = "tower-http" -version = "0.4.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "61c5bb1d698276a2443e5ecfabc1008bf15a36c12e6a7176e7bf089ea9131140" -dependencies = [ - "bitflags 2.6.0", - "bytes", - "futures-core", - "futures-util", - "http 0.2.12", - "http-body 0.4.6", - "http-range-header", - "httpdate", - "mime", - "mime_guess", - "percent-encoding", - "pin-project-lite", - "tokio", - "tokio-util", - "tower-layer", - "tower-service", - "tracing", -] - [[package]] name = "tower-http" version = "0.5.2" @@ -6441,12 +6420,19 @@ dependencies = [ "http 1.1.0", "http-body 1.0.0", "http-body-util", + "http-range-header", + "httpdate", "iri-string", + "mime", + "mime_guess", + "percent-encoding", "pin-project-lite", "tokio", + "tokio-util", "tower", "tower-layer", "tower-service", + "tracing", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index e5a373dd8..b002bed1d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -209,6 +209,11 @@ features = ["full"] version = "0.4.13" features = ["util"] +# Tower HTTP layers +[workspace.dependencies.tower-http] +version = "0.5.2" +features = ["cors", "fs", "add-extension"] + # Logging and tracing [workspace.dependencies.tracing] version = "0.1.40" diff --git a/crates/axum-utils/Cargo.toml b/crates/axum-utils/Cargo.toml index c077d8667..00384864c 100644 --- a/crates/axum-utils/Cargo.toml +++ b/crates/axum-utils/Cargo.toml @@ -15,12 +15,15 @@ workspace = true async-trait.workspace = true axum.workspace = true axum-extra.workspace = true +bytes = "1.6.0" chrono.workspace = true data-encoding = "2.6.0" futures-util = "0.3.30" headers.workspace = true http.workspace = true http-body.workspace = true +http-body-util.workspace = true +hyper-util.workspace = true icu_locid = "1.4.0" mime = "0.3.17" rand.workspace = true diff --git a/crates/axum-utils/src/client_authorization.rs b/crates/axum-utils/src/client_authorization.rs index 0f191e44a..d716b0dc9 100644 --- a/crates/axum-utils/src/client_authorization.rs +++ b/crates/axum-utils/src/client_authorization.rs @@ -16,14 +16,14 @@ use std::collections::HashMap; use async_trait::async_trait; use axum::{ - body::HttpBody, extract::{ - rejection::{FailedToDeserializeForm, FormRejection, TypedHeaderRejectionReason}, - Form, FromRequest, FromRequestParts, TypedHeader, + rejection::{FailedToDeserializeForm, FormRejection}, + Form, FromRequest, FromRequestParts, }, response::IntoResponse, BoxError, Json, }; +use axum_extra::typed_header::{TypedHeader, TypedHeaderRejectionReason}; use headers::{authorization::Basic, Authorization}; use http::{Request, StatusCode}; use mas_data_model::{Client, JwksOrJwksUri}; @@ -337,18 +337,18 @@ impl IntoResponse for ClientAuthorizationError { } #[async_trait] -impl FromRequest for ClientAuthorization +impl FromRequest for ClientAuthorization where F: DeserializeOwned, - B: HttpBody + Send + 'static, - B::Data: Send, - B::Error: Into, S: Send + Sync, { type Rejection = ClientAuthorizationError; #[allow(clippy::too_many_lines)] - async fn from_request(req: Request, state: &S) -> Result { + async fn from_request( + req: Request, + state: &S, + ) -> Result { // Split the request into parts so we can extract some headers let (mut parts, body) = req.into_parts(); diff --git a/crates/axum-utils/src/fancy_error.rs b/crates/axum-utils/src/fancy_error.rs index bad289bda..ccdca7391 100644 --- a/crates/axum-utils/src/fancy_error.rs +++ b/crates/axum-utils/src/fancy_error.rs @@ -15,8 +15,9 @@ use axum::{ http::StatusCode, response::{IntoResponse, Response}, - Extension, TypedHeader, + Extension, }; +use axum_extra::typed_header::TypedHeader; use headers::ContentType; use mas_templates::ErrorContext; diff --git a/crates/axum-utils/src/http_client_factory.rs b/crates/axum-utils/src/http_client_factory.rs index 78a812acf..bcdc9c144 100644 --- a/crates/axum-utils/src/http_client_factory.rs +++ b/crates/axum-utils/src/http_client_factory.rs @@ -1,4 +1,4 @@ -// Copyright 2022 The Matrix.org Foundation C.I.C. +// Copyright 2022-2024 The Matrix.org Foundation C.I.C. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -12,7 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -use axum::body::Full; +use http_body_util::Full; +use hyper_util::rt::TokioExecutor; use mas_http::{ make_traced_connector, BodyToBytesResponseLayer, Client, ClientLayer, ClientService, HttpService, TracedClient, TracedConnector, @@ -50,7 +51,7 @@ impl HttpClientFactory { B: axum::body::HttpBody + Send, B::Data: Send, { - let client = Client::builder().build(self.traced_connector.clone()); + let client = Client::builder(TokioExecutor::new()).build(self.traced_connector.clone()); self.client_layer .clone() .with_category(category) diff --git a/crates/axum-utils/src/jwt.rs b/crates/axum-utils/src/jwt.rs index eea1c90de..6af0529fe 100644 --- a/crates/axum-utils/src/jwt.rs +++ b/crates/axum-utils/src/jwt.rs @@ -12,10 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -use axum::{ - response::{IntoResponse, Response}, - TypedHeader, -}; +use axum::response::{IntoResponse, Response}; +use axum_extra::typed_header::TypedHeader; use headers::ContentType; use mas_jose::jwt::Jwt; use mime::Mime; diff --git a/crates/axum-utils/src/user_authorization.rs b/crates/axum-utils/src/user_authorization.rs index 9181428af..e56c23e3c 100644 --- a/crates/axum-utils/src/user_authorization.rs +++ b/crates/axum-utils/src/user_authorization.rs @@ -16,14 +16,13 @@ use std::{collections::HashMap, error::Error}; use async_trait::async_trait; use axum::{ - body::HttpBody, extract::{ - rejection::{FailedToDeserializeForm, FormRejection, TypedHeaderRejectionReason}, - Form, FromRequest, FromRequestParts, TypedHeader, + rejection::{FailedToDeserializeForm, FormRejection}, + Form, FromRequest, FromRequestParts, }, response::{IntoResponse, Response}, - BoxError, }; +use axum_extra::typed_header::{TypedHeader, TypedHeaderRejectionReason}; use headers::{authorization::Bearer, Authorization, Header, HeaderMapExt, HeaderName}; use http::{header::WWW_AUTHENTICATE, HeaderMap, HeaderValue, Request, StatusCode}; use mas_data_model::Session; @@ -289,17 +288,17 @@ where } #[async_trait] -impl FromRequest for UserAuthorization +impl FromRequest for UserAuthorization where F: DeserializeOwned, - B: HttpBody + Send + 'static, - B::Data: Send, - B::Error: Into, S: Send + Sync, { type Rejection = UserAuthorizationError; - async fn from_request(req: Request, state: &S) -> Result { + async fn from_request( + req: Request, + state: &S, + ) -> Result { let (mut parts, body) = req.into_parts(); let header = TypedHeader::>::from_request_parts(&mut parts, state).await; diff --git a/crates/cli/Cargo.toml b/crates/cli/Cargo.toml index 463948a49..14032a5da 100644 --- a/crates/cli/Cargo.toml +++ b/crates/cli/Cargo.toml @@ -33,7 +33,7 @@ serde_yaml = "0.9.34" sqlx.workspace = true tokio.workspace = true tower.workspace = true -tower-http = { version = "0.4.4", features = ["fs"] } +tower-http.workspace = true url.workspace = true zeroize = "1.8.1" diff --git a/crates/handlers/Cargo.toml b/crates/handlers/Cargo.toml index 10d8ff2eb..98b2c7281 100644 --- a/crates/handlers/Cargo.toml +++ b/crates/handlers/Cargo.toml @@ -30,7 +30,7 @@ sentry = { version = "0.31.8", default-features = false } # Web server hyper.workspace = true tower.workspace = true -tower-http = { version = "0.4.4", features = ["cors"] } +tower-http.workspace = true axum.workspace = true axum-macros = "0.4.1" axum-extra.workspace = true diff --git a/crates/http/Cargo.toml b/crates/http/Cargo.toml index c0692fcc5..2d336d3bf 100644 --- a/crates/http/Cargo.toml +++ b/crates/http/Cargo.toml @@ -25,12 +25,13 @@ opentelemetry.workspace = true opentelemetry-semantic-conventions.workspace = true rustls = { workspace = true, optional = true } rustls-platform-verifier = { workspace = true, optional = true } +pin-project-lite = "0.2.14" serde.workspace = true serde_json.workspace = true serde_urlencoded = "0.7.1" thiserror.workspace = true tower.workspace = true -tower-http = { version = "0.5.2", features = ["cors"] } +tower-http.workspace = true tracing.workspace = true tracing-opentelemetry.workspace = true diff --git a/crates/http/src/layers/client.rs b/crates/http/src/layers/client.rs index bd055fb80..7ca42d7a3 100644 --- a/crates/http/src/layers/client.rs +++ b/crates/http/src/layers/client.rs @@ -1,4 +1,4 @@ -// Copyright 2022 The Matrix.org Foundation C.I.C. +// Copyright 2022-2024 The Matrix.org Foundation C.I.C. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -use std::time::Duration; - use headers::{ContentLength, HeaderMapExt, Host, UserAgent}; use http::{header::USER_AGENT, HeaderValue, Request, Response}; use hyper_util::client::legacy::connect::HttpInfo; @@ -35,7 +33,6 @@ use tower::{ use tower_http::{ follow_redirect::{FollowRedirect, FollowRedirectLayer}, set_header::{SetRequestHeader, SetRequestHeaderLayer}, - timeout::{Timeout, TimeoutLayer}, }; use tracing::Span; @@ -45,7 +42,7 @@ pub type ClientService = SetRequestHeader< ConcurrencyLimit< FollowRedirect< TraceService< - TraceContextService>, + TraceContextService, MakeSpanForRequest, EnrichSpanOnResponse, EnrichSpanOnError, @@ -183,7 +180,6 @@ pub struct ClientLayer { follow_redirect_layer: FollowRedirectLayer, trace_layer: TraceLayer, trace_context_layer: TraceContextLayer, - timeout_layer: TimeoutLayer, duration_recorder_layer: DurationRecorderLayer, in_flight_counter_layer: InFlightCounterLayer, } @@ -208,7 +204,6 @@ impl ClientLayer { .on_response(EnrichSpanOnResponse) .on_error(EnrichSpanOnError), trace_context_layer: TraceContextLayer::new(), - timeout_layer: TimeoutLayer::new(Duration::from_secs(10)), duration_recorder_layer: DurationRecorderLayer::new("http.client.duration") .on_request(OnRequestLabels::default()) .on_response(OnResponseLabels) @@ -253,7 +248,6 @@ where &self.follow_redirect_layer, &self.trace_layer, &self.trace_context_layer, - &self.timeout_layer, ) .layer(inner) } diff --git a/crates/listener/Cargo.toml b/crates/listener/Cargo.toml index f39b708bf..d15ea9a6c 100644 --- a/crates/listener/Cargo.toml +++ b/crates/listener/Cargo.toml @@ -25,7 +25,7 @@ thiserror.workspace = true tokio.workspace = true tokio-rustls = "0.26.0" tower.workspace = true -tower-http = { version = "0.5.2", features = ["add-extension"] } +tower-http.workspace = true tracing.workspace = true [dev-dependencies]