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

reverse proxy: use Rewrite instead of Director #324

Merged
merged 1 commit into from
Nov 26, 2024

Conversation

stlaz
Copy link
Collaborator

@stlaz stlaz commented Nov 14, 2024

This is to mitigate Golang's behavior with ReverseProxy.Director when, after the outgoing request is mutated, any headers specified by "Connection" would get rmoved from the mutated request. ReverseProxy.Rewrite() is only called once the headers specified in "Connection" were already removed from the outgoing-to-be request.

Fixes #319

edit: useful resources for more context:

@stlaz stlaz added the sig-auth-acceptance issues created during review for sig-auth-acceptance label Nov 14, 2024
@stlaz stlaz self-assigned this Nov 14, 2024
target := cfg.KubeRBACProxyInfo.UpstreamURL
pr.SetURL(target)
pr.Out.Host = target.Host
copyHeaderIfSet(pr.In, pr.Out, "X-Forwarded-For")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Wouldn't it make sense to add the IP address of the client, if not already set?

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-For#syntax

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

pr.SetXForwarded() does that but only if X-Forwarded-For is already set

Copy link
Collaborator

Choose a reason for hiding this comment

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

oh, yes. I see. But why do we need to copy the X-Forwarded-For header? Does it get removed otherwise?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Copy link
Collaborator

Choose a reason for hiding this comment

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

diff --git a/cmd/kube-rbac-proxy/app/kube-rbac-proxy.go b/cmd/kube-rbac-proxy/app/kube-rbac-proxy.go
index 82f3d737..7857dc36 100644
--- a/cmd/kube-rbac-proxy/app/kube-rbac-proxy.go
+++ b/cmd/kube-rbac-proxy/app/kube-rbac-proxy.go
@@ -217,7 +217,7 @@ func Run(cfg *server.KubeRBACProxyConfig) error {
 			target := cfg.KubeRBACProxyInfo.UpstreamURL
 			pr.SetURL(target)
 			pr.Out.Host = target.Host
-			copyHeaderIfSet(pr.In, pr.Out, "X-Forwarded-For")
+			//copyHeaderIfSet(pr.In, pr.Out, "X-Forwarded-For")
 			pr.SetXForwarded()
 		},
 	}

make test-unit doesn't fail.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added unit tests.

Rewrite: func(pr *httputil.ProxyRequest) {
target := cfg.KubeRBACProxyInfo.UpstreamURL
pr.SetURL(target)
pr.Out.Host = target.Host
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why do you set it, after rewrite removed it? To keep the same behavior as with the Director?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, I meant to keep the behavior the same.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, I assumed so, but I am wondering if this is some best practice like rewrite vs director 😅

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ok, I don't know why they are unsetting it, but it works with an empty request.Host, because of this: https://github.com/golang/go/blob/master/src/net/http/request.go#L605-L611. It will be automatically set, when the request is being written.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is to mitigate Golang's behavior with `ReverseProxy.Director`
when, after the outgoing request is mutated, any headers specified by
"Connection" would get rmoved from the mutated request.
`ReverseProxy.Rewrite()` is only called once the headers specified in
"Connection" were already removed from the outgoing-to-be request.
@ibihim ibihim merged commit ab5cad1 into brancz:sig-auth-acceptance Nov 26, 2024
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
sig-auth-acceptance issues created during review for sig-auth-acceptance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants