Skip to content

Commit a52ce8f

Browse files
author
Shlomi Kushchi
committed
lint
1 parent 4d49a6f commit a52ce8f

File tree

2 files changed

+73
-59
lines changed

2 files changed

+73
-59
lines changed

sdk/core/azure_core_amqp/src/fe2o3/connection.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,11 @@ impl AmqpConnectionApis for Fe2o3AmqpConnection {
118118
builder = builder.hostname(url.host_str());
119119
}
120120

121-
let connection_type = if endpoint.scheme().starts_with("socks5") { "socks5" } else { "direct" };
121+
let connection_type = if endpoint.scheme().starts_with("socks5") {
122+
"socks5"
123+
} else {
124+
"direct"
125+
};
122126
let connection = if endpoint.scheme() == "socks5" || endpoint.scheme() == "socks5h" {
123127
debug!(
124128
connection_id = %id,
@@ -128,7 +132,8 @@ impl AmqpConnectionApis for Fe2o3AmqpConnection {
128132
);
129133

130134
// Use fe2o3's open_with_stream() to inject SOCKS5 connection
131-
let stream = SocksConnection::connect(&endpoint, &url).await
135+
let stream = SocksConnection::connect(&endpoint, &url)
136+
.await
132137
.map_err(|e| {
133138
error!(
134139
connection_id = %id,

sdk/core/azure_core_amqp/src/socks5.rs

Lines changed: 66 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,14 @@
125125
//!
126126
//! See [`SocksConnection`] for detailed usage examples and API documentation.
127127
128-
use tokio_socks::{tcp::Socks5Stream, TargetAddr};
129-
use tokio::io::{AsyncRead, AsyncWrite};
130-
use azure_core::{http::Url, error::Result};
131-
use tracing::{debug, error, trace};
132-
use tokio_native_tls::TlsConnector;
128+
use azure_core::{error::Result, http::Url};
133129
use native_tls::TlsConnector as NativeTlsConnector;
134130
use std::pin::Pin;
135131
use std::task::{Context, Poll};
132+
use tokio::io::{AsyncRead, AsyncWrite};
133+
use tokio_native_tls::TlsConnector;
134+
use tokio_socks::{tcp::Socks5Stream, TargetAddr};
135+
use tracing::{debug, error, trace};
136136

137137
/// A trait that combines AsyncRead, AsyncWrite, Unpin, Send and Debug for SOCKS5 streams
138138
pub trait SocksStream: AsyncRead + AsyncWrite + Unpin + Send + std::fmt::Debug + 'static {}
@@ -311,14 +311,14 @@ impl SocksConnection {
311311
);
312312
return Err(azure_core::Error::with_message(
313313
azure_core::error::ErrorKind::Other,
314-
format!("Invalid SOCKS5 scheme: {}", url.scheme())
314+
format!("Invalid SOCKS5 scheme: {}", url.scheme()),
315315
));
316316
}
317317
if url.host_str().is_none() {
318318
error!("Missing host in SOCKS5 proxy URL");
319319
return Err(azure_core::Error::with_message(
320320
azure_core::error::ErrorKind::Other,
321-
"Missing host in SOCKS5 URL"
321+
"Missing host in SOCKS5 URL",
322322
));
323323
}
324324
Ok(())
@@ -406,10 +406,7 @@ impl SocksConnection {
406406
///
407407
/// Returns a boxed stream implementing [`SocksStream`] trait, ready for use
408408
/// with fe2o3-amqp's `Connection::open_with_stream()` method.
409-
pub async fn connect(
410-
proxy_url: &Url,
411-
target_url: &Url,
412-
) -> Result<Box<dyn SocksStream>> {
409+
pub async fn connect(proxy_url: &Url, target_url: &Url) -> Result<Box<dyn SocksStream>> {
413410
debug!(
414411
proxy_url = %Self::mask_credentials(proxy_url),
415412
target_host = %target_url.host_str().unwrap_or("unknown"),
@@ -421,14 +418,13 @@ impl SocksConnection {
421418
Self::validate_proxy_url(proxy_url)?;
422419

423420
// DNS resolution happens at proxy server for socks5h:// scheme
424-
let proxy_host = proxy_url.host_str()
425-
.ok_or_else(|| {
426-
error!("Missing proxy host in SOCKS5 URL");
427-
azure_core::Error::with_message(
428-
azure_core::error::ErrorKind::Other,
429-
"Missing proxy host in SOCKS5 URL"
430-
)
431-
})?;
421+
let proxy_host = proxy_url.host_str().ok_or_else(|| {
422+
error!("Missing proxy host in SOCKS5 URL");
423+
azure_core::Error::with_message(
424+
azure_core::error::ErrorKind::Other,
425+
"Missing proxy host in SOCKS5 URL",
426+
)
427+
})?;
432428
let proxy_port = proxy_url.port().unwrap_or(1080);
433429

434430
debug!(
@@ -441,7 +437,7 @@ impl SocksConnection {
441437
// Always use domain name - let SOCKS5 proxy handle resolution
442438
let target_addr = TargetAddr::Domain(
443439
target_url.host_str().unwrap_or("").into(),
444-
target_url.port().unwrap_or(5671)
440+
target_url.port().unwrap_or(5671),
445441
);
446442

447443
// Handle authentication if provided in URL
@@ -452,7 +448,7 @@ impl SocksConnection {
452448
error!("Empty username in SOCKS5 proxy URL");
453449
return Err(azure_core::Error::with_message(
454450
azure_core::error::ErrorKind::Other,
455-
"Empty username in SOCKS5 URL"
451+
"Empty username in SOCKS5 URL",
456452
));
457453
}
458454

@@ -465,8 +461,9 @@ impl SocksConnection {
465461
(proxy_host, proxy_port),
466462
target_addr,
467463
username,
468-
password
469-
).await
464+
password,
465+
)
466+
.await
470467
} else {
471468
debug!("Connecting to SOCKS5 proxy without authentication");
472469
Socks5Stream::connect((proxy_host, proxy_port), target_addr).await
@@ -479,11 +476,13 @@ impl SocksConnection {
479476
"SOCKS5 connection establishment failed"
480477
);
481478

482-
azure_core::Error::new(
483-
azure_core::error::ErrorKind::Other,
484-
Box::new(e)
485-
).with_context(format!("SOCKS5 connection failed: proxy={}, target={}",
486-
Self::mask_credentials(proxy_url), target_url))
479+
azure_core::Error::new(azure_core::error::ErrorKind::Other, Box::new(e)).with_context(
480+
format!(
481+
"SOCKS5 connection failed: proxy={}, target={}",
482+
Self::mask_credentials(proxy_url),
483+
target_url
484+
),
485+
)
487486
})?;
488487

489488
debug!(
@@ -493,7 +492,8 @@ impl SocksConnection {
493492
);
494493

495494
// Check if target URL requires TLS (amqps://)
496-
let requires_tls = target_url.scheme() == "amqps" || target_url.port().unwrap_or(5671) == 5671;
495+
let requires_tls =
496+
target_url.scheme() == "amqps" || target_url.port().unwrap_or(5671) == 5671;
497497

498498
if requires_tls {
499499
debug!(
@@ -502,30 +502,30 @@ impl SocksConnection {
502502
);
503503

504504
// Create TLS connector with default settings
505-
let native_connector = NativeTlsConnector::new()
506-
.map_err(|e| {
507-
error!(
508-
error = %e,
509-
"Failed to create TLS connector"
510-
);
511-
azure_core::Error::with_message(
512-
azure_core::error::ErrorKind::Other,
513-
format!("Failed to create TLS connector: {}", e)
514-
)
515-
})?;
505+
let native_connector = NativeTlsConnector::new().map_err(|e| {
506+
error!(
507+
error = %e,
508+
"Failed to create TLS connector"
509+
);
510+
azure_core::Error::with_message(
511+
azure_core::error::ErrorKind::Other,
512+
format!("Failed to create TLS connector: {}", e),
513+
)
514+
})?;
516515

517516
let connector = TlsConnector::from(native_connector);
518-
let target_host = target_url.host_str()
519-
.ok_or_else(|| {
520-
error!("Missing target host for TLS connection");
521-
azure_core::Error::with_message(
522-
azure_core::error::ErrorKind::Other,
523-
"Missing target host for TLS connection"
524-
)
525-
})?;
517+
let target_host = target_url.host_str().ok_or_else(|| {
518+
error!("Missing target host for TLS connection");
519+
azure_core::Error::with_message(
520+
azure_core::error::ErrorKind::Other,
521+
"Missing target host for TLS connection",
522+
)
523+
})?;
526524

527525
// Establish TLS connection over SOCKS5 stream
528-
let tls_stream = connector.connect(target_host, stream.into_inner()).await
526+
let tls_stream = connector
527+
.connect(target_host, stream.into_inner())
528+
.await
529529
.map_err(|e| {
530530
error!(
531531
target_host = %target_host,
@@ -534,7 +534,7 @@ impl SocksConnection {
534534
);
535535
azure_core::Error::with_message(
536536
azure_core::error::ErrorKind::Other,
537-
format!("TLS handshake failed: {}", e)
537+
format!("TLS handshake failed: {}", e),
538538
)
539539
})?;
540540

@@ -703,7 +703,8 @@ mod tests {
703703
#[test]
704704
fn test_mask_credentials_with_auth() {
705705
// Username and password should be masked
706-
let url_with_auth = Url::parse("socks5://username:[email protected]:1080").unwrap();
706+
let url_with_auth =
707+
Url::parse("socks5://username:[email protected]:1080").unwrap();
707708
let masked = SocksConnection::mask_credentials(&url_with_auth);
708709
assert_eq!(masked, "socks5://***:***@proxy.example.com:1080");
709710

@@ -724,20 +725,26 @@ mod tests {
724725
#[test]
725726
fn test_mask_credentials_special_characters() {
726727
// Test credentials with special characters (URL encoded)
727-
let url_special = Url::parse("socks5://user%40domain:p%[email protected]:1080").unwrap();
728+
let url_special =
729+
Url::parse("socks5://user%40domain:p%[email protected]:1080").unwrap();
728730
let masked = SocksConnection::mask_credentials(&url_special);
729731
assert_eq!(masked, "socks5://***:***@proxy.example.com:1080");
730732

731733
// Test with complex credentials
732-
let url_complex = Url::parse("socks5://admin:secretP%[email protected]:8080").unwrap();
734+
let url_complex =
735+
Url::parse("socks5://admin:secretP%[email protected]:8080").unwrap();
733736
let masked_complex = SocksConnection::mask_credentials(&url_complex);
734-
assert_eq!(masked_complex, "socks5://***:***@proxy-server.corp.com:8080");
737+
assert_eq!(
738+
masked_complex,
739+
"socks5://***:***@proxy-server.corp.com:8080"
740+
);
735741
}
736742

737743
#[test]
738744
fn test_mask_credentials_preserves_structure() {
739745
// Verify that masking preserves host, port, and scheme
740-
let original = Url::parse("socks5h://testuser:[email protected]:12345").unwrap();
746+
let original =
747+
Url::parse("socks5h://testuser:[email protected]:12345").unwrap();
741748
let masked = SocksConnection::mask_credentials(&original);
742749

743750
assert!(masked.starts_with("socks5h://"));
@@ -777,7 +784,9 @@ mod tests {
777784
// Verify credentials are not exposed
778785
assert!(!masked.contains("pass"));
779786
assert!(!masked.contains("secret"));
780-
assert!(!masked.contains("admin") || url_str.contains("admin") == masked.contains("admin"));
787+
assert!(
788+
!masked.contains("admin") || url_str.contains("admin") == masked.contains("admin")
789+
);
781790
}
782791
}
783-
}
792+
}

0 commit comments

Comments
 (0)