-
Notifications
You must be signed in to change notification settings - Fork 55
/
connections.go
134 lines (111 loc) · 2.82 KB
/
connections.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package cmd
import (
"context"
"crypto/tls"
"crypto/x509"
"errors"
"net/http"
"strings"
"time"
)
func canReachInternet() (bool, error) {
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "https://google.com", nil)
if err != nil {
return false, err
}
res, err := http.DefaultClient.Do(req)
if err != nil {
return false, err
}
defer res.Body.Close()
return true, nil
}
func canConnectToEndpoint(endpoint string) (bool, error) {
if !strings.HasPrefix(endpoint, "https://") {
endpoint = "https://" + endpoint
}
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, endpoint, nil)
if err != nil {
return false, err
}
client := http.Client{
Timeout: 5 * time.Second,
Transport: &http.Transport{
TLSHandshakeTimeout: 5 * time.Second,
TLSClientConfig: &tls.Config{
//nolint:gosec // ok for testing connections
InsecureSkipVerify: true,
},
},
}
_, err = client.Do(req)
if err != nil {
return false, err
}
return true, nil
}
func canConnectToTLSEndpoint(row statusRow) (bool, error) {
endpoint := row.Endpoint
if !strings.HasPrefix(endpoint, "https://") {
endpoint = "https://" + endpoint
}
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, endpoint, nil)
if err != nil {
return false, err
}
caCertPool := x509.NewCertPool()
if len(row.CertificateAuthorityData) > 0 {
caCertPool.AppendCertsFromPEM([]byte(row.CertificateAuthorityData))
}
// this should use the same TLS configuration as the rest of the app
client := http.Client{
Timeout: 5 * time.Second,
Transport: &http.Transport{
TLSHandshakeTimeout: 5 * time.Second,
TLSClientConfig: &tls.Config{
MinVersion: tls.VersionTLS13,
RootCAs: caCertPool,
},
},
}
res, err := client.Do(req)
if err != nil {
return false, err
}
defer res.Body.Close()
return true, nil
}
func canGetEngineStatus(row statusRow) (bool, error) {
endpoint := row.Endpoint
if !strings.HasPrefix(endpoint, "https://") {
endpoint = "https://" + endpoint
}
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, endpoint+"/healthz", nil)
if err != nil {
return false, err
}
caCertPool := x509.NewCertPool()
if len(row.CertificateAuthorityData) > 0 {
caCertPool.AppendCertsFromPEM(row.CertificateAuthorityData)
}
// this should use the same TLS configuration as the rest of the app
client := http.Client{
Timeout: 5 * time.Second,
Transport: &http.Transport{
TLSHandshakeTimeout: 5 * time.Second,
TLSClientConfig: &tls.Config{
MinVersion: tls.VersionTLS13,
RootCAs: caCertPool,
},
},
}
res, err := client.Do(req)
if err != nil {
return false, err
}
defer res.Body.Close()
if res.StatusCode != 200 {
return false, errors.New("unexpected response code " + res.Status)
}
return true, nil
}