1
0
mirror of https://github.com/matrix-org/matrix-authentication-service.git synced 2025-08-09 04:22:45 +03:00

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.
This commit is contained in:
Quentin Gliech
2024-07-02 10:38:05 +02:00
parent f338aacf26
commit e7f50a92d6
13 changed files with 56 additions and 68 deletions

48
Cargo.lock generated
View File

@@ -2369,9 +2369,9 @@ dependencies = [
[[package]] [[package]]
name = "http-range-header" name = "http-range-header"
version = "0.3.1" version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "add0ab9360ddbd88cfeb3bd9574a1d85cfdfa14db10b3e21d3700dbc4328758f" checksum = "08a397c49fec283e3d6211adbe480be95aae5f304cfb923e9970e08956d5168a"
[[package]] [[package]]
name = "httparse" name = "httparse"
@@ -3155,12 +3155,15 @@ dependencies = [
"async-trait", "async-trait",
"axum", "axum",
"axum-extra", "axum-extra",
"bytes",
"chrono", "chrono",
"data-encoding", "data-encoding",
"futures-util", "futures-util",
"headers", "headers",
"http 1.1.0", "http 1.1.0",
"http-body 1.0.0", "http-body 1.0.0",
"http-body-util",
"hyper-util",
"icu_locid", "icu_locid",
"mas-data-model", "mas-data-model",
"mas-http", "mas-http",
@@ -3243,7 +3246,7 @@ dependencies = [
"sqlx", "sqlx",
"tokio", "tokio",
"tower", "tower",
"tower-http 0.4.4", "tower-http",
"tracing", "tracing",
"tracing-appender", "tracing-appender",
"tracing-opentelemetry", "tracing-opentelemetry",
@@ -3367,7 +3370,7 @@ dependencies = [
"time", "time",
"tokio", "tokio",
"tower", "tower",
"tower-http 0.4.4", "tower-http",
"tracing", "tracing",
"tracing-subscriber", "tracing-subscriber",
"ulid", "ulid",
@@ -3392,6 +3395,7 @@ dependencies = [
"mas-tower", "mas-tower",
"opentelemetry", "opentelemetry",
"opentelemetry-semantic-conventions", "opentelemetry-semantic-conventions",
"pin-project-lite",
"rustls 0.23.10", "rustls 0.23.10",
"rustls-platform-verifier", "rustls-platform-verifier",
"serde", "serde",
@@ -3400,7 +3404,7 @@ dependencies = [
"thiserror", "thiserror",
"tokio", "tokio",
"tower", "tower",
"tower-http 0.5.2", "tower-http",
"tracing", "tracing",
"tracing-opentelemetry", "tracing-opentelemetry",
] ]
@@ -3547,7 +3551,7 @@ dependencies = [
"tokio-rustls", "tokio-rustls",
"tokio-test", "tokio-test",
"tower", "tower",
"tower-http 0.5.2", "tower-http",
"tracing", "tracing",
"tracing-subscriber", "tracing-subscriber",
] ]
@@ -6404,31 +6408,6 @@ dependencies = [
"tracing", "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]] [[package]]
name = "tower-http" name = "tower-http"
version = "0.5.2" version = "0.5.2"
@@ -6441,12 +6420,19 @@ dependencies = [
"http 1.1.0", "http 1.1.0",
"http-body 1.0.0", "http-body 1.0.0",
"http-body-util", "http-body-util",
"http-range-header",
"httpdate",
"iri-string", "iri-string",
"mime",
"mime_guess",
"percent-encoding",
"pin-project-lite", "pin-project-lite",
"tokio", "tokio",
"tokio-util",
"tower", "tower",
"tower-layer", "tower-layer",
"tower-service", "tower-service",
"tracing",
] ]
[[package]] [[package]]

View File

@@ -209,6 +209,11 @@ features = ["full"]
version = "0.4.13" version = "0.4.13"
features = ["util"] features = ["util"]
# Tower HTTP layers
[workspace.dependencies.tower-http]
version = "0.5.2"
features = ["cors", "fs", "add-extension"]
# Logging and tracing # Logging and tracing
[workspace.dependencies.tracing] [workspace.dependencies.tracing]
version = "0.1.40" version = "0.1.40"

View File

@@ -15,12 +15,15 @@ workspace = true
async-trait.workspace = true async-trait.workspace = true
axum.workspace = true axum.workspace = true
axum-extra.workspace = true axum-extra.workspace = true
bytes = "1.6.0"
chrono.workspace = true chrono.workspace = true
data-encoding = "2.6.0" data-encoding = "2.6.0"
futures-util = "0.3.30" futures-util = "0.3.30"
headers.workspace = true headers.workspace = true
http.workspace = true http.workspace = true
http-body.workspace = true http-body.workspace = true
http-body-util.workspace = true
hyper-util.workspace = true
icu_locid = "1.4.0" icu_locid = "1.4.0"
mime = "0.3.17" mime = "0.3.17"
rand.workspace = true rand.workspace = true

View File

@@ -16,14 +16,14 @@ use std::collections::HashMap;
use async_trait::async_trait; use async_trait::async_trait;
use axum::{ use axum::{
body::HttpBody,
extract::{ extract::{
rejection::{FailedToDeserializeForm, FormRejection, TypedHeaderRejectionReason}, rejection::{FailedToDeserializeForm, FormRejection},
Form, FromRequest, FromRequestParts, TypedHeader, Form, FromRequest, FromRequestParts,
}, },
response::IntoResponse, response::IntoResponse,
BoxError, Json, BoxError, Json,
}; };
use axum_extra::typed_header::{TypedHeader, TypedHeaderRejectionReason};
use headers::{authorization::Basic, Authorization}; use headers::{authorization::Basic, Authorization};
use http::{Request, StatusCode}; use http::{Request, StatusCode};
use mas_data_model::{Client, JwksOrJwksUri}; use mas_data_model::{Client, JwksOrJwksUri};
@@ -337,18 +337,18 @@ impl IntoResponse for ClientAuthorizationError {
} }
#[async_trait] #[async_trait]
impl<S, B, F> FromRequest<S, B> for ClientAuthorization<F> impl<S, F> FromRequest<S> for ClientAuthorization<F>
where where
F: DeserializeOwned, F: DeserializeOwned,
B: HttpBody + Send + 'static,
B::Data: Send,
B::Error: Into<BoxError>,
S: Send + Sync, S: Send + Sync,
{ {
type Rejection = ClientAuthorizationError; type Rejection = ClientAuthorizationError;
#[allow(clippy::too_many_lines)] #[allow(clippy::too_many_lines)]
async fn from_request(req: Request<B>, state: &S) -> Result<Self, Self::Rejection> { async fn from_request(
req: Request<axum::body::Body>,
state: &S,
) -> Result<Self, Self::Rejection> {
// Split the request into parts so we can extract some headers // Split the request into parts so we can extract some headers
let (mut parts, body) = req.into_parts(); let (mut parts, body) = req.into_parts();

View File

@@ -15,8 +15,9 @@
use axum::{ use axum::{
http::StatusCode, http::StatusCode,
response::{IntoResponse, Response}, response::{IntoResponse, Response},
Extension, TypedHeader, Extension,
}; };
use axum_extra::typed_header::TypedHeader;
use headers::ContentType; use headers::ContentType;
use mas_templates::ErrorContext; use mas_templates::ErrorContext;

View File

@@ -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"); // Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with 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 // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
use axum::body::Full; use http_body_util::Full;
use hyper_util::rt::TokioExecutor;
use mas_http::{ use mas_http::{
make_traced_connector, BodyToBytesResponseLayer, Client, ClientLayer, ClientService, make_traced_connector, BodyToBytesResponseLayer, Client, ClientLayer, ClientService,
HttpService, TracedClient, TracedConnector, HttpService, TracedClient, TracedConnector,
@@ -50,7 +51,7 @@ impl HttpClientFactory {
B: axum::body::HttpBody + Send, B: axum::body::HttpBody + Send,
B::Data: 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 self.client_layer
.clone() .clone()
.with_category(category) .with_category(category)

View File

@@ -12,10 +12,8 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
use axum::{ use axum::response::{IntoResponse, Response};
response::{IntoResponse, Response}, use axum_extra::typed_header::TypedHeader;
TypedHeader,
};
use headers::ContentType; use headers::ContentType;
use mas_jose::jwt::Jwt; use mas_jose::jwt::Jwt;
use mime::Mime; use mime::Mime;

View File

@@ -16,14 +16,13 @@ use std::{collections::HashMap, error::Error};
use async_trait::async_trait; use async_trait::async_trait;
use axum::{ use axum::{
body::HttpBody,
extract::{ extract::{
rejection::{FailedToDeserializeForm, FormRejection, TypedHeaderRejectionReason}, rejection::{FailedToDeserializeForm, FormRejection},
Form, FromRequest, FromRequestParts, TypedHeader, Form, FromRequest, FromRequestParts,
}, },
response::{IntoResponse, Response}, response::{IntoResponse, Response},
BoxError,
}; };
use axum_extra::typed_header::{TypedHeader, TypedHeaderRejectionReason};
use headers::{authorization::Bearer, Authorization, Header, HeaderMapExt, HeaderName}; use headers::{authorization::Bearer, Authorization, Header, HeaderMapExt, HeaderName};
use http::{header::WWW_AUTHENTICATE, HeaderMap, HeaderValue, Request, StatusCode}; use http::{header::WWW_AUTHENTICATE, HeaderMap, HeaderValue, Request, StatusCode};
use mas_data_model::Session; use mas_data_model::Session;
@@ -289,17 +288,17 @@ where
} }
#[async_trait] #[async_trait]
impl<S, B, F> FromRequest<S, B> for UserAuthorization<F> impl<S, F> FromRequest<S> for UserAuthorization<F>
where where
F: DeserializeOwned, F: DeserializeOwned,
B: HttpBody + Send + 'static,
B::Data: Send,
B::Error: Into<BoxError>,
S: Send + Sync, S: Send + Sync,
{ {
type Rejection = UserAuthorizationError; type Rejection = UserAuthorizationError;
async fn from_request(req: Request<B>, state: &S) -> Result<Self, Self::Rejection> { async fn from_request(
req: Request<axum::body::Body>,
state: &S,
) -> Result<Self, Self::Rejection> {
let (mut parts, body) = req.into_parts(); let (mut parts, body) = req.into_parts();
let header = let header =
TypedHeader::<Authorization<Bearer>>::from_request_parts(&mut parts, state).await; TypedHeader::<Authorization<Bearer>>::from_request_parts(&mut parts, state).await;

View File

@@ -33,7 +33,7 @@ serde_yaml = "0.9.34"
sqlx.workspace = true sqlx.workspace = true
tokio.workspace = true tokio.workspace = true
tower.workspace = true tower.workspace = true
tower-http = { version = "0.4.4", features = ["fs"] } tower-http.workspace = true
url.workspace = true url.workspace = true
zeroize = "1.8.1" zeroize = "1.8.1"

View File

@@ -30,7 +30,7 @@ sentry = { version = "0.31.8", default-features = false }
# Web server # Web server
hyper.workspace = true hyper.workspace = true
tower.workspace = true tower.workspace = true
tower-http = { version = "0.4.4", features = ["cors"] } tower-http.workspace = true
axum.workspace = true axum.workspace = true
axum-macros = "0.4.1" axum-macros = "0.4.1"
axum-extra.workspace = true axum-extra.workspace = true

View File

@@ -25,12 +25,13 @@ opentelemetry.workspace = true
opentelemetry-semantic-conventions.workspace = true opentelemetry-semantic-conventions.workspace = true
rustls = { workspace = true, optional = true } rustls = { workspace = true, optional = true }
rustls-platform-verifier = { workspace = true, optional = true } rustls-platform-verifier = { workspace = true, optional = true }
pin-project-lite = "0.2.14"
serde.workspace = true serde.workspace = true
serde_json.workspace = true serde_json.workspace = true
serde_urlencoded = "0.7.1" serde_urlencoded = "0.7.1"
thiserror.workspace = true thiserror.workspace = true
tower.workspace = true tower.workspace = true
tower-http = { version = "0.5.2", features = ["cors"] } tower-http.workspace = true
tracing.workspace = true tracing.workspace = true
tracing-opentelemetry.workspace = true tracing-opentelemetry.workspace = true

View File

@@ -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"); // Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with 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 // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
use std::time::Duration;
use headers::{ContentLength, HeaderMapExt, Host, UserAgent}; use headers::{ContentLength, HeaderMapExt, Host, UserAgent};
use http::{header::USER_AGENT, HeaderValue, Request, Response}; use http::{header::USER_AGENT, HeaderValue, Request, Response};
use hyper_util::client::legacy::connect::HttpInfo; use hyper_util::client::legacy::connect::HttpInfo;
@@ -35,7 +33,6 @@ use tower::{
use tower_http::{ use tower_http::{
follow_redirect::{FollowRedirect, FollowRedirectLayer}, follow_redirect::{FollowRedirect, FollowRedirectLayer},
set_header::{SetRequestHeader, SetRequestHeaderLayer}, set_header::{SetRequestHeader, SetRequestHeaderLayer},
timeout::{Timeout, TimeoutLayer},
}; };
use tracing::Span; use tracing::Span;
@@ -45,7 +42,7 @@ pub type ClientService<S> = SetRequestHeader<
ConcurrencyLimit< ConcurrencyLimit<
FollowRedirect< FollowRedirect<
TraceService< TraceService<
TraceContextService<Timeout<S>>, TraceContextService<S>,
MakeSpanForRequest, MakeSpanForRequest,
EnrichSpanOnResponse, EnrichSpanOnResponse,
EnrichSpanOnError, EnrichSpanOnError,
@@ -183,7 +180,6 @@ pub struct ClientLayer {
follow_redirect_layer: FollowRedirectLayer, follow_redirect_layer: FollowRedirectLayer,
trace_layer: TraceLayer<MakeSpanForRequest, EnrichSpanOnResponse, EnrichSpanOnError>, trace_layer: TraceLayer<MakeSpanForRequest, EnrichSpanOnResponse, EnrichSpanOnError>,
trace_context_layer: TraceContextLayer, trace_context_layer: TraceContextLayer,
timeout_layer: TimeoutLayer,
duration_recorder_layer: DurationRecorderLayer<OnRequestLabels, OnResponseLabels, KeyValue>, duration_recorder_layer: DurationRecorderLayer<OnRequestLabels, OnResponseLabels, KeyValue>,
in_flight_counter_layer: InFlightCounterLayer<OnRequestLabels>, in_flight_counter_layer: InFlightCounterLayer<OnRequestLabels>,
} }
@@ -208,7 +204,6 @@ impl ClientLayer {
.on_response(EnrichSpanOnResponse) .on_response(EnrichSpanOnResponse)
.on_error(EnrichSpanOnError), .on_error(EnrichSpanOnError),
trace_context_layer: TraceContextLayer::new(), trace_context_layer: TraceContextLayer::new(),
timeout_layer: TimeoutLayer::new(Duration::from_secs(10)),
duration_recorder_layer: DurationRecorderLayer::new("http.client.duration") duration_recorder_layer: DurationRecorderLayer::new("http.client.duration")
.on_request(OnRequestLabels::default()) .on_request(OnRequestLabels::default())
.on_response(OnResponseLabels) .on_response(OnResponseLabels)
@@ -253,7 +248,6 @@ where
&self.follow_redirect_layer, &self.follow_redirect_layer,
&self.trace_layer, &self.trace_layer,
&self.trace_context_layer, &self.trace_context_layer,
&self.timeout_layer,
) )
.layer(inner) .layer(inner)
} }

View File

@@ -25,7 +25,7 @@ thiserror.workspace = true
tokio.workspace = true tokio.workspace = true
tokio-rustls = "0.26.0" tokio-rustls = "0.26.0"
tower.workspace = true tower.workspace = true
tower-http = { version = "0.5.2", features = ["add-extension"] } tower-http.workspace = true
tracing.workspace = true tracing.workspace = true
[dev-dependencies] [dev-dependencies]