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

fix(service-proxy): avoid Duplicate Proxy Path in URL Rewrite Function #868

Merged
merged 13 commits into from
Jan 30, 2025

Conversation

ibakshay
Copy link
Contributor

Description

This PR fixes an issue in the rewrite function of the service-proxy, where outgoing requests to a upstream service could contain duplicate proxy paths. The issue primarily affected applications like perses, where requests for static assets (e.g., JavaScript, CSS, and favicon files) resulted in 404 Not Found errors due to incorrect URL rewriting.

Root Cause

  1. The rewrite function previously unconditionally appended the Kubernetes API proxy path (/api/v1/.../proxy/) to req.Out.URL.Path, even when the request already contained the full proxy path.
  2. This resulted in URLs like:
    https://api.blueprints.greenhouse.shoot.canary.k8s-hana.ondemand.com/api/v1/namespaces/kube-monitoring/services/perses:8080/proxy/api/v1/namespaces/kube-monitoring/services/perses:8080/proxy/perses/main.js
    
    instead of:
    https://api.blueprints.greenhouse.shoot.canary.k8s-hana.ondemand.com/api/v1/namespaces/kube-monitoring/services/perses:8080/proxy/perses/main.js
    

I have also added a lot more tests to cover more scenarios.

Related Issues

Closes #838

…tion and related tests

Signed-off-by: Akshay Iyyadurai Balasundaram <akshay.iyyadurai.balasundaram@sap.com>
Signed-off-by: Akshay Iyyadurai Balasundaram <akshay.iyyadurai.balasundaram@sap.com>
@ibakshay ibakshay requested a review from a team as a code owner January 29, 2025 12:15
@ibakshay ibakshay changed the title fix(service-proxy) Avoid Duplicate Proxy Path in URL Rewrite Function fix(service-proxy): Avoid Duplicate Proxy Path in URL Rewrite Function Jan 29, 2025
@github-actions github-actions bot added the documentation Improvements or additions to documentation label Jan 29, 2025
@ibakshay ibakshay self-assigned this Jan 29, 2025
@ibakshay ibakshay changed the title fix(service-proxy): Avoid Duplicate Proxy Path in URL Rewrite Function fix(service-proxy): avoid Duplicate Proxy Path in URL Rewrite Function Jan 29, 2025
Signed-off-by: Akshay Iyyadurai Balasundaram <akshay.iyyadurai.balasundaram@sap.com>
Signed-off-by: Akshay Iyyadurai Balasundaram <akshay.iyyadurai.balasundaram@sap.com>
Signed-off-by: Akshay Iyyadurai Balasundaram <akshay.iyyadurai.balasundaram@sap.com>
Copy link
Contributor

@abhijith-darshan abhijith-darshan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @ibakshay 🚀

Some minor feedback

cmd/service-proxy/proxy_test.go Outdated Show resolved Hide resolved
cmd/service-proxy/proxy_test.go Outdated Show resolved Hide resolved
cmd/service-proxy/proxy_test.go Outdated Show resolved Hide resolved
cmd/service-proxy/proxy.go Outdated Show resolved Hide resolved
cmd/service-proxy/proxy.go Show resolved Hide resolved
…st URLs

Signed-off-by: Akshay Iyyadurai Balasundaram <akshay.iyyadurai.balasundaram@sap.com>
Signed-off-by: Akshay Iyyadurai Balasundaram <akshay.iyyadurai.balasundaram@sap.com>
@abhijith-darshan abhijith-darshan self-requested a review January 30, 2025 08:47
@ibakshay ibakshay merged commit d44875f into main Jan 30, 2025
22 checks passed
@ibakshay ibakshay deleted the ibakshay/fix-service-proxy branch January 30, 2025 08:50
Copy link
Contributor

@uwe-mayer uwe-mayer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @ibakshay for taking this!

Some comments agreeing with @abhijith-darshan on mock data...

cmd/service-proxy/proxy.go Show resolved Hide resolved
Comment on lines 229 to 234
req.Out.URL.Path = strings.TrimSuffix(upstreamServiceRouteURL.Path, "/") + req.Out.URL.Path
}

// Set the correct upstream service route URL details
req.Out.URL.Scheme = upstreamServiceRouteURL.Scheme
req.Out.URL.Host = upstreamServiceRouteURL.Host
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rewrite of the out.URL was previously covered by req.SetURL(..) in line 227. With this change the query is no longer considered, right?

Copy link
Contributor Author

@ibakshay ibakshay Jan 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the req.SetURL(...) was not handling this test case (valid host with already prefix path) properly.
Below is the test failure output when we have req.SetURL(...)

expected URL https://api.test-api-server.com/api/v1/namespaces/kube-monitoring/services/test-service:8080/existing-path
got https://api.test-api-server.com/api/v1/namespaces/kube-monitoring/services/test-service:8080/api/v1/namespaces/kube-monitoring/services/test-service:8080/existing-path

That is why I had to manually set the req.out.URL... instead of relying on req.SetURL(..) function.

}

// modifyResponse strips the k8s API server proxy path prepended to the location header during redirects:
// https://github.com/kubernetes/kubernetes/blob/master/staging/src/k8s.io/apimachinery/pkg/util/proxy/transport.go#L113
func (pm *ProxyManager) modifyResponse(resp *http.Response) error {
logger := log.FromContext(resp.Request.Context())
logger.Info("Modifying response", "statusCode", resp.StatusCode, "originalLocation", resp.Header.Get("Location"))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not very deep into how the whole service proxy works. The comment on this method indicates that it was originally intended to handle the API server proxy path during redirects.
cc @uwe-mayer

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment on this method indicates that it was originally intended to handle the API server proxy path during redirects.

Yes, that is correct. I have tested and I can confirm thatmodifyResponse does handle redirects properly without any issues. I have also added tests for this method :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core-apis documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

🐛 [BUG] - Service Proxy not working for Perses
5 participants