-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathservice_endpoint.rs
More file actions
295 lines (246 loc) · 8.96 KB
/
Copy pathservice_endpoint.rs
File metadata and controls
295 lines (246 loc) · 8.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
//! Service endpoint configuration for external validation
//!
//! This module provides types to represent service endpoints that can be
//! tested via HTTP or HTTPS. When TLS is enabled, the endpoint uses the
//! domain with HTTPS protocol and resolves it locally to the server IP.
use std::net::{IpAddr, SocketAddr};
use url::Url;
use crate::shared::DomainName;
/// Error when creating a `ServiceEndpoint` with an invalid URL
#[derive(Debug, Clone, PartialEq)]
pub struct InvalidServiceEndpointUrl {
/// The URL string that failed to parse
pub url_string: String,
/// The parse error message
pub reason: String,
}
impl std::fmt::Display for InvalidServiceEndpointUrl {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Invalid service endpoint URL '{}': {}",
self.url_string, self.reason
)
}
}
impl std::error::Error for InvalidServiceEndpointUrl {}
/// Represents a service endpoint for external validation testing
///
/// Internally stores a validated URL and the server IP address.
/// For HTTPS endpoints, the IP is used to resolve the domain locally
/// (since we can't rely on DNS for `.local` domains).
///
/// # Examples
///
/// ```
/// use std::net::SocketAddr;
/// use torrust_tracker_deployer_lib::infrastructure::external_validators::ServiceEndpoint;
///
/// // HTTP endpoint
/// let socket_addr: SocketAddr = "10.0.0.1:1212".parse().unwrap();
/// let endpoint = ServiceEndpoint::http(socket_addr, "/api/health_check").unwrap();
/// assert_eq!(endpoint.url().as_str(), "http://10.0.0.1:1212/api/health_check");
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct ServiceEndpoint {
/// The validated URL for this endpoint
url: Url,
/// The server IP address.
/// For HTTP: extracted from the socket address.
/// For HTTPS: used to resolve the domain locally.
server_ip: IpAddr,
}
impl ServiceEndpoint {
/// Create a new HTTP endpoint (no TLS)
///
/// # Arguments
///
/// * `socket_addr` - The IP address and port the service listens on
/// * `path` - The health check path (e.g., `/api/health_check`)
///
/// # Errors
///
/// Returns an error if the socket address and path don't form a valid URL.
pub fn http(
socket_addr: SocketAddr,
path: impl Into<String>,
) -> Result<Self, InvalidServiceEndpointUrl> {
let path = path.into();
let url_string = format!("http://{}:{}{}", socket_addr.ip(), socket_addr.port(), path); // DevSkim: ignore DS137138
let url = Url::parse(&url_string).map_err(|e| InvalidServiceEndpointUrl {
url_string,
reason: e.to_string(),
})?;
Ok(Self {
url,
server_ip: socket_addr.ip(),
})
}
/// Create a new HTTPS endpoint with TLS
///
/// # Arguments
///
/// * `domain` - The domain name for HTTPS access (required for certificate issuance)
/// * `path` - The health check path (e.g., `/api/health_check`)
/// * `server_ip` - The IP address to resolve the domain to
///
/// # Errors
///
/// Returns an error if the domain and path don't form a valid URL.
pub fn https(
domain: &DomainName,
path: impl Into<String>,
server_ip: IpAddr,
) -> Result<Self, InvalidServiceEndpointUrl> {
let path = path.into();
let url_string = format!("https://{}{}", domain.as_str(), path);
let url = Url::parse(&url_string).map_err(|e| InvalidServiceEndpointUrl {
url_string,
reason: e.to_string(),
})?;
Ok(Self { url, server_ip })
}
/// Returns the URL for this endpoint
#[must_use]
pub fn url(&self) -> &Url {
&self.url
}
/// Returns true if this endpoint uses TLS (HTTPS)
#[must_use]
pub fn uses_tls(&self) -> bool {
self.url.scheme() == "https"
}
/// Returns the server IP address
#[must_use]
pub fn server_ip(&self) -> IpAddr {
self.server_ip
}
/// Returns the port for this endpoint
///
/// For HTTP: the configured port from the URL.
/// For HTTPS: 443 (the default HTTPS port).
#[must_use]
pub fn port(&self) -> u16 {
self.url.port_or_known_default().unwrap_or(80)
}
/// Get the domain if this is an HTTPS endpoint
#[must_use]
pub fn domain(&self) -> Option<&str> {
if self.uses_tls() {
self.url.host_str()
} else {
None
}
}
/// Check if the domain ends with `.local` (for self-signed cert handling)
#[must_use]
pub fn is_local_domain(&self) -> bool {
self.domain().is_some_and(|d| {
std::path::Path::new(d)
.extension()
.is_some_and(|ext| ext.eq_ignore_ascii_case("local"))
})
}
/// Returns the socket address for connecting to this endpoint
///
/// Combines the server IP with the port from the URL.
#[must_use]
pub fn socket_addr(&self) -> SocketAddr {
SocketAddr::new(self.server_ip(), self.port())
}
}
#[cfg(test)]
mod tests {
use std::net::IpAddr;
use super::*;
fn test_ip() -> IpAddr {
"10.0.0.1".parse().unwrap()
}
fn test_socket_addr(port: u16) -> SocketAddr {
SocketAddr::new(test_ip(), port)
}
#[test]
fn it_should_create_http_endpoint() {
let endpoint = ServiceEndpoint::http(test_socket_addr(1212), "/api/health_check").unwrap();
assert_eq!(endpoint.server_ip(), test_ip());
assert_eq!(endpoint.port(), 1212);
assert!(!endpoint.uses_tls());
assert!(endpoint.domain().is_none());
}
#[test]
fn it_should_create_https_endpoint() {
let domain = DomainName::new("api.tracker.local").unwrap();
let endpoint = ServiceEndpoint::https(&domain, "/api/health_check", test_ip()).unwrap();
assert_eq!(endpoint.server_ip(), test_ip());
assert_eq!(endpoint.port(), 443);
assert!(endpoint.uses_tls());
assert_eq!(endpoint.domain().unwrap(), "api.tracker.local");
}
#[test]
fn it_should_build_http_url() {
let endpoint = ServiceEndpoint::http(test_socket_addr(1212), "/api/health_check").unwrap();
assert_eq!(
endpoint.url().as_str(),
"http://10.0.0.1:1212/api/health_check" // DevSkim: ignore DS137138
);
}
#[test]
fn it_should_build_https_url() {
let domain = DomainName::new("api.tracker.local").unwrap();
let endpoint = ServiceEndpoint::https(&domain, "/api/health_check", test_ip()).unwrap();
// HTTPS uses domain, not IP
assert_eq!(
endpoint.url().as_str(),
"https://api.tracker.local/api/health_check"
);
}
#[test]
fn it_should_detect_local_domain() {
let domain = DomainName::new("api.tracker.local").unwrap();
let endpoint = ServiceEndpoint::https(&domain, "/api/health_check", test_ip()).unwrap();
assert!(endpoint.is_local_domain());
}
#[test]
fn it_should_not_detect_non_local_domain_as_local() {
let domain = DomainName::new("api.tracker.example.com").unwrap();
let endpoint = ServiceEndpoint::https(&domain, "/api/health_check", test_ip()).unwrap();
assert!(!endpoint.is_local_domain());
}
#[test]
fn it_should_return_port_443_for_https() {
let domain = DomainName::new("api.tracker.local").unwrap();
let endpoint = ServiceEndpoint::https(&domain, "/api/health_check", test_ip()).unwrap();
assert_eq!(endpoint.port(), 443);
}
#[test]
fn it_should_return_configured_port_for_http() {
let endpoint = ServiceEndpoint::http(test_socket_addr(1212), "/api/health_check").unwrap();
assert_eq!(endpoint.port(), 1212);
}
#[test]
fn it_should_return_socket_addr_for_http() {
let endpoint = ServiceEndpoint::http(test_socket_addr(1212), "/api/health_check").unwrap();
assert_eq!(endpoint.socket_addr(), test_socket_addr(1212));
}
#[test]
fn it_should_return_socket_addr_with_port_443_for_https() {
let domain = DomainName::new("api.tracker.local").unwrap();
let endpoint = ServiceEndpoint::https(&domain, "/api/health_check", test_ip()).unwrap();
assert_eq!(endpoint.socket_addr(), test_socket_addr(443));
}
#[test]
fn it_should_return_error_for_invalid_http_path() {
// A path with invalid characters that would break URL parsing
let result = ServiceEndpoint::http(test_socket_addr(1212), "not a valid path\x00");
assert!(result.is_err());
let err = result.unwrap_err();
assert!(err.url_string.contains("not a valid path"));
}
#[test]
fn it_should_return_url_reference() {
let endpoint = ServiceEndpoint::http(test_socket_addr(1212), "/api/health_check").unwrap();
// url() returns a reference, not a clone
let url_ref: &Url = endpoint.url();
assert_eq!(url_ref.path(), "/api/health_check");
}
}