Skip to content

The agent that grows with you

fix(webhook): refuse a coalesced route to an unserved profile at the edge

Tekniumcbe8b995f3d0
diff --git a/gateway/platforms/webhook_ingress.py b/gateway/platforms/webhook_ingress.py
index 602aa18e8f9..406d85557de 100644
--- a/gateway/platforms/webhook_ingress.py
+++ b/gateway/platforms/webhook_ingress.py
@@ -55,20 +55,25 @@ def _refuse_parked(runner, profile):
def parked_profile_refusal(runner, profile, route_name, route_config):
- """A retryable 503 for a webhook whose profile is not served right now, else None: a
- ``/p/<profile>/`` prefix that is parked or has no serving authority here, or a coalesced route
- that ``profile_routes`` sends to a parked profile (a coalesced delivery is acknowledged before
- admission applies that route). Answered before the delivery id is recorded: coalesced,
- ``deliver_only`` and ``cron_job`` routes act before (or without) an admission, so the in-memory id
- is their only replay guard, and a recorded id would answer the provider's retry as a duplicate
- once the profile is served again."""
+ """A 503 for a webhook whose profile is not served right now, else None: a ``/p/<profile>/``
+ prefix that is parked or has no serving authority here, or a coalesced route that
+ ``profile_routes`` sends to a profile admission would refuse (a coalesced delivery is acknowledged
+ before admission applies that route). A parked target is retryable (``Retry-After``); any other
+ rejected target gets the answer the immediate route gives when admission refuses it. Answered
+ before the delivery id is recorded: coalesced, ``deliver_only`` and ``cron_job`` routes act before
+ (or without) an admission, so the in-memory id is their only replay guard, and a recorded id would
+ answer the provider's retry as a duplicate once the profile is served again."""
+ from aiohttp import web
if _prefix_not_served(runner, profile):
error = f'Profile {profile} is not being served; retry this delivery'
- elif _coalesced_to_parked(runner, profile, route_name, route_config):
- error = 'The routed profile is not being served right now; retry this delivery'
else:
- return None
- from aiohttp import web
+ refusal = _coalesced_route_refusal(runner, profile, route_name, route_config)
+ if refusal is None:
+ return None
+ if refusal == 'refused':
+ # ``WebhookAdapter._dispatch_agent_run``'s answer when admission refuses the delivery.
+ return web.json_response({'error': 'Admission unavailable; retry this delivery'}, status=503)
+ error = 'The routed profile is not being served right now; retry this delivery'
from gateway.parked_route_refusal import ParkedRouteRefused
return web.json_response({'error': error}, status=ParkedRouteRefused.status, headers=ParkedRouteRefused.headers)
@@ -88,16 +93,18 @@ def _prefix_not_served(runner, profile):
return authority is None or getattr(authority, 'retiring', False) is True
-def _coalesced_to_parked(runner, profile, route_name, route_config):
- """A coalesced bare-route delivery that ``profile_routes`` sends to a parked profile. Asks the
- admission's own resolver (``GatewayRunner._profile_name_for_source``) so this edge and admission
- give one answer: an explicit ``/p/<profile>/`` wins over ``profile_routes`` there (the source's
- ``profile`` is set), and routes apply only under multiplexing."""
+def _coalesced_route_refusal(runner, profile, route_name, route_config):
+ """How admission would refuse a coalesced bare-route delivery that ``profile_routes`` sends away:
+ ``'parked'`` (retryable), ``'refused'`` (a profile neither served here nor parked: standalone,
+ unknown, or without a live authority), else None. Asks the admission's own resolver
+ (``GatewayRunner._profile_name_for_source``) so this edge and admission give one answer: an
+ explicit ``/p/<profile>/`` wins over ``profile_routes`` there (the source's ``profile`` is set),
+ and routes apply only under multiplexing."""
if profile or not isinstance(route_config.get('coalesce'), dict):
- return False
+ return None
resolve = getattr(runner, '_profile_name_for_source', None)
if not callable(resolve):
- return False
+ return None
from gateway.config import Platform
from gateway.profile_routing import ProfileRouteRejected
from gateway.run_runtime import profile_parked
@@ -110,8 +117,12 @@ def _coalesced_to_parked(runner, profile, route_name, route_config):
except ProfileRouteRejected as exc:
# A parked target leaves the served set, so the resolver rejects it; admission retries
# that one (``_rejected_route_profile``) and refuses any other rejection for good.
- routed = exc.profile
- return bool(routed) and profile_parked(runner, routed)
+ return 'parked' if exc.profile and profile_parked(runner, exc.profile) else 'refused'
+ if not routed:
+ return None
+ if profile_parked(runner, routed):
+ return 'parked'
+ return 'refused' if _prefix_not_served(runner, routed) else None
async def admit_producer(adapter, event):
diff --git a/tests/gateway/test_parked_profile_ingress.py b/tests/gateway/test_parked_profile_ingress.py
index 40bded6e94f..876d6ead67f 100644
--- a/tests/gateway/test_parked_profile_ingress.py
+++ b/tests/gateway/test_parked_profile_ingress.py
@@ -316,6 +316,45 @@ async def test_an_explicit_parked_profile_prefix_is_retried_even_when_a_route_po
_close(runner, homes)
[email protected]('target', ['standalone', 'unknown'])
+async def test_a_coalesced_bare_route_to_an_unserved_profile_is_refused_like_the_immediate_one(
+ tmp_path, monkeypatch, target):
+ """A ``profile_routes`` rule sending a bare route to a profile this gateway does not serve and
+ has not parked (``standalone: true``, or no such profile) is refused by admission for good. The
+ coalesced route must give the immediate route's answer before its delivery id is recorded, not
+ ``202 coalesced`` followed by a flush that drops the event (providers never resend a 202)."""
+ from gateway.platforms.webhook import _INSECURE_NO_AUTH
+ from gateway.profile_routing import ProfileRoute
+ homes = _homes(tmp_path, monkeypatch)
+ root, beta = homes[0][1].resolve(), homes[1][1].resolve()
+ name = 'gamma' if target == 'standalone' else 'ghost'
+ if target == 'standalone':
+ gamma = root / 'profiles' / 'gamma'
+ gamma.mkdir(parents=True, mode=0o700)
+ (gamma / 'config.yaml').write_text('gateway:\n standalone: true\n')
+ runner = await _runner_with_beta_parked(tmp_path, monkeypatch, homes, profile_routes=[
+ ProfileRoute(name=f'{route}-to-{name}', platform='webhook', profile=name, user_id=f'webhook:{route}')
+ for route in ('events', 'events_now')])
+ await _serve_beta_again(runner, beta)
+ adapter = _webhook(runner, {
+ 'events': {'secret': _INSECURE_NO_AUTH, 'prompt': 'event {id}', 'coalesce': {'key': 'id', 'window_seconds': 0.05}},
+ 'events_now': {'secret': _INSECURE_NO_AUTH, 'prompt': 'event {id}'}})
+ settled = lambda: not adapter._coalescer.pending and not adapter._background_tasks
+ try:
+ immediate = await adapter._handle_webhook(_webhook_request('delivery-1', {'id': 17}, 'events_now', None))
+ coalesced = await adapter._handle_webhook(_webhook_request('delivery-2', {'id': 18}, 'events', None))
+ await async_wait_until(settled, what='nothing left to flush')
+ assert immediate.status == 503, (immediate.status, immediate.text)
+ assert (coalesced.status, json.loads(coalesced.text)) == (immediate.status, json.loads(immediate.text)), \
+ (coalesced.status, coalesced.text)
+ assert adapter._seen_deliveries == {}, 'the refused delivery id was recorded'
+ assert _admissions(root) + _admissions(beta) == 0
+ finally:
+ _cancel(adapter)
+ _close(runner, homes)
+
+
@pytest.mark.asyncio
@pytest.mark.parametrize('mode', ['deliver_only', 'cron_job'])
async def test_a_non_durable_webhook_for_a_parked_profile_is_refused_before_its_id_is_recorded(