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

(cherry picked from commit 87336db)

# Conflicts:
#	dev-tools/mage/settings.go
#	magefile.go
  • Loading branch information
pchila authored and mergify[bot] committed Apr 17, 2024
1 parent 7ac63cf commit b27797f
Show file tree
Hide file tree
Showing 5 changed files with 460 additions and 60 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
12 changes: 11 additions & 1 deletion dev-tools/mage/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ 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"
<<<<<<< HEAD

Check failure on line 49 in dev-tools/mage/settings.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

expected 'IDENT', found '<<' (typecheck)
=======
// AgentCommitHashEnvVar allows to override agent commit hash string during packaging
AgentCommitHashEnvVar
>>>>>>> 87336dbaab (Add a mage target that packages elastic-agent using elastic-agent-core DRA (#4403))

Check failure on line 53 in dev-tools/mage/settings.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

illegal character U+0023 '#' (typecheck)

// Mapped functions
agentPackageVersionMappedFunc = "agent_package_version"
Expand Down Expand Up @@ -283,7 +288,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 b27797f

Please sign in to comment.