[core] Device certificates: device_certs host field; edge config v3 and Caddy client_authentication with a CA bundle and an allow list, 403 without a certificate
This commit is contained in:
@@ -10,7 +10,7 @@
|
||||
|
||||
use serde_json::{Map, Value, json};
|
||||
|
||||
use super::{CompileOptions, join, strip_headers};
|
||||
use super::{CompileOptions, DEVICE_ALLOW_FILE, DEVICE_CA_FILE, join, strip_headers};
|
||||
use crate::{
|
||||
AccessLog, AuthMode, ForwardAuth, HeaderRule, HeaderValue, Host, Model, Scheme, TlsMode,
|
||||
Upstream,
|
||||
@@ -52,13 +52,23 @@ pub(super) fn compile(model: &Model, o: &CompileOptions) -> Value {
|
||||
routes.push(not_found());
|
||||
|
||||
let domi: Vec<&Host> = hosts.iter().filter(|h| h.tls == TlsMode::Domi).collect();
|
||||
let mut policies: Vec<Value> = domi
|
||||
// One policy per host that needs one: a Domi certificate, device certificates, or both.
|
||||
let mut policies: Vec<Value> = hosts
|
||||
.iter()
|
||||
.filter(|h| h.tls == TlsMode::Domi || h.device_certs)
|
||||
.map(|h| {
|
||||
json!({
|
||||
"match": { "sni": [h.hostname.as_str()] },
|
||||
"certificate_selection": { "any_tag": [cert_tag(h)] }
|
||||
})
|
||||
let mut p = Map::new();
|
||||
p.insert("match".into(), json!({ "sni": [h.hostname.as_str()] }));
|
||||
if h.tls == TlsMode::Domi {
|
||||
p.insert(
|
||||
"certificate_selection".into(),
|
||||
json!({ "any_tag": [cert_tag(h)] }),
|
||||
);
|
||||
}
|
||||
if h.device_certs {
|
||||
p.insert("client_authentication".into(), client_auth(o));
|
||||
}
|
||||
Value::Object(p)
|
||||
})
|
||||
.collect();
|
||||
policies.push(json!({}));
|
||||
@@ -203,6 +213,14 @@ fn logging(file: &str) -> Value {
|
||||
|
||||
fn host_route(h: &Host, fa: Option<&ForwardAuth>, o: &CompileOptions) -> Value {
|
||||
let mut steps = Vec::new();
|
||||
if h.device_certs {
|
||||
// No client certificate on this connection: 403, like edge. A certificate that is not
|
||||
// trusted or not on the allow list never gets this far (the handshake fails).
|
||||
steps.push(json!({
|
||||
"match": [{ "vars": { "{http.request.tls.client.fingerprint}": [""] } }],
|
||||
"handle": [{ "handler": "static_response", "status_code": 403 }]
|
||||
}));
|
||||
}
|
||||
if let (AuthMode::ForwardAuth, Some(fa)) = (h.auth, fa) {
|
||||
// Client-sent identity headers never reach the auth server or the upstream.
|
||||
steps.push(json!({ "handle": [{ "handler": "headers", "request": { "delete": strip_headers(fa) } }] }));
|
||||
@@ -339,3 +357,16 @@ fn forward_auth(fa: &ForwardAuth) -> Value {
|
||||
);
|
||||
Value::Object(rp)
|
||||
}
|
||||
|
||||
/// Device certificates: optional in the handshake (a missing one is a 403 in the route, as in
|
||||
/// edge), but one that is presented must chain to the Domi CA bundle and be on the allow list.
|
||||
fn client_auth(o: &CompileOptions) -> Value {
|
||||
json!({
|
||||
"ca": { "provider": "file", "pem_files": [join(&o.cert_dir, DEVICE_CA_FILE)] },
|
||||
"verifiers": [{
|
||||
"verifier": "leaf",
|
||||
"leaf_certs_loaders": [{ "loader": "file", "files": [join(&o.cert_dir, DEVICE_ALLOW_FILE)] }]
|
||||
}],
|
||||
"mode": "verify_if_given"
|
||||
})
|
||||
}
|
||||
|
||||
@@ -4,10 +4,10 @@
|
||||
|
||||
//! Model → [`EdgeConfig`] for the Pingora data plane.
|
||||
|
||||
use super::{CompileOptions, join, strip_headers};
|
||||
use super::{CompileOptions, DEVICE_ALLOW_FILE, DEVICE_CA_FILE, join, strip_headers};
|
||||
use crate::edge_config::{
|
||||
EDGE_CONFIG_VERSION, EdgeAcme, EdgeConfig, EdgeForwardAuth, EdgeHeaderOp, EdgeHealthCheck,
|
||||
EdgeHost, EdgeTls, EdgeUpstream, EdgeValue,
|
||||
EDGE_CONFIG_VERSION, EdgeAcme, EdgeConfig, EdgeDeviceCerts, EdgeForwardAuth, EdgeHeaderOp,
|
||||
EdgeHealthCheck, EdgeHost, EdgeTls, EdgeUpstream, EdgeValue,
|
||||
};
|
||||
use crate::{AccessLog, AuthMode, HeaderRule, HeaderValue, Model, Scheme, TlsMode, Upstream};
|
||||
|
||||
@@ -26,6 +26,14 @@ pub(super) fn compile(model: &Model, o: &CompileOptions) -> EdgeConfig {
|
||||
directory: site.acme.directory.clone(),
|
||||
email: site.acme.email.clone(),
|
||||
}),
|
||||
device_certs: model
|
||||
.hosts()
|
||||
.iter()
|
||||
.any(|h| h.device_certs)
|
||||
.then(|| EdgeDeviceCerts {
|
||||
ca: join(&o.cert_dir, DEVICE_CA_FILE),
|
||||
allow: join(&o.cert_dir, DEVICE_ALLOW_FILE),
|
||||
}),
|
||||
forward_auth: site.forward_auth.as_ref().map(|fa| EdgeForwardAuth {
|
||||
upstream: upstream(&fa.upstream),
|
||||
path_and_query: fa.path_and_query.clone(),
|
||||
@@ -55,6 +63,7 @@ pub(super) fn compile(model: &Model, o: &CompileOptions) -> EdgeConfig {
|
||||
}),
|
||||
access_log: h.access_log == AccessLog::Truncated,
|
||||
share_links: h.share_links,
|
||||
device_certs: h.device_certs,
|
||||
})
|
||||
.collect(),
|
||||
}
|
||||
|
||||
@@ -17,6 +17,11 @@
|
||||
mod caddy;
|
||||
mod edge;
|
||||
|
||||
/// In the certificate directory: the CA bundle for device certificates (written by control).
|
||||
pub const DEVICE_CA_FILE: &str = "domi-devices-ca.pem";
|
||||
/// In the certificate directory: every allowed (issued, not revoked) device certificate.
|
||||
pub const DEVICE_ALLOW_FILE: &str = "domi-devices.pem";
|
||||
|
||||
use std::net::SocketAddr;
|
||||
|
||||
use crate::Model;
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// Bumped on any incompatible change; edge refuses other versions.
|
||||
pub const EDGE_CONFIG_VERSION: u32 = 2;
|
||||
pub const EDGE_CONFIG_VERSION: u32 = 3;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
@@ -29,6 +29,8 @@ pub struct EdgeConfig {
|
||||
pub access_log: String,
|
||||
pub acme: Option<EdgeAcme>,
|
||||
pub forward_auth: Option<EdgeForwardAuth>,
|
||||
/// Device certificates (mTLS); present when any host has `device_certs`.
|
||||
pub device_certs: Option<EdgeDeviceCerts>,
|
||||
/// Sorted by hostname.
|
||||
pub hosts: Vec<EdgeHost>,
|
||||
}
|
||||
@@ -106,4 +108,17 @@ pub struct EdgeHost {
|
||||
/// Share-link tokens (see `ianitor_core::share`) let visitors in on this host without
|
||||
/// forward-auth. Checked against the key credential and the revocation file.
|
||||
pub share_links: bool,
|
||||
/// Only visitors with an allowed Domi device certificate get in (403 otherwise).
|
||||
pub device_certs: bool,
|
||||
}
|
||||
|
||||
/// Where edge finds what it needs to check device certificates. Both files are written by
|
||||
/// control: the CA bundle (the Domi intermediates that issue device certificates) and the
|
||||
/// allow list (every device certificate that is issued and not revoked, PEM). A certificate
|
||||
/// that chains to the bundle but is not on the list is refused in the handshake.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct EdgeDeviceCerts {
|
||||
pub ca: String,
|
||||
pub allow: String,
|
||||
}
|
||||
|
||||
@@ -171,6 +171,8 @@ pub struct Host {
|
||||
pub health_check: Option<HealthCheck>,
|
||||
pub share_links: bool,
|
||||
pub access_log: AccessLog,
|
||||
/// Visitors must present a Domi device certificate (mTLS) that is on the allow list.
|
||||
pub device_certs: bool,
|
||||
}
|
||||
|
||||
/// The forward-auth endpoint (Authelia's `/api/authz/forward-auth`).
|
||||
|
||||
@@ -121,6 +121,8 @@ struct RawHost {
|
||||
#[serde(default)]
|
||||
share_links: bool,
|
||||
#[serde(default)]
|
||||
device_certs: bool,
|
||||
#[serde(default)]
|
||||
access_log: RawAccessLog,
|
||||
health_check: Option<RawHealthCheck>,
|
||||
#[serde(default)]
|
||||
@@ -517,6 +519,7 @@ fn check_host(file: &str, raw: RawHost, site: Option<&Site>, errors: &mut Vec<E>
|
||||
websockets: raw.websockets,
|
||||
health_check,
|
||||
share_links: raw.share_links,
|
||||
device_certs: raw.device_certs,
|
||||
access_log: match raw.access_log {
|
||||
RawAccessLog::Off => AccessLog::Off,
|
||||
RawAccessLog::Truncated => AccessLog::Truncated,
|
||||
|
||||
Reference in New Issue
Block a user