-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
cloudid.go
224 lines (183 loc) · 6.17 KB
/
cloudid.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
// Package cloudid contains functions for parsing the cloud.id and cloud.auth
// settings and modifying the configuration to take them into account.
package cloudid
import (
"encoding/base64"
"fmt"
"net/url"
"strings"
"github.com/pkg/errors"
"github.com/elastic/beats/v7/libbeat/common"
"github.com/elastic/beats/v7/libbeat/logp"
)
const defaultCloudPort = "443"
// CloudID encapsulates the encoded (i.e. raw) and decoded parts of Elastic Cloud ID.
type CloudID struct {
id string
esURL string
kibURL string
auth string
username string
password string
}
// NewCloudID constructs a new CloudID object by decoding the given cloud ID and cloud auth.
func NewCloudID(cloudID string, cloudAuth string) (*CloudID, error) {
cid := CloudID{
id: cloudID,
auth: cloudAuth,
}
if err := cid.decode(); err != nil {
return nil, err
}
return &cid, nil
}
// ElasticsearchURL returns the Elasticsearch URL decoded from the cloud ID.
func (c *CloudID) ElasticsearchURL() string {
return c.esURL
}
// KibanaURL returns the Kibana URL decoded from the cloud ID.
func (c *CloudID) KibanaURL() string {
return c.kibURL
}
// Username returns the username decoded from the cloud auth.
func (c *CloudID) Username() string {
return c.username
}
// Password returns the password decoded from the cloud auth.
func (c *CloudID) Password() string {
return c.password
}
func (c *CloudID) decode() error {
var err error
if err = c.decodeCloudID(); err != nil {
return errors.Wrapf(err, "invalid cloud id '%v'", c.id)
}
if c.auth != "" {
if err = c.decodeCloudAuth(); err != nil {
return errors.Wrap(err, "invalid cloud auth")
}
}
return nil
}
// decodeCloudID decodes the c.id into c.esURL and c.kibURL
func (c *CloudID) decodeCloudID() error {
cloudID := c.id
// 1. Ignore anything before `:`.
idx := strings.LastIndex(cloudID, ":")
if idx >= 0 {
cloudID = cloudID[idx+1:]
}
// 2. base64 decode
decoded, err := base64.StdEncoding.DecodeString(cloudID)
if err != nil {
return errors.Wrapf(err, "base64 decoding failed on %s", cloudID)
}
// 3. separate based on `$`
words := strings.Split(string(decoded), "$")
if len(words) < 3 {
return errors.Errorf("Expected at least 3 parts in %s", string(decoded))
}
// 4. extract port from the ES and Kibana host, or use 443 as the default
host, port := extractPortFromName(words[0], defaultCloudPort)
esID, esPort := extractPortFromName(words[1], port)
kbID, kbPort := extractPortFromName(words[2], port)
// 5. form the URLs
esURL := url.URL{Scheme: "https", Host: fmt.Sprintf("%s.%s:%s", esID, host, esPort)}
kibanaURL := url.URL{Scheme: "https", Host: fmt.Sprintf("%s.%s:%s", kbID, host, kbPort)}
c.esURL = esURL.String()
c.kibURL = kibanaURL.String()
return nil
}
// decodeCloudAuth splits the c.auth into c.username and c.password.
func (c *CloudID) decodeCloudAuth() error {
cloudAuth := c.auth
idx := strings.Index(cloudAuth, ":")
if idx < 0 {
return errors.New("cloud.auth setting doesn't contain `:` to split between username and password")
}
c.username = cloudAuth[0:idx]
c.password = cloudAuth[idx+1:]
return nil
}
// OverwriteSettings modifies the received config object by overwriting the
// output.elasticsearch.hosts, output.elasticsearch.username, output.elasticsearch.password,
// setup.kibana.host settings based on values derived from the cloud.id and cloud.auth
// settings.
func OverwriteSettings(cfg *common.Config) error {
logger := logp.NewLogger("cloudid")
cloudID, _ := cfg.String("cloud.id", -1)
cloudAuth, _ := cfg.String("cloud.auth", -1)
if cloudID == "" && cloudAuth == "" {
// nothing to hack
return nil
}
logger.Debugf("cloud.id: %s, cloud.auth: %s", cloudID, cloudAuth)
if cloudID == "" {
return errors.New("cloud.auth specified but cloud.id is empty. Please specify both")
}
// cloudID overwrites
cid, err := NewCloudID(cloudID, cloudAuth)
if err != nil {
return errors.Errorf("Error decoding cloud.id: %v", err)
}
logger.Infof("Setting Elasticsearch and Kibana URLs based on the cloud id: output.elasticsearch.hosts=%s and setup.kibana.host=%s", cid.esURL, cid.kibURL)
esURLConfig, err := common.NewConfigFrom([]string{cid.ElasticsearchURL()})
if err != nil {
return err
}
// Before enabling the ES output, check that no other output is enabled
tmp := struct {
Output common.ConfigNamespace `config:"output"`
}{}
if err := cfg.Unpack(&tmp); err != nil {
return err
}
if out := tmp.Output; out.IsSet() && out.Name() != "elasticsearch" {
return errors.Errorf("The cloud.id setting enables the Elasticsearch output, but you already have the %s output enabled in the config", out.Name())
}
err = cfg.SetChild("output.elasticsearch.hosts", -1, esURLConfig)
if err != nil {
return err
}
err = cfg.SetString("setup.kibana.host", -1, cid.KibanaURL())
if err != nil {
return err
}
if cloudAuth != "" {
// cloudAuth overwrites
err = cfg.SetString("output.elasticsearch.username", -1, cid.Username())
if err != nil {
return err
}
err = cfg.SetString("output.elasticsearch.password", -1, cid.Password())
if err != nil {
return err
}
}
return nil
}
// extractPortFromName takes a string in the form `id:port` and returns the
// ID and the port. If there's no `:`, the default port is returned
func extractPortFromName(word string, defaultPort string) (id, port string) {
idx := strings.LastIndex(word, ":")
if idx >= 0 {
return word[:idx], word[idx+1:]
}
return word, defaultPort
}