Skip to content

Commit

Permalink
Backport of easltic-agent PR #3682 (#40704)
Browse files Browse the repository at this point in the history
This is a manual backport of elastic/elastic-agent#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
  • Loading branch information
pchila authored Sep 9, 2024
1 parent bc2cbf4 commit 75f30b5
Showing 1 changed file with 37 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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...)
Expand Down

0 comments on commit 75f30b5

Please sign in to comment.