diff --git a/integration/scripts/download-images.sh b/integration/scripts/download-images.sh deleted file mode 100755 index 86d630d4caad..000000000000 --- a/integration/scripts/download-images.sh +++ /dev/null @@ -1,21 +0,0 @@ -#!/bin/bash - -TEST_IMAGE=ghcr.io/aquasecurity/trivy-test-images - -CURRENT=$(cd $(dirname $0);pwd) - -mkdir -p ${CURRENT}/../testdata/fixtures/images/ - -# List the tags -TAGS=$(crane ls ${TEST_IMAGE}) - -# Download missing images -for tag in $TAGS -do - dir=${CURRENT}/../testdata/fixtures/images/ - if [ ! -e "${dir}/${tag}.tar.gz" ]; then - echo "Downloading $tag..." - crane pull "${TEST_IMAGE}:${tag}" "${dir}/${tag}.tar" - gzip "${dir}/${tag}.tar" - fi -done diff --git a/integration/scripts/download-vm-images.sh b/integration/scripts/download-vm-images.sh deleted file mode 100755 index 4a6307ad79f2..000000000000 --- a/integration/scripts/download-vm-images.sh +++ /dev/null @@ -1,24 +0,0 @@ -#!/bin/bash - -TEST_VM=ghcr.io/aquasecurity/trivy-test-vm-images - -CRANE_IMG=gcr.io/go-containerregistry/crane:v0.12.1 -ORAS_IMG=ghcr.io/oras-project/oras:v0.16.0 - -CURRENT=$(cd $(dirname $0);pwd) - -mkdir -p ${CURRENT}/../testdata/fixtures/vm-images/ - -# List the tags -TAGS=$(docker run --rm ${CRANE_IMG} ls ${TEST_VM}) - -# Download missing images -for tag in $TAGS -do - dir=${CURRENT}/../testdata/fixtures/vm-images/ - if [ ! -e "${dir}/${tag}.img.gz" ] || [ ! -e "${dir}/${tag}.vmdk.gz" ]; then - echo "Downloading $tag..." - echo "oras pull ${TEST_VM}:${tag}" - docker run --rm -v ${dir}:/workspace ${ORAS_IMG} pull "${TEST_VM}:${tag}" - fi -done \ No newline at end of file diff --git a/magefiles/fixture.go b/magefiles/fixture.go new file mode 100644 index 000000000000..0ed9ae8d4217 --- /dev/null +++ b/magefiles/fixture.go @@ -0,0 +1,112 @@ +package main + +import ( + "fmt" + "io" + "os" + "path/filepath" + "strings" + + "github.com/google/go-containerregistry/pkg/crane" + v1 "github.com/google/go-containerregistry/pkg/v1" + "github.com/magefile/mage/sh" +) + +func fixtureContainerImages() error { + const ( + testImages = "ghcr.io/aquasecurity/trivy-test-images" + dir = "integration/testdata/fixtures/images/" + ) + if err := os.MkdirAll(dir, 0750); err != nil { + return err + } + tags, err := crane.ListTags(testImages) + if err != nil { + return err + } + for _, tag := range tags { + fileName := tag + ".tar.gz" + filePath := filepath.Join(dir, fileName) + if exists(filePath) { + continue + } + fmt.Printf("Downloading %s...\n", tag) + imgName := fmt.Sprintf("%s:%s", testImages, tag) + img, err := crane.Pull(imgName) + if err != nil { + return err + } + tarPath := strings.TrimSuffix(filePath, ".gz") + if err = crane.Save(img, imgName, tarPath); err != nil { + return err + } + if err = sh.Run("gzip", tarPath); err != nil { + return err + } + } + return nil +} + +func fixtureVMImages() error { + const ( + testVMImages = "ghcr.io/aquasecurity/trivy-test-vm-images" + titleAnnotation = "org.opencontainers.image.title" + dir = "integration/testdata/fixtures/vm-images/" + ) + if err := os.MkdirAll(dir, 0750); err != nil { + return err + } + tags, err := crane.ListTags(testVMImages) + if err != nil { + return err + } + for _, tag := range tags { + img, err := crane.Pull(fmt.Sprintf("%s:%s", testVMImages, tag)) + if err != nil { + return err + } + + manifest, err := img.Manifest() + if err != nil { + return err + } + + layers, err := img.Layers() + if err != nil { + return err + } + + for i, layer := range layers { + fileName, ok := manifest.Layers[i].Annotations[titleAnnotation] + if !ok { + continue + } + filePath := filepath.Join(dir, fileName) + if exists(filePath) { + return nil + } + fmt.Printf("Downloading %s...\n", fileName) + if err = saveLayer(layer, filePath); err != nil { + return err + } + } + } + return nil +} + +func saveLayer(layer v1.Layer, filePath string) error { + f, err := os.Create(filePath) + if err != nil { + return err + } + defer f.Close() + + c, err := layer.Compressed() + if err != nil { + return err + } + if _, err = io.Copy(f, c); err != nil { + return err + } + return nil +} diff --git a/magefiles/magefile.go b/magefiles/magefile.go index fffd9200080a..615d99929053 100644 --- a/magefiles/magefile.go +++ b/magefiles/magefile.go @@ -61,14 +61,6 @@ func (Tool) Wire() error { return sh.Run("go", "install", "github.com/google/wire/cmd/wire@v0.5.0") } -// Crane installs crane -func (Tool) Crane() error { - if exists(filepath.Join(GOBIN, "crane")) { - return nil - } - return sh.Run("go", "install", "github.com/google/go-containerregistry/cmd/crane@v0.9.0") -} - // GolangciLint installs golangci-lint func (Tool) GolangciLint() error { const version = "v1.52.2" @@ -179,20 +171,12 @@ type Test mg.Namespace // FixtureContainerImages downloads and extracts required images func (Test) FixtureContainerImages() error { - mg.Deps(Tool{}.Crane) - if err := os.MkdirAll(filepath.Join("integration", "testdata", "fixtures", "images"), 0750); err != nil { - return err - } - - downloadScript := filepath.Join("integration", "scripts", "download-images.sh") - return sh.Run(downloadScript) + return fixtureContainerImages() } // FixtureVMImages downloads and extracts required VM images func (Test) FixtureVMImages() error { - mg.Deps(Tool{}.Crane) - downloadScript := filepath.Join("integration", "scripts", "download-vm-images.sh") - return sh.Run(downloadScript) + return fixtureVMImages() } // GenerateModules compiles WASM modules for unit tests