# `Ankusa.Edge.RouteGuard`
[🔗](https://github.com/jamescarr/ankusa/blob/ankusa-v0.7.0/packages/ankusa/lib/ankusa/edge/route_guard.ex#L1)

The ingress gate: with routes enabled, a `POST` is captured only if it passes
the IP rules and matches an enabled route.

`Ankusa.Edge.Router` calls this before anything else on the capture path, so a
rejected request is never read, never verified, and never written to the store —
it cannot become a record, a dispatch, or a delivery. It is also a plain
`Plug`, so an embedder that owns its own Bandit pipeline can put it in front of
whatever it already has.

## Responses

| Situation | Status | Body |
| --- | --- | --- |
| No route matches | `404` | `{"error": "not_found"}` |
| Path matches, method does not | `404` | `{"error": "not_found"}` |
| IP rules deny | `403` | `{"error": "forbidden"}` |
| IP rules deny, `routes.ip_denied_status: 404` | `404` | `{"error": "not_found"}` |

A method rejection is a `404` like a path rejection, so a probe cannot learn
which paths exist by comparing status codes. No `www-authenticate` or
`retry-after` header is sent: there is nothing for the sender to do.

One exception is the operator's to make. A sender denied by a *route's own*
`ip_rules` gets `routes.ip_denied_status` (403 by default) while an unknown path
gets `404`, so with the default it can tell that the path it hit is a route. A
global IP denial answers every path alike and leaks nothing. Set
`routes.ip_denied_status: 404` when that distinction matters.

## Senders retry on 4xx

Some providers retry any non-2xx, some give up, some disable the endpoint after
enough failures. Ankusa does not retry a rejected hook — it never accepted
it — so the provider's own policy decides what happens next, and `403`/`404`
are the only signals it gets. Dry-run a route change
(`POST /admin/routes/test`) before rolling it out.

## Failing closed

Routes enabled with no snapshot loaded (a store that never published one) is a
rejection, not a pass: the alternative is capturing everything exactly when the
allowlist is broken. The same goes for a conn whose peer address is nil —
there is no address to match rules against, so there is nothing to allow.

## What a match assigns

A captured request carries the route that admitted it as
`conn.assigns.ankusa_route`, an `Ankusa.Routes.Route`. `Ankusa.Edge.Router`
hands it to `Ankusa.Edge.Ingest.lookup/3`, which holds the route's `tenant_id`
and `source_id` against what the resolver said: a route owned by one tenant
admits nothing into another tenant's source, and a route bound to a source
admits nothing for another. A mismatch is the `404` an unknown source gets.

## Telemetry

  * `[:ankusa, :routes, :match]` — metadata
    `%{instance:, route_id:, cached:, cacheable:}`. `:cached` is true when the
    decision came from `Ankusa.Routes.Cache`. `:cacheable` is false when the
    request path is past the cache's key bound (`Ankusa.Routes.Cache.cacheable?/1`),
    so every request for it is matched by a scan of the route table and none is
    ever a `cached: true`: the way to tell "a legitimate path that is too long
    to cache" from an ordinary miss.
  * `[:ankusa, :routes, :reject]` — metadata
    `%{instance:, reason:, method:, path:}` with `:reason` one of `:no_route`,
    `:method`, `:ip_denied`.

Rejections are also logged at `:debug`, sampled at `routes.log_sample` (0
disables it): a scanner hitting random paths must not fill a disk, but an
operator debugging "why is this one provider failing" needs a line. The warning
for a node with no route table loaded is sampled the same way, so a store that
is slow to come up does not write one line per rejected hook.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
