Skip to content

Commit ed8db60

Browse files
author
Shlomi Kushchi
committed
CR fixes
1 parent c9a5d1d commit ed8db60

File tree

8 files changed

+79
-56
lines changed

8 files changed

+79
-56
lines changed

sdk/core/azure_core_amqp/Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ serde.workspace = true
2929
serde_amqp = { workspace = true, optional = true }
3030
serde_bytes = { workspace = true, optional = true }
3131
tokio.workspace = true
32-
tokio-socks = "0.5"
33-
tokio-native-tls = "0.3"
34-
native-tls = "0.2"
32+
tokio-socks = { version = "0.5", optional = true }
33+
native-tls = { version = "0.2", optional = true }
34+
tokio-native-tls = { version = "0.3", optional = true }
3535
tracing.workspace = true
3636
typespec = { workspace = true, features = ["amqp"] }
3737
typespec_macros.workspace = true
@@ -52,6 +52,7 @@ fe2o3_amqp = [
5252
"serde_amqp",
5353
"serde_bytes",
5454
]
55+
socks5 = ["dep:tokio-socks", "dep:native-tls", "dep:tokio-native-tls"]
5556

5657
[lints]
5758
workspace = true

sdk/core/azure_core_amqp/README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,16 @@ This crate is part of a collection of crates: for more information please refer
88

99
## SOCKS5 Proxy Support
1010

11-
This crate supports SOCKS5 proxy connections for corporate environments. SOCKS5 support is enabled by configuring the `custom_endpoint` option with a SOCKS5 URL:
11+
This crate supports SOCKS5 proxy connections for corporate environments.
12+
13+
**Note**: SOCKS5 support requires enabling the `socks5` feature:
14+
15+
```toml
16+
[dependencies]
17+
azure_core_amqp = { version = "0.8", features = ["socks5"] }
18+
```
19+
20+
SOCKS5 support is enabled by configuring the `custom_endpoint` option with a SOCKS5 URL:
1221

