This repository has been archived by the owner on Sep 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathendpoint.go
246 lines (199 loc) · 7.59 KB
/
endpoint.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
// 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 main
import (
"fmt"
"time"
"github.com/cenkalti/backoff/v4"
"github.com/cucumber/godog"
"github.com/elastic/e2e-testing/internal/common"
"github.com/elastic/e2e-testing/internal/deploy"
"github.com/elastic/e2e-testing/internal/utils"
log "github.com/sirupsen/logrus"
)
func (fts *FleetTestSuite) theHostNameIsNotShownInTheAdminViewInTheSecurityApp() error {
log.Trace("Checking if the hostname is not shown in the Administration view in the Security App")
maxTimeout := time.Duration(utils.TimeoutFactor) * time.Minute
retryCount := 1
exp := utils.GetExponentialBackOff(maxTimeout)
agentListedInSecurityFn := func() error {
agentService := deploy.NewServiceRequest(common.ElasticAgentServiceName)
manifest, _ := fts.getDeployer().GetServiceManifest(fts.currentContext, agentService)
host, err := fts.kibanaClient.IsAgentListedInSecurityApp(fts.currentContext, manifest.Hostname)
if err != nil {
log.WithFields(log.Fields{
"elapsedTime": exp.GetElapsedTime(),
"err": err,
"host": host,
"hostname": manifest.Hostname,
"retry": retryCount,
}).Warn("We could not check the agent in the Administration view in the Security App yet")
retryCount++
return err
}
log.WithFields(log.Fields{
"elapsedTime": exp.GetElapsedTime(),
"hostname": manifest.Hostname,
"retries": retryCount,
}).Info("The Agent is not listed in the Administration view in the Security App")
return nil
}
err := backoff.Retry(agentListedInSecurityFn, exp)
if err != nil {
return err
}
return nil
}
func (fts *FleetTestSuite) theHostNameIsShownInTheAdminViewInTheSecurityApp(status string) error {
log.Trace("Checking if the hostname is shown in the Admin view in the Security App")
maxTimeout := time.Duration(utils.TimeoutFactor) * time.Minute
retryCount := 1
exp := utils.GetExponentialBackOff(maxTimeout)
agentListedInSecurityFn := func() error {
agentService := deploy.NewServiceRequest(common.ElasticAgentServiceName)
manifest, _ := fts.getDeployer().GetServiceManifest(fts.currentContext, agentService)
matches, err := fts.kibanaClient.IsAgentListedInSecurityAppWithStatus(fts.currentContext, manifest.Hostname, status)
if err != nil || !matches {
log.WithFields(log.Fields{
"elapsedTime": exp.GetElapsedTime(),
"desiredStatus": status,
"err": err,
"hostname": manifest.Hostname,
"matches": matches,
"retry": retryCount,
}).Warn("The agent is not listed in the Administration view in the Security App in the desired status yet")
retryCount++
return err
}
log.WithFields(log.Fields{
"elapsedTime": exp.GetElapsedTime(),
"desiredStatus": status,
"hostname": manifest.Hostname,
"matches": matches,
"retries": retryCount,
}).Info("The Agent is listed in the Administration view in the Security App in the desired status")
return nil
}
err := backoff.Retry(agentListedInSecurityFn, exp)
if err != nil {
return err
}
return nil
}
func (fts *FleetTestSuite) thePolicyIsUpdatedToHaveMode(name string, mode string) error {
if name != "malware" {
log.WithFields(log.Fields{
"name": name,
}).Warn("We only support 'malware' policy to be updated")
return godog.ErrPending
}
if mode != "detect" && mode != "prevent" {
log.WithFields(log.Fields{
"name": name,
"mode": mode,
}).Warn("We only support 'detect' and 'prevent' modes")
return godog.ErrPending
}
packageDS, err := fts.kibanaClient.GetIntegrationFromAgentPolicy(fts.currentContext, "endpoint", fts.Policy)
if err != nil {
return err
}
fts.Integration = packageDS.Package
for _, item := range packageDS.Inputs {
if item.Type == "endpoint" {
item.Config.(map[string]interface{})["policy"].(map[string]interface{})["value"].(map[string]interface{})["windows"].(map[string]interface{})["malware"].(map[string]interface{})["mode"] = mode
item.Config.(map[string]interface{})["policy"].(map[string]interface{})["value"].(map[string]interface{})["mac"].(map[string]interface{})["malware"].(map[string]interface{})["mode"] = mode
}
}
log.WithFields(log.Fields{
"inputs": packageDS.Inputs,
}).Trace("Upgrading integration package config")
updatedAt, err := fts.kibanaClient.UpdateIntegrationPackagePolicy(fts.currentContext, packageDS)
if err != nil {
return err
}
// we use a string because we are not able to process what comes in the event, so we will do
// an alphabetical order, as they share same layout but different millis and timezone format
fts.PolicyUpdatedAt = updatedAt
return nil
}
func (fts *FleetTestSuite) thePolicyWillReflectTheChangeInTheSecurityApp() error {
agentService := deploy.NewServiceRequest(common.ElasticAgentServiceName)
manifest, _ := fts.getDeployer().GetServiceManifest(fts.currentContext, agentService)
agentID, err := fts.kibanaClient.GetAgentIDByHostname(fts.currentContext, manifest.Hostname)
if err != nil {
return err
}
pkgPolicy, err := fts.kibanaClient.GetIntegrationFromAgentPolicy(fts.currentContext, "endpoint", fts.Policy)
if err != nil {
return err
}
maxTimeout := time.Duration(utils.TimeoutFactor) * time.Minute * 2
retryCount := 1
exp := utils.GetExponentialBackOff(maxTimeout)
getEventsFn := func() error {
err := fts.kibanaClient.GetAgentEvents(fts.currentContext, "endpoint-security", agentID, pkgPolicy.ID, fts.PolicyUpdatedAt)
if err != nil {
log.WithFields(log.Fields{
"elapsedTime": exp.GetElapsedTime(),
"err": err,
"retries": retryCount,
}).Warn("There are no events for the agent in Fleet")
retryCount++
return err
}
log.WithFields(log.Fields{
"elapsedTime": exp.GetElapsedTime(),
"retries": retryCount,
}).Info("There are events for the agent in Fleet")
return nil
}
err = backoff.Retry(getEventsFn, exp)
if err != nil {
return err
}
return nil
}
func (fts *FleetTestSuite) thePolicyResponseWillBeShownInTheSecurityApp() error {
agentService := deploy.NewServiceRequest(common.ElasticAgentServiceName)
manifest, _ := fts.getDeployer().GetServiceManifest(fts.currentContext, agentService)
agentID, err := fts.kibanaClient.GetAgentIDByHostname(fts.currentContext, manifest.Hostname)
if err != nil {
return err
}
maxTimeout := time.Duration(utils.TimeoutFactor) * time.Minute
retryCount := 1
exp := utils.GetExponentialBackOff(maxTimeout)
getEventsFn := func() error {
listed, err := fts.kibanaClient.IsPolicyResponseListedInSecurityApp(fts.currentContext, agentID)
if err != nil {
log.WithFields(log.Fields{
"elapsedTime": exp.GetElapsedTime(),
"err": err,
"retries": retryCount,
}).Warn("Could not get metadata from the Administration view in the Security App yet")
retryCount++
return err
}
if !listed {
log.WithFields(log.Fields{
"agentID": agentID,
"elapsedTime": exp.GetElapsedTime(),
"retries": retryCount,
}).Warn("The policy response is not listed as 'success' in the Administration view in the Security App yet")
retryCount++
return fmt.Errorf("the policy response is not listed as 'success' in the Administration view in the Security App yet")
}
log.WithFields(log.Fields{
"elapsedTime": exp.GetElapsedTime(),
"retries": retryCount,
}).Info("The policy response is listed as 'success' in the Administration view in the Security App")
return nil
}
err = backoff.Retry(getEventsFn, exp)
if err != nil {
return err
}
return nil
}