Skip to content

Commit

Permalink
[TT-13507] Fix for custom domains with substring listen path (#6705)
Browse files Browse the repository at this point in the history
### **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

<!-- Provide a general summary of your changes in the Title above -->

## Description

<!-- Describe your changes in detail -->

## Related Issue

<!-- This project only accepts pull requests related to open issues. -->
<!-- If suggesting a new feature or change, please discuss it in an
issue first. -->
<!-- If fixing a bug, there should be an issue describing it with steps
to reproduce. -->
<!-- OSS: Please link to the issue here. Tyk: please create/link the
JIRA ticket. -->

## 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**
Bug fix, Configuration changes


___

### **Description**
- Fixed the domain sorting logic in the API loader by changing the
comparison from domain length to lexicographical order to ensure correct
domain matching.
- Updated the Docker configuration for the httpbin service to specify
the platform as `linux/amd64`, addressing compatibility issues on macOS
with Apple Silicon.



___



### **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>api_loader.go</strong><dd><code>Fix domain sorting
logic in API loader</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; </dd></summary>
<hr>

gateway/api_loader.go

<li>Modified sorting logic for API specs by domain.<br> <li> Changed
comparison from domain length to lexicographical order.<br>


</details>


  </td>
<td><a
href="https://github.com/TykTechnologies/tyk/pull/6705/files#diff-cdf0b7f176c9d18e1a314b78ddefc2cb3a94b3de66f1f360174692c915734c68">+1/-1</a>&nbsp;
&nbsp; &nbsp; </td>

</tr>
</table></td></tr><tr><td><strong>Configuration
changes</strong></td><td><table>
<tr>
  <td>
    <details>
<summary><strong>httpbin.yml</strong><dd><code>Specify platform for
httpbin service in Docker configuration</code></dd></summary>
<hr>

docker/services/httpbin.yml

- Added platform specification for httpbin service.



</details>


  </td>
<td><a
href="https://github.com/TykTechnologies/tyk/pull/6705/files#diff-1a1b1d8bf4798076a41ca5f3bba0d465dc387e58b6593adbb28a4d5fe60eb810">+1/-0</a>&nbsp;
&nbsp; &nbsp; </td>

</tr>
</table></td></tr></tr></tbody></table>

___

> 💡 **PR-Agent usage**: Comment `/help "your question"` on any pull
request to receive relevant information
  • Loading branch information
andrei-tyk authored Nov 21, 2024
1 parent 72dd619 commit 7081f2e
Show file tree
Hide file tree
Showing 3 changed files with 8,903 additions and 8 deletions.
1 change: 1 addition & 0 deletions docker/services/httpbin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
services:
httpbin:
image: tykio/ci-tools:latest
platform: linux/amd64
networks:
- proxy
ports:
Expand Down
23 changes: 15 additions & 8 deletions gateway/api_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -924,21 +924,28 @@ func (gw *Gateway) loadGraphQLPlayground(spec *APISpec, subrouter *mux.Router) {
})
}

func sortSpecsByListenPath(specs []*APISpec) {
// sort by listen path from longer to shorter, so that /foo
// doesn't break /foo-bar
sort.Slice(specs, func(i, j int) bool {
// we sort by the following rules:
// - decreasing order of listen path length
// - if a domain is empty it should be at the end
if (specs[i].Domain == "") != (specs[j].Domain == "") {
return specs[i].Domain != ""
}
return len(specs[i].Proxy.ListenPath) > len(specs[j].Proxy.ListenPath)
})
}

// Create the individual API (app) specs based on live configurations and assign middleware
func (gw *Gateway) loadApps(specs []*APISpec) {
mainLog.Info("Loading API configurations.")

tmpSpecRegister := make(map[string]*APISpec)
tmpSpecHandles := new(sync.Map)

// sort by listen path from longer to shorter, so that /foo
// doesn't break /foo-bar
sort.Slice(specs, func(i, j int) bool {
if specs[i].Domain != specs[j].Domain {
return len(specs[i].Domain) > len(specs[j].Domain)
}
return len(specs[i].Proxy.ListenPath) > len(specs[j].Proxy.ListenPath)
})
sortSpecsByListenPath(specs)

// Create a new handler for each API spec
apisByListen := countApisByListenHash(specs)
Expand Down
Loading

0 comments on commit 7081f2e

Please sign in to comment.