From 75f30b5b32463f88c92bf6164e992055d73cf1e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paolo=20Chil=C3=A0?= Date: Mon, 9 Sep 2024 10:21:16 +0200 Subject: [PATCH] Backport of easltic-agent PR #3682 (#40704) This is a manual backport of https://github.com/elastic/elastic-agent/pull/3682 Only the essentials have been backported ignoring features like parsed agent version or download retries which are part of other PRs that have not been backported onto elastic-agent 7.17 --- .../application/upgrade/step_download.go | 41 +++++++++++++++++-- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/x-pack/elastic-agent/pkg/agent/application/upgrade/step_download.go b/x-pack/elastic-agent/pkg/agent/application/upgrade/step_download.go index 282c58cfee96..24197c413a88 100644 --- a/x-pack/elastic-agent/pkg/agent/application/upgrade/step_download.go +++ b/x-pack/elastic-agent/pkg/agent/application/upgrade/step_download.go @@ -24,22 +24,53 @@ const ( defaultUpgradeFallbackPGP = "https://artifacts.elastic.co/GPG-KEY-elastic-agent" ) +type downloaderFactory func(string, *logger.Logger, *artifact.Config) (download.Downloader, error) + func (u *Upgrader) downloadArtifact(ctx context.Context, version, sourceURI string, skipVerifyOverride bool, pgpBytes ...string) (string, error) { // do not update source config settings := *u.settings + + var factory downloaderFactory + var verifier download.Verifier + var err error if sourceURI != "" { if strings.HasPrefix(sourceURI, "file://") { // update the DropPath so the fs.Downloader can download from this // path instead of looking into the installed downloads directory settings.DropPath = strings.TrimPrefix(sourceURI, "file://") + + // set specific downloader, local file just uses the fs.NewDownloader + // no fallback is allowed because it was requested that this specific source be used + factory = func(version string, l *logger.Logger, config *artifact.Config) (download.Downloader, error) { + return fs.NewDownloader(config), nil + } + + // set specific verifier, local file verifies locally only + allowEmptyPgp, pgp := release.PGP() + verifier, err = fs.NewVerifier(&settings, allowEmptyPgp, pgp) + if err != nil { + return "", errors.New(err, "initiating verifier") + } + // log that a local upgrade artifact is being used + u.log.Infow("Using local upgrade artifact", "version", version, + "drop_path", settings.DropPath, + "target_path", settings.TargetDirectory, "install_path", settings.InstallPath) } else { settings.SourceURI = sourceURI } } + if factory == nil { + // set the factory to the newDownloader factory + factory = newDownloader + u.log.Infow("Downloading upgrade artifact", "version", version, + "source_uri", settings.SourceURI, "drop_path", settings.DropPath, + "target_path", settings.TargetDirectory, "install_path", settings.InstallPath) + } + pgpBytes = appendFallbackPGP(pgpBytes) - fetcher, err := newDownloader(version, u.log, &settings) + fetcher, err := factory(version, u.log, &settings) if err != nil { return "", errors.New(err, "initiating fetcher") } @@ -53,9 +84,11 @@ func (u *Upgrader) downloadArtifact(ctx context.Context, version, sourceURI stri return path, nil } - verifier, err := newVerifier(version, u.log, &settings) - if err != nil { - return "", errors.New(err, "initiating verifier") + if verifier == nil { + verifier, err = newVerifier(version, u.log, &settings) + if err != nil { + return "", errors.New(err, "initiating verifier") + } } matches, err := verifier.Verify(agentSpec, version, true, pgpBytes...)