Skip to content

Commit bb9e041

Browse files
aqian01jszwedko
andcommitted
fix(agent-data-plane): preserve DNS for hostname IPC endpoints (1.3.x backport) (#2069)
## Summary Follow-up to PR #2059. This keeps the no-DNS IPC behavior for literal-IP endpoints like `127.0.0.1`, but preserves DNS resolution when the configured IPC endpoint host is a hostname. Also includes the small doc wording cleanup on `without_dns_resolution()`. ## Testing - `cargo fmt --package datadog-agent-commons --package saluki-io --check` - `cargo test -p datadog-agent-commons ipc::client::tests` Note: full `cargo fmt --check` still hits existing generated-file formatting diffs under stable rustfmt on this branch. Co-authored-by: jesse.szwedko <jesse.szwedko@datadoghq.com>
1 parent 32e815b commit bb9e041

2 files changed

Lines changed: 45 additions & 5 deletions

File tree

  • lib
    • datadog-agent/commons/src/ipc/client
    • saluki-io/src/net/client/http

lib/datadog-agent/commons/src/ipc/client/mod.rs

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Helpers for interacting with the Datadog Agent.
22
3-
use std::time::Duration;
3+
use std::{net::IpAddr, time::Duration};
44

55
use backon::Retryable as _;
66
use datadog_protos::agent::v1::{RefreshRemoteAgentRequest, RegisterRemoteAgentRequest, RegisterRemoteAgentResponse};
@@ -15,7 +15,7 @@ use saluki_error::{generic_error, ErrorContext as _, GenericError};
1515
use saluki_io::net::client::http::HttpsCapableConnectorBuilder;
1616
use tonic::{
1717
service::interceptor::InterceptedService,
18-
transport::{Channel, Endpoint},
18+
transport::{Channel, Endpoint, Uri},
1919
Code, Request, Response,
2020
};
2121
use tracing::warn;
@@ -68,15 +68,20 @@ impl RemoteAgentClient {
6868
let auth_interceptor = BearerAuthInterceptor::from_file(&config.auth().auth_token_file_path()).await?;
6969
let ipc_cert_file_path = config.auth().ipc_cert_file_path();
7070
let client_tls_config = build_ipc_client_ipc_tls_config(ipc_cert_file_path).await?;
71-
let connector_builder = HttpsCapableConnectorBuilder::default().without_dns_resolution();
71+
let endpoint = config.endpoint()?;
72+
let connector_builder = HttpsCapableConnectorBuilder::default();
73+
let connector_builder = if endpoint_requires_dns_resolution(&endpoint) {
74+
connector_builder
75+
} else {
76+
connector_builder.without_dns_resolution()
77+
};
7278
#[cfg(target_os = "linux")]
7379
let connector_builder = if let Some(addr) = config.vsock_addr()? {
7480
connector_builder.with_vsock_addr(addr)
7581
} else {
7682
connector_builder
7783
};
7884
let https_connector = connector_builder.build(client_tls_config)?;
79-
let endpoint = config.endpoint()?;
8085
let channel = Endpoint::from(endpoint.clone())
8186
.connect_timeout(Duration::from_secs(2))
8287
.connect_with_connector(https_connector)
@@ -240,6 +245,19 @@ impl RemoteAgentClient {
240245
}
241246
}
242247

248+
fn endpoint_requires_dns_resolution(endpoint: &Uri) -> bool {
249+
match endpoint.host() {
250+
Some(host) => {
251+
let host = host
252+
.strip_prefix('[')
253+
.and_then(|host| host.strip_suffix(']'))
254+
.unwrap_or(host);
255+
host.parse::<IpAddr>().is_err()
256+
}
257+
None => false,
258+
}
259+
}
260+
243261
async fn try_query_agent_api(
244262
client: &mut AgentSecureClient<InterceptedService<Channel, BearerAuthInterceptor>>,
245263
) -> Result<(), GenericError> {
@@ -260,3 +278,25 @@ async fn try_query_agent_api(
260278
},
261279
}
262280
}
281+
282+
#[cfg(test)]
283+
mod tests {
284+
use tonic::transport::Uri;
285+
286+
use super::endpoint_requires_dns_resolution;
287+
288+
#[test]
289+
fn hostname_endpoint_requires_dns_resolution() {
290+
let endpoint = "https://datadog-agent:5001".parse::<Uri>().expect("valid URI");
291+
assert!(endpoint_requires_dns_resolution(&endpoint));
292+
}
293+
294+
#[test]
295+
fn literal_ip_endpoints_do_not_require_dns_resolution() {
296+
let ipv4_endpoint = "https://127.0.0.1:5001".parse::<Uri>().expect("valid URI");
297+
assert!(!endpoint_requires_dns_resolution(&ipv4_endpoint));
298+
299+
let ipv6_endpoint = "https://[::1]:5001".parse::<Uri>().expect("valid URI");
300+
assert!(!endpoint_requires_dns_resolution(&ipv6_endpoint));
301+
}
302+
}

lib/saluki-io/src/net/client/http/conn.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ impl HttpsCapableConnectorBuilder {
483483
/// DNS, such as Unix sockets, vsock, or literal-IP TCP endpoints. Hostname-based TCP
484484
/// destinations will fail to resolve when this is enabled.
485485
///
486-
/// Defaults to enabled.
486+
/// DNS resolution is enabled by default.
487487
pub fn without_dns_resolution(mut self) -> Self {
488488
self.dns_resolution_disabled = true;
489489
self

0 commit comments

Comments
 (0)