-
Notifications
You must be signed in to change notification settings - Fork 117
/
clients.go
162 lines (143 loc) · 6.02 KB
/
clients.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package stack
import (
"context"
"errors"
"fmt"
"os"
"github.com/elastic/elastic-package/internal/elasticsearch"
"github.com/elastic/elastic-package/internal/kibana"
"github.com/elastic/elastic-package/internal/logger"
"github.com/elastic/elastic-package/internal/profile"
)
// NewElasticsearchClient creates an Elasticsearch client with the settings provided by the shellinit
// environment variables.
func NewElasticsearchClient(customOptions ...elasticsearch.ClientOption) (*elasticsearch.Client, error) {
options := []elasticsearch.ClientOption{
elasticsearch.OptionWithAddress(os.Getenv(ElasticsearchHostEnv)),
elasticsearch.OptionWithPassword(os.Getenv(ElasticsearchPasswordEnv)),
elasticsearch.OptionWithUsername(os.Getenv(ElasticsearchUsernameEnv)),
elasticsearch.OptionWithCertificateAuthority(os.Getenv(CACertificateEnv)),
}
options = append(options, customOptions...)
client, err := elasticsearch.NewClient(options...)
if errors.Is(err, elasticsearch.ErrUndefinedAddress) {
return nil, UndefinedEnvError(ElasticsearchHostEnv)
}
return client, err
}
// NewElasticsearchClientFromProfile creates an Elasticsearch client with the settings provided by the shellinit
// environment variables. If these environment variables are not set, it uses the information
// in the provided profile.
func NewElasticsearchClientFromProfile(profile *profile.Profile, customOptions ...elasticsearch.ClientOption) (*elasticsearch.Client, error) {
profileConfig, err := StackInitConfig(profile)
if err != nil {
return nil, fmt.Errorf("failed to load config from profile: %w", err)
}
elasticsearchHost, found := os.LookupEnv(ElasticsearchHostEnv)
if !found {
// Using backgound context on initial call to avoid context cancellation.
status, err := Status(context.Background(), Options{Profile: profile})
if err != nil {
return nil, fmt.Errorf("failed to check status of stack in current profile: %w", err)
}
if len(status) == 0 {
return nil, ErrUnavailableStack
}
elasticsearchHost = profileConfig.ElasticsearchHostPort
logger.Debugf("Connecting with Elasticsearch host from current profile (profile: %s, host: %q)", profile.ProfileName, elasticsearchHost)
}
elasticsearchPassword, found := os.LookupEnv(ElasticsearchPasswordEnv)
if !found {
elasticsearchPassword = profileConfig.ElasticsearchPassword
}
elasticsearchUsername, found := os.LookupEnv(ElasticsearchUsernameEnv)
if !found {
elasticsearchUsername = profileConfig.ElasticsearchUsername
}
caCertificate, found := os.LookupEnv(CACertificateEnv)
if !found {
caCertificate = profileConfig.CACertificatePath
}
options := []elasticsearch.ClientOption{
elasticsearch.OptionWithAddress(elasticsearchHost),
elasticsearch.OptionWithPassword(elasticsearchPassword),
elasticsearch.OptionWithUsername(elasticsearchUsername),
elasticsearch.OptionWithCertificateAuthority(caCertificate),
}
options = append(options, customOptions...)
return elasticsearch.NewClient(options...)
}
// NewKibanaClient creates a kibana client with the settings provided by the shellinit
// environment variables.
func NewKibanaClient(customOptions ...kibana.ClientOption) (*kibana.Client, error) {
options := []kibana.ClientOption{
kibana.Address(os.Getenv(KibanaHostEnv)),
kibana.Password(os.Getenv(ElasticsearchPasswordEnv)),
kibana.Username(os.Getenv(ElasticsearchUsernameEnv)),
kibana.CertificateAuthority(os.Getenv(CACertificateEnv)),
}
options = append(options, customOptions...)
client, err := kibana.NewClient(options...)
if errors.Is(err, kibana.ErrUndefinedHost) {
return nil, UndefinedEnvError(KibanaHostEnv)
}
return client, err
}
// NewKibanaClientFromProfile creates a kibana client with the settings provided by the shellinit
// environment variables. If these environment variables are not set, it uses the information
// in the provided profile.
func NewKibanaClientFromProfile(profile *profile.Profile, customOptions ...kibana.ClientOption) (*kibana.Client, error) {
profileConfig, err := StackInitConfig(profile)
if err != nil {
return nil, fmt.Errorf("failed to load config from profile: %w", err)
}
kibanaHost, found := os.LookupEnv(KibanaHostEnv)
if !found {
// Using background context on initial call to avoid context cancellation.
status, err := Status(context.Background(), Options{Profile: profile})
if err != nil {
return nil, fmt.Errorf("failed to check status of stack in current profile: %w", err)
}
if len(status) == 0 {
return nil, ErrUnavailableStack
}
kibanaHost = profileConfig.KibanaHostPort
logger.Debugf("Connecting with Kibana host from current profile (profile: %s, host: %q)", profile.ProfileName, kibanaHost)
}
elasticsearchPassword, found := os.LookupEnv(ElasticsearchPasswordEnv)
if !found {
elasticsearchPassword = profileConfig.ElasticsearchPassword
}
elasticsearchUsername, found := os.LookupEnv(ElasticsearchUsernameEnv)
if !found {
elasticsearchUsername = profileConfig.ElasticsearchUsername
}
caCertificate, found := os.LookupEnv(CACertificateEnv)
if !found {
caCertificate = profileConfig.CACertificatePath
}
options := []kibana.ClientOption{
kibana.Address(kibanaHost),
kibana.Password(elasticsearchPassword),
kibana.Username(elasticsearchUsername),
kibana.CertificateAuthority(caCertificate),
}
options = append(options, customOptions...)
return kibana.NewClient(options...)
}
// FindCACertificate looks for the CA certificate for the stack in the current profile.
// If not found, it uses the environment variable provided by shellinit.
func FindCACertificate(profile *profile.Profile) (string, error) {
caCertPath, found := os.LookupEnv(CACertificateEnv)
if !found {
profileConfig, err := StackInitConfig(profile)
if err != nil {
return "", fmt.Errorf("failed to load config from profile: %w", err)
}
caCertPath = profileConfig.CACertificatePath
}
return caCertPath, nil
}