1322
```rust,no_run
1423
use azure_core::http::Url;

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

Lines changed: 51 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use super::error::{Fe2o3ConnectionError, Fe2o3ConnectionOpenError, Fe2o3TransportError};
55
use crate::connection::{AmqpConnectionApis, AmqpConnectionOptions};
66
use crate::error::AmqpErrorKind;
7+
#[cfg(feature = "socks5")]
78
use crate::socks5::SocksConnection;
89
use crate::value::{AmqpOrderedMap, AmqpSymbol, AmqpValue};
910
use azure_core::{http::Url, Result};
@@ -45,6 +46,7 @@ impl Drop for Fe2o3AmqpConnection {
4546
}
4647
}
4748

49+
4850
#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
4951
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
5052
impl AmqpConnectionApis for Fe2o3AmqpConnection {
@@ -123,58 +125,59 @@ impl AmqpConnectionApis for Fe2o3AmqpConnection {
123125
} else {
124126
"direct"
125127
};
126-
let connection = if endpoint.scheme() == "socks5" || endpoint.scheme() == "socks5h" {
127-
debug!(
128-
connection_id = %id,
129-
proxy_scheme = %endpoint.scheme(),
130-
target_host = %url.host_str().unwrap_or("unknown"),
131-
"Opening AMQP connection through SOCKS5 proxy"
132-
);
133-
134-
// Use fe2o3's open_with_stream() to inject SOCKS5 connection
135-
let stream = SocksConnection::connect(&endpoint, &url)
136-
.await
137-
.map_err(|e| {
138-
error!(
128+
let connection = {
129+
#[cfg(feature = "socks5")]
130+
{
131+
if endpoint.scheme() == "socks5" || endpoint.scheme() == "socks5h" {
132+
debug!(
139133
connection_id = %id,
140-
proxy_url = %SocksConnection::mask_credentials(&endpoint),
141-
error = %e,
142-
"Failed to establish SOCKS5 connection"
134+
proxy_scheme = %endpoint.scheme(),
135+
target_host = %url.host_str().unwrap_or("unknown"),
136+
"Opening AMQP connection through SOCKS5 proxy"
143137
);
144-
e
145-
})?;
146138

147-
debug!(
148-
connection_id = %id,
149-
"Opening AMQP connection with SOCKS5 stream"
150-
);
139+
let stream =
140+
SocksConnection::connect(&endpoint, &url)
141+
.await
142+
.map_err(|e| {
143+
error!(
144+
connection_id = %id,
145+
proxy_url = %SocksConnection::mask_credentials(&endpoint),
146+
error = %e,
147+
"Failed to establish SOCKS5 connection"
148+
);
149+
e
150+
})?;
151151

152-
builder.open_with_stream(stream).await.map_err(|e| {
153-
error!(
154-
connection_id = %id,
155-
error = %e,
156-
"Failed to open AMQP connection over SOCKS5 stream"
157-
);
158-
azure_core::Error::from(Fe2o3ConnectionOpenError(e))
159-
})?
160-
} else {
161-
debug!(
162-
connection_id = %id,
163-
endpoint = %endpoint,
164-
"Opening direct AMQP connection"
165-
);
166-
167-
// Maintain existing behavior for non-SOCKS5 URLs
168-
let endpoint_str = endpoint.to_string();
169-
builder.open(endpoint).await.map_err(|e| {
170-
error!(
171-
connection_id = %id,
172-
endpoint = %endpoint_str,
173-
error = %e,
174-
"Failed to open direct AMQP connection"
175-
);
176-
azure_core::Error::from(Fe2o3ConnectionOpenError(e))
177-
})?
152+
debug!(connection_id = %id, "Opening AMQP connection with SOCKS5 stream");
153+
builder.open_with_stream(stream).await.map_err(|e| {
154+
error!(connection_id = %id, error = %e, "Failed to open AMQP connection over SOCKS5 stream");
155+
azure_core::Error::from(Fe2o3ConnectionOpenError(e))
156+
})?
157+
} else {
158+
debug!(connection_id = %id, endpoint = %endpoint, "Opening direct AMQP connection");
159+
let endpoint_str = endpoint.to_string();
160+
builder.open(endpoint).await.map_err(|e| {
161+
error!(connection_id = %id, endpoint = %endpoint_str, error = %e, "Failed to open direct AMQP connection");
162+
azure_core::Error::from(Fe2o3ConnectionOpenError(e))
163+
})?
164+
}
165+
}
166+
#[cfg(not(feature = "socks5"))]
167+
{
168+
if endpoint.scheme() == "socks5" || endpoint.scheme() == "socks5h" {
169+
return Err(azure_core::Error::with_message(
170+
azure_core::error::ErrorKind::Amqp,
171+
"SOCKS5 proxy support is not enabled. Enable the 'socks5' feature to use SOCKS5 proxies."
172+
));
173+
}
174+
debug!(connection_id = %id, endpoint = %endpoint, "Opening direct AMQP connection");
175+
let endpoint_str = endpoint.to_string();
176+
builder.open(endpoint).await.map_err(|e| {
177+
error!(connection_id = %id, endpoint = %endpoint_str, error = %e, "Failed to open direct AMQP connection");
178+
azure_core::Error::from(Fe2o3ConnectionOpenError(e))
179+
})?
180+
}
178181
};
179182

180183
debug!(

sdk/core/azure_core_amqp/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ mod receiver;
2121
mod sender;
2222
mod session;
2323
mod simple_value;
24+
#[cfg(feature = "socks5")]
2425
mod socks5;
2526
mod value;
2627

sdk/eventhubs/azure_messaging_eventhubs/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ tracing-subscriber = { workspace = true, features = ["env-filter", "fmt"] }
4444

4545
[features]
4646
in_memory_checkpoint_store = []
47+
socks5 = ["azure_core_amqp/socks5"]
4748
default = ["azure_core_amqp/default"]
4849

4950
[[bench]]

sdk/eventhubs/azure_messaging_eventhubs/examples/eventhubs_socks5_proxy.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT license.
33

4+
#![cfg(feature = "socks5")]
5+
46
//! This sample demonstrates how to connect to an Event Hub through a SOCKS5 proxy using the `ProducerClient`.
57
//!
68
//! # SOCKS5 Proxy Setup
@@ -31,8 +33,10 @@
3133
//!
3234
//! # Usage
3335
//!
36+
//! **Note**: This example requires the `socks5` feature to be enabled.
37+
//!
3438
//! ```bash
35-
//! cargo run --example eventhubs_socks5_proxy
39+
//! cargo run --features socks5 --example eventhubs_socks5_proxy
3640
//! ```
3741
3842
use azure_identity::DeveloperToolsCredential;

sdk/eventhubs/azure_messaging_eventhubs/tests/eventhubs_socks5_messaging.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) Microsoft Corporation. All Rights reserved
22
// Licensed under the MIT license.
33

4+
#![cfg(feature = "socks5")]
5+
46
//! SOCKS5 Proxy Messaging Tests for EventHubs
57
//!
68
//! These tests verify EventHub message operations (send/receive) work through SOCKS5 proxies.
@@ -21,7 +23,7 @@
2123
//!
2224
//! 3. **Run messaging tests**:
2325
//! ```bash
24-
//! AZURE_TEST_MODE=live cargo test --test eventhubs_socks5_messaging
26+
//! AZURE_TEST_MODE=live cargo test --features socks5 --test eventhubs_socks5_messaging
2527
//! ```
2628
//!
2729
//! # Test Coverage

sdk/eventhubs/azure_messaging_eventhubs/tests/eventhubs_socks5_proxy.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) Microsoft Corporation. All Rights reserved
22
// Licensed under the MIT license.
33

4+
#![cfg(feature = "socks5")]
5+
46
//! SOCKS5 Proxy Integration Tests for EventHubs
57
//!
68
//! These tests verify EventHub connections work through SOCKS5 proxies.
@@ -26,9 +28,9 @@
2628
//! export SOCKS5_PROXY_URL_WITH_AUTH="socks5://user:pass@my-proxy-domain:12345"
2729
//! ```
2830
//!
29-
//! 3. **Run live tests**:
31+
//! 3. **Run live tests** (requires the `socks5` feature):
3032
//! ```bash
31-
//! AZURE_TEST_MODE=live cargo test --test eventhubs_socks5_proxy
33+
//! AZURE_TEST_MODE=live cargo test --features socks5 --test eventhubs_socks5_proxy
3234
//! ```
3335
//!
3436
//! # Default Configuration

0 commit comments

Comments
 (0)