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.4: TT-12347 fix management of custom keys in mdcb installations (#6353) #6356

Merged

Conversation

buger
Copy link
Member

@buger buger commented Jun 19, 2024

User description

TT-12347 fix management of custom keys in mdcb installations (#6353)

User description

Description

Related Issue

https://tyktech.atlassian.net/browse/TT-12347

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

PR Type

Enhancement


Description

  • Changed the order of key search logic in the SessionDetail method of
    DefaultSessionManager.
  • Moved keyName to be appended after generating token with orgID.

Changes walkthrough 📝

Relevant files
Enhancement
auth_manager.go
Modify key search order in `SessionDetail` method               

gateway/auth_manager.go

  • Changed the order of key search logic in SessionDetail method.
  • Moved keyName to be appended after generating token with orgID.
  • +3/-1     

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


    PR Type

    Enhancement, Tests


    Description

    • Modified the key search logic in SessionDetail to include custom keys.
    • Implemented a fallback deletion mechanism using token ID in RPCStorageHandler.
    • Added comprehensive tests for custom key processing in the edge gateway.
    • Included tests for the new fallback deletion logic using token ID.

    Changes walkthrough 📝

    Relevant files
    Enhancement
    auth_manager.go
    Modify key search logic to include custom keys.                   

    gateway/auth_manager.go

  • Adjusted the order of key search logic in SessionDetail.
  • Added custom key handling in the key search list.
  • +2/-2     
    rpc_storage_handler.go
    Add fallback deletion using token ID in RPC storage handler.

    gateway/rpc_storage_handler.go

  • Implemented deleteUsingTokenID function for fallback deletion.
  • Updated ProcessKeySpaceChanges to use the new deletion function.
  • +18/-1   
    Tests
    auth_manager_test.go
    Add tests for custom key processing in edge gateway.         

    gateway/auth_manager_test.go

  • Added TestCustomKeysEdgeGw to verify custom key processing.
  • Included test cases for both hashed and non-hashed keys.
  • +98/-0   
    rpc_storage_handler_test.go
    Add tests for fallback deletion using token ID.                   

    gateway/rpc_storage_handler_test.go

  • Added TestDeleteUsingTokenID to test fallback deletion logic.
  • Included scenarios for both successful and unsuccessful deletions.
  • +63/-0   

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

    ### **User description**
    <!-- Provide a general summary of your changes in the Title above -->
    
    ## Description
    
    <!-- Describe your changes in detail -->
    
    ## Related Issue
    
    https://tyktech.atlassian.net/browse/TT-12347
    
    ## 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: -->
    
    - [ ] 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
    
    
    ___
    
    ### **PR Type**
    Enhancement
    
    
    ___
    
    ### **Description**
    - Changed the order of key search logic in the `SessionDetail` method of
    `DefaultSessionManager`.
    - Moved `keyName` to be appended after generating token with `orgID`.
    
    
    ___
    
    
    
    ### **Changes walkthrough** 📝
    <table><thead><tr><th></th><th align="left">Relevant
    files</th></tr></thead><tbody><tr><td><strong>Enhancement
    </strong></td><td><table>
    <tr>
      <td>
        <details>
    <summary><strong>auth_manager.go</strong><dd><code>Modify key search
    order in `SessionDetail` method</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    &nbsp; &nbsp; &nbsp; </dd></summary>
    <hr>
    
    gateway/auth_manager.go
    <li>Changed the order of key search logic in <code>SessionDetail</code>
    method.<br> <li> Moved <code>keyName</code> to be appended after
    generating token with <code>orgID</code>.<br>
    
    
    </details>
        
    
      </td>
    <td><a
    href="https://github.com/TykTechnologies/tyk/pull/6353/files#diff-311e18b071d244ed1615c0019215b633278679df373288b3451bcc5cf7c52c4e">+3/-1</a>&nbsp;
    &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 d3f52ab)
    Copy link
    Contributor

    PR Reviewer Guide 🔍

    ⏱️ Estimated effort to review [1-5] 3
    🧪 Relevant tests Yes
    🔒 Security concerns No
    ⚡ Key issues to review Possible Bug:
    The logic in gateway/auth_manager.go where toSearchList is modified might introduce a bug. The original keyName is now added after potentially generating a token, which could affect the order of operations and the intended logic.
    Code Clarity:
    In gateway/rpc_storage_handler.go, the method deleteUsingTokenID could be better documented to explain its purpose and the conditions under which it operates.

    Copy link
    Contributor

    API Changes

    no api changes detected

    Copy link
    Contributor

    PR Code Suggestions ✨

    CategorySuggestion                                                                                                                                    Score
    Best practice
    Use a constant for repeated string literals to improve code maintainability

    Use a constant for the repeated string "apikey-" to avoid potential typos and improve
    maintainability.

    gateway/auth_manager_test.go [666]

    -val, err := ts.Gw.GlobalSessionManager.Store().GetRawKey("apikey-" + keyResp.Key)
    +const apiKeyPrefix = "apikey-"
    +val, err := ts.Gw.GlobalSessionManager.Store().GetRawKey(apiKeyPrefix + keyResp.Key)
     
    Suggestion importance[1-10]: 9

    Why: The suggestion enhances code maintainability and reduces the risk of typos by using a constant for the repeated string literal, which is a best practice in coding.

    9
    Enhancement
    Simplify error handling by checking for an error first

    Refactor the error handling in deleteUsingTokenID to consolidate error checks and simplify
    the function logic.

    gateway/rpc_storage_handler.go [1156-1158]

    -if err == nil {
    -    _, status = r.Gw.handleDeleteKey(id, orgId, "-1", resetQuota)
    +if err != nil {
    +    return status, err
     }
    +_, status = r.Gw.handleDeleteKey(id, orgId, "-1", resetQuota)
     
    Suggestion importance[1-10]: 8

    Why: The suggestion improves the readability and maintainability of the code by simplifying the error handling logic, making it easier to understand and reducing the chance of errors.

    8
    Possible bug
    Ensure slice is initialized before use to prevent runtime panics

    To avoid potential nil pointer dereference, ensure jsonKeyValList is initialized properly
    before use.

    gateway/auth_manager.go [167]

    +if toSearchList == nil {
    +    return errors.New("toSearchList is nil")
    +}
     jsonKeyValList, err = b.store.GetMultiKey(toSearchList)
     
    Suggestion importance[1-10]: 7

    Why: The suggestion correctly identifies a potential issue with toSearchList being nil. However, the likelihood of toSearchList being nil is low given the context, making this a minor improvement rather than a critical fix.

    7

    Copy link

    sonarcloud bot commented Jun 19, 2024

    @buger buger merged commit e3e45c8 into release-5.4 Jun 19, 2024
    34 checks passed
    @buger buger deleted the merge/release-5.4/d3f52ab09057c3a729e0c5ae0c75522af13937f8 branch June 19, 2024 13:38
    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