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
/
elastic_agent_enroll.go
125 lines (101 loc) · 3.85 KB
/
elastic_agent_enroll.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
// 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"
"strings"
"time"
"github.com/elastic/e2e-testing/internal/common"
"github.com/elastic/e2e-testing/internal/deploy"
"github.com/elastic/e2e-testing/internal/installer"
"github.com/elastic/e2e-testing/internal/utils"
log "github.com/sirupsen/logrus"
"go.elastic.co/apm/v2"
)
func (fts *FleetTestSuite) anAttemptToEnrollANewAgentFails() error {
log.Trace("Enrolling a new agent with an revoked token")
serviceName := common.ElasticAgentServiceName
agentService := deploy.NewServiceRequest(serviceName)
agentInstaller, _ := installer.Attach(fts.currentContext, fts.getDeployer(), agentService, fts.InstallerType)
err := agentInstaller.Uninstall(fts.currentContext)
if err != nil {
log.Errorf("could not uninstall the current agent: %v", err)
return err
}
err = fts.deployAgentToFleet(InstallerType(fts.InstallerType))
if err == nil {
err = fmt.Errorf("the agent was enrolled although the token was previously revoked")
log.WithFields(log.Fields{
"tokenID": fts.CurrentTokenID,
"error": err,
}).Error(err.Error())
return err
}
// checking the error message produced by the install command in TAR installer
// to distinguish from other install errors
if err != nil && strings.Contains(err.Error(), "Error: enroll command failed") {
log.WithFields(log.Fields{
"err": err,
"token": fts.CurrentToken,
}).Debug("As expected, it's not possible to enroll an agent with a revoked token")
return nil
}
return nil
}
func (fts *FleetTestSuite) theAgentIsUnenrolled() error {
return fts.unenrollHostname()
}
func (fts *FleetTestSuite) theAgentIsReenrolledOnTheHost() error {
log.Trace("Re-enrolling the agent on the host with same token")
agentService := deploy.NewServiceRequest(common.ElasticAgentServiceName)
agentInstaller, _ := installer.Attach(fts.currentContext, fts.getDeployer(), agentService, fts.InstallerType)
err := agentInstaller.Enroll(fts.currentContext, fts.CurrentToken, fts.ElasticAgentFlags)
if err != nil {
return err
}
return nil
}
func (fts *FleetTestSuite) theEnrollmentTokenIsRevoked() error {
log.WithFields(log.Fields{
"token": fts.CurrentToken,
"tokenID": fts.CurrentTokenID,
}).Trace("Revoking enrollment token")
err := fts.kibanaClient.DeleteEnrollmentAPIKey(fts.currentContext, fts.CurrentTokenID)
if err != nil {
return err
}
log.WithFields(log.Fields{
"token": fts.CurrentToken,
"tokenID": fts.CurrentTokenID,
}).Debug("Token was revoked")
// FIXME: Remove once https://github.com/elastic/kibana/issues/105078 is addressed
utils.Sleep(time.Duration(utils.TimeoutFactor) * 20 * time.Second)
return nil
}
// unenrollHostname deletes the statuses for an existing agent, filtering by hostname
func (fts *FleetTestSuite) unenrollHostname() error {
span, _ := apm.StartSpanOptions(fts.currentContext, "Unenrolling hostname", "elastic-agent.hostname.unenroll", apm.SpanOptions{
Parent: apm.SpanFromContext(fts.currentContext).TraceContext(),
})
defer span.End()
agentService := deploy.NewServiceRequest(common.ElasticAgentServiceName)
manifest, _ := fts.getDeployer().GetServiceManifest(fts.currentContext, agentService)
log.Tracef("Un-enrolling all agentIDs for %s", manifest.Hostname)
agents, err := fts.kibanaClient.ListAgents(fts.currentContext)
if err != nil {
return err
}
for _, agent := range agents {
if agent.LocalMetadata.Host.HostName == manifest.Hostname {
log.WithFields(log.Fields{
"hostname": manifest.Hostname,
}).Debug("Un-enrolling agent in Fleet")
err := fts.kibanaClient.UnEnrollAgent(fts.currentContext, agent.LocalMetadata.Host.HostName)
if err != nil {
return err
}
}
}
return nil
}