[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
+105
View File
@@ -0,0 +1,105 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//! Forward-auth compatible with Authelia's ForwardAuth implementation
//! (`GET /api/authz/forward-auth`) and with Caddy's `forward_auth` directive:
//!
//! - The subrequest is a `GET` to the configured path, carrying the client's request headers
//! (minus hop-by-hop and framing headers, never a body) plus `X-Forwarded-Method`,
//! `X-Forwarded-Proto`, `X-Forwarded-Host`, `X-Forwarded-Uri` and `X-Forwarded-For`, which
//! edge always sets itself (client-supplied values are replaced).
//! - A 2xx answer allows the request: each configured identity header is removed from the
//! client request and, when the auth answer has a non-empty value, set from it.
//! - Any other answer is returned to the client as it is (status, headers, body), and the
//! request never reaches the upstream.
//! - If the auth server cannot be reached, the request fails closed with 502.
use std::time::Duration;
use bytes::Bytes;
use http::header::{self, HeaderName, HeaderValue};
use pingora::Result;
use pingora::connectors::http::Connector;
use pingora::http::{RequestHeader, ResponseHeader};
use pingora::proxy::Session;
use crate::fetch::fetch;
use crate::runtime::ForwardAuthRt;
const MAX_AUTH_BODY: usize = 1 << 20;
const AUTH_TIMEOUT: Duration = Duration::from_secs(10);
pub enum Outcome {
Allow(Vec<(HeaderName, Option<HeaderValue>)>),
Deny(Box<ResponseHeader>, Bytes),
}
/// Request headers never copied to a subrequest or a passed-through response.
pub fn hop_by_hop(name: &HeaderName) -> bool {
matches!(
name.as_str(),
"connection"
| "keep-alive"
| "proxy-connection"
| "te"
| "trailer"
| "transfer-encoding"
| "upgrade"
| "content-length"
| "proxy-authenticate"
| "proxy-authorization"
)
}
fn forwarded(name: &HeaderName) -> bool {
name.as_str().starts_with("x-forwarded-")
}
pub async fn check(
connector: &Connector,
fa: &ForwardAuthRt,
session: &Session,
host_header: &str,
client_ip: Option<String>,
) -> Result<Outcome> {
let req = session.req_header();
let mut sub = RequestHeader::build("GET", fa.path_and_query.as_bytes(), None)?;
for (name, value) in req.headers.iter() {
if !hop_by_hop(name) && !forwarded(name) && name != header::HOST {
sub.append_header(name.clone(), value.clone())?;
}
}
let uri = req.uri.path_and_query().map(|p| p.as_str()).unwrap_or("/");
sub.insert_header(header::HOST, host_header)?;
sub.insert_header("X-Forwarded-Method", req.method.as_str())?;
sub.insert_header("X-Forwarded-Proto", "https")?;
sub.insert_header("X-Forwarded-Host", host_header)?;
sub.insert_header("X-Forwarded-Uri", uri)?;
if let Some(ip) = client_ip {
sub.insert_header("X-Forwarded-For", ip)?;
}
let got = fetch(connector, &fa.upstream, sub, MAX_AUTH_BODY, AUTH_TIMEOUT).await?;
let status = got.header.status.as_u16();
if (200..300).contains(&status) {
let copy = fa
.copy_headers
.iter()
.map(|n| {
let v = got.header.headers.get(n).filter(|v| !v.is_empty()).cloned();
(n.clone(), v)
})
.collect();
return Ok(Outcome::Allow(copy));
}
let mut resp = ResponseHeader::build(got.header.status, None)?;
for (name, value) in got.header.headers.iter() {
if !hop_by_hop(name) {
resp.append_header(name.clone(), value.clone())?;
}
}
resp.insert_header(header::CONTENT_LENGTH, got.body.len().to_string())?;
Ok(Outcome::Deny(Box::new(resp), got.body))
}