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

Merging to release-5-lts: [TT-10856/TT-11593]fix quota limits not working with url rewrite to self (#6133) #6168

Conversation

buger
Copy link
Member

@buger buger commented Mar 20, 2024

[TT-10856/TT-11593]fix quota limits not working with url rewrite to self (#6133)

User description

fix an issue where quota limits were not applied when URL rewrite
middleware target is using tyk://self/<target-endpoint>/

Description

Quota limits were not applied when URL rewrite middleware is enabled
with target of format tyk://self/<target-endpoint>/.
This was happening with keys created with access_rights specified for
per API basis.
Creating keys from policies also filled in access_rights in keys.
Having access_rights filled in caused quota_renews to be set to 0
during URL rewrite. This caused quota limit resets everytime.
Updating session before URL rewrite would fix this as the quota_renews
is correctly applied.
calling mw.Base().UpdateRequestSession(r) after every middleware
execution is okay because it will update session only if a session
update is scheduled.

if !ctxSessionUpdateScheduled(r) {

The quota related values returned in API response headers
X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset were
wrong since a session update was scheduled only during the first hit
after a quota reset. This PR also fixes this behaviour to report the
correct quota information in those headers.

Related Issue

https://tyktech.atlassian.net/browse/TT-10856
https://tyktech.atlassian.net/browse/TT-11593

Motivation and Context

How This Has Been Tested

Screenshots (if appropriate)

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing
    functionality to change)
  • Refactoring or add test (improvements in base code or adds test
    coverage to functionality)

Checklist

  • I ensured that the documentation is up to date
  • I explained why this PR updates go.mod in detail with reasoning
    why it's required
  • I would like a code coverage CI quality gate exception and have
    explained why

Type

bug_fix, tests


Description

  • Fixed an issue where quota limits were not applied when URL rewrite
    middleware target is using tyk://self/<endpoint>/.
  • Added a test case to ensure quota limits are correctly enforced when
    using URL rewrite to self.

Changes walkthrough

Relevant files
Bug fix
middleware.go
Ensure Session Updates After Middleware Execution               

gateway/middleware.go

  • Added call to UpdateRequestSession after middleware execution to
    ensure session updates, particularly for quota limits, are applied.
  • +1/-0     
    Tests
    middleware_test.go
    Add Test for Quota Limits with URL Rewrite to Self             

    gateway/middleware_test.go

  • Imported time package for session creation.
  • Added createSession helper function to simplify session creation for
    tests.
  • Added TestQuotaNotAppliedWithURLRewrite to validate quota limits are
    correctly applied when using URL rewrite to self.
  • +66/-0   

    PR-Agent usage:
    Comment /help on the PR to get a list of all available PR-Agent tools
    and their descriptions

    …elf (#6133)
    
    fix an issue where quota limits were not applied when URL rewrite
    middleware target is using `tyk://self/<target-endpoint>/`
    
    Quota limits were not applied when URL rewrite middleware is enabled
    with target of format `tyk://self/<target-endpoint>/`.
    This was happening with keys created with `access_rights` specified for
    per API basis.
    Creating keys from policies also filled in `access_rights` in keys.
    Having `access_rights` filled in caused `quota_renews` to be set to 0
    during URL rewrite. This caused quota limit resets everytime.
    Updating session before URL rewrite would fix this as the `quota_renews`
    is correctly applied.
    calling `mw.Base().UpdateRequestSession(r)` after every middleware
    execution is okay because it will update session only if a session
    update is scheduled.
    
    https://github.com/TykTechnologies/tyk/blob/53886d044c43930b332dbe4a73cf727069df0770/gateway/middleware.go#L329
    
    The quota related values returned in API response headers
    `X-RateLimit-Limit`, `X-RateLimit-Remaining`, `X-RateLimit-Reset` were
    wrong since a session update was scheduled only during the first hit
    after a quota reset. This PR also fixes this behaviour to report the
    correct quota information in those headers.
    
    https://tyktech.atlassian.net/browse/TT-10856
    https://tyktech.atlassian.net/browse/TT-11593
    
    <!-- Why is this change required? What problem does it solve? -->
    
    <!-- Please describe in detail how you tested your changes -->
    <!-- Include details of your testing environment, and the tests -->
    <!-- you ran to see how your change affects other areas of the code,
    etc. -->
    <!-- This information is helpful for reviewers and QA. -->
    
    <!-- What types of changes does your code introduce? Put an `x` in all
    the boxes that apply: -->
    
    - [x] Bug fix (non-breaking change which fixes an issue)
    - [ ] New feature (non-breaking change which adds functionality)
    - [ ] Breaking change (fix or feature that would cause existing
    functionality to change)
    - [ ] Refactoring or add test (improvements in base code or adds test
    coverage to functionality)
    
    <!-- Go over all the following points, and put an `x` in all the boxes
    that apply -->
    <!-- If there are no documentation updates required, mark the item as
    checked. -->
    <!-- Raise up any additional concerns not covered by the checklist. -->
    
    - [ ] I ensured that the documentation is up to date
    - [ ] I explained why this PR updates go.mod in detail with reasoning
    why it's required
    - [ ] I would like a code coverage CI quality gate exception and have
    explained why
    
    ___
    
    bug_fix, tests
    
    ___
    
    - Fixed an issue where quota limits were not applied when URL rewrite
    middleware target is using `tyk://self/<endpoint>/`.
    - Added a test case to ensure quota limits are correctly enforced when
    using URL rewrite to self.
    
    ___
    
    <table><thead><tr><th></th><th align="left">Relevant
    files</th></tr></thead><tbody><tr><td><strong>Bug
    fix</strong></td><td><table>
    <tr>
      <td>
        <details>
    <summary><strong>middleware.go</strong><dd><code>Ensure Session Updates
    After Middleware Execution</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    &nbsp; &nbsp; &nbsp; </dd></summary>
    <hr>
    
    gateway/middleware.go
    <li>Added call to <code>UpdateRequestSession</code> after middleware
    execution to <br>ensure session updates, particularly for quota limits,
    are applied.<br>
    
    </details>
    
      </td>
    <td><a
    href="https://github.com/TykTechnologies/tyk/pull/6133/files#diff-703054910891a4db633eca0f42ed779d6b4fa75cd9b3aa4c503e681364201c1b">+1/-0</a>&nbsp;
    &nbsp; &nbsp; </td>
    </tr>
    </table></td></tr><tr><td><strong>Tests</strong></td><td><table>
    <tr>
      <td>
        <details>
    <summary><strong>middleware_test.go</strong><dd><code>Add Test for Quota
    Limits with URL Rewrite to Self</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    &nbsp; &nbsp; </dd></summary>
    <hr>
    
    gateway/middleware_test.go
    <li>Imported <code>time</code> package for session creation.<br> <li>
    Added <code>createSession</code> helper function to simplify session
    creation for <br>tests.<br> <li> Added
    <code>TestQuotaNotAppliedWithURLRewrite</code> to validate quota limits
    are <br>correctly applied when using URL rewrite to self.<br>
    
    </details>
    
      </td>
    <td><a
    href="https://github.com/TykTechnologies/tyk/pull/6133/files#diff-6a09a08e3f82cc5e9d8c6b5c8426d75ea1e5d85e15ab008fca1f512e7c49c1e6">+66/-0</a>&nbsp;
    &nbsp; </td>
    </tr>
    </table></td></tr></tr></tbody></table>
    
    ___
    
    > ✨ **PR-Agent usage**:
    >Comment `/help` on the PR to get a list of all available PR-Agent tools
    and their descriptions
    
    (cherry picked from commit 3a78b0d)
    Copy link
    Contributor

    API Changes

    no api changes detected

    Copy link
    Contributor

    💥 CI tests failed 🙈

    git-state

    all ok

    Please look at the run or in the Checks tab.

    @jeffy-mathew jeffy-mathew marked this pull request as ready for review March 20, 2024 10:44
    @jeffy-mathew jeffy-mathew enabled auto-merge (squash) March 20, 2024 10:44
    Copy link

    sonarcloud bot commented Mar 20, 2024

    Quality Gate Failed Quality Gate failed

    Failed conditions
    69.1% Coverage on New Code (required ≥ 80%)

    See analysis details on SonarCloud

    @jeffy-mathew jeffy-mathew merged commit 78c891c into release-5-lts Mar 22, 2024
    28 of 29 checks passed
    @jeffy-mathew jeffy-mathew deleted the merge/release-5-lts/3a78b0d053d9f3309d9c51c79dc630b0828c793e branch March 22, 2024 09:31
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    None yet
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    2 participants