[core] Unix upstreams, tests first: unix:/path reaches both engines (edge address, Caddy dial), the path is strict, forward-auth never goes to a socket (red: not parsed yet)
This commit is contained in:
@@ -37,6 +37,12 @@ fn template(code: &str) -> &'static str {
|
|||||||
}
|
}
|
||||||
"host.upstream.not_url" => "Write it as a full address, like http://10.0.0.23:3000.",
|
"host.upstream.not_url" => "Write it as a full address, like http://10.0.0.23:3000.",
|
||||||
"host.upstream.scheme" => "Start the address with http:// or https://.",
|
"host.upstream.scheme" => "Start the address with http:// or https://.",
|
||||||
|
"host.upstream.unix_path" => {
|
||||||
|
"Write a socket as unix:/absolute/path.sock, with letters, digits, dots, dashes and underscores only."
|
||||||
|
}
|
||||||
|
"host.upstream.unix_socket" => {
|
||||||
|
"Only the web UI's admin host can use a socket, and only control's own ([ui] socket in control.toml)."
|
||||||
|
}
|
||||||
"host.upstream.no_host" => "Add the server's name or IP address after http://.",
|
"host.upstream.no_host" => "Add the server's name or IP address after http://.",
|
||||||
"host.upstream.host_name" => {
|
"host.upstream.host_name" => {
|
||||||
"The server name is not usable. Use letters a–z, digits, dots and dashes, or an IP address."
|
"The server name is not usable. Use letters a–z, digits, dots and dashes, or an IP address."
|
||||||
|
|||||||
@@ -64,11 +64,13 @@ impl Scheme {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Where the upstream host is: a DNS name or an IP literal (IPv6 without brackets).
|
/// Where the upstream host is: a DNS name, an IP literal (IPv6 without brackets), or a Unix
|
||||||
|
/// socket path (`unix:/abs/path`, plain HTTP; which socket a host may use is control's call).
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||||
pub enum UpstreamHost {
|
pub enum UpstreamHost {
|
||||||
Name(String),
|
Name(String),
|
||||||
Ip(std::net::IpAddr),
|
Ip(std::net::IpAddr),
|
||||||
|
Unix(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A validated upstream: scheme, host and explicit port. No path, query, fragment or userinfo.
|
/// A validated upstream: scheme, host and explicit port. No path, query, fragment or userinfo.
|
||||||
@@ -81,26 +83,44 @@ pub struct Upstream {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Upstream {
|
impl Upstream {
|
||||||
/// `host:port` as used for dialing, with IPv6 in brackets.
|
/// `host:port` as used for dialing, with IPv6 in brackets; `unix:<path>` for a socket.
|
||||||
pub fn authority(&self) -> String {
|
pub fn authority(&self) -> String {
|
||||||
match &self.host {
|
match &self.host {
|
||||||
UpstreamHost::Name(n) => format!("{n}:{}", self.port),
|
UpstreamHost::Name(n) => format!("{n}:{}", self.port),
|
||||||
UpstreamHost::Ip(std::net::IpAddr::V6(ip)) => format!("[{ip}]:{}", self.port),
|
UpstreamHost::Ip(std::net::IpAddr::V6(ip)) => format!("[{ip}]:{}", self.port),
|
||||||
UpstreamHost::Ip(ip) => format!("{ip}:{}", self.port),
|
UpstreamHost::Ip(ip) => format!("{ip}:{}", self.port),
|
||||||
|
UpstreamHost::Unix(p) => format!("unix:{p}"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The host part for TLS SNI and certificate verification.
|
/// The host part for TLS SNI and certificate verification (empty for a socket, which is
|
||||||
|
/// always plain HTTP).
|
||||||
pub fn host_str(&self) -> String {
|
pub fn host_str(&self) -> String {
|
||||||
match &self.host {
|
match &self.host {
|
||||||
UpstreamHost::Name(n) => n.clone(),
|
UpstreamHost::Name(n) => n.clone(),
|
||||||
UpstreamHost::Ip(ip) => ip.to_string(),
|
UpstreamHost::Ip(ip) => ip.to_string(),
|
||||||
|
UpstreamHost::Unix(_) => String::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The socket path of a `unix:` upstream.
|
||||||
|
pub fn unix_path(&self) -> Option<&str> {
|
||||||
|
match &self.host {
|
||||||
|
UpstreamHost::Unix(p) => Some(p),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_unix(&self) -> bool {
|
||||||
|
self.unix_path().is_some()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for Upstream {
|
impl fmt::Display for Upstream {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
if self.is_unix() {
|
||||||
|
return f.write_str(&self.authority());
|
||||||
|
}
|
||||||
write!(f, "{}://{}", self.scheme.as_str(), self.authority())
|
write!(f, "{}://{}", self.scheme.as_str(), self.authority())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,118 @@
|
|||||||
|
// 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/.
|
||||||
|
|
||||||
|
//! Unix-socket upstreams (G14 option b, tests first): the admin host reaches control on a
|
||||||
|
//! Unix socket in a directory only the ianitor group can use, so no local process can take
|
||||||
|
//! control's place while it restarts. Route files are hostile input: the path is strict, and
|
||||||
|
//! which socket a host may use is control's decision (its `[ui] socket`), not core's.
|
||||||
|
|
||||||
|
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
|
||||||
|
|
||||||
|
use ianitor_core::compile::{self, CompileOptions};
|
||||||
|
use ianitor_core::edge_config::EdgeConfig;
|
||||||
|
use ianitor_core::{Model, SourceFile, ValidationError, load};
|
||||||
|
|
||||||
|
const SOCKET: &str = "/run/ianitor-control/ui.sock";
|
||||||
|
|
||||||
|
fn load_host(upstreams: &str) -> Result<Model, Vec<ValidationError>> {
|
||||||
|
let body = format!("hostname = \"ui.test\"\ntls = \"domi\"\nupstreams = [{upstreams}]\n");
|
||||||
|
load(
|
||||||
|
&SourceFile {
|
||||||
|
name: "ianitor.toml",
|
||||||
|
contents: "",
|
||||||
|
},
|
||||||
|
&[SourceFile {
|
||||||
|
name: "hosts/ui.test.toml",
|
||||||
|
contents: &body,
|
||||||
|
}],
|
||||||
|
)
|
||||||
|
.map_err(ianitor_core::ValidationErrors::into_errors)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn options() -> CompileOptions {
|
||||||
|
CompileOptions {
|
||||||
|
http_listen: "0.0.0.0:80".parse().unwrap(),
|
||||||
|
https_listen: "0.0.0.0:443".parse().unwrap(),
|
||||||
|
cert_dir: "/var/lib/ianitor/certs".into(),
|
||||||
|
state_dir: "/var/lib/ianitor".into(),
|
||||||
|
access_log: "/var/log/ianitor/access.log".into(),
|
||||||
|
caddy_credentials_dir: "/run/credentials/caddy-fallback.service".into(),
|
||||||
|
caddy_admin: None,
|
||||||
|
caddy_admin_socket: None,
|
||||||
|
trusted_proxies: Vec::new(),
|
||||||
|
proxy_socket: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn a_unix_socket_upstream_reaches_both_engines() {
|
||||||
|
let model = load_host(&format!("\"unix:{SOCKET}\"")).unwrap();
|
||||||
|
let up = &model.hosts()[0].upstreams[0];
|
||||||
|
assert!(up.is_unix());
|
||||||
|
assert_eq!(up.to_string(), format!("unix:{SOCKET}"));
|
||||||
|
|
||||||
|
let edge: EdgeConfig =
|
||||||
|
serde_json::from_str(&compile::edge(&model, &options()).unwrap()).unwrap();
|
||||||
|
let u = &edge.hosts[0].upstreams[0];
|
||||||
|
assert_eq!(u.address, format!("unix:{SOCKET}"));
|
||||||
|
assert!(!u.tls);
|
||||||
|
|
||||||
|
let caddy: serde_json::Value =
|
||||||
|
serde_json::from_str(&compile::caddy(&model, &options()).unwrap()).unwrap();
|
||||||
|
let text = caddy.to_string();
|
||||||
|
assert!(
|
||||||
|
text.contains("\"dial\":\"unix//run/ianitor-control/ui.sock\""),
|
||||||
|
"{text}"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn a_unix_socket_path_is_strict() {
|
||||||
|
let long = format!("unix:/run/{}.sock", "a".repeat(100));
|
||||||
|
for bad in [
|
||||||
|
"unix:relative.sock",
|
||||||
|
"unix:",
|
||||||
|
"unix:/",
|
||||||
|
"unix:/run/",
|
||||||
|
"unix://run/x.sock",
|
||||||
|
"unix:/run//x.sock",
|
||||||
|
"unix:/run/../etc/x.sock",
|
||||||
|
"unix:/run/./x.sock",
|
||||||
|
"unix:/run/x y.sock",
|
||||||
|
"unix:/run/{env.HOME}.sock",
|
||||||
|
"unix:/run/x.sock?q=1",
|
||||||
|
"unix:/run/x\\y.sock",
|
||||||
|
"unix:/run/ü.sock",
|
||||||
|
long.as_str(),
|
||||||
|
] {
|
||||||
|
let errs = load_host(&format!("{:?}", bad))
|
||||||
|
.err()
|
||||||
|
.unwrap_or_else(|| panic!("{bad} was accepted"));
|
||||||
|
assert!(
|
||||||
|
errs.iter().any(|e| e.code() == "host.upstream.unix_path"),
|
||||||
|
"{bad}: {errs:?}"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn forward_auth_never_goes_to_a_socket() {
|
||||||
|
let errs = load(
|
||||||
|
&SourceFile {
|
||||||
|
name: "ianitor.toml",
|
||||||
|
contents: "[forward_auth]\nurl = \"unix:/run/authelia.sock\"\n",
|
||||||
|
},
|
||||||
|
&[],
|
||||||
|
)
|
||||||
|
.unwrap_err();
|
||||||
|
assert!(!errs.errors().is_empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn a_unix_upstream_and_a_tcp_one_are_both_kept() {
|
||||||
|
let model = load_host(&format!("\"unix:{SOCKET}\", \"http://127.0.0.1:7878\"")).unwrap();
|
||||||
|
assert_eq!(model.hosts()[0].upstreams.len(), 2);
|
||||||
|
let dup = load_host(&format!("\"unix:{SOCKET}\", \"unix:{SOCKET}\"")).unwrap_err();
|
||||||
|
assert!(dup.iter().any(|e| e.code() == "host.upstream.duplicate"));
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user