[Tutorial] Securing Rate Limit Actions with JSON Web Tokens (JWT)
Most applications require external users or clients to access a variety of their services and therefore require policies to secure and protect them. Rate limiting is a strategy implemented in API or Edge Gateways to protect backend services by preventing service outages from being overrun with more requests than it’s resources can process and respond to within the agreed service levels.
Rate limits can be configured in a variety of ways to limit the volume or requests over a time period to the endpoint, by the client ID, by HTTP method, with integrated security, and more. In previous posts we tried multiple rate limits by client ID, rate limits to prioritize traffic by HTTP method, and in this post we will secure rate limit actions with JSON Web Tokens (JWT).
When to use this use case
Headers are a convenient way to determine the values for rate limit actions and can often require additional security depending on the use case. While Gloo API Gateway has the ability to extensively rate limit on any header coming in, Gloo can also secure the header by encoding the values as claims in a JWT that is passed on in the request and the headers can be derived from the extracted claims after the JWT has been verified. You can provide a secure method for your users to acquire a JWT by setting up the ability for an auth negotiation with a trusted auth provider.
Try this use case
Follow along using this tutorial to try configuring this type of rate limiting policy.
Set up your environment using these instructions. Any self-hosted or cloud managed Kubernetes (our demo uses GKE) with Gloo API Gateway (latest 1.4) installed will work. Then deploy the demo app, add a virtual service to expose via Gloo, and query the service.
Then you’ll use these instructions to set up and test the rate limit configuration and also layer in Open Policy Agent (OPA) and a Web Application Firewall (WAF) to further secure the traffic.
- Apply settings with
settings-patch-1.yamlto see how headers work
- Apply rate limit actions on the route with
- Add JWT claims and verification with
- Layer on WAF and OPA with
Here is what the YAML looks like with added JWT:
- prefix: /
- header: x-token
- claim: type
- claim: number
-----BEGIN PUBLIC KEY-----
-----END PUBLIC KEY-----
Watch the demo
We hope you found this tutorial informative and useful in your exploration of API Gateway functionality. You can watch the full webinar featuring this demo here and sign up for an upcoming webinar here.