Skip to content

Commit 357d4a8

Browse files
authored
Revert "Enhancement/6394 allow deb rpm to upgrade with endpoint tamper protection (#6907) (#8609)" (#8638)
This reverts commit fc01978.
1 parent 0a34bd9 commit 357d4a8

File tree

7 files changed

+43
-523
lines changed

7 files changed

+43
-523
lines changed

changelog/fragments/1740166208-allow-deb-rpm-upgrade-with-tamper-protected-endpoint.yaml

Lines changed: 0 additions & 30 deletions
This file was deleted.

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

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,42 +6,13 @@ commit_hash="{{ commit_short }}"
66
version_dir="{{agent_package_version}}{{snapshot_suffix}}"
77
symlink="/usr/share/elastic-agent/bin/elastic-agent"
88
new_agent_dir="/var/lib/elastic-agent/data/elastic-agent-$version_dir-$commit_hash"
9-
new_endpoint_component_bin="$new_agent_dir/components/endpoint-security"
109

1110
# delete $symlink if exists
1211
if test -L "$symlink"; then
1312
echo "found symlink $symlink, unlink"
1413
unlink "$symlink"
1514
fi
1615

17-
SERVICE_NAME="ElasticEndpoint"
18-
19-
echo "Checking if $SERVICE_NAME is installed"
20-
if systemctl list-unit-files --type=service | grep -q "^${SERVICE_NAME}.service"; then
21-
22-
installed_endpoint_version="$(/opt/Elastic/Endpoint/elastic-endpoint version)"
23-
archive_endpoint_version="$("$new_endpoint_component_bin" version)"
24-
25-
echo "${SERVICE_NAME} is installed at version ${installed_endpoint_version}"
26-
if ! systemctl is-active --quiet "$SERVICE_NAME"; then
27-
echo "$SERVICE_NAME is installed but not running"
28-
if [[ "$installed_endpoint_version" == "$archive_endpoint_version" ]]; then
29-
echo "New endpoint and installed endpoint versions are the same: \"${installed_endpoint_version}\""
30-
echo "Starting $SERVICE_NAME"
31-
sudo systemctl start ${SERVICE_NAME}
32-
else
33-
echo "New endpoint version \"${archive_endpoint_version}\" is different than the one that's already
34-
installed \"${installed_endpoint_version}\""
35-
fi
36-
else
37-
# Endpoint can already be running if tamper protection is not enabled
38-
echo "$SERVICE_NAME is already running"
39-
fi
40-
else
41-
echo "$SERVICE_NAME is not installed"
42-
fi
43-
44-
4516
# create symlink to the new agent
4617
echo "create symlink "$symlink" to "$new_agent_dir/elastic-agent""
4718
ln -s "$new_agent_dir/elastic-agent" "$symlink"

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

Lines changed: 21 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,6 @@
22

33
set -e
44

5-
SERVICE_NAME="ElasticEndpoint"
6-
7-
# Check if the endpoint service is installed
8-
if systemctl list-unit-files --type=service | grep -q "^${SERVICE_NAME}.service"; then
9-
echo "$SERVICE_NAME is installed"
10-
11-
# Remove the vault directory if it exists
12-
if [ -d "/opt/Elastic/Endpoint/state/vault" ]; then
13-
14-
# Check if the endpoint is running
15-
if systemctl --quiet is-active $SERVICE_NAME; then
16-
echo "$SERVICE_NAME is running, stopping it"
17-
systemctl --quiet stop $SERVICE_NAME
18-
else
19-
echo "$SERVICE_NAME is not running"
20-
fi
21-
22-
echo "$SERVICE_NAME is tamper protected"
23-
echo "Removing $SERVICE_NAME vault"
24-
rm -rf "/opt/Elastic/Endpoint/state/vault"
25-
else
26-
echo "$SERVICE_NAME tamper protection is not enabled"
27-
fi
28-
else
29-
echo "$SERVICE_NAME is not installed"
30-
fi
31-
325
commit_hash="{{ commit_short }}"
336
version_dir="{{agent_package_version}}{{snapshot_suffix}}"
347
symlink="/usr/share/elastic-agent/bin/elastic-agent"
@@ -39,27 +12,27 @@ old_agent_dir=""
3912
# upon upgrade we migrate the current symlink to an upgrade symlink as the previous
4013
# installed version will remove the symlink
4114
if test -L "$symlink"; then
42-
resolved_symlink="$(readlink -- "$symlink")"
43-
if ! [ -z "$resolved_symlink" ]; then
44-
old_agent_dir="$(dirname "$resolved_symlink")"
45-
echo "previous installation directory $old_agent_dir"
46-
else
47-
echo "unable to read existing symlink"
48-
fi
15+
resolved_symlink="$(readlink -- "$symlink")"
16+
if ! [ -z "$resolved_symlink" ]; then
17+
old_agent_dir="$( dirname "$resolved_symlink" )"
18+
echo "previous installation directory $old_agent_dir"
19+
else
20+
echo "unable to read existing symlink"
21+
fi
4922

50-
# copy the state files if there was a previous agent install
51-
if ! [ -z "$old_agent_dir" ] && ! [ "$old_agent_dir" -ef "$new_agent_dir" ]; then
52-
yml_path="$old_agent_dir/state.yml"
53-
enc_path="$old_agent_dir/state.enc"
54-
echo "migrate state from $old_agent_dir to $new_agent_dir"
23+
# copy the state files if there was a previous agent install
24+
if ! [ -z "$old_agent_dir" ] && ! [ "$old_agent_dir" -ef "$new_agent_dir" ]; then
25+
yml_path="$old_agent_dir/state.yml"
26+
enc_path="$old_agent_dir/state.enc"
27+
echo "migrate state from $old_agent_dir to $new_agent_dir"
5528

56-
if test -f "$yml_path"; then
57-
echo "found "$yml_path", copy to "$new_agent_dir"."
58-
mkdir -p "$new_agent_dir"
59-
cp "$yml_path" "$new_agent_dir"
60-
else
61-
echo "didn't find $yml_path"
62-
fi
29+
if test -f "$yml_path"; then
30+
echo "found "$yml_path", copy to "$new_agent_dir"."
31+
mkdir -p "$new_agent_dir"
32+
cp "$yml_path" "$new_agent_dir"
33+
else
34+
echo "didn't find $yml_path"
35+
fi
6336

6437
if test -f "$enc_path"; then
6538
echo "found "$enc_path", copy to "$new_agent_dir"."
@@ -80,7 +53,7 @@ if test -L "$symlink"; then
8053
fi
8154
fi
8255
else
83-
echo "no previous installation found"
56+
echo "no previous installation found"
8457

8558
# create dir in case it does not exist
8659
mkdir -p "$new_agent_dir"
@@ -98,3 +71,4 @@ else
9871
fi
9972
fi
10073
fi
74+

pkg/testing/fixture.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ func (f *Fixture) RunBeat(ctx context.Context) error {
346346
process.WithContext(ctx),
347347
process.WithArgs(args),
348348
process.WithCmdOptions(attachOutErr(stdOut, stdErr)))
349+
349350
if err != nil {
350351
return fmt.Errorf("failed to spawn %s: %w", f.binaryName, err)
351352
}
@@ -400,8 +401,7 @@ func RunProcess(t *testing.T,
400401
lp Logger,
401402
ctx context.Context, runLength time.Duration,
402403
logOutput, allowErrs bool,
403-
processPath string, args ...string,
404-
) error {
404+
processPath string, args ...string) error {
405405
if _, deadlineSet := ctx.Deadline(); !deadlineSet {
406406
t.Fatal("Context passed to RunProcess() has no deadline set.")
407407
}
@@ -419,6 +419,7 @@ func RunProcess(t *testing.T,
419419
process.WithContext(ctx),
420420
process.WithArgs(args),
421421
process.WithCmdOptions(attachOutErr(stdOut, stdErr)))
422+
422423
if err != nil {
423424
return fmt.Errorf("failed to spawn %q: %w", processPath, err)
424425
}
@@ -548,6 +549,7 @@ func (f *Fixture) executeWithClient(ctx context.Context, command string, disable
548549
process.WithContext(ctx),
549550
process.WithArgs(args),
550551
process.WithCmdOptions(attachOutErr(stdOut, stdErr)))
552+
551553
if err != nil {
552554
return fmt.Errorf("failed to spawn %s: %w", f.binaryName, err)
553555
}

pkg/testing/fixture_install.go

Lines changed: 14 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,12 @@ import (
2626
"github.com/elastic/elastic-agent-libs/mapstr"
2727
agentsystemprocess "github.com/elastic/elastic-agent-system-metrics/metric/system/process"
2828
"github.com/elastic/elastic-agent/internal/pkg/agent/application/paths"
29-
"github.com/elastic/elastic-agent/pkg/control"
3029
"github.com/elastic/elastic-agent/pkg/control/v2/client"
3130
"github.com/elastic/elastic-agent/pkg/core/process"
3231
)
3332

3433
// ErrNotInstalled is returned in cases where Agent isn't installed
35-
var ErrNotInstalled = errors.New("Elastic Agent is not installed") //nolint:staticcheck // Elastic Agent is a proper noun
34+
var ErrNotInstalled = errors.New("Elastic Agent is not installed") //nolint:stylecheck // Elastic Agent is a proper noun
3635

3736
// CmdOpts creates vectors of command arguments for different agent commands
3837
type CmdOpts interface {
@@ -331,12 +330,12 @@ func (f *Fixture) installNoPkgManager(ctx context.Context, installOpts *InstallO
331330

332331
// environment variable AGENT_KEEP_INSTALLED=true will skip the uninstallation
333332
// useful to debug the issue with the Elastic Agent
334-
if f.t.Failed() && KeepInstalledFlag() {
333+
if f.t.Failed() && keepInstalledFlag() {
335334
f.t.Logf("skipping uninstall; test failed and AGENT_KEEP_INSTALLED=true")
336335
return
337336
}
338337

339-
if KeepInstalledFlag() {
338+
if keepInstalledFlag() {
340339
f.t.Logf("ignoring AGENT_KEEP_INSTALLED=true as test succeeded, " +
341340
"keeping the agent installed will jeopardise other tests")
342341
}
@@ -446,19 +445,6 @@ func getProcesses(t *gotesting.T, regex string) []runningProcess {
446445
return processes
447446
}
448447

449-
func (f *Fixture) SetDebRpmClient() error {
450-
workDir := "/var/lib/elastic-agent"
451-
socketPath, err := control.AddressFromPath(f.operatingSystem, workDir)
452-
if err != nil {
453-
return fmt.Errorf("failed to get control protcol address: %w", err)
454-
}
455-
456-
c := client.New(client.WithAddress(socketPath))
457-
f.setClient(c)
458-
459-
return nil
460-
}
461-
462448
// installDeb installs the prepared Elastic Agent binary from the deb
463449
// package and registers a t.Cleanup function to uninstall the agent if
464450
// it hasn't been uninstalled. It also takes care of collecting a
@@ -496,7 +482,7 @@ func (f *Fixture) installDeb(ctx context.Context, installOpts *InstallOpts, shou
496482
f.t.Logf("error systemctl stop elastic-agent: %s, output: %s", err, string(out))
497483
}
498484

499-
if KeepInstalledFlag() {
485+
if keepInstalledFlag() {
500486
f.t.Logf("skipping uninstall; test failed and AGENT_KEEP_INSTALLED=true")
501487
return
502488
}
@@ -516,11 +502,6 @@ func (f *Fixture) installDeb(ctx context.Context, installOpts *InstallOpts, shou
516502
return out, fmt.Errorf("systemctl start elastic-agent failed: %w", err)
517503
}
518504

519-
err = f.SetDebRpmClient()
520-
if err != nil {
521-
return nil, err
522-
}
523-
524505
if !shouldEnroll {
525506
return nil, nil
526507
}
@@ -539,11 +520,11 @@ func (f *Fixture) installDeb(ctx context.Context, installOpts *InstallOpts, shou
539520
if installOpts.DelayEnroll {
540521
enrollArgs = append(enrollArgs, "--delay-enroll")
541522
}
542-
if installOpts.URL != "" {
543-
enrollArgs = append(enrollArgs, "--url", installOpts.URL)
523+
if installOpts.EnrollOpts.URL != "" {
524+
enrollArgs = append(enrollArgs, "--url", installOpts.EnrollOpts.URL)
544525
}
545-
if installOpts.EnrollmentToken != "" {
546-
enrollArgs = append(enrollArgs, "--enrollment-token", installOpts.EnrollmentToken)
526+
if installOpts.EnrollOpts.EnrollmentToken != "" {
527+
enrollArgs = append(enrollArgs, "--enrollment-token", installOpts.EnrollOpts.EnrollmentToken)
547528
}
548529
out, err = exec.CommandContext(ctx, "sudo", enrollArgs...).CombinedOutput()
549530
if err != nil {
@@ -612,11 +593,6 @@ func (f *Fixture) installRpm(ctx context.Context, installOpts *InstallOpts, shou
612593
return out, fmt.Errorf("systemctl start elastic-agent failed: %w", err)
613594
}
614595

615-
err = f.SetDebRpmClient()
616-
if err != nil {
617-
return nil, err
618-
}
619-
620596
if !shouldEnroll {
621597
return nil, nil
622598
}
@@ -635,11 +611,11 @@ func (f *Fixture) installRpm(ctx context.Context, installOpts *InstallOpts, shou
635611
if installOpts.DelayEnroll {
636612
enrollArgs = append(enrollArgs, "--delay-enroll")
637613
}
638-
if installOpts.URL != "" {
639-
enrollArgs = append(enrollArgs, "--url", installOpts.URL)
614+
if installOpts.EnrollOpts.URL != "" {
615+
enrollArgs = append(enrollArgs, "--url", installOpts.EnrollOpts.URL)
640616
}
641-
if installOpts.EnrollmentToken != "" {
642-
enrollArgs = append(enrollArgs, "--enrollment-token", installOpts.EnrollmentToken)
617+
if installOpts.EnrollOpts.EnrollmentToken != "" {
618+
enrollArgs = append(enrollArgs, "--enrollment-token", installOpts.EnrollOpts.EnrollmentToken)
643619
}
644620
// run sudo elastic-agent enroll
645621
out, err = exec.CommandContext(ctx, "sudo", enrollArgs...).CombinedOutput()
@@ -746,7 +722,7 @@ func (f *Fixture) uninstallNoPkgManager(ctx context.Context, uninstallOpts *Unin
746722
}
747723

748724
if err != nil && topPathStats != nil {
749-
return out, fmt.Errorf("Elastic Agent is still installed at [%s]", topPath) //nolint:staticcheck // Elastic Agent is a proper noun
725+
return out, fmt.Errorf("Elastic Agent is still installed at [%s]", topPath) //nolint:stylecheck // Elastic Agent is a proper noun
750726
}
751727

752728
return out, nil
@@ -849,7 +825,7 @@ func collectDiagFlag() bool {
849825
return v
850826
}
851827

852-
func KeepInstalledFlag() bool {
828+
func keepInstalledFlag() bool {
853829
// failure reports false (ignore error)
854830
v, _ := strconv.ParseBool(os.Getenv("AGENT_KEEP_INSTALLED"))
855831
return v

pkg/testing/tools/tools.go

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,6 @@ func IsPolicyRevision(ctx context.Context, t *testing.T, client *kibana.Client,
3636
}
3737
}
3838

39-
func GetUninstallToken(ctx context.Context, kibClient *kibana.Client, policyId string) (string, error) {
40-
resp, err := kibClient.GetPolicyUninstallTokens(ctx, policyId)
41-
if err != nil {
42-
return "", fmt.Errorf("failed to fetch uninstall tokens: %w", err)
43-
}
44-
if len(resp.Items) == 0 {
45-
return "", fmt.Errorf("expected non-zero number of tokens")
46-
}
47-
48-
if len(resp.Items[0].Token) == 0 {
49-
return "", fmt.Errorf("expected non-empty token")
50-
}
51-
52-
return resp.Items[0].Token, nil
53-
}
54-
5539
// InstallAgentWithPolicy creates the given policy, enrolls the given agent
5640
// fixture in Fleet using the default Fleet Server, waits for the agent to be
5741
// online, and returns the created policy.
@@ -153,6 +137,7 @@ func InstallAgentForPolicyWithToken(ctx context.Context, t *testing.T,
153137

154138
installOpts.URL = fleetServerURL
155139
}
140+
156141
output, err := agentFixture.Install(ctx, &installOpts)
157142
if err != nil {
158143
t.Log(string(output))

0 commit comments

Comments
 (0)