Skip to content

Commit 87336db

Browse files
authored
Add a mage target that packages elastic-agent using elastic-agent-core 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
1 parent 2a6484a commit 87336db

File tree

6 files changed

+451
-77
lines changed

6 files changed

+451
-77
lines changed

.buildkite/pipeline.elastic-agent-package.yml

+12-12
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ env:
1010

1111
steps:
1212
- input: "Build parameters"
13-
if: build.env("ManifestURL") == null
13+
if: build.env("MANIFEST_URL") == null
1414
fields:
15-
- text: "ManifestURL"
16-
key: "ManifestURL"
15+
- text: "MANIFEST_URL"
16+
key: "MANIFEST_URL"
1717
default: ""
1818
required: true
1919
hint: "Link to the build manifest URL."
@@ -51,7 +51,7 @@ steps:
5151
hint: "If the DRA release manager script would actually publish anything or just print"
5252

5353
- wait: ~
54-
if: build.env("ManifestURL") == null
54+
if: build.env("MANIFEST_URL") == null
5555

5656
- group: ":Packaging Artefacts"
5757
key: "package"
@@ -63,10 +63,10 @@ steps:
6363
machineType: "c2-standard-16"
6464
diskSizeGb: 400
6565
command: |
66-
if [[ -z "${ManifestURL}" ]]; then
67-
export ManifestURL=$(buildkite-agent meta-data get ManifestURL --default "")
68-
if [[ -z "${ManifestURL}" ]]; then
69-
echo ":broken_heart: Missing ManifestURL variable or empty string provided"
66+
if [[ -z "${MANIFEST_URL}" ]]; then
67+
export MANIFEST_URL=$(buildkite-agent meta-data get MANIFEST_URL --default "")
68+
if [[ -z "${MANIFEST_URL}" ]]; then
69+
echo ":broken_heart: Missing MANIFEST_URL variable or empty string provided"
7070
exit 1
7171
fi
7272
fi
@@ -86,10 +86,10 @@ steps:
8686
PLATFORMS: "linux/arm64"
8787
PACKAGES: "docker"
8888
command: |
89-
if [[ -z "${ManifestURL}" ]]; then
90-
export ManifestURL=$(buildkite-agent meta-data get ManifestURL --default "")
91-
if [[ -z "${ManifestURL}" ]]; then
92-
echo ":broken_heart: Missing ManifestURL variable or empty string provided"
89+
if [[ -z "${MANIFEST_URL}" ]]; then
90+
export MANIFEST_URL=$(buildkite-agent meta-data get MANIFEST_URL --default "")
91+
if [[ -z "${MANIFEST_URL}" ]]; then
92+
echo ":broken_heart: Missing MANIFEST_URL variable or empty string provided"
9393
exit 1
9494
fi
9595
fi

.buildkite/scripts/steps/package.sh

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@ set -euo pipefail
55
_SELF=$(dirname $0)
66
source "${_SELF}/../common.sh"
77

8-
if test -z "${ManifestURL=:""}"; then
9-
echo "Missing variable ManifestURL, export it before use."
8+
if test -z "${MANIFEST_URL=:""}"; then
9+
echo "Missing variable MANIFEST_URL, export it before use."
1010
exit 2
1111
fi
1212

1313
export AGENT_DROP_PATH=build/elastic-agent-drop
1414
mkdir -p $AGENT_DROP_PATH
1515

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

1919
echo "+++ Generate dependencies report"
20-
BEAT_VERSION_FULL=$(curl -s -XGET "${ManifestURL}" |jq '.version' -r )
20+
BEAT_VERSION_FULL=$(curl -s -XGET "${MANIFEST_URL}" |jq '.version' -r )
2121
bash "${_SELF}/../../../dev-tools/dependencies-report"
2222
mkdir -p build/distributions/reports
2323
mv dependencies.csv "build/distributions/reports/dependencies-${BEAT_VERSION_FULL}.csv"

