Part 7 of 9 · 3 min read
Jobs that every service needs
Authentication at the gateway, authorization in the services, API keys and the other cross-cutting work.
Think of a building with one security desk at the entrance. The desk checks passes once. Inside, each room still decides who may open its own cabinets.
Some work would otherwise be repeated in every service. The book calls these cross-cutting concerns, and the gateway is a proxy where they can be done once: caching of frequent replies, rate limiting to protect services (page 7), and security.
For security the book separates two ideas.
Authentication and authorization
Authentication checks who is making the request. Authorization decides what that caller may do.
Example: a valid login proves that the caller is Asha (authentication). Whether Asha may delete order 90 is a different check (authorization).
The book recommends authentication at the gateway, since it is the point of entry. This puts the logic for many login methods in one place and hides it from the services. Authorization is best left to the individual services, to avoid tying the gateway to their business rules.
The steps in the book's figure, simplified:
- The client sends a request with credentials to the gateway.
- The gateway checks them with an authentication service.
- The authentication service replies with a security token.
- The gateway forwards the request to the internal services, with the token attached.
- Each service reads the token to learn who the caller is and what roles they hold, and decides what is allowed.
- The gateway composes the replies and answers the client.
Tokens come in two kinds, as page 5 showed. The book names them opaque and transparent.
- An opaque token has no information in it, such as a random identifier. A service must call the authentication service to learn what it stands for.
- A transparent token carries the facts, such as a JSON Web Token (JWT), signed so that services can check it without any call. The cost is that revoking one is harder.
Another method for public APIs is the API key. The book describes it as a custom key that lets the gateway see which application is calling and limit what that application can do. Public APIs of large companies use this.
API key
An API key is a long secret string given to an application so that the gateway can identify it and apply its limits.
Example: a partner app sends X-Api-Key: k_91ab.... The gateway sees that this partner may make 100 calls a minute.
A gateway is also a good place to add a request identifier, a unique value attached to each incoming call and passed to every inner call. Logs from all services can then be joined to trace one request (Level 9).
A worked example of the saving. A system has 25 services. Without a gateway, each service must check tokens, limit rates and log requests.
- Three jobs times 25 services is 75 pieces of similar code.
- With a gateway doing two of the jobs, the count is 25 for the third (authorization) plus 2 in the gateway, 27 in all.
- A change to the login method touches one place instead of 25.
Common mistake
Trusting the internal network because the gateway checked the caller. A service that accepts any request from inside the network is exposed if one internal machine is breached. Services should still verify the token and decide authorization themselves.
Check yourself:
- Which should the gateway do, authentication or authorization, and why?
- What is the difference between an opaque and a transparent token?