Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion mesh/crates/mesh-llm-host-runtime/src/plugin/health.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,32 @@ async fn probe_openai_compatible_http_endpoint(address: &str) -> EndpointHealthR
probe_openai_compatible_http_endpoint_for_protocol(address, None).await
}

fn is_loopback_host(url: &Url) -> bool {
match url.host() {
Some(url::Host::Ipv4(ip)) => ip.is_loopback(),
Some(url::Host::Ipv6(ip)) => ip.is_loopback(),
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
Some(url::Host::Domain(host)) => {
host.trim_end_matches('.').eq_ignore_ascii_case("localhost")
}
None => false,
}
}

/// Plain-HTTP probes never talk TLS, so skip loading system CA certificates;
/// loopback probes also bypass proxy settings and redirects, matching
/// `network::local_http::local_http_client`.
fn health_probe_client_builder(url: &Url) -> reqwest::ClientBuilder {
if url.scheme() == "http" {
if is_loopback_host(url) {
mesh_llm_system::local_http::local_http_client_builder()
} else {
reqwest::Client::builder().tls_certs_only([])
}
} else {
reqwest::Client::builder()
}
}

async fn probe_openai_compatible_http_endpoint_for_protocol(
address: &str,
protocol: Option<&str>,
Expand All @@ -400,7 +426,7 @@ async fn probe_openai_compatible_http_endpoint_for_protocol(
}
};

let client = match reqwest::Client::builder()
let client = match health_probe_client_builder(&models_url)
.timeout(std::time::Duration::from_secs(3))
.build()
{
Expand Down Expand Up @@ -939,3 +965,7 @@ mod managed_task_tests {
assert!(!model_matches_protocol(&depth, Some("openai_compatible")));
}
}

#[cfg(test)]
#[path = "health_probe_tests.rs"]
mod probe_client_tests;
126 changes: 126 additions & 0 deletions mesh/crates/mesh-llm-host-runtime/src/plugin/health_probe_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
use super::{
health_probe_client_builder, is_loopback_host,
probe_openai_compatible_http_endpoint_for_protocol,
};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpListener;
use url::Url;

const NO_CA_OR_PROXY_CHILD: &str = "MESH_LLM_TEST_PLUGIN_HEALTH_NO_CA_CHILD";

#[test]
fn is_loopback_host_classifies_loopback_and_remote_urls() {
for url in [
"http://127.0.0.1:8000/v1/models",
"http://[::1]:8000/v1/models",
"http://localhost:8000/v1/models",
"http://LOCALHOST.:8000/v1/models",
] {
assert!(
is_loopback_host(&Url::parse(url).expect("parse loopback url")),
"expected loopback: {url}"
);
}
for url in [
"http://192.0.2.1:8000/v1/models",
"http://plugin.internal:8000/v1/models",
"https://api.example.com/v1/models",
] {
assert!(
!is_loopback_host(&Url::parse(url).expect("parse remote url")),
"expected non-loopback: {url}"
);
}
}

// SSL_CERT_FILE and SSL_CERT_DIR point system root loading at an empty file and
// directory on Linux, and HTTP_PROXY points at a closed port; both are set on
// the child test process only.
#[test]
fn probe_http_endpoint_reaches_loopback_without_ca_bundle_or_proxy() {
let temp = tempfile::tempdir().expect("tempdir for empty CA bundle");
let empty_file = temp.path().join("empty.pem");
std::fs::write(&empty_file, "").expect("write empty cert file");
let empty_dir = temp.path().join("certs");
std::fs::create_dir(&empty_dir).expect("create empty cert dir");

let closed = std::net::TcpListener::bind("127.0.0.1:0").expect("bind closed proxy port");
let proxy = format!("http://{}", closed.local_addr().expect("closed proxy addr"));
drop(closed);

let output = std::process::Command::new(std::env::current_exe().expect("current test exe"))
.args([
"--exact",
"plugin::health::probe_client_tests::probe_http_endpoint_without_ca_bundle_child",
"--nocapture",
])
.env(NO_CA_OR_PROXY_CHILD, "1")
.env("SSL_CERT_FILE", &empty_file)
.env("SSL_CERT_DIR", &empty_dir)
.env("HTTP_PROXY", &proxy)
.env("http_proxy", &proxy)
.env("ALL_PROXY", &proxy)
.env("all_proxy", &proxy)
.env_remove("NO_PROXY")
.env_remove("no_proxy")
.output()
.expect("spawn plugin health probe child");

assert!(
output.status.success(),
"plugin health probe child failed:\nstdout:\n{}\nstderr:\n{}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
);
assert!(
String::from_utf8_lossy(&output.stdout).contains("1 passed"),
"child test filter must execute its assertion"
);
}

#[tokio::test]
async fn probe_http_endpoint_without_ca_bundle_child() {
if std::env::var_os(NO_CA_OR_PROXY_CHILD).is_none() {
return;
}

// Non-loopback plain HTTP must also build without system CA certificates.
let remote_http = Url::parse("http://192.0.2.1:8000/v1/models").expect("parse remote http url");
assert!(
health_probe_client_builder(&remote_http).build().is_ok(),
"non-loopback plain-HTTP probe client should build without CA certs"
);

let listener = TcpListener::bind("127.0.0.1:0")
.await
.expect("bind loopback probe server");
let addr = listener.local_addr().expect("probe server addr");
let server = tokio::spawn(async move {
let (mut socket, _) = listener.accept().await.expect("accept health probe");
let mut buf = [0u8; 1024];
let _ = socket.read(&mut buf).await;
let body = r#"{"data":[{"id":"demo-model","availability":"ready","workload_class":"causal_generation"}]}"#;
let response = format!(
"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}",
body.len(),
body
);
socket
.write_all(response.as_bytes())
.await
.expect("write probe response");
let _ = socket.shutdown().await;
});

let record = probe_openai_compatible_http_endpoint_for_protocol(
&format!("http://{addr}"),
Some("openai_compatible"),
)
.await;
assert!(
record.available,
"loopback plain-HTTP plugin health probe should succeed without CA certs or proxy: {record:?}"
);
assert_eq!(record.models, vec!["demo-model".to_string()]);
server.await.expect("probe server join");
}
Loading