2021-12-03 22:50:58 -05:00
|
|
|
//go:build extended
|
2020-10-14 16:47:20 -05:00
|
|
|
// +build extended
|
|
|
|
|
2020-06-16 20:52:40 -05:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-07-06 17:44:32 -05:00
|
|
|
"errors"
|
2020-06-16 20:52:40 -05:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2020-07-06 17:44:32 -05:00
|
|
|
"time"
|
2020-06-16 20:52:40 -05:00
|
|
|
|
|
|
|
"github.com/dustin/go-humanize"
|
|
|
|
jsoniter "github.com/json-iterator/go"
|
|
|
|
"github.com/olekukonko/tablewriter"
|
|
|
|
"gopkg.in/yaml.v2"
|
2021-12-03 22:50:58 -05:00
|
|
|
zotErrors "zotregistry.io/zot/errors"
|
2022-02-24 15:31:36 -05:00
|
|
|
"zotregistry.io/zot/pkg/api/constants"
|
2020-06-16 20:52:40 -05:00
|
|
|
)
|
|
|
|
|
2020-07-06 17:44:32 -05:00
|
|
|
type SearchService interface {
|
2020-07-17 14:42:22 -05:00
|
|
|
getAllImages(ctx context.Context, config searchConfig, username, password string,
|
2021-12-13 14:23:31 -05:00
|
|
|
channel chan stringResult, wtgrp *sync.WaitGroup)
|
2020-07-17 14:42:22 -05:00
|
|
|
getImageByName(ctx context.Context, config searchConfig, username, password, imageName string,
|
2021-12-13 14:23:31 -05:00
|
|
|
channel chan stringResult, wtgrp *sync.WaitGroup)
|
2020-07-06 17:44:32 -05:00
|
|
|
getCveByImage(ctx context.Context, config searchConfig, username, password, imageName string,
|
2021-12-13 14:23:31 -05:00
|
|
|
channel chan stringResult, wtgrp *sync.WaitGroup)
|
|
|
|
getImagesByCveID(ctx context.Context, config searchConfig, username, password, cvid string,
|
|
|
|
channel chan stringResult, wtgrp *sync.WaitGroup)
|
2021-05-26 12:22:31 -05:00
|
|
|
getImagesByDigest(ctx context.Context, config searchConfig, username, password, digest string,
|
2021-12-13 14:23:31 -05:00
|
|
|
channel chan stringResult, wtgrp *sync.WaitGroup)
|
|
|
|
getImageByNameAndCVEID(ctx context.Context, config searchConfig, username, password, imageName, cvid string,
|
|
|
|
channel chan stringResult, wtgrp *sync.WaitGroup)
|
|
|
|
getFixedTagsForCVE(ctx context.Context, config searchConfig, username, password, imageName, cvid string,
|
|
|
|
channel chan stringResult, wtgrp *sync.WaitGroup)
|
2020-06-16 20:52:40 -05:00
|
|
|
}
|
2020-07-06 17:44:32 -05:00
|
|
|
|
2020-06-16 20:52:40 -05:00
|
|
|
type searchService struct{}
|
|
|
|
|
2020-07-06 17:44:32 -05:00
|
|
|
func NewSearchService() SearchService {
|
2020-06-16 20:52:40 -05:00
|
|
|
return searchService{}
|
|
|
|
}
|
|
|
|
|
2020-07-17 14:42:22 -05:00
|
|
|
func (service searchService) getImageByName(ctx context.Context, config searchConfig,
|
2022-03-21 12:37:23 -05:00
|
|
|
username, password, imageName string, rch chan stringResult, wtgrp *sync.WaitGroup,
|
|
|
|
) {
|
2021-12-13 14:23:31 -05:00
|
|
|
defer wtgrp.Done()
|
|
|
|
defer close(rch)
|
2020-06-16 20:52:40 -05:00
|
|
|
|
2020-07-17 14:42:22 -05:00
|
|
|
var localWg sync.WaitGroup
|
2022-03-21 12:37:23 -05:00
|
|
|
rlim := newSmoothRateLimiter(&localWg, rch)
|
2020-06-16 20:52:40 -05:00
|
|
|
|
2020-07-17 14:42:22 -05:00
|
|
|
localWg.Add(1)
|
2020-06-16 20:52:40 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
go rlim.startRateLimiter(ctx)
|
2020-07-17 14:42:22 -05:00
|
|
|
localWg.Add(1)
|
2020-06-16 20:52:40 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
go getImage(ctx, config, username, password, imageName, rch, &localWg, rlim)
|
2020-07-17 14:42:22 -05:00
|
|
|
|
|
|
|
localWg.Wait()
|
2020-06-16 20:52:40 -05:00
|
|
|
}
|
|
|
|
|
2020-07-17 14:42:22 -05:00
|
|
|
func (service searchService) getAllImages(ctx context.Context, config searchConfig, username, password string,
|
2022-03-21 12:37:23 -05:00
|
|
|
rch chan stringResult, wtgrp *sync.WaitGroup,
|
|
|
|
) {
|
2021-12-13 14:23:31 -05:00
|
|
|
defer wtgrp.Done()
|
|
|
|
defer close(rch)
|
2020-06-16 20:52:40 -05:00
|
|
|
|
|
|
|
catalog := &catalogResponse{}
|
|
|
|
|
2022-02-24 15:31:36 -05:00
|
|
|
catalogEndPoint, err := combineServerAndEndpointURL(*config.servURL, fmt.Sprintf("%s%s",
|
|
|
|
constants.RoutePrefix, constants.ExtCatalogPrefix))
|
2020-06-16 20:52:40 -05:00
|
|
|
if err != nil {
|
|
|
|
if isContextDone(ctx) {
|
|
|
|
return
|
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
rch <- stringResult{"", err}
|
2020-06-16 20:52:40 -05:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
_, err = makeGETRequest(ctx, catalogEndPoint, username, password, *config.verifyTLS, catalog)
|
2020-06-16 20:52:40 -05:00
|
|
|
if err != nil {
|
|
|
|
if isContextDone(ctx) {
|
|
|
|
return
|
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
rch <- stringResult{"", err}
|
2020-06-16 20:52:40 -05:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-17 14:42:22 -05:00
|
|
|
var localWg sync.WaitGroup
|
2020-06-16 20:52:40 -05:00
|
|
|
|
2022-03-21 12:37:23 -05:00
|
|
|
rlim := newSmoothRateLimiter(&localWg, rch)
|
2020-07-17 14:42:22 -05:00
|
|
|
|
|
|
|
localWg.Add(1)
|
2020-06-16 20:52:40 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
go rlim.startRateLimiter(ctx)
|
2020-06-16 20:52:40 -05:00
|
|
|
|
|
|
|
for _, repo := range catalog.Repositories {
|
2020-07-17 14:42:22 -05:00
|
|
|
localWg.Add(1)
|
2020-06-16 20:52:40 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
go getImage(ctx, config, username, password, repo, rch, &localWg, rlim)
|
2020-06-16 20:52:40 -05:00
|
|
|
}
|
2020-07-17 14:42:22 -05:00
|
|
|
|
|
|
|
localWg.Wait()
|
2020-06-16 20:52:40 -05:00
|
|
|
}
|
2020-07-17 14:42:22 -05:00
|
|
|
|
|
|
|
func getImage(ctx context.Context, config searchConfig, username, password, imageName string,
|
2022-03-21 12:37:23 -05:00
|
|
|
rch chan stringResult, wtgrp *sync.WaitGroup, pool *requestsPool,
|
|
|
|
) {
|
2021-12-13 14:23:31 -05:00
|
|
|
defer wtgrp.Done()
|
2020-06-16 20:52:40 -05:00
|
|
|
|
2020-07-17 14:42:22 -05:00
|
|
|
tagListEndpoint, err := combineServerAndEndpointURL(*config.servURL, fmt.Sprintf("/v2/%s/tags/list", imageName))
|
2020-06-16 20:52:40 -05:00
|
|
|
if err != nil {
|
|
|
|
if isContextDone(ctx) {
|
|
|
|
return
|
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
rch <- stringResult{"", err}
|
2020-06-16 20:52:40 -05:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
tagsList := &tagListResp{}
|
2021-12-13 14:23:31 -05:00
|
|
|
_, err = makeGETRequest(ctx, tagListEndpoint, username, password, *config.verifyTLS, &tagsList)
|
2020-06-16 20:52:40 -05:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if isContextDone(ctx) {
|
|
|
|
return
|
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
rch <- stringResult{"", err}
|
2020-06-16 20:52:40 -05:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tag := range tagsList.Tags {
|
2021-12-13 14:23:31 -05:00
|
|
|
wtgrp.Add(1)
|
2020-06-16 20:52:40 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
go addManifestCallToPool(ctx, config, pool, username, password, imageName, tag, rch, wtgrp)
|
2020-06-16 20:52:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-06 17:44:32 -05:00
|
|
|
func (service searchService) getImagesByCveID(ctx context.Context, config searchConfig, username,
|
2022-03-21 12:37:23 -05:00
|
|
|
password, cvid string, rch chan stringResult, wtgrp *sync.WaitGroup,
|
|
|
|
) {
|
2021-12-13 14:23:31 -05:00
|
|
|
defer wtgrp.Done()
|
|
|
|
defer close(rch)
|
2020-07-06 17:44:32 -05:00
|
|
|
|
|
|
|
query := fmt.Sprintf(`{ImageListForCVE(id: "%s") {`+`
|
|
|
|
Name Tags }
|
|
|
|
}`,
|
2021-12-13 14:23:31 -05:00
|
|
|
cvid)
|
2020-07-06 17:44:32 -05:00
|
|
|
result := &imagesForCve{}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
err := service.makeGraphQLQuery(ctx, config, username, password, query, result)
|
2020-07-06 17:44:32 -05:00
|
|
|
if err != nil {
|
|
|
|
if isContextDone(ctx) {
|
|
|
|
return
|
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
rch <- stringResult{"", err}
|
2020-07-06 17:44:32 -05:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-26 12:22:31 -05:00
|
|
|
if result.Errors != nil || err != nil {
|
|
|
|
var errBuilder strings.Builder
|
|
|
|
|
|
|
|
for _, err := range result.Errors {
|
|
|
|
fmt.Fprintln(&errBuilder, err.Message)
|
|
|
|
}
|
|
|
|
|
|
|
|
if isContextDone(ctx) {
|
|
|
|
return
|
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
rch <- stringResult{"", errors.New(errBuilder.String())} //nolint: goerr113
|
2021-05-26 12:22:31 -05:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var localWg sync.WaitGroup
|
|
|
|
|
2022-03-21 12:37:23 -05:00
|
|
|
rlim := newSmoothRateLimiter(&localWg, rch)
|
2021-05-26 12:22:31 -05:00
|
|
|
localWg.Add(1)
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
go rlim.startRateLimiter(ctx)
|
2021-05-26 12:22:31 -05:00
|
|
|
|
|
|
|
for _, image := range result.Data.ImageListForCVE {
|
|
|
|
for _, tag := range image.Tags {
|
|
|
|
localWg.Add(1)
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
go addManifestCallToPool(ctx, config, rlim, username, password, image.Name, tag, rch, &localWg)
|
2021-05-26 12:22:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
localWg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (service searchService) getImagesByDigest(ctx context.Context, config searchConfig, username,
|
2022-03-21 12:37:23 -05:00
|
|
|
password string, digest string, rch chan stringResult, wtgrp *sync.WaitGroup,
|
|
|
|
) {
|
2021-12-13 14:23:31 -05:00
|
|
|
defer wtgrp.Done()
|
|
|
|
defer close(rch)
|
2021-05-26 12:22:31 -05:00
|
|
|
|
|
|
|
query := fmt.Sprintf(`{ImageListForDigest(id: "%s") {`+`
|
|
|
|
Name Tags }
|
|
|
|
}`,
|
|
|
|
digest)
|
|
|
|
result := &imagesForDigest{}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
err := service.makeGraphQLQuery(ctx, config, username, password, query, result)
|
2020-07-06 17:44:32 -05:00
|
|
|
if err != nil {
|
|
|
|
if isContextDone(ctx) {
|
|
|
|
return
|
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
rch <- stringResult{"", err}
|
2020-07-06 17:44:32 -05:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if result.Errors != nil {
|
|
|
|
var errBuilder strings.Builder
|
|
|
|
|
|
|
|
for _, err := range result.Errors {
|
|
|
|
fmt.Fprintln(&errBuilder, err.Message)
|
|
|
|
}
|
|
|
|
|
|
|
|
if isContextDone(ctx) {
|
|
|
|
return
|
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
rch <- stringResult{"", errors.New(errBuilder.String())} //nolint: goerr113
|
2020-07-06 17:44:32 -05:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var localWg sync.WaitGroup
|
|
|
|
|
2022-03-21 12:37:23 -05:00
|
|
|
rlim := newSmoothRateLimiter(&localWg, rch)
|
2020-07-06 17:44:32 -05:00
|
|
|
localWg.Add(1)
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
go rlim.startRateLimiter(ctx)
|
2020-07-06 17:44:32 -05:00
|
|
|
|
2021-05-26 12:22:31 -05:00
|
|
|
for _, image := range result.Data.ImageListForDigest {
|
2020-07-06 17:44:32 -05:00
|
|
|
for _, tag := range image.Tags {
|
|
|
|
localWg.Add(1)
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
go addManifestCallToPool(ctx, config, rlim, username, password, image.Name, tag, rch, &localWg)
|
2020-07-06 17:44:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
localWg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (service searchService) getImageByNameAndCVEID(ctx context.Context, config searchConfig, username,
|
2022-03-21 12:37:23 -05:00
|
|
|
password, imageName, cvid string, rch chan stringResult, wtgrp *sync.WaitGroup,
|
|
|
|
) {
|
2021-12-13 14:23:31 -05:00
|
|
|
defer wtgrp.Done()
|
|
|
|
defer close(rch)
|
2020-07-06 17:44:32 -05:00
|
|
|
|
|
|
|
query := fmt.Sprintf(`{ImageListForCVE(id: "%s") {`+`
|
|
|
|
Name Tags }
|
|
|
|
}`,
|
2021-12-13 14:23:31 -05:00
|
|
|
cvid)
|
2020-07-06 17:44:32 -05:00
|
|
|
result := &imagesForCve{}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
err := service.makeGraphQLQuery(ctx, config, username, password, query, result)
|
2020-07-06 17:44:32 -05:00
|
|
|
if err != nil {
|
|
|
|
if isContextDone(ctx) {
|
|
|
|
return
|
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
rch <- stringResult{"", err}
|
2020-07-06 17:44:32 -05:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if result.Errors != nil {
|
|
|
|
var errBuilder strings.Builder
|
|
|
|
|
|
|
|
for _, err := range result.Errors {
|
|
|
|
fmt.Fprintln(&errBuilder, err.Message)
|
|
|
|
}
|
|
|
|
|
|
|
|
if isContextDone(ctx) {
|
|
|
|
return
|
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
rch <- stringResult{"", errors.New(errBuilder.String())} //nolint: goerr113
|
2020-07-06 17:44:32 -05:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var localWg sync.WaitGroup
|
|
|
|
|
2022-03-21 12:37:23 -05:00
|
|
|
rlim := newSmoothRateLimiter(&localWg, rch)
|
2020-07-06 17:44:32 -05:00
|
|
|
localWg.Add(1)
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
go rlim.startRateLimiter(ctx)
|
2020-07-06 17:44:32 -05:00
|
|
|
|
|
|
|
for _, image := range result.Data.ImageListForCVE {
|
|
|
|
if !strings.EqualFold(imageName, image.Name) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tag := range image.Tags {
|
|
|
|
localWg.Add(1)
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
go addManifestCallToPool(ctx, config, rlim, username, password, image.Name, tag, rch, &localWg)
|
2020-07-06 17:44:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
localWg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (service searchService) getCveByImage(ctx context.Context, config searchConfig, username, password,
|
2022-03-21 12:37:23 -05:00
|
|
|
imageName string, rch chan stringResult, wtgrp *sync.WaitGroup,
|
|
|
|
) {
|
2021-12-13 14:23:31 -05:00
|
|
|
defer wtgrp.Done()
|
|
|
|
defer close(rch)
|
2020-07-06 17:44:32 -05:00
|
|
|
|
|
|
|
query := fmt.Sprintf(`{ CVEListForImage (image:"%s")`+
|
|
|
|
` { Tag CVEList { Id Title Severity Description `+
|
|
|
|
`PackageList {Name InstalledVersion FixedVersion}} } }`, imageName)
|
|
|
|
result := &cveResult{}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
err := service.makeGraphQLQuery(ctx, config, username, password, query, result)
|
2020-07-06 17:44:32 -05:00
|
|
|
if err != nil {
|
|
|
|
if isContextDone(ctx) {
|
|
|
|
return
|
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
rch <- stringResult{"", err}
|
2020-07-06 17:44:32 -05:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if result.Errors != nil {
|
|
|
|
var errBuilder strings.Builder
|
|
|
|
|
|
|
|
for _, err := range result.Errors {
|
|
|
|
fmt.Fprintln(&errBuilder, err.Message)
|
|
|
|
}
|
|
|
|
|
|
|
|
if isContextDone(ctx) {
|
|
|
|
return
|
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
rch <- stringResult{"", errors.New(errBuilder.String())} //nolint: goerr113
|
2020-07-06 17:44:32 -05:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-04 12:56:47 -05:00
|
|
|
result.Data.CVEListForImage.CVEList = groupCVEsBySeverity(result.Data.CVEListForImage.CVEList)
|
|
|
|
|
2020-07-06 17:44:32 -05:00
|
|
|
str, err := result.string(*config.outputFormat)
|
|
|
|
if err != nil {
|
|
|
|
if isContextDone(ctx) {
|
|
|
|
return
|
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
rch <- stringResult{"", err}
|
2020-07-06 17:44:32 -05:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if isContextDone(ctx) {
|
|
|
|
return
|
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
rch <- stringResult{str, nil}
|
2020-07-06 17:44:32 -05:00
|
|
|
}
|
|
|
|
|
2020-09-04 12:56:47 -05:00
|
|
|
func groupCVEsBySeverity(cveList []cve) []cve {
|
|
|
|
high := make([]cve, 0)
|
|
|
|
med := make([]cve, 0)
|
|
|
|
low := make([]cve, 0)
|
|
|
|
|
|
|
|
for _, cve := range cveList {
|
|
|
|
switch cve.Severity {
|
|
|
|
case "LOW":
|
|
|
|
low = append(low, cve)
|
|
|
|
|
|
|
|
case "MEDIUM":
|
|
|
|
med = append(med, cve)
|
|
|
|
|
|
|
|
case "HIGH":
|
|
|
|
high = append(high, cve)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return append(append(high, med...), low...)
|
|
|
|
}
|
|
|
|
|
2020-06-16 20:52:40 -05:00
|
|
|
func isContextDone(ctx context.Context) bool {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-06 17:44:32 -05:00
|
|
|
func (service searchService) getFixedTagsForCVE(ctx context.Context, config searchConfig,
|
2022-03-21 12:37:23 -05:00
|
|
|
username, password, imageName, cvid string, rch chan stringResult, wtgrp *sync.WaitGroup,
|
|
|
|
) {
|
2021-12-13 14:23:31 -05:00
|
|
|
defer wtgrp.Done()
|
|
|
|
defer close(rch)
|
2020-07-06 17:44:32 -05:00
|
|
|
|
|
|
|
query := fmt.Sprintf(`{ImageListWithCVEFixed (id: "%s", image: "%s") {`+`
|
|
|
|
Tags {Name Timestamp} }
|
|
|
|
}`,
|
2021-12-13 14:23:31 -05:00
|
|
|
cvid, imageName)
|
2020-07-06 17:44:32 -05:00
|
|
|
result := &fixedTags{}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
err := service.makeGraphQLQuery(ctx, config, username, password, query, result)
|
2020-07-06 17:44:32 -05:00
|
|
|
if err != nil {
|
|
|
|
if isContextDone(ctx) {
|
|
|
|
return
|
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
rch <- stringResult{"", err}
|
2020-07-06 17:44:32 -05:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if result.Errors != nil {
|
|
|
|
var errBuilder strings.Builder
|
|
|
|
|
|
|
|
for _, err := range result.Errors {
|
|
|
|
fmt.Fprintln(&errBuilder, err.Message)
|
|
|
|
}
|
|
|
|
|
|
|
|
if isContextDone(ctx) {
|
|
|
|
return
|
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
rch <- stringResult{"", errors.New(errBuilder.String())} //nolint: goerr113
|
2020-07-06 17:44:32 -05:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var localWg sync.WaitGroup
|
|
|
|
|
2022-03-21 12:37:23 -05:00
|
|
|
rlim := newSmoothRateLimiter(&localWg, rch)
|
2020-07-06 17:44:32 -05:00
|
|
|
localWg.Add(1)
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
go rlim.startRateLimiter(ctx)
|
2020-07-06 17:44:32 -05:00
|
|
|
|
|
|
|
for _, imgTag := range result.Data.ImageListWithCVEFixed.Tags {
|
|
|
|
localWg.Add(1)
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
go addManifestCallToPool(ctx, config, rlim, username, password, imageName, imgTag.Name, rch, &localWg)
|
2020-07-06 17:44:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
localWg.Wait()
|
|
|
|
}
|
|
|
|
|
2021-05-26 12:22:31 -05:00
|
|
|
// Query using JQL, the query string is passed as a parameter
|
|
|
|
// errors are returned in the stringResult channel, the unmarshalled payload is in resultPtr.
|
2021-12-13 14:23:31 -05:00
|
|
|
func (service searchService) makeGraphQLQuery(ctx context.Context, config searchConfig,
|
|
|
|
username, password, query string,
|
2022-03-21 12:37:23 -05:00
|
|
|
resultPtr interface{},
|
|
|
|
) error {
|
2022-02-24 15:31:36 -05:00
|
|
|
endPoint, err := combineServerAndEndpointURL(*config.servURL, constants.ExtSearchPrefix)
|
2021-05-26 12:22:31 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
err = makeGraphQLRequest(ctx, endPoint, query, username, password, *config.verifyTLS, resultPtr)
|
2021-05-26 12:22:31 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
func addManifestCallToPool(ctx context.Context, config searchConfig, pool *requestsPool,
|
2022-03-21 12:37:23 -05:00
|
|
|
username, password, imageName, tagName string, rch chan stringResult, wtgrp *sync.WaitGroup,
|
|
|
|
) {
|
2021-12-13 14:23:31 -05:00
|
|
|
defer wtgrp.Done()
|
2020-06-16 20:52:40 -05:00
|
|
|
|
|
|
|
resultManifest := manifestResponse{}
|
|
|
|
|
2020-07-17 14:42:22 -05:00
|
|
|
manifestEndpoint, err := combineServerAndEndpointURL(*config.servURL,
|
|
|
|
fmt.Sprintf("/v2/%s/manifests/%s", imageName, tagName))
|
2020-06-16 20:52:40 -05:00
|
|
|
if err != nil {
|
|
|
|
if isContextDone(ctx) {
|
|
|
|
return
|
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
rch <- stringResult{"", err}
|
2020-06-16 20:52:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
job := manifestJob{
|
|
|
|
url: manifestEndpoint,
|
|
|
|
username: username,
|
|
|
|
imageName: imageName,
|
|
|
|
password: password,
|
|
|
|
tagName: tagName,
|
|
|
|
manifestResp: resultManifest,
|
2020-07-17 14:42:22 -05:00
|
|
|
config: config,
|
2020-06-16 20:52:40 -05:00
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
wtgrp.Add(1)
|
|
|
|
pool.submitJob(&job)
|
2020-06-16 20:52:40 -05:00
|
|
|
}
|
|
|
|
|
2020-07-06 17:44:32 -05:00
|
|
|
type cveResult struct {
|
|
|
|
Errors []errorGraphQL `json:"errors"`
|
|
|
|
Data cveData `json:"data"`
|
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2020-07-06 17:44:32 -05:00
|
|
|
type errorGraphQL struct {
|
|
|
|
Message string `json:"message"`
|
|
|
|
Path []string `json:"path"`
|
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
|
|
|
|
//nolint:tagliatelle // graphQL schema
|
2020-07-06 17:44:32 -05:00
|
|
|
type packageList struct {
|
|
|
|
Name string `json:"Name"`
|
|
|
|
InstalledVersion string `json:"InstalledVersion"`
|
|
|
|
FixedVersion string `json:"FixedVersion"`
|
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
|
|
|
|
//nolint:tagliatelle // graphQL schema
|
2020-07-06 17:44:32 -05:00
|
|
|
type cve struct {
|
|
|
|
ID string `json:"Id"`
|
|
|
|
Severity string `json:"Severity"`
|
|
|
|
Title string `json:"Title"`
|
|
|
|
Description string `json:"Description"`
|
|
|
|
PackageList []packageList `json:"PackageList"`
|
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
|
|
|
|
//nolint:tagliatelle // graphQL schema
|
2020-07-06 17:44:32 -05:00
|
|
|
type cveListForImage struct {
|
|
|
|
Tag string `json:"Tag"`
|
|
|
|
CVEList []cve `json:"CVEList"`
|
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
|
|
|
|
//nolint:tagliatelle // graphQL schema
|
2020-07-06 17:44:32 -05:00
|
|
|
type cveData struct {
|
|
|
|
CVEListForImage cveListForImage `json:"CVEListForImage"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cve cveResult) string(format string) (string, error) {
|
|
|
|
switch strings.ToLower(format) {
|
|
|
|
case "", defaultOutoutFormat:
|
|
|
|
return cve.stringPlainText()
|
|
|
|
case "json":
|
|
|
|
return cve.stringJSON()
|
|
|
|
case "yml", "yaml":
|
|
|
|
return cve.stringYAML()
|
|
|
|
default:
|
|
|
|
return "", ErrInvalidOutputFormat
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cve cveResult) stringPlainText() (string, error) {
|
|
|
|
var builder strings.Builder
|
|
|
|
|
|
|
|
table := getCVETableWriter(&builder)
|
|
|
|
|
|
|
|
for _, c := range cve.Data.CVEListForImage.CVEList {
|
2021-12-13 14:23:31 -05:00
|
|
|
id := ellipsize(c.ID, cvidWidth, ellipsis)
|
2020-07-06 17:44:32 -05:00
|
|
|
title := ellipsize(c.Title, cveTitleWidth, ellipsis)
|
|
|
|
severity := ellipsize(c.Severity, cveSeverityWidth, ellipsis)
|
2021-12-13 14:23:31 -05:00
|
|
|
row := make([]string, 3) //nolint:gomnd
|
2020-07-06 17:44:32 -05:00
|
|
|
row[colCVEIDIndex] = id
|
|
|
|
row[colCVESeverityIndex] = severity
|
|
|
|
row[colCVETitleIndex] = title
|
|
|
|
|
|
|
|
table.Append(row)
|
|
|
|
}
|
|
|
|
|
|
|
|
table.Render()
|
|
|
|
|
|
|
|
return builder.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cve cveResult) stringJSON() (string, error) {
|
2021-12-13 14:23:31 -05:00
|
|
|
json := jsoniter.ConfigCompatibleWithStandardLibrary
|
2020-07-06 17:44:32 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
body, err := json.MarshalIndent(cve.Data.CVEListForImage, "", " ")
|
2020-07-06 17:44:32 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(body), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cve cveResult) stringYAML() (string, error) {
|
|
|
|
body, err := yaml.Marshal(&cve.Data.CVEListForImage)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(body), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type fixedTags struct {
|
|
|
|
Errors []errorGraphQL `json:"errors"`
|
|
|
|
Data struct {
|
2021-12-13 14:23:31 -05:00
|
|
|
//nolint:tagliatelle // graphQL schema
|
2020-07-06 17:44:32 -05:00
|
|
|
ImageListWithCVEFixed struct {
|
|
|
|
Tags []struct {
|
|
|
|
Name string `json:"Name"`
|
|
|
|
Timestamp time.Time `json:"Timestamp"`
|
|
|
|
} `json:"Tags"`
|
|
|
|
} `json:"ImageListWithCVEFixed"`
|
|
|
|
} `json:"data"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type imagesForCve struct {
|
|
|
|
Errors []errorGraphQL `json:"errors"`
|
|
|
|
Data struct {
|
2021-12-13 14:23:31 -05:00
|
|
|
ImageListForCVE []tagListResp `json:"ImageListForCVE"` //nolint:tagliatelle // graphQL schema
|
2020-07-06 17:44:32 -05:00
|
|
|
} `json:"data"`
|
|
|
|
}
|
|
|
|
|
2021-05-26 12:22:31 -05:00
|
|
|
type imagesForDigest struct {
|
|
|
|
Errors []errorGraphQL `json:"errors"`
|
|
|
|
Data struct {
|
2021-12-13 14:23:31 -05:00
|
|
|
ImageListForDigest []tagListResp `json:"ImageListForDigest"` //nolint:tagliatelle // graphQL schema
|
2021-05-26 12:22:31 -05:00
|
|
|
} `json:"data"`
|
|
|
|
}
|
|
|
|
|
2020-06-16 20:52:40 -05:00
|
|
|
type tagListResp struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Tags []string `json:"tags"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type imageStruct struct {
|
2021-05-28 11:27:17 -05:00
|
|
|
Name string `json:"name"`
|
|
|
|
Tags []tags `json:"tags"`
|
|
|
|
verbose bool
|
2020-06-16 20:52:40 -05:00
|
|
|
}
|
2021-05-28 11:27:17 -05:00
|
|
|
|
2020-06-16 20:52:40 -05:00
|
|
|
type tags struct {
|
2021-05-28 11:27:17 -05:00
|
|
|
Name string `json:"name"`
|
|
|
|
Size uint64 `json:"size"`
|
|
|
|
Digest string `json:"digest"`
|
|
|
|
ConfigDigest string `json:"configDigest"`
|
|
|
|
Layers []layer `json:"layerDigests"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type layer struct {
|
2020-06-16 20:52:40 -05:00
|
|
|
Size uint64 `json:"size"`
|
|
|
|
Digest string `json:"digest"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (img imageStruct) string(format string) (string, error) {
|
|
|
|
switch strings.ToLower(format) {
|
2020-07-06 17:44:32 -05:00
|
|
|
case "", defaultOutoutFormat:
|
2020-06-16 20:52:40 -05:00
|
|
|
return img.stringPlainText()
|
|
|
|
case "json":
|
|
|
|
return img.stringJSON()
|
|
|
|
case "yml", "yaml":
|
|
|
|
return img.stringYAML()
|
|
|
|
default:
|
|
|
|
return "", ErrInvalidOutputFormat
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (img imageStruct) stringPlainText() (string, error) {
|
|
|
|
var builder strings.Builder
|
|
|
|
|
2020-07-06 17:44:32 -05:00
|
|
|
table := getImageTableWriter(&builder)
|
2021-05-28 11:27:17 -05:00
|
|
|
table.SetColMinWidth(colImageNameIndex, imageNameWidth)
|
|
|
|
table.SetColMinWidth(colTagIndex, tagWidth)
|
|
|
|
table.SetColMinWidth(colDigestIndex, digestWidth)
|
|
|
|
table.SetColMinWidth(colSizeIndex, sizeWidth)
|
|
|
|
|
|
|
|
if img.verbose {
|
|
|
|
table.SetColMinWidth(colConfigIndex, configWidth)
|
|
|
|
table.SetColMinWidth(colLayersIndex, layersWidth)
|
|
|
|
}
|
2020-06-16 20:52:40 -05:00
|
|
|
|
|
|
|
for _, tag := range img.Tags {
|
|
|
|
imageName := ellipsize(img.Name, imageNameWidth, ellipsis)
|
|
|
|
tagName := ellipsize(tag.Name, tagWidth, ellipsis)
|
|
|
|
digest := ellipsize(tag.Digest, digestWidth, "")
|
|
|
|
size := ellipsize(strings.ReplaceAll(humanize.Bytes(tag.Size), " ", ""), sizeWidth, ellipsis)
|
2021-05-28 11:27:17 -05:00
|
|
|
config := ellipsize(tag.ConfigDigest, configWidth, "")
|
2021-12-13 14:23:31 -05:00
|
|
|
row := make([]string, 6) //nolint:gomnd
|
2020-07-06 17:44:32 -05:00
|
|
|
|
|
|
|
row[colImageNameIndex] = imageName
|
|
|
|
row[colTagIndex] = tagName
|
|
|
|
row[colDigestIndex] = digest
|
|
|
|
row[colSizeIndex] = size
|
2020-06-16 20:52:40 -05:00
|
|
|
|
2021-05-28 11:27:17 -05:00
|
|
|
if img.verbose {
|
|
|
|
row[colConfigIndex] = config
|
|
|
|
row[colLayersIndex] = ""
|
|
|
|
}
|
|
|
|
|
2020-06-16 20:52:40 -05:00
|
|
|
table.Append(row)
|
2021-05-28 11:27:17 -05:00
|
|
|
|
|
|
|
if img.verbose {
|
|
|
|
for _, entry := range tag.Layers {
|
|
|
|
layerSize := ellipsize(strings.ReplaceAll(humanize.Bytes(entry.Size), " ", ""), sizeWidth, ellipsis)
|
|
|
|
layerDigest := ellipsize(entry.Digest, digestWidth, "")
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
layerRow := make([]string, 6) //nolint:gomnd
|
2021-05-28 11:27:17 -05:00
|
|
|
layerRow[colImageNameIndex] = ""
|
|
|
|
layerRow[colTagIndex] = ""
|
|
|
|
layerRow[colDigestIndex] = ""
|
|
|
|
layerRow[colSizeIndex] = layerSize
|
|
|
|
layerRow[colConfigIndex] = ""
|
|
|
|
layerRow[colLayersIndex] = layerDigest
|
|
|
|
|
|
|
|
table.Append(layerRow)
|
|
|
|
}
|
|
|
|
}
|
2020-06-16 20:52:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
table.Render()
|
|
|
|
|
|
|
|
return builder.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (img imageStruct) stringJSON() (string, error) {
|
2021-12-13 14:23:31 -05:00
|
|
|
json := jsoniter.ConfigCompatibleWithStandardLibrary
|
2020-06-16 20:52:40 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
body, err := json.MarshalIndent(img, "", " ")
|
2020-06-16 20:52:40 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(body), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (img imageStruct) stringYAML() (string, error) {
|
|
|
|
body, err := yaml.Marshal(&img)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(body), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type catalogResponse struct {
|
|
|
|
Repositories []string `json:"repositories"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type manifestResponse struct {
|
|
|
|
Layers []struct {
|
|
|
|
MediaType string `json:"mediaType"`
|
|
|
|
Digest string `json:"digest"`
|
|
|
|
Size uint64 `json:"size"`
|
|
|
|
} `json:"layers"`
|
|
|
|
Annotations struct {
|
2021-12-13 14:23:31 -05:00
|
|
|
WsTychoStackerStackerYaml string `json:"ws.tycho.stacker.stacker_yaml"` //nolint:tagliatelle // custom annotation
|
|
|
|
WsTychoStackerGitVersion string `json:"ws.tycho.stacker.git_version"` //nolint:tagliatelle // custom annotation
|
2020-06-16 20:52:40 -05:00
|
|
|
} `json:"annotations"`
|
|
|
|
Config struct {
|
|
|
|
Size int `json:"size"`
|
|
|
|
Digest string `json:"digest"`
|
|
|
|
MediaType string `json:"mediaType"`
|
|
|
|
} `json:"config"`
|
|
|
|
SchemaVersion int `json:"schemaVersion"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func combineServerAndEndpointURL(serverURL, endPoint string) (string, error) {
|
|
|
|
if !isURL(serverURL) {
|
|
|
|
return "", zotErrors.ErrInvalidURL
|
|
|
|
}
|
|
|
|
|
|
|
|
newURL, err := url.Parse(serverURL)
|
|
|
|
if err != nil {
|
|
|
|
return "", zotErrors.ErrInvalidURL
|
|
|
|
}
|
|
|
|
|
|
|
|
newURL, _ = newURL.Parse(endPoint)
|
|
|
|
|
|
|
|
return newURL.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ellipsize(text string, max int, trailing string) string {
|
2020-07-06 17:44:32 -05:00
|
|
|
text = strings.TrimSpace(text)
|
2020-06-16 20:52:40 -05:00
|
|
|
if len(text) <= max {
|
|
|
|
return text
|
|
|
|
}
|
|
|
|
|
|
|
|
chopLength := len(trailing)
|
|
|
|
|
|
|
|
return text[:max-chopLength] + trailing
|
|
|
|
}
|
|
|
|
|
2020-07-06 17:44:32 -05:00
|
|
|
func getImageTableWriter(writer io.Writer) *tablewriter.Table {
|
2020-06-16 20:52:40 -05:00
|
|
|
table := tablewriter.NewWriter(writer)
|
|
|
|
|
|
|
|
table.SetAutoWrapText(false)
|
|
|
|
table.SetAutoFormatHeaders(true)
|
|
|
|
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
|
|
|
|
table.SetAlignment(tablewriter.ALIGN_LEFT)
|
|
|
|
table.SetCenterSeparator("")
|
|
|
|
table.SetColumnSeparator("")
|
|
|
|
table.SetRowSeparator("")
|
|
|
|
table.SetHeaderLine(false)
|
|
|
|
table.SetBorder(false)
|
|
|
|
table.SetTablePadding(" ")
|
|
|
|
table.SetNoWhiteSpace(true)
|
|
|
|
|
|
|
|
return table
|
|
|
|
}
|
|
|
|
|
2020-07-06 17:44:32 -05:00
|
|
|
func getCVETableWriter(writer io.Writer) *tablewriter.Table {
|
|
|
|
table := tablewriter.NewWriter(writer)
|
|
|
|
|
|
|
|
table.SetAutoWrapText(false)
|
|
|
|
table.SetAutoFormatHeaders(true)
|
|
|
|
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
|
|
|
|
table.SetAlignment(tablewriter.ALIGN_LEFT)
|
|
|
|
table.SetCenterSeparator("")
|
|
|
|
table.SetColumnSeparator("")
|
|
|
|
table.SetRowSeparator("")
|
|
|
|
table.SetHeaderLine(false)
|
|
|
|
table.SetBorder(false)
|
|
|
|
table.SetTablePadding(" ")
|
|
|
|
table.SetNoWhiteSpace(true)
|
2021-12-13 14:23:31 -05:00
|
|
|
table.SetColMinWidth(colCVEIDIndex, cvidWidth)
|
2020-07-06 17:44:32 -05:00
|
|
|
table.SetColMinWidth(colCVESeverityIndex, cveSeverityWidth)
|
|
|
|
table.SetColMinWidth(colCVETitleIndex, cveTitleWidth)
|
|
|
|
|
|
|
|
return table
|
|
|
|
}
|
|
|
|
|
2020-06-16 20:52:40 -05:00
|
|
|
const (
|
|
|
|
imageNameWidth = 32
|
|
|
|
tagWidth = 24
|
|
|
|
digestWidth = 8
|
|
|
|
sizeWidth = 8
|
2021-05-28 11:27:17 -05:00
|
|
|
configWidth = 8
|
|
|
|
layersWidth = 8
|
2020-06-16 20:52:40 -05:00
|
|
|
ellipsis = "..."
|
|
|
|
|
|
|
|
colImageNameIndex = 0
|
|
|
|
colTagIndex = 1
|
|
|
|
colDigestIndex = 2
|
2021-05-28 11:27:17 -05:00
|
|
|
colConfigIndex = 3
|
|
|
|
colLayersIndex = 4
|
|
|
|
colSizeIndex = 5
|
2020-07-06 17:44:32 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
cvidWidth = 16
|
2020-07-06 17:44:32 -05:00
|
|
|
cveSeverityWidth = 8
|
|
|
|
cveTitleWidth = 48
|
|
|
|
|
|
|
|
colCVEIDIndex = 0
|
|
|
|
colCVESeverityIndex = 1
|
|
|
|
colCVETitleIndex = 2
|
|
|
|
|
|
|
|
defaultOutoutFormat = "text"
|
2020-06-16 20:52:40 -05:00
|
|
|
)
|