Ch.69: System Design: API Gateway Permissions for the Wrong Route
Transcript
0:00 An API gateway gives clients one address, applies shared request rules and forwards accepted calls to internal services. Let's design its routing, permission checks and configuration changes. The backend should receive exactly the operation we allowed. Take Maya in Harbor, our fictional workspace app. She can list projects, but administrative deletion is forbidden for her ordinary account. Then suppose we reuse the list request's cached yes for deletion. The same caller gets permission for the wrong operation.
0:28 Same account, different permission question. Let's follow both requests and keep each answer attached to the operation it actually allowed. Now trace Maya through this architecture: her app reaches a gateway fleet, then Harbor's project service. Each gateway has local routes, a credential verifier and rules for each operation. Changing those rules is a separate job. A publisher sends configurations to the gateways. Each instance can cache earlier permission answers; the services still protect project data.
0:57 So opening the project list doesn't require a call to the publisher. We'll return to this same map after working through the checks. First, Maya needs a stable public API to list projects. Administrators need deletion, and integrations use the same entry point. Those are our functional requirements. Her app shouldn't change whenever we move the backend. We keep its public route stable while forwarding to the current private destination. The gateway also rejects malformed calls, verifies credentials and checks permission for the selected operation.
1:28 Accepted calls carry a trustworthy identity to the service. Does that permission check also decide which specific projects this caller may delete, with all the organization's sharing rules? I'd leave project membership with its service. Operation permission admits deletion; object permission covers the particular project. Like one building pass versus keys for individual cabinets. So even an administrator admitted to deletion can be refused for a project belonging to another customer organization.
1:55 The service checks that particular project. Yes. AWS's gateway docs also show how a cached allow can cover the wrong route. We'll unpack that; the source links are in the description. Those shared checks create our non-functional requirements. For example, assume a fictional peak of 50,000 requests each second, with gateways spread across three availability zones. Give the gateway an added-latency target below 20 milliseconds at the 99th percentile, under the workload we're sizing. The whole client deadline is 300 milliseconds.
2:27 With average requests of 4 kilobytes and responses of 16 kilobytes, what's passing through that supposedly small box? Using decimal units, incoming payload alone is about 200 megabytes per second, before protocol overhead. These are design assumptions, not vendor benchmarks. And the larger responses put about 800 megabytes a second on the way out. I'd budget that network traffic and bound body sizes and concurrent calls. For replica count, we need a load test at this request mix and latency target.
2:57 Suppose it later measured 5,000 requests a second per instance. Then our fictional 50,000 needs 10 just to carry the peak, before failure headroom. We haven't measured that rate; it's a sizing illustration. Now Maya wants her projects while one zone is down. Do we queue everything on the other two? No. Send calls to healthy gateways, then admit only what those survivors support within our latency target. Refuse excess calls early; surviving capacity still needs a measurement. And if every surviving gateway waits on the same permission database, what have those extra copies actually bought us?
3:32 They haven't removed that dependency. Local routes and credential checks avoid waiting on it when their validity allows it. We still need to decide what each local answer proves. Before building the fleet, imagine a small-app maintainer asking: why operate another system when this service can enforce the same rules? Fair enough. Harbor earns the gateway when several services and clients need the same external policies. Otherwise we're distributing policy code and migration work everywhere. A reverse proxy forwards; a load balancer picks an eligible server; gateway rules control API entry.
4:07 One mailroom can forward parcels, pick desks and check access. The distinction is the decision, rather than three mandatory boxes. A bad shared rule can spread very efficiently too; I'd review that risk before celebrating reduced duplication. Which leaves the first trust decision: the incoming request carries claims supplied by the client. Let's unpack how the backend gets an identity it can actually trust. Before trusting Maya's call, reject invalid framing and excessive sizes. Then inspect the credential she presents, rather than accepting an identity she supplies.
4:41 An access token is one such credential. Our configured verifier checks its signature, issuer, intended API and validity period, rather than merely decoding readable fields. My first thought was to keep a customer-ID header after checking the token. I hadn't established whether that header and token described the same customer. Right. For example, Maya's token identifies her customer organization, while the header names another. Discard reserved identity headers and build Maya's context from the verified token fields.
5:11 Could the client skip the gateway and send that same internal header straight to the backend instead? Not in our design. The service accepts that gateway context only over an authenticated internal connection from an allowed gateway, with direct client access blocked. That makes the internal connection part of the trust boundary. A familiar-looking header on an arbitrary connection wouldn't carry the same authority. With Maya's identity established, we can choose her operation. Which means the operation must stay stable through its check.
5:41 Otherwise a correct permission decision can accompany the wrong forwarded request. Next, select that operation by matching the host, HTTP method and normalized path against a local route table. The route identifies its backend action and policy. By normalized path, you mean the agreed representation used for matching, rather than letting two components interpret the same incoming address differently? Yes. Reject ambiguous forms or resolve them consistently. Maya's call uses one unchanging set of routes and permissions; a newly arriving version applies to new calls.
6:14 Got it. Suppose a compatibility rule rewrites an old address to a newer operation. Does that happen before or after the permission check? Before it, if the rewrite changes the operation being authorized. A later change needs a fresh decision for the new operation, not the old allow. It's like clearing a parcel for the reception desk, then changing its label to the records vault. The original check covered a different destination. Right. The vault can't rely on reception's stamp. Envoy documents a later stage clearing its remembered route, so another route's required authorization can be missed.
6:50 So our design freezes the operation first, then checks that operation's permission. We're not claiming every Envoy deployment, or every gateway, has this problem. That saved route belongs to one call. A different cache can reuse permission answers across calls, which brings us back to Maya's list and delete. Now consider this: Maya asks for her project list. The permission checker allows that member operation, and we cache its yes to save repeating the check. Then Maya attempts the administrative delete with that same token.
7:21 If the cache key contains only the token, does the checker run again? No. That cache can return the earlier yes before the checker sees the administrative operation. Each lookup matched the key, but the two permission questions differed. Hold on. The cache hasn't verified the second permission at all. It remembered an answer after discarding the operation that made that answer meaningful. Yes, AWS documents that scope problem. It calls the checker an authorizer. Its HTTP API warning concerns a simple yes-or-no response being cached across matching requests. Could the checker return which operations are allowed, rather than a yes that loses track of the question?
8:01 Yes, in a permission-policy response. Instead of one yes stamp, it's a permit naming the doors it opens. The contrast is named scope versus bare approval. For these two operations, that permit must distinguish the doors. For cached yes-or-no answers, adding the route to the key separates Maya's entries. And AWS's 2020 explanation offered the simpler response to reduce checker code. It was a convenience choice, rather than a different meaning for permission. Ah, less response code still leaves us with cache design.
8:33 For Harbor, include the verified credential, selected operation and policy version. Every other decision input must match too. Yes, including the relevant customer organization and request context. If we can't name all those inputs, don't cache that decision. Bound its lifetime by credential validity and our freshness contract. So a cache hit is permission to reuse one particular answer. It isn't permission to skip a different question because the caller looks familiar. Exactly. Maya's administrative call gets its own decision and is denied. Now let's keep the calls we do admit from consuming unlimited time or work. Once the operation is allowed, apply admission and concurrency limits before a queue grows without bound.
9:16 Large uploads can use a separately authorized storage path instead of unrestricted buffering. And its deadline started before these checks. A request spending 10 milliseconds in admission carries at most 290 milliseconds to its backend. Suppose a deletion reaches the service, but the gateway times out waiting for its reply. Can the proxy safely repeat the call on another server? Only if the service's retry contract makes repeating this same request safe, without applying its business change again.
9:46 A timeout doesn't tell us whether deletion happened. Our gateway mustn't invent that guarantee. So Harbor bounds work without taking ownership of backend outcomes. That leaves configuration rollout: how do thousands of operations get consistent rules onto the gateway fleet? Now imagine an operator disabling administrative deletion during maintenance. That's a new configuration bundle: routes, permission rules and the backend destinations they depend on travel together. Configuration work is our control plane; serving calls is the data plane.
10:17 It's one publisher redrawing a delivery map while couriers keep using their existing copies. Before activation, test both versions. An administrator's deletion was allowed under the old bundle and must be refused under the new one. Maya's deletion stays denied under both. What if Maya's list call matches a new route before this gateway has loaded where it should send her? Then its route points to a destination missing from local configuration. Load that destination first. Envoy's configuration protocol explains this ordering between separate resource updates. Locally, I'd activate routes and permissions together.
10:50 Calls already in progress keep their original rules; new calls use the active version. And put the policy version into any route-dependent decision-cache key. Otherwise an old cached answer could outlive the rule that allowed it. But that local swap doesn't switch the whole fleet. An administrator reaches an instance that missed the update. Does it still allow deletion? It can, while its previous configuration remains valid. I don't know yet when to stop admitting that administrator. Let's follow this instance until its cutoff.
11:20 So follow the administrator at that disconnected gateway. It received confirmation for its old rules before the restriction, then lost contact. Harbor's 30-second freshness contract limits how long it may continue. Does an unchanged configuration become unusable 30 seconds after we create it, even while every gateway is connected and healthy? No. That confirmation expires, not the rules' creation date. The publisher names the active revision and its expiry; healthy gateways keep renewing an unchanged version.
11:50 For the example, call the publisher's expiry time 30. The gateway reports 25 on its clock when that confirmation arrives. Arrival doesn't buy another 30 seconds. Now the administrator's next call arrives when this clock reads 27. Suppose we can reliably bound clock error to 2 seconds. Publisher time could already be 29. So we can admit this call, assuming the credential and cached permission answer are still valid? And the verification keys are still usable, yes. At local time 28, our 2-second margin reaches expiry.
12:22 Without a renewal, we refuse that next administrative call. That's a narrow window. What if we can't trust the clock-error bound at all? Then we refuse administrative calls rather than guess when the publisher's deadline arrives. The contract permits bounded restriction delay, not instant revocation. An expired message can't revive permission. Back at 27, what if verification keys or a required remote permission check can't be reached? A cached answer helps only if it was verified and every input and validity condition still matches.
12:53 Credential, key, permission answer and configuration confirmation must remain usable. An unknown key or required permission stays unknown, so we refuse that protected call. I'd give the public health check its own explicit policy. Otherwise someone might add a broad permission bypass just to make a dashboard green. And if the new bundle itself is bad, rollback restores compatible routing and permission rules together. That repairs future handling; it can't undo a deletion already carried out. Now, to check whether the backend received the operation we authorized, give each call a request ID.
13:27 The gateway records its authorized operation, policy version and whether the decision came from cache, keeping tokens and bodies out. Suppose the service logs delete under Maya's request ID, but the gateway logged an approved list. Is that the mismatch we're hunting? Yes. The service received delete after the gateway authorized list. Test that late rewrite through the real request path: stop it or check permission for deletion. Also send Maya's direct administrative request and confirm it's denied.
13:58 For the disconnected gateway, compare its revision and confirmation expiry with the publisher's active state; a fleet average can hide it. Now those records connect the specific allow to the operation received. Let's follow Maya's two calls across the complete map. Now, on the same architecture map, Maya's list request gets a verified identity, a stable route and operation permission. Where does her deletion stop? At the gateway: her list answer can't authorize deletion. A permitted administrator can still be admitted by the isolated gateway before its conservative cutoff, but is refused after it.
14:34 Beside that path, the publisher supplies tested configurations and instances report adoption. Services receive verified context and the remaining deadline, while still deciding access to each project. With both calls resolved, I'd add the gateway when shared routing and API policy justify it. One small service can keep these responsibilities inside. I'd defend that choice by following Maya's request. Establish the caller, fix the operation, check its policy, then forward work within the remaining budget.
15:03 I like that every allow now has a specific meaning, and the backend still owns its data. Thanks for listening to Learning Podcasts.