Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cherry-pick #19503 to 7.x: [Elastic Agent] Fix artifact downloading to allow endpoint-security to be downloaded #19513

Merged
merged 1 commit into from
Jun 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions x-pack/elastic-agent/CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,4 @@
- Agent now runs the GRPC server and spawned application connect by to Agent {pull}18973[18973]
- Rename input.type logs to logfile {pull}19360[19360]
- Agent now installs/uninstalls Elastic Endpoint {pull}19248[19248]
- Agent now downloads Elastic Endpoint {pull}19503[19503]
2 changes: 1 addition & 1 deletion x-pack/elastic-agent/pkg/agent/operation/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func waitFor(t *testing.T, check func() error) {

type DummyDownloader struct{}

func (*DummyDownloader) Download(_ context.Context, p, v string) (string, error) {
func (*DummyDownloader) Download(_ context.Context, p, a, v string) (string, error) {
return "", nil
}

Expand Down
15 changes: 10 additions & 5 deletions x-pack/elastic-agent/pkg/agent/operation/monitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
package operation

import (
multierror "github.com/hashicorp/go-multierror"
"fmt"

"github.com/hashicorp/go-multierror"

"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/configrequest"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/errors"
Expand All @@ -19,6 +21,7 @@ const (
monitoringEnabledSubkey = "enabled"
logsProcessName = "filebeat"
metricsProcessName = "metricbeat"
artifactPrefix = "beats"
)

func (o *Operator) handleStartSidecar(s configrequest.Step) (result error) {
Expand Down Expand Up @@ -133,8 +136,9 @@ func (o *Operator) generateMonitoringSteps(version string, output interface{}) [
ID: stepID,
Version: version,
ProgramSpec: program.Spec{
Name: logsProcessName,
Cmd: logsProcessName,
Name: logsProcessName,
Cmd: logsProcessName,
Artifact: fmt.Sprintf("%s/%s", artifactPrefix, logsProcessName),
},
Meta: map[string]interface{}{
configrequest.MetaConfigKey: fbConfig,
Expand All @@ -154,8 +158,9 @@ func (o *Operator) generateMonitoringSteps(version string, output interface{}) [
ID: stepID,
Version: version,
ProgramSpec: program.Spec{
Name: metricsProcessName,
Cmd: metricsProcessName,
Name: metricsProcessName,
Cmd: metricsProcessName,
Artifact: fmt.Sprintf("%s/%s", artifactPrefix, logsProcessName),
},
Meta: map[string]interface{}{
configrequest.MetaConfigKey: mbConfig,
Expand Down
1 change: 1 addition & 0 deletions x-pack/elastic-agent/pkg/agent/operation/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type Application interface {
type Descriptor interface {
ServicePort() int
BinaryName() string
ArtifactName() string
Version() string
ID() string
Directory() string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (o *operationFetch) Run(ctx context.Context, application Application) (err
}
}()

fullPath, err := o.downloader.Download(ctx, o.program.BinaryName(), o.program.Version())
fullPath, err := o.downloader.Download(ctx, o.program.BinaryName(), o.program.ArtifactName(), o.program.Version())
if err == nil {
o.logger.Infof("operation '%s' downloaded %s.%s into %s", o.Name(), o.program.BinaryName(), o.program.Version(), fullPath)
}
Expand Down
1 change: 1 addition & 0 deletions x-pack/elastic-agent/pkg/agent/program/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Spec struct {
ServicePort int `yaml:"service,omitempty"`
Cmd string `yaml:"cmd"`
Args []string `yaml:"args"`
Artifact string `yaml:"artifact"`
Rules *transpiler.RuleList `yaml:"rules"`
CheckInstallSteps *transpiler.StepList `yaml:"check_install"`
PostInstallSteps *transpiler.StepList `yaml:"post_install"`
Expand Down
8 changes: 5 additions & 3 deletions x-pack/elastic-agent/pkg/agent/program/spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ import (

func TestSerialization(t *testing.T) {
spec := Spec{
Name: "hello",
Cmd: "hellocmd",
Args: []string{"-c", "first"},
Name: "hello",
Cmd: "hellocmd",
Args: []string{"-c", "first"},
Artifact: "nested/hellocmd",
Rules: transpiler.NewRuleList(
transpiler.Copy("inputs", "filebeat"),
transpiler.Filter("filebeat", "output", "keystore"),
Expand Down Expand Up @@ -60,6 +61,7 @@ cmd: hellocmd
args:
- -c
- first
artifact: nested/hellocmd
rules:
- copy:
from: inputs
Expand Down
2 changes: 1 addition & 1 deletion x-pack/elastic-agent/pkg/agent/program/supported.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions x-pack/elastic-agent/pkg/artifact/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ var packageArchMap = map[string]string{
}

// GetArtifactName constructs a path to a downloaded artifact
func GetArtifactName(program, version, operatingSystem, arch string) (string, error) {
func GetArtifactName(programName, version, operatingSystem, arch string) (string, error) {
key := fmt.Sprintf("%s-binary-%s", operatingSystem, arch)
suffix, found := packageArchMap[key]
if !found {
return "", errors.New(fmt.Sprintf("'%s' is not a valid combination for a package", key), errors.TypeConfig)
}

return fmt.Sprintf("%s-%s-%s", program, version, suffix), nil
return fmt.Sprintf("%s-%s-%s", programName, version, suffix), nil
}

// GetArtifactPath returns a full path of artifact for a program in specific version
Expand Down
6 changes: 3 additions & 3 deletions x-pack/elastic-agent/pkg/artifact/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ type Config struct {
// Architecture: target architecture [32, 64]
Architecture string `json:"-" config:",ignore"`

// BeatsSourceURI: source of the artifacts, e.g https://artifacts.elastic.co/downloads/beats/
BeatsSourceURI string `json:"sourceURI" config:"sourceURI"`
// SourceURI: source of the artifacts, e.g https://artifacts.elastic.co/downloads/
SourceURI string `json:"sourceURI" config:"sourceURI"`

// TargetDirectory: path to the directory containing downloaded packages
TargetDirectory string `json:"targetDirectory" config:"target_directory"`
Expand All @@ -49,7 +49,7 @@ type Config struct {
func DefaultConfig() *Config {
dataPath := paths.Data()
return &Config{
BeatsSourceURI: "https://artifacts.elastic.co/downloads/beats/",
SourceURI: "https://artifacts.elastic.co/downloads/",
TargetDirectory: filepath.Join(dataPath, "downloads"),
Timeout: 30 * time.Second,
PgpFile: filepath.Join(dataPath, "elastic.pgp"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ func NewDownloader(downloaders ...download.Downloader) *Downloader {

// Download fetches the package from configured source.
// Returns absolute path to downloaded package and an error.
func (e *Downloader) Download(ctx context.Context, programName, version string) (string, error) {
func (e *Downloader) Download(ctx context.Context, programName, artifactName, version string) (string, error) {
var err error

for _, d := range e.dd {
s, e := d.Download(ctx, programName, version)
s, e := d.Download(ctx, programName, artifactName, version)
if e == nil {
return s, nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type FailingDownloader struct {
called bool
}

func (d *FailingDownloader) Download(ctx context.Context, a, b string) (string, error) {
func (d *FailingDownloader) Download(ctx context.Context, a, b, c string) (string, error) {
d.called = true
return "", errors.New("failing")
}
Expand All @@ -29,7 +29,7 @@ type SuccDownloader struct {
called bool
}

func (d *SuccDownloader) Download(ctx context.Context, a, b string) (string, error) {
func (d *SuccDownloader) Download(ctx context.Context, a, b, c string) (string, error) {
d.called = true
return "succ", nil
}
Expand Down Expand Up @@ -58,7 +58,7 @@ func TestComposed(t *testing.T) {

for _, tc := range testCases {
d := NewDownloader(tc.downloaders[0], tc.downloaders[1])
r, _ := d.Download(nil, "a", "b")
r, _ := d.Download(nil, "a", "b", "c")

assert.Equal(t, tc.expectedResult, r == "succ")

Expand Down
2 changes: 1 addition & 1 deletion x-pack/elastic-agent/pkg/artifact/download/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ import "context"

// Downloader is an interface allowing download of an artifact
type Downloader interface {
Download(ctx context.Context, programName, version string) (string, error)
Download(ctx context.Context, programName, artifactName, version string) (string, error)
}
10 changes: 5 additions & 5 deletions x-pack/elastic-agent/pkg/artifact/download/fs/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func NewDownloader(config *artifact.Config) *Downloader {

// Download fetches the package from configured source.
// Returns absolute path to downloaded package and an error.
func (e *Downloader) Download(_ context.Context, programName, version string) (_ string, err error) {
func (e *Downloader) Download(_ context.Context, programName, artifactName, version string) (_ string, err error) {
downloadedFiles := make([]string, 0, 2)
defer func() {
if err != nil {
Expand All @@ -50,18 +50,18 @@ func (e *Downloader) Download(_ context.Context, programName, version string) (_
}()

// download from source to dest
path, err := e.download(e.config.OS(), programName, version)
path, err := e.download(e.config.OS(), programName, artifactName, version)
downloadedFiles = append(downloadedFiles, path)
if err != nil {
return "", err
}

hashPath, err := e.downloadHash(e.config.OS(), programName, version)
hashPath, err := e.downloadHash(e.config.OS(), programName, artifactName, version)
downloadedFiles = append(downloadedFiles, hashPath)
return path, err
}

func (e *Downloader) download(operatingSystem, programName, version string) (string, error) {
func (e *Downloader) download(operatingSystem, programName, _, version string) (string, error) {
filename, err := artifact.GetArtifactName(programName, version, operatingSystem, e.config.Arch())
if err != nil {
return "", errors.New(err, "generating package name failed")
Expand All @@ -75,7 +75,7 @@ func (e *Downloader) download(operatingSystem, programName, version string) (str
return e.downloadFile(filename, fullPath)
}

func (e *Downloader) downloadHash(operatingSystem, programName, version string) (string, error) {
func (e *Downloader) downloadHash(operatingSystem, programName, _, version string) (string, error) {
filename, err := artifact.GetArtifactName(programName, version, operatingSystem, e.config.Arch())
if err != nil {
return "", errors.New(err, "generating package name failed")
Expand Down
11 changes: 6 additions & 5 deletions x-pack/elastic-agent/pkg/artifact/download/fs/verifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import (
)

const (
beatName = "filebeat"
version = "7.5.1"
sourcePattern = "/downloads/beats/filebeat/"
beatName = "filebeat"
artifactName = "beats/filebeat"
version = "7.5.1"
)

type testCase struct {
Expand All @@ -37,6 +37,7 @@ func TestFetchVerify(t *testing.T) {
targetPath := filepath.Join("testdata", "download")
ctx := context.Background()
programName := "beat"
artifactName := "beats/beat"
version := "8.0.0"

targetFilePath := filepath.Join(targetPath, "beat-8.0.0-darwin-x86_64.tar.gz")
Expand Down Expand Up @@ -77,7 +78,7 @@ func TestFetchVerify(t *testing.T) {
// second one should pass
// download not skipped: package missing
// verify passes because hash is not correct
_, err = downloader.Download(ctx, programName, version)
_, err = downloader.Download(ctx, programName, artifactName, version)
assert.NoError(t, err)

// file downloaded ok
Expand Down Expand Up @@ -146,7 +147,7 @@ func TestVerify(t *testing.T) {
}

testClient := NewDownloader(config)
artifact, err := testClient.Download(context.Background(), beatName, version)
artifact, err := testClient.Download(context.Background(), beatName, artifactName, version)
if err != nil {
t.Fatal(err)
}
Expand Down
Loading