Skip to content

Commit 8039a05

Browse files
blakerousepchila
andauthored
Fix issue with --delay-enroll and --unprivileged on Windows (#4779)
* Perform fix permissions after delay enrollment. Add integration tests for privileged/unprivileged and Windows. * Add changelog entry. * Fix import. * Update testing/integration/delay_enroll_test.go Co-authored-by: Paolo Chilà <paolo.chila@elastic.co> --------- Co-authored-by: Paolo Chilà <paolo.chila@elastic.co>
1 parent 76573de commit 8039a05

File tree

3 files changed

+107
-6
lines changed

3 files changed

+107
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Kind can be one of:
2+
# - breaking-change: a change to previously-documented behavior
3+
# - deprecation: functionality that is being removed in a later release
4+
# - bug-fix: fixes a problem in a previous version
5+
# - enhancement: extends functionality but does not break or fix existing behavior
6+
# - feature: new functionality
7+
# - known-issue: problems that we are aware of in a given version
8+
# - security: impacts on the security of a product or a user’s deployment.
9+
# - upgrade: important information for someone upgrading from a prior version
10+
# - other: does not fit into any of the other categories
11+
kind: bug-fix
12+
13+
# Change summary; a 80ish characters long description of the change.
14+
summary: Fix delay enrollment to work in unprivileged mode on Windows
15+
16+
# Long description; in case the summary is not enough to describe the change
17+
# this field accommodate a description without length limits.
18+
# NOTE: This field will be rendered only for breaking-change and known-issue kinds at the moment.
19+
#description:
20+
21+
# Affected component; usually one of "elastic-agent", "fleet-server", "filebeat", "metricbeat", "auditbeat", "all", etc.
22+
component:
23+
24+
# PR URL; optional; the PR number that added the changeset.
25+
# If not present is automatically filled by the tooling finding the PR where this changelog fragment has been added.
26+
# NOTE: the tooling supports backports, so it's able to fill the original PR number instead of the backport PR number.
27+
# Please provide it if you are adding a fragment for a different PR.
28+
pr: https://github.com/elastic/elastic-agent/pull/4779
29+
30+
# Issue URL; optional; the GitHub issue related to this changeset (either closes or is part of).
31+
# If not present is automatically filled by the tooling with the issue linked to the PR number.
32+
issue: https://github.com/elastic/elastic-agent/issues/4678

internal/pkg/agent/cmd/enroll_cmd.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,18 @@ func (c *enrollCmd) Execute(ctx context.Context, streams *cli.IOStreams) error {
255255
if c.options.FleetServer.Host != "" {
256256
return errors.New("--delay-enroll cannot be used with --fleet-server-es", errors.TypeConfig)
257257
}
258-
return c.writeDelayEnroll(streams)
258+
err = c.writeDelayEnroll(streams)
259+
if err != nil {
260+
// context for error already provided in writeDelayEnroll
261+
return err
262+
}
263+
if c.options.FixPermissions != nil {
264+
err = perms.FixPermissions(paths.Top(), perms.WithOwnership(*c.options.FixPermissions))
265+
if err != nil {
266+
return errors.New(err, "failed to fix permissions")
267+
}
268+
}
269+
return nil
259270
}
260271

261272
err = c.enrollWithBackoff(ctx, persistentConfig)

testing/integration/delay_enroll_test.go

+63-5
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ package integration
99
import (
1010
"context"
1111
"fmt"
12-
"os/exec"
1312
"testing"
1413
"time"
1514

1615
"github.com/google/uuid"
1716
"github.com/stretchr/testify/require"
1817

1918
"github.com/elastic/elastic-agent-libs/kibana"
19+
"github.com/elastic/elastic-agent/internal/pkg/agent/install"
2020
atesting "github.com/elastic/elastic-agent/pkg/testing"
2121
"github.com/elastic/elastic-agent/pkg/testing/define"
2222
"github.com/elastic/elastic-agent/pkg/testing/tools"
@@ -30,7 +30,6 @@ func TestDelayEnroll(t *testing.T) {
3030
Stack: &define.Stack{},
3131
Local: false,
3232
Sudo: true,
33-
OS: []define.OS{{Type: define.Linux}},
3433
})
3534

3635
ctx, cancel := testcontext.WithDeadline(t, context.Background(), time.Now().Add(10*time.Minute))
@@ -64,6 +63,7 @@ func TestDelayEnroll(t *testing.T) {
6463
NonInteractive: true,
6564
Force: true,
6665
DelayEnroll: true,
66+
Privileged: false,
6767
}
6868
// Install the Elastic-Agent with the policy that was just
6969
// created.
@@ -77,11 +77,69 @@ func TestDelayEnroll(t *testing.T) {
7777
require.NoError(t, err)
7878

7979
// Start elastic-agent via service, this should do the enrollment
80-
cmd := exec.Command("/usr/bin/systemctl", "start", "elastic-agent")
81-
stdErrStdout, err := cmd.CombinedOutput()
82-
require.NoErrorf(t, err, "systemctl start elastic-agent output was %s", stdErrStdout)
80+
err = install.StartService("") // topPath can be blank as this is only starting the service
81+
require.NoErrorf(t, err, "failed to start service")
8382

8483
// check to make sure enroll worked
8584
check.ConnectedToFleet(ctx, t, agentFixture, 5*time.Minute)
85+
}
86+
87+
func TestDelayEnrollUnprivileged(t *testing.T) {
88+
info := define.Require(t, define.Requirements{
89+
Group: Fleet,
90+
Stack: &define.Stack{},
91+
Local: false,
92+
Sudo: true,
93+
})
94+
95+
ctx, cancel := testcontext.WithDeadline(t, context.Background(), time.Now().Add(10*time.Minute))
96+
defer cancel()
97+
98+
agentFixture, err := define.NewFixtureFromLocalBuild(t, define.Version())
99+
require.NoError(t, err)
100+
101+
// 1. Create a policy in Fleet with monitoring enabled.
102+
// To ensure there are no conflicts with previous test runs against
103+
// the same ESS stack, we add a UUID at the end of the policy
104+
// name. This policy does not contain any integration.
105+
t.Log("Enrolling agent in Fleet with a test policy")
106+
createPolicyReq := kibana.AgentPolicy{
107+
Name: fmt.Sprintf("test-policy-enroll-%s", uuid.New().String()),
108+
Namespace: info.Namespace,
109+
Description: "test policy for agent enrollment",
110+
MonitoringEnabled: []kibana.MonitoringEnabledOption{
111+
kibana.MonitoringEnabledLogs,
112+
kibana.MonitoringEnabledMetrics,
113+
},
114+
AgentFeatures: []map[string]interface{}{
115+
{
116+
"name": "test_enroll",
117+
"enabled": true,
118+
},
119+
},
120+
}
86121

122+
installOpts := atesting.InstallOpts{
123+
NonInteractive: true,
124+
Force: true,
125+
DelayEnroll: true,
126+
Privileged: false,
127+
}
128+
// Install the Elastic-Agent with the policy that was just
129+
// created.
130+
_, err = tools.InstallAgentWithPolicy(
131+
ctx,
132+
t,
133+
installOpts,
134+
agentFixture,
135+
info.KibanaClient,
136+
createPolicyReq)
137+
require.NoError(t, err)
138+
139+
// Start elastic-agent via service, this should do the enrollment
140+
err = install.StartService("") // topPath can be blank as this is only starting the service
141+
require.NoErrorf(t, err, "failed to start service")
142+
143+
// check to make sure enroll worked
144+
check.ConnectedToFleet(ctx, t, agentFixture, 5*time.Minute)
87145
}

0 commit comments

Comments
 (0)