[edge] Proxy: Pingora data plane with SNI certs, forward-auth, header rules, health checks, ACME

This commit is contained in:
2026-11-07 14:57:36 +01:00
parent 4fe041082d
commit 17e1df8320
24 changed files with 2316 additions and 123 deletions
+111 -26
View File
@@ -11,10 +11,10 @@
mod common;
use common::{host_file, start, EdgeSpec};
use common::{EdgeSpec, host_file, start};
use ianitor_testkit::authelia::MockAuthelia;
use ianitor_testkit::client::{self, Req, WsOutcome};
use ianitor_testkit::upstream::{closed_port, Echo, WsEcho};
use ianitor_testkit::upstream::{Echo, WsEcho, closed_port};
fn site(auth_url: &str) -> String {
format!("[forward_auth]\nurl = \"{auth_url}\"\n")
@@ -29,8 +29,17 @@ async fn denial_never_reaches_the_upstream() {
let edge = start(EdgeSpec {
site: &site,
hosts: vec![
host_file("auth.test", &format!("upstreams = [\"{}\"]\nauth = \"forward-auth\"\n", echo.url())),
host_file("wsauth.test", &format!("upstreams = [\"{}\"]\nauth = \"forward-auth\"\n", ws.url())),
host_file(
"auth.test",
&format!(
"upstreams = [\"{}\"]\nauth = \"forward-auth\"\n",
echo.url()
),
),
host_file(
"wsauth.test",
&format!("upstreams = [\"{}\"]\nauth = \"forward-auth\"\n", ws.url()),
),
],
..Default::default()
});
@@ -48,7 +57,10 @@ async fn denial_never_reaches_the_upstream() {
r.header("location"),
Some("https://auth.test/?rd=https%3A%2F%2Fauth.test%2Fprivate%3Fx%3D1")
);
assert_eq!(r.header("set-cookie"), Some("authelia_flow=1; Path=/; HttpOnly"));
assert_eq!(
r.header("set-cookie"),
Some("authelia_flow=1; Path=/; HttpOnly")
);
assert!(r.text().contains("302 Found"));
let r = go(Req::new("HEAD", "auth.test", "/")).await;
@@ -60,7 +72,10 @@ async fn denial_never_reaches_the_upstream() {
let r = go(Req::get("auth.test", "/").header("authorization", "Bearer wrong")).await;
assert_eq!(r.status, 401);
assert_eq!(r.header("www-authenticate"), Some("Basic realm=\"Authorization Required\""));
assert_eq!(
r.header("www-authenticate"),
Some("Basic realm=\"Authorization Required\"")
);
let r = go(Req::get("auth.test", "/").header("cookie", "authelia_session=forbidden")).await;
assert_eq!(r.status, 403);
@@ -74,16 +89,38 @@ async fn denial_never_reaches_the_upstream() {
.header("x-forwarded-for", "10.0.0.1"))
.await;
assert_eq!(r.status, 302);
assert!(r.header("location").unwrap().contains("auth.test%2Fadmin"), "edge sends its own X-Forwarded-*");
assert!(
r.header("location").unwrap().contains("auth.test%2Fadmin"),
"edge sends its own X-Forwarded-*"
);
// A websocket upgrade without a session is denied before any upgrade happens.
match client::websocket(edge.https, "wsauth.test", roots.clone(), "wsauth.test", "/socket", &[], "hi").await.unwrap() {
match client::websocket(
edge.https,
"wsauth.test",
roots.clone(),
"wsauth.test",
"/socket",
&[],
"hi",
)
.await
.unwrap()
{
WsOutcome::Refused(r) => assert_eq!(r.status, 302),
WsOutcome::Upgraded { .. } => panic!("unauthenticated websocket was upgraded"),
}
assert!(echo.proxied().is_empty(), "a denied request reached the upstream: {:?}", echo.proxied());
assert_eq!(ws.connections(), 0, "a denied websocket reached the upstream");
assert!(
echo.proxied().is_empty(),
"a denied request reached the upstream: {:?}",
echo.proxied()
);
assert_eq!(
ws.connections(),
0,
"a denied websocket reached the upstream"
);
// What Authelia was told, per its proxy-authorization guide.
let asked = authelia.requests();
@@ -95,10 +132,22 @@ async fn denial_never_reaches_the_upstream() {
assert_eq!(post.method, "GET", "the auth subrequest is a GET");
assert_eq!(post.path, "/api/authz/forward-auth");
assert_eq!(post.header("x-forwarded-proto"), Some(b"https".as_slice()));
assert_eq!(post.header("x-forwarded-host"), Some(b"auth.test".as_slice()));
assert_eq!(post.header("x-forwarded-uri"), Some(b"/submit?x=1".as_slice()));
assert_eq!(post.header("x-forwarded-for"), Some(b"127.0.0.1".as_slice()));
assert!(post.body.is_empty(), "the request body is not sent to the auth server");
assert_eq!(
post.header("x-forwarded-host"),
Some(b"auth.test".as_slice())
);
assert_eq!(
post.header("x-forwarded-uri"),
Some(b"/submit?x=1".as_slice())
);
assert_eq!(
post.header("x-forwarded-for"),
Some(b"127.0.0.1".as_slice())
);
assert!(
post.body.is_empty(),
"the request body is not sent to the auth server"
);
}
#[tokio::test(flavor = "multi_thread")]
@@ -108,7 +157,13 @@ async fn allowed_request_carries_identity_from_the_auth_server_only() {
let site = site(&authelia.url());
let edge = start(EdgeSpec {
site: &site,
hosts: vec![host_file("auth.test", &format!("upstreams = [\"{}\"]\nauth = \"forward-auth\"\n", echo.url()))],
hosts: vec![host_file(
"auth.test",
&format!(
"upstreams = [\"{}\"]\nauth = \"forward-auth\"\n",
echo.url()
),
)],
..Default::default()
});
let roots = edge.roots();
@@ -131,10 +186,19 @@ async fn allowed_request_carries_identity_from_the_auth_server_only() {
let up = &seen[0];
assert_eq!(up.header("remote-user"), Some(b"alice".as_slice()));
assert_eq!(up.header("remote-groups"), Some(b"admins,dev".as_slice()));
assert_eq!(up.header("remote-email"), Some(b"alice@example.org".as_slice()));
assert_eq!(
up.header("remote-email"),
Some(b"alice@example.org".as_slice())
);
assert_eq!(up.header("remote-name"), Some(b"Alice Example".as_slice()));
assert_eq!(up.body, b"form=data", "the body still reaches the upstream");
assert_eq!(up.headers.iter().filter(|(n, _)| n == "remote-user").count(), 1);
assert_eq!(
up.headers
.iter()
.filter(|(n, _)| n == "remote-user")
.count(),
1
);
// Allowed without identity: the client's own Remote-* headers are removed, not passed on.
echo.clear();
@@ -142,31 +206,52 @@ async fn allowed_request_carries_identity_from_the_auth_server_only() {
edge.https,
"auth.test",
roots.clone(),
&Req::get("auth.test", "/").header("cookie", "authelia_session=nameless").header("remote-user", "mallory"),
&Req::get("auth.test", "/")
.header("cookie", "authelia_session=nameless")
.header("remote-user", "mallory"),
)
.await
.unwrap();
assert_eq!(r.status, 200);
assert_eq!(echo.proxied()[0].header("remote-user"), None);
let r = client::https(edge.https, "auth.test", roots, &Req::get("auth.test", "/").header("authorization", "Bearer good"))
.await
.unwrap();
let r = client::https(
edge.https,
"auth.test",
roots,
&Req::get("auth.test", "/").header("authorization", "Bearer good"),
)
.await
.unwrap();
assert_eq!(r.status, 200);
}
#[tokio::test(flavor = "multi_thread")]
async fn unreachable_auth_server_fails_closed() {
let echo = Echo::start("app").await;
let site = site(&format!("http://127.0.0.1:{}/api/authz/forward-auth", closed_port()));
let site = site(&format!(
"http://127.0.0.1:{}/api/authz/forward-auth",
closed_port()
));
let edge = start(EdgeSpec {
site: &site,
hosts: vec![host_file("auth.test", &format!("upstreams = [\"{}\"]\nauth = \"forward-auth\"\n", echo.url()))],
hosts: vec![host_file(
"auth.test",
&format!(
"upstreams = [\"{}\"]\nauth = \"forward-auth\"\n",
echo.url()
),
)],
..Default::default()
});
let r = client::https(edge.https, "auth.test", edge.roots(), &Req::get("auth.test", "/").header("cookie", "authelia_session=good"))
.await
.unwrap();
let r = client::https(
edge.https,
"auth.test",
edge.roots(),
&Req::get("auth.test", "/").header("cookie", "authelia_session=good"),
)
.await
.unwrap();
assert_eq!(r.status, 502);
assert!(echo.proxied().is_empty());
}