Skip to content

Commit

Permalink
snapshot/downloader: fix fetching snapshot URI (#1791)
Browse files Browse the repository at this point in the history
When parsing the response from the artifacts-api to get the latest
snapshot (https://artifacts-api.elastic.co/v1/search/%s-SNAPSHOT/elastic-agent)
it could happen that an entry that is not from the Elastic-Agent (like
the Elastic-Agent-Shipper) would be evaluated and it would cause the
`snapshotURI` function return an error.

This commit fixes it by iterating the whole map before returning an
error.
  • Loading branch information
belimawr authored Dec 5, 2022
1 parent 12b5b1a commit 8c034dd
Showing 1 changed file with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,18 @@ func snapshotURI(versionOverride string, config *artifact.Config) (string, error
return "", fmt.Errorf("uri is not a string")
}

// Because we're iterating over a map from the API response,
// the order is random and some elements there do not contain the
// `/beats/elastic-agent/` substring, so we need to go through the
// whole map before returning an error.
//
// One of the elements that might be there and do not contain this
// substring is the `elastic-agent-shipper`, whose URL is something like:
// https://snapshots.elastic.co/8.7.0-d050210c/downloads/elastic-agent-shipper/elastic-agent-shipper-8.7.0-SNAPSHOT-linux-x86_64.tar.gz
index := strings.Index(uri, "/beats/elastic-agent/")
if index == -1 {
return "", fmt.Errorf("not an agent uri: '%s'", uri)
if index != -1 {
return uri[:index], nil
}

return uri[:index], nil
}

return "", fmt.Errorf("uri not detected")
Expand Down

0 comments on commit 8c034dd

Please sign in to comment.