Skip to content

Commit 88a6a39

Browse files
authored
add request_method to rate-limit policy (#7832)
1 parent 92a1d97 commit 88a6a39

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

pkg/apis/configuration/validation/policy.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,7 @@ var rateLimitKeyVariables = map[string]bool{
585585
"request_uri": true,
586586
"uri": true,
587587
"args": true,
588+
"request_method": true,
588589
}
589590

590591
func validateRateLimitKey(key string, fieldPath *field.Path, isPlus bool) field.ErrorList {

pkg/apis/configuration/validation/policy_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,41 @@ func TestValidateRateLimit_FailsOnInvalidInput(t *testing.T) {
722722
}
723723
}
724724

725+
func TestValidateRateLimitKey(t *testing.T) {
726+
t.Parallel()
727+
728+
for varName := range rateLimitKeyVariables {
729+
keyToTest := "${" + varName + "}"
730+
testMsg := "validating key: " + keyToTest
731+
732+
t.Run(testMsg, func(t *testing.T) {
733+
t.Parallel()
734+
allErrs := validateRateLimitKey(keyToTest, field.NewPath("key"), false)
735+
if len(allErrs) > 0 {
736+
t.Errorf("validateRateLimitKey returned error for valid key %q, error %v", keyToTest, allErrs)
737+
}
738+
})
739+
}
740+
741+
t.Run("invalid unknown key", func(t *testing.T) {
742+
t.Parallel()
743+
invalidKey := "${not_a_valid_key}"
744+
allErrs := validateRateLimitKey(invalidKey, field.NewPath("key"), false)
745+
if len(allErrs) == 0 {
746+
t.Errorf("validateRateLimitKey returned no errors for an unknown key %q", invalidKey)
747+
}
748+
})
749+
750+
t.Run("empty key", func(t *testing.T) {
751+
t.Parallel()
752+
emptyKey := ""
753+
allErrs := validateRateLimitKey(emptyKey, field.NewPath("key"), false)
754+
if len(allErrs) == 0 {
755+
t.Errorf("validateRateLimitKey %q returned no errors for an empty key", emptyKey)
756+
}
757+
})
758+
}
759+
725760
func TestValidateJWT_PassesOnValidInput(t *testing.T) {
726761
t.Parallel()
727762
tests := []struct {

site/content/configuration/policy-resource.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ When the [Zone Sync feature]({{< ref "/configuration/global-configuration/config
129129
|Field | Description | Type | Required |
130130
| ---| ---| ---| --- |
131131
|``rate`` | The rate of requests permitted. The rate is specified in requests per second (r/s) or requests per minute (r/m). | ``string`` | Yes |
132-
|``key`` | The key to which the rate limit is applied. Can contain text, variables, or a combination of them. Variables must be surrounded by ``${}``. For example: ``${binary_remote_addr}``. Accepted variables are ``$binary_remote_addr``, ``$request_uri``, ``$url``, ``$http_``, ``$args``, ``$arg_``, ``$cookie_``, ``$jwt_claim_``. | ``string`` | Yes |
132+
|``key`` | The key to which the rate limit is applied. Can contain text, variables, or a combination of them. Variables must be surrounded by ``${}``. For example: ``${binary_remote_addr}``. Accepted variables are ``$binary_remote_addr``, ``$request_uri``,``$request_method``, ``$url``, ``$http_``, ``$args``, ``$arg_``, ``$cookie_``, ``$jwt_claim_``. | ``string`` | Yes |
133133
|``zoneSize`` | Size of the shared memory zone. Only positive values are allowed. Allowed suffixes are ``k`` or ``m``, if none are present ``k`` is assumed. | ``string`` | Yes |
134134
|``delay`` | The delay parameter specifies a limit at which excessive requests become delayed. If not set all excessive requests are delayed. | ``int`` | No |
135135
|``noDelay`` | Disables the delaying of excessive requests while requests are being limited. Overrides ``delay`` if both are set. | ``bool`` | No |

0 commit comments

Comments
 (0)