Skip to content

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

Merged
jeffy-mathew merged 2 commits into
masterfrom
fix/TT-10856/quota-not-working-with-url-rewrite-to-self
Mar 13, 2024
Merged

[TT-10856/TT-11593]fix quota limits not working with url rewrite to self#6133
jeffy-mathew merged 2 commits into
masterfrom
fix/TT-10856/quota-not-working-with-url-rewrite-to-self

Conversation

@jeffy-mathew

@jeffy-mathew jeffy-mathew commented Mar 10, 2024

Copy link
Copy Markdown
Contributor

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

    @github-actions

    Copy link
    Copy Markdown
    Contributor

    PR Description updated to latest commit (2f4d4fa)

    @github-actions

    Copy link
    Copy Markdown
    Contributor

    API Changes

    no api changes detected

    @github-actions

    Copy link
    Copy Markdown
    Contributor

    PR Review

    ⏱️ Estimated effort to review [1-5]

    3, because the PR includes both a bug fix and an addition of a test case. The logic changes are not extensive but require a good understanding of the existing middleware and session management system to ensure the fix does not introduce new issues.

    🧪 Relevant tests

    Yes

    🔍 Possible issues

    Possible Bug: The UpdateRequestSession method is called without checking if the session update is necessary. This could lead to unnecessary session updates, impacting performance.

    🔒 Security concerns

    No

    Code feedback:
    relevant filegateway/middleware.go
    suggestion      

    Consider adding a condition to check if the session needs to be updated before calling mw.Base().UpdateRequestSession(r). This could improve performance by avoiding unnecessary session updates. [important]

    relevant linemw.Base().UpdateRequestSession(r)

    relevant filegateway/middleware_test.go
    suggestion      

    It's recommended to add a cleanup step to remove or reset the session created during the test to prevent side effects on other tests. This can be achieved by using defer to call a cleanup function after creating the session. [medium]

    relevant lineassert.NoError(t, ts.Gw.GlobalSessionManager.UpdateSession(authKey, session, 60, false))


    ✨ Review tool usage guide:

    Overview:
    The review tool scans the PR code changes, and generates a PR review. The tool can be triggered automatically every time a new PR is opened, or can be invoked manually by commenting on any PR.
    When commenting, to edit configurations related to the review tool (pr_reviewer section), use the following template:

    /review --pr_reviewer.some_config1=... --pr_reviewer.some_config2=...
    

    With a configuration file, use the following template:

    [pr_reviewer]
    some_config1=...
    some_config2=...
    
    Utilizing extra instructions

    The review tool can be configured with extra instructions, which can be used to guide the model to a feedback tailored to the needs of your project.

    Be specific, clear, and concise in the instructions. With extra instructions, you are the prompter. Specify the relevant sub-tool, and the relevant aspects of the PR that you want to emphasize.

    Examples for extra instructions:

    [pr_reviewer] # /review #
    extra_instructions="""
    In the 'possible issues' section, emphasize the following:
    - Does the code logic cover relevant edge cases?
    - Is the code logic clear and easy to understand?
    - Is the code logic efficient?
    ...
    """
    

    Use triple quotes to write multi-line instructions. Use bullet points to make the instructions more readable.

    How to enable\disable automation
    • When you first install PR-Agent app, the default mode for the review tool is:
    pr_commands = ["/review", ...]
    

    meaning the review tool will run automatically on every PR, with the default configuration.
    Edit this field to enable/disable the tool, or to change the used configurations

    Auto-labels

    The review tool can auto-generate two specific types of labels for a PR:

    • a possible security issue label, that detects possible security issues (enable_review_labels_security flag)
    • a Review effort [1-5]: x label, where x is the estimated effort to review the PR (enable_review_labels_effort flag)
    Extra sub-tools

    The review tool provides a collection of possible feedbacks about a PR.
    It is recommended to review the possible options, and choose the ones relevant for your use case.
    Some of the feature that are disabled by default are quite useful, and should be considered for enabling. For example:
    require_score_review, require_soc2_ticket, and more.

    Auto-approve PRs

    By invoking:

    /review auto_approve
    

    The tool will automatically approve the PR, and add a comment with the approval.

    To ensure safety, the auto-approval feature is disabled by default. To enable auto-approval, you need to actively set in a pre-defined configuration file the following:

    [pr_reviewer]
    enable_auto_approval = true
    

    (this specific flag cannot be set with a command line argument, only in the configuration file, committed to the repository)

    You can also enable auto-approval only if the PR meets certain requirements, such as that the estimated_review_effort is equal or below a certain threshold, by adjusting the flag:

    [pr_reviewer]
    maximal_review_effort = 5
    
    More PR-Agent commands

    To invoke the PR-Agent, add a comment using one of the following commands:

    • /review: Request a review of your Pull Request.
    • /describe: Update the PR title and description based on the contents of the PR.
    • /improve [--extended]: Suggest code improvements. Extended mode provides a higher quality feedback.
    • /ask <QUESTION>: Ask a question about the PR.
    • /update_changelog: Update the changelog based on the PR's contents.
    • /add_docs 💎: Generate docstring for new components introduced in the PR.
    • /generate_labels 💎: Generate labels for the PR based on the PR's contents.
    • /analyze 💎: Automatically analyzes the PR, and presents changes walkthrough for each component.

    See the tools guide for more details.
    To list the possible configuration parameters, add a /config comment.

    See the review usage page for a comprehensive guide on using this tool.

    @github-actions

    github-actions Bot commented Mar 10, 2024

    Copy link
    Copy Markdown
    Contributor

    PR Code Suggestions

    CategorySuggestions                                                                                                                                                       
    Best practice
    Add error handling for UpdateRequestSession call.

    Consider checking for errors when calling UpdateRequestSession. If UpdateRequestSession
    encounters an issue, it should be handled appropriately rather than ignoring the potential
    error. This ensures that any problems with updating the request session are caught and can
    be dealt with in a manner that doesn't silently fail or introduce unexpected behavior.

    gateway/middleware.go [196]

    -mw.Base().UpdateRequestSession(r)
    +if err := mw.Base().UpdateRequestSession(r); err != nil {
    +    // Handle error, for example, log it or return an error response
    +    mw.Logger().WithError(err).Error("Failed to update request session")
    +}
     
    Enhancement
    Use mock time for predictable time-dependent tests.

    To ensure the test TestQuotaNotAppliedWithURLRewrite accurately reflects real-world
    scenarios and time-dependent behavior, consider using a mock time or a time manipulation
    library. This approach allows for more predictable and reliable testing outcomes,
    especially when dealing with time-sensitive functionality like quotas and expiration.

    gateway/middleware_test.go [413-414]

    -DateCreated: time.Now().Add(time.Hour * -1),
    -Expires:     time.Now().Add(time.Hour).Unix(),
    +// Assuming a mock time setup is available
    +mockTime := time.Date(2020, 1, 1, 12, 0, 0, 0, time.UTC)
    +DateCreated: mockTime.Add(time.Hour * -1),
    +Expires:     mockTime.Add(time.Hour).Unix(),
     
    Add more assertions to verify API behavior comprehensively.

    To enhance the robustness of the test TestQuotaNotAppliedWithURLRewrite, consider adding
    more assertions to verify not just the HTTP status codes but also the response body or
    headers. This ensures that the API's behavior is as expected, beyond just the status code,
    providing a more comprehensive test coverage.

    gateway/middleware_test.go [456-472]

    -_, _ = ts.Run(t, []test.TestCase{
    +_, responses := ts.Run(t, []test.TestCase{
         {
             Headers: authorization,
             Path:    "/quota-test/abc",
             Code:    http.StatusOK,
         },
         {
             Headers: authorization,
             Path:    "/quota-test/abc",
             Code:    http.StatusOK,
         },
         {
             Headers: authorization,
             Path:    "/quota-test/abc",
             Code:    http.StatusForbidden,
         },
     }...)
    +// Example additional assertion
    +for _, resp := range responses {
    +    assert.Contains(t, resp.Body, "expected content in response")
    +}
     
    Maintainability
    Separate API spec setup from test case execution for clarity.

    For improved test clarity and maintainability, consider separating the setup of the API
    spec from the test case execution in TestQuotaNotAppliedWithURLRewrite. This separation
    makes the test easier to read and understand, especially for new contributors or when
    revisiting the test in the future.

    gateway/middleware_test.go [436-446]

    -specs := ts.Gw.BuildAndLoadAPI(func(spec *APISpec) {
    -    spec.Proxy.ListenPath = "/quota-test"
    -    spec.UseKeylessAccess = false
    -    UpdateAPIVersion(spec, "Default", func(v *apidef.VersionInfo) {
    -        v.ExtendedPaths.URLRewrite = []apidef.URLRewriteMeta{{
    -            Path:         "/abc",
    -            Method:       http.MethodGet,
    -            MatchPattern: "/abc",
    -            RewriteTo:    "tyk://self/anything",
    -        }}
    -    })
    -})
    +// Separate API spec setup for clarity
    +setupAPISpec := func() *APISpec {
    +    return &APISpec{
    +        Proxy: ProxyDefinition{
    +            ListenPath: "/quota-test",
    +        },
    +        UseKeylessAccess: false,
    +        Versions: map[string]*VersionInfo{
    +            "Default": {
    +                ExtendedPaths: ExtendedPathsSet{
    +                    URLRewrite: []URLRewriteMeta{{
    +                        Path:         "/abc",
    +                        Method:       http.MethodGet,
    +                        MatchPattern: "/abc",
    +                        RewriteTo:    "tyk://self/anything",
    +                    }},
    +                },
    +            },
    +        },
    +    }
    +}
    +specs := ts.Gw.BuildAndLoadAPI(setupAPISpec)
     
    Refactor repeated authorization header setup for cleaner test cases.

    To improve code readability and maintainability, consider refactoring the repeated
    authorization header setup in TestQuotaNotAppliedWithURLRewrite into a single variable or
    function. This reduces redundancy and makes the test cases cleaner.

    gateway/middleware_test.go [453-455]

    -authorization := map[string]string{
    -    "Authorization": authKey,
    +// Refactored to a function for dynamic header generation
    +getAuthorizationHeader := func(key string) map[string]string {
    +    return map[string]string{
    +        "Authorization": key,
    +    }
     }
    +// Usage
    +_, _ = ts.Run(t, []test.TestCase{
    +    {
    +        Headers: getAuthorizationHeader(authKey),
    +        Path:    "/quota-test/abc",
    +        Code:    http.StatusOK,
    +    },
    +    ...
    +}...)
     

    ✨ Improve tool usage guide:

    Overview:
    The improve tool scans the PR code changes, and automatically generates suggestions for improving the PR code. The tool can be triggered automatically every time a new PR is opened, or can be invoked manually by commenting on a PR.
    When commenting, to edit configurations related to the improve tool (pr_code_suggestions section), use the following template:

    /improve --pr_code_suggestions.some_config1=... --pr_code_suggestions.some_config2=...
    

    With a configuration file, use the following template:

    [pr_code_suggestions]
    some_config1=...
    some_config2=...
    
    Enabling\disabling automation

    When you first install the app, the default mode for the improve tool is:

    pr_commands = ["/improve --pr_code_suggestions.summarize=true", ...]
    

    meaning the improve tool will run automatically on every PR, with summarization enabled. Delete this line to disable the tool from running automatically.

    Utilizing extra instructions

    Extra instructions are very important for the improve tool, since they enable to guide the model to suggestions that are more relevant to the specific needs of the project.

    Be specific, clear, and concise in the instructions. With extra instructions, you are the prompter. Specify relevant aspects that you want the model to focus on.

    Examples for extra instructions:

    [pr_code_suggestions] # /improve #
    extra_instructions="""
    Emphasize the following aspects:
    - Does the code logic cover relevant edge cases?
    - Is the code logic clear and easy to understand?
    - Is the code logic efficient?
    ...
    """
    

    Use triple quotes to write multi-line instructions. Use bullet points to make the instructions more readable.

    A note on code suggestions quality
    • While the current AI for code is getting better and better (GPT-4), it's not flawless. Not all the suggestions will be perfect, and a user should not accept all of them automatically.
    • Suggestions are not meant to be simplistic. Instead, they aim to give deep feedback and raise questions, ideas and thoughts to the user, who can then use his judgment, experience, and understanding of the code base.
    • Recommended to use the 'extra_instructions' field to guide the model to suggestions that are more relevant to the specific needs of the project, or use the custom suggestions 💎 tool
    • With large PRs, best quality will be obtained by using 'improve --extended' mode.
    More PR-Agent commands

    To invoke the PR-Agent, add a comment using one of the following commands:

    • /review: Request a review of your Pull Request.
    • /describe: Update the PR title and description based on the contents of the PR.
    • /improve [--extended]: Suggest code improvements. Extended mode provides a higher quality feedback.
    • /ask <QUESTION>: Ask a question about the PR.
    • /update_changelog: Update the changelog based on the PR's contents.
    • /add_docs 💎: Generate docstring for new components introduced in the PR.
    • /generate_labels 💎: Generate labels for the PR based on the PR's contents.
    • /analyze 💎: Automatically analyzes the PR, and presents changes walkthrough for each component.

    See the tools guide for more details.
    To list the possible configuration parameters, add a /config comment.

    See the improve usage page for a more comprehensive guide on using this tool.

    @github-actions

    Copy link
    Copy Markdown
    Contributor

    💥 CI tests failed 🙈

    git-state

    all ok

    Please look at the run or in the Checks tab.

    @buger

    buger commented Mar 10, 2024

    Copy link
    Copy Markdown
    Member

    API tests result - postgres15-sha256 env: success
    Branch used: refs/pull/6133/merge
    Commit: 068630a
    Triggered by: pull_request (@jeffy-mathew)
    Execution page

    @buger

    buger commented Mar 10, 2024

    Copy link
    Copy Markdown
    Member

    API tests result - mongo44-sha256 env: success
    Branch used: refs/pull/6133/merge
    Commit: 068630a
    Triggered by: pull_request (@jeffy-mathew)
    Execution page

    @jeffy-mathew
    jeffy-mathew force-pushed the fix/TT-10856/quota-not-working-with-url-rewrite-to-self branch from 2f4d4fa to 068630a Compare March 11, 2024 08:55
    @jeffy-mathew
    jeffy-mathew force-pushed the fix/TT-10856/quota-not-working-with-url-rewrite-to-self branch from 76b7449 to b8c773e Compare March 13, 2024 12:02
    @github-actions

    Copy link
    Copy Markdown
    Contributor

    💥 CI tests failed 🙈

    git-state

    diff --git a/gateway/reverse_proxy_test.go b/gateway/reverse_proxy_test.go
    index 11ea2e0..7ebe94c 100644
    --- a/gateway/reverse_proxy_test.go
    +++ b/gateway/reverse_proxy_test.go
    @@ -19,10 +19,11 @@ import (
     	textTemplate "text/template"
     	"time"
     
    -	"github.com/TykTechnologies/tyk/header"
     	"github.com/stretchr/testify/assert"
     	"github.com/stretchr/testify/require"
     
    +	"github.com/TykTechnologies/tyk/header"
    +
     	"github.com/TykTechnologies/graphql-go-tools/pkg/execution/datasource"
     	"github.com/TykTechnologies/graphql-go-tools/pkg/graphql"
     

    Please look at the run or in the Checks tab.

    1 similar comment
    @github-actions

    Copy link
    Copy Markdown
    Contributor

    💥 CI tests failed 🙈

    git-state

    diff --git a/gateway/reverse_proxy_test.go b/gateway/reverse_proxy_test.go
    index 11ea2e0..7ebe94c 100644
    --- a/gateway/reverse_proxy_test.go
    +++ b/gateway/reverse_proxy_test.go
    @@ -19,10 +19,11 @@ import (
     	textTemplate "text/template"
     	"time"
     
    -	"github.com/TykTechnologies/tyk/header"
     	"github.com/stretchr/testify/assert"
     	"github.com/stretchr/testify/require"
     
    +	"github.com/TykTechnologies/tyk/header"
    +
     	"github.com/TykTechnologies/graphql-go-tools/pkg/execution/datasource"
     	"github.com/TykTechnologies/graphql-go-tools/pkg/graphql"
     

    Please look at the run or in the Checks tab.

    @github-actions

    Copy link
    Copy Markdown
    Contributor

    💥 CI tests failed 🙈

    git-state

    diff --git a/gateway/reverse_proxy_test.go b/gateway/reverse_proxy_test.go
    index 11ea2e0..7ebe94c 100644
    --- a/gateway/reverse_proxy_test.go
    +++ b/gateway/reverse_proxy_test.go
    @@ -19,10 +19,11 @@ import (
     	textTemplate "text/template"
     	"time"
     
    -	"github.com/TykTechnologies/tyk/header"
     	"github.com/stretchr/testify/assert"
     	"github.com/stretchr/testify/require"
     
    +	"github.com/TykTechnologies/tyk/header"
    +
     	"github.com/TykTechnologies/graphql-go-tools/pkg/execution/datasource"
     	"github.com/TykTechnologies/graphql-go-tools/pkg/graphql"
     

    Please look at the run or in the Checks tab.

    1 similar comment
    @github-actions

    Copy link
    Copy Markdown
    Contributor

    💥 CI tests failed 🙈

    git-state

    diff --git a/gateway/reverse_proxy_test.go b/gateway/reverse_proxy_test.go
    index 11ea2e0..7ebe94c 100644
    --- a/gateway/reverse_proxy_test.go
    +++ b/gateway/reverse_proxy_test.go
    @@ -19,10 +19,11 @@ import (
     	textTemplate "text/template"
     	"time"
     
    -	"github.com/TykTechnologies/tyk/header"
     	"github.com/stretchr/testify/assert"
     	"github.com/stretchr/testify/require"
     
    +	"github.com/TykTechnologies/tyk/header"
    +
     	"github.com/TykTechnologies/graphql-go-tools/pkg/execution/datasource"
     	"github.com/TykTechnologies/graphql-go-tools/pkg/graphql"
     

    Please look at the run or in the Checks tab.

    @jeffy-mathew
    jeffy-mathew requested a review from andrei-tyk March 13, 2024 12:05
    @jeffy-mathew
    jeffy-mathew force-pushed the fix/TT-10856/quota-not-working-with-url-rewrite-to-self branch from b8c773e to 0565ec1 Compare March 13, 2024 12:09
    @sonarqubecloud

    Copy link
    Copy Markdown

    @jeffy-mathew jeffy-mathew changed the title [TT-10856]fix quota limits not working with url rewrite to self [TT-10856/TT-11593]fix quota limits not working with url rewrite to self Mar 13, 2024
    @jeffy-mathew
    jeffy-mathew merged commit 3a78b0d into master Mar 13, 2024
    @jeffy-mathew
    jeffy-mathew deleted the fix/TT-10856/quota-not-working-with-url-rewrite-to-self branch March 13, 2024 14:21
    @jeffy-mathew

    Copy link
    Copy Markdown
    Contributor Author

    /release to release-5.3

    @jeffy-mathew

    Copy link
    Copy Markdown
    Contributor Author

    /release to release-5.3.0

    @tykbot

    tykbot Bot commented Mar 13, 2024

    Copy link
    Copy Markdown

    Working on it! Note that it can take a few minutes.

    1 similar comment
    @tykbot

    tykbot Bot commented Mar 13, 2024

    Copy link
    Copy Markdown

    Working on it! Note that it can take a few minutes.

    tykbot Bot pushed a commit that referenced this pull request Mar 13, 2024
    …elf (#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.
    
    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.
    
    ## Related Issue
    https://tyktech.atlassian.net/browse/TT-10856
    https://tyktech.atlassian.net/browse/TT-11593
    ## Motivation and Context
    
    <!-- Why is this change required? What problem does it solve? -->
    
    ## How This Has Been Tested
    
    <!-- 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. -->
    
    ## Screenshots (if appropriate)
    
    ## Types of changes
    
    <!-- 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)
    
    ## Checklist
    
    <!-- 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
    
    
    ___
    
    ## **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**
    <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)
    @tykbot

    tykbot Bot commented Mar 13, 2024

    Copy link
    Copy Markdown

    @jeffy-mathew Seems like there is conflict and it require manual merge.

    @tykbot

    tykbot Bot commented Mar 13, 2024

    Copy link
    Copy Markdown

    Still working...

    tykbot Bot pushed a commit that referenced this pull request Mar 13, 2024
    …elf (#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.
    
    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.
    
    ## Related Issue
    https://tyktech.atlassian.net/browse/TT-10856
    https://tyktech.atlassian.net/browse/TT-11593
    ## Motivation and Context
    
    <!-- Why is this change required? What problem does it solve? -->
    
    ## How This Has Been Tested
    
    <!-- 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. -->
    
    ## Screenshots (if appropriate)
    
    ## Types of changes
    
    <!-- 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)
    
    ## Checklist
    
    <!-- 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
    
    
    ___
    
    ## **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**
    <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)
    buger added a commit that referenced this pull request Mar 13, 2024
    …king with url rewrite to self (#6133)
    
    [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.
    
    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.
    
    ## Related Issue
    https://tyktech.atlassian.net/browse/TT-10856
    https://tyktech.atlassian.net/browse/TT-11593
    ## Motivation and Context
    
    <!-- Why is this change required? What problem does it solve? -->
    
    ## How This Has Been Tested
    
    <!-- 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. -->
    
    ## Screenshots (if appropriate)
    
    ## Types of changes
    
    <!-- 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)
    
    ## Checklist
    
    <!-- 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
    
    
    ___
    
    ## **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**
    <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
    @tykbot

    tykbot Bot commented Mar 13, 2024

    Copy link
    Copy Markdown

    @jeffy-mathew Succesfully merged PR

    jeffy-mathew added a commit that referenced this pull request Mar 13, 2024
    …ng with url rewrite to self (#6133) (#6143)
    
    ## **User description**
    [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.
    
    
    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.
    
    ## Related Issue
    https://tyktech.atlassian.net/browse/TT-10856
    https://tyktech.atlassian.net/browse/TT-11593
    ## Motivation and Context
    
    <!-- Why is this change required? What problem does it solve? -->
    
    ## How This Has Been Tested
    
    <!-- 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. -->
    
    ## Screenshots (if appropriate)
    
    ## Types of changes
    
    <!-- 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)
    
    ## Checklist
    
    <!-- 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
    
    
    ___
    
    ## **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**
    <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
    
    
    ___
    
    ## **Type**
    bug_fix, tests
    
    
    ___
    
    ## **Description**
    - Fixed an issue where quota limits were not correctly applied when
    using URL rewrite to self (`tyk://self/<target-endpoint>/`).
    - Added tests to ensure quota limits are correctly applied and that the
    correct quota information is returned in response headers.
    - Refactored session update scheduling in the quota check to ensure
    correct behavior.
    
    
    ___
    
    
    
    ## **Changes walkthrough**
    <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 correct session
    update for quota limits</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></summary>
    <hr>
    
    gateway/middleware.go
    <li>Update session before proceeding to the next middleware to ensure
    <br>quota limits are correctly applied.<br>
    
    
    </details>
        
    
      </td>
    <td><a
    href="https://github.com/TykTechnologies/tyk/pull/6143/files#diff-703054910891a4db633eca0f42ed779d6b4fa75cd9b3aa4c503e681364201c1b">+1/-2</a>&nbsp;
    &nbsp; &nbsp; </td>
    </tr>                    
    
    <tr>
      <td>
        <details>
    <summary><strong>session_manager.go</strong><dd><code>Refactor session
    update scheduling for quota checks</code>&nbsp; &nbsp; &nbsp; &nbsp;
    &nbsp; &nbsp; </dd></summary>
    <hr>
    
    gateway/session_manager.go
    <li>Moved session update scheduling to ensure it's always executed when
    <br>checking for quota exceeded.<br> <li> This change ensures that quota
    renewal information is correctly <br>updated.<br>
    
    
    </details>
        
    
      </td>
    <td><a
    href="https://github.com/TykTechnologies/tyk/pull/6143/files#diff-e6b40a285464cd86736e970c4c0b320b44c75b18b363d38c200e9a9d36cdabb6">+4/-1</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>Test for quota
    limits with URL rewrite to self</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></summary>
    <hr>
    
    gateway/middleware_test.go
    <li>Added a test to verify that quota limits are correctly applied when
    <br>URL rewrite to self is used.<br> <li> Introduced a helper function
    to create a session with quota limits for <br>testing.<br>
    
    
    </details>
        
    
      </td>
    <td><a
    href="https://github.com/TykTechnologies/tyk/pull/6143/files#diff-6a09a08e3f82cc5e9d8c6b5c8426d75ea1e5d85e15ab008fca1f512e7c49c1e6">+66/-0</a>&nbsp;
    &nbsp; </td>
    </tr>                    
    
    <tr>
      <td>
        <details>
    <summary><strong>reverse_proxy_test.go</strong><dd><code>Test for
    correct quota information in response headers</code>&nbsp; &nbsp; &nbsp;
    </dd></summary>
    <hr>
    
    gateway/reverse_proxy_test.go
    <li>Added tests to verify correct quota information in response
    headers.<br> <li> Utilized helper function to create test sessions with
    quota.<br>
    
    
    </details>
        
    
      </td>
    <td><a
    href="https://github.com/TykTechnologies/tyk/pull/6143/files#diff-ce040f6555143f760fba6059744bc600b6954f0966dfb0fa2832b5eabf7a3c3f">+49/-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
    
    Co-authored-by: Jeffy Mathew <[email protected]>
    jeffy-mathew added a commit that referenced this pull request Mar 20, 2024
    @jeffy-mathew

    Copy link
    Copy Markdown
    Contributor Author

    /release to release-5-lts

    @tykbot

    tykbot Bot commented Mar 20, 2024

    Copy link
    Copy Markdown

    Working on it! Note that it can take a few minutes.

    @tykbot

    tykbot Bot commented Mar 20, 2024

    Copy link
    Copy Markdown

    Still working...

    tykbot Bot pushed a commit that referenced this pull request Mar 20, 2024
    …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)
    @tykbot

    tykbot Bot commented Mar 20, 2024

    Copy link
    Copy Markdown

    @jeffy-mathew Succesfully merged PR

    jeffy-mathew added a commit that referenced this pull request Mar 22, 2024
    …king with url rewrite to self (#6133) (#6168)
    
    [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.
    
    
    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.
    
    ## Related Issue
    https://tyktech.atlassian.net/browse/TT-10856
    https://tyktech.atlassian.net/browse/TT-11593
    ## Motivation and Context
    
    <!-- Why is this change required? What problem does it solve? -->
    
    ## How This Has Been Tested
    
    <!-- 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. -->
    
    ## Screenshots (if appropriate)
    
    ## Types of changes
    
    <!-- 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)
    
    ## Checklist
    
    <!-- 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
    
    
    ___
    
    ## **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**
    <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
    
    ---------
    
    Co-authored-by: Jeffy Mathew <[email protected]>
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Projects

    None yet

    Development

    Successfully merging this pull request may close these issues.

    3 participants