gateway icon indicating copy to clipboard operation
gateway copied to clipboard

Set ratelimiting to all cidrs with exceptions

Open zetaab opened this issue 2 years ago • 21 comments

Description:

I have usecase that I should add ratelimiting to all cidrs EXCEPT few "whitelisted ips". As I see it, it is quite common use case that people want to set rate limits to everything except few internal api ips (whitelist services that should work always, no matter what).

Relevant Links:

https://www.envoyproxy.io/docs/envoy/latest/api-v3/config/route/v3/route_components.proto#envoy-v3-api-msg-config-route-v3-ratelimit-action envoy does not support any complex logic in this field. So envoy changes needed as well

zetaab avatar Oct 04 '24 05:10 zetaab

envoy issue https://github.com/envoyproxy/envoy/issues/36442

zetaab avatar Oct 04 '24 05:10 zetaab

@envoyproxy/gateway-maintainers / @zetaab / @arkodg Do you have any idea why this does not work? this is built following the reference here

The idea is to set a lower rate limit for certain CIDR blocks and for rest define a higher limit.

---

apiVersion: gateway.envoyproxy.io/v1alpha1
kind: BackendTrafficPolicy
metadata:
  name: api--rate-limit-all
spec:
  targetRef:
    group: gateway.networking.k8s.io
    kind: HTTPRoute
    name: api--http-route
  rateLimit:
    type: Global
    global:
      rules:
        - limit:
            requests: 100
            unit: Second
        - limit:
            requests: 500
            unit: Minute

---

apiVersion: gateway.envoyproxy.io/v1alpha1
kind: BackendTrafficPolicy
metadata:
  name: api--limit-whitelist
spec:
  targetRef:
    group: gateway.networking.k8s.io
    kind: HTTPRoute
    name: api--http-route
  rateLimit:
    type: Global
    global:
      rules:
        - clientSelectors:
          - sourceCIDR:
              value: x.y.z.r/30
              type: Distinct
          - sourceCIDR:
              value: x.y.x.r/32
          - sourceCIDR:
              value: x.y.y.r/32
          - sourceCIDR:
              value: x.y.r.r/32
          - sourceCIDR:
              value: y.y.x.r/31
              type: Distinct
          limit:
            requests: 10000
            unit: Minute

---

apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: api--http-route
spec:
  parentRefs:
    - name: external-gateway
  hostnames:
    - example.com
  rules:
    - matches:
      - path:
          type: PathPrefix
          value: /
      filters:
        - type: ExtensionRef
          extensionRef:
            group: gateway.envoyproxy.io
            kind: BackendTrafficPolicy
            name: api--limit-whitelist
      backendRefs:
        - name: api--service
          port: 3000
    - matches:
      - path:
          type: PathPrefix
          value: /
      filters:
        - type: ExtensionRef
          extensionRef:
            group: gateway.envoyproxy.io
            kind: BackendTrafficPolicy
            name: api--rate-limit-all
      backendRefs:
        - name: api--service
          port: 3000

avinandi avatar Oct 28 '24 21:10 avinandi

This issue has been automatically marked as stale because it has not had activity in the last 30 days.

github-actions[bot] avatar Nov 28 '24 00:11 github-actions[bot]

still valid

zetaab avatar Nov 28 '24 07:11 zetaab

This issue has been automatically marked as stale because it has not had activity in the last 30 days.

github-actions[bot] avatar Dec 28 '24 08:12 github-actions[bot]

not stale

zetaab avatar Feb 04 '25 16:02 zetaab

This issue has been automatically marked as stale because it has not had activity in the last 30 days.

github-actions[bot] avatar Mar 10 '25 04:03 github-actions[bot]

Still valid issue

avinandi avatar Mar 10 '25 08:03 avinandi

This issue has been automatically marked as stale because it has not had activity in the last 30 days.

github-actions[bot] avatar Apr 09 '25 12:04 github-actions[bot]

still valid

W4SD avatar Apr 17 '25 07:04 W4SD

@nezdolik is it easier to implement this in the RL Filter than implement IP Tagging for this use case ?

arkodg avatar Apr 17 '25 17:04 arkodg

not sure why https://github.com/envoyproxy/envoy/issues/36442 was closed as not planned, i think is doable in Envoy with ip tagging + ratelimit filters.

nezdolik avatar Apr 21 '25 21:04 nezdolik

@arkodg you are asking about implementation in Envoy Gateway right? For the mentioned above use case can be simpler to just implement in RL filter. But we for example need dynamic and large in size structures of CIDR ranges. Ip tagging is also useful in conjunction with RBAC, is a common use case.

nezdolik avatar Apr 21 '25 21:04 nezdolik

for this specific case, the Envoy RL Filter doesn't support , it supports it for the headerValueMatch action https://www.envoyproxy.io/docs/envoy/latest/api-v3/config/route/v3/route_components.proto#config-route-v3-ratelimit-action-headervaluematch (called expectMatch) but not for remote_address https://www.envoyproxy.io/docs/envoy/latest/api-v3/config/route/v3/route_components.proto#envoy-v3-api-msg-config-route-v3-ratelimit-action-remoteaddress.

since EG's API already has the cidr noun for RL and Authz, imo this (enhancement in Envoy proxy + EG) seems like a simpler task

if users are more comfortable and familiar with the ip tagging approach, then that needs to be exposed in EG

arkodg avatar Apr 21 '25 21:04 arkodg

This issue has been automatically marked as stale because it has not had activity in the last 30 days.

github-actions[bot] avatar May 24 '25 04:05 github-actions[bot]

still valid

kraashen avatar Jul 29 '25 17:07 kraashen

@arkodg from what I gather from the above conversation here, there are two options to implement this:

  1. Go with IPTagging + HeaderValueMatch with expectMatch=false against tag values in the IP tag header
  2. Implement expectMatch for RemoteAddress and MaskedRemoteAddress rate limit actions in the proxy and use that instead

Looks like the second option might be the right thing to do here.

rudrakhp avatar Jan 01 '26 09:01 rudrakhp

+1 for option2

zirain avatar Jan 01 '26 10:01 zirain

cc: @envoyproxy/gateway-maintainers for any opinions

rudrakhp avatar Jan 01 '26 10:01 rudrakhp

I was earlier leaning towards 2. but 1. seems to be useful for other use cases too like CIDR based routing (IPTagging + HeaderValueMatch for route match)

arkodg avatar Jan 01 '26 19:01 arkodg