// SPDX-License-Identifier: Apache-2.0 // Copyright (c) 2026 Shane Utt //! Filter conversion from Gateway API filter types to Praxis filter config. use gateway_api::httproutes::{ HttpRouteRules, HttpRouteRulesFilters, HttpRouteRulesFiltersRequestHeaderModifier, HttpRouteRulesFiltersRequestRedirectScheme, HttpRouteRulesFiltersResponseHeaderModifier, HttpRouteRulesFiltersType, HttpRouteRulesFiltersUrlRewrite, HttpRouteRulesFiltersUrlRewritePath, HttpRouteRulesFiltersUrlRewritePathType, HttpRouteRulesMatches, HttpRouteRulesMatchesPathType, }; use serde::Serialize; use tracing::warn; use super::routing::PraxisFilterEntry; // ----------------------------------------------------------------------------- // Constants // ----------------------------------------------------------------------------- /// Redirect status used when the route names none, or names one Praxis /// cannot accept. const DEFAULT_REDIRECT_STATUS: u16 = 302; // ----------------------------------------------------------------------------- // HeaderEntry // ----------------------------------------------------------------------------- /// Header modification entry. /// /// Represents a header name-value pair for modification filters. #[derive(Debug, Clone, Serialize, PartialEq)] struct HeaderEntry { /// Header name. name: String, /// Header value. value: String, } // ----------------------------------------------------------------------------- // HeaderFilterConfig // ----------------------------------------------------------------------------- /// Header filter configuration matching the Praxis `HeaderFilterConfig`. /// /// Maps to all six fields in the proxy's `deny_unknown_fields` schema: /// `request_add`, `request_set`, `request_remove`, `response_add`, /// `response_set`, and `response_remove`. #[derive(Debug, Clone, Default, Serialize, PartialEq)] struct HeaderFilterConfig { /// Headers to add to the request. #[serde(skip_serializing_if = "Option::is_none")] request_add: Option>, /// Header names to remove from the request. #[serde(skip_serializing_if = "Option::is_none")] request_remove: Option>, /// Headers to set on the request (overwrites existing values). #[serde(skip_serializing_if = "Option::is_none")] request_set: Option>, /// Headers to add to the response. #[serde(skip_serializing_if = "Option::is_none")] response_add: Option>, /// Header names to remove from the response. #[serde(skip_serializing_if = "Option::is_none")] response_remove: Option>, /// Headers to set on the response. #[serde(skip_serializing_if = "Option::is_none")] response_set: Option>, } // ----------------------------------------------------------------------------- // RedirectFilterConfig // ----------------------------------------------------------------------------- /// Redirect filter configuration matching the Praxis `RedirectConfig`. /// /// The proxy expects `status` (u16) and `location` (URL template with /// `${path}` and `${query}` placeholders). #[derive(Debug, Clone, Serialize, PartialEq)] struct RedirectFilterConfig { /// HTTP redirect status code (301, 302, 307, 308). status: u16, /// Location URL template with `${path}` and `${query}` placeholders. location: String, } // ----------------------------------------------------------------------------- // RouteFilters // ----------------------------------------------------------------------------- /// The filters one Gateway's routes contribute, split by where in the /// chain they have to run. /// /// Praxis evaluates a chain in order and the `router` filter sits in the /// middle of it, so a route filter's position is not a matter of taste. /// The router picks a cluster from the request's path and `Host` header, /// reading `ctx.rewritten_path` in preference to the original URI, and /// rejects with 404 when nothing matches. /// /// That splits route filters in two. A filter that answers the request /// itself belongs before the router, because the rule it came from has /// no backend and therefore no route for the router to find. A filter /// that changes what the router reads — the path or the `Host` header — /// belongs after it, because Gateway API selects the rule from the /// request as it arrived and applies the rule's transformations to what /// is forwarded upstream. #[derive(Debug, Clone, Default, PartialEq)] pub struct RouteFilters { /// Filters that answer the request without an upstream. /// /// Run before the router, which would otherwise 404 the traffic /// they exist to serve. pub terminating: Vec, /// Filters that reshape a request the router has already placed. /// /// Run after the router, so route selection sees the request the /// client sent. pub transforming: Vec, } // ----------------------------------------------------------------------------- // ServedRule // ----------------------------------------------------------------------------- /// A rule paired with whether the gateway can forward its traffic. #[derive(Debug, Clone, Copy)] pub struct ServedRule<'rule> { /// The rule contributing filters. pub rule: &'rule HttpRouteRules, /// Whether the rule has a backend the gateway can forward to. /// /// `false` covers two cases: every `backendRef` was refused — a /// cross-namespace reference with no `ReferenceGrant` covering it — /// or the backends are authorized but their cluster has no ready /// endpoints yet. Either way the rule produces no route, and the /// router answers a request for it with 404. The Gateway API /// prescribes 500 for an unresolvable reference, and a Service /// with no ready endpoints can take no traffic either, so both /// get the same static response as a rule that named no backend /// at all. pub resolvable: bool, } // ----------------------------------------------------------------------------- // Filter Conversion // ----------------------------------------------------------------------------- /// Converts `HTTPRoute` filters to Praxis filter configurations. /// /// Each rule produces its own filter entries scoped by Praxis conditional /// filters (`conditions`) derived from the rule's path match. This ensures /// header modifications and redirects apply only to traffic matching the /// originating rule. pub fn convert_filters(rules: &[ServedRule<'_>]) -> RouteFilters { let scoped = scope_rules(rules); let mut filters = RouteFilters::default(); for (index, served) in rules.iter().enumerate() { let condition = rule_conditions(index, &scoped); if !served.resolvable && !rule_has_redirect(served.rule) { emit_no_backend_response(condition.as_ref(), &mut filters.terminating); } emit_conditional_timeout(served.rule, condition.as_ref(), &mut filters.transforming); if served.rule.filters.is_some() { convert_rule_filters(served.rule, condition.as_ref(), &mut filters); } } filters } /// Checks if a rule has a `RequestRedirect` filter. fn rule_has_redirect(rule: &HttpRouteRules) -> bool { rule.filters.as_ref().is_some_and(|fs| { fs.iter() .any(|filter| filter.r#type == HttpRouteRulesFiltersType::RequestRedirect) }) } /// What a rule's filters need to know about the rule that carries them. struct RuleContext<'cond> { /// The Praxis `conditions` list gating every filter of this rule. condition: Option<&'cond yaml_serde::Value>, /// The `PathPrefix` value the rule matched on, if it matched one. /// /// `ReplacePrefixMatch` replaces exactly this much of the request /// path, so the rewrite cannot be built without it. prefix: Option<&'cond str>, } /// Converts filters from a single rule into conditional filter entries. fn convert_rule_filters(rule: &HttpRouteRules, condition: Option<&yaml_serde::Value>, filters: &mut RouteFilters) { let Some(rule_filters) = &rule.filters else { return; }; let ctx = RuleContext { condition, prefix: matched_prefix(rule), }; let mut header_config = HeaderFilterConfig::default(); let mut has_header_mods = false; for filter in rule_filters { has_header_mods |= dispatch_filter(filter, &ctx, &mut header_config, filters); } if has_header_mods { emit_conditional_header_filter(&header_config, condition, &mut filters.transforming); } } /// Returns the `PathPrefix` value the rule's first match uses. fn matched_prefix(rule: &HttpRouteRules) -> Option<&str> { let path = rule.matches.as_deref().and_then(<[_]>::first)?.path.as_ref()?; match path.r#type { Some(HttpRouteRulesMatchesPathType::PathPrefix) => path.value.as_deref(), _ => None, } } /// Dispatches a single filter to the appropriate handler. /// /// Returns `true` if header config was modified. fn dispatch_filter( filter: &HttpRouteRulesFilters, ctx: &RuleContext<'_>, header_config: &mut HeaderFilterConfig, filters: &mut RouteFilters, ) -> bool { match &filter.r#type { HttpRouteRulesFiltersType::RequestHeaderModifier => dispatch_request_header(filter, header_config), HttpRouteRulesFiltersType::ResponseHeaderModifier => dispatch_response_header(filter, header_config), HttpRouteRulesFiltersType::RequestRedirect => { if let Some(redirect) = &filter.request_redirect { emit_conditional_redirect(redirect, ctx.condition, &mut filters.terminating); } false }, HttpRouteRulesFiltersType::UrlRewrite => dispatch_url_rewrite(filter, ctx, header_config, filters), other @ (HttpRouteRulesFiltersType::RequestMirror | HttpRouteRulesFiltersType::ExtensionRef | HttpRouteRulesFiltersType::Cors) => { warn!(?other, "unsupported filter type, ignoring"); false }, } } /// A rule's traffic predicate, in Praxis `ConditionMatch` form. /// /// Empty for a rule that constrains nothing and so matches every /// request. /// /// Filters are chain-level in Praxis, not per-route, so a filter is /// confined to its own rule's traffic only as precisely as /// `praxis_core::config::ConditionMatch` allows: path, path prefix, /// methods and headers. That type has no host field, so two routes /// sharing a listener and a path but differing only in hostname still /// share their filters. Narrowing that further needs host matching in /// the Praxis condition schema. type Predicate = yaml_serde::Mapping; /// Dispatches a `URLRewrite` filter. /// /// Returns `true` when the hostname rewrite put a `Host` header into /// the rule's header config. fn dispatch_url_rewrite( filter: &HttpRouteRulesFilters, ctx: &RuleContext<'_>, header_config: &mut HeaderFilterConfig, filters: &mut RouteFilters, ) -> bool { let Some(rewrite) = &filter.url_rewrite else { return false; }; if let Some(path) = &rewrite.path { emit_conditional_path_rewrite(path, ctx, &mut filters.transforming); } rewrite_host(rewrite, header_config) } /// Records a hostname rewrite as a `Host` request header override. /// /// Praxis has no dedicated host rewrite, but it also never replaces the /// `Host` header on the way upstream, so setting it is the rewrite. The /// filter runs after the router — see [`RouteFilters`] — which is what /// keeps the new hostname out of route selection. fn rewrite_host(rewrite: &HttpRouteRulesFiltersUrlRewrite, header_config: &mut HeaderFilterConfig) -> bool { let Some(hostname) = rewrite.hostname.as_deref() else { return false; }; header_config .request_set .get_or_insert_with(Vec::new) .push(HeaderEntry { name: "host".to_owned(), value: hostname.to_owned(), }); true } /// How a rule ranks against a sibling when both could match a request. /// /// Gateway API resolves an overlap by path specificity first — an /// exact match over any prefix, then the longer prefix — and breaks /// the remaining tie on the number of header matches. Field order here /// is the comparison order the derived `Ord` uses, so it has to stay in /// that sequence. #[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord)] struct Precedence { /// An exact path match outranks every prefix match. exact_path: bool, /// A longer prefix outranks a shorter one. path_len: usize, /// More header matches break a path tie. headers: usize, } /// A rule's predicate paired with the rank it holds among its siblings. struct ScopedRule { /// The traffic this rule claims. predicate: Predicate, /// Where the rule sits in the overlap ordering. precedence: Precedence, } /// Describes every rule by the traffic it claims and its rank. fn scope_rules(rules: &[ServedRule<'_>]) -> Vec { rules.iter().map(|served| scope_rule(served.rule)).collect() } /// Describes one rule by the traffic it claims and its rank. /// /// Only the first match is read. A rule listing several matches claims /// the union of them, which a single `ConditionMatch` cannot express. fn scope_rule(rule: &HttpRouteRules) -> ScopedRule { let first = rule.matches.as_deref().and_then(<[_]>::first); ScopedRule { predicate: first.map(rule_predicate).unwrap_or_default(), precedence: first.map(rule_precedence).unwrap_or_default(), } } /// Builds the predicate matching one rule's traffic. fn rule_predicate(rule_match: &HttpRouteRulesMatches) -> Predicate { let mut predicate = Predicate::new(); insert_path_predicate(rule_match, &mut predicate); insert_header_predicate(rule_match, &mut predicate); predicate } /// Ranks one rule's match against its siblings. fn rule_precedence(rule_match: &HttpRouteRulesMatches) -> Precedence { let path = rule_match.path.as_ref(); Precedence { exact_path: matches!( path.and_then(|pm| pm.r#type.as_ref()), Some(HttpRouteRulesMatchesPathType::Exact) ), path_len: path.and_then(|pm| pm.value.as_deref()).map_or(0, str::len), headers: rule_match.headers.as_deref().map_or(0, <[_]>::len), } } /// Builds the Praxis `conditions` list scoping one rule's filters. /// /// The rule's own predicate becomes a `when`, and the predicate of /// every sibling that outranks it becomes an `unless`. The `unless` /// clauses are what keep a chain-level filter inside its own rule: /// Gateway API hands overlapping traffic to the higher-ranked rule, so /// the loser's filters must not touch it. Without them a rule that /// declares no `matches` at all — which claims every request — would /// rewrite or re-header the traffic of every other rule on the /// listener. /// /// A sibling whose traffic cannot overlap is still listed. Its /// predicate then only ever fails against requests that already fail /// this rule's own `when`, so the extra clause costs a comparison and /// changes nothing. fn rule_conditions(index: usize, scoped: &[ScopedRule]) -> Option { let own = scoped.get(index)?; let mut conditions = Vec::new(); if !own.predicate.is_empty() { conditions.push(condition_entry("when", &own.predicate)); } for predicate in outranking_predicates(own, scoped) { conditions.push(condition_entry("unless", predicate)); } (!conditions.is_empty()).then(|| yaml_serde::Value::Sequence(conditions)) } /// Collects the distinct predicates of the rules that outrank `own`. fn outranking_predicates<'scope>(own: &ScopedRule, scoped: &'scope [ScopedRule]) -> Vec<&'scope Predicate> { let mut collected: Vec<&Predicate> = Vec::new(); for other in scoped { let outranks = other.precedence > own.precedence && !other.predicate.is_empty(); if outranks && !collected.contains(&&other.predicate) { collected.push(&other.predicate); } } collected } /// Wraps a predicate in a `when` or `unless` condition entry. fn condition_entry(keyword: &str, predicate: &Predicate) -> yaml_serde::Value { yaml_serde::Value::Mapping(yaml_serde::Mapping::from_iter([( yaml_serde::Value::String(keyword.to_owned()), yaml_serde::Value::Mapping(predicate.clone()), )])) } /// Adds the path constraint to a filter predicate. /// /// An `Exact` match uses the Praxis `path` field and a `PathPrefix` /// match uses `path_prefix`. Collapsing both onto `path_prefix`, as this /// did before, made a filter scoped to exactly `/foo` fire on `/foo/bar` /// as well. fn insert_path_predicate(rule_match: &HttpRouteRulesMatches, predicate: &mut yaml_serde::Mapping) { let Some(path) = rule_match.path.as_ref() else { return }; let Some(value) = path.value.as_deref() else { return }; let field = match &path.r#type { Some(HttpRouteRulesMatchesPathType::Exact) => "path", Some(HttpRouteRulesMatchesPathType::PathPrefix) => "path_prefix", _ => return, }; predicate.insert( yaml_serde::Value::String(field.to_owned()), yaml_serde::Value::String(value.to_owned()), ); } /// Adds the rule's header constraints to a filter predicate. /// /// Narrows the filter to the traffic its own rule matches. Without it a /// header modifier written for one route also fires for any other route /// sharing its path on the same listener. fn insert_header_predicate(rule_match: &HttpRouteRulesMatches, predicate: &mut yaml_serde::Mapping) { let Some(headers) = rule_match.headers.as_deref().filter(|hs| !hs.is_empty()) else { return; }; let mut mapping = yaml_serde::Mapping::new(); for header in headers { mapping.insert( yaml_serde::Value::String(header.name.clone()), yaml_serde::Value::String(header.value.clone()), ); } predicate.insert( yaml_serde::Value::String("headers".to_owned()), yaml_serde::Value::Mapping(mapping), ); } /// Dispatches a request header modifier filter. fn dispatch_request_header(filter: &HttpRouteRulesFilters, config: &mut HeaderFilterConfig) -> bool { filter .request_header_modifier .as_ref() .is_some_and(|modifier| process_request_header_modifier(modifier, config)) } /// Dispatches a response header modifier filter. fn dispatch_response_header(filter: &HttpRouteRulesFilters, config: &mut HeaderFilterConfig) -> bool { filter .response_header_modifier .as_ref() .is_some_and(|modifier| process_response_header_modifier(modifier, config)) } // ----------------------------------------------------------------------------- // Utility Functions // ----------------------------------------------------------------------------- /// Processes a request header modifier into the accumulated header config. /// /// Maps Gateway API `add` to `request_add`, `set` to `request_set`, /// and `remove` to `request_remove`. /// /// Returns `true` if any modifications were applied. fn process_request_header_modifier( modifier: &HttpRouteRulesFiltersRequestHeaderModifier, config: &mut HeaderFilterConfig, ) -> bool { let mut modified = false; modified |= collect_request_add(modifier.add.as_ref(), config); modified |= collect_request_set(modifier.set.as_ref(), config); modified |= collect_request_remove(modifier.remove.as_ref(), config); modified } /// Collects `add` headers into `request_add`. fn collect_request_add( add: Option<&Vec>, config: &mut HeaderFilterConfig, ) -> bool { let Some(headers) = add else { return false }; let entries = to_header_entries(headers.iter().map(|hdr| (&hdr.name, &hdr.value))); config.request_add.get_or_insert_with(Vec::new).extend(entries); true } /// Maps `set` headers to `request_set` (overwrite semantics). fn collect_request_set( set: Option<&Vec>, config: &mut HeaderFilterConfig, ) -> bool { let Some(headers) = set else { return false }; let entries = to_header_entries(headers.iter().map(|hdr| (&hdr.name, &hdr.value))); config.request_set.get_or_insert_with(Vec::new).extend(entries); true } /// Maps `remove` headers to `request_remove`. fn collect_request_remove(remove: Option<&Vec>, config: &mut HeaderFilterConfig) -> bool { let Some(headers) = remove else { return false }; config .request_remove .get_or_insert_with(Vec::new) .extend(headers.iter().cloned()); true } /// Processes a response header modifier into the accumulated header config. /// /// Returns `true` if any modifications were applied. fn process_response_header_modifier( modifier: &HttpRouteRulesFiltersResponseHeaderModifier, config: &mut HeaderFilterConfig, ) -> bool { let mut modified = false; if let Some(add_headers) = &modifier.add { let entries = to_header_entries(add_headers.iter().map(|hdr| (&hdr.name, &hdr.value))); config.response_add.get_or_insert_with(Vec::new).extend(entries); modified = true; } if let Some(set_headers) = &modifier.set { let entries = to_header_entries(set_headers.iter().map(|hdr| (&hdr.name, &hdr.value))); config.response_set.get_or_insert_with(Vec::new).extend(entries); modified = true; } if let Some(remove_headers) = &modifier.remove { config .response_remove .get_or_insert_with(Vec::new) .extend(remove_headers.iter().cloned()); modified = true; } modified } /// Converts name-value pairs into [`HeaderEntry`] values. fn to_header_entries<'iter>(pairs: impl Iterator) -> Vec { pairs .map(|(name, value)| HeaderEntry { name: name.clone(), value: value.clone(), }) .collect() } /// Emits a conditional redirect filter entry. /// /// Builds a Praxis `location` URL template from Gateway API redirect /// fields (scheme, hostname, port) with `${path}${query}` placeholders. fn emit_conditional_redirect( redirect: &gateway_api::httproutes::HttpRouteRulesFiltersRequestRedirect, condition: Option<&yaml_serde::Value>, filters: &mut Vec, ) { let location = build_redirect_location(redirect); let status = redirect_status(redirect.status_code); let redirect_config = RedirectFilterConfig { status, location }; match yaml_serde::to_value(&redirect_config) { Ok(config) => { let config = inject_conditions(config, condition); filters.push(PraxisFilterEntry { filter: "redirect".to_owned(), config, }); }, Err(err) => warn!(%err, "failed to serialize redirect filter config"), } } /// Maps a Gateway API redirect status onto one Praxis accepts. /// /// Praxis deserializes this field through a `TryFrom` limited to /// 301, 302, 307 and 308, and its filter config denies unknown values, /// so an out-of-range status does not degrade one redirect — it fails /// the whole document and leaves that Gateway's data plane without a /// config. Anything unrecognised falls back to the Gateway API default /// rather than being passed through. fn redirect_status(status_code: Option) -> u16 { match status_code { Some(301) => 301, Some(307) => 307, Some(308) => 308, Some(302) | None => DEFAULT_REDIRECT_STATUS, Some(other) => { warn!(status = other, "unsupported redirect status, falling back to 302"); DEFAULT_REDIRECT_STATUS }, } } /// Builds a redirect location URL template from Gateway API fields. fn build_redirect_location(redirect: &gateway_api::httproutes::HttpRouteRulesFiltersRequestRedirect) -> String { let scheme = redirect.scheme.as_ref().map(|sc| match sc { HttpRouteRulesFiltersRequestRedirectScheme::Http => "http", HttpRouteRulesFiltersRequestRedirectScheme::Https => "https", }); let hostname = redirect.hostname.as_deref().unwrap_or("${host}"); match (scheme, redirect.port) { (Some(sc), Some(port)) => format!("{sc}://{hostname}:{port}${{path}}${{query}}"), (Some(sc), None) => format!("{sc}://{hostname}${{path}}${{query}}"), (None, Some(port)) => format!("${{scheme}}://{hostname}:{port}${{path}}${{query}}"), (None, None) => format!("${{scheme}}://{hostname}${{path}}${{query}}"), } } // ----------------------------------------------------------------------------- // Timeouts // ----------------------------------------------------------------------------- /// Emits a conditional `timeout` filter for a rule's timeouts. /// /// A rule may set `request`, `backendRequest`, or both. Praxis has one /// timeout to give, so the shorter of the two is what gets enforced — /// which is also the one that would fire first if both existed. A /// timeout of `0s` disables it, and a rule setting nothing gets no /// filter. fn emit_conditional_timeout( rule: &HttpRouteRules, condition: Option<&yaml_serde::Value>, filters: &mut Vec, ) { let Some(timeouts) = &rule.timeouts else { return }; let request = timeouts.request.as_deref().and_then(parse_duration_ms); let backend = timeouts.backend_request.as_deref().and_then(parse_duration_ms); let Some(timeout_ms) = [request, backend].into_iter().flatten().filter(|ms| *ms > 0).min() else { return; }; let mut config = yaml_serde::Mapping::new(); config.insert( yaml_serde::Value::String("timeout_ms".to_owned()), yaml_serde::Value::Number(timeout_ms.into()), ); let config = inject_conditions(yaml_serde::Value::Mapping(config), condition); filters.push(PraxisFilterEntry { filter: "timeout".to_owned(), config, }); } /// Parses a Gateway API duration into whole milliseconds. /// /// [GEP-2257] spells a duration as a run of `` pairs with /// units `h`, `m`, `s` and `ms` — `500ms`, `1s`, `1h30m`. The CRD /// enforces the grammar with a pattern, so a value that fails to parse /// here is one the API server should never have admitted; it is /// dropped rather than guessed at, leaving the rule without a timeout. /// /// [GEP-2257]: https://gateway-api.sigs.k8s.io/geps/gep-2257/ fn parse_duration_ms(value: &str) -> Option { let mut total: u64 = 0; let mut rest = value; while !rest.is_empty() { let digits = rest.find(|ch: char| !ch.is_ascii_digit())?; if digits == 0 { return None; } let (number, tail) = rest.split_at(digits); let (unit_ms, unit_len) = unit_millis(tail)?; total = total.checked_add(number.parse::().ok()?.checked_mul(unit_ms)?)?; rest = tail.get(unit_len..)?; } (value != rest).then_some(total) } /// Returns the millisecond value of the unit starting `tail`, with its /// length. /// /// `ms` is tested before `m`, or every millisecond would be read as a /// minute followed by a stray `s`. fn unit_millis(tail: &str) -> Option<(u64, usize)> { if tail.starts_with("ms") { Some((1, 2)) } else if tail.starts_with('s') { Some((1_000, 1)) } else if tail.starts_with('m') { Some((60_000, 1)) } else if tail.starts_with('h') { Some((3_600_000, 1)) } else { None } } // ----------------------------------------------------------------------------- // Path Rewrite // ----------------------------------------------------------------------------- /// Emits a conditional `path_rewrite` filter for a `URLRewrite` path. /// /// Every entry carries `allow_rewrite_override`. Praxis rejects a chain /// holding more than one rewrite filter unless the later ones opt in, /// and a Gateway whose routes rewrite two different prefixes produces /// exactly that — even though the `conditions` make the two mutually /// exclusive at request time. Setting the flag on all of them beats /// tracking which one happens to be emitted first; it is inert on the /// first entry, which the check never inspects. fn emit_conditional_path_rewrite( path: &HttpRouteRulesFiltersUrlRewritePath, ctx: &RuleContext<'_>, filters: &mut Vec, ) { let Some(mut config) = path_rewrite_config(path, ctx.prefix) else { return; }; config.insert( yaml_serde::Value::String("allow_rewrite_override".to_owned()), yaml_serde::Value::Bool(true), ); let config = inject_conditions(yaml_serde::Value::Mapping(config), ctx.condition); filters.push(PraxisFilterEntry { filter: "path_rewrite".to_owned(), config, }); } /// Builds the `path_rewrite` config for one `URLRewrite` path modifier. /// /// Returns `None` when the rewrite names no replacement, or when a /// `ReplacePrefixMatch` reaches here on a rule with no prefix to /// replace. [`validate_route`] rejects that rule before the config is /// generated, so this is the belt to that braces. /// /// [`validate_route`]: crate::gateway_api::route_validation::validate_route fn path_rewrite_config( path: &HttpRouteRulesFiltersUrlRewritePath, prefix: Option<&str>, ) -> Option { match path.r#type { HttpRouteRulesFiltersUrlRewritePathType::ReplaceFullPath => { Some(replace_full_path(path.replace_full_path.as_deref()?)) }, HttpRouteRulesFiltersUrlRewritePathType::ReplacePrefixMatch => { let Some(prefix) = prefix else { warn!("ReplacePrefixMatch on a rule with no PathPrefix match, ignoring"); return None; }; Some(replace_prefix_match(prefix, path.replace_prefix_match.as_deref()?)) }, } } /// Builds the config that replaces the whole request path. /// /// Praxis offers no "set the path" operation, so this is a regex /// replace of everything. The query string is not part of what the /// pattern sees; `path_rewrite` re-attaches it afterwards. fn replace_full_path(replacement: &str) -> yaml_serde::Mapping { replace_operation("^.*$", &escape_replacement(replacement)) } /// Builds the config that replaces the prefix the rule matched on. /// /// Gateway API replaces whole path segments: `/prefix/one/two` under a /// `/prefix/one` match and a `/one` replacement becomes `/one/two`, and /// the bare `/prefix/one` becomes `/one`. A trailing slash is /// insignificant on both sides. /// /// Replacing with `/` is the one case a single regex cannot cover, /// because the remainder is empty for the bare prefix and a /// replacement of `${1}` would leave an empty path. Praxis's /// `strip_prefix` has precisely the Gateway API semantics there, /// including yielding `/` for that case, so it handles it instead. fn replace_prefix_match(prefix: &str, replacement: &str) -> yaml_serde::Mapping { let prefix = prefix.trim_end_matches('/'); let replacement = replacement.trim_end_matches('/'); if replacement.is_empty() { let mut config = yaml_serde::Mapping::new(); config.insert( yaml_serde::Value::String("strip_prefix".to_owned()), yaml_serde::Value::String(prefix.to_owned()), ); return config; } let pattern = format!("^{}(/.*)?$", escape_pattern(prefix)); let replacement = format!("{}${{1}}", escape_replacement(replacement)); replace_operation(&pattern, &replacement) } /// Builds a `path_rewrite` `replace` operation. fn replace_operation(pattern: &str, replacement: &str) -> yaml_serde::Mapping { let mut replace = yaml_serde::Mapping::new(); replace.insert( yaml_serde::Value::String("pattern".to_owned()), yaml_serde::Value::String(pattern.to_owned()), ); replace.insert( yaml_serde::Value::String("replacement".to_owned()), yaml_serde::Value::String(replacement.to_owned()), ); let mut config = yaml_serde::Mapping::new(); config.insert( yaml_serde::Value::String("replace".to_owned()), yaml_serde::Value::Mapping(replace), ); config } /// Escapes a literal so a regular expression matches it verbatim. /// /// A path prefix routinely contains `.` and `-`, and an unescaped `.` /// in the pattern matches any character — `/v1.api` would rewrite /// `/v1Xapi` as well. Only the characters the `regex` crate treats as /// meta are escaped; backslash-escaping an arbitrary character is an /// error there, not a no-op. fn escape_pattern(literal: &str) -> String { /// Characters `regex::is_meta_character` reports, and the only ones /// that crate accepts a backslash in front of. const META: &[char] = &[ '\\', '.', '+', '*', '?', '(', ')', '|', '[', ']', '{', '}', '^', '$', '#', '&', '-', '~', ]; let mut escaped = String::with_capacity(literal.len()); for ch in literal.chars() { if META.contains(&ch) { escaped.push('\\'); } escaped.push(ch); } escaped } /// Escapes a replacement string so every `$` in it stays literal. /// /// The `regex` crate reads `$1` and `$name` in a replacement as capture /// group references, and a rewrite target is a path the route author /// chose, which may contain a dollar sign. fn escape_replacement(literal: &str) -> String { literal.replace('$', "$$") } /// Emits a conditional header filter entry. fn emit_conditional_header_filter( config: &HeaderFilterConfig, condition: Option<&yaml_serde::Value>, filters: &mut Vec, ) { match yaml_serde::to_value(config) { Ok(config) => { let config = inject_conditions(config, condition); filters.push(PraxisFilterEntry { filter: "headers".to_owned(), config, }); }, Err(err) => warn!(%err, "failed to serialize header filter config"), } } /// Emits a `static_response` filter returning 500 for rules with no backends. fn emit_no_backend_response(condition: Option<&yaml_serde::Value>, filters: &mut Vec) { let mut config = yaml_serde::Mapping::new(); config.insert( yaml_serde::Value::String("status".to_owned()), yaml_serde::Value::Number(500.into()), ); config.insert( yaml_serde::Value::String("body".to_owned()), yaml_serde::Value::String("no backends available".to_owned()), ); let config = inject_conditions(yaml_serde::Value::Mapping(config), condition); filters.push(PraxisFilterEntry { filter: "static_response".to_owned(), config, }); } /// Injects `conditions` into a filter config mapping. fn inject_conditions(mut config: yaml_serde::Value, condition: Option<&yaml_serde::Value>) -> yaml_serde::Value { if let (Some(cond), Some(map)) = (condition, config.as_mapping_mut()) { map.insert(yaml_serde::Value::String("conditions".to_owned()), cond.clone()); } config } // ----------------------------------------------------------------------------- // Tests // ----------------------------------------------------------------------------- #[cfg(test)] #[expect(clippy::too_many_lines, reason = "tests")] mod tests { use gateway_api::httproutes::{HttpRouteRulesBackendRefs, HttpRouteRulesTimeouts}; use super::*; fn dummy_backend_refs() -> Vec { vec![HttpRouteRulesBackendRefs { name: "dummy".to_owned(), port: Some(80), ..Default::default() }] } #[test] fn test_convert_filters_request_header_modifier() { use gateway_api::httproutes::{ HttpRouteRulesFilters, HttpRouteRulesFiltersRequestHeaderModifier, HttpRouteRulesFiltersRequestHeaderModifierAdd, HttpRouteRulesFiltersRequestHeaderModifierSet, }; let rules = vec![HttpRouteRules { backend_refs: Some(dummy_backend_refs()), filters: Some(vec![HttpRouteRulesFilters { r#type: HttpRouteRulesFiltersType::RequestHeaderModifier, request_header_modifier: Some(HttpRouteRulesFiltersRequestHeaderModifier { add: Some(vec![HttpRouteRulesFiltersRequestHeaderModifierAdd { name: "X-Custom".to_owned(), value: "custom-value".to_owned(), }]), set: Some(vec![HttpRouteRulesFiltersRequestHeaderModifierSet { name: "X-Override".to_owned(), value: "override-value".to_owned(), }]), remove: Some(vec!["X-Remove".to_owned()]), }), ..Default::default() }]), ..Default::default() }]; let filters = convert_filters(&served(&rules)); let transforming = &filters.transforming; assert_eq!(transforming.len(), 1, "should produce one header filter"); assert_eq!(transforming[0].filter, "headers", "filter name should be headers"); assert!( filters.terminating.is_empty(), "a header modifier does not answer the request, so nothing runs before the router" ); let config_str = yaml_serde::to_string(&transforming[0].config).unwrap(); assert!( config_str.contains("request_add"), "should have request_add for added headers" ); assert!(config_str.contains("X-Custom"), "should contain added header"); assert!( config_str.contains("request_set"), "set headers should map to request_set" ); assert!(config_str.contains("X-Override"), "should contain set header"); assert!( config_str.contains("request_remove"), "remove should map to request_remove" ); assert!(config_str.contains("X-Remove"), "should contain removed header name"); } #[test] fn test_convert_filters_response_header_modifier() { use gateway_api::httproutes::{ HttpRouteRulesFilters, HttpRouteRulesFiltersResponseHeaderModifier, HttpRouteRulesFiltersResponseHeaderModifierAdd, }; let rules = vec![HttpRouteRules { backend_refs: Some(dummy_backend_refs()), filters: Some(vec![HttpRouteRulesFilters { r#type: HttpRouteRulesFiltersType::ResponseHeaderModifier, response_header_modifier: Some(HttpRouteRulesFiltersResponseHeaderModifier { add: Some(vec![HttpRouteRulesFiltersResponseHeaderModifierAdd { name: "X-Response".to_owned(), value: "response-value".to_owned(), }]), remove: Some(vec!["X-Remove-Response".to_owned()]), ..Default::default() }), ..Default::default() }]), ..Default::default() }]; let filters = convert_filters(&served(&rules)); assert_eq!(filters.transforming.len(), 1, "should produce one header filter"); let config_str = yaml_serde::to_string(&filters.transforming[0].config).unwrap(); assert!(config_str.contains("X-Response"), "should contain response header"); assert!( config_str.contains("X-Remove-Response"), "should contain removed response header" ); } #[test] fn test_convert_filters_request_redirect() { use gateway_api::httproutes::{HttpRouteRulesFilters, HttpRouteRulesFiltersRequestRedirect}; let rules = vec![HttpRouteRules { backend_refs: Some(dummy_backend_refs()), filters: Some(vec![HttpRouteRulesFilters { r#type: HttpRouteRulesFiltersType::RequestRedirect, request_redirect: Some(HttpRouteRulesFiltersRequestRedirect { status_code: Some(302), scheme: Some(HttpRouteRulesFiltersRequestRedirectScheme::Https), hostname: Some("example.com".to_owned()), port: Some(443), ..Default::default() }), ..Default::default() }]), ..Default::default() }]; let filters = convert_filters(&served(&rules)); let terminating = &filters.terminating; assert_eq!(terminating.len(), 1, "should produce one redirect filter"); assert_eq!(terminating[0].filter, "redirect", "filter name should be redirect"); assert!( filters.transforming.is_empty(), "a redirect answers the request itself and never reaches an upstream" ); let config_str = yaml_serde::to_string(&terminating[0].config).unwrap(); assert!(config_str.contains("302"), "should contain status code"); assert!( config_str.contains("https://example.com:443"), "location should contain scheme, hostname, and port" ); } #[test] fn test_convert_filters_mixed() { use gateway_api::httproutes::{ HttpRouteRulesFilters, HttpRouteRulesFiltersRequestHeaderModifier, HttpRouteRulesFiltersRequestHeaderModifierAdd, HttpRouteRulesFiltersRequestRedirect, }; let rules = vec![HttpRouteRules { backend_refs: Some(dummy_backend_refs()), filters: Some(vec![ HttpRouteRulesFilters { r#type: HttpRouteRulesFiltersType::RequestHeaderModifier, request_header_modifier: Some(HttpRouteRulesFiltersRequestHeaderModifier { add: Some(vec![HttpRouteRulesFiltersRequestHeaderModifierAdd { name: "X-Header".to_owned(), value: "value".to_owned(), }]), ..Default::default() }), ..Default::default() }, HttpRouteRulesFilters { r#type: HttpRouteRulesFiltersType::RequestRedirect, request_redirect: Some(HttpRouteRulesFiltersRequestRedirect { status_code: Some(301), scheme: Some(HttpRouteRulesFiltersRequestRedirectScheme::Https), hostname: Some("example.com".to_owned()), ..Default::default() }), ..Default::default() }, ]), ..Default::default() }]; let filters = convert_filters(&served(&rules)); assert!( filters.terminating.iter().any(|f| f.filter == "redirect"), "the redirect answers the request, so it runs before the router" ); assert!( filters.transforming.iter().any(|f| f.filter == "headers"), "the header modifier reshapes a routed request, so it runs after the router" ); } #[test] fn test_convert_filters_per_rule_conditions() { use gateway_api::httproutes::{ HttpRouteRulesFilters, HttpRouteRulesFiltersRequestHeaderModifier, HttpRouteRulesFiltersRequestHeaderModifierAdd, HttpRouteRulesMatches, HttpRouteRulesMatchesPath, }; let rules = vec![ HttpRouteRules { backend_refs: Some(dummy_backend_refs()), matches: Some(vec![HttpRouteRulesMatches { path: Some(HttpRouteRulesMatchesPath { r#type: Some(HttpRouteRulesMatchesPathType::PathPrefix), value: Some("/set".to_owned()), }), ..Default::default() }]), filters: Some(vec![HttpRouteRulesFilters { r#type: HttpRouteRulesFiltersType::RequestHeaderModifier, request_header_modifier: Some(HttpRouteRulesFiltersRequestHeaderModifier { add: Some(vec![HttpRouteRulesFiltersRequestHeaderModifierAdd { name: "X-First".to_owned(), value: "first".to_owned(), }]), ..Default::default() }), ..Default::default() }]), ..Default::default() }, HttpRouteRules { backend_refs: Some(dummy_backend_refs()), matches: Some(vec![HttpRouteRulesMatches { path: Some(HttpRouteRulesMatchesPath { r#type: Some(HttpRouteRulesMatchesPathType::PathPrefix), value: Some("/add".to_owned()), }), ..Default::default() }]), filters: Some(vec![HttpRouteRulesFilters { r#type: HttpRouteRulesFiltersType::RequestHeaderModifier, request_header_modifier: Some(HttpRouteRulesFiltersRequestHeaderModifier { add: Some(vec![HttpRouteRulesFiltersRequestHeaderModifierAdd { name: "X-Second".to_owned(), value: "second".to_owned(), }]), ..Default::default() }), ..Default::default() }]), ..Default::default() }, ]; let filters = convert_filters(&served(&rules)); let transforming = &filters.transforming; assert_eq!(transforming.len(), 2, "should produce one filter per rule"); let first_yaml = yaml_serde::to_string(&transforming[0].config).unwrap(); assert!( first_yaml.contains("X-First"), "first filter should have X-First header" ); assert!( first_yaml.contains("path_prefix"), "first filter should have path_prefix condition" ); assert!( first_yaml.contains("/set"), "first filter should be conditioned on /set" ); let second_yaml = yaml_serde::to_string(&transforming[1].config).unwrap(); assert!( second_yaml.contains("X-Second"), "second filter should have X-Second header" ); assert!( second_yaml.contains("/add"), "second filter should be conditioned on /add" ); } // ----------------------------------------------------------------------- // Timeouts // ----------------------------------------------------------------------- #[test] fn test_gateway_api_durations_parse_to_milliseconds() { for (text, expected) in [ ("500ms", 500_u64), ("1s", 1_000), ("0s", 0), ("1m", 60_000), ("1h", 3_600_000), ("1h30m", 5_400_000), ("1m30s500ms", 90_500), ] { assert_eq!( parse_duration_ms(text), Some(expected), "GEP-2257 spells {text} as {expected}ms" ); } } #[test] fn test_a_bare_number_is_not_a_duration() { for text in ["500", "", "ms", "1x", "1s500"] { assert_eq!( parse_duration_ms(text), None, "{text} is outside the GEP-2257 grammar and must not be guessed at" ); } } #[test] fn test_the_shorter_timeout_is_the_one_enforced() { let mut rule = rule_with_path(HttpRouteRulesMatchesPathType::PathPrefix, "/slow"); rule.timeouts = Some(HttpRouteRulesTimeouts { backend_request: Some("500ms".to_owned()), request: Some("2s".to_owned()), }); let mut filters = Vec::new(); emit_conditional_timeout(&rule, None, &mut filters); assert_eq!( filters.first().and_then(|f| f.config.get("timeout_ms")), Some(&yaml_serde::Value::Number(500.into())), "Praxis has one timeout to give, and the shorter one is what would fire first anyway" ); } #[test] fn test_a_zero_timeout_disables_the_filter() { let mut rule = rule_with_path(HttpRouteRulesMatchesPathType::PathPrefix, "/slow"); rule.timeouts = Some(HttpRouteRulesTimeouts { backend_request: None, request: Some("0s".to_owned()), }); let mut filters = Vec::new(); emit_conditional_timeout(&rule, None, &mut filters); assert!( filters.is_empty(), "the Gateway API reads a zero duration as no timeout, not an instant one" ); } // ----------------------------------------------------------------------- // Unresolvable Backends // ----------------------------------------------------------------------- #[test] fn test_a_rule_whose_backends_were_all_refused_answers_500() { let mut rule = rule_with_path(HttpRouteRulesMatchesPathType::PathPrefix, "/v2"); rule.backend_refs = Some(dummy_backend_refs()); let rules = [ServedRule { rule: &rule, resolvable: false, }]; let filters = convert_filters(&rules); assert_eq!( filters.terminating.first().map(|f| f.filter.as_str()), Some("static_response"), "a backendRef refused for want of a ReferenceGrant leaves no route, and the router \ would answer 404 where the Gateway API prescribes 500" ); } #[test] fn test_a_rule_with_a_resolved_backend_adds_no_static_response() { let mut rule = rule_with_path(HttpRouteRulesMatchesPathType::PathPrefix, "/v2"); rule.backend_refs = Some(dummy_backend_refs()); let rules = [ServedRule { rule: &rule, resolvable: true, }]; assert!( convert_filters(&rules).terminating.is_empty(), "the router serves this rule, so nothing may answer ahead of it" ); } // ----------------------------------------------------------------------- // URL Rewrite // ----------------------------------------------------------------------- #[test] fn test_replace_prefix_match_keeps_the_remainder() { let config = replace_prefix_match("/prefix/one", "/one"); let replace = config .get("replace") .expect("a non-empty replacement is a regex replace"); assert_eq!( replace["pattern"], yaml_serde::Value::String("^/prefix/one(/.*)?$".to_owned()), "the group has to be optional so the bare prefix rewrites too" ); assert_eq!( replace["replacement"], yaml_serde::Value::String("/one${1}".to_owned()), "whatever followed the prefix is carried across unchanged" ); } #[test] fn test_replace_prefix_match_ignores_trailing_slashes() { let with_slashes = replace_prefix_match("/prefix/", "/one/"); let without = replace_prefix_match("/prefix", "/one"); assert_eq!( with_slashes, without, "Gateway API treats a trailing slash as insignificant on both the match and the \ replacement, and keeping one would emit //" ); } #[test] fn test_replace_prefix_match_with_a_bare_slash_strips() { let config = replace_prefix_match("/strip-prefix", "/"); assert_eq!( config.get("strip_prefix"), Some(&yaml_serde::Value::String("/strip-prefix".to_owned())), "a regex replacement of ${{1}} would leave the bare prefix with an empty path, which \ Praxis rejects; strip_prefix yields / for that case and matches the spec elsewhere" ); } #[test] fn test_replace_full_path_discards_everything() { let config = replace_full_path("/one"); let replace = config.get("replace").expect("a full-path rewrite is a regex replace"); assert_eq!( replace["pattern"], yaml_serde::Value::String("^.*$".to_owned()), "the whole path goes, however long" ); assert_eq!( replace["replacement"], yaml_serde::Value::String("/one".to_owned()), "the replacement is the new path verbatim" ); } #[test] fn test_a_prefix_is_matched_literally() { let config = replace_prefix_match("/v1.api", "/v2"); let replace = config .get("replace") .expect("a non-empty replacement is a regex replace"); assert_eq!( replace["pattern"], yaml_serde::Value::String("^/v1\\.api(/.*)?$".to_owned()), "an unescaped dot matches any character, so /v1Xapi would be rewritten too" ); } #[test] fn test_a_dollar_in_a_replacement_stays_literal() { let config = replace_full_path("/cost/$total"); let replace = config.get("replace").expect("a full-path rewrite is a regex replace"); assert_eq!( replace["replacement"], yaml_serde::Value::String("/cost/$$total".to_owned()), "the regex crate reads $total as a capture group reference and would substitute nothing" ); } #[test] fn test_host_rewrite_sets_the_host_header_after_the_router() { let rules = vec![rule_with_rewrite( HttpRouteRulesMatchesPathType::PathPrefix, "/one", HttpRouteRulesFiltersUrlRewrite { hostname: Some("one.example.org".to_owned()), path: None, }, )]; let filters = convert_filters(&served(&rules)); let yaml = yaml_serde::to_string(&filters.transforming[0].config).unwrap(); assert_eq!( filters.transforming[0].filter, "headers", "a host rewrite is a header set" ); assert!( yaml.contains("request_set") && yaml.contains("one.example.org"), "the rewritten hostname has to reach the upstream as the Host header: {yaml}" ); assert!( filters.terminating.is_empty(), "setting Host before the router would change which route matched" ); } #[test] fn test_path_rewrite_allows_overriding_a_sibling_rewrite() { let rules = vec![ rule_with_rewrite( HttpRouteRulesMatchesPathType::PathPrefix, "/one", rewrite_to_prefix("/first"), ), rule_with_rewrite( HttpRouteRulesMatchesPathType::PathPrefix, "/two", rewrite_to_prefix("/second"), ), ]; let filters = convert_filters(&served(&rules)); assert_eq!(filters.transforming.len(), 2, "each rule rewrites its own traffic"); for entry in &filters.transforming { assert_eq!( entry.config.get("allow_rewrite_override"), Some(&yaml_serde::Value::Bool(true)), "Praxis refuses a chain with two rewrite filters unless the later ones opt in, \ even when their conditions make them mutually exclusive" ); } } // ----------------------------------------------------------------------- // Condition Scoping // ----------------------------------------------------------------------- #[test] fn test_exact_path_scopes_on_path_not_prefix() { let rule = rule_with_path(HttpRouteRulesMatchesPathType::Exact, "/foo"); let cond = lone_rule_condition(&rule).expect("an exact path should produce a condition"); let when = &cond[0]["when"]; assert_eq!( when["path"], yaml_serde::Value::String("/foo".to_owned()), "an Exact match must scope on the Praxis path field" ); assert!( when.get("path_prefix").is_none(), "using path_prefix for an Exact match would fire the filter on /foo/bar too" ); } #[test] fn test_prefix_path_scopes_on_path_prefix() { let rule = rule_with_path(HttpRouteRulesMatchesPathType::PathPrefix, "/api"); let cond = lone_rule_condition(&rule).expect("a prefix path should produce a condition"); assert_eq!( cond[0]["when"]["path_prefix"], yaml_serde::Value::String("/api".to_owned()), "a PathPrefix match must scope on path_prefix" ); } #[test] fn test_header_constraints_narrow_the_condition() { let mut rule = rule_with_path(HttpRouteRulesMatchesPathType::PathPrefix, "/api"); if let Some(matches) = rule.matches.as_mut() && let Some(first) = matches.first_mut() { first.headers = Some(vec![gateway_api::httproutes::HttpRouteRulesMatchesHeaders { name: "x-tenant".to_owned(), value: "acme".to_owned(), r#type: None, }]); } let cond = lone_rule_condition(&rule).expect("condition expected"); assert_eq!( cond[0]["when"]["headers"]["x-tenant"], yaml_serde::Value::String("acme".to_owned()), "a rule's header match must scope its filters, or the filter fires for other routes \ sharing the same path on this listener" ); } #[test] fn test_header_only_rule_still_produces_a_condition() { let rule = HttpRouteRules { matches: Some(vec![HttpRouteRulesMatches { headers: Some(vec![gateway_api::httproutes::HttpRouteRulesMatchesHeaders { name: "x-canary".to_owned(), value: "true".to_owned(), r#type: None, }]), ..Default::default() }]), ..Default::default() }; let cond = lone_rule_condition(&rule).expect("a header-only rule should still be scoped"); assert!( cond[0]["when"].get("headers").is_some(), "a rule constrained only by headers must not produce an unscoped filter" ); } #[test] fn test_unconstrained_rule_has_no_condition() { let rule = HttpRouteRules { matches: Some(vec![HttpRouteRulesMatches::default()]), ..Default::default() }; assert!( lone_rule_condition(&rule).is_none(), "a rule with nothing to match on cannot be scoped and must stay unconditional" ); } #[test] fn test_catch_all_rule_is_scoped_away_from_its_siblings() { let rules = vec![ rule_with_path(HttpRouteRulesMatchesPathType::PathPrefix, "/one"), HttpRouteRules::default(), ]; let scoped = scope_rules(&served(&rules)); let cond = rule_conditions(1, &scoped).expect("a catch-all rule beside a narrower one must be scoped"); assert_eq!( cond[0]["unless"]["path_prefix"], yaml_serde::Value::String("/one".to_owned()), "Gateway API gives /one traffic to the narrower rule, so the catch-all rule's filters \ must skip it — without this the catch-all rewrites every request on the listener" ); assert!( cond[0].get("when").is_none(), "a catch-all rule claims no traffic of its own to gate on" ); } #[test] fn test_a_narrower_rule_is_not_scoped_against_a_broader_one() { let rules = vec![ rule_with_path(HttpRouteRulesMatchesPathType::PathPrefix, "/one"), rule_with_path(HttpRouteRulesMatchesPathType::PathPrefix, "/one/two"), ]; let scoped = scope_rules(&served(&rules)); let cond = rule_conditions(1, &scoped).expect("a rule with a path match is always scoped"); assert_eq!( cond.as_sequence().map(Vec::len), Some(1), "the longer prefix wins the overlap, so it needs no unless clause against the shorter one" ); } #[test] fn test_an_exact_match_outranks_a_longer_prefix() { let rules = vec![ rule_with_path(HttpRouteRulesMatchesPathType::PathPrefix, "/one/two/three"), rule_with_path(HttpRouteRulesMatchesPathType::Exact, "/one"), ]; let scoped = scope_rules(&served(&rules)); let cond = rule_conditions(0, &scoped).expect("a rule with a path match is always scoped"); assert_eq!( cond[1]["unless"]["path"], yaml_serde::Value::String("/one".to_owned()), "Gateway API ranks an exact match above any prefix, however long" ); } #[test] fn test_identical_predicates_are_listed_once() { let rules = vec![ HttpRouteRules::default(), rule_with_path(HttpRouteRulesMatchesPathType::PathPrefix, "/one"), rule_with_path(HttpRouteRulesMatchesPathType::PathPrefix, "/one"), ]; let scoped = scope_rules(&served(&rules)); let cond = rule_conditions(0, &scoped).expect("a catch-all rule beside narrower ones must be scoped"); assert_eq!( cond.as_sequence().map(Vec::len), Some(1), "two siblings claiming the same traffic need one unless clause, not two" ); } // ----------------------------------------------------------------------- // Test Utilities // ----------------------------------------------------------------------- /// Builds a `ReplacePrefixMatch` rewrite to the given prefix. fn rewrite_to_prefix(replacement: &str) -> HttpRouteRulesFiltersUrlRewrite { HttpRouteRulesFiltersUrlRewrite { hostname: None, path: Some(HttpRouteRulesFiltersUrlRewritePath { r#type: HttpRouteRulesFiltersUrlRewritePathType::ReplacePrefixMatch, replace_full_path: None, replace_prefix_match: Some(replacement.to_owned()), }), } } /// Builds a rule with one path match and one `URLRewrite` filter. fn rule_with_rewrite( kind: HttpRouteRulesMatchesPathType, value: &str, rewrite: HttpRouteRulesFiltersUrlRewrite, ) -> HttpRouteRules { let mut rule = rule_with_path(kind, value); rule.backend_refs = Some(dummy_backend_refs()); rule.filters = Some(vec![HttpRouteRulesFilters { r#type: HttpRouteRulesFiltersType::UrlRewrite, url_rewrite: Some(rewrite), ..Default::default() }]); rule } /// Pairs rules with whether their backends resolved, as the /// controller does before converting them. fn served(rules: &[HttpRouteRules]) -> Vec> { rules .iter() .map(|rule| ServedRule { rule, resolvable: rule.backend_refs.as_ref().is_some_and(|refs| !refs.is_empty()), }) .collect() } /// Scopes a rule that has no siblings to be scoped against. fn lone_rule_condition(rule: &HttpRouteRules) -> Option { let rules = [rule.clone()]; rule_conditions(0, &scope_rules(&served(&rules))) } /// Builds a rule with a single path match of the given type. fn rule_with_path(kind: HttpRouteRulesMatchesPathType, value: &str) -> HttpRouteRules { HttpRouteRules { matches: Some(vec![HttpRouteRulesMatches { path: Some(gateway_api::httproutes::HttpRouteRulesMatchesPath { r#type: Some(kind), value: Some(value.to_owned()), }), ..Default::default() }]), ..Default::default() } } // ----------------------------------------------------------------------- // Redirect Status // ----------------------------------------------------------------------- #[test] fn test_supported_redirect_statuses_pass_through() { for code in [301_i64, 302, 307, 308] { assert_eq!( i64::from(redirect_status(Some(code))), code, "Praxis accepts {code} and it should reach the config unchanged" ); } } #[test] fn test_unsupported_redirect_status_falls_back() { for code in [200_i64, 303, 399, -1, 99999] { assert_eq!( redirect_status(Some(code)), DEFAULT_REDIRECT_STATUS, "Praxis denies unknown redirect statuses and rejects the whole document, so {code} \ must never be emitted" ); } } #[test] fn test_absent_redirect_status_uses_the_spec_default() { assert_eq!( redirect_status(None), 302, "the Gateway API default for an unspecified redirect status is 302" ); } }