Skip to content

Commit 249885f

Browse files
[deb/rpm] restart endpoint with tamper protection after elastic-agent (#8637)
* fix: use rpm from local build
1 parent f2b9e43 commit 249885f

File tree

2 files changed

+45
-32
lines changed

2 files changed

+45
-32
lines changed

dev-tools/packaging/templates/linux/postinstall.sh.tmpl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ if test -L "$symlink"; then
1515
fi
1616

1717
SERVICE_NAME="ElasticEndpoint"
18+
should_restart_endpoint=false
1819

1920
echo "Checking if $SERVICE_NAME is installed"
2021
if systemctl list-unit-files --type=service | grep -q "^${SERVICE_NAME}.service"; then
@@ -27,8 +28,7 @@ if systemctl list-unit-files --type=service | grep -q "^${SERVICE_NAME}.service"
2728
echo "$SERVICE_NAME is installed but not running"
2829
if [[ "$installed_endpoint_version" == "$archive_endpoint_version" ]]; then
2930
echo "New endpoint and installed endpoint versions are the same: \"${installed_endpoint_version}\""
30-
echo "Starting $SERVICE_NAME"
31-
sudo systemctl start ${SERVICE_NAME}
31+
should_restart_endpoint=true
3232
else
3333
echo "New endpoint version \"${archive_endpoint_version}\" is different than the one that's already
3434
installed \"${installed_endpoint_version}\""
@@ -53,4 +53,9 @@ echo "systemd enable/restart elastic-agent"
5353
systemctl daemon-reload 2> /dev/null
5454
systemctl enable elastic-agent 2> /dev/null || true
5555
systemctl restart elastic-agent 2> /dev/null || true
56+
# restart endpoint if needed
57+
if [ "$should_restart_endpoint" = true ]; then
58+
echo "Starting $SERVICE_NAME after elastic-agent"
59+
systemctl restart "$SERVICE_NAME" 2> /dev/null || true
60+
fi
5661
exit 0

testing/integration/endpoint_security_test.go

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,6 @@ func TestUpgradeAgentWithTamperProtectedEndpoint_RPM(t *testing.T) {
109109
},
110110
})
111111

