Add "fake ssl termination" capability to the hydra client

Replicating de "--fake-ssl-termination" option of the official hydra
client
This commit is contained in:
2020-05-21 13:12:17 +02:00
parent 389eb3885b
commit 44338f06e3
4 changed files with 37 additions and 9 deletions

View File

@ -188,11 +188,26 @@ func fromURL(url url.URL, path string, query url.Values) string {
return url.String()
}
func NewClient(baseURL *url.URL, httpTimeout time.Duration) *Client {
type fakeSSLTerminationTransport struct {
T http.RoundTripper
}
func (t *fakeSSLTerminationTransport) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Add("X-Forwarded-Proto", "https")
return t.T.RoundTrip(req)
}
func NewClient(baseURL *url.URL, fakeSSLTermination bool, httpTimeout time.Duration) *Client {
httpClient := &http.Client{
Timeout: httpTimeout,
}
if fakeSSLTermination {
httpClient.Transport = &fakeSSLTerminationTransport{http.DefaultTransport}
}
return &Client{
baseURL: baseURL,
http: &http.Client{
Timeout: 30 * time.Second,
},
http: httpClient,
}
}

View File

@ -8,7 +8,7 @@ import (
"gitlab.com/wpetit/goweb/service"
)
func ServiceProvider(rawBaseURL string, httpTimeout time.Duration) service.Provider {
func ServiceProvider(rawBaseURL string, fakeSSLTermination bool, httpTimeout time.Duration) service.Provider {
var (
baseURL *url.URL
err error
@ -19,7 +19,7 @@ func ServiceProvider(rawBaseURL string, httpTimeout time.Duration) service.Provi
err = errors.Wrap(err, "could not parse base url")
}
client := NewClient(baseURL, httpTimeout)
client := NewClient(baseURL, fakeSSLTermination, httpTimeout)
return func(ctn *service.Container) (interface{}, error) {
if err != nil {