/
http_client.go
44 lines (38 loc) · 957 Bytes
/
http_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
package helpers
import (
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"net"
"net/http"
"os"
"time"
)
func NewHTTPSClient(insecureSkipVerify bool, caCertFiles []string, communicationTimeout time.Duration) (*http.Client, error) {
dialer := &net.Dialer{
Timeout: 5 * time.Second,
KeepAlive: 30 * time.Second,
}
tlsConfig := &tls.Config{InsecureSkipVerify: insecureSkipVerify}
caCertPool := x509.NewCertPool()
for _, caCertFile := range caCertFiles {
if caCertFile != "" {
certBytes, err := os.ReadFile(caCertFile)
if err != nil {
return nil, fmt.Errorf("failed to read ca cert file: %s", err.Error())
}
if ok := caCertPool.AppendCertsFromPEM(certBytes); !ok {
return nil, errors.New("Unable to load caCert")
}
}
}
tlsConfig.RootCAs = caCertPool
return &http.Client{
Transport: &http.Transport{
Dial: dialer.Dial,
TLSClientConfig: tlsConfig,
},
Timeout: communicationTimeout,
}, nil
}