[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:
2026-11-07 22:46:07 +01:00
parent dc23bfe842
commit 18da3f5d5a
11 changed files with 201 additions and 14 deletions
+65
View File
@@ -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
+46
View File
@@ -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": [
+11 -4
View File
@@ -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
}
]
}
+3
View File
@@ -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");