Skip to content

Commit 21de0a1

Browse files
authored
docs: Added rate limit to OIDC+K8s auth user guide (#121)
1 parent b508fa9 commit 21de0a1

File tree

2 files changed

+99
-5
lines changed

2 files changed

+99
-5
lines changed

examples/oidc-k8s-auth/README.md

+69-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
# Protecting an API with JSON Web Tokens (JWTs) and Kubernetes authnz using Kuadrant
1+
# Rate-limiting and protecting an API with JSON Web Tokens (JWTs) and Kubernetes authnz using Kuadrant
22

3-
Example of protecting an API (the Toy Store API) with authentication based on ID tokens (signed JWTs) issued by an
4-
OpenId Connect (OIDC) server (Keycloak) and alternative Kubernetes Service Account tokens, and authorization based on
5-
Kubernetes RBAC, with permissions (bindings) stored as Kubernetes Roles and RoleBindings.
3+
Example of rate-limiting and protecting an API (the Toy Store API) with authentication based on ID tokens (signed JWTs)
4+
issued by an OpenId Connect (OIDC) server (Keycloak) and alternative Kubernetes Service Account tokens, and authorization
5+
based on Kubernetes RBAC, with permissions (bindings) stored as Kubernetes Roles and RoleBindings.
66

77
## Pre-requisites
88

99
- [Docker](https://www.docker.com/)
1010
- [kubectl](https://kubernetes.io/docs/reference/kubectl/) command-line tool
1111
- [jq](https://stedolan.github.io/jq/)
1212

13-
## Run the guide ❶ →
13+
## Run the guide ❶ →
1414

1515
### ❶ Setup the environment
1616

@@ -124,6 +124,15 @@ spec:
124124
user:
125125
valueFrom:
126126
authJSON: auth.identity.sub
127+
response:
128+
- name: rate-limit
129+
json:
130+
properties:
131+
- name: userID
132+
valueFrom:
133+
authJSON: auth.identity.sub
134+
wrapper: envoyDynamicMetadata
135+
wrapperKey: ext_auth_data
127136
EOF
128137
```
129138

@@ -270,6 +279,61 @@ curl -H "Authorization: Bearer $SA_TOKEN" -H 'Host: api.toystore.com' -X POST ht
270279
# HTTP/1.1 403 Forbidden
271280
```
272281

282+
### ❼ Create the `RateLimitPolicy`
283+
284+
```sh
285+
kubectl apply -f -<<EOF
286+
apiVersion: kuadrant.io/v1beta1
287+
kind: RateLimitPolicy
288+
metadata:
289+
name: toystore-rate-limit
290+
spec:
291+
targetRef:
292+
group: gateway.networking.k8s.io
293+
kind: HTTPRoute
294+
name: toystore
295+
rateLimits:
296+
- configurations:
297+
- actions:
298+
- metadata:
299+
descriptor_key: "userID"
300+
default_value: "no-user"
301+
metadata_key:
302+
key: "envoy.filters.http.ext_authz"
303+
path:
304+
- segment:
305+
key: "ext_auth_data"
306+
- segment:
307+
key: "userID"
308+
limits:
309+
- conditions: []
310+
maxValue: 5
311+
seconds: 10
312+
variables:
313+
- userID
314+
EOF
315+
```
316+
317+
> **Note:** It may take a couple minutes for the RateLimitPolicy to be applied depending on your cluster.
318+
319+
#### Try the API rate limited
320+
321+
Send requests as the Keycloak-authenticated user:
322+
323+
```sh
324+
while :; do curl --write-out '%{http_code}' --silent --output /dev/null -H "Authorization: Bearer $ACCESS_TOKEN" -H 'Host: api.toystore.com' http://localhost:9080/toy | egrep --color "\b(429)\b|$"; sleep 1; done
325+
```
326+
327+
Send requests as the service account:
328+
329+
```sh
330+
while :; do curl --write-out '%{http_code}' --silent --output /dev/null -H "Authorization: Bearer $SA_TOKEN" -H 'Host: api.toystore.com' http://localhost:9080/toy | egrep --color "\b(429)\b|$"; sleep 1; done
331+
```
332+
333+
Each user should be entitled to a maximum of 5 requests to the API every 10 seconds.
334+
335+
> **Note:** You may need to refresh the tokens if they are expired.
336+
273337
## Cleanup
274338

275339
```sh
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Rate limit policy to protect an API
2+
# Each user ID is entitled to a limit of 5 requests to the API every 10 seconds.
3+
apiVersion: kuadrant.io/v1beta1
4+
kind: RateLimitPolicy
5+
metadata:
6+
name: toystore-rate-limit
7+
spec:
8+
targetRef:
9+
group: gateway.networking.k8s.io
10+
kind: HTTPRoute
11+
name: toystore
12+
rateLimits:
13+
- configurations:
14+
- actions:
15+
- metadata:
16+
descriptor_key: "userID"
17+
default_value: "no-user"
18+
metadata_key:
19+
key: "envoy.filters.http.ext_authz"
20+
path:
21+
- segment:
22+
key: "ext_auth_data"
23+
- segment:
24+
key: "userID"
25+
limits:
26+
- conditions: []
27+
maxValue: 5
28+
seconds: 10
29+
variables:
30+
- userID

0 commit comments

Comments
 (0)