Skip to content

Commit

Permalink
Setup TLS adaptor when parsing an amqprs uri (#121)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
tyilo authored Feb 28, 2024
1 parent 07b5924 commit 27af06c
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions amqprs/src/api/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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() {
Expand All @@ -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"))]
Expand Down

0 comments on commit 27af06c

Please sign in to comment.