-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathhttp.go
48 lines (41 loc) · 997 Bytes
/
http.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
46
47
48
package csprcollector
import (
"crypto/tls"
"crypto/x509"
"io/ioutil"
"log"
"net/http"
)
func NewHttpClient(certFile, keyFile, caFile string) *http.Client {
var certs []tls.Certificate
if certFile != "" && keyFile != "" {
certs = append(certs, newClientCert(certFile, keyFile))
}
var caCertPool *x509.CertPool
if caFile != "" {
caCertPool = newCaCertPool(caFile)
}
tlsConfig := &tls.Config{
Certificates: certs,
RootCAs: caCertPool,
}
tlsConfig.BuildNameToCertificate()
transport := &http.Transport{TLSClientConfig: tlsConfig}
return &http.Client{Transport: transport}
}
func newClientCert(certFile, keyFile string) tls.Certificate {
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
log.Fatal(err)
}
return cert
}
func newCaCertPool(caFile string) *x509.CertPool {
caCert, err := ioutil.ReadFile(caFile)
if err != nil {
log.Fatal(err)
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
return caCertPool
}