[core] Compilers: Pingora runtime config and Caddy JSON, golden files
This commit is contained in:
@@ -12,7 +12,7 @@ use std::path::{Path, PathBuf};
|
||||
|
||||
use ianitor_core::compile::{self, CompileError, CompileOptions};
|
||||
use ianitor_core::edge_config::EdgeConfig;
|
||||
use ianitor_core::{load, Model, SourceFile};
|
||||
use ianitor_core::{Model, SourceFile, load};
|
||||
|
||||
fn fixtures() -> PathBuf {
|
||||
Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/routes")
|
||||
@@ -34,15 +34,33 @@ fn host_files(reverse: bool) -> Vec<(String, String)> {
|
||||
}
|
||||
files
|
||||
.into_iter()
|
||||
.map(|p| (format!("hosts/{}", p.file_name().unwrap().to_string_lossy()), read(&p)))
|
||||
.map(|p| {
|
||||
(
|
||||
format!("hosts/{}", p.file_name().unwrap().to_string_lossy()),
|
||||
read(&p),
|
||||
)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn model(reverse: bool) -> Model {
|
||||
let site = read(&fixtures().join("ianitor.toml"));
|
||||
let files = host_files(reverse);
|
||||
let sources: Vec<SourceFile> = files.iter().map(|(n, c)| SourceFile { name: n, contents: c }).collect();
|
||||
load(&SourceFile { name: "ianitor.toml", contents: &site }, &sources).expect("fixtures are valid")
|
||||
let sources: Vec<SourceFile> = files
|
||||
.iter()
|
||||
.map(|(n, c)| SourceFile {
|
||||
name: n,
|
||||
contents: c,
|
||||
})
|
||||
.collect();
|
||||
load(
|
||||
&SourceFile {
|
||||
name: "ianitor.toml",
|
||||
contents: &site,
|
||||
},
|
||||
&sources,
|
||||
)
|
||||
.expect("fixtures are valid")
|
||||
}
|
||||
|
||||
fn options() -> CompileOptions {
|
||||
@@ -64,7 +82,11 @@ fn edge_compiler_is_deterministic() {
|
||||
for _ in 0..50 {
|
||||
assert_eq!(first, compile::edge(&m, &options()).unwrap());
|
||||
}
|
||||
assert_eq!(first, compile::edge(&model(true), &options()).unwrap(), "file order must not matter");
|
||||
assert_eq!(
|
||||
first,
|
||||
compile::edge(&model(true), &options()).unwrap(),
|
||||
"file order must not matter"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -74,7 +96,11 @@ fn caddy_compiler_is_deterministic() {
|
||||
for _ in 0..50 {
|
||||
assert_eq!(first, compile::caddy(&m, &options()).unwrap());
|
||||
}
|
||||
assert_eq!(first, compile::caddy(&model(true), &options()).unwrap(), "file order must not matter");
|
||||
assert_eq!(
|
||||
first,
|
||||
compile::caddy(&model(true), &options()).unwrap(),
|
||||
"file order must not matter"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -84,7 +110,10 @@ fn determinism_holds_across_threads() {
|
||||
.map(|i| {
|
||||
s.spawn(move || {
|
||||
let m = model(i % 2 == 0);
|
||||
(compile::edge(&m, &options()).unwrap(), compile::caddy(&m, &options()).unwrap())
|
||||
(
|
||||
compile::edge(&m, &options()).unwrap(),
|
||||
compile::caddy(&m, &options()).unwrap(),
|
||||
)
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
@@ -124,7 +153,10 @@ fn caddy_output_is_valid_json_with_servers() {
|
||||
fn outputs_reference_secrets_by_name_only() {
|
||||
let edge = compile::edge(&model(false), &options()).unwrap();
|
||||
let caddy = compile::caddy(&model(false), &options()).unwrap();
|
||||
assert!(edge.contains("\"photos_api_key\""), "edge config names the secret");
|
||||
assert!(
|
||||
edge.contains("\"photos_api_key\""),
|
||||
"edge config names the secret"
|
||||
);
|
||||
assert!(caddy.contains("{file./run/credentials/caddy-fallback.service/photos_api_key}"));
|
||||
// The compilers never see secret values, so nothing secret-shaped can be in the output.
|
||||
for out in [&edge, &caddy] {
|
||||
@@ -136,8 +168,14 @@ fn outputs_reference_secrets_by_name_only() {
|
||||
fn rejects_relative_paths_in_options() {
|
||||
let mut o = options();
|
||||
o.cert_dir = "certs".into();
|
||||
assert!(matches!(compile::edge(&model(false), &o), Err(CompileError::BadOption { .. })));
|
||||
assert!(matches!(compile::caddy(&model(false), &o), Err(CompileError::BadOption { .. })));
|
||||
assert!(matches!(
|
||||
compile::edge(&model(false), &o),
|
||||
Err(CompileError::BadOption { .. })
|
||||
));
|
||||
assert!(matches!(
|
||||
compile::caddy(&model(false), &o),
|
||||
Err(CompileError::BadOption { .. })
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -150,7 +188,9 @@ fn rejects_path_options_that_would_break_out_of_json_or_placeholders() {
|
||||
}
|
||||
|
||||
fn golden(name: &str, actual: &str) {
|
||||
let path = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/golden").join(name);
|
||||
let path = Path::new(env!("CARGO_MANIFEST_DIR"))
|
||||
.join("tests/golden")
|
||||
.join(name);
|
||||
if std::env::var_os("IANITOR_UPDATE_GOLDEN").is_some() {
|
||||
std::fs::write(&path, actual).unwrap();
|
||||
return;
|
||||
@@ -164,10 +204,16 @@ fn golden(name: &str, actual: &str) {
|
||||
|
||||
#[test]
|
||||
fn edge_output_matches_golden_file() {
|
||||
golden("edge.json", &compile::edge(&model(false), &options()).unwrap());
|
||||
golden(
|
||||
"edge.json",
|
||||
&compile::edge(&model(false), &options()).unwrap(),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn caddy_output_matches_golden_file() {
|
||||
golden("caddy.json", &compile::caddy(&model(false), &options()).unwrap());
|
||||
golden(
|
||||
"caddy.json",
|
||||
&compile::caddy(&model(false), &options()).unwrap(),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,558 @@
|
||||
{
|
||||
"admin": {
|
||||
"disabled": true
|
||||
},
|
||||
"apps": {
|
||||
"http": {
|
||||
"http_port": 80,
|
||||
"https_port": 443,
|
||||
"servers": {
|
||||
"ianitor_http": {
|
||||
"automatic_https": {
|
||||
"disable_redirects": true
|
||||
},
|
||||
"listen": [
|
||||
"0.0.0.0:80"
|
||||
],
|
||||
"protocols": [
|
||||
"h1"
|
||||
],
|
||||
"routes": [
|
||||
{
|
||||
"handle": [
|
||||
{
|
||||
"handler": "static_response",
|
||||
"headers": {
|
||||
"Location": [
|
||||
"https://{http.request.host}{http.request.uri}"
|
||||
]
|
||||
},
|
||||
"status_code": 308
|
||||
}
|
||||
],
|
||||
"match": [
|
||||
{
|
||||
"host": [
|
||||
"photos.example.org",
|
||||
"status.example.org",
|
||||
"wiki.home.test"
|
||||
]
|
||||
}
|
||||
],
|
||||
"terminal": true
|
||||
},
|
||||
{
|
||||
"handle": [
|
||||
{
|
||||
"handler": "static_response",
|
||||
"status_code": 404
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
"ianitor_https": {
|
||||
"automatic_https": {
|
||||
"disable_redirects": true,
|
||||
"skip_certificates": [
|
||||
"wiki.home.test"
|
||||
]
|
||||
},
|
||||
"listen": [
|
||||
"0.0.0.0:443"
|
||||
],
|
||||
"logs": {
|
||||
"logger_names": {
|
||||
"photos.example.org": [
|
||||
"ianitor_access"
|
||||
]
|
||||
},
|
||||
"skip_unmapped_hosts": true
|
||||
},
|
||||
"protocols": [
|
||||
"h1",
|
||||
"h2"
|
||||
],
|
||||
"routes": [
|
||||
{
|
||||
"handle": [
|
||||
{
|
||||
"handler": "subroute",
|
||||
"routes": [
|
||||
{
|
||||
"handle": [
|
||||
{
|
||||
"handler": "reverse_proxy",
|
||||
"headers": {
|
||||
"request": {
|
||||
"delete": [
|
||||
"X-Debug"
|
||||
],
|
||||
"set": {
|
||||
"X-Api-Key": [
|
||||
"{file./run/credentials/caddy-fallback.service/photos_api_key}"
|
||||
]
|
||||
}
|
||||
},
|
||||
"response": {
|
||||
"delete": [
|
||||
"X-Powered-By"
|
||||
],
|
||||
"set": {
|
||||
"Strict-Transport-Security": [
|
||||
"max-age=31536000"
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"health_checks": {
|
||||
"active": {
|
||||
"interval": "15s",
|
||||
"timeout": "5s",
|
||||
"uri": "/api/v1/status"
|
||||
}
|
||||
},
|
||||
"load_balancing": {
|
||||
"selection_policy": {
|
||||
"policy": "round_robin"
|
||||
}
|
||||
},
|
||||
"upstreams": [
|
||||
{
|
||||
"dial": "127.0.0.1:2342"
|
||||
},
|
||||
{
|
||||
"dial": "127.0.0.1:2343"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"match": [
|
||||
{
|
||||
"host": [
|
||||
"photos.example.org"
|
||||
]
|
||||
}
|
||||
],
|
||||
"terminal": true
|
||||
},
|
||||
{
|
||||
"handle": [
|
||||
{
|
||||
"handler": "subroute",
|
||||
"routes": [
|
||||
{
|
||||
"handle": [
|
||||
{
|
||||
"handler": "reverse_proxy",
|
||||
"load_balancing": {
|
||||
"selection_policy": {
|
||||
"policy": "round_robin"
|
||||
}
|
||||
},
|
||||
"upstreams": [
|
||||
{
|
||||
"dial": "[::1]:3001"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"match": [
|
||||
{
|
||||
"host": [
|
||||
"status.example.org"
|
||||
]
|
||||
}
|
||||
],
|
||||
"terminal": true
|
||||
},
|
||||
{
|
||||
"handle": [
|
||||
{
|
||||
"handler": "subroute",
|
||||
"routes": [
|
||||
{
|
||||
"handle": [
|
||||
{
|
||||
"handle_response": [
|
||||
{
|
||||
"match": {
|
||||
"status_code": [
|
||||
2
|
||||
]
|
||||
},
|
||||
"routes": [
|
||||
{
|
||||
"handle": [
|
||||
{
|
||||
"handler": "vars"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"handle": [
|
||||
{
|
||||
"handler": "headers",
|
||||
"request": {
|
||||
"delete": [
|
||||
"Remote-User"
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"handle": [
|
||||
{
|
||||
"handler": "headers",
|
||||
"request": {
|
||||
"set": {
|
||||
"Remote-User": [
|
||||
"{http.reverse_proxy.header.Remote-User}"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"match": [
|
||||
{
|
||||
"not": [
|
||||
{
|
||||
"vars": {
|
||||
"{http.reverse_proxy.header.Remote-User}": [
|
||||
""
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"handle": [
|
||||
{
|
||||
"handler": "headers",
|
||||
"request": {
|
||||
"delete": [
|
||||
"Remote-Groups"
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"handle": [
|
||||
{
|
||||
"handler": "headers",
|
||||
"request": {
|
||||
"set": {
|
||||
"Remote-Groups": [
|
||||
"{http.reverse_proxy.header.Remote-Groups}"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"match": [
|
||||
{
|
||||
"not": [
|
||||
{
|
||||
"vars": {
|
||||
"{http.reverse_proxy.header.Remote-Groups}": [
|
||||
""
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"handle": [
|
||||
{
|
||||
"handler": "headers",
|
||||
"request": {
|
||||
"delete": [
|
||||
"Remote-Email"
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"handle": [
|
||||
{
|
||||
"handler": "headers",
|
||||
"request": {
|
||||
"set": {
|
||||
"Remote-Email": [
|
||||
"{http.reverse_proxy.header.Remote-Email}"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"match": [
|
||||
{
|
||||
"not": [
|
||||
{
|
||||
"vars": {
|
||||
"{http.reverse_proxy.header.Remote-Email}": [
|
||||
""
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"handle": [
|
||||
{
|
||||
"handler": "headers",
|
||||
"request": {
|
||||
"delete": [
|
||||
"Remote-Name"
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"handle": [
|
||||
{
|
||||
"handler": "headers",
|
||||
"request": {
|
||||
"set": {
|
||||
"Remote-Name": [
|
||||
"{http.reverse_proxy.header.Remote-Name}"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"match": [
|
||||
{
|
||||
"not": [
|
||||
{
|
||||
"vars": {
|
||||
"{http.reverse_proxy.header.Remote-Name}": [
|
||||
""
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"handler": "reverse_proxy",
|
||||
"headers": {
|
||||
"request": {
|
||||
"set": {
|
||||
"X-Forwarded-Method": [
|
||||
"{http.request.method}"
|
||||
],
|
||||
"X-Forwarded-Uri": [
|
||||
"{http.request.uri}"
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"rewrite": {
|
||||
"method": "GET",
|
||||
"uri": "/api/authz/forward-auth"
|
||||
},
|
||||
"upstreams": [
|
||||
{
|
||||
"dial": "127.0.0.1:9091"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"handle": [
|
||||
{
|
||||
"handler": "static_response",
|
||||
"status_code": 403
|
||||
}
|
||||
],
|
||||
"match": [
|
||||
{
|
||||
"header_regexp": {
|
||||
"Upgrade": {
|
||||
"pattern": "(?i)^websocket$"
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"handle": [
|
||||
{
|
||||
"handler": "reverse_proxy",
|
||||
"headers": {
|
||||
"request": {
|
||||
"set": {
|
||||
"Authorization": [
|
||||
"{file./run/credentials/caddy-fallback.service/wiki_token}"
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"load_balancing": {
|
||||
"selection_policy": {
|
||||
"policy": "round_robin"
|
||||
}
|
||||
},
|
||||
"transport": {
|
||||
"protocol": "http",
|
||||
"tls": {}
|
||||
},
|
||||
"upstreams": [
|
||||
{
|
||||
"dial": "wiki.lan:8443"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"match": [
|
||||
{
|
||||
"host": [
|
||||
"wiki.home.test"
|
||||
]
|
||||
}
|
||||
],
|
||||
"terminal": true
|
||||
},
|
||||
{
|
||||
"handle": [
|
||||
{
|
||||
"handler": "static_response",
|
||||
"status_code": 404
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"tls_connection_policies": [
|
||||
{
|
||||
"certificate_selection": {
|
||||
"any_tag": [
|
||||
"ianitor:wiki.home.test"
|
||||
]
|
||||
},
|
||||
"match": {
|
||||
"sni": [
|
||||
"wiki.home.test"
|
||||
]
|
||||
}
|
||||
},
|
||||
{}
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"tls": {
|
||||
"automation": {
|
||||
"policies": [
|
||||
{
|
||||
"issuers": [
|
||||
{
|
||||
"ca": "https://acme-staging-v02.api.letsencrypt.org/directory",
|
||||
"email": "admin@example.org",
|
||||
"module": "acme"
|
||||
}
|
||||
],
|
||||
"subjects": [
|
||||
"photos.example.org",
|
||||
"status.example.org"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
"certificates": {
|
||||
"load_files": [
|
||||
{
|
||||
"certificate": "/var/lib/ianitor/certs/wiki.home.test.crt",
|
||||
"key": "/var/lib/ianitor/certs/wiki.home.test.key",
|
||||
"tags": [
|
||||
"ianitor:wiki.home.test"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"logging": {
|
||||
"logs": {
|
||||
"default": {
|
||||
"exclude": [
|
||||
"http.log.access.ianitor_access"
|
||||
]
|
||||
},
|
||||
"ianitor_access": {
|
||||
"encoder": {
|
||||
"fields": {
|
||||
"request>client_ip": {
|
||||
"filter": "ip_mask",
|
||||
"ipv4_cidr": 24,
|
||||
"ipv6_cidr": 48
|
||||
},
|
||||
"request>headers": {
|
||||
"filter": "delete"
|
||||
},
|
||||
"request>remote_ip": {
|
||||
"filter": "ip_mask",
|
||||
"ipv4_cidr": 24,
|
||||
"ipv6_cidr": 48
|
||||
},
|
||||
"request>remote_port": {
|
||||
"filter": "delete"
|
||||
},
|
||||
"request>tls": {
|
||||
"filter": "delete"
|
||||
},
|
||||
"request>uri": {
|
||||
"filter": "regexp",
|
||||
"regexp": "\\?.*$",
|
||||
"value": ""
|
||||
},
|
||||
"resp_headers": {
|
||||
"filter": "delete"
|
||||
}
|
||||
},
|
||||
"format": "filter",
|
||||
"wrap": {
|
||||
"format": "json"
|
||||
}
|
||||
},
|
||||
"include": [
|
||||
"http.log.access.ianitor_access"
|
||||
],
|
||||
"writer": {
|
||||
"filename": "/var/log/ianitor/access.log",
|
||||
"output": "file"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"storage": {
|
||||
"module": "file_system",
|
||||
"root": "/var/lib/ianitor/caddy"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
{
|
||||
"version": 1,
|
||||
"http_listen": "0.0.0.0:80",
|
||||
"https_listen": "0.0.0.0:443",
|
||||
"https_port": 443,
|
||||
"cert_dir": "/var/lib/ianitor/certs",
|
||||
"state_dir": "/var/lib/ianitor",
|
||||
"access_log": "/var/log/ianitor/access.log",
|
||||
"acme": {
|
||||
"directory": "https://acme-staging-v02.api.letsencrypt.org/directory",
|
||||
"email": "admin@example.org"
|
||||
},
|
||||
"forward_auth": {
|
||||
"upstream": {
|
||||
"address": "127.0.0.1:9091",
|
||||
"tls": false,
|
||||
"sni": ""
|
||||
},
|
||||
"path_and_query": "/api/authz/forward-auth",
|
||||
"copy_headers": [
|
||||
"Remote-User",
|
||||
"Remote-Groups",
|
||||
"Remote-Email",
|
||||
"Remote-Name"
|
||||
]
|
||||
},
|
||||
"hosts": [
|
||||
{
|
||||
"hostname": "photos.example.org",
|
||||
"tls": {
|
||||
"source": "acme"
|
||||
},
|
||||
"upstreams": [
|
||||
{
|
||||
"address": "127.0.0.1:2342",
|
||||
"tls": false,
|
||||
"sni": ""
|
||||
},
|
||||
{
|
||||
"address": "127.0.0.1:2343",
|
||||
"tls": false,
|
||||
"sni": ""
|
||||
}
|
||||
],
|
||||
"forward_auth": false,
|
||||
"websockets": true,
|
||||
"request_headers": [
|
||||
{
|
||||
"op": "set",
|
||||
"name": "X-Api-Key",
|
||||
"value": {
|
||||
"kind": "secret",
|
||||
"name": "photos_api_key"
|
||||
}
|
||||
},
|
||||
{
|
||||
"op": "remove",
|
||||
"name": "X-Debug"
|
||||
}
|
||||
],
|
||||
"response_headers": [
|
||||
{
|
||||
"op": "set",
|
||||
"name": "Strict-Transport-Security",
|
||||
"value": {
|
||||
"kind": "literal",
|
||||
"value": "max-age=31536000"
|
||||
}
|
||||
},
|
||||
{
|
||||
"op": "remove",
|
||||
"name": "X-Powered-By"
|
||||
}
|
||||
],
|
||||
"health_check": {
|
||||
"path": "/api/v1/status",
|
||||
"interval_secs": 15
|
||||
},
|
||||
"access_log": true
|
||||
},
|
||||
{
|
||||
"hostname": "status.example.org",
|
||||
"tls": {
|
||||
"source": "acme"
|
||||
},
|
||||
"upstreams": [
|
||||
{
|
||||
"address": "[::1]:3001",
|
||||
"tls": false,
|
||||
"sni": ""
|
||||
}
|
||||
],
|
||||
"forward_auth": false,
|
||||
"websockets": true,
|
||||
"request_headers": [],
|
||||
"response_headers": [],
|
||||
"health_check": null,
|
||||
"access_log": false
|
||||
},
|
||||
{
|
||||
"hostname": "wiki.home.test",
|
||||
"tls": {
|
||||
"source": "files",
|
||||
"cert": "/var/lib/ianitor/certs/wiki.home.test.crt",
|
||||
"key": "/var/lib/ianitor/certs/wiki.home.test.key"
|
||||
},
|
||||
"upstreams": [
|
||||
{
|
||||
"address": "wiki.lan:8443",
|
||||
"tls": true,
|
||||
"sni": "wiki.lan"
|
||||
}
|
||||
],
|
||||
"forward_auth": true,
|
||||
"websockets": false,
|
||||
"request_headers": [
|
||||
{
|
||||
"op": "set",
|
||||
"name": "Authorization",
|
||||
"value": {
|
||||
"kind": "secret",
|
||||
"name": "wiki_token"
|
||||
}
|
||||
}
|
||||
],
|
||||
"response_headers": [],
|
||||
"health_check": null,
|
||||
"access_log": false
|
||||
}
|
||||
]
|
||||
}
|
||||
+352
-76
@@ -8,8 +8,8 @@
|
||||
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
|
||||
|
||||
use ianitor_core::{
|
||||
load, AccessLog, AuthMode, HeaderRule, HeaderValue, SourceFile, TlsMode, ValidationError,
|
||||
ValidationErrors,
|
||||
AccessLog, AuthMode, HeaderRule, HeaderValue, SourceFile, TlsMode, ValidationError,
|
||||
ValidationErrors, load,
|
||||
};
|
||||
|
||||
const SITE: &str = r#"
|
||||
@@ -20,11 +20,20 @@ url = "http://127.0.0.1:9091/api/authz/forward-auth"
|
||||
"#;
|
||||
|
||||
fn site() -> SourceFile<'static> {
|
||||
SourceFile { name: "ianitor.toml", contents: SITE }
|
||||
SourceFile {
|
||||
name: "ianitor.toml",
|
||||
contents: SITE,
|
||||
}
|
||||
}
|
||||
|
||||
fn one(contents: &str) -> Result<ianitor_core::Model, ValidationErrors> {
|
||||
load(&site(), &[SourceFile { name: "hosts/app.test.toml", contents }])
|
||||
load(
|
||||
&site(),
|
||||
&[SourceFile {
|
||||
name: "hosts/app.test.toml",
|
||||
contents,
|
||||
}],
|
||||
)
|
||||
}
|
||||
|
||||
fn errors(contents: &str) -> Vec<ValidationError> {
|
||||
@@ -68,10 +77,9 @@ fn minimal_host_gets_documented_defaults() {
|
||||
|
||||
#[test]
|
||||
fn full_host_is_accepted() {
|
||||
let model = one(
|
||||
r#"
|
||||
let model = one(r#"
|
||||
hostname = "app.test"
|
||||
upstreams = ["http://127.0.0.1:8080", "https://backend.lan", "http://[::1]:9000/"]
|
||||
upstreams = ["https://127.0.0.1:8443", "https://backend.lan", "https://[::1]:9000/"]
|
||||
tls = "domi"
|
||||
auth = "forward-auth"
|
||||
websockets = false
|
||||
@@ -95,8 +103,7 @@ name = "X-Debug"
|
||||
action = "set"
|
||||
name = "Strict-Transport-Security"
|
||||
value = "max-age=31536000"
|
||||
"#,
|
||||
)
|
||||
"#)
|
||||
.expect("full host is valid");
|
||||
let h = &model.hosts()[0];
|
||||
assert_eq!(h.tls, TlsMode::Domi);
|
||||
@@ -108,12 +115,27 @@ value = "max-age=31536000"
|
||||
assert_eq!(hc.path, "/healthz");
|
||||
assert_eq!(hc.interval_secs, 10);
|
||||
let ups: Vec<String> = h.upstreams.iter().map(|u| u.to_string()).collect();
|
||||
assert_eq!(ups, ["http://127.0.0.1:8080", "https://backend.lan:443", "http://[::1]:9000"]);
|
||||
assert_eq!(
|
||||
ups,
|
||||
[
|
||||
"https://127.0.0.1:8443",
|
||||
"https://backend.lan:443",
|
||||
"https://[::1]:9000"
|
||||
]
|
||||
);
|
||||
assert_eq!(
|
||||
h.request_headers[0],
|
||||
HeaderRule::Set { name: "X-Api-Key".into(), value: HeaderValue::Secret("api_key".into()) }
|
||||
HeaderRule::Set {
|
||||
name: "X-Api-Key".into(),
|
||||
value: HeaderValue::Secret("api_key".into())
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
h.request_headers[1],
|
||||
HeaderRule::Remove {
|
||||
name: "X-Debug".into()
|
||||
}
|
||||
);
|
||||
assert_eq!(h.request_headers[1], HeaderRule::Remove { name: "X-Debug".into() });
|
||||
assert_eq!(
|
||||
h.response_headers[0],
|
||||
HeaderRule::Set {
|
||||
@@ -125,18 +147,38 @@ value = "max-age=31536000"
|
||||
|
||||
#[test]
|
||||
fn hosts_are_sorted_by_hostname_whatever_the_file_order() {
|
||||
let a = SourceFile { name: "b.toml", contents: "hostname = \"b.test\"\nupstreams = [\"http://127.0.0.1:1\"]\n" };
|
||||
let b = SourceFile { name: "a.toml", contents: "hostname = \"a.test\"\nupstreams = [\"http://127.0.0.1:1\"]\n" };
|
||||
let a = SourceFile {
|
||||
name: "b.toml",
|
||||
contents: "hostname = \"b.test\"\nupstreams = [\"http://127.0.0.1:1\"]\n",
|
||||
};
|
||||
let b = SourceFile {
|
||||
name: "a.toml",
|
||||
contents: "hostname = \"a.test\"\nupstreams = [\"http://127.0.0.1:1\"]\n",
|
||||
};
|
||||
let m1 = load(&site(), &[a, b]).unwrap();
|
||||
let m2 = load(&site(), &[b, a]).unwrap();
|
||||
let names = |m: &ianitor_core::Model| m.hosts().iter().map(|h| h.hostname.to_string()).collect::<Vec<_>>();
|
||||
let names = |m: &ianitor_core::Model| {
|
||||
m.hosts()
|
||||
.iter()
|
||||
.map(|h| h.hostname.to_string())
|
||||
.collect::<Vec<_>>()
|
||||
};
|
||||
assert_eq!(names(&m1), ["a.test", "b.test"]);
|
||||
assert_eq!(m1, m2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_site_file_is_allowed() {
|
||||
let m = load(&SourceFile { name: "ianitor.toml", contents: "" }, &[SourceFile { name: "a.toml", contents: MINIMAL }]);
|
||||
let m = load(
|
||||
&SourceFile {
|
||||
name: "ianitor.toml",
|
||||
contents: "",
|
||||
},
|
||||
&[SourceFile {
|
||||
name: "a.toml",
|
||||
contents: MINIMAL,
|
||||
}],
|
||||
);
|
||||
assert!(m.is_ok(), "{m:?}");
|
||||
}
|
||||
|
||||
@@ -145,20 +187,34 @@ fn empty_site_file_is_allowed() {
|
||||
#[test]
|
||||
fn rejects_unknown_top_level_field() {
|
||||
let e = single_error(&format!("{MINIMAL}colour = \"red\"\n"));
|
||||
assert_eq!(e, ValidationError::UnknownField { file: "hosts/app.test.toml".into(), field: "colour".into() });
|
||||
assert_eq!(
|
||||
e,
|
||||
ValidationError::UnknownField {
|
||||
file: "hosts/app.test.toml".into(),
|
||||
field: "colour".into()
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_misspelled_field() {
|
||||
let e = single_error("hostname = \"app.test\"\nupstream = [\"http://127.0.0.1:1\"]\n");
|
||||
// `upstream` is unknown; it is reported before the missing `upstreams`.
|
||||
assert!(matches!(e, ValidationError::UnknownField { ref field, .. } if field == "upstream"), "{e:?}");
|
||||
assert!(
|
||||
matches!(e, ValidationError::UnknownField { ref field, .. } if field == "upstream"),
|
||||
"{e:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_unknown_field_in_health_check() {
|
||||
let e = single_error(&format!("{MINIMAL}[health_check]\npath = \"/h\"\ninterval_secs = 5\ntimeout = 3\n"));
|
||||
assert!(matches!(e, ValidationError::UnknownField { ref field, .. } if field == "timeout"), "{e:?}");
|
||||
let e = single_error(&format!(
|
||||
"{MINIMAL}[health_check]\npath = \"/h\"\ninterval_secs = 5\ntimeout = 3\n"
|
||||
));
|
||||
assert!(
|
||||
matches!(e, ValidationError::UnknownField { ref field, .. } if field == "timeout"),
|
||||
"{e:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -166,14 +222,23 @@ fn rejects_unknown_field_in_header_rule() {
|
||||
let e = single_error(&format!(
|
||||
"{MINIMAL}[[request_headers]]\naction = \"set\"\nname = \"X-A\"\nvalue = \"1\"\nappend = true\n"
|
||||
));
|
||||
assert!(matches!(e, ValidationError::UnknownField { ref field, .. } if field == "append"), "{e:?}");
|
||||
assert!(
|
||||
matches!(e, ValidationError::UnknownField { ref field, .. } if field == "append"),
|
||||
"{e:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_unknown_field_in_site_file() {
|
||||
let r = load(
|
||||
&SourceFile { name: "ianitor.toml", contents: "secrets = []\nsecret_values = { a = \"b\" }\n" },
|
||||
&[SourceFile { name: "a.toml", contents: MINIMAL }],
|
||||
&SourceFile {
|
||||
name: "ianitor.toml",
|
||||
contents: "secrets = []\nsecret_values = { a = \"b\" }\n",
|
||||
},
|
||||
&[SourceFile {
|
||||
name: "a.toml",
|
||||
contents: MINIMAL,
|
||||
}],
|
||||
);
|
||||
let errs = r.unwrap_err();
|
||||
assert!(
|
||||
@@ -184,7 +249,9 @@ fn rejects_unknown_field_in_site_file() {
|
||||
|
||||
#[test]
|
||||
fn rejects_unknown_enum_value() {
|
||||
let e = single_error("hostname = \"app.test\"\nupstreams = [\"http://127.0.0.1:1\"]\ntls = \"selfsigned\"\n");
|
||||
let e = single_error(
|
||||
"hostname = \"app.test\"\nupstreams = [\"http://127.0.0.1:1\"]\ntls = \"selfsigned\"\n",
|
||||
);
|
||||
assert!(matches!(e, ValidationError::InvalidValue { .. }), "{e:?}");
|
||||
}
|
||||
|
||||
@@ -205,13 +272,25 @@ fn rejects_toml_syntax_error() {
|
||||
#[test]
|
||||
fn rejects_missing_hostname() {
|
||||
let e = single_error("upstreams = [\"http://127.0.0.1:8080\"]\n");
|
||||
assert_eq!(e, ValidationError::MissingField { file: "hosts/app.test.toml".into(), field: "hostname".into() });
|
||||
assert_eq!(
|
||||
e,
|
||||
ValidationError::MissingField {
|
||||
file: "hosts/app.test.toml".into(),
|
||||
field: "hostname".into()
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_missing_upstreams() {
|
||||
let e = single_error("hostname = \"app.test\"\n");
|
||||
assert_eq!(e, ValidationError::MissingField { file: "hosts/app.test.toml".into(), field: "upstreams".into() });
|
||||
assert_eq!(
|
||||
e,
|
||||
ValidationError::MissingField {
|
||||
file: "hosts/app.test.toml".into(),
|
||||
field: "upstreams".into()
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -219,11 +298,26 @@ fn rejects_bad_hostnames() {
|
||||
let long_label = format!("{}.test", "a".repeat(64));
|
||||
let long_name = format!("{}test", "abcdefghi.".repeat(26));
|
||||
for bad in [
|
||||
"", "UPPER.test", "-bad.test", "bad-.test", "a..b", "*.wild.test", "1.2.3.4", "[::1]",
|
||||
"has space.test", "under_score.test", "app.test.", ".app.test", "app.test:443",
|
||||
long_label.as_str(), long_name.as_str(), "ünicode.test",
|
||||
"",
|
||||
"UPPER.test",
|
||||
"-bad.test",
|
||||
"bad-.test",
|
||||
"a..b",
|
||||
"*.wild.test",
|
||||
"1.2.3.4",
|
||||
"[::1]",
|
||||
"has space.test",
|
||||
"under_score.test",
|
||||
"app.test.",
|
||||
".app.test",
|
||||
"app.test:443",
|
||||
long_label.as_str(),
|
||||
long_name.as_str(),
|
||||
"ünicode.test",
|
||||
] {
|
||||
let e = single_error(&format!("hostname = {bad:?}\nupstreams = [\"http://127.0.0.1:1\"]\n"));
|
||||
let e = single_error(&format!(
|
||||
"hostname = {bad:?}\nupstreams = [\"http://127.0.0.1:1\"]\n"
|
||||
));
|
||||
assert!(
|
||||
matches!(e, ValidationError::InvalidHostname { ref hostname, .. } if hostname == bad),
|
||||
"hostname {bad:?} gave {e:?}"
|
||||
@@ -233,8 +327,16 @@ fn rejects_bad_hostnames() {
|
||||
|
||||
#[test]
|
||||
fn accepts_good_hostnames() {
|
||||
for good in ["localhost", "a.b", "app.test", "x-1.example.org", "0day.example.org"] {
|
||||
let r = one(&format!("hostname = {good:?}\nupstreams = [\"http://127.0.0.1:1\"]\n"));
|
||||
for good in [
|
||||
"localhost",
|
||||
"a.b",
|
||||
"app.test",
|
||||
"x-1.example.org",
|
||||
"0day.example.org",
|
||||
] {
|
||||
let r = one(&format!(
|
||||
"hostname = {good:?}\nupstreams = [\"http://127.0.0.1:1\"]\n"
|
||||
));
|
||||
assert!(r.is_ok(), "{good}: {r:?}");
|
||||
}
|
||||
}
|
||||
@@ -244,8 +346,14 @@ fn rejects_duplicate_hostnames_across_files() {
|
||||
let r = load(
|
||||
&site(),
|
||||
&[
|
||||
SourceFile { name: "hosts/one.toml", contents: MINIMAL },
|
||||
SourceFile { name: "hosts/two.toml", contents: MINIMAL },
|
||||
SourceFile {
|
||||
name: "hosts/one.toml",
|
||||
contents: MINIMAL,
|
||||
},
|
||||
SourceFile {
|
||||
name: "hosts/two.toml",
|
||||
contents: MINIMAL,
|
||||
},
|
||||
],
|
||||
);
|
||||
let errs = r.unwrap_err();
|
||||
@@ -264,21 +372,43 @@ fn duplicate_detection_is_independent_of_file_order() {
|
||||
let r = load(
|
||||
&site(),
|
||||
&[
|
||||
SourceFile { name: "hosts/two.toml", contents: MINIMAL },
|
||||
SourceFile { name: "hosts/one.toml", contents: MINIMAL },
|
||||
SourceFile {
|
||||
name: "hosts/two.toml",
|
||||
contents: MINIMAL,
|
||||
},
|
||||
SourceFile {
|
||||
name: "hosts/one.toml",
|
||||
contents: MINIMAL,
|
||||
},
|
||||
],
|
||||
);
|
||||
let errs = r.unwrap_err();
|
||||
assert!(matches!(&errs.errors()[0], ValidationError::DuplicateHostname { first, second, .. } if first == "hosts/one.toml" && second == "hosts/two.toml"));
|
||||
assert!(
|
||||
matches!(&errs.errors()[0], ValidationError::DuplicateHostname { first, second, .. } if first == "hosts/one.toml" && second == "hosts/two.toml")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_duplicate_source_file_names() {
|
||||
let r = load(
|
||||
&site(),
|
||||
&[SourceFile { name: "a.toml", contents: MINIMAL }, SourceFile { name: "a.toml", contents: MINIMAL }],
|
||||
&[
|
||||
SourceFile {
|
||||
name: "a.toml",
|
||||
contents: MINIMAL,
|
||||
},
|
||||
SourceFile {
|
||||
name: "a.toml",
|
||||
contents: MINIMAL,
|
||||
},
|
||||
],
|
||||
);
|
||||
assert!(
|
||||
r.unwrap_err()
|
||||
.errors()
|
||||
.iter()
|
||||
.any(|e| matches!(e, ValidationError::DuplicateFile { .. }))
|
||||
);
|
||||
assert!(r.unwrap_err().errors().iter().any(|e| matches!(e, ValidationError::DuplicateFile { .. })));
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- upstreams
|
||||
@@ -310,6 +440,17 @@ fn rejects_bad_upstream_urls() {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_mixed_upstream_schemes() {
|
||||
// Caddy has one transport per reverse_proxy handler, so a host cannot mix http and https.
|
||||
let e = single_error(
|
||||
"hostname = \"app.test\"
|
||||
upstreams = [\"http://127.0.0.1:1\", \"https://127.0.0.1:2\"]
|
||||
",
|
||||
);
|
||||
assert!(matches!(e, ValidationError::BadUpstreamUrl { .. }), "{e:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_empty_upstream_list() {
|
||||
let e = single_error("hostname = \"app.test\"\nupstreams = []\n");
|
||||
@@ -318,8 +459,13 @@ fn rejects_empty_upstream_list() {
|
||||
|
||||
#[test]
|
||||
fn rejects_duplicate_upstreams() {
|
||||
let e = single_error("hostname = \"app.test\"\nupstreams = [\"http://127.0.0.1:80\", \"http://127.0.0.1\"]\n");
|
||||
assert!(matches!(e, ValidationError::DuplicateUpstream { .. }), "{e:?}");
|
||||
let e = single_error(
|
||||
"hostname = \"app.test\"\nupstreams = [\"http://127.0.0.1:80\", \"http://127.0.0.1\"]\n",
|
||||
);
|
||||
assert!(
|
||||
matches!(e, ValidationError::DuplicateUpstream { .. }),
|
||||
"{e:?}"
|
||||
);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- secrets
|
||||
@@ -329,19 +475,35 @@ fn rejects_undefined_secret_reference() {
|
||||
let e = single_error(&format!(
|
||||
"{MINIMAL}[[request_headers]]\naction = \"set\"\nname = \"X-Api-Key\"\nsecret = \"nope\"\n"
|
||||
));
|
||||
assert_eq!(e, ValidationError::UndefinedSecret { file: "hosts/app.test.toml".into(), secret: "nope".into() });
|
||||
assert_eq!(
|
||||
e,
|
||||
ValidationError::UndefinedSecret {
|
||||
file: "hosts/app.test.toml".into(),
|
||||
secret: "nope".into()
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn undefined_secret_is_reported_when_site_declares_none() {
|
||||
let r = load(
|
||||
&SourceFile { name: "ianitor.toml", contents: "" },
|
||||
&SourceFile {
|
||||
name: "ianitor.toml",
|
||||
contents: "",
|
||||
},
|
||||
&[SourceFile {
|
||||
name: "a.toml",
|
||||
contents: &format!("{MINIMAL}[[request_headers]]\naction = \"set\"\nname = \"X-K\"\nsecret = \"api_key\"\n"),
|
||||
contents: &format!(
|
||||
"{MINIMAL}[[request_headers]]\naction = \"set\"\nname = \"X-K\"\nsecret = \"api_key\"\n"
|
||||
),
|
||||
}],
|
||||
);
|
||||
assert!(r.unwrap_err().errors().iter().any(|e| matches!(e, ValidationError::UndefinedSecret { .. })));
|
||||
assert!(
|
||||
r.unwrap_err()
|
||||
.errors()
|
||||
.iter()
|
||||
.any(|e| matches!(e, ValidationError::UndefinedSecret { .. }))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -349,19 +511,30 @@ fn rejects_secret_in_response_header() {
|
||||
let e = single_error(&format!(
|
||||
"{MINIMAL}[[response_headers]]\naction = \"set\"\nname = \"X-Leak\"\nsecret = \"api_key\"\n"
|
||||
));
|
||||
assert!(matches!(e, ValidationError::SecretInResponseHeader { .. }), "{e:?}");
|
||||
assert!(
|
||||
matches!(e, ValidationError::SecretInResponseHeader { .. }),
|
||||
"{e:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_bad_secret_names_in_site_file() {
|
||||
for bad in ["../etc/passwd", "a/b", "", "UPPER", "with space", ".hidden"] {
|
||||
let r = load(
|
||||
&SourceFile { name: "ianitor.toml", contents: &format!("secrets = [{bad:?}]\n") },
|
||||
&[SourceFile { name: "a.toml", contents: MINIMAL }],
|
||||
&SourceFile {
|
||||
name: "ianitor.toml",
|
||||
contents: &format!("secrets = [{bad:?}]\n"),
|
||||
},
|
||||
&[SourceFile {
|
||||
name: "a.toml",
|
||||
contents: MINIMAL,
|
||||
}],
|
||||
);
|
||||
let errs = r.unwrap_err();
|
||||
assert!(
|
||||
errs.errors().iter().any(|e| matches!(e, ValidationError::InvalidSecretName { name, .. } if name == bad)),
|
||||
errs.errors().iter().any(
|
||||
|e| matches!(e, ValidationError::InvalidSecretName { name, .. } if name == bad)
|
||||
),
|
||||
"{bad:?}: {errs:?}"
|
||||
);
|
||||
}
|
||||
@@ -376,38 +549,92 @@ fn rule(list: &str, body: &str) -> String {
|
||||
#[test]
|
||||
fn rejects_malformed_header_rules() {
|
||||
let cases = [
|
||||
("set without value", rule("request_headers", "action = \"set\"\nname = \"X-A\"")),
|
||||
("remove with value", rule("request_headers", "action = \"remove\"\nname = \"X-A\"\nvalue = \"1\"")),
|
||||
("value and secret", rule("request_headers", "action = \"set\"\nname = \"X-A\"\nvalue = \"1\"\nsecret = \"api_key\"")),
|
||||
(
|
||||
"set without value",
|
||||
rule("request_headers", "action = \"set\"\nname = \"X-A\""),
|
||||
),
|
||||
(
|
||||
"remove with value",
|
||||
rule(
|
||||
"request_headers",
|
||||
"action = \"remove\"\nname = \"X-A\"\nvalue = \"1\"",
|
||||
),
|
||||
),
|
||||
(
|
||||
"value and secret",
|
||||
rule(
|
||||
"request_headers",
|
||||
"action = \"set\"\nname = \"X-A\"\nvalue = \"1\"\nsecret = \"api_key\"",
|
||||
),
|
||||
),
|
||||
];
|
||||
for (what, input) in cases {
|
||||
let e = single_error(&input);
|
||||
assert!(matches!(e, ValidationError::BadHeaderRule { .. }), "{what}: {e:?}");
|
||||
assert!(
|
||||
matches!(e, ValidationError::BadHeaderRule { .. }),
|
||||
"{what}: {e:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_invalid_header_names() {
|
||||
for bad in ["Bad Header", "", "X:Y", "X\nY", "Ä"] {
|
||||
let e = single_error(&rule("request_headers", &format!("action = \"remove\"\nname = {bad:?}")));
|
||||
assert!(matches!(e, ValidationError::InvalidHeaderName { ref name, .. } if name == bad), "{bad:?}: {e:?}");
|
||||
let e = single_error(&rule(
|
||||
"request_headers",
|
||||
&format!("action = \"remove\"\nname = {bad:?}"),
|
||||
));
|
||||
assert!(
|
||||
matches!(e, ValidationError::InvalidHeaderName { ref name, .. } if name == bad),
|
||||
"{bad:?}: {e:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_invalid_header_values() {
|
||||
for bad in ["a\nb", "a\rb", "a\u{0}b", "{http.request.host}", "x}", "\u{7f}", " leading", "ü"] {
|
||||
for bad in [
|
||||
"a\nb",
|
||||
"a\rb",
|
||||
"a\u{0}b",
|
||||
"{http.request.host}",
|
||||
"x}",
|
||||
"\u{7f}",
|
||||
" leading",
|
||||
"ü",
|
||||
] {
|
||||
let value = toml::Value::String(bad.to_owned()).to_string();
|
||||
let e = single_error(&rule("response_headers", &format!("action = \"set\"\nname = \"X-A\"\nvalue = {value}")));
|
||||
assert!(matches!(e, ValidationError::InvalidHeaderValue { .. }), "{bad:?}: {e:?}");
|
||||
let e = single_error(&rule(
|
||||
"response_headers",
|
||||
&format!("action = \"set\"\nname = \"X-A\"\nvalue = {value}"),
|
||||
));
|
||||
assert!(
|
||||
matches!(e, ValidationError::InvalidHeaderValue { .. }),
|
||||
"{bad:?}: {e:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_protected_headers() {
|
||||
for name in ["Host", "content-length", "Transfer-Encoding", "Connection", "Upgrade", "X-Forwarded-For", "x-forwarded-proto", "X-Forwarded-Host"] {
|
||||
let e = single_error(&rule("request_headers", &format!("action = \"remove\"\nname = {name:?}")));
|
||||
assert!(matches!(e, ValidationError::ProtectedHeader { .. }), "{name}: {e:?}");
|
||||
for name in [
|
||||
"Host",
|
||||
"content-length",
|
||||
"Transfer-Encoding",
|
||||
"Connection",
|
||||
"Upgrade",
|
||||
"X-Forwarded-For",
|
||||
"x-forwarded-proto",
|
||||
"X-Forwarded-Host",
|
||||
] {
|
||||
let e = single_error(&rule(
|
||||
"request_headers",
|
||||
&format!("action = \"remove\"\nname = {name:?}"),
|
||||
));
|
||||
assert!(
|
||||
matches!(e, ValidationError::ProtectedHeader { .. }),
|
||||
"{name}: {e:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -417,7 +644,10 @@ fn identity_headers_are_protected_on_forward_auth_hosts() {
|
||||
"{MINIMAL}auth = \"forward-auth\"\n[[request_headers]]\naction = \"set\"\nname = \"remote-user\"\nvalue = \"admin\"\n"
|
||||
);
|
||||
let e = single_error(&input);
|
||||
assert!(matches!(e, ValidationError::ProtectedHeader { .. }), "{e:?}");
|
||||
assert!(
|
||||
matches!(e, ValidationError::ProtectedHeader { .. }),
|
||||
"{e:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -426,7 +656,10 @@ fn rejects_duplicate_header_rules_case_insensitively() {
|
||||
"{MINIMAL}[[request_headers]]\naction = \"set\"\nname = \"X-A\"\nvalue = \"1\"\n[[request_headers]]\naction = \"remove\"\nname = \"x-a\"\n"
|
||||
);
|
||||
let e = single_error(&input);
|
||||
assert!(matches!(e, ValidationError::DuplicateHeaderRule { .. }), "{e:?}");
|
||||
assert!(
|
||||
matches!(e, ValidationError::DuplicateHeaderRule { .. }),
|
||||
"{e:?}"
|
||||
);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- auth and health checks
|
||||
@@ -434,21 +667,46 @@ fn rejects_duplicate_header_rules_case_insensitively() {
|
||||
#[test]
|
||||
fn forward_auth_requires_site_configuration() {
|
||||
let r = load(
|
||||
&SourceFile { name: "ianitor.toml", contents: "" },
|
||||
&[SourceFile { name: "a.toml", contents: &format!("{MINIMAL}auth = \"forward-auth\"\n") }],
|
||||
&SourceFile {
|
||||
name: "ianitor.toml",
|
||||
contents: "",
|
||||
},
|
||||
&[SourceFile {
|
||||
name: "a.toml",
|
||||
contents: &format!("{MINIMAL}auth = \"forward-auth\"\n"),
|
||||
}],
|
||||
);
|
||||
assert!(
|
||||
r.unwrap_err()
|
||||
.errors()
|
||||
.iter()
|
||||
.any(|e| matches!(e, ValidationError::ForwardAuthNotConfigured { .. }))
|
||||
);
|
||||
assert!(r.unwrap_err().errors().iter().any(|e| matches!(e, ValidationError::ForwardAuthNotConfigured { .. })));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_bad_forward_auth_site_config() {
|
||||
for bad in ["ftp://127.0.0.1/x", "http://u:p@127.0.0.1:9091/api", "http://127.0.0.1:9091/api#x", "nope"] {
|
||||
for bad in [
|
||||
"ftp://127.0.0.1/x",
|
||||
"http://u:p@127.0.0.1:9091/api",
|
||||
"http://127.0.0.1:9091/api#x",
|
||||
"nope",
|
||||
] {
|
||||
let r = load(
|
||||
&SourceFile { name: "ianitor.toml", contents: &format!("[forward_auth]\nurl = {bad:?}\n") },
|
||||
&[SourceFile { name: "a.toml", contents: MINIMAL }],
|
||||
&SourceFile {
|
||||
name: "ianitor.toml",
|
||||
contents: &format!("[forward_auth]\nurl = {bad:?}\n"),
|
||||
},
|
||||
&[SourceFile {
|
||||
name: "a.toml",
|
||||
contents: MINIMAL,
|
||||
}],
|
||||
);
|
||||
assert!(
|
||||
r.unwrap_err().errors().iter().any(|e| matches!(e, ValidationError::BadForwardAuth { .. })),
|
||||
r.unwrap_err()
|
||||
.errors()
|
||||
.iter()
|
||||
.any(|e| matches!(e, ValidationError::BadForwardAuth { .. })),
|
||||
"{bad}"
|
||||
);
|
||||
}
|
||||
@@ -464,7 +722,10 @@ fn rejects_bad_health_checks() {
|
||||
"path = \"/{x}\"\ninterval_secs = 5",
|
||||
] {
|
||||
let e = single_error(&format!("{MINIMAL}[health_check]\n{body}\n"));
|
||||
assert!(matches!(e, ValidationError::BadHealthCheck { .. }), "{body}: {e:?}");
|
||||
assert!(
|
||||
matches!(e, ValidationError::BadHealthCheck { .. }),
|
||||
"{body}: {e:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -475,9 +736,18 @@ fn collects_errors_from_every_file() {
|
||||
let r = load(
|
||||
&site(),
|
||||
&[
|
||||
SourceFile { name: "a.toml", contents: "hostname = \"a.test\"\nupstreams = [\"ftp://x\"]\n" },
|
||||
SourceFile { name: "b.toml", contents: "hostname = \"B.test\"\nupstreams = [\"http://127.0.0.1:1\"]\n" },
|
||||
SourceFile { name: "c.toml", contents: MINIMAL },
|
||||
SourceFile {
|
||||
name: "a.toml",
|
||||
contents: "hostname = \"a.test\"\nupstreams = [\"ftp://x\"]\n",
|
||||
},
|
||||
SourceFile {
|
||||
name: "b.toml",
|
||||
contents: "hostname = \"B.test\"\nupstreams = [\"http://127.0.0.1:1\"]\n",
|
||||
},
|
||||
SourceFile {
|
||||
name: "c.toml",
|
||||
contents: MINIMAL,
|
||||
},
|
||||
],
|
||||
);
|
||||
let errs = r.unwrap_err();
|
||||
@@ -495,8 +765,14 @@ fn error_messages_name_the_file_in_plain_english() {
|
||||
#[test]
|
||||
fn never_panics_on_garbage() {
|
||||
let samples = [
|
||||
"\u{0}\u{1}\u{2}", "[[[[", "hostname = 1", "hostname = [\"a\"]", "= =", "[health_check]\npath = 1",
|
||||
"hostname = \"a.test\"\nupstreams = [1, 2]", "hostname = \"a.test\"\nupstreams = [[]]",
|
||||
"\u{0}\u{1}\u{2}",
|
||||
"[[[[",
|
||||
"hostname = 1",
|
||||
"hostname = [\"a\"]",
|
||||
"= =",
|
||||
"[health_check]\npath = 1",
|
||||
"hostname = \"a.test\"\nupstreams = [1, 2]",
|
||||
"hostname = \"a.test\"\nupstreams = [[]]",
|
||||
"hostname = \"a.test\"\nupstreams = [\"http://127.0.0.1:1\"]\n[[request_headers]]\n",
|
||||
"hostname = \"a.test\"\nupstreams = [\"http://127.0.0.1:1\"]\nrequest_headers = 5",
|
||||
];
|
||||
|
||||
Reference in New Issue
Block a user