[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,
|
||||
|
||||
@@ -277,3 +277,68 @@ fn websocket_refusal_in_caddy_matches_any_upgrade_token() {
|
||||
"token-aware Upgrade matcher"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn device_cert_hosts_check_client_certificates_in_both_engines() {
|
||||
let m = model(false);
|
||||
let edge: EdgeConfig = serde_json::from_str(&compile::edge(&m, &options()).unwrap()).unwrap();
|
||||
let dc = edge.device_certs.expect("a host needs device certificates");
|
||||
assert_eq!(dc.ca, "/var/lib/ianitor/certs/domi-devices-ca.pem");
|
||||
assert_eq!(dc.allow, "/var/lib/ianitor/certs/domi-devices.pem");
|
||||
let on: Vec<&str> = edge
|
||||
.hosts
|
||||
.iter()
|
||||
.filter(|h| h.device_certs)
|
||||
.map(|h| h.hostname.as_str())
|
||||
.collect();
|
||||
assert_eq!(on, ["status.example.org"]);
|
||||
|
||||
let v: serde_json::Value =
|
||||
serde_json::from_str(&compile::caddy(&m, &options()).unwrap()).unwrap();
|
||||
let https = &v["apps"]["http"]["servers"]["ianitor_https"];
|
||||
let policies = https["tls_connection_policies"].as_array().unwrap();
|
||||
let with_auth: Vec<&serde_json::Value> = policies
|
||||
.iter()
|
||||
.filter(|p| p.get("client_authentication").is_some())
|
||||
.collect();
|
||||
assert_eq!(
|
||||
with_auth.len(),
|
||||
1,
|
||||
"only the device host asks for a certificate"
|
||||
);
|
||||
assert_eq!(with_auth[0]["match"]["sni"][0], "status.example.org");
|
||||
assert_eq!(
|
||||
with_auth[0]["client_authentication"]["mode"],
|
||||
"verify_if_given"
|
||||
);
|
||||
let routes = https["routes"].as_array().unwrap();
|
||||
let status = routes
|
||||
.iter()
|
||||
.find(|r| r["match"][0]["host"][0] == "status.example.org")
|
||||
.unwrap();
|
||||
let first = &status["handle"][0]["routes"][0];
|
||||
assert_eq!(
|
||||
first["handle"][0]["status_code"], 403,
|
||||
"no certificate: 403 first"
|
||||
);
|
||||
|
||||
// Without a device host, neither engine asks for client certificates.
|
||||
let plain = ianitor_core::load(
|
||||
&SourceFile {
|
||||
name: "ianitor.toml",
|
||||
contents: "",
|
||||
},
|
||||
&[SourceFile {
|
||||
name: "hosts/a.test.toml",
|
||||
contents: "hostname = \"a.test\"\nupstreams = [\"http://127.0.0.1:1\"]\n",
|
||||
}],
|
||||
)
|
||||
.unwrap();
|
||||
let e: EdgeConfig = serde_json::from_str(&compile::edge(&plain, &options()).unwrap()).unwrap();
|
||||
assert!(e.device_certs.is_none());
|
||||
assert!(
|
||||
!compile::caddy(&plain, &options())
|
||||
.unwrap()
|
||||
.contains("client_authentication")
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,2 +1,3 @@
|
||||
hostname = "status.example.org"
|
||||
upstreams = ["http://[::1]:3001"]
|
||||
device_certs = true
|
||||
|
||||
@@ -145,6 +145,23 @@
|
||||
{
|
||||
"handler": "subroute",
|
||||
"routes": [
|
||||
{
|
||||
"handle": [
|
||||
{
|
||||
"handler": "static_response",
|
||||
"status_code": 403
|
||||
}
|
||||
],
|
||||
"match": [
|
||||
{
|
||||
"vars": {
|
||||
"{http.request.tls.client.fingerprint}": [
|
||||
""
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"handle": [
|
||||
{
|
||||
@@ -465,6 +482,35 @@
|
||||
}
|
||||
],
|
||||
"tls_connection_policies": [
|
||||
{
|
||||
"client_authentication": {
|
||||
"ca": {
|
||||
"pem_files": [
|
||||
"/var/lib/ianitor/certs/domi-devices-ca.pem"
|
||||
],
|
||||
"provider": "file"
|
||||
},
|
||||
"mode": "verify_if_given",
|
||||
"verifiers": [
|
||||
{
|
||||
"leaf_certs_loaders": [
|
||||
{
|
||||
"files": [
|
||||
"/var/lib/ianitor/certs/domi-devices.pem"
|
||||
],
|
||||
"loader": "file"
|
||||
}
|
||||
],
|
||||
"verifier": "leaf"
|
||||
}
|
||||
]
|
||||
},
|
||||
"match": {
|
||||
"sni": [
|
||||
"status.example.org"
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"certificate_selection": {
|
||||
"any_tag": [
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"version": 2,
|
||||
"version": 3,
|
||||
"http_listen": "0.0.0.0:80",
|
||||
"https_listen": "0.0.0.0:443",
|
||||
"https_port": 443,
|
||||
@@ -30,6 +30,10 @@
|
||||
"Remote-Name"
|
||||
]
|
||||
},
|
||||
"device_certs": {
|
||||
"ca": "/var/lib/ianitor/certs/domi-devices-ca.pem",
|
||||
"allow": "/var/lib/ianitor/certs/domi-devices.pem"
|
||||
},
|
||||
"hosts": [
|
||||
{
|
||||
"hostname": "photos.example.org",
|
||||
@@ -83,7 +87,8 @@
|
||||
"interval_secs": 15
|
||||
},
|
||||
"access_log": true,
|
||||
"share_links": false
|
||||
"share_links": false,
|
||||
"device_certs": false
|
||||
},
|
||||
{
|
||||
"hostname": "status.example.org",
|
||||
@@ -103,7 +108,8 @@
|
||||
"response_headers": [],
|
||||
"health_check": null,
|
||||
"access_log": false,
|
||||
"share_links": false
|
||||
"share_links": false,
|
||||
"device_certs": true
|
||||
},
|
||||
{
|
||||
"hostname": "wiki.home.test",
|
||||
@@ -134,7 +140,8 @@
|
||||
"response_headers": [],
|
||||
"health_check": null,
|
||||
"access_log": false,
|
||||
"share_links": false
|
||||
"share_links": false,
|
||||
"device_certs": false
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
@@ -68,6 +68,7 @@ fn minimal_host_gets_documented_defaults() {
|
||||
assert_eq!(h.auth, AuthMode::None);
|
||||
assert!(h.websockets, "websockets default on, like Caddy");
|
||||
assert!(!h.share_links);
|
||||
assert!(!h.device_certs, "device certificates are opt-in");
|
||||
assert_eq!(h.access_log, AccessLog::Off);
|
||||
assert!(h.health_check.is_none());
|
||||
assert!(h.request_headers.is_empty() && h.response_headers.is_empty());
|
||||
@@ -85,6 +86,7 @@ auth = "forward-auth"
|
||||
websockets = false
|
||||
share_links = true
|
||||
access_log = "truncated"
|
||||
device_certs = true
|
||||
|
||||
[health_check]
|
||||
path = "/healthz"
|
||||
@@ -110,6 +112,7 @@ value = "max-age=31536000"
|
||||
assert_eq!(h.auth, AuthMode::ForwardAuth);
|
||||
assert!(!h.websockets);
|
||||
assert!(h.share_links);
|
||||
assert!(h.device_certs);
|
||||
assert_eq!(h.access_log, AccessLog::Truncated);
|
||||
let hc = h.health_check.as_ref().unwrap();
|
||||
assert_eq!(hc.path, "/healthz");
|
||||
|
||||
Reference in New Issue
Block a user