Skip to content

Commit

Permalink
Merging to release-5.7.0: [TT-13507] Fix for custom domains with subs…
Browse files Browse the repository at this point in the history
…tring listen path (#6705) (#6724)

### **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

<!-- 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

[TT-13507]:
https://tyktech.atlassian.net/browse/TT-13507?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ


___

### **PR Type**
Bug fix, Configuration changes


___

### **Description**
- Refactored the domain sorting logic in the API loader by introducing a
new function `sortSpecsByListenPath`. The sorting now prioritizes listen
path length over domain length and ensures empty domains are sorted to
the end.
- 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>Refactor domain sorting
logic in API loader</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></summary>
<hr>

gateway/api_loader.go

<li>Introduced a new function <code>sortSpecsByListenPath</code> to
handle sorting.<br> <li> Changed sorting logic to prioritize listen path
length over domain <br>length.<br> <li> Ensured empty domains are sorted
to the end.<br>


</details>


  </td>
<td><a
href="https://github.com/TykTechnologies/tyk/pull/6724/files#diff-cdf0b7f176c9d18e1a314b78ddefc2cb3a94b3de66f1f360174692c915734c68">+15/-8</a>&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

<li>Added platform specification <code>linux/amd64</code> for the
httpbin service.<br>


</details>


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

</tr>
</table></td></tr><tr><td><strong>Additional files
(token-limit)</strong></td><td><table>
<tr>
  <td>
    <details>
<summary><strong>api_loader_test.go</strong><dd><code>...</code>&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &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_test.go

...



</details>


  </td>
<td><a
href="https://github.com/TykTechnologies/tyk/pull/6724/files#diff-f696545a659f4d96421b253edef4bcc8da0e7f52120b8f8866d32cbbb7cc1afc">+8888/-1</a></td>

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

___

> 💡 **PR-Agent usage**: Comment `/help "your question"` on any pull
request to receive relevant information

Co-authored-by: andrei-tyk <[email protected]>
(cherry picked from commit bbd01ec)
  • Loading branch information
buger authored and Tyk Bot committed Nov 21, 2024
1 parent 58911f4 commit 7f16493
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 @@ -912,21 +912,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 7f16493

Please sign in to comment.