-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
45 lines (41 loc) · 1.15 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package bifrost
import (
"crypto/tls"
"fmt"
"net/http"
"net/url"
)
type singleHostRoundTripper struct {
apiurl *url.URL
transport http.RoundTripper
}
func (s singleHostRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
r.URL.Scheme = s.apiurl.Scheme
r.URL.Host = s.apiurl.Host
path, err := url.JoinPath(s.apiurl.Path, r.URL.Path)
if err != nil {
return nil, fmt.Errorf("error joining request path with apiurl path: %w", err)
}
r.URL.Path = path
return s.transport.RoundTrip(r)
}
// HTTPClient returns a HTTP client set up for mTLS with the provided api URL.
// The returned client will only send requests to the api URL host.
// The request path will be joined with the api URL path, if any.
// The client will use the provided TLS client certificate to identify itself.
func HTTPClient(apiUrl string, clientCert *tls.Certificate) (*http.Client, error) {
u, err := url.Parse(apiUrl)
if err != nil {
return nil, err
}
return &http.Client{
Transport: &singleHostRoundTripper{
apiurl: u,
transport: &http.Transport{
TLSClientConfig: &tls.Config{
Certificates: []tls.Certificate{*clientCert},
},
},
},
}, nil
}