-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ratelimit based on JWT claim (#7175)
- Loading branch information
Showing
10 changed files
with
235 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# Rate Limit JWT claim | ||
|
||
In this example, we deploy a web application, configure load balancing for it via a VirtualServer, and apply a rate | ||
limit policy using a JWT claim as the key to the rate limit. | ||
|
||
## Prerequisites | ||
|
||
1. Follow the [installation](https://docs.nginx.com/nginx-ingress-controller/installation/installation-with-manifests/) | ||
instructions to deploy the Ingress Controller. | ||
1. Save the public IP address of the Ingress Controller into a shell variable: | ||
|
||
```console | ||
IC_IP=XXX.YYY.ZZZ.III | ||
``` | ||
|
||
1. Save the HTTP port of the Ingress Controller into a shell variable: | ||
|
||
```console | ||
IC_HTTP_PORT=<port number> | ||
``` | ||
|
||
## Step 1 - Deploy a Web Application | ||
|
||
Create the application deployment and service: | ||
|
||
```console | ||
kubectl apply -f webapp.yaml | ||
``` | ||
|
||
## Step 2 - Deploy the Rate Limit Policy | ||
|
||
In this step, we create a policy with the name `rate-limit-jwt` that allows only 1 request per second coming from a | ||
single IP address. | ||
|
||
Create the policy: | ||
|
||
```console | ||
kubectl apply -f rate-limit.yaml | ||
``` | ||
|
||
## Step 3 - Configure Load Balancing | ||
|
||
Create a VirtualServer resource for the web application: | ||
|
||
```console | ||
kubectl apply -f virtual-server.yaml | ||
``` | ||
|
||
Note that the VirtualServer references the policy `rate-limit-jwt` created in Step 2. | ||
|
||
## Step 4 - Test the Configuration | ||
|
||
The JWT payload used in this testing looks like: | ||
|
||
```json | ||
{ | ||
"name": "Quotation System", | ||
"sub": "quotes", | ||
"iss": "My API Gateway" | ||
} | ||
``` | ||
|
||
In this test we are relying on the NGINX Plus `ngx_http_auth_jwt_module` to extract the `sub` claim from the JWT payload into the `$jwt_claim_sub` variable and use this as the rate limiting `key`. | ||
|
||
Let's test the configuration. If you access the application at a rate that exceeds one request per second, NGINX will | ||
start rejecting your requests: | ||
|
||
```console | ||
curl --resolve webapp.example.com:$IC_HTTP_PORT:$IC_IP http://webapp.example.com:$IC_HTTP_PORT/ -H "Authorization: Bearer: `cat token.jwt`" | ||
``` | ||
|
||
```text | ||
Server address: 10.8.1.19:8080 | ||
Server name: webapp-dc88fc766-zr7f8 | ||
. . . | ||
``` | ||
|
||
```console | ||
curl --resolve webapp.example.com:$IC_HTTP_PORT:$IC_IP http://webapp.example.com:$IC_HTTP_PORT/ -H "Authorization: Bearer: `cat token.jwt`" | ||
``` | ||
|
||
```text | ||
<html> | ||
<head><title>503 Service Temporarily Unavailable</title></head> | ||
<body> | ||
<center><h1>503 Service Temporarily Unavailable</h1></center> | ||
</body> | ||
</html> | ||
``` | ||
|
||
> Note: The command result is truncated for the clarity of the example. |
9 changes: 9 additions & 0 deletions
9
examples/custom-resources/rate-limit-jwt-claim/rate-limit.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
apiVersion: k8s.nginx.org/v1 | ||
kind: Policy | ||
metadata: | ||
name: rate-limit-jwt | ||
spec: | ||
rateLimit: | ||
rate: 1r/s | ||
key: ${jwt_claim_sub} | ||
zoneSize: 10M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImtpZCI6IjAwMDEifQ.eyJuYW1lIjoiUXVvdGF0aW9uIFN5c3RlbSIsInN1YiI6InF1b3RlcyIsImlzcyI6Ik15IEFQSSBHYXRld2F5In0.ggVOHYnVFB8GVPE-VOIo3jD71gTkLffAY0hQOGXPL2I |
16 changes: 16 additions & 0 deletions
16
examples/custom-resources/rate-limit-jwt-claim/virtual-server.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
apiVersion: k8s.nginx.org/v1 | ||
kind: VirtualServer | ||
metadata: | ||
name: webapp | ||
spec: | ||
host: webapp.example.com | ||
policies: | ||
- name: rate-limit-jwt | ||
upstreams: | ||
- name: webapp | ||
service: webapp-svc | ||
port: 80 | ||
routes: | ||
- path: / | ||
action: | ||
pass: webapp |
32 changes: 32 additions & 0 deletions
32
examples/custom-resources/rate-limit-jwt-claim/webapp.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: webapp | ||
spec: | ||
replicas: 1 | ||
selector: | ||
matchLabels: | ||
app: webapp | ||
template: | ||
metadata: | ||
labels: | ||
app: webapp | ||
spec: | ||
containers: | ||
- name: webapp | ||
image: nginxdemos/nginx-hello:plain-text | ||
ports: | ||
- containerPort: 8080 | ||
--- | ||
apiVersion: v1 | ||
kind: Service | ||
metadata: | ||
name: webapp-svc | ||
spec: | ||
ports: | ||
- port: 80 | ||
targetPort: 8080 | ||
protocol: TCP | ||
name: http | ||
selector: | ||
app: webapp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
apiVersion: k8s.nginx.org/v1 | ||
kind: Policy | ||
metadata: | ||
name: rate-limit-jwt-claim-sub | ||
spec: | ||
rateLimit: | ||
rate: 1r/s | ||
key: ${jwt_claim_sub} | ||
zoneSize: 10M |
22 changes: 22 additions & 0 deletions
22
tests/data/rate-limit/spec/virtual-server-jwt-claim-sub.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
apiVersion: k8s.nginx.org/v1 | ||
kind: VirtualServer | ||
metadata: | ||
name: virtual-server | ||
spec: | ||
host: virtual-server.example.com | ||
policies: | ||
- name: rate-limit-jwt-claim-sub | ||
upstreams: | ||
- name: backend2 | ||
service: backend2-svc | ||
port: 80 | ||
- name: backend1 | ||
service: backend1-svc | ||
port: 80 | ||
routes: | ||
- path: "/backend1" | ||
action: | ||
pass: backend1 | ||
- path: "/backend2" | ||
action: | ||
pass: backend2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters