-
Notifications
You must be signed in to change notification settings - Fork 55
/
check.go
117 lines (96 loc) · 3.05 KB
/
check.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
package elasticsearch
import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
)
// Config is the Elasticsearch checker configuration settings container.
type Config struct {
DSN string // DSN is the Elasticsearch connection DSN. Required.
Password string // Password is the Elasticsearch connection password. Required.
SSLCertPath string // SSLCertPath is the path to the SSL certificate to use for the connection. Optional.
}
// New creates a new Elasticsearch health check that verifies the status of the cluster.
func New(config Config) func(ctx context.Context) error {
if config.DSN == "" || config.Password == "" {
return func(ctx context.Context) error {
return fmt.Errorf("elasticsearch DSN and password are required")
}
}
client, err := makeHTTPClient(config.SSLCertPath)
if err != nil {
return func(ctx context.Context) error {
return fmt.Errorf("failed to create Elasticsearch HTTP client: %w", err)
}
}
return func(ctx context.Context) error {
return checkHealth(ctx, client, config.DSN, config.Password)
}
}
func makeHTTPClient(sslCertPath string) (*http.Client, error) {
httpClient := http.Client{
Timeout: 5 * time.Second,
}
// If SSLCert is set, configure the client to use it.
// Otherwise, skip TLS verification.
if sslCertPath != "" {
cert, err := tls.LoadX509KeyPair(sslCertPath, sslCertPath)
if err != nil {
return nil, fmt.Errorf("failed to load Elasticsearch SSL certificate: %w", err)
}
// Configure the client to use the certificate.
httpTransport := &http.Transport{
TLSClientConfig: &tls.Config{
Certificates: []tls.Certificate{cert},
},
}
httpClient.Transport = httpTransport
return &httpClient, nil
}
// Configure the client to skip TLS verification.
httpTransport := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
httpClient.Transport = httpTransport
return &httpClient, nil
}
func checkHealth(ctx context.Context, client *http.Client, dsn string, password string) error {
req, err := http.NewRequestWithContext(
ctx,
http.MethodGet,
fmt.Sprintf("https://%s/_cluster/health", dsn),
nil,
)
if err != nil {
return fmt.Errorf("failed to create Elasticsearch health check request: %w", err)
}
req.SetBasicAuth("elastic", password)
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("failed to send Elasticsearch health check request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected status code from Elasticsearch health check: %d", resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("failed to read Elasticsearch health check response: %w", err)
}
healthResp := struct {
Status string `json:"status"`
}{}
if err := json.Unmarshal(body, &healthResp); err != nil {
return fmt.Errorf("failed to parse Elasticsearch health check response: %w", err)
}
if healthResp.Status != "green" {
return fmt.Errorf("elasticsearch cluster status is not green: %s", healthResp.Status)
}
return nil
}