@@ -3,18 +3,16 @@ package healthcheck
3
3
import (
4
4
"bytes"
5
5
"context"
6
- "crypto/tls"
7
- "crypto/x509"
8
6
"encoding/json"
9
7
"fmt"
10
8
"html"
11
9
"io"
12
10
"net"
13
11
"net/http"
14
- "os"
15
12
"regexp"
16
13
"time"
17
14
15
+ "github.com/appclacks/cabourotte/tls"
18
16
"github.com/pkg/errors"
19
17
"go.uber.org/zap"
20
18
@@ -39,6 +37,7 @@ type HTTPHealthcheckConfiguration struct {
39
37
SourceIP IP `json:"source-ip,omitempty" yaml:"source-ip,omitempty"`
40
38
BodyRegexp []Regexp `json:"body-regexp,omitempty" yaml:"body-regexp,omitempty"`
41
39
Insecure bool `json:"insecure"`
40
+ ServerName string `json:"server-name"`
42
41
Timeout Duration `json:"timeout"`
43
42
Key string `json:"key,omitempty"`
44
43
Cert string `json:"cert,omitempty"`
@@ -127,7 +126,6 @@ func (h *HTTPHealthcheck) Initialize() error {
127
126
h .buildURL ()
128
127
129
128
dialer := net.Dialer {}
130
- tlsConfig := & tls.Config {}
131
129
if h .Config .SourceIP != nil {
132
130
srcIP := net .IP (h .Config .SourceIP ).String ()
133
131
addr , err := net .ResolveTCPAddr ("tcp" , fmt .Sprintf ("%s:0" , srcIP ))
@@ -138,27 +136,10 @@ func (h *HTTPHealthcheck) Initialize() error {
138
136
LocalAddr : addr ,
139
137
}
140
138
}
141
- if h .Config .Key != "" {
142
- cert , err := tls .LoadX509KeyPair (h .Config .Cert , h .Config .Key )
143
- if err != nil {
144
- return errors .Wrapf (err , "Fail to load certificates" )
145
- }
146
- tlsConfig .Certificates = []tls.Certificate {cert }
147
- }
148
- if h .Config .Cacert != "" {
149
- caCert , err := os .ReadFile (h .Config .Cacert )
150
- if err != nil {
151
- return errors .Wrapf (err , "Fail to load the ca certificate" )
152
- }
153
- caCertPool := x509 .NewCertPool ()
154
- result := caCertPool .AppendCertsFromPEM (caCert )
155
- if ! result {
156
- return fmt .Errorf ("fail to read ca certificate for healthcheck %s" , h .Config .Base .Name )
157
- }
158
- tlsConfig .RootCAs = caCertPool
159
-
139
+ tlsConfig , err := tls .GetTLSConfig (h .Config .Key , h .Config .Cert , h .Config .Cacert , h .Config .ServerName , h .Config .Insecure )
140
+ if err != nil {
141
+ return err
160
142
}
161
- tlsConfig .InsecureSkipVerify = h .Config .Insecure
162
143
h .transport = & http.Transport {
163
144
DialContext : dialer .DialContext ,
164
145
TLSClientConfig : tlsConfig ,
0 commit comments