Skip to content

Commit b05ffa3

Browse files
authored
fix(sdk): allow hostnames in server address configuration (#2923)
1 parent 2284d0f commit b05ffa3

7 files changed

Lines changed: 335 additions & 43 deletions

File tree

core/common/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ pub use utils::crypto::*;
127127
pub use utils::duration::{IggyDuration, SEC_IN_MICRO};
128128
pub use utils::expiry::IggyExpiry;
129129
pub use utils::hash::*;
130+
pub use utils::net::validate_server_address;
130131
pub use utils::personal_access_token_expiry::PersonalAccessTokenExpiry;
131132
pub use utils::random_id;
132133
pub use utils::serde_secret;

core/common/src/types/configuration/tcp_config/tcp_client_config_builder.rs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
use crate::{AutoLogin, IggyDuration, IggyError, TcpClientConfig};
19-
use std::net::SocketAddr;
18+
use crate::{AutoLogin, IggyDuration, IggyError, TcpClientConfig, validate_server_address};
2019

2120
/// Builder for the TCP client configuration.
2221
/// Allows configuring the TCP client with custom settings or using defaults:
@@ -103,13 +102,9 @@ impl TcpClientConfigBuilder {
103102
}
104103

105104
/// Builds the TCP client configuration.
106-
pub fn build(self) -> Result<TcpClientConfig, IggyError> {
107-
let addr = self.config.server_address.trim();
108-
109-
if addr.parse::<SocketAddr>().is_err() {
110-
let (ip, port) = addr.rsplit_once(':').unwrap_or((addr, ""));
111-
return Err(IggyError::InvalidIpAddress(ip.to_owned(), port.to_owned()));
112-
}
105+
pub fn build(mut self) -> Result<TcpClientConfig, IggyError> {
106+
self.config.server_address = self.config.server_address.trim().to_string();
107+
validate_server_address(&self.config.server_address)?;
113108

114109
Ok(self.config)
115110
}
@@ -148,12 +143,16 @@ mod tests {
148143
}
149144

150145
#[test]
151-
fn invalid_ip_should_fail() {
146+
fn valid_dns_name_should_succeed() {
147+
let builder = builder_with_address("localhost:8080");
148+
assert!(builder.build().is_ok());
149+
}
150+
151+
#[test]
152+
fn unresolvable_hostname_should_succeed() {
153+
// Format is valid; DNS resolution is not attempted
152154
let builder = builder_with_address("invalid.ip:8080");
153-
assert!(matches!(
154-
builder.build(),
155-
Err(IggyError::InvalidIpAddress(_, _))
156-
));
155+
assert!(builder.build().is_ok());
157156
}
158157

159158
#[test]
@@ -182,4 +181,10 @@ mod tests {
182181
Err(IggyError::InvalidIpAddress(_, _))
183182
));
184183
}
184+
185+
#[test]
186+
fn docker_compose_service_name_should_succeed() {
187+
let builder = builder_with_address("iggy-server:8090");
188+
assert!(builder.build().is_ok());
189+
}
185190
}

core/common/src/types/configuration/websocket_config/websocket_client_config_builder.rs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
* under the License.
1717
*/
1818

19-
use crate::{AutoLogin, IggyDuration, IggyError, WebSocketClientConfig};
20-
use std::net::SocketAddr;
19+
use crate::{AutoLogin, IggyDuration, IggyError, WebSocketClientConfig, validate_server_address};
2120

2221
/// Builder for the WebSocket client configuration.
2322
/// Allows configuring the WebSocket client with custom settings or using defaults:
@@ -139,19 +138,9 @@ impl WebSocketClientConfigBuilder {
139138
}
140139

141140
/// Builds the WebSocket client configuration.
142-
pub fn build(self) -> Result<WebSocketClientConfig, IggyError> {
143-
let addr = self.config.server_address.trim();
144-
145-
// Check if it's a valid socket address or host:port format
146-
if addr.parse::<SocketAddr>().is_err() {
147-
let (host, port) = addr.rsplit_once(':').unwrap_or((addr, ""));
148-
if port.is_empty() || port.parse::<u16>().is_err() {
149-
return Err(IggyError::InvalidIpAddress(
150-
host.to_owned(),
151-
port.to_owned(),
152-
));
153-
}
154-
}
141+
pub fn build(mut self) -> Result<WebSocketClientConfig, IggyError> {
142+
self.config.server_address = self.config.server_address.trim().to_string();
143+
validate_server_address(&self.config.server_address)?;
155144

156145
Ok(self.config)
157146
}

core/common/src/utils/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub(crate) mod crypto;
2323
pub(crate) mod duration;
2424
pub(crate) mod expiry;
2525
pub(crate) mod hash;
26+
pub(crate) mod net;
2627
pub(crate) mod personal_access_token_expiry;
2728
pub mod random_id;
2829
pub mod serde_secret;

0 commit comments

Comments
 (0)