-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
client.go
418 lines (350 loc) · 11.8 KB
/
client.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
// 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 kibana
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"mime"
"mime/multipart"
"net/http"
"net/textproto"
"net/url"
"path"
"strings"
"github.com/joeshaw/multierror"
"github.com/elastic/beats/v7/libbeat/common"
"github.com/elastic/beats/v7/libbeat/common/transport/httpcommon"
"github.com/elastic/beats/v7/libbeat/common/useragent"
"github.com/elastic/beats/v7/libbeat/logp"
)
var (
// We started using Saved Objects API in 7.15. But to help integration
// developers migrate their dashboards we are more lenient.
MinimumRequiredVersionSavedObjects = common.MustNewVersion("7.14.0")
)
type Connection struct {
URL string
Username string
Password string
APIKey string
ServiceToken string
Headers http.Header
HTTP *http.Client
Version common.Version
}
type Client struct {
Connection
log *logp.Logger
}
func addToURL(_url, _path string, params url.Values) string {
if len(params) == 0 {
return _url + _path
}
return strings.Join([]string{_url, _path, "?", params.Encode()}, "")
}
func extractError(result []byte) error {
var kibanaResult struct {
Message string
Attributes struct {
Objects []struct {
Id string
Error struct {
Message string
}
}
}
}
if err := json.Unmarshal(result, &kibanaResult); err != nil {
return err
}
var errs multierror.Errors
if kibanaResult.Message != "" {
for _, err := range kibanaResult.Attributes.Objects {
errs = append(errs, fmt.Errorf("id: %s, message: %s", err.Id, err.Error.Message))
}
return fmt.Errorf("%s: %+v", kibanaResult.Message, errs.Err())
}
return nil
}
func extractMessage(result []byte) error {
var kibanaResult struct {
Success bool
Errors []struct {
Id string
Type string
Error struct {
Type string
References []struct {
Type string
Id string
}
}
}
}
if err := json.Unmarshal(result, &kibanaResult); err != nil {
return nil
}
if !kibanaResult.Success {
var errs multierror.Errors
for _, err := range kibanaResult.Errors {
errs = append(errs, fmt.Errorf("error: %s, asset ID=%s; asset type=%s; references=%+v", err.Error.Type, err.Id, err.Type, err.Error.References))
}
return errs.Err()
}
return nil
}
// NewKibanaClient builds and returns a new Kibana client
func NewKibanaClient(cfg *common.Config, beatname string) (*Client, error) {
config := DefaultClientConfig()
if err := cfg.Unpack(&config); err != nil {
return nil, err
}
return NewClientWithConfig(&config, beatname)
}
// NewClientWithConfig creates and returns a kibana client using the given config
func NewClientWithConfig(config *ClientConfig, beatname string) (*Client, error) {
return NewClientWithConfigDefault(config, 5601, beatname)
}
// NewClientWithConfig creates and returns a kibana client using the given config
func NewClientWithConfigDefault(config *ClientConfig, defaultPort int, beatname string) (*Client, error) {
if err := config.Validate(); err != nil {
return nil, err
}
p := config.Path
if config.SpaceID != "" {
p = path.Join(p, "s", config.SpaceID)
}
kibanaURL, err := common.MakeURL(config.Protocol, p, config.Host, defaultPort)
if err != nil {
return nil, fmt.Errorf("invalid Kibana host: %v", err)
}
u, err := url.Parse(kibanaURL)
if err != nil {
return nil, fmt.Errorf("failed to parse the Kibana URL: %v", err)
}
username := config.Username
password := config.Password
if u.User != nil {
username = u.User.Username()
password, _ = u.User.Password()
u.User = nil
if config.APIKey != "" && (username != "" || password != "") {
return nil, fmt.Errorf("cannot set api_key with username/password in Kibana URL")
}
// Re-write URL without credentials.
kibanaURL = u.String()
}
log := logp.NewLogger("kibana")
log.Infof("Kibana url: %s", kibanaURL)
headers := make(http.Header)
for k, v := range config.Headers {
headers.Set(k, v)
}
if beatname == "" {
beatname = "Libbeat"
}
userAgent := useragent.UserAgent(beatname)
rt, err := config.Transport.Client(httpcommon.WithHeaderRoundTripper(map[string]string{"User-Agent": userAgent}))
if err != nil {
return nil, err
}
client := &Client{
Connection: Connection{
URL: kibanaURL,
Username: username,
Password: password,
APIKey: config.APIKey,
ServiceToken: config.ServiceToken,
Headers: headers,
HTTP: rt,
},
log: log,
}
if !config.IgnoreVersion {
if err = client.readVersion(); err != nil {
return nil, fmt.Errorf("fail to get the Kibana version: %v", err)
}
}
return client, nil
}
func (conn *Connection) Request(method, extraPath string,
params url.Values, headers http.Header, body io.Reader) (int, []byte, error) {
resp, err := conn.Send(method, extraPath, params, headers, body)
if err != nil {
return 0, nil, fmt.Errorf("fail to execute the HTTP %s request: %v", method, err)
}
defer resp.Body.Close()
var retError error
if resp.StatusCode >= 300 {
retError = fmt.Errorf("%v", resp.Status)
}
result, err := ioutil.ReadAll(resp.Body)
if err != nil {
return 0, nil, fmt.Errorf("fail to read response %s", err)
}
if resp.StatusCode >= 300 {
retError = extractError(result)
} else {
retError = extractMessage(result)
}
return resp.StatusCode, result, retError
}
// Sends an application/json request to Kibana with appropriate kbn headers
func (conn *Connection) Send(method, extraPath string,
params url.Values, headers http.Header, body io.Reader) (*http.Response, error) {
return conn.SendWithContext(context.Background(), method, extraPath, params, headers, body)
}
// SendWithContext sends an application/json request to Kibana with appropriate kbn headers and the given context.
func (conn *Connection) SendWithContext(ctx context.Context, method, extraPath string,
params url.Values, headers http.Header, body io.Reader) (*http.Response, error) {
reqURL := addToURL(conn.URL, extraPath, params)
req, err := http.NewRequestWithContext(ctx, method, reqURL, body)
if err != nil {
return nil, fmt.Errorf("fail to create the HTTP %s request: %+v", method, err)
}
if conn.Username != "" || conn.Password != "" {
req.SetBasicAuth(conn.Username, conn.Password)
}
if conn.APIKey != "" {
v := "ApiKey " + base64.StdEncoding.EncodeToString([]byte(conn.APIKey))
req.Header.Set("Authorization", v)
}
if conn.ServiceToken != "" {
v := "Bearer " + conn.ServiceToken
req.Header.Set("Authorization", v)
}
addHeaders(req.Header, conn.Headers)
addHeaders(req.Header, headers)
contentType := req.Header.Get("Content-Type")
contentType, _, _ = mime.ParseMediaType(contentType)
if contentType != "multipart/form-data" && contentType != "application/ndjson" {
req.Header.Set("Content-Type", "application/json")
}
req.Header.Set("Accept", "application/json")
req.Header.Set("kbn-xsrf", "1")
return conn.RoundTrip(req)
}
func addHeaders(out, in http.Header) {
for k, vs := range in {
for _, v := range vs {
out.Add(k, v)
}
}
}
// Implements RoundTrip interface
func (conn *Connection) RoundTrip(r *http.Request) (*http.Response, error) {
return conn.HTTP.Do(r)
}
func (client *Client) readVersion() error {
type kibanaVersionResponse struct {
Name string `json:"name"`
Version struct {
Number string `json:"number"`
Snapshot bool `json:"build_snapshot"`
} `json:"version"`
}
type kibanaVersionResponse5x struct {
Name string `json:"name"`
Version string `json:"version"`
}
code, result, err := client.Connection.Request("GET", "/api/status", nil, nil, nil)
if err != nil || code >= 400 {
return fmt.Errorf("HTTP GET request to %s/api/status fails: %v. Response: %s.",
client.Connection.URL, err, truncateString(result))
}
var versionString string
var kibanaVersion kibanaVersionResponse
err = json.Unmarshal(result, &kibanaVersion)
if err != nil {
return fmt.Errorf("fail to unmarshal the response from GET %s/api/status. Response: %s. Kibana status api returns: %v",
client.Connection.URL, truncateString(result), err)
}
versionString = kibanaVersion.Version.Number
if kibanaVersion.Version.Snapshot {
// needed for the tests
versionString += "-SNAPSHOT"
}
version, err := common.NewVersion(versionString)
if err != nil {
return fmt.Errorf("fail to parse kibana version (%v): %+v", versionString, err)
}
client.Version = *version
return nil
}
// GetVersion returns the version read from kibana. The version is not set if
// IgnoreVersion was set when creating the client.
func (client *Client) GetVersion() common.Version { return client.Version }
func (client *Client) ImportMultiPartFormFile(url string, params url.Values, filename string, contents string) error {
buf := &bytes.Buffer{}
w := multipart.NewWriter(buf)
pHeaders := textproto.MIMEHeader{}
pHeaders.Add("Content-Disposition", fmt.Sprintf(`form-data; name="file"; filename="%s"`, filename))
pHeaders.Add("Content-Type", "application/ndjson")
p, err := w.CreatePart(pHeaders)
if err != nil {
return fmt.Errorf("failed to create multipart writer for payload: %+v", err)
}
_, err = io.Copy(p, strings.NewReader(contents))
if err != nil {
return fmt.Errorf("failed to copy contents of the object: %+v", err)
}
w.Close()
headers := http.Header{}
headers.Add("Content-Type", w.FormDataContentType())
statusCode, response, err := client.Connection.Request("POST", url, params, headers, buf)
if err != nil || statusCode >= 300 {
return fmt.Errorf("returned %d to import file: %v. Response: %s", statusCode, err, response)
}
client.log.Debugf("Imported multipart file to %s with params %v", url, params)
return nil
}
func (client *Client) Close() error { return nil }
// GetDashboard returns the dashboard with the given id with the index pattern removed
func (client *Client) GetDashboard(id string) ([]byte, error) {
if client.Version.LessThan(MinimumRequiredVersionSavedObjects) {
return nil, fmt.Errorf("Kibana version must be at least " + MinimumRequiredVersionSavedObjects.String())
}
body := fmt.Sprintf(`{"objects": [{"type": "dashboard", "id": "%s" }], "includeReferencesDeep": true, "excludeExportDetails": true}`, id)
statusCode, response, err := client.Request("POST", "/api/saved_objects/_export", nil, nil, strings.NewReader(body))
if err != nil || statusCode >= 300 {
return nil, fmt.Errorf("error exporting dashboard: %+v, code: %d", err, statusCode)
}
result, err := RemoveIndexPattern(response)
if err != nil {
return nil, fmt.Errorf("error removing index pattern: %+v", err)
}
return result, nil
}
// truncateString returns a truncated string if the length is greater than 250
// runes. If the string is truncated "... (truncated)" is appended. Newlines are
// replaced by spaces in the returned string.
//
// This function is useful for logging raw HTTP responses with errors when those
// responses can be very large (such as an HTML page with CSS content).
func truncateString(b []byte) string {
const maxLength = 250
runes := bytes.Runes(b)
if len(runes) > maxLength {
runes = append(runes[:maxLength], []rune("... (truncated)")...)
}
return strings.Replace(string(runes), "\n", " ", -1)
}