From 27af06c86732e18aec23fd18aae288382606f7e9 Mon Sep 17 00:00:00 2001 From: Asger Hautop Drewsen Date: Wed, 28 Feb 2024 21:57:17 +0100 Subject: [PATCH] Setup TLS adaptor when parsing an amqprs uri (#121) * Setup TLS adaptor when parsing an amqprs uri * Don't use too new Rust features * Fix clippy lints * Fix compiling with urlspec, but not tls feature * Add test for trying to parse an amqps url without the tls feature enabled * Check tls adaptor domain in test * Remove test that doesn't apply anymore --- amqprs/src/api/connection.rs | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/amqprs/src/api/connection.rs b/amqprs/src/api/connection.rs index bd7acc9..d2e5fa8 100644 --- a/amqprs/src/api/connection.rs +++ b/amqprs/src/api/connection.rs @@ -475,8 +475,9 @@ impl TryFrom<&str> for OpenConnectionArguments { .unwrap_or("guest"); // Apply authority + let host = pu_authority.host().to_string(); let mut args = OpenConnectionArguments::new( - pu_authority.host().to_string().as_str(), + host.as_str(), pu_authority.port().unwrap_or(default_port), pu_authority_username, pu_authority_password, @@ -519,6 +520,17 @@ impl TryFrom<&str> for OpenConnectionArguments { .unwrap_or(DEFAULT_HEARTBEAT); args.heartbeat(heartbeat); + if scheme == AMQPS_SCHEME { + #[cfg(feature = "tls")] + args.tls_adaptor( + TlsAdaptor::without_client_auth(None, host.to_string()) + .map_err(|e| Error::UriError(format!("error creating TLS adaptor: {}", e)))?, + ); + + #[cfg(not(feature = "tls"))] + return Err(Error::UriError("can't create amqps url without the `tls` feature enabled".to_string())); + } + Ok(args) } } @@ -1472,6 +1484,15 @@ mod tests { assert_eq!(args.heartbeat, 30); } + #[cfg(all(feature = "urispec", not(feature = "tls")))] + #[test] + fn test_urispec_amqps_without_tls() { + match OpenConnectionArguments::try_from("amqps://user:bitnami@localhost?heartbeat=10") { + Ok(_) => panic!("Unexpected ok"), + Err(e) => assert!(matches!(e, crate::api::Error::UriError(_))), + } + } + #[cfg(all(feature = "urispec", feature = "tls"))] #[test] fn test_urispec_amqps() { @@ -1481,15 +1502,8 @@ mod tests { assert_eq!(args.port, 5671); assert_eq!(args.virtual_host, "/"); assert_eq!(args.heartbeat, 10); - } - - #[cfg(all(feature = "urispec", feature = "tls"))] - #[tokio::test] - #[should_panic(expected = "UriError")] - async fn test_amqps_scheme_without_tls() { - let args = OpenConnectionArguments::try_from("amqps://user:bitnami@localhost?heartbeat=10") - .unwrap(); - Connection::open(&args).await.unwrap(); + let tls_adaptor = args.tls_adaptor.unwrap(); + assert_eq!(tls_adaptor.domain, "localhost"); } #[cfg(all(feature = "urispec", feature = "tls"))]