Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AND NOT style matches for RateLimiting #2193

Closed
arkodg opened this issue Nov 15, 2023 · 15 comments · Fixed by #4483
Closed

AND NOT style matches for RateLimiting #2193

arkodg opened this issue Nov 15, 2023 · 15 comments · Fixed by #4483
Assignees
Labels
area/api API-related issues area/policy kind/enhancement New feature or request
Milestone

Comments

@arkodg
Copy link
Contributor

arkodg commented Nov 15, 2023

Description:

Describe the desired behavior, what scenario it enables and how it
would be used.

Consider this config

cat <<EOF | kubectl apply -f -
apiVersion: gateway.envoyproxy.io/v1alpha1
kind: SecurityPolicy
metadata:
  name: jwt-backend
spec:
  targetRef:
    group: gateway.networking.k8s.io
    kind: HTTPRoute
    name: backend
  jwt:
    providers:
    - name: example
      remoteJWKS:
        uri: https://test.eu.auth0.com/.well-known/jwks.json
      claimToHeaders:
      - claim: email
        header: x-user-email
---
apiVersion: gateway.envoyproxy.io/v1alpha1
kind: BackendTrafficPolicy 
metadata:
  name: policy-httproute
spec:
  targetRef:
    group: gateway.networking.k8s.io
    kind: HTTPRoute
    name: backend
    namespace: envoy
  rateLimit:
    type: Global
    global:
      rules:
      - clientSelectors:
        - headers:
          - name: x-user-email
            value: [email protected]
        limit:
          requests: 10
          unit: Minute
    - clientSelectors:
      - headers:
        - type: Distinct
          name: x-claim-email
      limit:
        requests: 5
        unit: Minute
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: backend
spec:
  parentRefs:
    - name: eg
      namespace: envoy
  hostnames:
    - "www.example.com"
  rules:
    - backendRefs:
        - group: ""
          kind: Service
          name: backend
          port: 3000
          weight: 1
      matches:
        - path:
            type: PathPrefix
            value: /
EOF

I want to allow [email protected] user to 10 req/min but all other user 5 req/min.
But with this config [email protected] user is getting 429 after 5 requests.

Relates to https://envoyproxy.slack.com/archives/C03E6NHLESV/p1699784524023299

[optional Relevant Links:]

Any extra documentation required to understand the issue.

@arkodg arkodg added kind/enhancement New feature or request help wanted Extra attention is needed area/policy labels Nov 15, 2023
@arkodg
Copy link
Contributor Author

arkodg commented Nov 15, 2023

thinking out loud, this use case can be achieved in two ways

  1. by adding a dontMatch field
    - clientSelectors:
      - headers:
        - type: Distinct
          name: x-claim-email
        - type: Exact
           name: x-claim-email
           dontMatch: true
           value: [email protected]
  1. by adding a except field
    - clientSelectors:
      - headers:
        - type: Distinct
          name: x-claim-email
         except:
           headers:    
           - type: Exact
              name: x-claim-email
              value: [email protected]

this api field would decide the value of expect_match in https://www.envoyproxy.io/docs/envoy/latest/api-v3/config/route/v3/route_components.proto#config-route-v3-ratelimit-action-headervaluematch

@arkodg arkodg added the area/api API-related issues label Nov 15, 2023
@slayer321
Copy link
Contributor

I'm new to this part of the code , will like to learn an implement this feature.
/assign

@arkodg arkodg removed the help wanted Extra attention is needed label Nov 16, 2023
@arkodg
Copy link
Contributor Author

arkodg commented Nov 16, 2023

ptal @envoyproxy/gateway-maintainers

Copy link

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

@arkodg
Copy link
Contributor Author

arkodg commented Sep 3, 2024

hey @slayer321 still planning on working on this issue ?

@arkodg arkodg removed the stale label Sep 3, 2024
@arkodg arkodg added this to the v1.2.0-rc1 milestone Sep 3, 2024
@slayer321
Copy link
Contributor

Hey @arkodg , currently stuck with some other stuff .. will not be able to work on this issue as of now.

@slayer321 slayer321 removed their assignment Sep 4, 2024
@arkodg arkodg added the help wanted Extra attention is needed label Sep 4, 2024
@rudrakhp
Copy link
Contributor

rudrakhp commented Sep 6, 2024

@arkodg interested in picking up this issue, thanks!

@arkodg
Copy link
Contributor Author

arkodg commented Sep 6, 2024

great thanks @rudrakhp, suggest breaking up the work into multiple PRs (API, Implementation, Docs/E2E)

@rudrakhp
Copy link
Contributor

rudrakhp commented Sep 7, 2024

@arkodg raised API PR to support this in both header and source matches. Thought it might be easier to do so when dealing with scenarios where subset IP CIDRs might be exempted or have different rate limits. Preferred the dontMatch approach, went with another name though. Please do review, thanks!

Copy link

github-actions bot commented Oct 7, 2024

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

@github-actions github-actions bot added the stale label Oct 7, 2024
@arkodg arkodg modified the milestones: v1.2.0-rc1, v1.2.0 Oct 15, 2024
@arkodg
Copy link
Contributor Author

arkodg commented Oct 15, 2024

keeping this issue open to track completion of docs and e2e

@rudrakhp
Copy link
Contributor

@arkodg E2E tests in PR #4452 is failing with following reason:

2024-10-16T11:30:45.0052006Z          Error:        Received unexpected error:
2024-10-16T11:30:45.0055244Z                        BackendTrafficPolicy.gateway.envoyproxy.io "ratelimit-anded-headers-with-invert" is invalid: spec.rateLimit.global.rules[0].clientSelectors[0].headers[1]: Duplicate value: map[string]interface {}{"name":"x-user-name"}

I noticed that the HeaderMatch list is defined as a +listType=map in CEL validations, with +listMapKey=name. This restricts rate limit filters with multiple header match filters with same name (example below).

  rateLimit:
    type: Global
    global:
      rules:
        - clientSelectors:
            - headers:
                - name: x-user-name
                  type: Distinct
                - name: x-user-name
                  type: Exact
                  value: admin
                  invert: true
          limit:
            requests: 3
            unit: Hour

I think a API fix is needed to change the +listType=set, unless there was a specific reason map was chosen?

PS: I would also remove invert in SourceCIDRMatch since the xDS APIs don't provide a means to do that today. Playing around with CIDR ranges would probably render invert redundant in this case.

@arkodg
Copy link
Contributor Author

arkodg commented Oct 16, 2024

thanks for catching this @rudrakhp

@rudrakhp
Copy link
Contributor

rudrakhp commented Oct 17, 2024

Thanks for confirming, also not sure why we need a status check as Invert is an attribute of HeaderMatch and cannot be found in SourceMatch type after I remove it from the API spec (check the API fix PR).

@arkodg
Copy link
Contributor Author

arkodg commented Oct 17, 2024

Thanks for confirming, also not sure why we need a status check as Invert is an attribute of HeaderMatch and cannot be found in SourceMatch type after I remove it from the API spec (check the API fix PR).

Ah in that case, let's skip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/api API-related issues area/policy kind/enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants