litellm/tests/unit/gateway/routes/test_allowlist.py
mrinal-berri cd6ef91115
fix(helm): route every gateway allowlist path to the gateway in the componentized ingress (#50760)
* fix(helm): route every gateway allowlist path to the gateway in the componentized ingress

The ingress gateway prefix list had drifted from gateway/routes/allowlist.py, so requests for decisions, systemone, extraction, traces, logs, live sessions and several provider passthroughs fell into the backend catch-all, and /eu_jev/v1/systemone returned 404. The missing paths are added, the exact gateway paths move into a list, and a unit test now fails whenever the chart and the allowlist disagree in either direction

* fix(helm): keep extraPaths that repeat a built-in gateway path rendering on upgrade

Values written for an older chart add the newly built-in gateway prefixes through ingress.extraPaths with service gateway, which the duplicate check now rejected, so helm upgrade failed to render. Such an entry is skipped, and a built-in path pointed at another Service or port is still rejected. Also asserts that the new exact path and a new prefix render to the gateway Service

* revert(helm): drop the extraPaths skip for built-in gateway paths

Keeps this PR to the missing paths and the drift test. An extraPaths entry that repeats a now built-in gateway path is rejected at render time as before, and operators remove it on upgrade. The checks that the new paths render to the gateway Service stay

* fix(helm): skip an ingress.extraPaths entry identical to a built-in route

Values that added a path through extraPaths before the chart routed it stopped rendering once the path became built in, since the duplicate check rejected every repeat. A repeat with the same path, pathType, Service and port (by number or the http port name) now renders nothing, and a repeat that routes elsewhere is still rejected

* refactor(helm): limit the ingress fix to gateway-only routes and the drift test

Drops the extraPaths identical-route skip, so the duplicate check is the same as on main. /v1/traces and /v1/logs leave the gateway list and keep going to the backend, since the backend also serves them and their gateway entries date from the retired OTLP ingest. The drift test now requires only paths that the gateway alone keeps

* test(helm): require every gateway exact path the backend does not list exactly

A backend prefix such as /debug/ made /debug/memory/summary count as shared, so the drift test stopped requiring its Exact gateway rule. Exact paths now count as shared only when the backend lists the same exact path

* chore(helm): keep the gateway prefix comment as it is on main

* fix(helm): fit the componentized ingress under the default ALB rule quota

The AWS Load Balancer Controller makes one listener rule per Ingress path and an ALB allows 100 rules by default, so the 113 single-host paths would fail with TooManyRules. Gateway prefixes are now listed in pairs, and on alb each pair is one rule, its first prefix as the Ingress path with a use-annotation forward to the gateway and the second added through a conditions annotation. The default alb render drops to 62 rules for the same path patterns, and nginx, monolith and component-host renders keep one path per prefix

* fix(helm): order grouped ALB rules by their shorter prefix and keep gateway conditions

Each gateway prefix pair now lists its shorter prefix first and uses it as the Ingress path, so a longer extraPaths child of either prefix still sorts ahead of the grouped rule. Conditions an operator set for the gateway Service are carried onto every grouped rule, which uses a generated backend name. The grouped annotations and paths are written inline from the pair list
2026-11-13 15:06:44 -08:00

94 lines
4 KiB
Python

import re
from pathlib import Path
from typing import Final
import pytest
from backend.routes.allowlist import BACKEND_EXACT_PATHS, BACKEND_PATH_PREFIXES
from gateway.routes.allowlist import GATEWAY_EXACT_PATHS, GATEWAY_PATH_PREFIXES
_INGRESS_TEMPLATE: Final = (
Path(__file__).parents[4] / "litellm" / "deploy" / "helm" / "litellm" / "templates" / "ingress.yaml"
)
# A route template whose first segment is any provider name (/{provider}/v1/files). An Ingress Prefix cannot
# express it, so those requests reach the backend unless the provider has its own prefix above.
_PREFIXES_AN_INGRESS_CANNOT_ROUTE: Final = frozenset({"/{provider}/"})
def _template_block(name: str) -> str:
match: Final = re.search(rf"\${name} := list(.*?)-}}}}", _INGRESS_TEMPLATE.read_text(), re.S)
assert match is not None, f"${name} not found in {_INGRESS_TEMPLATE}"
return match.group(1)
def _quoted_paths(block: str) -> frozenset[str]:
return frozenset(re.findall(r'"(/[^"]*)"', block))
_CHART_PREFIX_GROUPS: Final = tuple(
tuple(re.findall(r'"(/[^"]*)"', group))
for group in re.findall(r"\(list ([^)]*)\)", _template_block("gatewayPrefixGroups"))
)
_CHART_PREFIXES: Final = _quoted_paths(_template_block("gatewayPrefixGroups"))
_CHART_EXACT_PATHS: Final = _quoted_paths(_template_block("gatewayExactPaths"))
def _backend_keeps(path: str) -> bool:
return path in BACKEND_EXACT_PATHS or any(path.startswith(prefix) for prefix in BACKEND_PATH_PREFIXES)
_GATEWAY_ONLY_PREFIXES: Final = sorted(
frozenset(prefix for prefix in GATEWAY_PATH_PREFIXES if not _backend_keeps(prefix))
- _PREFIXES_AN_INGRESS_CANNOT_ROUTE
)
_GATEWAY_ONLY_EXACT_PATHS: Final = sorted(GATEWAY_EXACT_PATHS - BACKEND_EXACT_PATHS)
def _routed_to_gateway_by_chart_prefix(path: str) -> bool:
trimmed: Final = path.rstrip("/")
return any(trimmed == prefix or trimmed.startswith(f"{prefix}/") for prefix in _CHART_PREFIXES)
def _gateway_serves_something_under(chart_prefix: str) -> bool:
under: Final = f"{chart_prefix}/"
by_prefix: Final = any(
chart_prefix.startswith(prefix) or prefix.rstrip("/") == chart_prefix or prefix.startswith(under)
for prefix in GATEWAY_PATH_PREFIXES
)
return by_prefix or any(path == chart_prefix or path.startswith(under) for path in GATEWAY_EXACT_PATHS)
@pytest.mark.parametrize("prefix", _GATEWAY_ONLY_PREFIXES)
def test_ingress_sends_every_gateway_only_prefix_to_the_gateway(prefix: str) -> None:
assert _routed_to_gateway_by_chart_prefix(prefix)
@pytest.mark.parametrize("path", _GATEWAY_ONLY_EXACT_PATHS)
def test_ingress_sends_every_gateway_only_exact_path_to_the_gateway(path: str) -> None:
assert path in _CHART_EXACT_PATHS or _routed_to_gateway_by_chart_prefix(path)
@pytest.mark.parametrize("chart_prefix", sorted(_CHART_PREFIXES))
def test_ingress_sends_no_prefix_to_the_gateway_that_the_gateway_dropped(chart_prefix: str) -> None:
assert _gateway_serves_something_under(chart_prefix)
@pytest.mark.parametrize("chart_path", sorted(_CHART_EXACT_PATHS))
def test_ingress_sends_no_exact_path_to_the_gateway_that_the_gateway_dropped(chart_path: str) -> None:
assert chart_path in GATEWAY_EXACT_PATHS
@pytest.mark.parametrize("group", _CHART_PREFIX_GROUPS, ids=lambda group: " ".join(group))
def test_each_gateway_prefix_group_fits_one_alb_rule(group: tuple[str, ...]) -> None:
assert 1 <= len(group) <= 2, f"{group} needs {2 * len(group)} path-pattern values, an ALB rule takes 5"
def test_gateway_prefix_groups_list_each_prefix_once() -> None:
flattened: Final = [prefix for group in _CHART_PREFIX_GROUPS for prefix in group]
assert len(flattened) == len(set(flattened)), sorted(p for p in set(flattened) if flattened.count(p) > 1)
@pytest.mark.parametrize("group", _CHART_PREFIX_GROUPS, ids=lambda group: " ".join(group))
def test_each_gateway_prefix_group_lists_its_shorter_prefix_first(group: tuple[str, ...]) -> None:
assert len(group[0]) <= len(group[-1]), f"{group} would order its ALB rule by the longer prefix"