2020-06-26 14:09:10 -05:00
|
|
|
package search
|
|
|
|
|
2022-07-15 06:10:51 -05:00
|
|
|
// This file will not be regenerated automatically.
|
|
|
|
//
|
|
|
|
// It serves as dependency injection for your app, add any dependencies you require here.
|
2020-06-26 14:09:10 -05:00
|
|
|
|
|
|
|
import (
|
2022-08-16 03:57:09 -05:00
|
|
|
"context"
|
|
|
|
"errors"
|
2022-09-13 09:20:44 -05:00
|
|
|
"fmt"
|
2022-07-12 07:58:04 -05:00
|
|
|
"sort"
|
2021-01-25 13:04:03 -05:00
|
|
|
"strconv"
|
2022-07-12 07:58:04 -05:00
|
|
|
"strings"
|
2020-06-26 14:09:10 -05:00
|
|
|
|
2022-07-29 10:33:34 -05:00
|
|
|
"github.com/99designs/gqlgen/graphql"
|
|
|
|
glob "github.com/bmatcuk/doublestar/v4" // nolint:gci
|
|
|
|
v1 "github.com/google/go-containerregistry/pkg/v1" // nolint:gci
|
2021-01-25 13:04:03 -05:00
|
|
|
godigest "github.com/opencontainers/go-digest"
|
2022-08-02 10:58:30 -05:00
|
|
|
ispec "github.com/opencontainers/image-spec/specs-go/v1"
|
2022-07-29 10:33:34 -05:00
|
|
|
"github.com/vektah/gqlparser/v2/gqlerror"
|
2021-12-03 22:50:58 -05:00
|
|
|
"zotregistry.io/zot/pkg/extensions/search/common"
|
|
|
|
cveinfo "zotregistry.io/zot/pkg/extensions/search/cve"
|
|
|
|
digestinfo "zotregistry.io/zot/pkg/extensions/search/digest"
|
2022-07-15 06:10:51 -05:00
|
|
|
"zotregistry.io/zot/pkg/extensions/search/gql_generated"
|
2022-08-16 03:57:09 -05:00
|
|
|
"zotregistry.io/zot/pkg/log" // nolint: gci
|
|
|
|
localCtx "zotregistry.io/zot/pkg/requestcontext"
|
2021-12-03 22:50:58 -05:00
|
|
|
"zotregistry.io/zot/pkg/storage"
|
2020-06-26 14:09:10 -05:00
|
|
|
) // THIS CODE IS A STARTING POINT ONLY. IT WILL NOT BE UPDATED WITH SCHEMA CHANGES.
|
|
|
|
|
|
|
|
// Resolver ...
|
|
|
|
type Resolver struct {
|
2021-04-05 19:40:33 -05:00
|
|
|
cveInfo *cveinfo.CveInfo
|
|
|
|
storeController storage.StoreController
|
2021-05-26 12:22:31 -05:00
|
|
|
digestInfo *digestinfo.DigestInfo
|
2021-01-25 13:04:03 -05:00
|
|
|
log log.Logger
|
2020-06-26 14:09:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type cveDetail struct {
|
|
|
|
Title string
|
|
|
|
Description string
|
|
|
|
Severity string
|
2022-07-15 06:10:51 -05:00
|
|
|
PackageList []*gql_generated.PackageInfo
|
2020-06-26 14:09:10 -05:00
|
|
|
}
|
|
|
|
|
2022-09-21 12:53:56 -05:00
|
|
|
var (
|
|
|
|
ErrBadCtxFormat = errors.New("type assertion failed")
|
|
|
|
ErrBadLayerCount = errors.New("manifest: layers count doesn't correspond to config history")
|
|
|
|
)
|
2022-08-16 03:57:09 -05:00
|
|
|
|
2020-06-26 14:09:10 -05:00
|
|
|
// GetResolverConfig ...
|
2022-07-15 06:10:51 -05:00
|
|
|
func GetResolverConfig(log log.Logger, storeController storage.StoreController, enableCVE bool) gql_generated.Config {
|
2021-01-25 13:04:03 -05:00
|
|
|
var cveInfo *cveinfo.CveInfo
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if enableCVE {
|
|
|
|
cveInfo, err = cveinfo.GetCVEInfo(storeController, log)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-06-26 14:09:10 -05:00
|
|
|
}
|
|
|
|
|
2021-09-30 08:27:13 -05:00
|
|
|
digestInfo := digestinfo.NewDigestInfo(storeController, log)
|
2020-06-26 14:09:10 -05:00
|
|
|
|
2021-01-25 13:04:03 -05:00
|
|
|
resConfig := &Resolver{cveInfo: cveInfo, storeController: storeController, digestInfo: digestInfo, log: log}
|
|
|
|
|
2022-07-15 06:10:51 -05:00
|
|
|
return gql_generated.Config{
|
|
|
|
Resolvers: resConfig, Directives: gql_generated.DirectiveRoot{},
|
|
|
|
Complexity: gql_generated.ComplexityRoot{},
|
2020-06-26 14:09:10 -05:00
|
|
|
}
|
2021-04-05 19:40:33 -05:00
|
|
|
}
|
2020-06-26 14:09:10 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
func (r *queryResolver) getImageListForCVE(repoList []string, cvid string, imgStore storage.ImageStore,
|
2022-03-21 12:37:23 -05:00
|
|
|
trivyCtx *cveinfo.TrivyCtx,
|
2022-01-19 10:57:10 -05:00
|
|
|
) ([]*gql_generated.ImageSummary, error) {
|
|
|
|
cveResult := []*gql_generated.ImageSummary{}
|
2022-09-21 12:53:56 -05:00
|
|
|
olu := common.NewBaseOciLayoutUtils(r.storeController, r.log)
|
2020-06-26 14:09:10 -05:00
|
|
|
|
2021-04-05 19:40:33 -05:00
|
|
|
for _, repo := range repoList {
|
2021-01-25 13:04:03 -05:00
|
|
|
r.log.Info().Str("repo", repo).Msg("extracting list of tags available in image repo")
|
2020-06-26 14:09:10 -05:00
|
|
|
|
2022-01-19 10:57:10 -05:00
|
|
|
imageListByCVE, err := r.cveInfo.GetImageListForCVE(repo, cvid, imgStore, trivyCtx)
|
2021-04-05 19:40:33 -05:00
|
|
|
if err != nil {
|
2021-01-25 13:04:03 -05:00
|
|
|
r.log.Error().Err(err).Msg("error getting tag")
|
2020-06-26 14:09:10 -05:00
|
|
|
|
2021-04-05 19:40:33 -05:00
|
|
|
return cveResult, err
|
2020-06-26 14:09:10 -05:00
|
|
|
}
|
|
|
|
|
2022-01-19 10:57:10 -05:00
|
|
|
for _, imageByCVE := range imageListByCVE {
|
2022-09-21 12:53:56 -05:00
|
|
|
imageConfig, err := olu.GetImageConfigInfo(repo, imageByCVE.Digest)
|
|
|
|
if err != nil {
|
|
|
|
return []*gql_generated.ImageSummary{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
imageInfo := BuildImageInfo(repo, imageByCVE.Tag, imageByCVE.Digest, imageByCVE.Manifest, imageConfig)
|
|
|
|
|
2022-01-19 10:57:10 -05:00
|
|
|
cveResult = append(
|
2022-09-21 12:53:56 -05:00
|
|
|
cveResult, imageInfo,
|
2022-01-19 10:57:10 -05:00
|
|
|
)
|
2020-06-26 14:09:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cveResult, nil
|
|
|
|
}
|
2020-08-19 01:53:04 -05:00
|
|
|
|
2022-01-19 10:57:10 -05:00
|
|
|
func (r *queryResolver) getImageListForDigest(repoList []string, digest string) ([]*gql_generated.ImageSummary, error) {
|
|
|
|
imgResultForDigest := []*gql_generated.ImageSummary{}
|
2022-09-21 12:53:56 -05:00
|
|
|
olu := common.NewBaseOciLayoutUtils(r.storeController, r.log)
|
2021-05-26 12:22:31 -05:00
|
|
|
|
|
|
|
var errResult error
|
|
|
|
|
|
|
|
for _, repo := range repoList {
|
2021-01-25 13:04:03 -05:00
|
|
|
r.log.Info().Str("repo", repo).Msg("filtering list of tags in image repo by digest")
|
2021-05-26 12:22:31 -05:00
|
|
|
|
2022-01-19 10:57:10 -05:00
|
|
|
imgTags, err := r.digestInfo.GetImageTagsByDigest(repo, digest)
|
2021-05-26 12:22:31 -05:00
|
|
|
if err != nil {
|
2021-01-25 13:04:03 -05:00
|
|
|
r.log.Error().Err(err).Msg("unable to get filtered list of image tags")
|
|
|
|
|
2022-01-19 10:57:10 -05:00
|
|
|
return []*gql_generated.ImageSummary{}, err
|
2021-05-26 12:22:31 -05:00
|
|
|
}
|
|
|
|
|
2022-01-19 10:57:10 -05:00
|
|
|
for _, imageInfo := range imgTags {
|
2022-09-21 12:53:56 -05:00
|
|
|
imageConfig, err := olu.GetImageConfigInfo(repo, imageInfo.Digest)
|
|
|
|
if err != nil {
|
|
|
|
return []*gql_generated.ImageSummary{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
imageInfo := BuildImageInfo(repo, imageInfo.Tag, imageInfo.Digest, imageInfo.Manifest, imageConfig)
|
|
|
|
|
2022-01-19 10:57:10 -05:00
|
|
|
imgResultForDigest = append(imgResultForDigest, imageInfo)
|
2021-05-26 12:22:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return imgResultForDigest, errResult
|
|
|
|
}
|
|
|
|
|
2022-09-13 09:20:44 -05:00
|
|
|
func repoListWithNewestImage(
|
|
|
|
ctx context.Context,
|
|
|
|
repoList []string,
|
|
|
|
olu common.OciLayoutUtils,
|
|
|
|
log log.Logger,
|
|
|
|
) ([]*gql_generated.RepoSummary, error) {
|
|
|
|
reposSummary := []*gql_generated.RepoSummary{}
|
2021-01-25 13:04:03 -05:00
|
|
|
|
2022-09-13 09:20:44 -05:00
|
|
|
for _, repo := range repoList {
|
2022-07-29 10:33:34 -05:00
|
|
|
lastUpdatedTag, err := olu.GetRepoLastUpdated(repo)
|
2021-01-25 13:04:03 -05:00
|
|
|
if err != nil {
|
2022-09-13 09:20:44 -05:00
|
|
|
msg := fmt.Sprintf("can't get last updated manifest for repo: %s", repo)
|
|
|
|
log.Error().Err(err).Msg(msg)
|
|
|
|
|
|
|
|
graphql.AddError(ctx, gqlerror.Errorf(msg))
|
2021-01-25 13:04:03 -05:00
|
|
|
|
2022-07-29 10:33:34 -05:00
|
|
|
continue
|
2021-01-25 13:04:03 -05:00
|
|
|
}
|
|
|
|
|
2022-07-29 10:33:34 -05:00
|
|
|
repoSize := int64(0)
|
|
|
|
repoBlob2Size := make(map[string]int64, 10)
|
|
|
|
|
|
|
|
manifests, err := olu.GetImageManifests(repo)
|
|
|
|
if err != nil {
|
2022-09-13 09:20:44 -05:00
|
|
|
msg := fmt.Sprintf("can't get manifests for repo: %s", repo)
|
|
|
|
|
|
|
|
log.Error().Err(err).Msg(msg)
|
|
|
|
graphql.AddError(ctx, gqlerror.Errorf(msg))
|
2021-01-25 13:04:03 -05:00
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-09-13 09:20:44 -05:00
|
|
|
repoPlatforms := make([]*gql_generated.OsArch, 0)
|
2022-07-29 10:33:34 -05:00
|
|
|
repoVendors := make([]*string, 0, len(manifests))
|
|
|
|
repoName := repo
|
2021-01-25 13:04:03 -05:00
|
|
|
|
2022-07-29 10:33:34 -05:00
|
|
|
var lastUpdatedImageSummary gql_generated.ImageSummary
|
2021-01-25 13:04:03 -05:00
|
|
|
|
2022-07-29 10:33:34 -05:00
|
|
|
var brokenManifest bool
|
2021-01-25 13:04:03 -05:00
|
|
|
|
2022-09-13 09:20:44 -05:00
|
|
|
for _, manifest := range manifests {
|
2022-07-29 10:33:34 -05:00
|
|
|
imageLayersSize := int64(0)
|
2022-09-13 09:20:44 -05:00
|
|
|
manifestSize := olu.GetImageManifestSize(repo, manifest.Digest)
|
2021-01-25 13:04:03 -05:00
|
|
|
|
2022-09-13 09:20:44 -05:00
|
|
|
imageBlobManifest, err := olu.GetImageBlobManifest(repo, manifest.Digest)
|
|
|
|
if err != nil {
|
|
|
|
msg := fmt.Sprintf("reference not found for manifest %s", manifest.Digest)
|
|
|
|
|
|
|
|
log.Error().Err(err).Msg(msg)
|
|
|
|
graphql.AddError(ctx, gqlerror.Errorf(msg))
|
|
|
|
|
|
|
|
brokenManifest = true
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
2021-01-25 13:04:03 -05:00
|
|
|
|
2022-07-29 10:33:34 -05:00
|
|
|
configSize := imageBlobManifest.Config.Size
|
2022-09-13 09:20:44 -05:00
|
|
|
repoBlob2Size[manifest.Digest.String()] = manifestSize
|
2022-07-29 10:33:34 -05:00
|
|
|
repoBlob2Size[imageBlobManifest.Config.Digest.Hex] = configSize
|
2021-01-25 13:04:03 -05:00
|
|
|
|
2022-07-29 10:33:34 -05:00
|
|
|
for _, layer := range imageBlobManifest.Layers {
|
|
|
|
repoBlob2Size[layer.Digest.String()] = layer.Size
|
|
|
|
imageLayersSize += layer.Size
|
|
|
|
}
|
2021-01-25 13:04:03 -05:00
|
|
|
|
2022-07-29 10:33:34 -05:00
|
|
|
imageSize := imageLayersSize + manifestSize + configSize
|
|
|
|
|
2022-09-13 09:20:44 -05:00
|
|
|
imageConfigInfo, err := olu.GetImageConfigInfo(repo, manifest.Digest)
|
|
|
|
if err != nil {
|
|
|
|
msg := fmt.Sprintf("can't get image config for manifest %s", manifest.Digest)
|
|
|
|
|
|
|
|
log.Error().Err(err).Msg(msg)
|
|
|
|
graphql.AddError(ctx, gqlerror.Errorf(msg))
|
|
|
|
|
|
|
|
brokenManifest = true
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
2021-01-25 13:04:03 -05:00
|
|
|
|
2022-07-29 10:33:34 -05:00
|
|
|
os, arch := olu.GetImagePlatform(imageConfigInfo)
|
|
|
|
osArch := &gql_generated.OsArch{
|
|
|
|
Os: &os,
|
|
|
|
Arch: &arch,
|
|
|
|
}
|
|
|
|
repoPlatforms = append(repoPlatforms, osArch)
|
2021-01-25 13:04:03 -05:00
|
|
|
|
2022-09-13 09:20:44 -05:00
|
|
|
// get image info from manifest annotation, if not found get from image config labels.
|
|
|
|
annotations := common.GetAnnotations(imageBlobManifest.Annotations, imageConfigInfo.Config.Labels)
|
|
|
|
|
|
|
|
repoVendors = append(repoVendors, &annotations.Vendor)
|
2021-01-25 13:04:03 -05:00
|
|
|
|
2022-07-29 10:33:34 -05:00
|
|
|
manifestTag, ok := manifest.Annotations[ispec.AnnotationRefName]
|
|
|
|
if !ok {
|
2022-09-13 09:20:44 -05:00
|
|
|
msg := fmt.Sprintf("reference not found for manifest %s", manifest.Digest.String())
|
|
|
|
|
|
|
|
log.Error().Msg(msg)
|
|
|
|
graphql.AddError(ctx, gqlerror.Errorf(msg))
|
|
|
|
|
2022-07-29 10:33:34 -05:00
|
|
|
brokenManifest = true
|
2021-01-25 13:04:03 -05:00
|
|
|
|
2022-07-29 10:33:34 -05:00
|
|
|
break
|
|
|
|
}
|
2021-01-25 13:04:03 -05:00
|
|
|
|
2022-07-29 10:33:34 -05:00
|
|
|
tag := manifestTag
|
|
|
|
size := strconv.Itoa(int(imageSize))
|
2022-09-13 09:20:44 -05:00
|
|
|
manifestDigest := manifest.Digest.Hex()
|
|
|
|
configDigest := imageBlobManifest.Config.Digest.Hex
|
|
|
|
isSigned := olu.CheckManifestSignature(repo, manifest.Digest)
|
2022-07-29 10:33:34 -05:00
|
|
|
lastUpdated := olu.GetImageLastUpdated(imageConfigInfo)
|
|
|
|
score := 0
|
|
|
|
|
|
|
|
imageSummary := gql_generated.ImageSummary{
|
2022-09-13 09:20:44 -05:00
|
|
|
RepoName: &repoName,
|
|
|
|
Tag: &tag,
|
|
|
|
LastUpdated: &lastUpdated,
|
|
|
|
Digest: &manifestDigest,
|
|
|
|
ConfigDigest: &configDigest,
|
|
|
|
IsSigned: &isSigned,
|
|
|
|
Size: &size,
|
|
|
|
Platform: osArch,
|
|
|
|
Vendor: &annotations.Vendor,
|
|
|
|
Score: &score,
|
|
|
|
Description: &annotations.Description,
|
|
|
|
Title: &annotations.Title,
|
|
|
|
Documentation: &annotations.Documentation,
|
|
|
|
Licenses: &annotations.Licenses,
|
|
|
|
Labels: &annotations.Labels,
|
|
|
|
Source: &annotations.Source,
|
2022-07-29 10:33:34 -05:00
|
|
|
}
|
|
|
|
|
2022-09-13 09:20:44 -05:00
|
|
|
if manifest.Digest.String() == lastUpdatedTag.Digest {
|
2022-07-29 10:33:34 -05:00
|
|
|
lastUpdatedImageSummary = imageSummary
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if brokenManifest {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for blob := range repoBlob2Size {
|
|
|
|
repoSize += repoBlob2Size[blob]
|
|
|
|
}
|
2021-01-25 13:04:03 -05:00
|
|
|
|
2022-07-29 10:33:34 -05:00
|
|
|
repoSizeStr := strconv.FormatInt(repoSize, 10)
|
|
|
|
index := 0
|
|
|
|
|
2022-09-13 09:20:44 -05:00
|
|
|
reposSummary = append(reposSummary, &gql_generated.RepoSummary{
|
2022-07-29 10:33:34 -05:00
|
|
|
Name: &repoName,
|
|
|
|
LastUpdated: &lastUpdatedTag.Timestamp,
|
|
|
|
Size: &repoSizeStr,
|
|
|
|
Platforms: repoPlatforms,
|
|
|
|
Vendors: repoVendors,
|
|
|
|
Score: &index,
|
|
|
|
NewestImage: &lastUpdatedImageSummary,
|
2021-01-25 13:04:03 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-09-13 09:20:44 -05:00
|
|
|
return reposSummary, nil
|
2021-01-25 13:04:03 -05:00
|
|
|
}
|
|
|
|
|
2022-07-12 07:58:04 -05:00
|
|
|
func cleanQuerry(query string) string {
|
|
|
|
query = strings.ToLower(query)
|
|
|
|
query = strings.Replace(query, ":", " ", 1)
|
|
|
|
|
|
|
|
return query
|
|
|
|
}
|
|
|
|
|
|
|
|
func globalSearch(repoList []string, name, tag string, olu common.OciLayoutUtils, log log.Logger) (
|
|
|
|
[]*gql_generated.RepoSummary, []*gql_generated.ImageSummary, []*gql_generated.LayerSummary,
|
|
|
|
) {
|
|
|
|
repos := []*gql_generated.RepoSummary{}
|
|
|
|
images := []*gql_generated.ImageSummary{}
|
|
|
|
layers := []*gql_generated.LayerSummary{}
|
|
|
|
|
|
|
|
for _, repo := range repoList {
|
|
|
|
repo := repo
|
|
|
|
|
|
|
|
// map used for dedube if 2 images reference the same blob
|
2022-07-20 04:30:34 -05:00
|
|
|
repoBlob2Size := make(map[string]int64, 10)
|
2022-07-12 07:58:04 -05:00
|
|
|
|
|
|
|
// made up of all manifests, configs and image layers
|
|
|
|
repoSize := int64(0)
|
|
|
|
|
2022-07-22 15:01:38 -05:00
|
|
|
lastUpdatedTag, err := olu.GetRepoLastUpdated(repo)
|
2022-07-12 07:58:04 -05:00
|
|
|
if err != nil {
|
2022-07-22 15:01:38 -05:00
|
|
|
log.Error().Err(err).Msgf("can't find latest updated tag for repo: %s", repo)
|
2022-07-12 07:58:04 -05:00
|
|
|
}
|
|
|
|
|
2022-08-02 10:58:30 -05:00
|
|
|
manifests, err := olu.GetImageManifests(repo)
|
2022-07-12 07:58:04 -05:00
|
|
|
if err != nil {
|
2022-08-02 10:58:30 -05:00
|
|
|
log.Error().Err(err).Msgf("can't get manifests for repo: %s", repo)
|
2022-07-12 07:58:04 -05:00
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-07-22 15:01:38 -05:00
|
|
|
var lastUpdatedImageSummary gql_generated.ImageSummary
|
|
|
|
|
2022-08-02 10:58:30 -05:00
|
|
|
repoPlatforms := make([]*gql_generated.OsArch, 0, len(manifests))
|
|
|
|
repoVendors := make([]*string, 0, len(manifests))
|
2022-07-12 07:58:04 -05:00
|
|
|
|
2022-08-02 10:58:30 -05:00
|
|
|
for i, manifest := range manifests {
|
2022-07-12 07:58:04 -05:00
|
|
|
imageLayersSize := int64(0)
|
2022-07-20 04:30:34 -05:00
|
|
|
|
2022-08-02 10:58:30 -05:00
|
|
|
manifestTag, ok := manifest.Annotations[ispec.AnnotationRefName]
|
|
|
|
if !ok {
|
2022-09-13 09:20:44 -05:00
|
|
|
log.Error().Str("digest", manifest.Digest.String()).Msg("reference not found for this manifest")
|
2022-08-02 10:58:30 -05:00
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
imageBlobManifest, err := olu.GetImageBlobManifest(repo, manifests[i].Digest)
|
2022-07-20 04:30:34 -05:00
|
|
|
if err != nil {
|
2022-08-02 10:58:30 -05:00
|
|
|
log.Error().Err(err).Msgf("can't read manifest for repo %s %s", repo, manifestTag)
|
2022-07-20 04:30:34 -05:00
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-09-13 09:20:44 -05:00
|
|
|
manifestSize := olu.GetImageManifestSize(repo, manifest.Digest)
|
2022-07-20 04:30:34 -05:00
|
|
|
configSize := imageBlobManifest.Config.Size
|
|
|
|
|
2022-09-13 09:20:44 -05:00
|
|
|
repoBlob2Size[manifest.Digest.String()] = manifestSize
|
2022-07-20 04:30:34 -05:00
|
|
|
repoBlob2Size[imageBlobManifest.Config.Digest.Hex] = configSize
|
2022-07-12 07:58:04 -05:00
|
|
|
|
2022-08-02 10:58:30 -05:00
|
|
|
for _, layer := range imageBlobManifest.Layers {
|
2022-07-12 07:58:04 -05:00
|
|
|
layer := layer
|
2022-08-02 10:58:30 -05:00
|
|
|
layerDigest := layer.Digest.String()
|
|
|
|
layerSizeStr := strconv.Itoa(int(layer.Size))
|
|
|
|
repoBlob2Size[layer.Digest.String()] = layer.Size
|
|
|
|
imageLayersSize += layer.Size
|
2022-07-12 07:58:04 -05:00
|
|
|
|
|
|
|
// if we have a tag we won't match a layer
|
|
|
|
if tag != "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-08-02 10:58:30 -05:00
|
|
|
if index := strings.Index(layerDigest, name); index != -1 {
|
2022-07-12 07:58:04 -05:00
|
|
|
layers = append(layers, &gql_generated.LayerSummary{
|
2022-08-02 10:58:30 -05:00
|
|
|
Digest: &layerDigest,
|
|
|
|
Size: &layerSizeStr,
|
2022-07-12 07:58:04 -05:00
|
|
|
Score: &index,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
imageSize := imageLayersSize + manifestSize + configSize
|
|
|
|
|
|
|
|
index := strings.Index(repo, name)
|
2022-08-02 10:58:30 -05:00
|
|
|
matchesTag := strings.HasPrefix(manifestTag, tag)
|
2022-07-12 07:58:04 -05:00
|
|
|
|
|
|
|
if index != -1 {
|
2022-08-02 10:58:30 -05:00
|
|
|
imageConfigInfo, err := olu.GetImageConfigInfo(repo, manifests[i].Digest)
|
2022-07-19 08:16:15 -05:00
|
|
|
if err != nil {
|
2022-08-02 10:58:30 -05:00
|
|
|
log.Error().Err(err).Msgf("can't retrieve config info for the image %s %s", repo, manifestTag)
|
2022-07-19 08:16:15 -05:00
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-07-12 07:58:04 -05:00
|
|
|
size := strconv.Itoa(int(imageSize))
|
2022-08-02 10:58:30 -05:00
|
|
|
isSigned := olu.CheckManifestSignature(repo, manifests[i].Digest)
|
2022-07-19 08:16:15 -05:00
|
|
|
|
2022-07-12 07:58:04 -05:00
|
|
|
// update matching score
|
|
|
|
score := calculateImageMatchingScore(repo, index, matchesTag)
|
|
|
|
|
2022-07-19 08:16:15 -05:00
|
|
|
lastUpdated := olu.GetImageLastUpdated(imageConfigInfo)
|
|
|
|
os, arch := olu.GetImagePlatform(imageConfigInfo)
|
2022-07-12 07:58:04 -05:00
|
|
|
osArch := &gql_generated.OsArch{
|
|
|
|
Os: &os,
|
|
|
|
Arch: &arch,
|
|
|
|
}
|
|
|
|
|
2022-09-13 09:20:44 -05:00
|
|
|
// get image info from manifest annotation, if not found get from image config labels.
|
|
|
|
annotations := common.GetAnnotations(imageBlobManifest.Annotations, imageConfigInfo.Config.Labels)
|
|
|
|
|
|
|
|
manifestDigest := manifest.Digest.Hex()
|
|
|
|
configDigest := imageBlobManifest.Config.Digest.Hex
|
|
|
|
|
2022-07-12 07:58:04 -05:00
|
|
|
repoPlatforms = append(repoPlatforms, osArch)
|
2022-09-13 09:20:44 -05:00
|
|
|
repoVendors = append(repoVendors, &annotations.Vendor)
|
2022-07-12 07:58:04 -05:00
|
|
|
|
2022-07-22 15:01:38 -05:00
|
|
|
imageSummary := gql_generated.ImageSummary{
|
2022-09-13 09:20:44 -05:00
|
|
|
RepoName: &repo,
|
|
|
|
Tag: &manifestTag,
|
|
|
|
LastUpdated: &lastUpdated,
|
|
|
|
Digest: &manifestDigest,
|
|
|
|
ConfigDigest: &configDigest,
|
|
|
|
IsSigned: &isSigned,
|
|
|
|
Size: &size,
|
|
|
|
Platform: osArch,
|
|
|
|
Vendor: &annotations.Vendor,
|
|
|
|
Score: &score,
|
|
|
|
Description: &annotations.Description,
|
|
|
|
Title: &annotations.Title,
|
|
|
|
Documentation: &annotations.Documentation,
|
|
|
|
Licenses: &annotations.Licenses,
|
|
|
|
Labels: &annotations.Labels,
|
|
|
|
Source: &annotations.Source,
|
2022-07-22 15:01:38 -05:00
|
|
|
}
|
|
|
|
|
2022-09-13 09:20:44 -05:00
|
|
|
if manifest.Digest.String() == lastUpdatedTag.Digest {
|
2022-07-22 15:01:38 -05:00
|
|
|
lastUpdatedImageSummary = imageSummary
|
|
|
|
}
|
|
|
|
|
|
|
|
images = append(images, &imageSummary)
|
2022-07-12 07:58:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-20 04:30:34 -05:00
|
|
|
for blob := range repoBlob2Size {
|
|
|
|
repoSize += repoBlob2Size[blob]
|
2022-07-12 07:58:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if index := strings.Index(repo, name); index != -1 {
|
|
|
|
repoSize := strconv.FormatInt(repoSize, 10)
|
|
|
|
|
|
|
|
repos = append(repos, &gql_generated.RepoSummary{
|
|
|
|
Name: &repo,
|
2022-07-22 15:01:38 -05:00
|
|
|
LastUpdated: &lastUpdatedTag.Timestamp,
|
2022-07-12 07:58:04 -05:00
|
|
|
Size: &repoSize,
|
|
|
|
Platforms: repoPlatforms,
|
|
|
|
Vendors: repoVendors,
|
|
|
|
Score: &index,
|
2022-01-19 10:57:10 -05:00
|
|
|
NewestImage: &lastUpdatedImageSummary,
|
2022-07-12 07:58:04 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(repos, func(i, j int) bool {
|
|
|
|
return *repos[i].Score < *repos[j].Score
|
|
|
|
})
|
|
|
|
|
|
|
|
sort.Slice(images, func(i, j int) bool {
|
|
|
|
return *images[i].Score < *images[j].Score
|
|
|
|
})
|
|
|
|
|
|
|
|
sort.Slice(layers, func(i, j int) bool {
|
|
|
|
return *layers[i].Score < *layers[j].Score
|
|
|
|
})
|
|
|
|
|
|
|
|
return repos, images, layers
|
|
|
|
}
|
|
|
|
|
|
|
|
// calcalculateImageMatchingScore iterated from the index of the matched string in the
|
|
|
|
// artifact name until the beginning of the string or until delimitator "/".
|
|
|
|
// The distance represents the score of the match.
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
// query: image
|
|
|
|
// repos: repo/test/myimage
|
|
|
|
// Score will be 2.
|
|
|
|
func calculateImageMatchingScore(artefactName string, index int, matchesTag bool) int {
|
|
|
|
score := 0
|
|
|
|
|
|
|
|
for index >= 1 {
|
|
|
|
if artefactName[index-1] == '/' {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
index--
|
|
|
|
score++
|
|
|
|
}
|
|
|
|
|
|
|
|
if !matchesTag {
|
|
|
|
score += 10
|
|
|
|
}
|
|
|
|
|
|
|
|
return score
|
|
|
|
}
|
|
|
|
|
2022-01-19 10:57:10 -05:00
|
|
|
func (r *queryResolver) getImageList(store storage.ImageStore, imageName string) (
|
|
|
|
[]*gql_generated.ImageSummary, error,
|
|
|
|
) {
|
|
|
|
results := make([]*gql_generated.ImageSummary, 0)
|
|
|
|
|
|
|
|
repoList, err := store.GetRepositories()
|
|
|
|
if err != nil {
|
|
|
|
r.log.Error().Err(err).Msg("extension api: error extracting repositories list")
|
|
|
|
|
|
|
|
return results, err
|
|
|
|
}
|
|
|
|
|
|
|
|
layoutUtils := common.NewBaseOciLayoutUtils(r.storeController, r.log)
|
|
|
|
|
|
|
|
for _, repo := range repoList {
|
|
|
|
if (imageName != "" && repo == imageName) || imageName == "" {
|
|
|
|
tagsInfo, err := layoutUtils.GetImageTagsWithTimestamp(repo)
|
|
|
|
if err != nil {
|
|
|
|
r.log.Error().Err(err).Msg("extension api: error getting tag timestamp info")
|
|
|
|
|
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(tagsInfo) == 0 {
|
|
|
|
r.log.Info().Str("no tagsinfo found for repo", repo).Msg(" continuing traversing")
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range tagsInfo {
|
|
|
|
// using a loop variable called tag would be reassigned after each iteration, using the same memory address
|
|
|
|
// directly access the value at the current index in the slice as ImageInfo requires pointers to tag fields
|
|
|
|
tag := tagsInfo[i]
|
|
|
|
|
|
|
|
digest := godigest.Digest(tag.Digest)
|
|
|
|
|
|
|
|
manifest, err := layoutUtils.GetImageBlobManifest(repo, digest)
|
|
|
|
if err != nil {
|
|
|
|
r.log.Error().Err(err).Msg("extension api: error reading manifest")
|
|
|
|
|
|
|
|
return results, err
|
|
|
|
}
|
|
|
|
|
2022-09-21 12:53:56 -05:00
|
|
|
imageConfig, err := layoutUtils.GetImageConfigInfo(repo, digest)
|
|
|
|
if err != nil {
|
|
|
|
return results, err
|
|
|
|
}
|
|
|
|
|
|
|
|
imageInfo := BuildImageInfo(repo, tag.Name, digest, manifest, imageConfig)
|
2022-01-19 10:57:10 -05:00
|
|
|
|
|
|
|
results = append(results, imageInfo)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(results) == 0 {
|
|
|
|
r.log.Info().Msg("no repositories found")
|
|
|
|
}
|
|
|
|
|
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
2022-09-21 12:53:56 -05:00
|
|
|
func BuildImageInfo(repo string, tag string, manifestDigest godigest.Digest,
|
|
|
|
manifest v1.Manifest, imageConfig ispec.Image,
|
2022-01-19 10:57:10 -05:00
|
|
|
) *gql_generated.ImageSummary {
|
|
|
|
layers := []*gql_generated.LayerSummary{}
|
|
|
|
size := int64(0)
|
|
|
|
|
2022-09-21 12:53:56 -05:00
|
|
|
log := log.NewLogger("debug", "")
|
2022-01-19 10:57:10 -05:00
|
|
|
|
2022-09-21 12:53:56 -05:00
|
|
|
allHistory := []*gql_generated.LayerHistory{}
|
|
|
|
|
|
|
|
formattedManifestDigest := manifestDigest.Hex()
|
|
|
|
|
|
|
|
history := imageConfig.History
|
|
|
|
if len(history) == 0 {
|
|
|
|
for _, layer := range manifest.Layers {
|
|
|
|
size += layer.Size
|
|
|
|
digest := layer.Digest.Hex
|
|
|
|
layerSize := strconv.FormatInt(layer.Size, 10)
|
|
|
|
|
|
|
|
layer := &gql_generated.LayerSummary{
|
2022-01-19 10:57:10 -05:00
|
|
|
Size: &layerSize,
|
|
|
|
Digest: &digest,
|
2022-09-21 12:53:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
layers = append(
|
|
|
|
layers,
|
|
|
|
layer,
|
|
|
|
)
|
|
|
|
|
|
|
|
allHistory = append(allHistory, &gql_generated.LayerHistory{
|
|
|
|
Layer: layer,
|
|
|
|
HistoryDescription: &gql_generated.HistoryDescription{},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
formattedSize := strconv.FormatInt(size, 10)
|
|
|
|
|
|
|
|
imageInfo := &gql_generated.ImageSummary{
|
|
|
|
RepoName: &repo,
|
|
|
|
Tag: &tag,
|
|
|
|
Digest: &formattedManifestDigest,
|
|
|
|
ConfigDigest: &manifest.Config.Digest.Hex,
|
|
|
|
Size: &formattedSize,
|
|
|
|
Layers: layers,
|
|
|
|
History: []*gql_generated.LayerHistory{},
|
|
|
|
}
|
|
|
|
|
|
|
|
return imageInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
// iterator over manifest layers
|
|
|
|
var layersIterator int
|
|
|
|
// since we are appending pointers, it is important to iterate with an index over slice
|
|
|
|
for i := range history {
|
|
|
|
allHistory = append(allHistory, &gql_generated.LayerHistory{
|
|
|
|
HistoryDescription: &gql_generated.HistoryDescription{
|
|
|
|
Created: history[i].Created,
|
|
|
|
CreatedBy: &history[i].CreatedBy,
|
|
|
|
Author: &history[i].Author,
|
|
|
|
Comment: &history[i].Comment,
|
|
|
|
EmptyLayer: &history[i].EmptyLayer,
|
2022-01-19 10:57:10 -05:00
|
|
|
},
|
2022-09-21 12:53:56 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
if history[i].EmptyLayer {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if layersIterator+1 > len(manifest.Layers) {
|
|
|
|
formattedSize := strconv.FormatInt(size, 10)
|
|
|
|
|
|
|
|
log.Error().Err(ErrBadLayerCount).Msg("error on creating layer history for ImageSummary")
|
|
|
|
|
|
|
|
return &gql_generated.ImageSummary{
|
|
|
|
RepoName: &repo,
|
|
|
|
Tag: &tag,
|
|
|
|
Digest: &formattedManifestDigest,
|
|
|
|
ConfigDigest: &manifest.Config.Digest.Hex,
|
|
|
|
Size: &formattedSize,
|
|
|
|
Layers: layers,
|
|
|
|
History: allHistory,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size += manifest.Layers[layersIterator].Size
|
|
|
|
digest := manifest.Layers[layersIterator].Digest.Hex
|
|
|
|
layerSize := strconv.FormatInt(manifest.Layers[layersIterator].Size, 10)
|
|
|
|
|
|
|
|
layer := &gql_generated.LayerSummary{
|
|
|
|
Size: &layerSize,
|
|
|
|
Digest: &digest,
|
|
|
|
}
|
|
|
|
|
|
|
|
layers = append(
|
|
|
|
layers,
|
|
|
|
layer,
|
2022-01-19 10:57:10 -05:00
|
|
|
)
|
2022-09-21 12:53:56 -05:00
|
|
|
|
|
|
|
allHistory[i].Layer = layer
|
|
|
|
|
|
|
|
layersIterator++
|
2022-01-19 10:57:10 -05:00
|
|
|
}
|
2020-08-19 01:53:04 -05:00
|
|
|
|
2022-01-19 10:57:10 -05:00
|
|
|
formattedSize := strconv.FormatInt(size, 10)
|
2020-08-19 01:53:04 -05:00
|
|
|
|
2022-01-19 10:57:10 -05:00
|
|
|
imageInfo := &gql_generated.ImageSummary{
|
|
|
|
RepoName: &repo,
|
|
|
|
Tag: &tag,
|
2022-09-21 12:53:56 -05:00
|
|
|
Digest: &formattedManifestDigest,
|
2022-01-19 10:57:10 -05:00
|
|
|
ConfigDigest: &manifest.Config.Digest.Hex,
|
|
|
|
Size: &formattedSize,
|
|
|
|
Layers: layers,
|
2022-09-21 12:53:56 -05:00
|
|
|
History: allHistory,
|
2020-09-04 15:16:15 -05:00
|
|
|
}
|
|
|
|
|
2022-01-19 10:57:10 -05:00
|
|
|
return imageInfo
|
2020-08-19 01:53:04 -05:00
|
|
|
}
|
2022-08-16 03:57:09 -05:00
|
|
|
|
|
|
|
// returns either a user has or not rights on 'repository'.
|
|
|
|
func matchesRepo(globPatterns map[string]bool, repository string) bool {
|
|
|
|
var longestMatchedPattern string
|
|
|
|
|
|
|
|
// because of the longest path matching rule, we need to check all patterns from config
|
|
|
|
for pattern := range globPatterns {
|
|
|
|
matched, err := glob.Match(pattern, repository)
|
|
|
|
if err == nil {
|
|
|
|
if matched && len(pattern) > len(longestMatchedPattern) {
|
|
|
|
longestMatchedPattern = pattern
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
allowed := globPatterns[longestMatchedPattern]
|
|
|
|
|
|
|
|
return allowed
|
|
|
|
}
|
|
|
|
|
|
|
|
// get passed context from authzHandler and filter out repos based on permissions.
|
|
|
|
func userAvailableRepos(ctx context.Context, repoList []string) ([]string, error) {
|
|
|
|
var availableRepos []string
|
|
|
|
|
|
|
|
authzCtxKey := localCtx.GetContextKey()
|
|
|
|
if authCtx := ctx.Value(authzCtxKey); authCtx != nil {
|
|
|
|
acCtx, ok := authCtx.(localCtx.AccessControlContext)
|
|
|
|
if !ok {
|
|
|
|
err := ErrBadCtxFormat
|
|
|
|
|
|
|
|
return []string{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, r := range repoList {
|
|
|
|
if acCtx.IsAdmin || matchesRepo(acCtx.GlobPatterns, r) {
|
|
|
|
availableRepos = append(availableRepos, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
availableRepos = repoList
|
|
|
|
}
|
|
|
|
|
|
|
|
return availableRepos, nil
|
|
|
|
}
|