[core] Share links and repo layout: HMAC tokens with epoch, prefix check, exact hosts/<hostname>.toml, error codes; edge config v2
This commit is contained in:
+157
-5
@@ -224,23 +224,175 @@ impl ValidationError {
|
||||
|
||||
/// 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"
|
||||
use ValidationError as E;
|
||||
use reason as r;
|
||||
let site = self.in_site_file();
|
||||
match self {
|
||||
E::Syntax { .. } => "file.syntax",
|
||||
E::UnknownField { .. } if site => "site.field.unknown",
|
||||
E::UnknownField { .. } => "host.field.unknown",
|
||||
E::MissingField { field, .. } if !site && field == "hostname" => {
|
||||
"host.hostname.missing"
|
||||
}
|
||||
E::MissingField { field, .. } if !site && field == "upstreams" => {
|
||||
"host.upstreams.missing"
|
||||
}
|
||||
E::MissingField { .. } => "file.field.missing",
|
||||
E::InvalidValue { .. } => "file.field.invalid",
|
||||
E::DuplicateFile { .. } => "repo.duplicate_file",
|
||||
E::FileTooLarge { .. } => "file.too_large",
|
||||
E::FileNameMismatch { .. } => "host.file_name",
|
||||
E::UnexpectedFile { .. } => "repo.unexpected_file",
|
||||
E::TooManyUpstreams { .. } => "host.upstream.too_many",
|
||||
E::InvalidHostname { reason, .. } => match *reason {
|
||||
x if x == r::HOST_EMPTY => "host.hostname.missing",
|
||||
x if x == r::HOST_TOO_LONG => "host.hostname.too_long",
|
||||
x if x == r::HOST_IP || x == r::HOST_NUMERIC => "host.hostname.numeric",
|
||||
x if x == r::HOST_WILDCARD => "host.hostname.wildcard",
|
||||
x if x == r::HOST_TRAILING_DOT => "host.hostname.trailing_dot",
|
||||
x if x == r::HOST_UPPERCASE => "host.hostname.uppercase",
|
||||
x if x == r::HOST_SPACES => "host.hostname.spaces",
|
||||
x if x == r::HOST_EMPTY_LABEL => "host.hostname.empty_label",
|
||||
x if x == r::HOST_LABEL_LONG => "host.hostname.label_long",
|
||||
x if x == r::HOST_LABEL_DASH => "host.hostname.label_dash",
|
||||
_ => "host.hostname.characters",
|
||||
},
|
||||
E::DuplicateHostname { .. } => "host.hostname.taken",
|
||||
E::NoUpstreams { .. } => "host.upstreams.missing",
|
||||
E::BadUpstreamUrl { reason, .. } => match *reason {
|
||||
x if x == r::URL_SCHEME => "host.upstream.scheme",
|
||||
x if x == r::URL_CREDENTIALS => "host.upstream.credentials",
|
||||
x if x == r::URL_FRAGMENT => "host.upstream.fragment",
|
||||
x if x == r::URL_NO_HOST => "host.upstream.no_host",
|
||||
x if x == r::URL_HOST_NAME => "host.upstream.host_name",
|
||||
x if x == r::URL_NO_PORT => "host.upstream.no_port",
|
||||
x if x == r::URL_PORT_ZERO => "host.upstream.port_zero",
|
||||
x if x == r::URL_PATH => "host.upstream.path",
|
||||
x if x == r::URL_QUERY => "host.upstream.query",
|
||||
x if x == r::URL_MIXED => "host.upstream.mixed_schemes",
|
||||
_ => "host.upstream.not_url",
|
||||
},
|
||||
E::DuplicateUpstream { .. } => "host.upstream.duplicate",
|
||||
E::UndefinedSecret { .. } => "host.header.secret_unknown",
|
||||
E::SecretInResponseHeader { .. } => "host.header.secret_response",
|
||||
E::InvalidSecretName { .. } => "site.secret.name",
|
||||
E::InvalidHeaderName { .. } | E::ProtectedHeader { .. } if site => {
|
||||
"site.forward_auth.header"
|
||||
}
|
||||
E::InvalidHeaderName { .. } => "host.header.name",
|
||||
E::InvalidHeaderValue { reason, .. } if *reason == r::VALUE_EMPTY => {
|
||||
"host.header.empty"
|
||||
}
|
||||
E::InvalidHeaderValue { .. } => "host.header.value",
|
||||
E::ProtectedHeader { .. } => "host.header.protected",
|
||||
E::DuplicateHeaderRule { .. } => "host.header.duplicate",
|
||||
E::BadHeaderRule { reason, .. } => match *reason {
|
||||
x if x == r::RULE_BOTH => "host.header.both",
|
||||
x if x == r::RULE_REMOVE_VALUE => "host.header.remove_value",
|
||||
_ => "host.header.empty",
|
||||
},
|
||||
E::BadHealthCheck { reason, .. } if *reason == r::HEALTH_INTERVAL => {
|
||||
"host.health.interval"
|
||||
}
|
||||
E::BadHealthCheck { .. } => "host.health.path",
|
||||
E::ForwardAuthNotConfigured { .. } => "host.auth.not_configured",
|
||||
E::BadForwardAuth { .. } => "site.forward_auth",
|
||||
E::BadAcme { .. } => "site.acme",
|
||||
}
|
||||
}
|
||||
|
||||
/// 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
|
||||
use ValidationError as E;
|
||||
if self.in_site_file() {
|
||||
return None;
|
||||
}
|
||||
match self {
|
||||
E::UnknownField { field, .. } | E::MissingField { field, .. } => Some(field.clone()),
|
||||
E::FileNameMismatch { .. }
|
||||
| E::InvalidHostname { .. }
|
||||
| E::DuplicateHostname { .. } => Some("hostname".into()),
|
||||
E::TooManyUpstreams { .. } | E::NoUpstreams { .. } => Some("upstreams".into()),
|
||||
E::BadUpstreamUrl { index, .. } => Some(match index {
|
||||
Some(i) => format!("upstreams[{i}]"),
|
||||
None => "upstreams".into(),
|
||||
}),
|
||||
E::DuplicateUpstream { index, .. } => Some(format!("upstreams[{index}]")),
|
||||
E::UndefinedSecret { field, .. }
|
||||
| E::SecretInResponseHeader { field, .. }
|
||||
| E::InvalidHeaderName { field, .. }
|
||||
| E::InvalidHeaderValue { field, .. }
|
||||
| E::ProtectedHeader { field, .. }
|
||||
| E::DuplicateHeaderRule { field, .. }
|
||||
| E::BadHeaderRule { field, .. } => Some(field.clone()),
|
||||
E::BadHealthCheck { reason, .. } if *reason == reason::HEALTH_INTERVAL => {
|
||||
Some("health_check.interval_secs".into())
|
||||
}
|
||||
E::BadHealthCheck { .. } => Some("health_check.path".into()),
|
||||
E::ForwardAuthNotConfigured { .. } => Some("auth".into()),
|
||||
_ => 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.
|
||||
use ValidationError as E;
|
||||
let mut p = BTreeMap::new();
|
||||
p.insert("file", self.file().to_owned());
|
||||
match self {
|
||||
E::Syntax { line: Some(l), .. } => {
|
||||
p.insert("line", l.to_string());
|
||||
}
|
||||
E::UnknownField { field, .. } | E::MissingField { field, .. } => {
|
||||
p.insert("field", field.clone());
|
||||
}
|
||||
E::FileTooLarge { max, .. } | E::TooManyUpstreams { max, .. } => {
|
||||
p.insert("max", max.to_string());
|
||||
}
|
||||
E::FileNameMismatch { hostname, .. } | E::DuplicateHostname { hostname, .. } => {
|
||||
p.insert("hostname", hostname.clone());
|
||||
}
|
||||
E::InvalidHostname {
|
||||
hostname, reason, ..
|
||||
} => {
|
||||
p.insert("hostname", hostname.clone());
|
||||
if *reason == reason::HOST_UPPERCASE || *reason == reason::HOST_TRAILING_DOT {
|
||||
p.insert(
|
||||
"suggestion",
|
||||
hostname.trim_end_matches('.').to_ascii_lowercase(),
|
||||
);
|
||||
}
|
||||
}
|
||||
E::BadUpstreamUrl { url, reason, .. } => {
|
||||
p.insert("url", url.clone());
|
||||
if [reason::URL_PATH, reason::URL_QUERY, reason::URL_FRAGMENT].contains(reason)
|
||||
&& let Ok(u) = url::Url::parse(url)
|
||||
&& let Some(host) = u.host_str()
|
||||
{
|
||||
let port = u.port().map(|p| format!(":{p}")).unwrap_or_default();
|
||||
p.insert("suggestion", format!("{}://{host}{port}", u.scheme()));
|
||||
}
|
||||
}
|
||||
E::DuplicateUpstream { url, .. } => {
|
||||
p.insert("url", url.clone());
|
||||
}
|
||||
E::UndefinedSecret { secret, .. } => {
|
||||
p.insert("secret", secret.clone());
|
||||
}
|
||||
E::InvalidSecretName { name, .. } | E::InvalidHeaderName { name, .. } => {
|
||||
p.insert("name", name.clone());
|
||||
}
|
||||
E::SecretInResponseHeader { header, .. }
|
||||
| E::InvalidHeaderValue { header, .. }
|
||||
| E::ProtectedHeader { header, .. }
|
||||
| E::DuplicateHeaderRule { header, .. }
|
||||
| E::BadHeaderRule { header, .. } => {
|
||||
p.insert("name", header.clone());
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
p
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user