Skip to content

Commit d0d7e9d

Browse files
committed
fix: set proper host header before proxying the request
Signed-off-by: Jason Zhu <jason.zhu@agoda.com>
1 parent 44f8909 commit d0d7e9d

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

internal/ansible/proxy/proxy.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"fmt"
2424
"io"
2525
"net/http"
26+
"net/url"
2627
"strings"
2728
"sync"
2829
"time"
@@ -144,6 +145,8 @@ func Run(done chan error, o Options) error {
144145

145146
// Remove the authorization header so the proxy can correctly inject the header.
146147
server.Handler = removeAuthorizationHeader(server.Handler)
148+
// Set the host header so that it matches the host in kubeconfig.
149+
server.Handler = setHostHeader(server.Handler, o.KubeConfig)
147150

148151
if o.OwnerInjection {
149152
server.Handler = &injectOwnerReferenceHandler{
@@ -283,6 +286,23 @@ func removeAuthorizationHeader(h http.Handler) http.Handler {
283286
})
284287
}
285288

289+
func setHostHeader(next http.Handler, kubeConfig *rest.Config) http.Handler {
290+
host := kubeConfig.Host
291+
if host == "" {
292+
return next
293+
}
294+
295+
// If host is a URL, extract the host portion
296+
if u, err := url.ParseRequestURI(host); err == nil {
297+
host = u.Host
298+
}
299+
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
300+
req.Header.Set("Host", host)
301+
req.Host = host
302+
next.ServeHTTP(w, req)
303+
})
304+
}
305+
286306
// Helper function used by recovering dependent watches and owner ref injection.
287307
func getRequestOwnerRef(req *http.Request) (*kubeconfig.NamespacedOwnerReference, error) {
288308
owner := kubeconfig.NamespacedOwnerReference{}

0 commit comments

Comments
 (0)