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.3: fix unnecessary call to DeleteRawKey if no allowance scope is defined (TT-11721) (#6373) #6375

Merged

Conversation

buger
Copy link
Member

@buger buger commented Jun 26, 2024

User description

fix unnecessary call to DeleteRawKey if no allowance scope is defined (#6373)

User description

This PR fixes an issue where DeleteRawKey is called multiple times for
AllowanceScopes that are non-existent.

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)

PR Type

Bug fix, Tests


Description

  • Added a new method deleteRawKeysWithAllowanceScope in
    DefaultSessionManager to handle deletion of raw keys with allowance
    scopes.
  • Modified ResetQuota to use the new method for better handling of raw
    key deletions.
  • Implemented checks to skip deletion if the allowance scope is empty,
    preventing unnecessary calls.
  • Added comprehensive unit tests for the new method, covering various
    edge cases and ensuring correct behavior.

Changes walkthrough 📝

Relevant files
Bug fix
auth_manager.go
Add method to handle raw key deletion with allowance scopes

gateway/auth_manager.go

  • Added a new method deleteRawKeysWithAllowanceScope to handle deletion
    of raw keys with allowance scopes.
  • Modified ResetQuota to use the new method.
  • Added checks to skip deletion if the allowance scope is empty.
  • +13/-2   
    Tests
    auth_manager_test.go
    Add unit tests for deleteRawKeysWithAllowanceScope method

    gateway/auth_manager_test.go

  • Added unit tests for deleteRawKeysWithAllowanceScope method.
  • Created a mock countingStorageHandler to count delete operations.
  • Tested various scenarios including nil storage handler, nil session,
    and sessions with/without allowance scopes.
  • +190/-0 

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


    PR Type

    Bug fix, Tests


    Description

    • Added a new method deleteRawKeysWithAllowanceScope in DefaultSessionManager to handle deletion of raw keys with allowance scopes.
    • Modified ResetQuota to use the new method for better handling of raw key deletions.
    • Implemented checks to skip deletion if the allowance scope is empty, preventing unnecessary calls.
    • Added comprehensive unit tests for the new method, covering various edge cases and ensuring correct behavior.

    Changes walkthrough 📝

    Relevant files
    Bug fix
    auth_manager.go
    Add method for raw key deletion with allowance scopes       

    gateway/auth_manager.go

  • Added deleteRawKeysWithAllowanceScope method to handle raw key
    deletions with allowance scopes.
  • Modified ResetQuota to use the new method.
  • Implemented checks to skip deletion if the allowance scope is empty.
  • +13/-2   
    Tests
    auth_manager_test.go
    Add unit tests for raw key deletion method                             

    gateway/auth_manager_test.go

  • Added unit tests for deleteRawKeysWithAllowanceScope.
  • Created countingStorageHandler for test purposes.
  • Covered edge cases and ensured correct behavior.
  • +190/-0 

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

    …#6373)
    
    ### **User description**
    This PR fixes an issue where `DeleteRawKey` is called multiple times for
    AllowanceScopes that are non-existent.
    
    ## 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)
    
    
    ___
    
    ### **PR Type**
    Bug fix, Tests
    
    
    ___
    
    ### **Description**
    - Added a new method `deleteRawKeysWithAllowanceScope` in
    `DefaultSessionManager` to handle deletion of raw keys with allowance
    scopes.
    - Modified `ResetQuota` to use the new method for better handling of raw
    key deletions.
    - Implemented checks to skip deletion if the allowance scope is empty,
    preventing unnecessary calls.
    - Added comprehensive unit tests for the new method, covering various
    edge cases and ensuring 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>auth_manager.go</strong><dd><code>Add method to handle
    raw key deletion with allowance scopes</code></dd></summary>
    <hr>
    
    gateway/auth_manager.go
    <li>Added a new method <code>deleteRawKeysWithAllowanceScope</code> to
    handle deletion <br>of raw keys with allowance scopes.<br> <li> Modified
    <code>ResetQuota</code> to use the new method.<br> <li> Added checks to
    skip deletion if the allowance scope is empty.<br>
    
    
    </details>
        
    
      </td>
    <td><a
    href="https://github.com/TykTechnologies/tyk/pull/6373/files#diff-311e18b071d244ed1615c0019215b633278679df373288b3451bcc5cf7c52c4e">+13/-2</a>&nbsp;
    &nbsp; </td>
    </tr>                    
    </table></td></tr><tr><td><strong>Tests
    </strong></td><td><table>
    <tr>
      <td>
        <details>
    <summary><strong>auth_manager_test.go</strong><dd><code>Add unit tests
    for deleteRawKeysWithAllowanceScope method</code></dd></summary>
    <hr>
    
    gateway/auth_manager_test.go
    <li>Added unit tests for <code>deleteRawKeysWithAllowanceScope</code>
    method.<br> <li> Created a mock <code>countingStorageHandler</code> to
    count delete operations.<br> <li> Tested various scenarios including nil
    storage handler, nil session, <br>and sessions with/without allowance
    scopes.<br>
    
    
    </details>
        
    
      </td>
    <td><a
    href="https://github.com/TykTechnologies/tyk/pull/6373/files#diff-665d567ebb80f4c7bb6c683cc8ad317928a10b450468634506012a20a54153c9">+190/-0</a>&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 3985ca2)
    @buger buger enabled auto-merge (squash) June 26, 2024 11:16
    Copy link
    Contributor

    PR Reviewer Guide 🔍

    ⏱️ Estimated effort to review [1-5] 2
    🧪 Relevant tests Yes
    🔒 Security concerns No
    ⚡ Key issues to review Possible Bug:
    The method deleteRawKeysWithAllowanceScope does not handle the case where keyName is empty or null. This could potentially lead to the deletion of incorrect keys or no operation being performed when it is actually needed.
    Error Handling:
    The method deleteRawKeysWithAllowanceScope returns silently if store or session is nil. It might be beneficial to log these events or handle them more explicitly to aid in debugging and maintainability.

    Copy link
    Contributor

    API Changes

    no api changes detected

    Copy link
    Contributor

    PR Code Suggestions ✨

    CategorySuggestion                                                                                                                                    Score
    Enhancement
    Handle potential errors from store.DeleteRawKey to improve robustness

    To enhance the robustness of the deleteRawKeysWithAllowanceScope function, consider
    handling potential errors from store.DeleteRawKey(rawKey). This could involve logging the
    error or incorporating error handling logic.

    gateway/auth_manager.go [95]

    -store.DeleteRawKey(rawKey)
    +if !store.DeleteRawKey(rawKey) {
    +    log.Errorf("Failed to delete raw key: %s", rawKey)
    +}
     
    Suggestion importance[1-10]: 9

    Why: Handling potential errors from store.DeleteRawKey is crucial for robustness, ensuring that failures are logged and can be addressed.

    9
    Add unit tests for deleteRawKeysWithAllowanceScope with edge case values for keyName

    Add a unit test to verify that deleteRawKeysWithAllowanceScope correctly handles cases
    where keyName is empty or contains special characters, to ensure robustness against
    different input scenarios.

    gateway/auth_manager.go [420]

    -sessionManager.deleteRawKeysWithAllowanceScope(handler, session, "keyName")
    +sessionManager.deleteRawKeysWithAllowanceScope(handler, session, "")
    +sessionManager.deleteRawKeysWithAllowanceScope(handler, session, "key@Name#123")
     
    Suggestion importance[1-10]: 8

    Why: Adding unit tests for edge cases ensures the function handles various input scenarios robustly, which is important for comprehensive testing.

    8
    Possible issue
    Add validation for acl.AllowanceScope to ensure it meets expected criteria before using it

    Consider checking if acl.AllowanceScope is not only non-empty but also valid according to
    expected formats or values before constructing the rawKey. This can prevent potential
    issues with invalid keys being used in operations.

    gateway/auth_manager.go [91-95]

    -if acl.AllowanceScope == "" {
    +if acl.AllowanceScope == "" || !isValidAllowanceScope(acl.AllowanceScope) {
         continue
     }
     rawKey := QuotaKeyPrefix + acl.AllowanceScope + "-" + keyName
     store.DeleteRawKey(rawKey)
     
    Suggestion importance[1-10]: 8

    Why: Adding validation for acl.AllowanceScope can prevent potential issues with invalid keys, improving the robustness and reliability of the code.

    8
    Maintainability
    Refactor raw key deletion logic into a separate method to improve code organization

    Refactor the loop in deleteRawKeysWithAllowanceScope to a separate method to improve
    readability and maintainability. This method could handle the construction and deletion of
    the raw key.

    gateway/auth_manager.go [90-95]

     for _, acl := range session.AccessRights {
    +    handleAllowanceScopeDeletion(acl, store, keyName)
    +}
    +
    +func handleAllowanceScopeDeletion(acl user.AccessDefinition, store storage.Handler, keyName string) {
         if acl.AllowanceScope == "" {
    -        continue
    +        return
         }
         rawKey := QuotaKeyPrefix + acl.AllowanceScope + "-" + keyName
    -    store.DeleteRawKey(rawKey)
    +    if !store.DeleteRawKey(rawKey) {
    +        log.Errorf("Failed to delete raw key: %s", rawKey)
    +    }
     }
     
    Suggestion importance[1-10]: 7

    Why: Refactoring the loop into a separate method improves readability and maintainability, though it is more of a code organization enhancement rather than a critical fix.

    7

    Copy link

    sonarcloud bot commented Jun 26, 2024

    Quality Gate Failed Quality Gate failed

    Failed conditions
    C Reliability Rating on New Code (required ≥ A)

    See analysis details on SonarCloud

    Catch issues before they fail your Quality Gate with our IDE extension SonarLint

    @buger buger merged commit 733dbb6 into release-5.3 Jun 26, 2024
    33 of 34 checks passed
    @buger buger deleted the merge/release-5.3/3985ca2ac9bdb66db87d5cd938265d043a2ffbd1 branch June 26, 2024 11:41
    @pvormste pvormste changed the title Merging to release-5.3: fix unnecessary call to DeleteRawKey if no allowance scope is defined (#6373) Merging to release-5.3: fix unnecessary call to DeleteRawKey if no allowance scope is defined (TT-11721) (#6373) Jun 26, 2024
    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.

    None yet

    2 participants