Compare commits
5 Commits
main
...
eitanya/au
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7843cc72c6 | ||
|
|
35a435acd4 | ||
|
|
acb2d2ba99 | ||
|
|
a63ffae592 | ||
|
|
ed5541b141 |
@ -37,7 +37,7 @@ pub mod proxy;
|
||||
pub mod serdes;
|
||||
pub mod state_manager;
|
||||
pub mod store;
|
||||
mod telemetry;
|
||||
pub mod telemetry;
|
||||
#[cfg(any(test, feature = "internal_benches"))]
|
||||
pub mod test_helpers;
|
||||
pub mod transport;
|
||||
@ -551,15 +551,40 @@ impl ConfigSource {
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ProxyInputs {
|
||||
cfg: Arc<Config>,
|
||||
stores: Stores,
|
||||
pub cfg: Arc<Config>,
|
||||
pub stores: Stores,
|
||||
|
||||
upstream: client::Client,
|
||||
pub upstream: client::Client,
|
||||
|
||||
metrics: Arc<metrics::Metrics>,
|
||||
pub metrics: Arc<metrics::Metrics>,
|
||||
|
||||
mcp_state: mcp::App,
|
||||
ca: Option<Arc<CaClient>>,
|
||||
pub mcp_state: mcp::App,
|
||||
pub ca: Option<Arc<CaClient>>,
|
||||
}
|
||||
|
||||
impl ProxyInputs {
|
||||
/// Create a new `ProxyInputs` for embedding agentgateway as a library.
|
||||
///
|
||||
/// This constructor is intended for use cases where the gateway is embedded
|
||||
/// directly into another application, bypassing [`app::run`] which creates
|
||||
/// its own admin servers, signal handlers, and XDS state management.
|
||||
pub fn new(
|
||||
cfg: Arc<Config>,
|
||||
stores: Stores,
|
||||
upstream: client::Client,
|
||||
metrics: Arc<metrics::Metrics>,
|
||||
mcp_state: mcp::App,
|
||||
ca: Option<Arc<CaClient>>,
|
||||
) -> Self {
|
||||
Self {
|
||||
cfg,
|
||||
stores,
|
||||
upstream,
|
||||
metrics,
|
||||
mcp_state,
|
||||
ca,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, serde::Serialize)]
|
||||
|
||||
@ -489,6 +489,83 @@ impl Gateway {
|
||||
},
|
||||
}
|
||||
},
|
||||
BindProtocol::auto => {
|
||||
// Auto-detect: peek at first byte to distinguish TLS from plaintext HTTP.
|
||||
// No timeout here — existing HTTP header_read_timeout and TLS handshake
|
||||
// timeout handle slow/dead clients downstream.
|
||||
let (ext, metrics, inner) = raw_stream.into_parts();
|
||||
let mut rewind = Socket::new_rewind(inner);
|
||||
let mut buf = [0u8; 1];
|
||||
match tokio::io::AsyncReadExt::read_exact(&mut rewind, &mut buf).await {
|
||||
Ok(_) => {
|
||||
rewind.rewind();
|
||||
let stream = Socket::from_rewind(ext, metrics, rewind);
|
||||
if buf[0] == 0x16 {
|
||||
// TLS ClientHello — dispatch as TLS
|
||||
match Self::maybe_terminate_tls(
|
||||
inputs.clone(),
|
||||
stream,
|
||||
&policies,
|
||||
bind_name.clone(),
|
||||
false,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok((selected_listener, tls_stream)) => match selected_listener.protocol {
|
||||
ListenerProtocol::HTTPS(_) => {
|
||||
let _ = Self::proxy(
|
||||
bind_name,
|
||||
inputs,
|
||||
Some(selected_listener),
|
||||
tls_stream,
|
||||
Arc::new(policies),
|
||||
drain,
|
||||
)
|
||||
.await;
|
||||
},
|
||||
ListenerProtocol::TLS(_) => {
|
||||
Self::proxy_tcp(
|
||||
bind_name,
|
||||
inputs,
|
||||
Some(selected_listener),
|
||||
tls_stream,
|
||||
drain,
|
||||
)
|
||||
.await
|
||||
},
|
||||
_ => {
|
||||
error!(
|
||||
"invalid: TLS listener protocol is neither HTTPS nor TLS: {:?}",
|
||||
selected_listener.protocol
|
||||
)
|
||||
},
|
||||
},
|
||||
Err(e) => {
|
||||
event!(
|
||||
target: "downstream connection",
|
||||
parent: None,
|
||||
tracing::Level::WARN,
|
||||
src.addr = %peer_addr,
|
||||
protocol = ?bind_protocol,
|
||||
error = ?e.to_string(),
|
||||
"failed to terminate TLS (auto-detected)",
|
||||
);
|
||||
},
|
||||
}
|
||||
} else {
|
||||
// Plaintext HTTP
|
||||
let err =
|
||||
Self::proxy(bind_name, inputs, None, stream, Arc::new(policies), drain).await;
|
||||
if let Err(e) = err {
|
||||
warn!(src.addr = %peer_addr, "proxy error: {e}");
|
||||
}
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
warn!(src.addr = %peer_addr, "auto-detect read failed: {e}");
|
||||
},
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@ -692,7 +769,7 @@ impl Gateway {
|
||||
let ch = start.client_hello();
|
||||
let sni = ch.server_name().unwrap_or_default();
|
||||
let best = listeners
|
||||
.best_match(sni)
|
||||
.best_match_tls(sni)
|
||||
.ok_or(anyhow!("no TLS listener match for {sni}"))?;
|
||||
match best.protocol.tls(tls_pol) {
|
||||
Some(Err(e)) => {
|
||||
|
||||
@ -1464,3 +1464,315 @@ fn accept_error_classification() {
|
||||
"again"
|
||||
)));
|
||||
}
|
||||
|
||||
/// BindProtocol::auto should detect plaintext HTTP and proxy it successfully.
|
||||
#[tokio::test]
|
||||
async fn auto_protocol_plaintext_http() {
|
||||
let mock = simple_mock().await;
|
||||
let route = basic_route(*mock.address());
|
||||
let bind = Bind {
|
||||
key: BIND_KEY,
|
||||
address: "127.0.0.1:0".parse().unwrap(),
|
||||
listeners: ListenerSet::from_list([Listener {
|
||||
key: LISTENER_KEY,
|
||||
name: Default::default(),
|
||||
hostname: Default::default(),
|
||||
protocol: ListenerProtocol::HTTP,
|
||||
tcp_routes: Default::default(),
|
||||
routes: RouteSet::from_list(vec![route]),
|
||||
}]),
|
||||
protocol: BindProtocol::auto,
|
||||
tunnel_protocol: Default::default(),
|
||||
};
|
||||
|
||||
let t = setup_proxy_test("{}")
|
||||
.unwrap()
|
||||
.with_backend(*mock.address())
|
||||
.with_bind(bind);
|
||||
let io = t.serve_http(strng::new("bind"));
|
||||
let res = RequestBuilder::new(Method::GET, "http://lo")
|
||||
.send(io)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(res.status(), 200);
|
||||
let body = read_body(res.into_body()).await;
|
||||
assert_eq!(body.method, Method::GET);
|
||||
}
|
||||
|
||||
/// BindProtocol::auto should detect a TLS ClientHello (first byte 0x16) and
|
||||
/// dispatch through TLS termination, just like BindProtocol::tls.
|
||||
#[tokio::test]
|
||||
async fn auto_protocol_tls_detection() {
|
||||
let mock = simple_mock().await;
|
||||
let route = basic_route(*mock.address());
|
||||
let bind = Bind {
|
||||
key: BIND_KEY,
|
||||
address: "127.0.0.1:0".parse().unwrap(),
|
||||
listeners: ListenerSet::from_list([Listener {
|
||||
key: LISTENER_KEY,
|
||||
name: Default::default(),
|
||||
hostname: strng::new("*.example.com"),
|
||||
protocol: ListenerProtocol::HTTPS(
|
||||
types::local::LocalTLSServerConfig {
|
||||
cert: "../../examples/tls/certs/cert.pem".into(),
|
||||
key: "../../examples/tls/certs/key.pem".into(),
|
||||
root: None,
|
||||
cipher_suites: None,
|
||||
min_tls_version: None,
|
||||
max_tls_version: None,
|
||||
}
|
||||
.try_into()
|
||||
.unwrap(),
|
||||
),
|
||||
tcp_routes: Default::default(),
|
||||
routes: RouteSet::from_list(vec![route]),
|
||||
}]),
|
||||
protocol: BindProtocol::auto,
|
||||
tunnel_protocol: Default::default(),
|
||||
};
|
||||
|
||||
let t = setup_proxy_test("{}")
|
||||
.unwrap()
|
||||
.with_backend(*mock.address())
|
||||
.with_bind(bind);
|
||||
let io = t.serve_https(strng::new("bind"), Some("a.example.com"));
|
||||
let res = RequestBuilder::new(Method::GET, "http://a.example.com")
|
||||
.send(io)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(res.status(), 200);
|
||||
}
|
||||
|
||||
/// BindProtocol::auto with TLS should reject connections that don't match the SNI,
|
||||
/// just like BindProtocol::tls does.
|
||||
#[tokio::test]
|
||||
async fn auto_protocol_tls_wrong_sni() {
|
||||
let mock = simple_mock().await;
|
||||
let route = basic_route(*mock.address());
|
||||
let bind = Bind {
|
||||
key: BIND_KEY,
|
||||
address: "127.0.0.1:0".parse().unwrap(),
|
||||
listeners: ListenerSet::from_list([Listener {
|
||||
key: LISTENER_KEY,
|
||||
name: Default::default(),
|
||||
hostname: strng::new("*.example.com"),
|
||||
protocol: ListenerProtocol::HTTPS(
|
||||
types::local::LocalTLSServerConfig {
|
||||
cert: "../../examples/tls/certs/cert.pem".into(),
|
||||
key: "../../examples/tls/certs/key.pem".into(),
|
||||
root: None,
|
||||
cipher_suites: None,
|
||||
min_tls_version: None,
|
||||
max_tls_version: None,
|
||||
}
|
||||
.try_into()
|
||||
.unwrap(),
|
||||
),
|
||||
tcp_routes: Default::default(),
|
||||
routes: RouteSet::from_list(vec![route]),
|
||||
}]),
|
||||
protocol: BindProtocol::auto,
|
||||
tunnel_protocol: Default::default(),
|
||||
};
|
||||
|
||||
let t = setup_proxy_test("{}")
|
||||
.unwrap()
|
||||
.with_backend(*mock.address())
|
||||
.with_bind(bind);
|
||||
let io = t.serve_https(strng::new("bind"), Some("not-the-domain"));
|
||||
let res = RequestBuilder::new(Method::GET, "http://lo").send(io).await;
|
||||
assert_matches!(res, Err(_));
|
||||
}
|
||||
|
||||
/// Plaintext HTTP on a bind with only an HTTPS listener must be rejected.
|
||||
/// This prevents a protocol downgrade where plaintext bypasses TLS.
|
||||
#[tokio::test]
|
||||
async fn auto_protocol_plaintext_rejected_for_https_only() {
|
||||
let mock = simple_mock().await;
|
||||
let route = basic_route(*mock.address());
|
||||
let bind = Bind {
|
||||
key: BIND_KEY,
|
||||
address: "127.0.0.1:0".parse().unwrap(),
|
||||
listeners: ListenerSet::from_list([Listener {
|
||||
key: LISTENER_KEY,
|
||||
name: Default::default(),
|
||||
hostname: strng::new("*.example.com"),
|
||||
protocol: ListenerProtocol::HTTPS(
|
||||
types::local::LocalTLSServerConfig {
|
||||
cert: "../../examples/tls/certs/cert.pem".into(),
|
||||
key: "../../examples/tls/certs/key.pem".into(),
|
||||
root: None,
|
||||
cipher_suites: None,
|
||||
min_tls_version: None,
|
||||
max_tls_version: None,
|
||||
}
|
||||
.try_into()
|
||||
.unwrap(),
|
||||
),
|
||||
tcp_routes: Default::default(),
|
||||
routes: RouteSet::from_list(vec![route]),
|
||||
}]),
|
||||
protocol: BindProtocol::auto,
|
||||
tunnel_protocol: Default::default(),
|
||||
};
|
||||
|
||||
let t = setup_proxy_test("{}")
|
||||
.unwrap()
|
||||
.with_backend(*mock.address())
|
||||
.with_bind(bind);
|
||||
// Send plaintext HTTP — should fail because only HTTPS listeners exist
|
||||
let io = t.serve_http(strng::new("bind"));
|
||||
let res = RequestBuilder::new(Method::GET, "http://a.example.com")
|
||||
.send(io)
|
||||
.await
|
||||
.unwrap();
|
||||
// No HTTP listener matches, so we get a 404 (listener not found)
|
||||
assert_eq!(res.status(), 404);
|
||||
}
|
||||
|
||||
/// TLS to a bind with only an HTTP listener must be rejected.
|
||||
#[tokio::test]
|
||||
async fn auto_protocol_tls_rejected_for_http_only() {
|
||||
let mock = simple_mock().await;
|
||||
let route = basic_route(*mock.address());
|
||||
let bind = Bind {
|
||||
key: BIND_KEY,
|
||||
address: "127.0.0.1:0".parse().unwrap(),
|
||||
listeners: ListenerSet::from_list([Listener {
|
||||
key: LISTENER_KEY,
|
||||
name: Default::default(),
|
||||
hostname: Default::default(),
|
||||
protocol: ListenerProtocol::HTTP,
|
||||
tcp_routes: Default::default(),
|
||||
routes: RouteSet::from_list(vec![route]),
|
||||
}]),
|
||||
protocol: BindProtocol::auto,
|
||||
tunnel_protocol: Default::default(),
|
||||
};
|
||||
|
||||
let t = setup_proxy_test("{}")
|
||||
.unwrap()
|
||||
.with_backend(*mock.address())
|
||||
.with_bind(bind);
|
||||
// Send TLS — should fail because only HTTP listeners exist (no TLS listener match)
|
||||
let io = t.serve_https(strng::new("bind"), Some("example.com"));
|
||||
let res = RequestBuilder::new(Method::GET, "http://example.com")
|
||||
.send(io)
|
||||
.await;
|
||||
assert_matches!(res, Err(_));
|
||||
}
|
||||
|
||||
/// Mixed listeners: a bind with both HTTP and HTTPS listeners should route
|
||||
/// plaintext to the HTTP listener and TLS to the HTTPS listener.
|
||||
/// The HTTP listener uses a specific hostname (not catch-all) so that if TLS
|
||||
/// traffic were accidentally routed to the HTTP path, it would fail to match.
|
||||
#[tokio::test]
|
||||
async fn auto_protocol_mixed_listeners() {
|
||||
let mock = simple_mock().await;
|
||||
let route = basic_route(*mock.address());
|
||||
let route2 = basic_route(*mock.address());
|
||||
let bind = Bind {
|
||||
key: BIND_KEY,
|
||||
address: "127.0.0.1:0".parse().unwrap(),
|
||||
listeners: ListenerSet::from_list([
|
||||
Listener {
|
||||
key: strng::new("http-listener"),
|
||||
name: Default::default(),
|
||||
hostname: strng::new("http.local"),
|
||||
protocol: ListenerProtocol::HTTP,
|
||||
tcp_routes: Default::default(),
|
||||
routes: RouteSet::from_list(vec![route]),
|
||||
},
|
||||
Listener {
|
||||
key: strng::new("https-listener"),
|
||||
name: Default::default(),
|
||||
hostname: strng::new("*.example.com"),
|
||||
protocol: ListenerProtocol::HTTPS(
|
||||
types::local::LocalTLSServerConfig {
|
||||
cert: "../../examples/tls/certs/cert.pem".into(),
|
||||
key: "../../examples/tls/certs/key.pem".into(),
|
||||
root: None,
|
||||
cipher_suites: None,
|
||||
min_tls_version: None,
|
||||
max_tls_version: None,
|
||||
}
|
||||
.try_into()
|
||||
.unwrap(),
|
||||
),
|
||||
tcp_routes: Default::default(),
|
||||
routes: RouteSet::from_list(vec![route2]),
|
||||
},
|
||||
]),
|
||||
protocol: BindProtocol::auto,
|
||||
tunnel_protocol: Default::default(),
|
||||
};
|
||||
|
||||
let t = setup_proxy_test("{}")
|
||||
.unwrap()
|
||||
.with_backend(*mock.address())
|
||||
.with_bind(bind);
|
||||
|
||||
// Plaintext HTTP to http.local should route to the HTTP listener
|
||||
let io = t.serve_http(strng::new("bind"));
|
||||
let res = RequestBuilder::new(Method::GET, "http://http.local")
|
||||
.send(io)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(res.status(), 200);
|
||||
|
||||
// Plaintext HTTP to a.example.com should fail (only HTTPS listener matches that host)
|
||||
let io = t.serve_http(strng::new("bind"));
|
||||
let res = RequestBuilder::new(Method::GET, "http://a.example.com")
|
||||
.send(io)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(res.status(), 404);
|
||||
|
||||
// TLS to a.example.com should route to the HTTPS listener
|
||||
let io = t.serve_https(strng::new("bind"), Some("a.example.com"));
|
||||
let res = RequestBuilder::new(Method::GET, "http://a.example.com")
|
||||
.send(io)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(res.status(), 200);
|
||||
|
||||
// TLS to http.local should fail (only HTTP listener matches that host, no TLS listener)
|
||||
let io = t.serve_https(strng::new("bind"), Some("http.local"));
|
||||
let res = RequestBuilder::new(Method::GET, "http://http.local")
|
||||
.send(io)
|
||||
.await;
|
||||
assert_matches!(res, Err(_));
|
||||
}
|
||||
|
||||
/// Connections that send no data should time out instead of hanging forever.
|
||||
#[tokio::test(start_paused = true)]
|
||||
async fn auto_protocol_peek_timeout() {
|
||||
let mock = simple_mock().await;
|
||||
let route = basic_route(*mock.address());
|
||||
let bind = Bind {
|
||||
key: BIND_KEY,
|
||||
address: "127.0.0.1:0".parse().unwrap(),
|
||||
listeners: ListenerSet::from_list([Listener {
|
||||
key: LISTENER_KEY,
|
||||
name: Default::default(),
|
||||
hostname: Default::default(),
|
||||
protocol: ListenerProtocol::HTTP,
|
||||
tcp_routes: Default::default(),
|
||||
routes: RouteSet::from_list(vec![route]),
|
||||
}]),
|
||||
protocol: BindProtocol::auto,
|
||||
tunnel_protocol: Default::default(),
|
||||
};
|
||||
|
||||
let t = setup_proxy_test("{}")
|
||||
.unwrap()
|
||||
.with_backend(*mock.address())
|
||||
.with_bind(bind);
|
||||
|
||||
// Get raw duplex stream but don't send any data
|
||||
let _client = t.serve(strng::new("bind"));
|
||||
// With start_paused = true, the tokio runtime auto-advances time.
|
||||
// The proxy_bind future should complete within the timeout (5s) rather than hanging.
|
||||
tokio::time::sleep(std::time::Duration::from_secs(10)).await;
|
||||
// If we reach here, the timeout worked (auto-advance means no real wait).
|
||||
}
|
||||
|
||||
@ -532,7 +532,7 @@ impl HTTPProxy {
|
||||
|
||||
// Now check if we actually have a listener - fail after tracing is set up
|
||||
let selected_listener = selected_listener
|
||||
.or_else(|| bind.listeners.best_match(&host))
|
||||
.or_else(|| bind.listeners.best_match_http(&host))
|
||||
.ok_or(ProxyError::ListenerNotFound);
|
||||
let selected_listener = match selected_listener {
|
||||
Ok(l) => {
|
||||
|
||||
@ -478,6 +478,9 @@ pub enum BindProtocol {
|
||||
// Note: TLS can be TLS (passthrough or termination) or HTTPS
|
||||
tls,
|
||||
tcp,
|
||||
/// Auto-detect protocol by peeking at the first byte of each connection.
|
||||
/// If the byte is 0x16 (TLS ClientHello), dispatch as `tls`; otherwise as `http`.
|
||||
auto,
|
||||
}
|
||||
|
||||
#[apply(schema!)]
|
||||
@ -1298,13 +1301,39 @@ impl ListenerSet {
|
||||
}
|
||||
|
||||
pub fn best_match(&self, host: &str) -> Option<Arc<Listener>> {
|
||||
if let Some(best) = self.inner.values().find(|l| l.hostname == host) {
|
||||
self.best_match_filtered(host, |_| true)
|
||||
}
|
||||
|
||||
/// Match only listeners with HTTP protocol (no TLS).
|
||||
pub fn best_match_http(&self, host: &str) -> Option<Arc<Listener>> {
|
||||
self.best_match_filtered(host, |p| matches!(p, ListenerProtocol::HTTP))
|
||||
}
|
||||
|
||||
/// Match only listeners with TLS-capable protocol (HTTPS or TLS).
|
||||
pub fn best_match_tls(&self, host: &str) -> Option<Arc<Listener>> {
|
||||
self.best_match_filtered(host, |p| {
|
||||
matches!(p, ListenerProtocol::HTTPS(_) | ListenerProtocol::TLS(_))
|
||||
})
|
||||
}
|
||||
|
||||
fn best_match_filtered(
|
||||
&self,
|
||||
host: &str,
|
||||
filter: impl Fn(&ListenerProtocol) -> bool,
|
||||
) -> Option<Arc<Listener>> {
|
||||
if let Some(best) = self
|
||||
.inner
|
||||
.values()
|
||||
.filter(|l| filter(&l.protocol))
|
||||
.find(|l| l.hostname == host)
|
||||
{
|
||||
trace!("found best match for {host} (exact)");
|
||||
return Some(best.clone());
|
||||
}
|
||||
if let Some(best) = self
|
||||
.inner
|
||||
.values()
|
||||
.filter(|l| filter(&l.protocol))
|
||||
.sorted_by_key(|l| -(l.hostname.len() as i64))
|
||||
.find(|l| l.hostname.starts_with("*") && host.ends_with(&l.hostname.as_str()[1..]))
|
||||
{
|
||||
@ -1312,7 +1341,12 @@ impl ListenerSet {
|
||||
return Some(best.clone());
|
||||
}
|
||||
trace!("trying to find best match for {host} (empty hostname)");
|
||||
self.inner.values().find(|l| l.hostname.is_empty()).cloned()
|
||||
self
|
||||
.inner
|
||||
.values()
|
||||
.filter(|l| filter(&l.protocol))
|
||||
.find(|l| l.hostname.is_empty())
|
||||
.cloned()
|
||||
}
|
||||
|
||||
pub fn insert(&mut self, v: Listener) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user