From 03d24ca5fb002aab36946cbd767245bf32e0e211 Mon Sep 17 00:00:00 2001 From: Hermann Core Date: Tue, 17 Dec 2024 11:40:20 +0100 Subject: [PATCH] upgrade all dependencies to their latest versions --- Cargo.toml | 75 ++++++++++--------- crates/database/src/lib.rs | 15 ++-- crates/filesystem/Cargo.toml | 2 +- crates/filesystem/src/lib.rs | 9 +-- crates/web-plugins/did-endpoint/Cargo.toml | 5 +- crates/web-plugins/did-endpoint/src/web.rs | 5 +- .../web-plugins/didcomm-messaging/Cargo.toml | 5 +- .../didcomm-messaging/did-utils/Cargo.toml | 13 ++-- .../did-utils/src/methods/errors.rs | 2 + .../did-utils/src/methods/web/resolver.rs | 42 ++++++----- .../protocols/forward/Cargo.toml | 2 +- .../mediator-coordination/Cargo.toml | 4 +- .../protocols/pickup/Cargo.toml | 2 +- .../protocols/trust-ping/Cargo.toml | 2 +- .../didcomm-messaging/shared/Cargo.toml | 2 +- .../didcomm-messaging/src/error.rs | 9 +-- .../didcomm-messaging/src/midlw.rs | 37 +++++---- .../didcomm-messaging/src/web/dispatcher.rs | 9 ++- crates/web-plugins/oob-messages/Cargo.toml | 6 +- crates/web-plugins/oob-messages/src/models.rs | 13 +++- src/main.rs | 24 +++--- src/plugins/index/web.rs | 5 +- 22 files changed, 155 insertions(+), 133 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2a33a6ac..6bb97444 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -48,50 +48,52 @@ mediator-coordination = { path = "./crates/web-plugins/didcomm-messaging/protoco # Other common dependencies serde = "1.0" sha2 = "0.10" -cfg-if = "0.1" +cfg-if = "1.0" getrandom = "0.2" -hyper-tls = "0.5.0" -json-patch = "1.0.0" -x25519-dalek = "2.0.0-rc.3" -multibase = "0.8.0" +hyper-tls = "0.6.0" +json-patch = "3.0.1" +x25519-dalek = "2.0.1" +multibase = "0.9.1" json-canon = "0.1.3" -qrcode = "0.12.0" -image = "0.23" -reqwest = "0.11" +qrcode = "0.14.1" +image = "0.25" +reqwest = "0.12" tempdir = "0.3.7" -headers = "0.3" -thiserror = "1.0.48" -url = "2.4.1" -num-bigint = "0.4.4" -base64 = "0.13.0" +headers = "0.4" +thiserror = "2.0.7" +url = "2.5.4" +num-bigint = "0.4.6" +base64 = "0.22.1" hex = "0.4.3" eyre = "0.6" anyhow = "1" -subtle = "2.5.0" -regex = "1.10.2" -mongodb = "2.7.1" -once_cell = "1.20.0" -tower = "0.4" -nix = "0.22.0" -uuid = "1.4.1" -axum = "0.6.20" -tokio = "1.30.0" -tracing = "0.1.37" -chrono = "0.4.26" -paste = "0.1" +subtle = "2.6.1" +regex = "1.11.1" +mongodb = "3.1.1" +once_cell = "1.20.2" +tower = "0.5" +nix = "0.29.0" +uuid = "1.11.0" +axum = "0.7.9" +tokio = "1.42.0" +tracing = "0.1.41" +chrono = "0.4.39" +paste = "1.0" didcomm = "0.4.1" -hyper = "0.14.27" -lazy_static = "1.4.0" -async-trait = "0.1.73" -dotenv-flow = "0.15.0" +hyper = "1.5.2" +hyper-util = "0.1" +http-body-util = "0.1" +lazy_static = "1.5.0" +async-trait = "0.1.83" +dotenv-flow = "0.16.2" serde_json = "1.0" -parking_lot = "0.12.0" -curve25519-dalek = "4.0.0-rc.3" -ed25519-dalek = "2.0.0-rc.3" -tracing-subscriber = "0.3.17" -tower-http = "0.4.3" +parking_lot = "0.12.3" +curve25519-dalek = "4.1.3" +ed25519-dalek = "2.1.1" +tracing-subscriber = "0.3.19" +tower-http = "0.6.2" base64ct = { version = "1.6.0", default-features = false } -zeroize = { version = "1.6.0", default-features = false } +zeroize = { version = "1.8.1", default-features = false } [dependencies] @@ -105,6 +107,7 @@ tracing.workspace = true lazy_static.workspace = true serde_json.workspace = true hyper.workspace = true +http-body-util.workspace = true tokio = { workspace = true, features = ["full"] } tracing-subscriber = { workspace = true, features = ["json"] } tower-http = { workspace = true, features = ["catch-panic", "trace", "cors"] } @@ -136,4 +139,4 @@ mediator-coordination = ["plugin-didcomm_messaging", "didcomm-messaging/mediator [dev-dependencies] -tower = { version = "0.4.13", features = ["util"] } +tower = { version = "0.5.2", features = ["util"] } diff --git a/crates/database/src/lib.rs b/crates/database/src/lib.rs index 079f558a..cb25bb46 100644 --- a/crates/database/src/lib.rs +++ b/crates/database/src/lib.rs @@ -77,7 +77,7 @@ where let collection = self.get_collection(); // Lock the Mutex and get the Collection - let mut cursor = collection.read().await.find(None, None).await?; + let mut cursor = collection.read().await.find(doc! {}).await?; while cursor.advance().await? { entities.push(cursor.deserialize_current()?); } @@ -91,7 +91,7 @@ where // Lock the Mutex and get the Collection let collection = collection.read().await; Ok(collection - .count_documents(filter, None) + .count_documents(filter) .await? .try_into() .map_err(|_| RepositoryError::Generic("count overflow".to_owned()))?) @@ -108,7 +108,7 @@ where // Lock the Mutex and get the Collection let collection = collection.read().await; - Ok(collection.find_one(filter, None).await?) + Ok(collection.find_one(filter).await?) } /// Stores a new entity. @@ -119,7 +119,7 @@ where let collection = collection.read().await; // Insert the new entity into the database - let metadata = collection.insert_one(entity.clone(), None).await?; + let metadata = collection.insert_one(entity.clone()).await?; // Set the ID if it was inserted and return the updated entity if let Bson::ObjectId(oid) = metadata.inserted_id { @@ -144,7 +144,7 @@ where let collection = collection.read().await; // Retrieve all entities from the database - let mut cursor = collection.find(filter, find_options).await?; + let mut cursor = collection.find(filter).with_options(find_options).await?; while cursor.advance().await? { entities.push(cursor.deserialize_current()?); } @@ -160,7 +160,7 @@ where let collection = collection.read().await; // Delete the entity from the database - collection.delete_one(doc! {"_id": id}, None).await?; + collection.delete_one(doc! {"_id": id}).await?; Ok(()) } @@ -179,8 +179,7 @@ where let metadata = collection .update_one( doc! {"_id": entity.id().unwrap()}, - doc! {"$set": bson::to_document(&entity).map_err(|_| RepositoryError::BsonConversionError)?}, - None, + doc! {"$set": bson::to_document(&entity).map_err(|_| RepositoryError::BsonConversionError)?} ) .await?; diff --git a/crates/filesystem/Cargo.toml b/crates/filesystem/Cargo.toml index e9b11a00..4db32f60 100644 --- a/crates/filesystem/Cargo.toml +++ b/crates/filesystem/Cargo.toml @@ -4,7 +4,7 @@ version = "0.1.0" edition = "2021" [dependencies] -nix.workspace = true +nix ={ workspace = true, features = ["fs"] } [features] test-utils = [] diff --git a/crates/filesystem/src/lib.rs b/crates/filesystem/src/lib.rs index 301bc195..ddc4066d 100644 --- a/crates/filesystem/src/lib.rs +++ b/crates/filesystem/src/lib.rs @@ -1,14 +1,13 @@ -use nix::fcntl::{flock, FlockArg}; +use nix::fcntl::{Flock, FlockArg}; use std::{ fs::OpenOptions, io::{Error as IoError, ErrorKind, Result as IoResult}, - os::unix::io::AsRawFd, path::Path, }; #[doc(hidden)] // Define a trait for file system operations -pub trait FileSystem: Send + Sync + 'static { +pub trait FileSystem: Send + 'static { fn read_to_string(&self, path: &Path) -> IoResult; fn write(&mut self, path: &Path, content: &str) -> IoResult<()>; fn read_dir_files(&self, path: &Path) -> IoResult>; @@ -59,7 +58,7 @@ impl FileSystem for StdFileSystem { .open(path)?; // Acquire an exclusive lock before writing to the file - flock(file.as_raw_fd(), FlockArg::LockExclusive) + let file = Flock::lock(file, FlockArg::LockExclusive) .map_err(|_| IoError::new(ErrorKind::Other, "Error acquiring file lock"))?; std::fs::write(path, content).map_err(|_| { @@ -70,7 +69,7 @@ impl FileSystem for StdFileSystem { })?; // Release the lock after writing to the file - flock(file.as_raw_fd(), FlockArg::Unlock) + file.unlock() .map_err(|_| IoError::new(ErrorKind::Other, "Error releasing file lock"))?; Ok(()) diff --git a/crates/web-plugins/did-endpoint/Cargo.toml b/crates/web-plugins/did-endpoint/Cargo.toml index c3b6f25f..5d649156 100644 --- a/crates/web-plugins/did-endpoint/Cargo.toml +++ b/crates/web-plugins/did-endpoint/Cargo.toml @@ -18,6 +18,7 @@ serde_json.workspace = true dotenv-flow.workspace = true multibase.workspace = true tracing.workspace = true +http-body-util.workspace = true uuid = { workspace = true, features = ["v4"] } hyper = { workspace = true, features = ["full"] } tokio = { workspace = true, features = ["full"] } @@ -25,8 +26,8 @@ axum = { workspace = true, features = ["macros"] } [dev-dependencies] async-trait.workspace = true -mockall = "0.13.0" +mockall = "0.13.1" json-canon = "0.1.3" filesystem = { workspace = true, features = ["test-utils"] } keystore = { workspace = true, features = ["test-utils"] } -tower = { version = "0.4.13", features = ["util"] } +tower = { version = "0.5.2", features = ["util"] } diff --git a/crates/web-plugins/did-endpoint/src/web.rs b/crates/web-plugins/did-endpoint/src/web.rs index 2e1e622b..11e59900 100644 --- a/crates/web-plugins/did-endpoint/src/web.rs +++ b/crates/web-plugins/did-endpoint/src/web.rs @@ -199,6 +199,7 @@ mod tests { proof::{CryptoProof, EdDsaJcs2022}, vc::VerifiablePresentation, }; + use http_body_util::BodyExt; use serde_json::json; use tower::util::ServiceExt; @@ -284,8 +285,8 @@ mod tests { assert_eq!(response.status(), StatusCode::OK); - let body = hyper::body::to_bytes(response.into_body()).await.unwrap(); - let vp: VerifiablePresentation = serde_json::from_slice(&body).unwrap(); + let body = BodyExt::collect(response.into_body()).await.unwrap(); + let vp: VerifiablePresentation = serde_json::from_slice(&body.to_bytes()).unwrap(); let vc = vp.verifiable_credential.get(0).unwrap(); let diddoc = serde_json::from_value(json!(vc.credential_subject)).unwrap(); diff --git a/crates/web-plugins/didcomm-messaging/Cargo.toml b/crates/web-plugins/didcomm-messaging/Cargo.toml index fbb75aed..33261701 100644 --- a/crates/web-plugins/didcomm-messaging/Cargo.toml +++ b/crates/web-plugins/didcomm-messaging/Cargo.toml @@ -26,6 +26,7 @@ tracing.workspace = true once_cell.workspace = true serde_json.workspace = true thiserror.workspace = true +http-body-util.workspace = true tokio = { workspace = true, features = ["full"] } hyper = { workspace = true, features = ["full"] } axum = { workspace = true, features = ["macros"] } @@ -52,8 +53,8 @@ async-trait.workspace = true uuid = { workspace = true, features = ["v4"] } json-canon = "0.1.3" shared = { workspace = true, features = ["test-utils"] } -tokio = { version = "1.27.0", default-features = false, features = [ +tokio = { version = "1.42.0", default-features = false, features = [ "macros", "rt", ] } -tower = { version = "0.4.13", features = ["util"] } +tower = { version = "0.5.2", features = ["util"] } diff --git a/crates/web-plugins/didcomm-messaging/did-utils/Cargo.toml b/crates/web-plugins/didcomm-messaging/did-utils/Cargo.toml index 99afeed9..ed70e067 100644 --- a/crates/web-plugins/didcomm-messaging/did-utils/Cargo.toml +++ b/crates/web-plugins/didcomm-messaging/did-utils/Cargo.toml @@ -5,11 +5,14 @@ edition = "2021" [dependencies] serde_json.workspace = true -chrono = { workspace = true, features = ["serde"] } -hyper = { workspace = true, features = ["client", "http2"] } tokio.workspace = true serde.workspace = true +axum.workspace = true hyper-tls.workspace = true +http-body-util.workspace = true +hyper-util ={ workspace = true, features = ["full"] } +hyper = { workspace = true, features = ["full"] } +chrono = { workspace = true, features = ["serde"] } # Cross-platform random number generator from os getrandom = { workspace = true, features = ["js"] } @@ -38,10 +41,10 @@ subtle.workspace = true regex.workspace = true [dev-dependencies] -hyper = { version = "0.14.26", features = ["server"] } -async-std = { version = "1.12.0", features = ["attributes"] } +hyper = { version = "1.5.2", features = ["server"] } +async-std = { version = "1.13.0", features = ["attributes"] } hex = "0.4.3" -tokio = { version = "1.27.0", default-features = false, features = [ +tokio = { version = "1.42.0", default-features = false, features = [ "macros", "rt", ] } diff --git a/crates/web-plugins/didcomm-messaging/did-utils/src/methods/errors.rs b/crates/web-plugins/didcomm-messaging/did-utils/src/methods/errors.rs index daba60b6..20e74a72 100644 --- a/crates/web-plugins/didcomm-messaging/did-utils/src/methods/errors.rs +++ b/crates/web-plugins/didcomm-messaging/did-utils/src/methods/errors.rs @@ -66,6 +66,8 @@ pub enum DidWebError { #[error("Parsing error: {0}")] ParsingError(#[from] ParsingErrorSource), #[error("URL parsing error: {0}")] + HttpClientError(#[from] hyper_util::client::legacy::Error), + #[error("URL parsing error: {0}")] HttpError(#[from] hyper::Error), #[error("Non-success server response: {0}")] NonSuccessResponse(StatusCode), diff --git a/crates/web-plugins/didcomm-messaging/did-utils/src/methods/web/resolver.rs b/crates/web-plugins/didcomm-messaging/did-utils/src/methods/web/resolver.rs index c33fe21d..8a9dfd3c 100644 --- a/crates/web-plugins/didcomm-messaging/did-utils/src/methods/web/resolver.rs +++ b/crates/web-plugins/didcomm-messaging/did-utils/src/methods/web/resolver.rs @@ -1,11 +1,19 @@ use crate::methods::resolution::{DIDResolutionMetadata, DIDResolutionOptions, MediaType, ResolutionOutput}; use async_trait::async_trait; +use http_body_util::{BodyExt, Full}; use hyper::{ - client::{connect::Connect, HttpConnector}, + body::Bytes, http::uri::{self, Scheme}, - Body, Client, Uri, + Uri, }; use hyper_tls::HttpsConnector; +use hyper_util::{ + client::legacy::{ + connect::{Connect, HttpConnector}, + Client, + }, + rt::TokioExecutor, +}; use crate::ldmodel::Context; use crate::methods::{errors::DidWebError, traits::DIDResolver}; @@ -17,7 +25,7 @@ pub struct DidWeb where C: Connect + Send + Sync + Clone + 'static, { - client: Client, + client: Client>, } impl DidWeb { @@ -25,7 +33,7 @@ impl DidWeb { #[cfg(test)] pub fn http() -> DidWeb { DidWeb { - client: Client::builder().build::<_, Body>(HttpConnector::new()), + client: Client::builder(TokioExecutor::new()).build_http(), } } } @@ -40,7 +48,7 @@ impl DidWeb> { /// Creates a new `DidWeb` resolver. pub fn new() -> DidWeb> { DidWeb { - client: Client::builder().build::<_, Body>(HttpsConnector::new()), + client: Client::builder(TokioExecutor::new()).build::<_, Full>(HttpsConnector::new()), } } } @@ -57,9 +65,9 @@ where return Err(DidWebError::NonSuccessResponse(res.status())); } - let body = hyper::body::to_bytes(res.into_body()).await?; + let body = BodyExt::collect(res.into_body()).await?; - String::from_utf8(body.to_vec()).map_err(|err| err.into()) + String::from_utf8(body.to_bytes().to_vec()).map_err(|err| err.into()) } /// Fetches and parses a DID document for the given DID. @@ -158,16 +166,12 @@ where mod tests { use super::*; - use hyper::{ - service::{make_service_fn, service_fn}, - Body, Request, Response, Server, - }; - + use axum::{body::Body, extract::Request, response::Response, routing::get}; use serde_json::Value; - use std::convert::Infallible; - use std::net::SocketAddr; + use std::{convert::Infallible, net::SocketAddr}; + use tokio::net::TcpListener; - async fn mock_server_handler(req: Request) -> Result, Infallible> { + async fn mock_server_handler(req: Request) -> Result { const DID_JSON: &str = r#" {"@context": "https://www.w3.org/ns/did/v1", "id": "did:web:localhost", @@ -194,13 +198,13 @@ mod tests { } async fn create_mock_server(port: u16) -> String { - let make_svc = make_service_fn(|_conn| async { Ok::<_, Infallible>(service_fn(mock_server_handler)) }); - let addr = SocketAddr::from(([127, 0, 0, 1], port)); - let server = Server::bind(&addr).serve(make_svc); + let listener = TcpListener::bind(addr).await.unwrap(); + + let app = axum::Router::new().route("/.well-known/did.json", get(mock_server_handler)); tokio::spawn(async move { - server.await.unwrap(); + axum::serve(listener, app).await.unwrap(); }); "localhost".to_string() diff --git a/crates/web-plugins/didcomm-messaging/protocols/forward/Cargo.toml b/crates/web-plugins/didcomm-messaging/protocols/forward/Cargo.toml index f48b31bb..e38d2ee0 100644 --- a/crates/web-plugins/didcomm-messaging/protocols/forward/Cargo.toml +++ b/crates/web-plugins/didcomm-messaging/protocols/forward/Cargo.toml @@ -22,4 +22,4 @@ keystore = { workspace = true, features = ["test-utils"] } shared = { workspace = true, features = ["test-utils"] } did-utils.workspace = true uuid = { workspace = true, features = ["v4"] } -tokio = { version = "1.27.0", default-features = false, features = ["macros", "rt"] } +tokio = { version = "1.42.0", default-features = false, features = ["macros", "rt"] } diff --git a/crates/web-plugins/didcomm-messaging/protocols/mediator-coordination/Cargo.toml b/crates/web-plugins/didcomm-messaging/protocols/mediator-coordination/Cargo.toml index 06dbca29..ae2f366d 100644 --- a/crates/web-plugins/didcomm-messaging/protocols/mediator-coordination/Cargo.toml +++ b/crates/web-plugins/didcomm-messaging/protocols/mediator-coordination/Cargo.toml @@ -25,9 +25,9 @@ uuid = { workspace = true, features = ["v4"] } hyper = { workspace = true, features = ["full"] } [dev-dependencies] -hyper = "0.14.27" +hyper = "1.5.2" json-canon = "0.1.3" -tokio = { version = "1.27.0", default-features = false, features = [ +tokio = { version = "1.42.0", default-features = false, features = [ "macros", "rt", ] } diff --git a/crates/web-plugins/didcomm-messaging/protocols/pickup/Cargo.toml b/crates/web-plugins/didcomm-messaging/protocols/pickup/Cargo.toml index 96521436..9ef52784 100644 --- a/crates/web-plugins/didcomm-messaging/protocols/pickup/Cargo.toml +++ b/crates/web-plugins/didcomm-messaging/protocols/pickup/Cargo.toml @@ -18,7 +18,7 @@ axum = { workspace = true, features = ["macros"] } [dev-dependencies] shared = { workspace = true, features = ["test-utils"] } -tokio = { version = "1.27.0", default-features = false, features = [ +tokio = { version = "1.42.0", default-features = false, features = [ "macros", "rt", ] } diff --git a/crates/web-plugins/didcomm-messaging/protocols/trust-ping/Cargo.toml b/crates/web-plugins/didcomm-messaging/protocols/trust-ping/Cargo.toml index 8dfe9f5f..1648d9d4 100644 --- a/crates/web-plugins/didcomm-messaging/protocols/trust-ping/Cargo.toml +++ b/crates/web-plugins/didcomm-messaging/protocols/trust-ping/Cargo.toml @@ -17,7 +17,7 @@ axum = { workspace = true, features = ["macros"] } [dev-dependencies] shared = { workspace = true, features = ["test-utils"] } -tokio = { version = "1.27.0", default-features = false, features = [ +tokio = { version = "1.42.0", default-features = false, features = [ "macros", "rt", ] } diff --git a/crates/web-plugins/didcomm-messaging/shared/Cargo.toml b/crates/web-plugins/didcomm-messaging/shared/Cargo.toml index 6fa47937..3509ec95 100644 --- a/crates/web-plugins/didcomm-messaging/shared/Cargo.toml +++ b/crates/web-plugins/didcomm-messaging/shared/Cargo.toml @@ -20,7 +20,7 @@ tokio = { workspace = true, features = ["full"] } didcomm = { workspace = true, features = ["uniffi"] } [dev-dependencies] -tokio = { version = "1.27.0", default-features = false, features = [ +tokio = { version = "1.42.0", default-features = false, features = [ "macros", "rt", ] } diff --git a/crates/web-plugins/didcomm-messaging/src/error.rs b/crates/web-plugins/didcomm-messaging/src/error.rs index a4eeec02..de3a9a87 100644 --- a/crates/web-plugins/didcomm-messaging/src/error.rs +++ b/crates/web-plugins/didcomm-messaging/src/error.rs @@ -1,5 +1,4 @@ use axum::Json; -use didcomm::error::ErrorKind as DidcommErrorKind; use serde_json::{json, Value}; use thiserror::Error; @@ -10,14 +9,10 @@ pub(crate) enum Error { AnonymousPacker, #[error("assumed didcomm-encrypted message is malformed")] MalformedDidcommEncrypted, - #[error("could not unpack message")] - MessageUnpackingFailure, - #[error("could not pack message: {0}")] - MessagePackingFailure(DidcommErrorKind), + #[error("Internal server error")] + InternalServer, #[error("unsupported content-type, only accept application/didcomm-encrypted+json")] NotDidcommEncryptedPayload, - #[error("unparseable payload")] - UnparseablePayload, } impl Error { diff --git a/crates/web-plugins/didcomm-messaging/src/midlw.rs b/crates/web-plugins/didcomm-messaging/src/midlw.rs index 19774e54..bb2549c4 100644 --- a/crates/web-plugins/didcomm-messaging/src/midlw.rs +++ b/crates/web-plugins/didcomm-messaging/src/midlw.rs @@ -1,11 +1,13 @@ use axum::{ body::Body, extract::State, - http::{header, Request, StatusCode}, + http::{header, StatusCode}, middleware::Next, response::{IntoResponse, Response}, }; -use didcomm::{error::ErrorKind as DidcommErrorKind, Message, PackEncryptedOptions, UnpackOptions}; +use didcomm::{Message, PackEncryptedOptions, UnpackOptions}; +use http_body_util::BodyExt; +use hyper::Request; use serde_json::Value; use std::sync::Arc; @@ -24,7 +26,7 @@ use shared::{ pub async fn unpack_didcomm_message( State(state): State>, request: Request, - next: Next, + next: Next, ) -> Response { // Enforce request content type to match `didcomm-encrypted+json` let content_type = request @@ -38,15 +40,17 @@ pub async fn unpack_didcomm_message( // Extract request payload let (parts, body) = request.into_parts(); - let bytes = match hyper::body::to_bytes(body).await { - Ok(bytes) => bytes, + let collected = match BodyExt::collect(body).await { + Ok(collected) => collected, Err(_) => { - let response = (StatusCode::BAD_REQUEST, Error::UnparseablePayload.json()); + tracing::error!("Failed to parse request body"); + let response = (StatusCode::BAD_REQUEST, Error::InternalServer.json()); return response.into_response(); } }; + let bytes = collected.to_bytes(); let payload = String::from_utf8_lossy(&bytes); // Attempt to unpack request payload @@ -114,10 +118,11 @@ async fn unpack_payload( ) .await; - let (plain_message, metadata) = res.map_err(|_| { + let (plain_message, metadata) = res.map_err(|err| { + tracing::error!("Failed to unpack message: {err:?}"); let response = ( - StatusCode::BAD_REQUEST, - Error::MessageUnpackingFailure.json(), + StatusCode::INTERNAL_SERVER_ERROR, + Error::InternalServer.json(), ); response.into_response() @@ -151,9 +156,10 @@ pub async fn pack_response_message( let to = msg.to.as_ref().and_then(|v| v.first()); if from.is_none() || to.is_none() { + tracing::error!("Failed to pack message: missing from or to field"); let response = ( StatusCode::INTERNAL_SERVER_ERROR, - Error::MessagePackingFailure(DidcommErrorKind::Malformed).json(), + Error::InternalServer.json(), ); return Err(response.into_response()); @@ -170,9 +176,10 @@ pub async fn pack_response_message( .await .map(|(packed_message, _metadata)| serde_json::from_str(&packed_message).unwrap()) .map_err(|err| { + tracing::error!("Failed to pack message: {err:?}"); let response = ( StatusCode::INTERNAL_SERVER_ERROR, - Error::MessagePackingFailure(err.kind()).json(), + Error::InternalServer.json(), ); response.into_response() @@ -236,7 +243,7 @@ mod tests { .await .unwrap_err(), StatusCode::INTERNAL_SERVER_ERROR, - Error::MessagePackingFailure(DidcommErrorKind::Malformed), + Error::InternalServer, ) .await; } @@ -262,7 +269,7 @@ mod tests { .await .unwrap_err(), StatusCode::INTERNAL_SERVER_ERROR, - Error::MessagePackingFailure(DidcommErrorKind::Unsupported), + Error::InternalServer, ) .await; } @@ -274,8 +281,8 @@ mod tests { async fn _assert_midlw_err(err: Response, status: StatusCode, mediation_error: Error) { assert_eq!(err.status(), status); - let body = hyper::body::to_bytes(err.into_body()).await.unwrap(); - let body: Value = serde_json::from_slice(&body).unwrap(); + let body = BodyExt::collect(err.into_body()).await.unwrap(); + let body: Value = serde_json::from_slice(&body.to_bytes()).unwrap(); assert_eq!( json_canon::to_string(&body).unwrap(), diff --git a/crates/web-plugins/didcomm-messaging/src/web/dispatcher.rs b/crates/web-plugins/didcomm-messaging/src/web/dispatcher.rs index 42ba828f..347bf6ed 100644 --- a/crates/web-plugins/didcomm-messaging/src/web/dispatcher.rs +++ b/crates/web-plugins/didcomm-messaging/src/web/dispatcher.rs @@ -62,7 +62,8 @@ mod tests { use super::*; use crate::manager::MessagePluginContainer; use axum::Router; - use hyper::{Body, Method, Request}; + use http_body_util::{BodyExt, Full}; + use hyper::{body::Bytes, Method, Request}; use message_api::{MessageHandler, MessagePlugin, MessageRouter}; use once_cell::sync::Lazy; use serde_json::{json, Value}; @@ -188,7 +189,7 @@ mod tests { .uri(String::from("/")) .method(Method::POST) .header(CONTENT_TYPE, DIDCOMM_ENCRYPTED_MIME_TYPE) - .body(Body::from(packed_msg)) + .body(Full::new(Bytes::from(packed_msg))) .unwrap(), ) .await @@ -202,8 +203,8 @@ mod tests { ); // Parse response's body - let body = hyper::body::to_bytes(response.into_body()).await.unwrap(); - let body: Value = serde_json::from_slice(&body).unwrap(); + let body = BodyExt::collect(response.into_body()).await.unwrap(); + let body: Value = serde_json::from_slice(&body.to_bytes()).unwrap(); let response = serde_json::to_string(&body).unwrap(); // Decrypt response diff --git a/crates/web-plugins/oob-messages/Cargo.toml b/crates/web-plugins/oob-messages/Cargo.toml index b6ebda1c..ca1ee437 100644 --- a/crates/web-plugins/oob-messages/Cargo.toml +++ b/crates/web-plugins/oob-messages/Cargo.toml @@ -21,6 +21,6 @@ uuid = { workspace = true, features = ["fast-rng", "v4"] } [dev-dependencies] filesystem = { workspace = true, features = ["test-utils"] } -mockall = "0.13.0" -tokio = { version = "1.30.0", features = ["full"] } -tower = "0.4" +mockall = "0.13.1" +tokio = { version = "1.42.0", features = ["full"] } +tower = "0.5" diff --git a/crates/web-plugins/oob-messages/src/models.rs b/crates/web-plugins/oob-messages/src/models.rs index b41415a9..1526c2c5 100644 --- a/crates/web-plugins/oob-messages/src/models.rs +++ b/crates/web-plugins/oob-messages/src/models.rs @@ -1,5 +1,6 @@ use crate::constants::OOB_INVITATION_2_0; -use base64::{encode_config, STANDARD}; +use base64::engine::general_purpose::STANDARD; +use base64::Engine; use did_utils::didcore::Document; use filesystem::FileSystem; use image::{DynamicImage, Luma}; @@ -9,6 +10,7 @@ use qrcode::QrCode; use serde::{Deserialize, Serialize}; use serde_json::to_string; use std::collections::HashMap; +use std::io::Cursor; use std::sync::RwLock; #[cfg(test)] @@ -163,13 +165,16 @@ where // Convert the image to a PNG-encoded byte vector let dynamic_image = DynamicImage::ImageLuma8(image); - let mut buffer = Vec::new(); + let mut buffer = Cursor::new(Vec::new()); dynamic_image - .write_to(&mut buffer, image::ImageOutputFormat::Png) + .write_to(&mut buffer, image::ImageFormat::Png) .map_err(|err| format!("Error encoding image to PNG: {err}"))?; + // Get the encoded bytes from the cursor + let buffer = buffer.into_inner(); + // Save the PNG-encoded byte vector as a base64-encoded string - let base64_string = encode_config(&buffer, STANDARD); + let base64_string = STANDARD.encode(&buffer); // Save to file fs.write_with_lock(path.as_ref(), &base64_string) diff --git a/src/main.rs b/src/main.rs index f9c0a739..adfb7f5f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ -use axum::Server; +use std::net::SocketAddr; use didcomm_mediator::app; use eyre::{Result, WrapErr}; -use std::net::SocketAddr; +use tokio::net::TcpListener; #[tokio::main] async fn main() -> Result<()> { @@ -11,15 +11,17 @@ async fn main() -> Result<()> { // Enable logging config_tracing(); - // Start server + // Configure server let port = std::env::var("SERVER_LOCAL_PORT").unwrap_or("3000".to_owned()); - let addr: SocketAddr = format!("0.0.0.0:{port}") - .parse() + let port = port.parse().context("failed to parse port")?; + let addr = SocketAddr::from(([0, 0, 0, 0], port)); + let listener = TcpListener::bind(addr) + .await .context("failed to parse address")?; - tracing::debug!("listening on {}", addr); + tracing::debug!("listening on {addr}"); - generic_server_with_graceful_shutdown(addr) + generic_server_with_graceful_shutdown(listener) .await .map_err(|err| { tracing::error!("{err:?}"); @@ -29,14 +31,12 @@ async fn main() -> Result<()> { Ok(()) } -async fn generic_server_with_graceful_shutdown(addr: SocketAddr) -> Result<()> { +async fn generic_server_with_graceful_shutdown(listener: TcpListener) -> Result<()> { // Load plugins let (mut plugin_container, router) = app()?; - // Spawn task for server - - Server::bind(&addr) - .serve(router.into_make_service()) + // Start server + axum::serve(listener, router) .await .context("failed to start server")?; diff --git a/src/plugins/index/web.rs b/src/plugins/index/web.rs index 32930b94..eef061e8 100644 --- a/src/plugins/index/web.rs +++ b/src/plugins/index/web.rs @@ -29,6 +29,7 @@ mod tests { }; use serde_json::Value; use tower::util::ServiceExt; + use http_body_util::BodyExt; #[tokio::test] async fn index() { @@ -46,8 +47,8 @@ mod tests { assert_eq!(response.status(), StatusCode::OK); - let body = hyper::body::to_bytes(response.into_body()).await.unwrap(); - let body: Value = serde_json::from_slice(&body).unwrap(); + let body = BodyExt::collect(response.into_body()).await.unwrap(); + let body: Value = serde_json::from_slice(&body.to_bytes()).unwrap(); assert_eq!(body.get("app").unwrap(), &crate_name()); }