@@ -17,6 +17,9 @@ import (
1717 "sync/atomic"
1818 "time"
1919
20+ "github.com/cenkalti/backoff/v5"
21+
22+ "github.com/elastic/elastic-agent/internal/pkg/agent/application/upgrade/artifact/download"
2023 semver "github.com/elastic/elastic-agent/pkg/version"
2124)
2225
@@ -125,21 +128,43 @@ func (r *artifactResult) Fetch(ctx context.Context, l Logger, dir string) error
125128 return fmt .Errorf ("failed to create path %q: %w" , dst , err )
126129 }
127130
128- err = DownloadPackage (ctx , l , r .doer , r .src , dst )
131+ _ , err = backoff .Retry (ctx , func () (any , error ) {
132+ if err = r .fetch (ctx , l , dst ); err != nil {
133+ return nil , err
134+ }
135+
136+ // check package hash
137+ if err = download .VerifySHA512Hash (dst ); err != nil {
138+ l .Logf ("inconsistent package hash detected: %s" , err )
139+ return nil , fmt .Errorf ("inconsistent package hash: %w" , err )
140+ }
141+
142+ return nil , nil
143+ },
144+ backoff .WithMaxTries (3 ),
145+ backoff .WithBackOff (backoff .NewConstantBackOff (3 * time .Second )),
146+ )
147+
129148 if err != nil {
149+ return fmt .Errorf ("failed to fetch %s: %w" , r .src , err )
150+ }
151+
152+ return nil
153+ }
154+
155+ func (r * artifactResult ) fetch (ctx context.Context , l Logger , dst string ) error {
156+ if err := DownloadPackage (ctx , l , r .doer , r .src , dst ); err != nil {
130157 return fmt .Errorf ("failed to download %s: %w" , r .src , err )
131158 }
132159
133160 // fetch package hash
134- err = DownloadPackage (ctx , l , r .doer , r .src + extHash , dst + extHash )
135- if err != nil {
136- return fmt .Errorf ("failed to download %s: %w" , r .src , err )
161+ if err := DownloadPackage (ctx , l , r .doer , r .src + extHash , dst + extHash ); err != nil {
162+ return fmt .Errorf ("failed to download %s: %w" , r .src + extHash , err )
137163 }
138164
139165 // fetch package asc
140- err = DownloadPackage (ctx , l , r .doer , r .src + extAsc , dst + extAsc )
141- if err != nil {
142- return fmt .Errorf ("failed to download %s: %w" , r .src , err )
166+ if err := DownloadPackage (ctx , l , r .doer , r .src + extAsc , dst + extAsc ); err != nil {
167+ return fmt .Errorf ("failed to download %s: %w" , r .src + extAsc , err )
143168 }
144169
145170 return nil
@@ -153,7 +178,12 @@ func findURI(ctx context.Context, doer httpDoer, version *semver.ParsedSemVer) (
153178 return fmt .Sprintf ("https://snapshots.elastic.co/%s-%s/downloads/beats/elastic-agent/" , version .CoreVersion (), version .BuildMetadata ()), nil
154179 }
155180
156- buildID , err := findLatestSnapshot (ctx , doer , version .CoreVersion ())
181+ buildID , err := backoff .Retry (ctx , func () (any , error ) {
182+ return findLatestSnapshot (ctx , doer , version .CoreVersion ())
183+ },
184+ backoff .WithMaxTries (3 ),
185+ backoff .WithBackOff (backoff .NewConstantBackOff (3 * time .Second )),
186+ )
157187 if err != nil {
158188 return "" , fmt .Errorf ("failed to find snapshot information for version %q: %w" , version , err )
159189 }
0 commit comments