[core] Schema: tests first for validation and compiler determinism (red: 37+11 failing)
This commit is contained in:
@@ -0,0 +1,225 @@
|
||||
// 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/.
|
||||
|
||||
//! The validated model. Values of these types only come out of [`crate::load`], so every
|
||||
//! compiler can rely on them being valid: the structs are `#[non_exhaustive]` and cannot be
|
||||
//! built outside this crate.
|
||||
|
||||
use std::collections::BTreeSet;
|
||||
use std::fmt;
|
||||
|
||||
/// A validated, lowercase DNS hostname (no wildcard, no trailing dot, not an IP address).
|
||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub struct Hostname(pub(crate) String);
|
||||
|
||||
impl Hostname {
|
||||
pub fn as_str(&self) -> &str {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Hostname {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.write_str(&self.0)
|
||||
}
|
||||
}
|
||||
|
||||
/// A validated secret name. It is also the credential file name under `$CREDENTIALS_DIRECTORY`,
|
||||
/// so it can never contain a path separator.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub struct SecretName(pub(crate) String);
|
||||
|
||||
impl SecretName {
|
||||
pub fn as_str(&self) -> &str {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for SecretName {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.write_str(&self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&str> for SecretName {
|
||||
/// For tests and comparisons only; validation happens in [`crate::load`].
|
||||
fn from(s: &str) -> Self {
|
||||
SecretName(s.to_owned())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub enum Scheme {
|
||||
Http,
|
||||
Https,
|
||||
}
|
||||
|
||||
impl Scheme {
|
||||
pub fn as_str(self) -> &'static str {
|
||||
match self {
|
||||
Scheme::Http => "http",
|
||||
Scheme::Https => "https",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Where the upstream host is: a DNS name or an IP literal (IPv6 without brackets).
|
||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub enum UpstreamHost {
|
||||
Name(String),
|
||||
Ip(std::net::IpAddr),
|
||||
}
|
||||
|
||||
/// A validated upstream: scheme, host and explicit port. No path, query, fragment or userinfo.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
#[non_exhaustive]
|
||||
pub struct Upstream {
|
||||
pub scheme: Scheme,
|
||||
pub host: UpstreamHost,
|
||||
pub port: u16,
|
||||
}
|
||||
|
||||
impl Upstream {
|
||||
/// `host:port` as used for dialing, with IPv6 in brackets.
|
||||
pub fn authority(&self) -> String {
|
||||
match &self.host {
|
||||
UpstreamHost::Name(n) => format!("{n}:{}", self.port),
|
||||
UpstreamHost::Ip(std::net::IpAddr::V6(ip)) => format!("[{ip}]:{}", self.port),
|
||||
UpstreamHost::Ip(ip) => format!("{ip}:{}", self.port),
|
||||
}
|
||||
}
|
||||
|
||||
/// The host part for TLS SNI and certificate verification.
|
||||
pub fn host_str(&self) -> String {
|
||||
match &self.host {
|
||||
UpstreamHost::Name(n) => n.clone(),
|
||||
UpstreamHost::Ip(ip) => ip.to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Upstream {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{}://{}", self.scheme.as_str(), self.authority())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum TlsMode {
|
||||
/// Public certificate from Let's Encrypt via ACME (HTTP-01).
|
||||
LetsEncrypt,
|
||||
/// Certificate issued by the Domi CA, read from the certificate directory.
|
||||
Domi,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum AuthMode {
|
||||
None,
|
||||
/// Every request is first checked with the site's forward-auth endpoint (Authelia).
|
||||
ForwardAuth,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum AccessLog {
|
||||
Off,
|
||||
/// Logged with IPv4 truncated to /24 and IPv6 to /48.
|
||||
Truncated,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum HeaderValue {
|
||||
Literal(String),
|
||||
/// Resolved by the engine at load time; never part of any compiled output.
|
||||
Secret(SecretName),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum HeaderRule {
|
||||
Set { name: String, value: HeaderValue },
|
||||
Remove { name: String },
|
||||
}
|
||||
|
||||
impl HeaderRule {
|
||||
pub fn name(&self) -> &str {
|
||||
match self {
|
||||
HeaderRule::Set { name, .. } | HeaderRule::Remove { name } => name,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
#[non_exhaustive]
|
||||
pub struct HealthCheck {
|
||||
pub path: String,
|
||||
pub interval_secs: u32,
|
||||
}
|
||||
|
||||
/// One validated host file.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
#[non_exhaustive]
|
||||
pub struct Host {
|
||||
pub hostname: Hostname,
|
||||
/// The route file this host came from, for messages.
|
||||
pub source: String,
|
||||
pub upstreams: Vec<Upstream>,
|
||||
pub tls: TlsMode,
|
||||
pub auth: AuthMode,
|
||||
pub request_headers: Vec<HeaderRule>,
|
||||
pub response_headers: Vec<HeaderRule>,
|
||||
pub websockets: bool,
|
||||
pub health_check: Option<HealthCheck>,
|
||||
pub share_links: bool,
|
||||
pub access_log: AccessLog,
|
||||
}
|
||||
|
||||
/// The forward-auth endpoint (Authelia's `/api/authz/forward-auth`).
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
#[non_exhaustive]
|
||||
pub struct ForwardAuth {
|
||||
pub upstream: Upstream,
|
||||
/// Path plus optional query, e.g. `/api/authz/forward-auth`.
|
||||
pub path_and_query: String,
|
||||
/// Headers copied from a 2xx auth response onto the upstream request.
|
||||
pub copy_headers: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
#[non_exhaustive]
|
||||
pub struct Acme {
|
||||
pub email: Option<String>,
|
||||
pub directory: String,
|
||||
}
|
||||
|
||||
/// Site-wide settings from `ianitor.toml`.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
#[non_exhaustive]
|
||||
pub struct Site {
|
||||
pub secrets: BTreeSet<SecretName>,
|
||||
pub forward_auth: Option<ForwardAuth>,
|
||||
pub acme: Acme,
|
||||
}
|
||||
|
||||
/// The whole validated routes tree. Hosts are sorted by hostname.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Model {
|
||||
pub(crate) site: Site,
|
||||
pub(crate) hosts: Vec<Host>,
|
||||
}
|
||||
|
||||
impl Model {
|
||||
pub fn site(&self) -> &Site {
|
||||
&self.site
|
||||
}
|
||||
|
||||
pub fn hosts(&self) -> &[Host] {
|
||||
&self.hosts
|
||||
}
|
||||
}
|
||||
|
||||
/// Let's Encrypt production directory, the default when `[acme] directory` is not set.
|
||||
pub const LETSENCRYPT_PRODUCTION: &str = "https://acme-v02.api.letsencrypt.org/directory";
|
||||
/// Let's Encrypt staging directory.
|
||||
pub const LETSENCRYPT_STAGING: &str = "https://acme-staging-v02.api.letsencrypt.org/directory";
|
||||
/// Identity headers Authelia returns on success (Authelia's Caddy integration guide).
|
||||
pub const AUTHELIA_COPY_HEADERS: [&str; 4] = ["Remote-User", "Remote-Groups", "Remote-Email", "Remote-Name"];
|
||||
Reference in New Issue
Block a user