Skip to content

Commit

Permalink
Add a mage target that packages elastic-agent using elastic-agent-cor…
Browse files Browse the repository at this point in the history
…e DRA (#4403)

* Extract dependencies download to its own function

* Add PackageUsingDRA mage target

* filter elastic-agent-core artifacts by platform

* update buildkite package script with packageUsingDRA target

* Add sha512 validation for downloaded elastic-agent-core packages

* Override the elastic agent commit hash when packaging using DRA

* change Manifest URL env variable name
  • Loading branch information
pchila committed Apr 29, 2024
1 parent fe984fd commit 10b22e1
Show file tree
Hide file tree
Showing 5 changed files with 544 additions and 69 deletions.
4 changes: 2 additions & 2 deletions .buildkite/scripts/steps/package.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ fi
export AGENT_DROP_PATH=build/elastic-agent-drop
mkdir -p $AGENT_DROP_PATH

# Download the components from the ManifestURL and then package those downloaded into the $AGENT_DROP_PATH
mage clean downloadManifest package ironbank fixDRADockerArtifacts
# Download the components from the MANIFEST_URL and then package those downloaded into the $AGENT_DROP_PATH
mage clean downloadManifest packageUsingDRA ironbank fixDRADockerArtifacts

echo "+++ Generate dependencies report"
BEAT_VERSION_FULL=$(curl -s -XGET "${MANIFEST_URL}" |jq '.version' -r )
Expand Down
4 changes: 2 additions & 2 deletions dev-tools/mage/manifest/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func DownloadComponentsFromManifest(manifest string, platforms []string, platfor
downloadTarget := filepath.Join(targetPath, pkgFilename)
if _, err := os.Stat(downloadTarget); err != nil {
errGrp.Go(func(ctx context.Context, url, target string) func() error {
return func() error { return downloadPackage(ctx, url, target) }
return func() error { return DownloadPackage(ctx, url, target) }
}(downloadsCtx, p, downloadTarget))
}
}
Expand All @@ -150,7 +150,7 @@ func DownloadComponentsFromManifest(manifest string, platforms []string, platfor
return nil
}

func downloadPackage(ctx context.Context, downloadUrl string, target string) error {
func DownloadPackage(ctx context.Context, downloadUrl string, target string) error {
parsedURL, errorUrl := url.Parse(downloadUrl)
if errorUrl != nil {
return errorInvalidManifestURL
Expand Down
9 changes: 8 additions & 1 deletion dev-tools/mage/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ const (
agentPackageVersionEnvVar = "AGENT_PACKAGE_VERSION"
//ManifestUrlEnvVar is the name fo the environment variable containing the Manifest URL to be used for packaging agent
ManifestUrlEnvVar = "MANIFEST_URL"
// AgentCommitHashEnvVar allows to override agent commit hash string during packaging
AgentCommitHashEnvVar

// Mapped functions
agentPackageVersionMappedFunc = "agent_package_version"
Expand Down Expand Up @@ -283,7 +285,12 @@ var (
func CommitHash() (string, error) {
var err error
commitHashOnce.Do(func() {
commitHash, err = sh.Output("git", "rev-parse", "HEAD")
// Check commit hash override first
commitHash = EnvOr(AgentCommitHashEnvVar, "")
if commitHash == "" {
// no override found, get the hash from HEAD
commitHash, err = sh.Output("git", "rev-parse", "HEAD")
}
})
return commitHash, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"crypto/sha512"
"encoding/hex"
"fmt"
"hash"
"io"
"net/http"
"net/url"
Expand Down Expand Up @@ -130,8 +131,16 @@ func getHashFileName(filename string) string {
// exists and that the checksum in the sidecar file matches the checksum of
// the file. It returns an error if validation fails.
func VerifySHA512Hash(filename string) error {
hasher := sha512.New()
checksumFileName := getHashFileName(filename)
return VerifyChecksum(hasher, filename, checksumFileName)
}

// VerifyChecksum checks that the hash contained in checksumFileName correspond to the hash calculated for filename using
// hasher.Sum()
func VerifyChecksum(hasher hash.Hash, filename, checksumFileName string) error {
// Read expected checksum.
expectedHash, err := readChecksumFile(getHashFileName(filename), filepath.Base(filename))
expectedHash, err := readChecksumFile(checksumFileName, filepath.Base(filename))
if err != nil {
return fmt.Errorf("could not read checksum file: %w", err)
}
Expand All @@ -143,12 +152,11 @@ func VerifySHA512Hash(filename string) error {
}
defer f.Close()

hash := sha512.New()
if _, err := io.Copy(hash, f); err != nil {
if _, err := io.Copy(hasher, f); err != nil {
return fmt.Errorf("faled to read file to calculate hash")
}

computedHash := hex.EncodeToString(hash.Sum(nil))
computedHash := hex.EncodeToString(hasher.Sum(nil))
if computedHash != expectedHash {
return &ChecksumMismatchError{
Expected: expectedHash,
Expand Down
Loading

0 comments on commit 10b22e1

Please sign in to comment.