[core] Tests first: share tokens, prefix escapes, repo layout, exact host path, error codes (red)
This commit is contained in:
+162
-9
@@ -2,11 +2,67 @@
|
||||
// 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/.
|
||||
|
||||
//! Typed validation errors. Every message names the file and says what is wrong in plain English,
|
||||
//! so the UI can show it as is.
|
||||
//! Typed validation errors. Every message names the file and says what is wrong in plain English.
|
||||
//!
|
||||
//! Each error also has a stable [`ValidationError::code`], the form field it is about
|
||||
//! ([`ValidationError::field`], e.g. `upstreams[1]`) and the values the UI shows in bold
|
||||
//! ([`ValidationError::params`]). The control API maps the code to the UI's copy
|
||||
//! (docs/ui-api.md §3.1) and keeps the `Display` text below as the technical detail.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::fmt;
|
||||
|
||||
/// The reasons carried in `&'static str` fields. [`ValidationError::code`] compares them by
|
||||
/// value, so every reason is written once, here.
|
||||
pub(crate) mod reason {
|
||||
pub const HOST_EMPTY: &str = "it is empty";
|
||||
pub const HOST_TOO_LONG: &str = "it is longer than 253 characters";
|
||||
pub const HOST_IP: &str = "IP addresses are not hostnames";
|
||||
pub const HOST_WILDCARD: &str = "wildcards are not supported";
|
||||
pub const HOST_TRAILING_DOT: &str = "it must not end with a dot";
|
||||
pub const HOST_UPPERCASE: &str = "it must be lowercase";
|
||||
pub const HOST_SPACES: &str = "it must not contain spaces";
|
||||
pub const HOST_EMPTY_LABEL: &str = "it has an empty label";
|
||||
pub const HOST_LABEL_LONG: &str = "a label is longer than 63 characters";
|
||||
pub const HOST_CHARACTERS: &str = "only a-z, 0-9, '-' and '.' are allowed";
|
||||
pub const HOST_LABEL_DASH: &str = "a label must not start or end with '-'";
|
||||
pub const HOST_NUMERIC: &str = "the last label must not be numeric";
|
||||
|
||||
pub const URL_ASCII: &str = "it must be plain ASCII without spaces";
|
||||
pub const URL_NOT_URL: &str = "it is not a URL like http://127.0.0.1:8080";
|
||||
pub const URL_SCHEME: &str = "only http:// and https:// are supported";
|
||||
pub const URL_CREDENTIALS: &str = "credentials in the URL are not allowed";
|
||||
pub const URL_FRAGMENT: &str = "a #fragment is not allowed";
|
||||
pub const URL_NO_HOST: &str = "it has no host";
|
||||
pub const URL_HOST_NAME: &str = "the host name is not valid";
|
||||
pub const URL_NO_PORT: &str = "it has no port";
|
||||
pub const URL_PORT_ZERO: &str = "port 0 is not usable";
|
||||
pub const URL_PATH: &str = "a path is not allowed; requests keep their own path";
|
||||
pub const URL_QUERY: &str = "a ?query is not allowed";
|
||||
pub const URL_BRACES: &str = "'{' and '}' are not allowed";
|
||||
pub const URL_MIXED: &str = "all upstreams of a host must use the same scheme";
|
||||
|
||||
pub const VALUE_EMPTY: &str = "an empty value is not allowed; use action = \"remove\"";
|
||||
pub const VALUE_LONG: &str = "it is longer than 4096 characters";
|
||||
pub const VALUE_ASCII: &str = "only visible ASCII characters and spaces are allowed";
|
||||
pub const VALUE_BRACES: &str = "'{' and '}' are not allowed (they are placeholders in Caddy)";
|
||||
pub const VALUE_EDGE_SPACE: &str = "it must not start or end with a space";
|
||||
|
||||
pub const RULE_REMOVE_VALUE: &str = "remove takes no value or secret";
|
||||
pub const RULE_BOTH: &str = "give either value or secret, not both";
|
||||
pub const RULE_NEITHER: &str = "set needs a value or a secret";
|
||||
|
||||
pub const HEALTH_PATH: &str =
|
||||
"path must start with '/' and contain only visible ASCII without '{', '}' or '#'";
|
||||
pub const HEALTH_INTERVAL: &str = "interval_secs must be between 1 and 3600";
|
||||
|
||||
pub const PATH_LAYOUT: &str =
|
||||
"only ianitor.toml and hosts/<hostname>.toml belong in the routes repository";
|
||||
pub const PATH_SUBDIR: &str = "folders inside hosts/ are not allowed";
|
||||
pub const PATH_NAME: &str = "the file name must be a lowercase hostname followed by .toml";
|
||||
pub const PATH_KIND: &str = "symbolic links, executables and submodules are not allowed";
|
||||
}
|
||||
|
||||
/// One problem found in the route files.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
|
||||
pub enum ValidationError {
|
||||
@@ -26,8 +82,10 @@ pub enum ValidationError {
|
||||
DuplicateFile { file: String },
|
||||
#[error("{file} is larger than {max} bytes")]
|
||||
FileTooLarge { file: String, max: usize },
|
||||
#[error("{file} must be named after its hostname: {hostname}.toml")]
|
||||
#[error("{file} must be named after its hostname: hosts/{hostname}.toml")]
|
||||
FileNameMismatch { file: String, hostname: String },
|
||||
#[error("{file}: {reason}")]
|
||||
UnexpectedFile { file: String, reason: &'static str },
|
||||
#[error("{file}: at most {max} upstreams per host")]
|
||||
TooManyUpstreams { file: String, max: usize },
|
||||
#[error("{file}: `{hostname}` is not a valid hostname: {reason}")]
|
||||
@@ -49,36 +107,64 @@ pub enum ValidationError {
|
||||
file: String,
|
||||
url: String,
|
||||
reason: &'static str,
|
||||
/// Position in `upstreams`; `None` when the problem is about the list as a whole.
|
||||
index: Option<usize>,
|
||||
},
|
||||
#[error("{file}: upstream `{url}` is listed twice")]
|
||||
DuplicateUpstream { file: String, url: String },
|
||||
DuplicateUpstream {
|
||||
file: String,
|
||||
url: String,
|
||||
index: usize,
|
||||
},
|
||||
#[error("{file}: secret `{secret}` is used but not declared in the site file")]
|
||||
UndefinedSecret { file: String, secret: String },
|
||||
UndefinedSecret {
|
||||
file: String,
|
||||
secret: String,
|
||||
field: String,
|
||||
},
|
||||
#[error(
|
||||
"{file}: response header `{header}` may not use a secret; secrets only go to upstreams"
|
||||
)]
|
||||
SecretInResponseHeader { file: String, header: String },
|
||||
SecretInResponseHeader {
|
||||
file: String,
|
||||
header: String,
|
||||
field: String,
|
||||
},
|
||||
#[error(
|
||||
"{file}: `{name}` is not a valid secret name (use a-z, 0-9, '_' and '-', at most 64 characters)"
|
||||
)]
|
||||
InvalidSecretName { file: String, name: String },
|
||||
#[error("{file}: `{name}` is not a valid header name")]
|
||||
InvalidHeaderName { file: String, name: String },
|
||||
InvalidHeaderName {
|
||||
file: String,
|
||||
name: String,
|
||||
field: String,
|
||||
},
|
||||
#[error("{file}: value for header `{header}` is not allowed: {reason}")]
|
||||
InvalidHeaderValue {
|
||||
file: String,
|
||||
header: String,
|
||||
reason: &'static str,
|
||||
field: String,
|
||||
},
|
||||
#[error("{file}: header `{header}` is managed by the proxy and cannot be changed by a rule")]
|
||||
ProtectedHeader { file: String, header: String },
|
||||
ProtectedHeader {
|
||||
file: String,
|
||||
header: String,
|
||||
field: String,
|
||||
},
|
||||
#[error("{file}: header `{header}` has more than one rule in the same list")]
|
||||
DuplicateHeaderRule { file: String, header: String },
|
||||
DuplicateHeaderRule {
|
||||
file: String,
|
||||
header: String,
|
||||
field: String,
|
||||
},
|
||||
#[error("{file}: rule for header `{header}` is incomplete: {reason}")]
|
||||
BadHeaderRule {
|
||||
file: String,
|
||||
header: String,
|
||||
reason: &'static str,
|
||||
field: String,
|
||||
},
|
||||
#[error("{file}: health check is not usable: {reason}")]
|
||||
BadHealthCheck { file: String, reason: &'static str },
|
||||
@@ -97,6 +183,68 @@ fn line_suffix(line: Option<usize>) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
impl ValidationError {
|
||||
/// The file the problem is in (for a duplicate hostname: the second file).
|
||||
pub fn file(&self) -> &str {
|
||||
use ValidationError as E;
|
||||
match self {
|
||||
E::Syntax { file, .. }
|
||||
| E::UnknownField { file, .. }
|
||||
| E::MissingField { file, .. }
|
||||
| E::InvalidValue { file, .. }
|
||||
| E::DuplicateFile { file }
|
||||
| E::FileTooLarge { file, .. }
|
||||
| E::FileNameMismatch { file, .. }
|
||||
| E::UnexpectedFile { file, .. }
|
||||
| E::TooManyUpstreams { file, .. }
|
||||
| E::InvalidHostname { file, .. }
|
||||
| E::NoUpstreams { file }
|
||||
| E::BadUpstreamUrl { file, .. }
|
||||
| E::DuplicateUpstream { file, .. }
|
||||
| E::UndefinedSecret { file, .. }
|
||||
| E::SecretInResponseHeader { file, .. }
|
||||
| E::InvalidSecretName { file, .. }
|
||||
| E::InvalidHeaderName { file, .. }
|
||||
| E::InvalidHeaderValue { file, .. }
|
||||
| E::ProtectedHeader { file, .. }
|
||||
| E::DuplicateHeaderRule { file, .. }
|
||||
| E::BadHeaderRule { file, .. }
|
||||
| E::BadHealthCheck { file, .. }
|
||||
| E::ForwardAuthNotConfigured { file }
|
||||
| E::BadForwardAuth { file, .. }
|
||||
| E::BadAcme { file, .. } => file,
|
||||
E::DuplicateHostname { second, .. } => second,
|
||||
}
|
||||
}
|
||||
|
||||
/// True when the problem is in the site file (or a stray file), not in a host file.
|
||||
pub fn in_site_file(&self) -> bool {
|
||||
!self.file().starts_with("hosts/")
|
||||
}
|
||||
|
||||
/// A stable code the UI switches on, e.g. `host.upstream.no_port` (docs/ui-api.md §3.1).
|
||||
pub fn code(&self) -> &'static str {
|
||||
// Tests first: not written yet.
|
||||
"unknown"
|
||||
}
|
||||
|
||||
/// The form field the problem is about, as a path into the host body (`hostname`,
|
||||
/// `upstreams[0]`, `request_headers[2].name`, `health_check.path`), or `None`.
|
||||
pub fn field(&self) -> Option<String> {
|
||||
// Tests first: not written yet.
|
||||
None
|
||||
}
|
||||
|
||||
/// The values a message shows: always `file`, plus `line`, `field`, `max`, `hostname`,
|
||||
/// `suggestion`, `url`, `name` or `secret` where they apply.
|
||||
pub fn params(&self) -> BTreeMap<&'static str, String> {
|
||||
// Tests first: not written yet.
|
||||
let mut p = BTreeMap::new();
|
||||
p.insert("file", self.file().to_owned());
|
||||
p
|
||||
}
|
||||
}
|
||||
|
||||
/// All problems found in one load, in a stable order (file by file, then hostname checks).
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct ValidationErrors(pub(crate) Vec<ValidationError>);
|
||||
@@ -109,6 +257,11 @@ impl ValidationErrors {
|
||||
pub fn into_errors(self) -> Vec<ValidationError> {
|
||||
self.0
|
||||
}
|
||||
|
||||
/// Wrap errors found outside [`crate::load`] (e.g. by [`crate::routes::check_path`]).
|
||||
pub fn from_errors(errors: Vec<ValidationError>) -> ValidationErrors {
|
||||
ValidationErrors(errors)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for ValidationErrors {
|
||||
|
||||
Reference in New Issue
Block a user