dev-tools/mage/manifest/manifest.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func DownloadComponentsFromManifest(manifest string, platforms []string, platfor
130130
downloadTarget := filepath.Join(targetPath, pkgFilename)
131131
if _, err := os.Stat(downloadTarget); err != nil {
132132
errGrp.Go(func(ctx context.Context, url, target string) func() error {
133-
return func() error { return downloadPackage(ctx, url, target) }
133+
return func() error { return DownloadPackage(ctx, url, target) }
134134
}(downloadsCtx, p, downloadTarget))
135135
}
136136
}
@@ -150,7 +150,7 @@ func DownloadComponentsFromManifest(manifest string, platforms []string, platfor
150150
return nil
151151
}
152152

153-
func downloadPackage(ctx context.Context, downloadUrl string, target string) error {
153+
func DownloadPackage(ctx context.Context, downloadUrl string, target string) error {
154154
parsedURL, errorUrl := url.Parse(downloadUrl)
155155
if errorUrl != nil {
156156
return errorInvalidManifestURL

dev-tools/mage/settings.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ const (
4444
// Env vars
4545
// agent package version
4646
agentPackageVersionEnvVar = "AGENT_PACKAGE_VERSION"
47+
//ManifestUrlEnvVar is the name fo the environment variable containing the Manifest URL to be used for packaging agent
48+
ManifestUrlEnvVar = "MANIFEST_URL"
49+
// AgentCommitHashEnvVar allows to override agent commit hash string during packaging
50+
AgentCommitHashEnvVar
4751

4852
// Mapped functions
4953
agentPackageVersionMappedFunc = "agent_package_version"
@@ -152,7 +156,7 @@ func initGlobals() {
152156

153157
agentPackageVersion = EnvOr(agentPackageVersionEnvVar, "")
154158

155-
ManifestURL = EnvOr("ManifestURL", "")
159+
ManifestURL = EnvOr(ManifestUrlEnvVar, "")
156160
PackagingFromManifest = ManifestURL != ""
157161
}
158162

@@ -281,7 +285,12 @@ var (
281285
func CommitHash() (string, error) {
282286
var err error
283287
commitHashOnce.Do(func() {
284-
commitHash, err = sh.Output("git", "rev-parse", "HEAD")
288+
// Check commit hash override first
289+
commitHash = EnvOr(AgentCommitHashEnvVar, "")
290+
if commitHash == "" {
291+
// no override found, get the hash from HEAD
292+
commitHash, err = sh.Output("git", "rev-parse", "HEAD")
293+
}
285294
})
286295
return commitHash, err
287296
}

internal/pkg/agent/application/upgrade/artifact/download/verifier.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"crypto/sha512"
1212
"encoding/hex"
1313
"fmt"
14+
"hash"
1415
"io"
1516
"net/http"
1617
"net/url"
@@ -130,8 +131,16 @@ func getHashFileName(filename string) string {
130131
// exists and that the checksum in the sidecar file matches the checksum of
131132
// the file. It returns an error if validation fails.
132133
func VerifySHA512Hash(filename string) error {
134+
hasher := sha512.New()
135+
checksumFileName := getHashFileName(filename)
136+
return VerifyChecksum(hasher, filename, checksumFileName)
137+
}
138+
139+
// VerifyChecksum checks that the hash contained in checksumFileName correspond to the hash calculated for filename using
140+
// hasher.Sum()
141+
func VerifyChecksum(hasher hash.Hash, filename, checksumFileName string) error {
133142
// Read expected checksum.
134-
expectedHash, err := readChecksumFile(getHashFileName(filename), filepath.Base(filename))
143+
expectedHash, err := readChecksumFile(checksumFileName, filepath.Base(filename))
135144
if err != nil {
136145
return fmt.Errorf("could not read checksum file: %w", err)
137146
}
@@ -143,12 +152,11 @@ func VerifySHA512Hash(filename string) error {
143152
}
144153
defer f.Close()
145154

146-
hash := sha512.New()
147-
if _, err := io.Copy(hash, f); err != nil {
155+
if _, err := io.Copy(hasher, f); err != nil {
148156
return fmt.Errorf("faled to read file to calculate hash")
149157
}
150158

151-
computedHash := hex.EncodeToString(hash.Sum(nil))
159+
computedHash := hex.EncodeToString(hasher.Sum(nil))
152160
if computedHash != expectedHash {
153161
return &ChecksumMismatchError{
154162
Expected: expectedHash,

0 commit comments

Comments
 (0)