Skip to content

Commit c279908

Browse files
haywoodshADubhlaoichpdabelf5
authored
Documentation for jwt claim based tiered rate limit (#7419)
* update docs and crd descriptions Signed-off-by: Haywood Shannon <[email protected]> * update descriptons of fields. Signed-off-by: Haywood Shannon <[email protected]> * fix rebase error Signed-off-by: Haywood Shannon <[email protected]> * update crd Signed-off-by: Haywood Shannon <[email protected]> --------- Signed-off-by: Haywood Shannon <[email protected]> Co-authored-by: Alan Dooley <[email protected]> Co-authored-by: Paul Abel <[email protected]>
1 parent 7ed593f commit c279908

File tree

5 files changed

+75
-5
lines changed

5 files changed

+75
-5
lines changed

config/crd/bases/k8s.nginx.org_policies.yaml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,15 +184,21 @@ spec:
184184
limit policy.
185185
properties:
186186
default:
187+
description: sets the rate limit in this policy to be the
188+
default if no conditions are met. In a group of policies
189+
with the same JWT condition, only one policy can be the
190+
default.
187191
type: boolean
188192
jwt:
189-
description: JWTCondition defines a condition for a rate limit
190-
by JWT claim.
193+
description: defines a JWT condition to rate limit against.
191194
properties:
192195
claim:
196+
description: the JWT claim to be rate limit by. Nested
197+
claims should be separated by "."
193198
pattern: ^([^$\s"'])*$
194199
type: string
195200
match:
201+
description: the value of the claim to match against.
196202
pattern: ^([^$\s."'])*$
197203
type: string
198204
required:

deploy/crds.yaml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,15 +346,21 @@ spec:
346346
limit policy.
347347
properties:
348348
default:
349+
description: sets the rate limit in this policy to be the
350+
default if no conditions are met. In a group of policies
351+
with the same JWT condition, only one policy can be the
352+
default.
349353
type: boolean
350354
jwt:
351-
description: JWTCondition defines a condition for a rate limit
352-
by JWT claim.
355+
description: defines a JWT condition to rate limit against.
353356
properties:
354357
claim:
358+
description: the JWT claim to be rate limit by. Nested
359+
claims should be separated by "."
355360
pattern: ^([^$\s"'])*$
356361
type: string
357362
match:
363+
description: the value of the claim to match against.
358364
pattern: ^([^$\s."'])*$
359365
type: string
360366
required:

examples/custom-resources/rate-limit-tiered-jwt-claim/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Rate Limit JWT claim
1+
# Tiered Rate Limits with JWT Claim
22

33
In this example, we deploy a web application, configure load balancing for it via a VirtualServer, and apply two rate
44
limit Policies, grouped in a tier, using a JWT claim `sub` as the key to the rate limit and using another JWT claim

pkg/apis/configuration/v1/types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,18 +616,22 @@ type RateLimit struct {
616616

617617
// RateLimitCondition defines a condition for a rate limit policy.
618618
type RateLimitCondition struct {
619+
// defines a JWT condition to rate limit against.
619620
JWT *JWTCondition `json:"jwt"`
620621
// +kubebuilder:validation:Optional
622+
// sets the rate limit in this policy to be the default if no conditions are met. In a group of policies with the same JWT condition, only one policy can be the default.
621623
Default bool `json:"default"`
622624
}
623625

624626
// JWTCondition defines a condition for a rate limit by JWT claim.
625627
type JWTCondition struct {
626628
// +kubebuilder:validation:Required
627629
// +kubebuilder:validation:Pattern=`^([^$\s"'])*$`
630+
// the JWT claim to be rate limit by. Nested claims should be separated by "."
628631
Claim string `json:"claim"`
629632
// +kubebuilder:validation:Required
630633
// +kubebuilder:validation:Pattern=`^([^$\s."'])*$`
634+
// the value of the claim to match against.
631635
Match string `json:"match"`
632636
}
633637

site/content/configuration/policy-resource.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ The feature is implemented using the NGINX [ngx_http_limit_req_module](https://n
132132
|``logLevel`` | Sets the desired logging level for cases when the server refuses to process requests due to rate exceeding, or delays request processing. Allowed values are ``info``, ``notice``, ``warn`` or ``error``. Default is ``error``. | ``string`` | No |
133133
|``rejectCode`` | Sets the status code to return in response to rejected requests. Must fall into the range ``400..599``. Default is ``503``. | ``int`` | No |
134134
|``scale`` | Enables a constant rate-limit by dividing the configured rate by the number of nginx-ingress pods currently serving traffic. This adjustment ensures that the rate-limit remains consistent, even as the number of nginx-pods fluctuates due to autoscaling. **This will not work properly if requests from a client are not evenly distributed across all ingress pods** (Such as with sticky sessions, long lived TCP Connections with many requests, and so forth). In such cases using [zone-sync]({{< ref "/configuration/global-configuration/configmap-resource.md#zone-sync" >}}) instead would give better results. | ``bool`` | No |
135+
|``condition`` | Add a condition to a rate-limit policy. | [ratelimit.condition](#ratelimitcondition) | No |
135136
{{% /table %}}
136137

137138
{{< note >}}
@@ -152,6 +153,59 @@ policies:
152153

153154
When you reference more than one rate limit policy, NGINX Ingress Controller will configure NGINX to use all referenced rate limits. When you define multiple policies, each additional policy inherits the `dryRun`, `logLevel`, and `rejectCode` parameters from the first policy referenced (`rate-limit-policy-one`, in the example above).
154155

156+
### RateLimit.Condition
157+
158+
RateLimit.Condition defines a condition for a rate limit policy. For example:
159+
160+
```yaml
161+
condition:
162+
jwt:
163+
claim: user_details.level
164+
match: premium
165+
default: true
166+
```
167+
168+
{{% table %}}
169+
|Field | Description | Type | Required |
170+
| ---| ---| ---| --- |
171+
|``jwt`` | defines a JWT condition to rate limit against. | [ratelimit.condition.jwt](#ratelimitconditionjwt) | No |
172+
|``default`` | sets the rate limit in this policy to be the default if no conditions are met. In a group of policies with the same JWT condition, only one policy can be the default. | ``bool`` | No |
173+
{{% /table %}}
174+
175+
The rate limit policy with condition is designed to be used in combination with one or more rate limit policies. For example, multiple rate limit policies with [RateLimit.Condition.JWT](#ratelimitconditionjwt) can be used to apply different tiers of rate limit based on the value of a JWT claim. For a practical example of tiered rate limiting by the value of a JWT claim, see the example in our [GitHub repository](https://github.com/nginx/kubernetes-ingress/tree/v{{< nic-version >}}/examples/custom-resources/rate-limit-tiered-jwt-claim/README.md).
176+
177+
### RateLimit.Condition.JWT
178+
{{< note >}}
179+
180+
This feature is only available with NGINX Plus.
181+
182+
{{< /note >}}
183+
184+
RateLimit.Condition.JWT defines a condition for a rate limit by JWT claim. For example, here we define a condition for a rate limit policy that only applies to requests with a JWT claim `user_details.level` with a value `premium`:
185+
186+
```yaml
187+
jwt:
188+
claim: user_details.level
189+
match: premium
190+
```
191+
192+
The rate limit policy will only apply to requests that contain a JWT with the specified claim and value. For example, the following JWT payload will match the JWT condition:
193+
194+
```json
195+
{
196+
"user_details": {
197+
"level": "premium"
198+
},
199+
"sub": "client1"
200+
}
201+
```
202+
203+
{{% table %}}
204+
|Field | Description | Type | Required |
205+
| ---| ---| ---| --- |
206+
|``claim`` | Claim is the JWT claim to be rate limit by. Nested claims should be separated by ".". | ``string`` | Yes |
207+
|``match`` | the value of the claim to match against. | ``string`` | Yes |
208+
{{% /table %}}
155209

156210
### APIKey
157211

0 commit comments

Comments
 (0)