TykTechnologies / tyk

Tyk Open Source API Gateway written in Go, supporting REST, GraphQL, TCP and gRPC protocols
Other
9.75k stars 1.09k forks source link

Merging to release-5.7: [TT-13507] Fix for custom domains with substring listen path (#6705) #6723

Closed buger closed 1 week ago

buger commented 1 week ago

User description

TT-13507 Fix for custom domains with substring listen path (#6705)

User description

Actual fix for domain matching edge case related to substring listen paths. Fixed the sorting function for the listen paths and added some unit tests.

Extra: httpbin service not working on macos with apple silicon; now it does

Description

Related Issue

Motivation and Context

How This Has Been Tested

Screenshots (if appropriate)

Types of changes

Checklist


PR Type

Bug fix, Configuration changes


Description


Changes walkthrough ๐Ÿ“

Relevant files
Bug fix
api_loader.go
Fix domain sorting logic in API loader                                     

gateway/api_loader.go
  • Modified sorting logic for API specs by domain.
  • Changed comparison from domain length to lexicographical order.
  • +1/-1     
    Configuration changes
    httpbin.yml
    Specify platform for httpbin service in Docker configuration

    docker/services/httpbin.yml - Added platform specification for httpbin service.
    +1/-0     

    ๐Ÿ’ก PR-Agent usage: Comment /help "your question" on any pull request to receive relevant information


    PR Type

    Bug fix, Configuration changes, Tests


    Description


    Changes walkthrough ๐Ÿ“

    Relevant files
    Bug fix
    api_loader.go
    Refactor domain sorting logic in API loader                           

    gateway/api_loader.go
  • Refactored sorting logic for API specs by listen path.
  • Changed sorting criteria to prioritize non-empty domains.
  • Extracted sorting logic into a separate function.
  • +15/-8   
    Configuration changes
    httpbin.yml
    Specify platform for httpbin service in Docker                     

    docker/services/httpbin.yml
  • Added platform specification for httpbin service in Docker
    configuration.
  • +1/-0     
    Tests
    api_loader_test.go
    Add unit tests for domain sorting logic                                   

    gateway/api_loader_test.go
  • Added unit tests for domain sorting logic.
  • Verified correct order of API specs by listen path.
  • +8888/-1
    Additional files (token-limit)
    api_loader_test.go
    ...                                                                                                           

    gateway/api_loader_test.go ...
    +8888/-1

    ๐Ÿ’ก PR-Agent usage: Comment /help "your question" on any pull request to receive relevant information

    github-actions[bot] commented 1 week ago

    PR Reviewer Guide ๐Ÿ”

    Here are some key observations to aid the review process:

    **๐ŸŽซ Ticket compliance analysis ๐Ÿ”ถ** **[6705](https://github.com/TykTechnologies/tyk/issues/6705) - Partially compliant** Fully compliant requirements: - Fix domain matching edge case related to substring listen paths. - Update the sorting function for listen paths. - Address compatibility issues on macOS with Apple Silicon for httpbin service. Not compliant requirements: - Add unit tests.
    โฑ๏ธ Estimated effort to review: 2 ๐Ÿ”ต๐Ÿ”ตโšชโšชโšช
    ๐Ÿงช No relevant tests
    ๐Ÿ”’ No security concerns identified
    โšก Recommended focus areas for review

    Sorting Logic
    The new sorting logic for API specs should be thoroughly tested to ensure it handles edge cases correctly, especially when domains are empty.
    github-actions[bot] commented 1 week ago

    API Changes

    no api changes detected
    github-actions[bot] commented 1 week ago

    PR Code Suggestions โœจ

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Improve the sorting logic to handle cases where both domains are empty ___ **Ensure that the sorting logic in sortSpecsByListenPath correctly handles cases where
    Domain is empty for both compared elements. Currently, it only checks if they are
    different, which might lead to inconsistent sorting when both are empty.** [gateway/api_loader.go [934-937]](https://github.com/TykTechnologies/tyk/pull/6723/files#diff-cdf0b7f176c9d18e1a314b78ddefc2cb3a94b3de66f1f360174692c915734c68R934-R937) ```diff +if specs[i].Domain == "" && specs[j].Domain == "" { + return len(specs[i].Proxy.ListenPath) > len(specs[j].Proxy.ListenPath) +} if (specs[i].Domain == "") != (specs[j].Domain == "") { return specs[i].Domain != "" } return len(specs[i].Proxy.ListenPath) > len(specs[j].Proxy.ListenPath) ```
    Suggestion importance[1-10]: 7 Why: The suggestion correctly identifies a potential issue in the sorting logic where both domains are empty, which could lead to inconsistent sorting results. The improved code explicitly handles this scenario, ensuring more predictable and stable sorting behavior.
    7