Skip to content
This repository has been archived by the owner on Sep 17, 2024. It is now read-only.

Commit

Permalink
fix: agent directories must belong to root (#2590)
Browse files Browse the repository at this point in the history
* chore: store agent pat into the metadata struct of the agent package

* chore: create agent directories for UNIX-based agent installers
  • Loading branch information
mdelapenya authored Jun 2, 2022
1 parent df6b3c8 commit 274a01a
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 6 deletions.
1 change: 1 addition & 0 deletions internal/deploy/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ type ServiceManifest struct {

// ServiceInstallerMetadata information about the installer
type ServiceInstallerMetadata struct {
AgentPath string
Arch string
Docker bool
FileExtension string
Expand Down
23 changes: 23 additions & 0 deletions internal/installer/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/elastic/e2e-testing/internal/common"
"github.com/elastic/e2e-testing/internal/deploy"
"github.com/elastic/e2e-testing/internal/io"
"github.com/elastic/e2e-testing/internal/systemd"
"github.com/elastic/e2e-testing/pkg/downloads"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -114,6 +115,28 @@ func doUpgrade(ctx context.Context, so deploy.ServiceOperator) error {
return nil
}

// createAgentDirectories makes sure the agent directories belong to the root user
func createAgentDirectories(ctx context.Context, i deploy.ServiceOperator, osArgs []string) error {
agentPath := i.PkgMetadata().AgentPath

err := io.MkdirAll(agentPath)
if err != nil {
return err
}

output, err := i.Exec(ctx, osArgs)
if err != nil {
return err
}

log.WithFields(log.Fields{
"args": osArgs,
"output": output,
"path": agentPath,
}).Debug("Agent directories will belong to root")
return nil
}

func systemCtlLog(ctx context.Context, OS string, execFn func(ctx context.Context, args []string) (string, error)) error {
cmds := systemd.LogCmds(common.ElasticAgentServiceName)
span, _ := apm.StartSpanOptions(ctx, "Retrieving logs for the Elastic Agent service", "elastic-agent."+OS+".log", apm.SpanOptions{
Expand Down
8 changes: 7 additions & 1 deletion internal/installer/elasticagent_deb.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func AttachElasticAgentDEBPackage(d deploy.Deployment, service deploy.ServiceReq
service: service,
deploy: d,
metadata: deploy.ServiceInstallerMetadata{
AgentPath: "/var/lib/elastic-agent",
PackageType: "deb",
Os: "linux",
Arch: utils.GetArchitecture(),
Expand All @@ -55,7 +56,7 @@ func (i *elasticAgentDEBPackage) AddFiles(ctx context.Context, files []string) e
// Inspect returns info on package
func (i *elasticAgentDEBPackage) Inspect() (deploy.ServiceOperatorManifest, error) {
return deploy.ServiceOperatorManifest{
WorkDir: "/var/lib/elastic-agent",
WorkDir: i.metadata.AgentPath,
CommitFile: "/etc/elastic-agent/.elastic-agent.active.commit",
}, nil
}
Expand Down Expand Up @@ -141,6 +142,11 @@ func (i *elasticAgentDEBPackage) Postinstall(ctx context.Context) error {

// Preinstall executes operations before installing a DEB package
func (i *elasticAgentDEBPackage) Preinstall(ctx context.Context) error {
err := createAgentDirectories(ctx, i, []string{"sudo", "chown", "-R", "root:root", i.metadata.AgentPath})
if err != nil {
return err
}

installArtifactFn := func(ctx context.Context, artifact string, version string, useCISnapshots bool) error {
span, _ := apm.StartSpanOptions(ctx, "Pre-install "+artifact, artifact+".debian.pre-install", apm.SpanOptions{
Parent: apm.SpanFromContext(ctx).TraceContext(),
Expand Down
3 changes: 2 additions & 1 deletion internal/installer/elasticagent_docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func AttachElasticAgentDockerPackage(d deploy.Deployment, service deploy.Service
service: service,
deploy: d,
metadata: deploy.ServiceInstallerMetadata{
AgentPath: "/usr/share/elastic-agent",
PackageType: "docker",
Os: "linux",
Arch: utils.GetArchitecture(),
Expand All @@ -53,7 +54,7 @@ func (i *elasticAgentDockerPackage) AddFiles(ctx context.Context, files []string
// Inspect returns info on package
func (i *elasticAgentDockerPackage) Inspect() (deploy.ServiceOperatorManifest, error) {
return deploy.ServiceOperatorManifest{
WorkDir: "/usr/share/elastic-agent",
WorkDir: i.metadata.AgentPath,
CommitFile: "/usr/share/elastic-agent/.elastic-agent.active.commit",
}, nil
}
Expand Down
8 changes: 7 additions & 1 deletion internal/installer/elasticagent_rpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func AttachElasticAgentRPMPackage(d deploy.Deployment, service deploy.ServiceReq
service: service,
deploy: d,
metadata: deploy.ServiceInstallerMetadata{
AgentPath: "/var/lib/elastic-agent",
PackageType: "rpm",
Os: "linux",
Arch: arch,
Expand All @@ -60,7 +61,7 @@ func (i *elasticAgentRPMPackage) AddFiles(ctx context.Context, files []string) e
// Inspect returns info on package
func (i *elasticAgentRPMPackage) Inspect() (deploy.ServiceOperatorManifest, error) {
return deploy.ServiceOperatorManifest{
WorkDir: "/var/lib/elastic-agent",
WorkDir: i.metadata.AgentPath,
CommitFile: "/etc/elastic-agent/.elastic-agent.active.commit",
}, nil
}
Expand Down Expand Up @@ -147,6 +148,11 @@ func (i *elasticAgentRPMPackage) Postinstall(ctx context.Context) error {

// Preinstall executes operations before installing a RPM package
func (i *elasticAgentRPMPackage) Preinstall(ctx context.Context) error {
err := createAgentDirectories(ctx, i, []string{"sudo", "chown", "-R", "root:root", i.metadata.AgentPath})
if err != nil {
return err
}

installArtifactFn := func(ctx context.Context, artifact string, version string, useCISnapshots bool) error {
span, _ := apm.StartSpanOptions(ctx, "Pre-install "+artifact, artifact+".rpm.pre-install", apm.SpanOptions{
Parent: apm.SpanFromContext(ctx).TraceContext(),
Expand Down
8 changes: 7 additions & 1 deletion internal/installer/elasticagent_tar.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func AttachElasticAgentTARPackage(d deploy.Deployment, service deploy.ServiceReq
service: service,
deploy: d,
metadata: deploy.ServiceInstallerMetadata{
AgentPath: "/opt/Elastic/Agent",
PackageType: "tar",
Os: "linux",
Arch: arch,
Expand All @@ -62,7 +63,7 @@ func (i *elasticAgentTARPackage) AddFiles(ctx context.Context, files []string) e
// Inspect returns info on package
func (i *elasticAgentTARPackage) Inspect() (deploy.ServiceOperatorManifest, error) {
return deploy.ServiceOperatorManifest{
WorkDir: "/opt/Elastic/Agent",
WorkDir: i.metadata.AgentPath,
CommitFile: "elastic-agent/.elastic-agent.active.commit",
}, nil
}
Expand Down Expand Up @@ -122,6 +123,11 @@ func (i *elasticAgentTARPackage) Postinstall(ctx context.Context) error {

// Preinstall executes operations before installing a TAR package
func (i *elasticAgentTARPackage) Preinstall(ctx context.Context) error {
err := createAgentDirectories(ctx, i, []string{"sudo", "chown", "-R", "root:root", i.metadata.AgentPath})
if err != nil {
return err
}

installArtifactFn := func(ctx context.Context, artifact string, version string, useCISnapshots bool) error {
span, _ := apm.StartSpanOptions(ctx, "Pre-install "+artifact, artifact+".tar.pre-install", apm.SpanOptions{
Parent: apm.SpanFromContext(ctx).TraceContext(),
Expand Down
8 changes: 7 additions & 1 deletion internal/installer/elasticagent_tar_macos.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func AttachElasticAgentTARDarwinPackage(d deploy.Deployment, service deploy.Serv
service: service,
deploy: d,
metadata: deploy.ServiceInstallerMetadata{
AgentPath: "/opt/Elastic/Agent",
PackageType: "tar",
Os: "darwin",
Arch: arch,
Expand All @@ -54,7 +55,7 @@ func (i *elasticAgentTARDarwinPackage) AddFiles(ctx context.Context, files []str
// Inspect returns info on package
func (i *elasticAgentTARDarwinPackage) Inspect() (deploy.ServiceOperatorManifest, error) {
return deploy.ServiceOperatorManifest{
WorkDir: "/opt/Elastic/Agent",
WorkDir: i.metadata.AgentPath,
CommitFile: "/elastic-agent/.elastic-agent.active.commit",
}, nil
}
Expand Down Expand Up @@ -123,6 +124,11 @@ func (i *elasticAgentTARDarwinPackage) Preinstall(ctx context.Context) error {
span.Context.SetLabel("runtime", runtime.GOOS)
defer span.End()

err := createAgentDirectories(ctx, i, []string{"sudo", "chown", "-R", "root:root", i.metadata.AgentPath})
if err != nil {
return err
}

artifact := "elastic-agent"

metadata := i.metadata
Expand Down
3 changes: 2 additions & 1 deletion internal/installer/elasticagent_zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func AttachElasticAgentZIPPackage(d deploy.Deployment, service deploy.ServiceReq
service: service,
deploy: d,
metadata: deploy.ServiceInstallerMetadata{
AgentPath: "C:\\Program Files\\Elastic\\Agent",
PackageType: "zip",
Os: "windows",
Arch: "x86_64",
Expand All @@ -48,7 +49,7 @@ func (i *elasticAgentZIPPackage) AddFiles(ctx context.Context, files []string) e
// Inspect returns info on package
func (i *elasticAgentZIPPackage) Inspect() (deploy.ServiceOperatorManifest, error) {
return deploy.ServiceOperatorManifest{
WorkDir: "C:\\Program Files\\Elastic\\Agent",
WorkDir: i.metadata.AgentPath,
CommitFile: "C:\\elastic-agent\\.elastic-agent.active.commit",
}, nil
}
Expand Down

0 comments on commit 274a01a

Please sign in to comment.