Skip to main content

9. Secure the API

The orders API answers anyone. This step closes it, in four ways, from the simplest to the most complete. They are alternatives: choose the one that fits who calls the API.

Where each protection is checked

In every case the check happens at the gateway. A request that fails it never reaches the integration, and the route does not change.

Authentication method by client type​

There is no single best protection. Start from who calls the API.

Who callsProtectionWhat the caller sendsWhat you manage
A few known applications, simplyAPI keyA key in a headerA list of keys
People or scripts, on an internal toolUser name and passwordA user name and a passwordA list of users
Applications, at scale and per APIToken (JWT)A token obtained beforehandClients, and what each may call
Anything, by rules of your ownExternal authenticationWhatever your service expectsA service that answers yes or no
People in a browserSign-inNothing: they are sent to sign inAn identity provider

Security rules are added from the page of the API instance, in its security policy.

A. API key authentication​

The quickest way to close an API.

  1. In Secret Manager, create an API Keys configuration named orders-keys. Add one entry per client, such as mobile-app and backoffice. Each key is shown once: copy it.
  2. On the API instance, add the rule API keys, choose orders-keys, and say where the key is sent: the header x-api-key.
  3. Deploy.
curl -i https://orders.example.com/orders
Answer
HTTP/1.1 401 Unauthorized

Useful options: Forward Client ID Header passes the name of the client to the integration, which can then tell mobile-app from backoffice; Sanitize Key removes the key before the request reaches the integration.

Limits: a key does not expire and says nothing about what the caller may do. Whoever holds it can call the API until you remove it.

B. HTTP Basic authentication​

  1. Create a Basic Auth Users configuration with the users and their passwords.
  2. On the API instance, add the rule user name and password, and choose it.
  3. Deploy.
With a user name and a password
curl -u alice:<password> https://orders.example.com/orders

A browser shows its own sign-in prompt. Forward Username Header passes the verified user name to the integration.

Limits: the password travels with every request. Suitable for an internal tool, not for a public API.

C. JWT authentication (OAuth 2.0 client credentials)​

The way to protect APIs called by applications. The caller first obtains a token from an auth gateway, valid for a limited time, then sends it with each request. The gateway checks the signature of the token, that it has not expired, and that it was issued for this API.

  1. Create the Auth Gateway. In API Manager, Auth Gateways, create one on the HTTPS access point of your gateway, at auth.example.com.
  2. Register the caller. Copy the audience ID shown on the orders API. In a list of clients, add a client: a name, a generated secret, the permission orders.read, and the audience ID. Choose the list on the auth gateway.
  3. Protect the API. On the API instance, add token verification (JWT): tokens issued by an auth gateway, yours, with Check the audience on.
  4. Deploy.
1. Obtain a token from the auth gateway
curl -u "<client-id>:<secret>" https://auth.example.com/api/oidc/token \
-d grant_type=client_credentials \
-d scope="orders.read"
Answer
{
"access_token": "eyJhbGciOi...",
"token_type": "bearer",
"expires_in": 3600
}
2. Call the API with the token
curl -H "Authorization: Bearer eyJhbGciOi..." https://orders.example.com/orders

Why it is better than a key:

  • a token expires: a stolen one is useless shortly after;
  • a token says which API it is for: a client registered for the orders API cannot call the billing API with it;
  • the secret of the client is sent to the auth gateway only, never to the API.

Each step is detailed in Auth Gateways, Lists of Clients and Protect an API with JWT.

If your company already has an identity provider that issues tokens, choose Another identity provider in the rule and give its issuer and the address of its keys: no auth gateway is needed.

D. External authorization​

When the decision depends on your own rules (a customer's contract, a time of day, a partner directory), the gateway can ask a service of yours for each request.

  1. Deploy the service that decides. It can be an integration: it receives the request's headers and answers allowed or denied.
  2. On the API instance, add the rule external authentication: choose HTTP or gRPC, and the service.
  3. Deploy.

Fail Open decides what happens when your service does not answer. Off (the default), the request is refused: the API is unavailable but never open. On, the request goes through.

Limits: every request now waits for your service, and depends on it. Keep it fast and run more than one instance.

Authentication on the gateway only protects what goes through the gateway

A workload inside the environment can still call the integration directly by its service name. Restrict ingress to the integration with network policies if that matters.

Combining rules in one security policy​

A security policy can hold several rules. Two combinations are common:

  • tokens + address rules: only your partners' addresses, and each with a valid token;
  • tokens + rate limit: each client is identified, and none can flood the API.

Add CORS when the API is called by a web page served from another site.

Test the refusals, not only the success

A policy is proven by what it rejects. For each rule, make at least one call that must fail.

Test matrix​

Run these calls after each change of policy. Each row checks one behavior.

TestExpected
Call without credentialsRefused
Call with valid credentialsAnswered
Call with the credentials of another APIRefused, with tokens and the audience check on
Look at the logs of the integrationOnly the accepted calls appear

Next: observe the integration.