2022-04-27 01:00:20 -05:00
|
|
|
//go:build search
|
|
|
|
// +build search
|
2021-10-15 10:05:00 -05:00
|
|
|
|
2022-10-05 05:21:14 -05:00
|
|
|
//nolint:lll,gosimple
|
2020-06-25 03:21:47 -05:00
|
|
|
package cveinfo_test
|
|
|
|
|
|
|
|
import (
|
2020-08-19 02:10:49 -05:00
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2022-10-21 13:17:06 -05:00
|
|
|
"io"
|
2022-02-15 20:15:13 -05:00
|
|
|
"net/http"
|
2020-06-25 03:21:47 -05:00
|
|
|
"os"
|
2020-08-19 02:10:49 -05:00
|
|
|
"path"
|
2022-12-09 14:40:06 -05:00
|
|
|
"strings"
|
2020-06-25 03:21:47 -05:00
|
|
|
"testing"
|
2020-08-19 02:10:49 -05:00
|
|
|
"time"
|
2020-06-25 03:21:47 -05:00
|
|
|
|
2022-09-28 13:39:54 -05:00
|
|
|
regTypes "github.com/google/go-containerregistry/pkg/v1/types"
|
2022-10-22 15:46:13 -05:00
|
|
|
godigest "github.com/opencontainers/go-digest"
|
2020-09-04 15:16:15 -05:00
|
|
|
ispec "github.com/opencontainers/image-spec/specs-go/v1"
|
2020-06-25 03:21:47 -05:00
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
2020-08-19 02:10:49 -05:00
|
|
|
"gopkg.in/resty.v1"
|
2022-10-20 11:39:20 -05:00
|
|
|
|
2023-01-09 15:37:44 -05:00
|
|
|
zerr "zotregistry.io/zot/errors"
|
2021-12-03 22:50:58 -05:00
|
|
|
"zotregistry.io/zot/pkg/api"
|
|
|
|
"zotregistry.io/zot/pkg/api/config"
|
2022-02-24 15:31:36 -05:00
|
|
|
"zotregistry.io/zot/pkg/api/constants"
|
2021-12-03 22:50:58 -05:00
|
|
|
extconf "zotregistry.io/zot/pkg/extensions/config"
|
|
|
|
"zotregistry.io/zot/pkg/extensions/monitoring"
|
|
|
|
"zotregistry.io/zot/pkg/extensions/search/common"
|
|
|
|
cveinfo "zotregistry.io/zot/pkg/extensions/search/cve"
|
2022-09-28 13:39:54 -05:00
|
|
|
cvemodel "zotregistry.io/zot/pkg/extensions/search/cve/model"
|
2021-12-03 22:50:58 -05:00
|
|
|
"zotregistry.io/zot/pkg/log"
|
2023-01-09 15:37:44 -05:00
|
|
|
"zotregistry.io/zot/pkg/meta/repodb"
|
|
|
|
bolt "zotregistry.io/zot/pkg/meta/repodb/boltdb-wrapper"
|
2021-12-03 22:50:58 -05:00
|
|
|
"zotregistry.io/zot/pkg/storage"
|
2022-09-30 12:35:16 -05:00
|
|
|
"zotregistry.io/zot/pkg/storage/local"
|
2022-01-19 14:54:17 -05:00
|
|
|
. "zotregistry.io/zot/pkg/test"
|
2022-09-28 13:39:54 -05:00
|
|
|
"zotregistry.io/zot/pkg/test/mocks"
|
2020-06-25 03:21:47 -05:00
|
|
|
)
|
|
|
|
|
2020-08-19 02:10:49 -05:00
|
|
|
const (
|
2021-06-28 04:45:42 -05:00
|
|
|
username = "test"
|
|
|
|
passphrase = "test"
|
2020-08-19 02:10:49 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type CveResult struct {
|
2022-12-09 14:40:06 -05:00
|
|
|
ImgList ImgList `json:"data"`
|
|
|
|
Errors []ErrorGQL `json:"errors"`
|
2020-08-19 02:10:49 -05:00
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
//nolint:tagliatelle // graphQL schema
|
2022-01-19 10:57:10 -05:00
|
|
|
type ImgListWithCVEFixed struct {
|
|
|
|
Images []ImageInfo `json:"ImageListWithCVEFixed"`
|
2020-09-04 17:02:10 -05:00
|
|
|
}
|
|
|
|
|
2022-01-19 10:57:10 -05:00
|
|
|
type ImageInfo struct {
|
|
|
|
RepoName string
|
|
|
|
LastUpdated time.Time
|
2020-09-04 17:02:10 -05:00
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
//nolint:tagliatelle // graphQL schema
|
2020-08-19 02:10:49 -05:00
|
|
|
type ImgList struct {
|
|
|
|
CVEResultForImage CVEResultForImage `json:"CVEListForImage"`
|
|
|
|
}
|
|
|
|
|
2022-12-09 14:40:06 -05:00
|
|
|
type ErrorGQL struct {
|
|
|
|
Message string `json:"message"`
|
|
|
|
Path []string `json:"path"`
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
//nolint:tagliatelle // graphQL schema
|
2020-08-19 02:10:49 -05:00
|
|
|
type CVEResultForImage struct {
|
2022-09-28 13:39:54 -05:00
|
|
|
Tag string `json:"Tag"`
|
|
|
|
CVEList []cvemodel.CVE `json:"CVEList"`
|
2020-08-19 02:10:49 -05:00
|
|
|
}
|
|
|
|
|
2023-01-09 15:37:44 -05:00
|
|
|
func testSetup(t *testing.T) (string, error) {
|
|
|
|
t.Helper()
|
|
|
|
dir := t.TempDir()
|
2021-01-25 13:04:03 -05:00
|
|
|
|
2023-01-09 15:37:44 -05:00
|
|
|
err := generateTestData(dir)
|
2020-08-19 02:10:49 -05:00
|
|
|
if err != nil {
|
2023-01-09 15:37:44 -05:00
|
|
|
return "", err
|
2020-08-19 02:10:49 -05:00
|
|
|
}
|
|
|
|
|
2023-01-09 15:37:44 -05:00
|
|
|
err = CopyFiles("../../../../test/data", dir)
|
2020-08-19 02:10:49 -05:00
|
|
|
if err != nil {
|
2023-01-09 15:37:44 -05:00
|
|
|
return "", err
|
2020-08-19 02:10:49 -05:00
|
|
|
}
|
|
|
|
|
2023-01-09 15:37:44 -05:00
|
|
|
return dir, nil
|
2020-06-25 03:21:47 -05:00
|
|
|
}
|
|
|
|
|
2023-01-09 15:37:44 -05:00
|
|
|
func generateTestData(dbDir string) error { //nolint: gocyclo
|
2020-08-19 02:10:49 -05:00
|
|
|
// Image dir with no files
|
2021-12-13 14:23:31 -05:00
|
|
|
err := os.Mkdir(path.Join(dbDir, "zot-noindex-test"), 0o755)
|
2020-08-19 02:10:49 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
err = os.Mkdir(path.Join(dbDir, "zot-nonreadable-test"), 0o755)
|
2020-09-04 15:16:15 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
index := ispec.Index{}
|
|
|
|
index.SchemaVersion = 2
|
|
|
|
|
2021-01-25 13:04:03 -05:00
|
|
|
buf, err := json.Marshal(index)
|
2020-09-04 15:16:15 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-09-02 07:56:02 -05:00
|
|
|
if err = os.WriteFile(path.Join(dbDir, "zot-nonreadable-test", "index.json"), buf, 0o111); err != nil {
|
2020-09-04 15:16:15 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-08-19 02:10:49 -05:00
|
|
|
// Image dir with invalid index.json
|
2021-12-13 14:23:31 -05:00
|
|
|
err = os.Mkdir(path.Join(dbDir, "zot-squashfs-invalid-index"), 0o755)
|
2020-08-19 02:10:49 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
content := `{"schemaVersion": 2,"manifests"[{"mediaType": "application/vnd.oci.image.manifest.v1+json","digest": "sha256:2a9b097b4e4c613dd8185eba55163201a221909f3d430f8df87cd3639afc5929","size": 1240,"annotations": {"org.opencontainers.image.ref.name": "commit-aaa7c6e7-squashfs"},"platform": {"architecture": "amd64","os": "linux"}}]}`
|
2020-08-19 02:10:49 -05:00
|
|
|
|
|
|
|
err = makeTestFile(path.Join(dbDir, "zot-squashfs-invalid-index", "index.json"), content)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Image dir with no blobs
|
2021-12-13 14:23:31 -05:00
|
|
|
err = os.Mkdir(path.Join(dbDir, "zot-squashfs-noblobs"), 0o755)
|
2020-08-19 02:10:49 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
content = `{"schemaVersion":2,"manifests":[{"mediaType":"application/vnd.oci.image.manifest.v1+json","digest":"sha256:2a9b097b4e4c613dd8185eba55163201a221909f3d430f8df87cd3639afc5929","size":1240,"annotations":{"org.opencontainers.image.ref.name":"commit-aaa7c6e7-squashfs"},"platform":{"architecture":"amd64","os":"linux"}}]}`
|
2020-08-19 02:10:49 -05:00
|
|
|
|
|
|
|
err = makeTestFile(path.Join(dbDir, "zot-squashfs-noblobs", "index.json"), content)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Image dir with invalid blob
|
2021-12-13 14:23:31 -05:00
|
|
|
err = os.MkdirAll(path.Join(dbDir, "zot-squashfs-invalid-blob", "blobs/sha256"), 0o755)
|
2020-08-19 02:10:49 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
content = fmt.Sprintf(`{"schemaVersion":2,"manifests":[{"mediaType":"application/vnd.oci.image.manifest.v1+json","digest":"sha256:2a9b097b4e4c613dd8185eba55163201a221909f3d430f8df87cd3639afc5929","size":1240,"annotations":{"org.opencontainers.image.ref.name":"commit-aaa7c6e7-squashfs"},"platform":{"architecture":"amd64","os":"linux"}}]}
|
|
|
|
`)
|
|
|
|
|
|
|
|
err = makeTestFile(path.Join(dbDir, "zot-squashfs-invalid-blob", "index.json"), content)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
content = fmt.Sprintf(`{"schemaVersion":2,"config"{"mediaType":"application/vnd.oci.image.config.v1+json","digest":"sha256:4b37d4133908ac9a3032ba996020f2ad41354a616b071ca7e726a1df18a0f354","size":1691},"layers":[{"mediaType":"application/vnd.oci.image.layer.squashfs","digest":"sha256:a01a66356aace53222e92fb6fd990b23eb44ab0e58dff6f853fa9f771ecf3ac5","size":54996992},{"mediaType":"application/vnd.oci.image.layer.squashfs","digest":"sha256:91c26d6934ef2b5c5c4d8458af9bfc4ca46cf90c22380193154964abc8298a7a","size":52330496},{"mediaType":"application/vnd.oci.image.layer.squashfs","digest":"sha256:f281a550ca49746cfc6b8f1ac52f8086b3d5845db2ca18fde980dab62ae3bf7d","size":23343104},{"mediaType":"application/vnd.oci.image.layer.squashfs","digest":"sha256:7ee02568717acdda336c9d56d4dc6ea7f3b1c553e43bb0c0ecc6fd3bbd059d1a","size":5910528},{"mediaType":"application/vnd.oci.image.layer.squashfs","digest":"sha256:8fb33b130588b239235dedd560cdf49d29bbf6f2db5419ac68e4592a85c1f416","size":123269120},{"mediaType":"application/vnd.oci.image.layer.squashfs","digest":"sha256:1b49f0b33d4a696bb94d84c9acab3623e2c195bfb446d446a583a2f9f27b04c3","size":113901568}],"annotations":{"com.cisco.stacker.git_version":"7-dev19-63-gaaa7c6e7","ws.tycho.stacker.git_version":"0.3.26"}}
|
|
|
|
`)
|
|
|
|
|
|
|
|
err = makeTestFile(path.Join(dbDir, "zot-squashfs-invalid-blob", "blobs/sha256", "2a9b097b4e4c613dd8185eba55163201a221909f3d430f8df87cd3639afc5929"), content)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-04 15:16:15 -05:00
|
|
|
// Create a squashfs image
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
err = os.MkdirAll(path.Join(dbDir, "zot-squashfs-test", "blobs/sha256"), 0o755)
|
2020-08-19 02:10:49 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-04 15:16:15 -05:00
|
|
|
il := ispec.ImageLayout{Version: ispec.ImageLayoutVersion}
|
|
|
|
buf, err = json.Marshal(il)
|
2020-08-19 02:10:49 -05:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-09-02 07:56:02 -05:00
|
|
|
if err = os.WriteFile(path.Join(dbDir, "zot-squashfs-test", "oci-layout"), buf, 0o644); err != nil { //nolint: gosec
|
2020-09-04 15:16:15 -05:00
|
|
|
return err
|
|
|
|
}
|
2020-08-19 02:10:49 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
err = os.Mkdir(path.Join(dbDir, "zot-squashfs-test", ".uploads"), 0o755)
|
2020-09-04 15:16:15 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
content = fmt.Sprintf(`{"schemaVersion":2,"manifests":[{"mediaType":"application/vnd.oci.image.manifest.v1+json","digest":"sha256:eca04f027f414362596f2632746d8a178362170b9ac9af772011fedcc3877ebb","size":886,"annotations":{"org.opencontainers.image.ref.name":"0.3.25"},"platform":{"architecture":"amd64","os":"linux"}},{"mediaType":"application/vnd.oci.image.manifest.v1+json","digest":"sha256:45df53588e59759a12bd3eca553cdc9063939baac9a28d7ebb6101e4ec230b76","size":873,"annotations":{"org.opencontainers.image.ref.name":"0.3.22-squashfs"},"platform":{"architecture":"amd64","os":"linux"}},{"mediaType":"application/vnd.oci.image.manifest.v1+json","digest":"sha256:71448405a4b89539fcfa581afb4dc7d257f98857686b8138b08a1c539f313099","size":886,"annotations":{"org.opencontainers.image.ref.name":"0.3.19"},"platform":{"architecture":"amd64","os":"linux"}}]}`)
|
|
|
|
|
|
|
|
err = makeTestFile(path.Join(dbDir, "zot-squashfs-test", "index.json"), content)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
content = fmt.Sprintf(`{"schemaVersion":2,"config":{"mediaType":"application/vnd.oci.image.config.v1+json","digest":"sha256:c5c2fd2b07ad4cb025cf20936d6bce6085584b8377780599be4da8a91739f0e8","size":1738},"layers":[{"mediaType":"application/vnd.oci.image.layer.v1.tar+gzip","digest":"sha256:3414b5ef0ad2f0390daaf55b63c422eeedef6191d47036a69d8ee396fabdce72","size":58993484},{"mediaType":"application/vnd.oci.image.layer.v1.tar+gzip","digest":"sha256:a3b04fff744c13dfa4883e01fa35e01af8daa7f72d9e9b6b7fad1f28843846b6","size":55631733},{"mediaType":"application/vnd.oci.image.layer.v1.tar+gzip","digest":"sha256:754f517f58f302190aa94e025c25890c18e1e811127aed1ef25c189278ec4ab0","size":113612795},{"mediaType":"application/vnd.oci.image.layer.v1.tar+gzip","digest":"sha256:ec004cd43488b803d6e232599e83a3164394d44fcd9f44755fed7b5791087ede","size":108889651}],"annotations":{"ws.tycho.stacker.git_version":"0.3.19"}}`)
|
|
|
|
|
|
|
|
err = makeTestFile(path.Join(dbDir, "zot-squashfs-test", "blobs/sha256", "71448405a4b89539fcfa581afb4dc7d257f98857686b8138b08a1c539f313099"), content)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
content = fmt.Sprintf(`{"created": "2020-04-08T05:32:49.805795564Z","author": "","architecture": "amd64","os": "linux","config": {"Env": ["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"]},"rootfs": {"type": "layers","diff_ids": []},"history": [{"created": "2020-04-08T05:08:43.590117872Z","created_by": "stacker umoci repack"}, {"created": "2020-04-08T05:08:53.213437118Z","created_by": "stacker build","author": "","empty_layer": true}, {"created": "2020-04-08T05:12:15.999154739Z","created_by": "stacker umoci repack","author": ""}, {"created": "2020-04-08T05:12:31.0513552Z","created_by": "stacker build","author": "","empty_layer": true}, {"created": "2020-04-08T05:20:38.068800557Z","created_by": "stacker umoci repack","author": ""}, {"created": "2020-04-08T05:21:01.956154957Z","created_by": "stacker build","author": "","empty_layer": true}, {"created": "2020-04-08T05:32:24.582132274Z","created_by": "stacker umoci repack","author": ""}, {"created": "2020-04-08T05:32:49.805795564Z","created_by": "stacker build","author": "","empty_layer": true}]}`)
|
|
|
|
|
|
|
|
err = makeTestFile(path.Join(dbDir, "zot-squashfs-test", "blobs/sha256", "c5c2fd2b07ad4cb025cf20936d6bce6085584b8377780599be4da8a91739f0e8"), content)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
content = fmt.Sprintf(`{"schemaVersion":2,"config":{"mediaType":"application/vnd.oci.image.config.v1+json","digest":"sha256:5f00b5570a5561a6f9b7e66e4f26e2e30c4d09b43a8d3f993f3c1c99be6137a6","size":1740},"layers":[{"mediaType":"application/vnd.oci.image.layer.v1.tar+gzip","digest":"sha256:f8b7e41ce10d9a0f614f068326c43431c2777e6fc346f729c2a643bfab24af83","size":59451113},{"mediaType":"application/vnd.oci.image.layer.v1.tar+gzip","digest":"sha256:9ca9274f196b56a708a7a672d3de88184c0158a30744d355dd0411f3a6850fa6","size":55685756},{"mediaType":"application/vnd.oci.image.layer.v1.tar+gzip","digest":"sha256:6c1ca50788f93937e9ce9341b564f86cbbcd28e367ed6a57cfc776aee4a9d050","size":113726186},{"mediaType":"application/vnd.oci.image.layer.v1.tar+gzip","digest":"sha256:d1a92139df86bdf00c818db75bf1ecc860857d142b426e9971a62f5f90e2aa57","size":108755643}],"annotations":{"ws.tycho.stacker.git_version":"0.3.25"}}`)
|
|
|
|
|
|
|
|
err = makeTestFile(path.Join(dbDir, "zot-squashfs-test", "blobs/sha256", "eca04f027f414362596f2632746d8a178362170b9ac9af772011fedcc3877ebb"), content)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
content = fmt.Sprintf(`{"created": "2020-04-08T05:32:49.805795564Z","author": "","architecture": "amd64","os": "linux","config": {"Env": ["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"]},"rootfs": {"type": "layers","diff_ids": []},"history": [{"created": "2020-05-11T18:17:24.516727354Z","created_by": "stacker umoci repack"}, {"created": "2020-04-08T05:08:53.213437118Z","created_by": "stacker build","author": "","empty_layer": true}, {"created": "2020-04-08T05:12:15.999154739Z","created_by": "stacker umoci repack","author": ""}, {"created": "2020-04-08T05:12:31.0513552Z","created_by": "stacker build","author": "","empty_layer": true}, {"created": "2020-04-08T05:20:38.068800557Z","created_by": "stacker umoci repack","author": ""}, {"created": "2020-04-08T05:21:01.956154957Z","created_by": "stacker build","author": "","empty_layer": true}, {"created": "2020-04-08T05:32:24.582132274Z","created_by": "stacker umoci repack","author": ""}, {"created": "2020-04-08T05:32:49.805795564Z","created_by": "stacker build","author": "","empty_layer": true}]}`)
|
|
|
|
|
|
|
|
err = makeTestFile(path.Join(dbDir, "zot-squashfs-test", "blobs/sha256", "5f00b5570a5561a6f9b7e66e4f26e2e30c4d09b43a8d3f993f3c1c99be6137a6"), content)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
content = fmt.Sprintf(`{"schemaVersion":2,"config":{"mediaType":"application/vnd.oci.image.config.v1+json","digest":"sha256:1fc1d045b241b04fea54333d76d4f57eb1961f9a314413f02a956b76e77a99f0","size":1218},"layers":[{"mediaType":"application/vnd.oci.image.layer.squashfs","digest":"sha256:c40d72b1556293c00a3e4b6c64c46ef4c7ae4d876dc18bad942b7d1903e8e5b7","size":54745420},{"mediaType":"application/vnd.oci.image.layer.squashfs","digest":"sha256:4115890e3e2563e545e03f264bfecb0097e24e02306ae3e7668dea52e00c6cc2","size":52213357},{"mediaType":"application/vnd.oci.image.layer.squashfs","digest":"sha256:91859e13e0cf704d5405199d73a2d1a0718391dbb183a77c4cb85d99e923ff57","size":109479329},{"mediaType":"application/vnd.oci.image.layer.squashfs","digest":"sha256:20aef84d8098d47a0643a2f99ce05f0deed957b3a259fb708c538f23ed97cc82","size":103996238}],"annotations":{"ws.tycho.stacker.git_version":"0.3.25"}}`)
|
|
|
|
|
|
|
|
err = makeTestFile(path.Join(dbDir, "zot-squashfs-test", "blobs/sha256", "45df53588e59759a12bd3eca553cdc9063939baac9a28d7ebb6101e4ec230b76"), content)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
content = fmt.Sprintf(`{"created": "2020-04-08T05:32:49.805795564Z","author": "","architecture": "amd64","os": "linux","config": {"Env": ["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"]},"rootfs": {"type": "layers","diff_ids": []},"history": [{"created": "2020-05-11T18:17:24.516727354Z","created_by": "stacker umoci repack"}, {"created": "2020-04-08T05:08:53.213437118Z","created_by": "stacker build","author": "","empty_layer": true}, {"created": "2020-04-08T05:12:15.999154739Z","created_by": "stacker umoci repack","author": ""}, {"created": "2020-05-11T19:30:02.467956112Z","created_by": "stacker build","author": "","empty_layer": true}, {"created": "2020-04-08T05:20:38.068800557Z","created_by": "stacker umoci repack","author": ""}, {"created": "2020-04-08T05:21:01.956154957Z","created_by": "stacker build","author": "","empty_layer": true}, {"created": "2020-04-08T05:32:24.582132274Z","created_by": "stacker umoci repack","author": ""}, {"created": "2020-04-08T05:32:49.805795564Z","created_by": "stacker build","author": "","empty_layer": true}]}`)
|
|
|
|
|
|
|
|
err = makeTestFile(path.Join(dbDir, "zot-squashfs-test", "blobs/sha256", "1fc1d045b241b04fea54333d76d4f57eb1961f9a314413f02a956b76e77a99f0"), content)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a image with invalid layer blob
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
err = os.MkdirAll(path.Join(dbDir, "zot-invalid-layer", "blobs/sha256"), 0o755)
|
2020-09-04 15:16:15 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
content = fmt.Sprintf(`{"schemaVersion":2,"manifests":[{"mediaType":"application/vnd.oci.image.manifest.v1+json","digest":"sha256:eca04f027f414362596f2632746d8a178362170b9ac9af772011fedcc3877ebb","size":886,"annotations":{"org.opencontainers.image.ref.name":"0.3.25"},"platform":{"architecture":"amd64","os":"linux"}},{"mediaType":"application/vnd.oci.image.manifest.v1+json","digest":"sha256:45df53588e59759a12bd3eca553cdc9063939baac9a28d7ebb6101e4ec230b76","size":873,"annotations":{"org.opencontainers.image.ref.name":"0.3.22-squashfs"},"platform":{"architecture":"amd64","os":"linux"}},{"mediaType":"application/vnd.oci.image.manifest.v1+json","digest":"sha256:71448405a4b89539fcfa581afb4dc7d257f98857686b8138b08a1c539f313099","size":886,"annotations":{"org.opencontainers.image.ref.name":"0.3.19"},"platform":{"architecture":"amd64","os":"linux"}}]}`)
|
|
|
|
|
|
|
|
err = makeTestFile(path.Join(dbDir, "zot-invalid-layer", "index.json"), content)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
content = fmt.Sprintf(`{"schemaVersion":2,"config":{"mediaType":"application/vnd.oci.image.config.v1+json","digest":"sha256:5f00b5570a5561a6f9b7e66e4f26e2e30c4d09b43a8d3f993f3c1c99be6137a6","size":1740},"layers":[{"mediaType":"application/vnd.oci.image.layer.v1.tar+gzip","digest":"sha256:f8b7e41ce10d9a0f614f068326c43431c2777e6fc346f729c2a643bfab24af83","size":59451113},{"mediaType":"application/vnd.oci.image.layer.v1.tar+gzip","digest":"sha256:9ca9274f196b56a708a7a672d3de88184c0158a30744d355dd0411f3a6850fa6","size":55685756},{"mediaType":"application/vnd.oci.image.layer.v1.tar+gzip","digest":"sha256:6c1ca50788f93937e9ce9341b564f86cbbcd28e367ed6a57cfc776aee4a9d050","size":113726186},{"mediaType":"application/vnd.oci.image.layer.v1.tar+gzip","digest":"sha256:d1a92139df86bdf00c818db75bf1ecc860857d142b426e9971a62f5f90e2aa57","size":108755643}],"annotations":{"ws.tycho.stacker.git_version":"0.3.25"}}`)
|
|
|
|
|
|
|
|
err = makeTestFile(path.Join(dbDir, "zot-invalid-layer", "blobs/sha256", "eca04f027f414362596f2632746d8a178362170b9ac9af772011fedcc3877ebb"), content)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
content = fmt.Sprintf(`{"created":"2020-05-11T19:12:23.239785708Z","author":"root@jenkinsProduction-Atom-Full-Build-c3-master-159CI","architecture":"amd64","os":"linux","config":{"Env":["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"]},"rootfs":{"type":"layers","diff_ids":["sha256:8817d297aa60796f41f559ba688d29b31830854014091233575d474f3a6e808e","sha256:dd5a09481ae1f5caf8d1dbc87bc7f86a01af030796467ba25851ad69964d226d","sha256:a8bce2aaf5ce6f1a5459b72de64927a1e507a911453789bf60df06752222cacd","sha256:dc0b750a934e8f376af23de6dcab1af282967498844a6510aed2c61277f20c11"]},"history":[{"created":"2020-05-11T18:17:24.516727354Z","created_by":"stacker umoci repack"},{"created":"2020-05-11T18:17:33.111086359Z","created_by":"stacker build","author":"root@jenkinsProduction-Atom-Full-Build-c3-master-159CI","empty_layer":true},{"created":"2020-05-11T18:18:43.147035914Z","created_by":"stacker umoci repack","author":"root@jenkinsProduction-Atom-Full-Build-c3-master-159CI"},{"created":"2020-05-11T18:19:03.346279546Z","created_by":"stacker build","author":"root@jenkinsProduction-Atom-Full-Build-c3-master-159CI","empty_layer":true},{"created":"2020-05-11T18:27:01.623678656Z","created_by":"stacker umoci repack","author":"root@jenkinsProduction-Atom-Full-Build-c3-master-159CI"},{"created":"2020-05-11T18:27:23.420280147Z","created_by":"stacker build","author":"root@jenkinsProduction-Atom-Full-Build-c3-master-159CI","empty_layer":true},{"created":"2020-05-11T19:11:54.886053615Z","created_by":"stacker umoci repack","author":"root@jenkinsProduction-Atom-Full-Build-c3-master-159CI"},{"created":"2020-05-11T19:12:23.239785708Z","created_by":"stacker build","author":"root@jenkinsProduction-Atom-Full-Build-c3-master-159CI","empty_layer":true}]`)
|
|
|
|
|
|
|
|
err = makeTestFile(path.Join(dbDir, "zot-invalid-layer", "blobs/sha256", "5f00b5570a5561a6f9b7e66e4f26e2e30c4d09b43a8d3f993f3c1c99be6137a6"), content)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a image with no layer blob
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
err = os.MkdirAll(path.Join(dbDir, "zot-no-layer", "blobs/sha256"), 0o755)
|
2020-09-04 15:16:15 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
content = fmt.Sprintf(`{"schemaVersion":2,"manifests":[{"mediaType":"application/vnd.oci.image.manifest.v1+json","digest":"sha256:eca04f027f414362596f2632746d8a178362170b9ac9af772011fedcc3877ebb","size":886,"annotations":{"org.opencontainers.image.ref.name":"0.3.25"},"platform":{"architecture":"amd64","os":"linux"}},{"mediaType":"application/vnd.oci.image.manifest.v1+json","digest":"sha256:45df53588e59759a12bd3eca553cdc9063939baac9a28d7ebb6101e4ec230b76","size":873,"annotations":{"org.opencontainers.image.ref.name":"0.3.22-squashfs"},"platform":{"architecture":"amd64","os":"linux"}},{"mediaType":"application/vnd.oci.image.manifest.v1+json","digest":"sha256:71448405a4b89539fcfa581afb4dc7d257f98857686b8138b08a1c539f313099","size":886,"annotations":{"org.opencontainers.image.ref.name":"0.3.19"},"platform":{"architecture":"amd64","os":"linux"}}]}`)
|
|
|
|
|
|
|
|
err = makeTestFile(path.Join(dbDir, "zot-no-layer", "index.json"), content)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
content = fmt.Sprintf(`{"schemaVersion":2,"config":{"mediaType":"application/vnd.oci.image.config.v1+json","digest":"sha256:5f00b5570a5561a6f9b7e66e4f26e2e30c4d09b43a8d3f993f3c1c99be6137a6","size":1740},"layers":[{"mediaType":"application/vnd.oci.image.layer.v1.tar+gzip","digest":"sha256:f8b7e41ce10d9a0f614f068326c43431c2777e6fc346f729c2a643bfab24af83","size":59451113},{"mediaType":"application/vnd.oci.image.layer.v1.tar+gzip","digest":"sha256:9ca9274f196b56a708a7a672d3de88184c0158a30744d355dd0411f3a6850fa6","size":55685756},{"mediaType":"application/vnd.oci.image.layer.v1.tar+gzip","digest":"sha256:6c1ca50788f93937e9ce9341b564f86cbbcd28e367ed6a57cfc776aee4a9d050","size":113726186},{"mediaType":"application/vnd.oci.image.layer.v1.tar+gzip","digest":"sha256:d1a92139df86bdf00c818db75bf1ecc860857d142b426e9971a62f5f90e2aa57","size":108755643}],"annotations":{"ws.tycho.stacker.git_version":"0.3.25"}}`)
|
|
|
|
|
|
|
|
err = makeTestFile(path.Join(dbDir, "zot-no-layer", "blobs/sha256", "eca04f027f414362596f2632746d8a178362170b9ac9af772011fedcc3877ebb"), content)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
content = fmt.Sprintf(`{"created":"2020-05-11T19:12:23.239785708Z","author":"root@jenkinsProduction-Atom-Full-Build-c3-master-159CI","architecture":"amd64","os":"linux","config":{"Env":["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"]},"rootfs":{"type":"layers","diff_ids":["sha256:8817d297aa60796f41f559ba688d29b31830854014091233575d474f3a6e808e","sha256:dd5a09481ae1f5caf8d1dbc87bc7f86a01af030796467ba25851ad69964d226d","sha256:a8bce2aaf5ce6f1a5459b72de64927a1e507a911453789bf60df06752222cacd","sha256:dc0b750a934e8f376af23de6dcab1af282967498844a6510aed2c61277f20c11"]},"history":[{"created":"2020-05-11T18:17:24.516727354Z","created_by":"stacker umoci repack"},{"created":"2020-05-11T18:17:33.111086359Z","created_by":"stacker build","author":"root@jenkinsProduction-Atom-Full-Build-c3-master-159CI","empty_layer":true},{"created":"2020-05-11T18:18:43.147035914Z","created_by":"stacker umoci repack","author":"root@jenkinsProduction-Atom-Full-Build-c3-master-159CI"},{"created":"2020-05-11T18:19:03.346279546Z","created_by":"stacker build","author":"root@jenkinsProduction-Atom-Full-Build-c3-master-159CI","empty_layer":true},{"created":"2020-05-11T18:27:01.623678656Z","created_by":"stacker umoci repack","author":"root@jenkinsProduction-Atom-Full-Build-c3-master-159CI"},{"created":"2020-05-11T18:27:23.420280147Z","created_by":"stacker build","author":"root@jenkinsProduction-Atom-Full-Build-c3-master-159CI","empty_layer":true},{"created":"2020-05-11T19:11:54.886053615Z","created_by":"stacker umoci repack","author":"root@jenkinsProduction-Atom-Full-Build-c3-master-159CI"},{"created":"2020-05-11T19:12:23.239785708Z","created_by":"stacker build","author":"root@jenkinsProduction-Atom-Full-Build-c3-master-159CI","empty_layer":true}]`)
|
|
|
|
|
|
|
|
err = makeTestFile(path.Join(dbDir, "zot-no-layer", "blobs/sha256", "5f00b5570a5561a6f9b7e66e4f26e2e30c4d09b43a8d3f993f3c1c99be6137a"), content)
|
2020-08-19 02:10:49 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-21 12:37:23 -05:00
|
|
|
func makeTestFile(fileName, content string) error {
|
2022-09-02 07:56:02 -05:00
|
|
|
if err := os.WriteFile(fileName, []byte(content), 0o600); err != nil {
|
2020-08-19 02:10:49 -05:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-28 13:39:54 -05:00
|
|
|
func TestImageFormat(t *testing.T) {
|
|
|
|
Convey("Test valid image", t, func() {
|
2021-04-05 19:40:33 -05:00
|
|
|
log := log.NewLogger("debug", "")
|
2023-01-09 15:37:44 -05:00
|
|
|
imgDir := "../../../../test/data"
|
|
|
|
dbDir := t.TempDir()
|
2021-04-05 19:40:33 -05:00
|
|
|
|
2022-06-24 08:08:47 -05:00
|
|
|
conf := config.New()
|
|
|
|
conf.Extensions = &extconf.ExtensionConfig{}
|
|
|
|
conf.Extensions.Lint = &extconf.LintConfig{}
|
|
|
|
|
2022-09-28 13:39:54 -05:00
|
|
|
metrics := monitoring.NewMetricsServer(false, log)
|
2023-01-09 15:37:44 -05:00
|
|
|
defaultStore := local.NewImageStore(imgDir, false, storage.DefaultGCDelay,
|
2022-11-02 17:53:08 -05:00
|
|
|
false, false, log, metrics, nil, nil)
|
2022-09-28 13:39:54 -05:00
|
|
|
storeController := storage.StoreController{DefaultStore: defaultStore}
|
2021-04-05 19:40:33 -05:00
|
|
|
|
2023-01-09 15:37:44 -05:00
|
|
|
repoDB, err := bolt.NewBoltDBWrapper(bolt.DBParameters{
|
|
|
|
RootDir: dbDir,
|
|
|
|
})
|
|
|
|
So(err, ShouldBeNil)
|
2021-04-05 19:40:33 -05:00
|
|
|
|
2023-01-09 15:37:44 -05:00
|
|
|
err = repodb.SyncRepoDB(repoDB, storeController, log)
|
2022-09-28 13:39:54 -05:00
|
|
|
So(err, ShouldBeNil)
|
2023-01-09 15:37:44 -05:00
|
|
|
|
|
|
|
cveInfo := cveinfo.NewCVEInfo(storeController, repoDB, log)
|
|
|
|
|
|
|
|
isValidImage, err := cveInfo.Scanner.IsImageFormatScannable("zot-test")
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
So(isValidImage, ShouldEqual, false)
|
2021-04-05 19:40:33 -05:00
|
|
|
|
2022-09-28 13:39:54 -05:00
|
|
|
isValidImage, err = cveInfo.Scanner.IsImageFormatScannable("zot-test:0.0.1")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(isValidImage, ShouldEqual, true)
|
2021-04-05 19:40:33 -05:00
|
|
|
|
2022-09-28 13:39:54 -05:00
|
|
|
isValidImage, err = cveInfo.Scanner.IsImageFormatScannable("zot-test:0.0.")
|
2023-01-09 15:37:44 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
2022-09-28 13:39:54 -05:00
|
|
|
So(isValidImage, ShouldEqual, false)
|
2021-04-05 19:40:33 -05:00
|
|
|
|
2022-09-28 13:39:54 -05:00
|
|
|
isValidImage, err = cveInfo.Scanner.IsImageFormatScannable("zot-noindex-test")
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
So(isValidImage, ShouldEqual, false)
|
2021-04-05 19:40:33 -05:00
|
|
|
|
2022-09-28 13:39:54 -05:00
|
|
|
isValidImage, err = cveInfo.Scanner.IsImageFormatScannable("zot--tet")
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
So(isValidImage, ShouldEqual, false)
|
2021-04-05 19:40:33 -05:00
|
|
|
|
2022-09-28 13:39:54 -05:00
|
|
|
isValidImage, err = cveInfo.Scanner.IsImageFormatScannable("zot-noindex-test")
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
So(isValidImage, ShouldEqual, false)
|
2021-04-05 19:40:33 -05:00
|
|
|
|
2022-09-28 13:39:54 -05:00
|
|
|
isValidImage, err = cveInfo.Scanner.IsImageFormatScannable("zot-squashfs-noblobs")
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
So(isValidImage, ShouldEqual, false)
|
2021-04-05 19:40:33 -05:00
|
|
|
|
2022-09-28 13:39:54 -05:00
|
|
|
isValidImage, err = cveInfo.Scanner.IsImageFormatScannable("zot-squashfs-invalid-index")
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
So(isValidImage, ShouldEqual, false)
|
|
|
|
|
|
|
|
isValidImage, err = cveInfo.Scanner.IsImageFormatScannable("zot-squashfs-invalid-blob")
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
So(isValidImage, ShouldEqual, false)
|
|
|
|
|
|
|
|
isValidImage, err = cveInfo.Scanner.IsImageFormatScannable("zot-squashfs-test:0.3.22-squashfs")
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
So(isValidImage, ShouldEqual, false)
|
|
|
|
|
|
|
|
isValidImage, err = cveInfo.Scanner.IsImageFormatScannable("zot-nonreadable-test")
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
So(isValidImage, ShouldEqual, false)
|
2021-04-05 19:40:33 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-08-19 02:10:49 -05:00
|
|
|
func TestCVESearch(t *testing.T) {
|
2021-12-28 08:29:30 -05:00
|
|
|
Convey("Test image vulnerability scanning", t, func() {
|
2023-01-09 15:37:44 -05:00
|
|
|
updateDuration, _ := time.ParseDuration("1h")
|
2021-11-10 09:31:03 -05:00
|
|
|
port := GetFreePort()
|
|
|
|
baseURL := GetBaseURL(port)
|
2021-06-08 15:11:18 -05:00
|
|
|
conf := config.New()
|
|
|
|
conf.HTTP.Port = port
|
2021-11-10 09:31:03 -05:00
|
|
|
htpasswdPath := MakeHtpasswdFile()
|
2020-08-19 02:10:49 -05:00
|
|
|
defer os.Remove(htpasswdPath)
|
|
|
|
|
2023-01-09 15:37:44 -05:00
|
|
|
dbDir, err := testSetup(t)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2021-06-08 15:11:18 -05:00
|
|
|
conf.HTTP.Auth = &config.AuthConfig{
|
|
|
|
HTPasswd: config.AuthHTPasswd{
|
2020-08-19 02:10:49 -05:00
|
|
|
Path: htpasswdPath,
|
|
|
|
},
|
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-12-28 08:29:30 -05:00
|
|
|
conf.Storage.RootDirectory = dbDir
|
2021-06-08 15:11:18 -05:00
|
|
|
cveConfig := &extconf.CVEConfig{
|
2020-10-14 12:29:17 -05:00
|
|
|
UpdateInterval: updateDuration,
|
2020-08-19 02:10:49 -05:00
|
|
|
}
|
2021-12-28 08:29:30 -05:00
|
|
|
defaultVal := true
|
2021-06-08 15:11:18 -05:00
|
|
|
searchConfig := &extconf.SearchConfig{
|
2022-10-21 07:33:54 -05:00
|
|
|
BaseConfig: extconf.BaseConfig{Enable: &defaultVal},
|
|
|
|
CVE: cveConfig,
|
2020-08-19 02:10:49 -05:00
|
|
|
}
|
2021-12-28 08:29:30 -05:00
|
|
|
conf.Extensions = &extconf.ExtensionConfig{
|
2020-08-19 02:10:49 -05:00
|
|
|
Search: searchConfig,
|
|
|
|
}
|
2021-12-28 08:29:30 -05:00
|
|
|
|
2022-10-21 13:17:06 -05:00
|
|
|
logFile, err := os.CreateTemp(t.TempDir(), "zot-log*.txt")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
logPath := logFile.Name()
|
|
|
|
defer os.Remove(logPath)
|
|
|
|
|
|
|
|
writers := io.MultiWriter(os.Stdout, logFile)
|
|
|
|
|
2021-12-28 08:29:30 -05:00
|
|
|
ctlr := api.NewController(conf)
|
2022-10-21 13:17:06 -05:00
|
|
|
ctlr.Log.Logger = ctlr.Log.Output(writers)
|
2021-12-28 08:29:30 -05:00
|
|
|
|
2020-08-19 02:10:49 -05:00
|
|
|
go func() {
|
|
|
|
// this blocks
|
2022-03-24 07:49:51 -05:00
|
|
|
if err := ctlr.Run(context.Background()); err != nil {
|
2020-08-19 02:10:49 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// wait till ready
|
|
|
|
for {
|
2021-06-28 04:45:42 -05:00
|
|
|
_, err := resty.R().Get(baseURL)
|
2020-08-19 02:10:49 -05:00
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
}
|
|
|
|
|
2020-10-02 18:47:54 -05:00
|
|
|
// Wait for trivy db to download
|
2022-10-21 13:17:06 -05:00
|
|
|
_, err = ReadLogFileAndSearchString(logPath, "DB update completed, next update scheduled", 90*time.Second)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-10-02 18:47:54 -05:00
|
|
|
|
2020-08-19 02:10:49 -05:00
|
|
|
defer func() {
|
|
|
|
ctx := context.Background()
|
2021-12-13 14:23:31 -05:00
|
|
|
_ = ctlr.Server.Shutdown(ctx)
|
2020-08-19 02:10:49 -05:00
|
|
|
}()
|
|
|
|
|
|
|
|
// without creds, should get access error
|
2021-06-28 04:45:42 -05:00
|
|
|
resp, err := resty.R().Get(baseURL + "/v2/")
|
2020-08-19 02:10:49 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(resp, ShouldNotBeNil)
|
|
|
|
So(resp.StatusCode(), ShouldEqual, 401)
|
2022-03-21 12:37:23 -05:00
|
|
|
var apiErr api.Error
|
|
|
|
err = json.Unmarshal(resp.Body(), &apiErr)
|
2020-08-19 02:10:49 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2022-10-18 22:46:06 -05:00
|
|
|
resp, err = resty.R().Get(baseURL + constants.FullSearchPrefix)
|
2020-08-19 02:10:49 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(resp, ShouldNotBeNil)
|
|
|
|
So(resp.StatusCode(), ShouldEqual, 401)
|
2022-03-21 12:37:23 -05:00
|
|
|
err = json.Unmarshal(resp.Body(), &apiErr)
|
2020-08-19 02:10:49 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
// with creds, should get expected status code
|
2021-06-28 04:45:42 -05:00
|
|
|
resp, _ = resty.R().SetBasicAuth(username, passphrase).Get(baseURL)
|
2020-08-19 02:10:49 -05:00
|
|
|
So(resp, ShouldNotBeNil)
|
|
|
|
So(resp.StatusCode(), ShouldEqual, 404)
|
|
|
|
|
2021-06-28 04:45:42 -05:00
|
|
|
resp, _ = resty.R().SetBasicAuth(username, passphrase).Get(baseURL + "/v2/")
|
2020-08-19 02:10:49 -05:00
|
|
|
So(resp, ShouldNotBeNil)
|
|
|
|
So(resp.StatusCode(), ShouldEqual, 200)
|
|
|
|
|
2022-10-18 22:46:06 -05:00
|
|
|
resp, _ = resty.R().SetBasicAuth(username, passphrase).Get(baseURL + constants.FullSearchPrefix)
|
2020-08-19 02:10:49 -05:00
|
|
|
So(resp, ShouldNotBeNil)
|
2022-07-15 06:10:51 -05:00
|
|
|
So(resp.StatusCode(), ShouldEqual, 422)
|
2020-08-19 02:10:49 -05:00
|
|
|
|
2022-12-09 14:40:06 -05:00
|
|
|
var cveResult CveResult
|
|
|
|
contains := false
|
|
|
|
resp, _ = resty.R().SetBasicAuth(username, passphrase).Get(baseURL + constants.FullSearchPrefix + "?query={CVEListForImage(image:\"zot-test\"){Tag%20CVEList{Id%20Description%20Severity%20PackageList{Name%20InstalledVersion%20FixedVersion}}}}")
|
|
|
|
err = json.Unmarshal(resp.Body(), &cveResult)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
for _, err := range cveResult.Errors {
|
|
|
|
result := strings.Contains(err.Message, "no reference provided")
|
|
|
|
if result {
|
|
|
|
contains = result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
So(contains, ShouldBeTrue)
|
|
|
|
|
2022-10-18 22:46:06 -05:00
|
|
|
resp, _ = resty.R().SetBasicAuth(username, passphrase).Get(baseURL + constants.FullSearchPrefix + "?query={CVEListForImage(image:\"zot-test:0.0.1\"){Tag%20CVEList{Id%20Description%20Severity%20PackageList{Name%20InstalledVersion%20FixedVersion}}}}")
|
2020-08-19 02:10:49 -05:00
|
|
|
So(resp, ShouldNotBeNil)
|
|
|
|
So(resp.StatusCode(), ShouldEqual, 200)
|
|
|
|
|
|
|
|
err = json.Unmarshal(resp.Body(), &cveResult)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(len(cveResult.ImgList.CVEResultForImage.CVEList), ShouldNotBeZeroValue)
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
cvid := cveResult.ImgList.CVEResultForImage.CVEList[0].ID
|
2020-08-19 02:10:49 -05:00
|
|
|
|
2022-10-18 22:46:06 -05:00
|
|
|
resp, _ = resty.R().SetBasicAuth(username, passphrase).Get(baseURL + constants.FullSearchPrefix + "?query={ImageListWithCVEFixed(id:\"" + cvid + "\",image:\"zot-test\"){RepoName%20LastUpdated}}")
|
2020-08-19 02:10:49 -05:00
|
|
|
So(resp, ShouldNotBeNil)
|
|
|
|
So(resp.StatusCode(), ShouldEqual, 200)
|
|
|
|
|
2022-01-19 10:57:10 -05:00
|
|
|
var imgListWithCVEFixed ImgListWithCVEFixed
|
|
|
|
err = json.Unmarshal(resp.Body(), &imgListWithCVEFixed)
|
2020-09-04 15:16:15 -05:00
|
|
|
So(err, ShouldBeNil)
|
2022-01-19 10:57:10 -05:00
|
|
|
So(len(imgListWithCVEFixed.Images), ShouldEqual, 0)
|
2020-09-04 15:16:15 -05:00
|
|
|
|
2022-10-18 22:46:06 -05:00
|
|
|
resp, _ = resty.R().SetBasicAuth(username, passphrase).Get(baseURL + constants.FullSearchPrefix + "?query={ImageListWithCVEFixed(id:\"" + cvid + "\",image:\"zot-cve-test\"){RepoName%20LastUpdated}}")
|
2020-08-19 02:10:49 -05:00
|
|
|
So(resp, ShouldNotBeNil)
|
|
|
|
So(resp.StatusCode(), ShouldEqual, 200)
|
|
|
|
|
2022-01-19 10:57:10 -05:00
|
|
|
err = json.Unmarshal(resp.Body(), &imgListWithCVEFixed)
|
2020-09-04 17:02:10 -05:00
|
|
|
So(err, ShouldBeNil)
|
2022-01-19 10:57:10 -05:00
|
|
|
So(len(imgListWithCVEFixed.Images), ShouldEqual, 0)
|
2020-09-04 17:02:10 -05:00
|
|
|
|
2022-10-18 22:46:06 -05:00
|
|
|
resp, _ = resty.R().SetBasicAuth(username, passphrase).Get(baseURL + constants.FullSearchPrefix + "?query={ImageListWithCVEFixed(id:\"" + cvid + "\",image:\"zot-test\"){RepoName%20LastUpdated}}")
|
2020-09-04 17:02:10 -05:00
|
|
|
So(resp, ShouldNotBeNil)
|
|
|
|
So(resp.StatusCode(), ShouldEqual, 200)
|
|
|
|
|
2022-10-18 22:46:06 -05:00
|
|
|
resp, _ = resty.R().SetBasicAuth(username, passphrase).Get(baseURL + constants.FullSearchPrefix + "?query={CVEListForImage(image:\"b/zot-squashfs-test:commit-aaa7c6e7-squashfs\"){Tag%20CVEList{Id%20Description%20Severity%20PackageList{Name%20InstalledVersion%20FixedVersion}}}}")
|
2020-08-19 02:10:49 -05:00
|
|
|
So(resp, ShouldNotBeNil)
|
|
|
|
So(resp.StatusCode(), ShouldEqual, 200)
|
|
|
|
|
|
|
|
var cveSquashFSResult CveResult
|
|
|
|
err = json.Unmarshal(resp.Body(), &cveSquashFSResult)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(len(cveSquashFSResult.ImgList.CVEResultForImage.CVEList), ShouldBeZeroValue)
|
|
|
|
|
2022-10-18 22:46:06 -05:00
|
|
|
resp, _ = resty.R().SetBasicAuth(username, passphrase).Get(baseURL + constants.FullSearchPrefix + "?query={CVEListForImage(image:\"zot-squashfs-noindex:commit-aaa7c6e7-squashfs\"){Tag%20CVEList{Id%20Description%20Severity%20PackageList{Name%20InstalledVersion%20FixedVersion}}}}")
|
2020-08-19 02:10:49 -05:00
|
|
|
So(resp, ShouldNotBeNil)
|
|
|
|
So(resp.StatusCode(), ShouldEqual, 200)
|
|
|
|
|
2022-10-18 22:46:06 -05:00
|
|
|
resp, _ = resty.R().SetBasicAuth(username, passphrase).Get(baseURL + constants.FullSearchPrefix + "?query={ImageListWithCVEFixed(id:\"" + cvid + "\",image:\"zot-squashfs-noindex\"){RepoName%20LastUpdated}}")
|
2020-08-19 02:10:49 -05:00
|
|
|
So(resp, ShouldNotBeNil)
|
|
|
|
So(resp.StatusCode(), ShouldEqual, 200)
|
|
|
|
|
2022-10-18 22:46:06 -05:00
|
|
|
resp, _ = resty.R().SetBasicAuth(username, passphrase).Get(baseURL + constants.FullSearchPrefix + "?query={CVEListForImage(image:\"zot-squashfs-invalid-index:commit-aaa7c6e7-squashfs\"){Tag%20CVEList{Id%20Description%20Severity%20PackageList{Name%20InstalledVersion%20FixedVersion}}}}")
|
2020-08-19 02:10:49 -05:00
|
|
|
So(resp, ShouldNotBeNil)
|
|
|
|
So(resp.StatusCode(), ShouldEqual, 200)
|
|
|
|
|
2022-10-18 22:46:06 -05:00
|
|
|
resp, _ = resty.R().SetBasicAuth(username, passphrase).Get(baseURL + constants.FullSearchPrefix + "?query={ImageListWithCVEFixed(id:\"" + cvid + "\",image:\"zot-squashfs-invalid-index\"){RepoName%20LastUpdated}}")
|
2020-08-19 02:10:49 -05:00
|
|
|
So(resp, ShouldNotBeNil)
|
|
|
|
So(resp.StatusCode(), ShouldEqual, 200)
|
|
|
|
|
2022-10-18 22:46:06 -05:00
|
|
|
resp, _ = resty.R().SetBasicAuth(username, passphrase).Get(baseURL + constants.FullSearchPrefix + "?query={CVEListForImage(image:\"zot-squashfs-noblobs:commit-aaa7c6e7-squashfs\"){Tag%20CVEList{Id%20Description%20Severity%20PackageList{Name%20InstalledVersion%20FixedVersion}}}}")
|
2020-08-19 02:10:49 -05:00
|
|
|
So(resp, ShouldNotBeNil)
|
|
|
|
So(resp.StatusCode(), ShouldEqual, 200)
|
|
|
|
|
2022-10-18 22:46:06 -05:00
|
|
|
resp, _ = resty.R().SetBasicAuth(username, passphrase).Get(baseURL + constants.FullSearchPrefix + "?query={ImageListWithCVEFixed(id:\"" + cvid + "\",image:\"zot-squashfs-noblob\"){RepoName%20LastUpdated}}")
|
2020-08-19 02:10:49 -05:00
|
|
|
So(resp, ShouldNotBeNil)
|
|
|
|
So(resp.StatusCode(), ShouldEqual, 200)
|
|
|
|
|
2022-10-18 22:46:06 -05:00
|
|
|
resp, _ = resty.R().SetBasicAuth(username, passphrase).Get(baseURL + constants.FullSearchPrefix + "?query={ImageListWithCVEFixed(id:\"" + cvid + "\",image:\"zot-squashfs-test\"){RepoName%20LastUpdated}}")
|
2020-09-04 15:16:15 -05:00
|
|
|
So(resp, ShouldNotBeNil)
|
|
|
|
So(resp.StatusCode(), ShouldEqual, 200)
|
|
|
|
|
2022-10-18 22:46:06 -05:00
|
|
|
resp, _ = resty.R().SetBasicAuth(username, passphrase).Get(baseURL + constants.FullSearchPrefix + "?query={CVEListForImage(image:\"zot-squashfs-invalid-blob:commit-aaa7c6e7-squashfs\"){Tag%20CVEList{Id%20Description%20Severity%20PackageList{Name%20InstalledVersion%20FixedVersion}}}}")
|
2020-08-19 02:10:49 -05:00
|
|
|
So(resp, ShouldNotBeNil)
|
|
|
|
So(resp.StatusCode(), ShouldEqual, 200)
|
|
|
|
|
2022-10-18 22:46:06 -05:00
|
|
|
resp, _ = resty.R().SetBasicAuth(username, passphrase).Get(baseURL + constants.FullSearchPrefix + "?query={ImageListWithCVEFixed(id:\"" + cvid + "\",image:\"zot-squashfs-invalid-blob\"){RepoName%20LastUpdated}}")
|
2020-08-19 02:10:49 -05:00
|
|
|
So(resp, ShouldNotBeNil)
|
|
|
|
So(resp.StatusCode(), ShouldEqual, 200)
|
|
|
|
|
2022-10-18 22:46:06 -05:00
|
|
|
resp, _ = resty.R().SetBasicAuth(username, passphrase).Get(baseURL + constants.FullSearchPrefix + "?query={CVEListForImage(image:\"zot-squashfs-test\"){Tag%20CVEList{Id%20Description%20Severity%20PackageList{Name%20InstalledVersion%20FixedVersion}}}}")
|
2020-08-19 02:10:49 -05:00
|
|
|
So(resp, ShouldNotBeNil)
|
|
|
|
So(resp.StatusCode(), ShouldEqual, 200)
|
|
|
|
|
2022-10-18 22:46:06 -05:00
|
|
|
resp, _ = resty.R().SetBasicAuth(username, passphrase).Get(baseURL + constants.FullSearchPrefix + "?query={CVEListForImage(image:\"cntos\"){Tag%20CVEList{Id%20Description%20Severity}}}")
|
2020-08-19 02:10:49 -05:00
|
|
|
So(resp, ShouldNotBeNil)
|
|
|
|
So(resp.StatusCode(), ShouldEqual, 200)
|
|
|
|
|
2022-10-18 22:46:06 -05:00
|
|
|
resp, _ = resty.R().SetBasicAuth(username, passphrase).Get(baseURL + constants.FullSearchPrefix + "?query={ImageListForCVE(id:\"CVE-201-20482\"){RepoName%20Tag}}")
|
2020-08-19 02:10:49 -05:00
|
|
|
So(resp, ShouldNotBeNil)
|
|
|
|
So(resp.StatusCode(), ShouldEqual, 200)
|
|
|
|
|
2022-10-18 22:46:06 -05:00
|
|
|
resp, _ = resty.R().SetBasicAuth(username, passphrase).Get(baseURL + constants.FullSearchPrefix + "?query={CVEListForImage(image:\"zot-test\"){Tag%20CVEList{Id%20Description}}}")
|
2020-08-19 02:10:49 -05:00
|
|
|
So(resp, ShouldNotBeNil)
|
|
|
|
So(resp.StatusCode(), ShouldEqual, 200)
|
|
|
|
|
2022-10-18 22:46:06 -05:00
|
|
|
resp, _ = resty.R().SetBasicAuth(username, passphrase).Get(baseURL + constants.FullSearchPrefix + "?query={CVEListForImage(image:\"zot-test:0.0.1\"){Tag}}")
|
2020-08-19 02:10:49 -05:00
|
|
|
So(resp, ShouldNotBeNil)
|
|
|
|
So(resp.StatusCode(), ShouldEqual, 200)
|
|
|
|
|
2022-10-18 22:46:06 -05:00
|
|
|
resp, _ = resty.R().SetBasicAuth(username, passphrase).Get(baseURL + constants.FullSearchPrefix + "?query={CVEListForImage(image:\"zot-test:0.0.1\"){CVEList{Id%20Description%20Severity}}}")
|
2020-08-19 02:10:49 -05:00
|
|
|
So(resp, ShouldNotBeNil)
|
|
|
|
So(resp.StatusCode(), ShouldEqual, 200)
|
|
|
|
|
2022-10-18 22:46:06 -05:00
|
|
|
resp, _ = resty.R().SetBasicAuth(username, passphrase).Get(baseURL + constants.FullSearchPrefix + "?query={CVEListForImage(image:\"zot-test:0.0.1\"){CVEList{Description%20Severity}}}")
|
2020-08-19 02:10:49 -05:00
|
|
|
So(resp, ShouldNotBeNil)
|
|
|
|
So(resp.StatusCode(), ShouldEqual, 200)
|
|
|
|
|
2022-10-18 22:46:06 -05:00
|
|
|
resp, _ = resty.R().SetBasicAuth(username, passphrase).Get(baseURL + constants.FullSearchPrefix + "?query={CVEListForImage(image:\"zot-test:0.0.1\"){CVEList{Id%20Severity}}}")
|
2020-08-19 02:10:49 -05:00
|
|
|
So(resp, ShouldNotBeNil)
|
|
|
|
So(resp.StatusCode(), ShouldEqual, 200)
|
|
|
|
|
2022-10-18 22:46:06 -05:00
|
|
|
resp, _ = resty.R().SetBasicAuth(username, passphrase).Get(baseURL + constants.FullSearchPrefix + "?query={CVEListForImage(image:\"zot-test:0.0.1\"){CVEList{Id%20Description}}}")
|
2020-08-19 02:10:49 -05:00
|
|
|
So(resp, ShouldNotBeNil)
|
|
|
|
So(resp.StatusCode(), ShouldEqual, 200)
|
|
|
|
|
2022-10-18 22:46:06 -05:00
|
|
|
resp, _ = resty.R().SetBasicAuth(username, passphrase).Get(baseURL + constants.FullSearchPrefix + "?query={CVEListForImage(image:\"zot-test:0.0.1\"){CVEList{Id}}}")
|
2020-08-19 02:10:49 -05:00
|
|
|
So(resp, ShouldNotBeNil)
|
|
|
|
So(resp.StatusCode(), ShouldEqual, 200)
|
|
|
|
|
2022-10-18 22:46:06 -05:00
|
|
|
resp, _ = resty.R().SetBasicAuth(username, passphrase).Get(baseURL + constants.FullSearchPrefix + "?query={CVEListForImage(image:\"zot-test:0.0.1\"){CVEList{Description}}}")
|
2020-08-19 02:10:49 -05:00
|
|
|
So(resp, ShouldNotBeNil)
|
|
|
|
So(resp.StatusCode(), ShouldEqual, 200)
|
|
|
|
|
|
|
|
// Testing Invalid Search URL
|
2022-10-18 22:46:06 -05:00
|
|
|
resp, _ = resty.R().SetBasicAuth(username, passphrase).Get(baseURL + constants.FullSearchPrefix + "?query={CVEListForImage(image:\"zot-test:0.0.1\"){Ta%20CVEList{Id%20Description%20Severity}}}")
|
2020-08-19 02:10:49 -05:00
|
|
|
So(resp, ShouldNotBeNil)
|
|
|
|
So(resp.StatusCode(), ShouldEqual, 422)
|
|
|
|
|
2022-10-18 22:46:06 -05:00
|
|
|
resp, _ = resty.R().SetBasicAuth(username, passphrase).Get(baseURL + constants.FullSearchPrefix + "?query={ImageListForCVE(tet:\"CVE-2018-20482\"){RepoName%20Tag}}")
|
2020-08-19 02:10:49 -05:00
|
|
|
So(resp, ShouldNotBeNil)
|
|
|
|
So(resp.StatusCode(), ShouldEqual, 422)
|
|
|
|
|
2022-10-18 22:46:06 -05:00
|
|
|
resp, _ = resty.R().SetBasicAuth(username, passphrase).Get(baseURL + constants.FullSearchPrefix + "?query={ImageistForCVE(id:\"CVE-2018-20482\"){RepoName%20Tag}}")
|
2020-08-19 02:10:49 -05:00
|
|
|
So(resp, ShouldNotBeNil)
|
|
|
|
So(resp.StatusCode(), ShouldEqual, 422)
|
|
|
|
|
2022-10-18 22:46:06 -05:00
|
|
|
resp, _ = resty.R().SetBasicAuth(username, passphrase).Get(baseURL + constants.FullSearchPrefix + "?query={ImageListForCVE(id:\"CVE-2018-20482\"){ame%20Tags}}")
|
2020-08-19 02:10:49 -05:00
|
|
|
So(resp, ShouldNotBeNil)
|
|
|
|
So(resp.StatusCode(), ShouldEqual, 422)
|
|
|
|
|
2022-10-18 22:46:06 -05:00
|
|
|
resp, _ = resty.R().SetBasicAuth(username, passphrase).Get(baseURL + constants.FullSearchPrefix + "?query={CVEListForImage(reo:\"zot-test:1.0.0\"){Tag%20CVEList{Id%20Description%20Severity}}}")
|
2020-08-19 02:10:49 -05:00
|
|
|
So(resp, ShouldNotBeNil)
|
|
|
|
So(resp.StatusCode(), ShouldEqual, 422)
|
2020-09-04 15:16:15 -05:00
|
|
|
|
2022-10-18 22:46:06 -05:00
|
|
|
resp, _ = resty.R().SetBasicAuth(username, passphrase).Get(baseURL + constants.FullSearchPrefix + "?query={ImageListForCVE(id:\"" + cvid + "\"){RepoName%20Tag}}")
|
2020-09-04 15:16:15 -05:00
|
|
|
So(resp, ShouldNotBeNil)
|
|
|
|
So(resp.StatusCode(), ShouldEqual, 200)
|
2020-08-19 02:10:49 -05:00
|
|
|
})
|
|
|
|
}
|
2020-10-22 19:31:16 -05:00
|
|
|
|
|
|
|
func TestCVEConfig(t *testing.T) {
|
|
|
|
Convey("Verify CVE config", t, func() {
|
2021-06-08 15:11:18 -05:00
|
|
|
conf := config.New()
|
2021-11-10 09:31:03 -05:00
|
|
|
port := GetFreePort()
|
2021-10-15 10:05:00 -05:00
|
|
|
conf.HTTP.Port = port
|
2021-11-10 09:31:03 -05:00
|
|
|
baseURL := GetBaseURL(port)
|
|
|
|
htpasswdPath := MakeHtpasswdFile()
|
2020-10-22 19:31:16 -05:00
|
|
|
defer os.Remove(htpasswdPath)
|
|
|
|
|
2021-06-08 15:11:18 -05:00
|
|
|
conf.HTTP.Auth = &config.AuthConfig{
|
|
|
|
HTPasswd: config.AuthHTPasswd{
|
2020-10-22 19:31:16 -05:00
|
|
|
Path: htpasswdPath,
|
|
|
|
},
|
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
|
|
|
|
ctlr := api.NewController(conf)
|
|
|
|
|
2022-03-07 03:55:12 -05:00
|
|
|
firstDir := t.TempDir()
|
2021-04-05 19:40:33 -05:00
|
|
|
|
2022-03-07 03:55:12 -05:00
|
|
|
secondDir := t.TempDir()
|
2021-04-05 19:40:33 -05:00
|
|
|
|
2022-03-07 03:55:12 -05:00
|
|
|
err := CopyFiles("../../../../test/data", path.Join(secondDir, "a"))
|
2021-04-05 19:40:33 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
ctlr.Config.Storage.RootDirectory = firstDir
|
2021-06-08 15:11:18 -05:00
|
|
|
subPaths := make(map[string]config.StorageConfig)
|
|
|
|
subPaths["/a"] = config.StorageConfig{
|
2021-04-05 19:40:33 -05:00
|
|
|
RootDirectory: secondDir,
|
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
|
|
|
|
ctlr.Config.Storage.SubPaths = subPaths
|
2021-04-05 19:40:33 -05:00
|
|
|
|
2020-10-22 19:31:16 -05:00
|
|
|
go func() {
|
|
|
|
// this blocks
|
2022-03-24 07:49:51 -05:00
|
|
|
if err := ctlr.Run(context.Background()); err != nil {
|
2020-10-22 19:31:16 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// wait till ready
|
|
|
|
for {
|
2021-06-28 04:45:42 -05:00
|
|
|
_, err := resty.R().Get(baseURL)
|
2020-10-22 19:31:16 -05:00
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
}
|
|
|
|
|
2022-02-24 15:31:36 -05:00
|
|
|
resp, _ := resty.R().SetBasicAuth(username, passphrase).Get(baseURL + constants.RoutePrefix + "/")
|
2021-04-05 19:40:33 -05:00
|
|
|
So(resp, ShouldNotBeNil)
|
|
|
|
So(resp.StatusCode(), ShouldEqual, 200)
|
|
|
|
|
2022-02-24 15:31:36 -05:00
|
|
|
resp, _ = resty.R().SetBasicAuth(username, passphrase).Get(baseURL + constants.RoutePrefix + constants.ExtCatalogPrefix)
|
2021-04-05 19:40:33 -05:00
|
|
|
So(resp, ShouldNotBeNil)
|
|
|
|
So(resp.StatusCode(), ShouldEqual, 200)
|
|
|
|
|
2021-06-28 04:45:42 -05:00
|
|
|
resp, _ = resty.R().SetBasicAuth(username, passphrase).Get(baseURL + "/v2/a/zot-test/tags/list")
|
2021-04-05 19:40:33 -05:00
|
|
|
So(resp, ShouldNotBeNil)
|
|
|
|
So(resp.StatusCode(), ShouldEqual, 200)
|
|
|
|
|
2021-06-28 04:45:42 -05:00
|
|
|
resp, _ = resty.R().SetBasicAuth(username, passphrase).Get(baseURL + "/v2/zot-test/tags/list")
|
2021-04-05 19:40:33 -05:00
|
|
|
So(resp, ShouldNotBeNil)
|
|
|
|
So(resp.StatusCode(), ShouldEqual, 404)
|
|
|
|
|
2020-10-22 19:31:16 -05:00
|
|
|
defer func() {
|
|
|
|
ctx := context.Background()
|
2021-12-13 14:23:31 -05:00
|
|
|
_ = ctlr.Server.Shutdown(ctx)
|
2020-10-22 19:31:16 -05:00
|
|
|
}()
|
|
|
|
})
|
|
|
|
}
|
2022-02-15 20:15:13 -05:00
|
|
|
|
|
|
|
func TestHTTPOptionsResponse(t *testing.T) {
|
|
|
|
Convey("Test http options response", t, func() {
|
|
|
|
conf := config.New()
|
|
|
|
port := GetFreePort()
|
|
|
|
conf.HTTP.Port = port
|
|
|
|
baseURL := GetBaseURL(port)
|
|
|
|
|
|
|
|
ctlr := api.NewController(conf)
|
|
|
|
|
2022-09-02 07:56:02 -05:00
|
|
|
firstDir, err := os.MkdirTemp("", "oci-repo-test")
|
2022-02-15 20:15:13 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2022-09-02 07:56:02 -05:00
|
|
|
secondDir, err := os.MkdirTemp("", "oci-repo-test")
|
2022-02-15 20:15:13 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(firstDir)
|
|
|
|
defer os.RemoveAll(secondDir)
|
|
|
|
|
|
|
|
err = CopyFiles("../../../../test/data", path.Join(secondDir, "a"))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctlr.Config.Storage.RootDirectory = firstDir
|
|
|
|
subPaths := make(map[string]config.StorageConfig)
|
|
|
|
subPaths["/a"] = config.StorageConfig{
|
|
|
|
RootDirectory: secondDir,
|
|
|
|
}
|
|
|
|
|
|
|
|
ctlr.Config.Storage.SubPaths = subPaths
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
// this blocks
|
2022-03-24 07:49:51 -05:00
|
|
|
if err := ctlr.Run(context.Background()); err != nil {
|
2022-02-15 20:15:13 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// wait till ready
|
|
|
|
for {
|
|
|
|
_, err := resty.R().Get(baseURL)
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
}
|
|
|
|
|
2022-02-24 15:31:36 -05:00
|
|
|
resp, _ := resty.R().Options(baseURL + constants.RoutePrefix + constants.ExtCatalogPrefix)
|
2022-02-15 20:15:13 -05:00
|
|
|
So(resp, ShouldNotBeNil)
|
|
|
|
So(resp.StatusCode(), ShouldEqual, http.StatusNoContent)
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
ctx := context.Background()
|
|
|
|
_ = ctlr.Server.Shutdown(ctx)
|
|
|
|
}()
|
|
|
|
})
|
|
|
|
}
|
2022-09-28 13:39:54 -05:00
|
|
|
|
|
|
|
func TestCVEStruct(t *testing.T) {
|
|
|
|
Convey("Unit test the CVE struct", t, func() {
|
2023-01-09 15:37:44 -05:00
|
|
|
repoDB, err := bolt.NewBoltDBWrapper(bolt.DBParameters{
|
|
|
|
RootDir: t.TempDir(),
|
|
|
|
})
|
|
|
|
So(err, ShouldBeNil)
|
2022-09-28 13:39:54 -05:00
|
|
|
|
2023-01-09 15:37:44 -05:00
|
|
|
// Create repodb data for scannable image with vulnerabilities
|
|
|
|
timeStamp11 := time.Date(2008, 1, 1, 12, 0, 0, 0, time.UTC)
|
2022-09-28 13:39:54 -05:00
|
|
|
|
2023-01-09 15:37:44 -05:00
|
|
|
configBlob11, err := json.Marshal(ispec.Image{
|
|
|
|
Created: &timeStamp11,
|
|
|
|
})
|
|
|
|
So(err, ShouldBeNil)
|
2022-10-24 14:27:26 -05:00
|
|
|
|
2023-01-09 15:37:44 -05:00
|
|
|
manifestBlob11, err := json.Marshal(ispec.Manifest{
|
|
|
|
Layers: []ispec.Descriptor{
|
|
|
|
{
|
|
|
|
MediaType: ispec.MediaTypeImageLayerGzip,
|
|
|
|
Size: 0,
|
|
|
|
Digest: godigest.NewDigestFromEncoded(godigest.SHA256, "digest"),
|
|
|
|
},
|
2022-09-28 13:39:54 -05:00
|
|
|
},
|
2023-01-09 15:37:44 -05:00
|
|
|
Config: ispec.Descriptor{
|
|
|
|
Digest: godigest.FromBytes(configBlob11),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
So(err, ShouldBeNil)
|
2022-09-28 13:39:54 -05:00
|
|
|
|
2023-01-09 15:37:44 -05:00
|
|
|
repoMeta11 := repodb.ManifestMetadata{
|
|
|
|
ManifestBlob: manifestBlob11,
|
|
|
|
ConfigBlob: configBlob11,
|
|
|
|
DownloadCount: 0,
|
|
|
|
Signatures: repodb.ManifestSignatures{},
|
|
|
|
}
|
2022-09-28 13:39:54 -05:00
|
|
|
|
2023-01-09 15:37:44 -05:00
|
|
|
digest11 := godigest.FromBytes(manifestBlob11)
|
|
|
|
err = repoDB.SetManifestMeta("repo1", digest11, repoMeta11)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
err = repoDB.SetRepoTag("repo1", "0.1.0", digest11, ispec.MediaTypeImageManifest)
|
|
|
|
So(err, ShouldBeNil)
|
2022-10-24 14:27:26 -05:00
|
|
|
|
2023-01-09 15:37:44 -05:00
|
|
|
timeStamp12 := time.Date(2009, 1, 1, 12, 0, 0, 0, time.UTC)
|
|
|
|
|
|
|
|
configBlob12, err := json.Marshal(ispec.Image{
|
|
|
|
Created: &timeStamp12,
|
|
|
|
})
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
manifestBlob12, err := json.Marshal(ispec.Manifest{
|
|
|
|
Layers: []ispec.Descriptor{
|
|
|
|
{
|
|
|
|
MediaType: ispec.MediaTypeImageLayerGzip,
|
|
|
|
Size: 0,
|
|
|
|
Digest: godigest.NewDigestFromEncoded(godigest.SHA256, "digest"),
|
|
|
|
},
|
2022-09-28 13:39:54 -05:00
|
|
|
},
|
2023-01-09 15:37:44 -05:00
|
|
|
Config: ispec.Descriptor{
|
|
|
|
Digest: godigest.FromBytes(configBlob12),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
So(err, ShouldBeNil)
|
2022-09-28 13:39:54 -05:00
|
|
|
|
2023-01-09 15:37:44 -05:00
|
|
|
repoMeta12 := repodb.ManifestMetadata{
|
|
|
|
ManifestBlob: manifestBlob12,
|
|
|
|
ConfigBlob: configBlob12,
|
|
|
|
DownloadCount: 0,
|
|
|
|
Signatures: repodb.ManifestSignatures{},
|
|
|
|
}
|
2022-09-28 13:39:54 -05:00
|
|
|
|
2023-01-09 15:37:44 -05:00
|
|
|
digest12 := godigest.FromBytes(manifestBlob12)
|
|
|
|
err = repoDB.SetManifestMeta("repo1", digest12, repoMeta12)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
err = repoDB.SetRepoTag("repo1", "1.0.0", digest12, ispec.MediaTypeImageManifest)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
timeStamp13 := time.Date(2010, 1, 1, 12, 0, 0, 0, time.UTC)
|
|
|
|
|
|
|
|
configBlob13, err := json.Marshal(ispec.Image{
|
|
|
|
Created: &timeStamp13,
|
|
|
|
})
|
|
|
|
So(err, ShouldBeNil)
|
2022-10-24 14:27:26 -05:00
|
|
|
|
2023-01-09 15:37:44 -05:00
|
|
|
manifestBlob13, err := json.Marshal(ispec.Manifest{
|
|
|
|
Layers: []ispec.Descriptor{
|
|
|
|
{
|
|
|
|
MediaType: ispec.MediaTypeImageLayerGzip,
|
|
|
|
Size: 0,
|
|
|
|
Digest: godigest.NewDigestFromEncoded(godigest.SHA256, "digest"),
|
|
|
|
},
|
2022-09-28 13:39:54 -05:00
|
|
|
},
|
2023-01-09 15:37:44 -05:00
|
|
|
Config: ispec.Descriptor{
|
|
|
|
Digest: godigest.FromBytes(configBlob13),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
repoMeta13 := repodb.ManifestMetadata{
|
|
|
|
ManifestBlob: manifestBlob13,
|
|
|
|
ConfigBlob: configBlob13,
|
2022-09-28 13:39:54 -05:00
|
|
|
}
|
|
|
|
|
2023-01-09 15:37:44 -05:00
|
|
|
digest13 := godigest.FromBytes(manifestBlob13)
|
|
|
|
err = repoDB.SetManifestMeta("repo1", digest13, repoMeta13)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
err = repoDB.SetRepoTag("repo1", "1.1.0", digest13, ispec.MediaTypeImageManifest)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
timeStamp14 := time.Date(2011, 1, 1, 12, 0, 0, 0, time.UTC)
|
|
|
|
|
|
|
|
configBlob14, err := json.Marshal(ispec.Image{
|
|
|
|
Created: &timeStamp14,
|
|
|
|
})
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
manifestBlob14, err := json.Marshal(ispec.Manifest{
|
|
|
|
Layers: []ispec.Descriptor{
|
|
|
|
{
|
|
|
|
MediaType: ispec.MediaTypeImageLayerGzip,
|
|
|
|
Size: 0,
|
|
|
|
Digest: godigest.NewDigestFromEncoded(godigest.SHA256, "digest"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Config: ispec.Descriptor{
|
|
|
|
Digest: godigest.FromBytes(configBlob14),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
repoMeta14 := repodb.ManifestMetadata{
|
|
|
|
ManifestBlob: manifestBlob14,
|
|
|
|
ConfigBlob: configBlob14,
|
|
|
|
}
|
|
|
|
|
|
|
|
digest14 := godigest.FromBytes(manifestBlob14)
|
|
|
|
err = repoDB.SetManifestMeta("repo1", digest14, repoMeta14)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
err = repoDB.SetRepoTag("repo1", "1.0.1", digest14, ispec.MediaTypeImageManifest)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
// Create repodb data for scannable image with no vulnerabilities
|
|
|
|
timeStamp61 := time.Date(2011, 1, 1, 12, 0, 0, 0, time.UTC)
|
|
|
|
|
|
|
|
configBlob61, err := json.Marshal(ispec.Image{
|
|
|
|
Created: &timeStamp61,
|
|
|
|
})
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
manifestBlob61, err := json.Marshal(ispec.Manifest{
|
|
|
|
Layers: []ispec.Descriptor{
|
|
|
|
{
|
|
|
|
MediaType: ispec.MediaTypeImageLayerGzip,
|
|
|
|
Size: 0,
|
|
|
|
Digest: godigest.NewDigestFromEncoded(godigest.SHA256, "digest"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Config: ispec.Descriptor{
|
|
|
|
Digest: godigest.FromBytes(configBlob61),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
repoMeta61 := repodb.ManifestMetadata{
|
|
|
|
ManifestBlob: manifestBlob61,
|
|
|
|
ConfigBlob: configBlob61,
|
|
|
|
}
|
|
|
|
|
|
|
|
digest61 := godigest.FromBytes(manifestBlob61)
|
|
|
|
err = repoDB.SetManifestMeta("repo6", digest61, repoMeta61)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
err = repoDB.SetRepoTag("repo6", "1.0.0", digest61, ispec.MediaTypeImageManifest)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
// Create repodb data for image not supporting scanning
|
|
|
|
timeStamp21 := time.Date(2009, 1, 1, 12, 0, 0, 0, time.UTC)
|
|
|
|
|
|
|
|
configBlob21, err := json.Marshal(ispec.Image{
|
|
|
|
Created: &timeStamp21,
|
|
|
|
})
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
manifestBlob21, err := json.Marshal(ispec.Manifest{
|
|
|
|
Layers: []ispec.Descriptor{
|
|
|
|
{
|
|
|
|
MediaType: ispec.MediaTypeImageLayerNonDistributableGzip,
|
|
|
|
Size: 0,
|
|
|
|
Digest: godigest.NewDigestFromEncoded(godigest.SHA256, "digest"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Config: ispec.Descriptor{
|
|
|
|
Digest: godigest.FromBytes(configBlob21),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
repoMeta21 := repodb.ManifestMetadata{
|
|
|
|
ManifestBlob: manifestBlob21,
|
|
|
|
ConfigBlob: configBlob21,
|
|
|
|
}
|
|
|
|
|
|
|
|
digest21 := godigest.FromBytes(manifestBlob21)
|
|
|
|
err = repoDB.SetManifestMeta("repo2", digest21, repoMeta21)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
err = repoDB.SetRepoTag("repo2", "1.0.0", digest21, ispec.MediaTypeImageManifest)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
// Create repodb data for invalid images/negative tests
|
|
|
|
manifestBlob31 := []byte("invalid manifest blob")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
repoMeta31 := repodb.ManifestMetadata{
|
|
|
|
ManifestBlob: manifestBlob31,
|
|
|
|
}
|
|
|
|
|
|
|
|
digest31 := godigest.FromBytes(manifestBlob31)
|
|
|
|
err = repoDB.SetManifestMeta("repo3", digest31, repoMeta31)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
err = repoDB.SetRepoTag("repo3", "invalid-manifest", digest31, ispec.MediaTypeImageManifest)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
configBlob41 := []byte("invalid config blob")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
repoMeta41 := repodb.ManifestMetadata{
|
|
|
|
ConfigBlob: configBlob41,
|
|
|
|
}
|
|
|
|
|
|
|
|
digest41 := godigest.FromString("abc7")
|
|
|
|
err = repoDB.SetManifestMeta("repo4", digest41, repoMeta41)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
err = repoDB.SetRepoTag("repo4", "invalid-config", digest41, ispec.MediaTypeImageManifest)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
digest51 := godigest.FromString("abc8")
|
|
|
|
err = repoDB.SetRepoTag("repo5", "nonexitent-manifest", digest51, ispec.MediaTypeImageManifest)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
// RepoDB loaded with initial data, mock the scanner
|
2022-09-28 13:39:54 -05:00
|
|
|
severities := map[string]int{
|
|
|
|
"UNKNOWN": 0,
|
|
|
|
"LOW": 1,
|
|
|
|
"MEDIUM": 2,
|
|
|
|
"HIGH": 3,
|
|
|
|
"CRITICAL": 4,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup test CVE data in mock scanner
|
|
|
|
scanner := mocks.CveScannerMock{
|
|
|
|
ScanImageFn: func(image string) (map[string]cvemodel.CVE, error) {
|
|
|
|
// Images in chronological order
|
|
|
|
if image == "repo1:0.1.0" {
|
|
|
|
return map[string]cvemodel.CVE{
|
|
|
|
"CVE1": {
|
|
|
|
ID: "CVE1",
|
|
|
|
Severity: "MEDIUM",
|
|
|
|
Title: "Title CVE1",
|
|
|
|
Description: "Description CVE1",
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if image == "repo1:1.0.0" {
|
|
|
|
return map[string]cvemodel.CVE{
|
|
|
|
"CVE1": {
|
|
|
|
ID: "CVE1",
|
|
|
|
Severity: "MEDIUM",
|
|
|
|
Title: "Title CVE1",
|
|
|
|
Description: "Description CVE1",
|
|
|
|
},
|
|
|
|
"CVE2": {
|
|
|
|
ID: "CVE2",
|
|
|
|
Severity: "HIGH",
|
|
|
|
Title: "Title CVE2",
|
|
|
|
Description: "Description CVE2",
|
|
|
|
},
|
|
|
|
"CVE3": {
|
|
|
|
ID: "CVE3",
|
|
|
|
Severity: "LOW",
|
|
|
|
Title: "Title CVE3",
|
|
|
|
Description: "Description CVE3",
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if image == "repo1:1.1.0" {
|
|
|
|
return map[string]cvemodel.CVE{
|
|
|
|
"CVE3": {
|
|
|
|
ID: "CVE3",
|
|
|
|
Severity: "LOW",
|
|
|
|
Title: "Title CVE3",
|
|
|
|
Description: "Description CVE3",
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// As a minor release on 1.0.0 banch
|
|
|
|
// does not include all fixes published in 1.1.0
|
|
|
|
if image == "repo1:1.0.1" {
|
|
|
|
return map[string]cvemodel.CVE{
|
|
|
|
"CVE1": {
|
|
|
|
ID: "CVE1",
|
|
|
|
Severity: "MEDIUM",
|
|
|
|
Title: "Title CVE1",
|
|
|
|
Description: "Description CVE1",
|
|
|
|
},
|
|
|
|
"CVE3": {
|
|
|
|
ID: "CVE3",
|
|
|
|
Severity: "LOW",
|
|
|
|
Title: "Title CVE3",
|
|
|
|
Description: "Description CVE3",
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// By default the image has no vulnerabilities
|
|
|
|
return map[string]cvemodel.CVE{}, nil
|
|
|
|
},
|
|
|
|
CompareSeveritiesFn: func(severity1, severity2 string) int {
|
|
|
|
return severities[severity2] - severities[severity1]
|
|
|
|
},
|
|
|
|
IsImageFormatScannableFn: func(image string) (bool, error) {
|
|
|
|
// Almost same logic compared to actual Trivy specific implementation
|
|
|
|
imageDir, inputTag := common.GetImageDirAndTag(image)
|
|
|
|
|
2023-01-09 15:37:44 -05:00
|
|
|
repoMeta, err := repoDB.GetRepoMeta(imageDir)
|
2022-09-28 13:39:54 -05:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2023-01-09 15:37:44 -05:00
|
|
|
manifestDigestStr, ok := repoMeta.Tags[inputTag]
|
|
|
|
if !ok {
|
|
|
|
return false, zerr.ErrTagMetaNotFound
|
|
|
|
}
|
2022-09-28 13:39:54 -05:00
|
|
|
|
2023-01-09 15:37:44 -05:00
|
|
|
manifestDigest, err := godigest.Parse(manifestDigestStr.Digest)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2022-09-28 13:39:54 -05:00
|
|
|
|
2023-01-09 15:37:44 -05:00
|
|
|
manifestData, err := repoDB.GetManifestData(manifestDigest)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var manifestContent ispec.Manifest
|
|
|
|
|
|
|
|
err = json.Unmarshal(manifestData.ManifestBlob, &manifestContent)
|
|
|
|
if err != nil {
|
|
|
|
return false, zerr.ErrScanNotSupported
|
|
|
|
}
|
2022-09-28 13:39:54 -05:00
|
|
|
|
2023-01-09 15:37:44 -05:00
|
|
|
for _, imageLayer := range manifestContent.Layers {
|
|
|
|
switch imageLayer.MediaType {
|
|
|
|
case ispec.MediaTypeImageLayerGzip, ispec.MediaTypeImageLayer, string(regTypes.DockerLayer):
|
2022-09-28 13:39:54 -05:00
|
|
|
|
2023-01-09 15:37:44 -05:00
|
|
|
return true, nil
|
|
|
|
default:
|
2022-09-28 13:39:54 -05:00
|
|
|
|
2023-01-09 15:37:44 -05:00
|
|
|
return false, zerr.ErrScanNotSupported
|
2022-09-28 13:39:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
log := log.NewLogger("debug", "")
|
2023-01-09 15:37:44 -05:00
|
|
|
cveInfo := cveinfo.BaseCveInfo{Log: log, Scanner: scanner, RepoDB: repoDB}
|
2022-09-28 13:39:54 -05:00
|
|
|
|
2023-01-09 15:37:44 -05:00
|
|
|
t.Log("Test GetCVESummaryForImage")
|
2022-09-28 13:39:54 -05:00
|
|
|
|
2023-01-09 15:37:44 -05:00
|
|
|
// Image is found
|
|
|
|
cveSummary, err := cveInfo.GetCVESummaryForImage("repo1:0.1.0")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(cveSummary.Count, ShouldEqual, 1)
|
|
|
|
So(cveSummary.MaxSeverity, ShouldEqual, "MEDIUM")
|
2022-09-28 13:39:54 -05:00
|
|
|
|
2023-01-09 15:37:44 -05:00
|
|
|
cveSummary, err = cveInfo.GetCVESummaryForImage("repo1:1.0.0")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(cveSummary.Count, ShouldEqual, 3)
|
|
|
|
So(cveSummary.MaxSeverity, ShouldEqual, "HIGH")
|
2022-09-28 13:39:54 -05:00
|
|
|
|
2023-01-09 15:37:44 -05:00
|
|
|
cveSummary, err = cveInfo.GetCVESummaryForImage("repo1:1.0.1")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(cveSummary.Count, ShouldEqual, 2)
|
|
|
|
So(cveSummary.MaxSeverity, ShouldEqual, "MEDIUM")
|
2022-09-28 13:39:54 -05:00
|
|
|
|
2023-01-09 15:37:44 -05:00
|
|
|
cveSummary, err = cveInfo.GetCVESummaryForImage("repo1:1.1.0")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(cveSummary.Count, ShouldEqual, 1)
|
|
|
|
So(cveSummary.MaxSeverity, ShouldEqual, "LOW")
|
2022-09-28 13:39:54 -05:00
|
|
|
|
2023-01-09 15:37:44 -05:00
|
|
|
cveSummary, err = cveInfo.GetCVESummaryForImage("repo6:1.0.0")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(cveSummary.Count, ShouldEqual, 0)
|
|
|
|
So(cveSummary.MaxSeverity, ShouldEqual, "NONE")
|
|
|
|
|
|
|
|
// Image is not scannable
|
|
|
|
cveSummary, err = cveInfo.GetCVESummaryForImage("repo2:1.0.0")
|
|
|
|
So(err, ShouldEqual, zerr.ErrScanNotSupported)
|
|
|
|
So(cveSummary.Count, ShouldEqual, 0)
|
|
|
|
So(cveSummary.MaxSeverity, ShouldEqual, "")
|
|
|
|
|
|
|
|
// Tag is not found
|
|
|
|
cveSummary, err = cveInfo.GetCVESummaryForImage("repo3:1.0.0")
|
|
|
|
So(err, ShouldEqual, zerr.ErrTagMetaNotFound)
|
|
|
|
So(cveSummary.Count, ShouldEqual, 0)
|
|
|
|
So(cveSummary.MaxSeverity, ShouldEqual, "")
|
|
|
|
|
|
|
|
// Manifest is not found
|
|
|
|
cveSummary, err = cveInfo.GetCVESummaryForImage("repo5:nonexitent-manifest")
|
|
|
|
So(err, ShouldEqual, zerr.ErrManifestDataNotFound)
|
|
|
|
So(cveSummary.Count, ShouldEqual, 0)
|
|
|
|
So(cveSummary.MaxSeverity, ShouldEqual, "")
|
|
|
|
|
|
|
|
// Repo is not found
|
|
|
|
cveSummary, err = cveInfo.GetCVESummaryForImage("repo100:1.0.0")
|
|
|
|
So(err, ShouldEqual, zerr.ErrRepoMetaNotFound)
|
|
|
|
So(cveSummary.Count, ShouldEqual, 0)
|
|
|
|
So(cveSummary.MaxSeverity, ShouldEqual, "")
|
|
|
|
|
|
|
|
t.Log("Test GetCVEListForImage")
|
|
|
|
|
|
|
|
// Image is found
|
|
|
|
cveMap, err := cveInfo.GetCVEListForImage("repo1:0.1.0")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(len(cveMap), ShouldEqual, 1)
|
|
|
|
So(cveMap, ShouldContainKey, "CVE1")
|
|
|
|
So(cveMap, ShouldNotContainKey, "CVE2")
|
|
|
|
So(cveMap, ShouldNotContainKey, "CVE3")
|
2022-09-28 13:39:54 -05:00
|
|
|
|
2023-01-09 15:37:44 -05:00
|
|
|
cveMap, err = cveInfo.GetCVEListForImage("repo1:1.0.0")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(len(cveMap), ShouldEqual, 3)
|
|
|
|
So(cveMap, ShouldContainKey, "CVE1")
|
|
|
|
So(cveMap, ShouldContainKey, "CVE2")
|
|
|
|
So(cveMap, ShouldContainKey, "CVE3")
|
2022-09-28 13:39:54 -05:00
|
|
|
|
2023-01-09 15:37:44 -05:00
|
|
|
cveMap, err = cveInfo.GetCVEListForImage("repo1:1.0.1")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(len(cveMap), ShouldEqual, 2)
|
|
|
|
So(cveMap, ShouldContainKey, "CVE1")
|
|
|
|
So(cveMap, ShouldNotContainKey, "CVE2")
|
|
|
|
So(cveMap, ShouldContainKey, "CVE3")
|
2022-09-28 13:39:54 -05:00
|
|
|
|
2023-01-09 15:37:44 -05:00
|
|
|
cveMap, err = cveInfo.GetCVEListForImage("repo1:1.1.0")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(len(cveMap), ShouldEqual, 1)
|
|
|
|
So(cveMap, ShouldNotContainKey, "CVE1")
|
|
|
|
So(cveMap, ShouldNotContainKey, "CVE2")
|
|
|
|
So(cveMap, ShouldContainKey, "CVE3")
|
2022-09-28 13:39:54 -05:00
|
|
|
|
2023-01-09 15:37:44 -05:00
|
|
|
cveMap, err = cveInfo.GetCVEListForImage("repo6:1.0.0")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(len(cveMap), ShouldEqual, 0)
|
2022-09-28 13:39:54 -05:00
|
|
|
|
2023-01-09 15:37:44 -05:00
|
|
|
// Image is not scannable
|
|
|
|
cveMap, err = cveInfo.GetCVEListForImage("repo2:1.0.0")
|
|
|
|
So(err, ShouldEqual, zerr.ErrScanNotSupported)
|
|
|
|
So(len(cveMap), ShouldEqual, 0)
|
2022-09-28 13:39:54 -05:00
|
|
|
|
2023-01-09 15:37:44 -05:00
|
|
|
// Tag is not found
|
|
|
|
cveMap, err = cveInfo.GetCVEListForImage("repo3:1.0.0")
|
|
|
|
So(err, ShouldEqual, zerr.ErrTagMetaNotFound)
|
|
|
|
So(len(cveMap), ShouldEqual, 0)
|
2022-09-28 13:39:54 -05:00
|
|
|
|
2023-01-09 15:37:44 -05:00
|
|
|
// Manifest is not found
|
|
|
|
cveMap, err = cveInfo.GetCVEListForImage("repo5:nonexitent-manifest")
|
|
|
|
So(err, ShouldEqual, zerr.ErrManifestDataNotFound)
|
|
|
|
So(len(cveMap), ShouldEqual, 0)
|
2022-09-28 13:39:54 -05:00
|
|
|
|
2023-01-09 15:37:44 -05:00
|
|
|
// Repo is not found
|
|
|
|
cveMap, err = cveInfo.GetCVEListForImage("repo100:1.0.0")
|
|
|
|
So(err, ShouldEqual, zerr.ErrRepoMetaNotFound)
|
|
|
|
So(len(cveMap), ShouldEqual, 0)
|
|
|
|
|
|
|
|
t.Log("Test GetImageListWithCVEFixed")
|
|
|
|
|
|
|
|
// Image is found
|
|
|
|
tagList, err := cveInfo.GetImageListWithCVEFixed("repo1", "CVE1")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(len(tagList), ShouldEqual, 1)
|
|
|
|
So(tagList[0].Name, ShouldEqual, "1.1.0")
|
|
|
|
|
|
|
|
tagList, err = cveInfo.GetImageListWithCVEFixed("repo1", "CVE2")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(len(tagList), ShouldEqual, 2)
|
|
|
|
expectedTags := []string{"1.0.1", "1.1.0"}
|
|
|
|
So(expectedTags, ShouldContain, tagList[0].Name)
|
|
|
|
So(expectedTags, ShouldContain, tagList[1].Name)
|
|
|
|
|
|
|
|
tagList, err = cveInfo.GetImageListWithCVEFixed("repo1", "CVE3")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
// CVE3 is not present in 0.1.0, but that is older than all other
|
|
|
|
// images where it is present. The rest of the images explicitly have it.
|
|
|
|
// This means we consider it not fixed in any image.
|
|
|
|
So(len(tagList), ShouldEqual, 0)
|
|
|
|
|
|
|
|
// Image doesn't have any CVEs in the first place
|
|
|
|
tagList, err = cveInfo.GetImageListWithCVEFixed("repo6", "CVE1")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(len(tagList), ShouldEqual, 1)
|
|
|
|
So(tagList[0].Name, ShouldEqual, "1.0.0")
|
|
|
|
|
|
|
|
// Image is not scannable
|
|
|
|
tagList, err = cveInfo.GetImageListWithCVEFixed("repo2", "CVE100")
|
|
|
|
// CVE is not considered fixed as scan is not possible
|
|
|
|
// but do not return an error
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(len(tagList), ShouldEqual, 0)
|
|
|
|
|
|
|
|
// Tag is not found, but we should not error
|
|
|
|
tagList, err = cveInfo.GetImageListWithCVEFixed("repo3", "CVE101")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(len(tagList), ShouldEqual, 0)
|
|
|
|
|
|
|
|
// Manifest is not found, we just consider exclude it from the fixed list
|
|
|
|
tagList, err = cveInfo.GetImageListWithCVEFixed("repo5", "CVE101")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(len(tagList), ShouldEqual, 0)
|
|
|
|
|
|
|
|
// Repo is not found, there could potentially be unaffected tags in the repo
|
|
|
|
// but we can't access their data
|
|
|
|
tagList, err = cveInfo.GetImageListWithCVEFixed("repo100", "CVE100")
|
|
|
|
So(err, ShouldEqual, zerr.ErrRepoMetaNotFound)
|
|
|
|
So(len(tagList), ShouldEqual, 0)
|
|
|
|
|
|
|
|
t.Log("Test GetImageListForCVE")
|
|
|
|
|
|
|
|
// Image is found
|
|
|
|
imageInfoByCveList, err := cveInfo.GetImageListForCVE("repo1", "CVE1")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(len(imageInfoByCveList), ShouldEqual, 3)
|
|
|
|
expectedTags = []string{"0.1.0", "1.0.0", "1.0.1"}
|
|
|
|
So(expectedTags, ShouldContain, imageInfoByCveList[0].Tag)
|
|
|
|
So(expectedTags, ShouldContain, imageInfoByCveList[1].Tag)
|
|
|
|
So(expectedTags, ShouldContain, imageInfoByCveList[2].Tag)
|
|
|
|
|
|
|
|
imageInfoByCveList, err = cveInfo.GetImageListForCVE("repo1", "CVE2")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(len(imageInfoByCveList), ShouldEqual, 1)
|
|
|
|
So(imageInfoByCveList[0].Tag, ShouldEqual, "1.0.0")
|
|
|
|
|
|
|
|
imageInfoByCveList, err = cveInfo.GetImageListForCVE("repo1", "CVE3")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(len(imageInfoByCveList), ShouldEqual, 3)
|
|
|
|
expectedTags = []string{"1.0.0", "1.0.1", "1.1.0"}
|
|
|
|
So(expectedTags, ShouldContain, imageInfoByCveList[0].Tag)
|
|
|
|
So(expectedTags, ShouldContain, imageInfoByCveList[1].Tag)
|
|
|
|
So(expectedTags, ShouldContain, imageInfoByCveList[2].Tag)
|
|
|
|
|
|
|
|
// Image/repo doesn't have the CVE at all
|
|
|
|
imageInfoByCveList, err = cveInfo.GetImageListForCVE("repo6", "CVE1")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(len(imageInfoByCveList), ShouldEqual, 0)
|
|
|
|
|
|
|
|
// Image is not scannable
|
|
|
|
imageInfoByCveList, err = cveInfo.GetImageListForCVE("repo2", "CVE100")
|
|
|
|
// Image is not considered affected with CVE as scan is not possible
|
|
|
|
// but do not return an error
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(len(imageInfoByCveList), ShouldEqual, 0)
|
|
|
|
|
|
|
|
// Tag is not found, but we should not error
|
|
|
|
imageInfoByCveList, err = cveInfo.GetImageListForCVE("repo3", "CVE101")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(len(imageInfoByCveList), ShouldEqual, 0)
|
|
|
|
|
|
|
|
// Manifest is not found, assume it is affetected by the CVE
|
|
|
|
// But we don't have enough of it's data to actually return it
|
|
|
|
imageInfoByCveList, err = cveInfo.GetImageListForCVE("repo5", "CVE101")
|
|
|
|
So(err, ShouldEqual, zerr.ErrManifestMetaNotFound)
|
|
|
|
So(len(imageInfoByCveList), ShouldEqual, 0)
|
|
|
|
|
|
|
|
// Repo is not found, assume it is affetected by the CVE
|
|
|
|
// But we don't have enough of it's data to actually return it
|
|
|
|
imageInfoByCveList, err = cveInfo.GetImageListForCVE("repo100", "CVE100")
|
|
|
|
So(err, ShouldEqual, zerr.ErrRepoMetaNotFound)
|
|
|
|
So(len(imageInfoByCveList), ShouldEqual, 0)
|
|
|
|
|
|
|
|
t.Log("Test errors while scanning")
|
|
|
|
|
|
|
|
faultyScanner := mocks.CveScannerMock{
|
|
|
|
ScanImageFn: func(image string) (map[string]cvemodel.CVE, error) {
|
|
|
|
// Could be any type of error, let's reuse this one
|
|
|
|
return nil, zerr.ErrScanNotSupported
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
cveInfo = cveinfo.BaseCveInfo{Log: log, Scanner: faultyScanner, RepoDB: repoDB}
|
|
|
|
|
|
|
|
cveSummary, err = cveInfo.GetCVESummaryForImage("repo1:0.1.0")
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
So(cveSummary.Count, ShouldEqual, 0)
|
|
|
|
So(cveSummary.MaxSeverity, ShouldEqual, "")
|
|
|
|
|
|
|
|
cveMap, err = cveInfo.GetCVEListForImage("repo1:0.1.0")
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
So(cveMap, ShouldBeNil)
|
|
|
|
|
|
|
|
tagList, err = cveInfo.GetImageListWithCVEFixed("repo1", "CVE1")
|
|
|
|
// CVE is not considered fixed as scan is not possible
|
|
|
|
// but do not return an error
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(len(tagList), ShouldEqual, 0)
|
|
|
|
|
|
|
|
imageInfoByCveList, err = cveInfo.GetImageListForCVE("repo1", "CVE1")
|
|
|
|
// Image is not considered affected with CVE as scan is not possible
|
|
|
|
// but do not return an error
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(len(imageInfoByCveList), ShouldEqual, 0)
|
2022-09-28 13:39:54 -05:00
|
|
|
})
|
|
|
|
}
|