112-
t.Skip("https://github.com/elastic/elastic-agent/issues/8613: Flaky uninstall token issue")
113-
114112
t.Run("Upgrade from older version to newer version", func(t *testing.T) {
115113
upgradeFromVersion, err := upgradetest.PreviousMinor()
116114
require.NoError(t, err)
@@ -197,14 +195,22 @@ func addEndpointCleanup(t *testing.T, uninstallToken string) {
197195
}
198196

199197
func installFirstAgent(ctx context.Context, t *testing.T, info *define.Info, isProtected bool, packageFormat string, upgradeFromVersion string) (*atesting.Fixture, string) {
200-
fixture, err := atesting.NewFixture(
201-
t,
202-
upgradeFromVersion,
203-
atesting.WithFetcher(atesting.ArtifactFetcher()),
204-
atesting.WithPackageFormat(packageFormat),
205-
)
206-
require.NoError(t, err)
207-
fixture.Prepare(ctx)
198+
var fixture *atesting.Fixture
199+
var err error
200+
201+
if upgradeFromVersion == define.Version() {
202+
fixture, err = define.NewFixtureFromLocalBuild(t, define.Version(), atesting.WithPackageFormat(packageFormat))
203+
} else {
204+
fixture, err = atesting.NewFixture(
205+
t,
206+
upgradeFromVersion,
207+
atesting.WithFetcher(atesting.ArtifactFetcher()),
208+
atesting.WithPackageFormat(packageFormat),
209+
)
210+
}
211+
require.NoError(t, err, "failed to create fixture")
212+
err = fixture.Prepare(ctx)
213+
require.NoError(t, err, "failed to prepare fixture")
208214

209215
t.Log("Creating a generic policy and enrollment token")
210216
policy := createBasicPolicy()
@@ -225,11 +231,12 @@ func installFirstAgent(ctx context.Context, t *testing.T, info *define.Info, isP
225231

226232
t.Log("Get the policy uninstall token")
227233
uninstallToken, err := tools.GetUninstallToken(ctx, info.KibanaClient, policyResp.ID)
228-
require.NoError(t, err)
234+
require.NoError(t, err, "failed to get uninstall token")
229235

230236
opts := atesting.InstallOpts{}
231237
t.Log("Install and enroll the first agent")
232-
tools.InstallAgentForPolicyWithToken(ctx, t, opts, fixture, info.KibanaClient, enrollKeyResp)
238+
_, err = tools.InstallAgentForPolicyWithToken(ctx, t, opts, fixture, info.KibanaClient, enrollKeyResp)
239+
require.NoError(t, err, "failed to install agent for policy with token")
233240

234241
addEndpointCleanup(t, uninstallToken)
235242

@@ -246,9 +253,6 @@ func installFirstAgent(ctx context.Context, t *testing.T, info *define.Info, isP
246253

247254
t.Log("The initial installation of both the agent and endpoint are healthy")
248255

249-
initEndpointVersion := getEndpointVersion(t)
250-
t.Logf("The initial endpoint version is %s", initEndpointVersion)
251-
252256
return fixture, uninstallToken
253257
}
254258

@@ -270,7 +274,8 @@ func testUnprotectedInstallUpgrade(
270274
t.Log("Setup agent fixture with the test build")
271275
fixture, err := define.NewFixtureFromLocalBuild(t, define.Version(), atesting.WithPackageFormat(packageFormat))
272276
require.NoError(t, err)
273-
fixture.Prepare(ctx)
277+
err = fixture.Prepare(ctx)
278+
require.NoError(t, err, "failed to prepare fixture")
274279

275280
t.Log("Getting source package")
276281
srcPkg, err := fixture.SrcPackage(ctx)
@@ -284,7 +289,8 @@ func testUnprotectedInstallUpgrade(
284289
t.Log(string(out))
285290
require.NoError(t, err, "agent installation with package manager should not fail")
286291

287-
fixture.SetDebRpmClient()
292+
err = fixture.SetDebRpmClient()
293+
require.NoError(t, err, "could not set DEB/RPM client")
288294

289295
upgradedAgentClient := fixture.Client()
290296
err = upgradedAgentClient.Connect(ctx)
@@ -353,23 +359,25 @@ func testTamperProtectedInstallUpgrade(
353359
if checkVersionUpgrade {
354360
t.Log("Setup agent fixture with the test build")
355361
fixture, err = define.NewFixtureFromLocalBuild(t, define.Version(), atesting.WithPackageFormat(packageFormat))
356-
require.NoError(t, err)
357-
fixture.Prepare(ctx)
362+
require.NoError(t, err, "failed to create agent fixture")
363+
err = fixture.Prepare(ctx)
364+
require.NoError(t, err, "failed to prepare agent fixture")
358365
}
359366

360367
t.Log("Getting source package")
361368
srcPkg, err := fixture.SrcPackage(ctx)
362-
require.NoError(t, err)
369+
require.NoError(t, err, "failed to get source package")
363370

364371
t.Log("Installing the second agent, upgrading from the older version")
365372
installCmd, err := getInstallCommand(ctx, fixture.PackageFormat(), srcPkg, nil)
366-
require.NoError(t, err)
373+
require.NoError(t, err, "failed to get install command")
367374

368375
out, err = installCmd.CombinedOutput()
369376
t.Log(string(out))
370377
require.NoError(t, err, "agent installation with package manager should not fail")
371378

372-
fixture.SetDebRpmClient()
379+
err = fixture.SetDebRpmClient()
380+
require.NoError(t, err, "failed to set deb/rpm client")
373381

374382
upgradedAgentClient := fixture.Client()
375383
err = upgradedAgentClient.Connect(ctx)
@@ -388,30 +396,30 @@ func testTamperProtectedInstallUpgrade(
388396
t.Logf("The upgraded endpoint version is %s", upgradedEndpointVersion)
389397

390398
startEndpointVersion, err := version.ParseVersion(initEndpointVersion)
391-
require.NoError(t, err)
399+
require.NoError(t, err, "failed to parse initial endpoint version")
392400

393401
parsedUpgradedVersion, err := version.ParseVersion(upgradedEndpointVersion)
394-
require.NoError(t, err)
402+
require.NoError(t, err, "failed to parse upgraded endpoint version")
395403

396404
t.Logf("Comparing start version %s to upgraded version %s", startEndpointVersion.String(), parsedUpgradedVersion.String())
397405
require.True(t, startEndpointVersion.Less(*parsedUpgradedVersion))
398406
}
399407

400408
// try to uninstall the agent without token and assert that endpoint is not removed
401-
t.Log("trying to uinstall without token, expecting error")
409+
t.Log("trying to uninstall without token, expecting error")
402410
out, err = exec.Command("sudo", "elastic-agent", "uninstall", "-f").CombinedOutput()
403411
t.Log(string(out))
404412
require.Error(t, err, "uninstalling agent without a token should fail because of tamper protection")
405413
t.Log("tamper protection for the upgraded agent is enabled")
406414

407-
// uninstall with the uninstall token and assert that endpoint is indeed removed.
408-
t.Log("trying to uinstall with token, not expecting any error")
415+
// uninstall with the token and assert that endpoint is indeed removed.
416+
t.Log("trying to uninstall with token, not expecting any error")
409417
out, err = exec.Command("sudo", "elastic-agent", "uninstall", "-f", "--uninstall-token", uninstallToken).CombinedOutput()
410418
t.Log(string(out))
411419
require.NoError(t, err, string(out))
412420

413421
_, err = exec.LookPath("elastic-agent")
414-
require.Error(t, err)
422+
require.Error(t, err, "expected elastic-agent binary to not exist in PATH after uninstall")
415423

416424
t.Log("successfully uninstalled endpoint using the uninstall token")
417425
}
@@ -1239,14 +1247,14 @@ func TestForceInstallOverProtectedPolicy(t *testing.T) {
12391247
PolicyID: policy.ID,
12401248
})
12411249
require.NoError(t, err)
1242-
url, err := fleettools.DefaultURL(ctx, info.KibanaClient)
1250+
fleetURL, err := fleettools.DefaultURL(ctx, info.KibanaClient)
12431251
require.NoError(t, err)
12441252

12451253
args := []string{
12461254
"install",
12471255
"--force",
12481256
"--url",
1249-
url,
1257+
fleetURL,
12501258
"--enrollment-token",
12511259
token.APIKey,
12521260
}

0 commit comments

Comments
 (0)