Skip to content

Commit 3451d88

Browse files
basic cleanup
1 parent 04b64fb commit 3451d88

File tree

4 files changed

+138
-6
lines changed

4 files changed

+138
-6
lines changed

internal/pkg/agent/application/coordinator/coordinator.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ type Coordinator struct {
285285
// Should only be interacted with via CoordinatorActive() or runLoopIteration()
286286
heartbeatChan chan struct{}
287287

288-
servicePidUpdate chan struct{}
288+
compPidUpdate chan struct{}
289289
}
290290

291291
// The channels Coordinator reads to receive updates from the various managers.
@@ -380,7 +380,7 @@ func New(logger *logger.Logger, cfg *configuration.Configuration, logLevel logp.
380380
overrideStateChan: make(chan *coordinatorOverrideState),
381381
upgradeDetailsChan: make(chan *details.Details),
382382
heartbeatChan: make(chan struct{}),
383-
servicePidUpdate: make(chan struct{}, 1),
383+
compPidUpdate: make(chan struct{}, 1),
384384
}
385385
// Setup communication channels for any non-nil components. This pattern
386386
// lets us transparently accept nil managers / simulated events during
@@ -1035,8 +1035,7 @@ func (c *Coordinator) runLoopIteration(ctx context.Context) {
10351035

10361036
case c.heartbeatChan <- struct{}{}:
10371037

1038-
case <-c.servicePidUpdate:
1039-
c.logger.Infof("got pid service update, refreshing config")
1038+
case <-c.compPidUpdate:
10401039
err := c.refreshComponentModel(ctx)
10411040
if err != nil {
10421041
err = fmt.Errorf("error refreshing component model for PID update: %w", err)

internal/pkg/agent/application/coordinator/coordinator_state.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ func (c *Coordinator) applyComponentState(state runtime.ComponentComponentState)
169169
c.stateNeedsRefresh = true
170170

171171
if pidRequiresUpdate {
172-
c.servicePidUpdate <- struct{}{}
172+
c.compPidUpdate <- struct{}{}
173173
}
174174
}
175175

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
2+
// or more contributor license agreements. Licensed under the Elastic License;
3+
// you may not use this file except in compliance with the Elastic License.
4+
5+
//go:build integration
6+
7+
package integration
8+
9+
import (
10+
"bytes"
11+
"context"
12+
_ "embed"
13+
"encoding/json"
14+
"fmt"
15+
"strings"
16+
"testing"
17+
"text/template"
18+
"time"
19+
20+
"github.com/google/uuid"
21+
22+
"github.com/elastic/elastic-agent-libs/kibana"
23+
"github.com/elastic/elastic-agent/pkg/control/v2/client"
24+
"github.com/elastic/elastic-agent/pkg/testing/define"
25+
)
26+
27+
//go:embed endpoint_security_package.json.tmpl
28+
var endpointPackagePolicyTemplate string
29+
30+
type endpointPackageTemplateVars struct {
31+
ID string
32+
Name string
33+
PolicyID string
34+
Version string
35+
}
36+
37+
// TODO: Setup a GitHub Action to update this for each release of https://github.com/elastic/endpoint-package
38+
const endpointPackageVersion = "8.11.0"
39+
40+
func agentAndEndpointAreHealthy(t *testing.T, ctx context.Context, agentClient client.Client) bool {
41+
t.Helper()
42+
43+
state, err := agentClient.State(ctx)
44+
if err != nil {
45+
t.Logf("Error getting agent state: %s", err)
46+
return false
47+
}
48+
49+
if state.State != client.Healthy {
50+
t.Logf("local Agent is not Healthy: current state: %+v", state)
51+
return false
52+
}
53+
54+
foundEndpointInputUnit := false
55+
foundEndpointOutputUnit := false
56+
for _, comp := range state.Components {
57+
isEndpointComponent := strings.Contains(comp.Name, "endpoint")
58+
if comp.State != client.Healthy {
59+
t.Logf("endpoint component is not Healthy: current state: %+v", comp)
60+
return false
61+
}
62+
63+
for _, unit := range comp.Units {
64+
if isEndpointComponent {
65+
if unit.UnitType == client.UnitTypeInput {
66+
foundEndpointInputUnit = true
67+
}
68+
if unit.UnitType == client.UnitTypeOutput {
69+
foundEndpointOutputUnit = true
70+
}
71+
}
72+
73+
if unit.State != client.Healthy {
74+
t.Logf("unit %q is not Healthy\n%+v", unit.UnitID, unit)
75+
return false
76+
}
77+
}
78+
}
79+
80+
// Ensure both the endpoint input and output units were found and healthy.
81+
if !foundEndpointInputUnit || !foundEndpointOutputUnit {
82+
t.Logf("State did not contain endpoint units (input: %v/output: %v) state: %+v. ", foundEndpointInputUnit, foundEndpointOutputUnit, state)
83+
return false
84+
}
85+
86+
return true
87+
}
88+
89+
// Installs the Elastic Defend package to cause the agent to install the endpoint-security service.
90+
func installElasticDefendPackage(t *testing.T, info *define.Info, policyID string) (r kibana.PackagePolicyResponse, err error) {
91+
t.Helper()
92+
93+
t.Log("Templating endpoint package policy request")
94+
tmpl, err := template.New("pkgpolicy").Parse(endpointPackagePolicyTemplate)
95+
if err != nil {
96+
return r, fmt.Errorf("error creating new template: %w", err)
97+
}
98+
99+
packagePolicyID := uuid.New().String()
100+
var pkgPolicyBuf bytes.Buffer
101+
102+
// Need unique name for Endpoint integration otherwise on multiple runs on the same instance you get
103+
// http error response with code 409: {StatusCode:409 Error:Conflict Message:An integration policy with the name Defend-cbomziz4uvn5fov9t1gsrcvdwn2p1s7tefnvgsye already exists. Please rename it or choose a different name.}
104+
err = tmpl.Execute(&pkgPolicyBuf, endpointPackageTemplateVars{
105+
ID: packagePolicyID,
106+
Name: "Defend-" + packagePolicyID,
107+
PolicyID: policyID,
108+
Version: endpointPackageVersion,
109+
})
110+
if err != nil {
111+
return r, fmt.Errorf("error executing template: %w", err)
112+
}
113+
114+
// Make sure the templated value is actually valid JSON before making the API request.
115+
// Using json.Unmarshal will give us the actual syntax error, calling json.Valid() would not.
116+
packagePolicyReq := kibana.PackagePolicyRequest{}
117+
err = json.Unmarshal(pkgPolicyBuf.Bytes(), &packagePolicyReq)
118+
if err != nil {
119+
return r, fmt.Errorf("templated package policy is not valid JSON: %s, %w", pkgPolicyBuf.String(), err)
120+
}
121+
122+
t.Log("POST /api/fleet/package_policies")
123+
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
124+
defer cancel()
125+
126+
pkgResp, err := info.KibanaClient.InstallFleetPackage(ctx, packagePolicyReq)
127+
if err != nil {
128+
t.Logf("Error installing fleet package: %v", err)
129+
return r, fmt.Errorf("error installing fleet package: %w", err)
130+
}
131+
t.Logf("Endpoint package Policy Response:\n%+v", pkgResp)
132+
return pkgResp, err
133+
}

testing/integration/monitoring_endpoint_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type EndpointMetricsMonRunner struct {
3131

3232
func TestEndpointAgentServiceMonitoring(t *testing.T) {
3333
info := define.Require(t, define.Requirements{
34-
Group: "fleet",
34+
Group: Fleet,
3535
Stack: &define.Stack{},
3636
Local: false, // requires Agent installation
3737
Sudo: true, // requires Agent installation

0 commit comments

Comments
 (0)