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

Set HTTP hostname based on TLS server name #350

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions config/http_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,9 @@ func NewRoundTripperFromConfig(cfg HTTPClientConfig, name string, optFuncs ...HT
http2t.ReadIdleTimeout = time.Minute
}

// hostnameRoundTripper sets the http.Request Host to the value set as the TLS server name
rt = newHostnameRoundTripper(tlsConfig, rt)

// If a authorization_credentials is provided, create a round tripper that will set the
// Authorization header correctly on each request.
if cfg.Authorization != nil && len(cfg.Authorization.Credentials) > 0 {
Expand Down Expand Up @@ -457,6 +460,28 @@ func NewRoundTripperFromConfig(cfg HTTPClientConfig, name string, optFuncs ...HT
return NewTLSRoundTripper(tlsConfig, cfg.TLSConfig.CAFile, newRT)
}

type hostnameRoundTripper struct {
tlsConfig *tls.Config
rt http.RoundTripper
}

func newHostnameRoundTripper(tlsConfig *tls.Config, rt http.RoundTripper) http.RoundTripper {
return &hostnameRoundTripper{
tlsConfig: tlsConfig,
rt: rt,
}
}

func (rt *hostnameRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
req = cloneRequest(req)
if rt.tlsConfig.ServerName != "" {
hostParts := strings.Split(req.Host, ":")
hostParts[0] = rt.tlsConfig.ServerName
req.Host = strings.Join(hostParts, ":")
}
return rt.rt.RoundTrip(req)
}

type authorizationCredentialsRoundTripper struct {
authType string
authCredentials Secret
Expand Down
25 changes: 23 additions & 2 deletions config/http_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import (
"testing"
"time"

yaml "gopkg.in/yaml.v2"
"gopkg.in/yaml.v2"
)

const (
Expand Down Expand Up @@ -179,6 +179,27 @@ func TestNewClientFromConfig(t *testing.T) {
handler: func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, ExpectedMessage)
},
}, {
clientConfig: HTTPClientConfig{
TLSConfig: TLSConfig{
CAFile: TLSCAChainPath,
CertFile: ClientCertificatePath,
KeyFile: ClientKeyNoPassPath,
ServerName: "test-domain.com",
InsecureSkipVerify: true},
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
InsecureSkipVerify: true},
InsecureSkipVerify: true,
},

},
handler: func(w http.ResponseWriter, r *http.Request) {
srvAddr := r.Context().Value(http.LocalAddrContextKey).(net.Addr)
srvPort := strings.Split(srvAddr.String(), ":")[1]

expectedHostHeader := "test-domain.com:" + srvPort
actualHostHeader := r.Host
if actualHostHeader != expectedHostHeader {
fmt.Fprintf(w, "The expected Host header (%s) differs from the obtained Host header (%s)",
expectedHostHeader, actualHostHeader)
}
fmt.Fprint(w, ExpectedMessage)
},
}, {
clientConfig: HTTPClientConfig{
BearerToken: BearerToken,
Expand Down Expand Up @@ -512,7 +533,7 @@ func TestCustomIdleConnTimeout(t *testing.T) {
t.Fatalf("Can't create a round-tripper from this config: %+v", cfg)
}

transport, ok := rt.(*http.Transport)
transport, ok := rt.(*hostnameRoundTripper).rt.(*http.Transport)
if !ok {
t.Fatalf("Unexpected transport: %+v", transport)
}
Expand Down