-
Notifications
You must be signed in to change notification settings - Fork 530
/
Copy pathkibana.go
318 lines (283 loc) · 9.5 KB
/
kibana.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
// 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 systemtest
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"mime/multipart"
"net/http"
"net/url"
"path"
"testing"
"time"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
"github.com/elastic/apm-server/systemtest/apmservertest"
"github.com/elastic/apm-server/systemtest/estest"
"github.com/elastic/apm-server/systemtest/fleettest"
"github.com/elastic/apm-tools/pkg/espoll"
)
const (
adminKibanaUser = adminElasticsearchUser
adminKibanaPass = adminElasticsearchPass
// agentPolicyDescription holds the description associated with agent
// policies created by CreateAgentPolicy. This can be used in filters.
agentPolicyDescription = "apm_systemtest"
)
var (
// KibanaURL is the base URL for Kibana, including userinfo for
// authenticating as the admin user.
KibanaURL *url.URL
// Fleet is a Fleet API client for use in tests.
Fleet *fleettest.Client
// IntegrationPackage holds the "apm" integration package details.
//
// IntegrationPackage is initialised by InitFleetPackage.
IntegrationPackage *fleettest.Package
)
func initKibana() {
kibanaConfig := apmservertest.DefaultConfig().Kibana
u, err := url.Parse(kibanaConfig.Host)
if err != nil {
log.Fatal(err)
}
u.User = url.UserPassword(adminKibanaUser, adminKibanaPass)
KibanaURL = u
Fleet = fleettest.NewClient(KibanaURL.String())
}
// InitFleet ensures Fleet is set up, destroys any existing agent policies previously
// created by the system tests and unenrolls the associated agents. After InitFleet
// returns successfully, the IntegrationPackage var will be initialised to the details
// of the installed APM integration package.
func InitFleet() error {
if err := Fleet.Setup(); err != nil {
log.Fatal(err)
}
agentPolicies, err := Fleet.AgentPolicies("ingest-agent-policies.description:" + agentPolicyDescription)
if err != nil {
return err
}
ids := make([]string, len(agentPolicies))
for i, agentPolicy := range agentPolicies {
ids[i] = agentPolicy.ID
}
if err := DestroyAgentPolicy(ids...); err != nil {
return fmt.Errorf("failed to destroy agent policy: %w", err)
}
return InitFleetPackage()
}
// InitFleetPackage and sets IntegrationPackage to the details of the installed
// APM integration package. InitFleetPackage assumes that Fleet has been set up
// already.
func InitFleetPackage() error {
packages, err := Fleet.ListPackages()
if err != nil {
return err
}
for _, pkg := range packages {
if pkg.Name != "apm" {
continue
}
IntegrationPackage = &pkg
return nil
}
return errors.New("'apm' integration package not installed")
}
// CreateAgentPolicy creates an Agent policy with the given name and namespace,
// creates an APM package policy with the provided config vars, and assigns it
// to the agent policy.
//
// The agent policy will be destroyed, and any assigned agents unenrolled, when
// the test completes.
//
// This should typically be used by tests instead of directly calling the
// fleettest.Client.CreateAgentPolicy method.
func CreateAgentPolicy(t testing.TB, name, namespace string, vars, config map[string]interface{}) (*fleettest.AgentPolicy, *fleettest.EnrollmentAPIKey) {
agentPolicy, key, err := Fleet.CreateAgentPolicy(name, namespace, agentPolicyDescription)
require.NoError(t, err)
t.Cleanup(func() {
err := DestroyAgentPolicy(agentPolicy.ID)
require.NoError(t, err)
})
packagePolicy := NewPackagePolicy(agentPolicy, vars, config)
err = Fleet.CreatePackagePolicy(packagePolicy)
require.NoError(t, err)
return agentPolicy, key
}
// DestroyAgentPolicy deletes the agent policies with given IDs,
// and bulk unenrolls the agents assigned to them.
func DestroyAgentPolicy(id ...string) error {
if len(id) == 0 {
return nil
}
agents, err := Fleet.Agents()
if err != nil {
return err
}
agentsByPolicy := make(map[string][]fleettest.Agent)
for _, agent := range agents {
agentsByPolicy[agent.PolicyID] = append(agentsByPolicy[agent.PolicyID], agent)
}
for _, agentPolicyID := range id {
if agents := agentsByPolicy[agentPolicyID]; len(agents) > 0 {
agentIDs := make([]string, len(agents))
for i, agent := range agents {
agentIDs[i] = agent.ID
}
if err := Fleet.BulkUnenrollAgents(true, agentIDs...); err != nil {
return err
}
}
if err := Fleet.DeleteAgentPolicy(agentPolicyID); err != nil {
return err
}
}
return nil
}
// NewPackagePolicy returns a new fleettest.PackagePolicy with config vars, but does not create it.
//
// The returned package policy is suitable for passing to Fleet.CreatePackagePolicy.
func NewPackagePolicy(agentPolicy *fleettest.AgentPolicy, varValues, extraConfig map[string]interface{}) *fleettest.PackagePolicy {
// Package policy names must be globally unique. We generate unique agent
// policy names, so just append the package name to that.
packagePolicyName := agentPolicy.Name + "-apm"
packagePolicy := fleettest.NewPackagePolicy(IntegrationPackage, packagePolicyName, agentPolicy.Namespace, agentPolicy.ID)
packagePolicy.Package.Name = IntegrationPackage.Name
packagePolicy.Package.Version = IntegrationPackage.Version
packagePolicy.Package.Title = IntegrationPackage.Title
for _, input := range IntegrationPackage.PolicyTemplates[0].Inputs {
vars := make(map[string]interface{})
for _, inputVar := range input.Vars {
value, ok := varValues[inputVar.Name]
if !ok {
value = inputVarDefault(inputVar)
}
varMap := map[string]interface{}{"type": inputVar.Type}
if value != nil {
if inputVar.Type == "yaml" {
encoded, err := json.Marshal(value)
if err != nil {
panic(err)
}
value = string(encoded)
}
varMap["value"] = value
}
vars[inputVar.Name] = varMap
}
packagePolicy.Inputs = append(packagePolicy.Inputs, fleettest.PackagePolicyInput{
Type: input.Type,
Enabled: true,
Streams: []interface{}{},
Vars: vars,
Config: extraConfig,
})
}
return packagePolicy
}
func inputVarDefault(inputVar fleettest.PackagePolicyTemplateInputVar) interface{} {
if inputVar.Name == "host" {
return ":8200"
}
if inputVar.Default != nil {
defaultValue := inputVar.Default
if inputVar.Type == "yaml" {
var v interface{}
if err := yaml.Unmarshal([]byte(defaultValue.(string)), &v); err != nil {
panic(err)
}
defaultValue = v
}
return defaultValue
}
if inputVar.Multi {
return []interface{}{}
}
return nil
}
// SourceMap holds information about a source map stored by Kibana.
type SourceMap struct {
ID string `json:"id"`
Created time.Time `json:"created"`
Body map[string]interface{} `json:"body"`
}
// CreateSourceMap creates or replaces a source map with the given service name
// and version, and bundle filepath. CreateSourceMap returns the ID of the stored
// source map, which may be passed to DeleteSourceMap for cleanup.
func CreateSourceMap(t testing.TB, sourcemap []byte, serviceName, serviceVersion, bundleFilepath string) string {
t.Helper()
var data bytes.Buffer
mw := multipart.NewWriter(&data)
require.NoError(t, mw.WriteField("service_name", serviceName))
require.NoError(t, mw.WriteField("service_version", serviceVersion))
require.NoError(t, mw.WriteField("bundle_filepath", bundleFilepath))
sourcemapFileWriter, err := mw.CreateFormFile("sourcemap", "sourcemap.js.map")
require.NoError(t, err)
sourcemapFileWriter.Write(sourcemap)
require.NoError(t, mw.Close())
apiURL := *KibanaURL
apiURL.Path += "/api/apm/sourcemaps"
req, _ := http.NewRequest("POST", apiURL.String(), &data)
req.Header.Add("Content-Type", mw.FormDataContentType())
req.Header.Set("kbn-xsrf", "1")
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
require.NoError(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode, string(respBody))
var result struct {
ID string `json:"id"`
}
err = json.Unmarshal(respBody, &result)
require.NoError(t, err)
cleanPath := bundleFilepath
u, err := url.Parse(bundleFilepath)
if err == nil {
u.Fragment = ""
u.RawQuery = ""
u.Path = path.Clean(u.Path)
cleanPath = u.String()
}
id := serviceName + "-" + serviceVersion + "-" + cleanPath
estest.ExpectMinDocs(t, Elasticsearch, 1, ".apm-source-map", espoll.TermQuery{
Field: "_id",
Value: id,
})
t.Cleanup(func() {
DeleteSourceMap(t, result.ID)
})
return result.ID
}
// DeleteSourceMap deletes a source map with the given ID.
func DeleteSourceMap(t testing.TB, id string) {
t.Helper()
url := *KibanaURL
url.Path += "/api/apm/sourcemaps/" + id
req, _ := http.NewRequest("DELETE", url.String(), nil)
req.Header.Set("kbn-xsrf", "1")
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
require.NoError(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode, string(respBody))
}