Skip to content

Commit

Permalink
fix(convert): now returned annotations for an index will fallback to …
Browse files Browse the repository at this point in the history
…annotations from a random manifest if the annotations field is not present on the index manifest

Signed-off-by: Laurentiu Niculae <[email protected]>
  • Loading branch information
laurentiuNiculae committed Aug 3, 2023
1 parent 77149aa commit 4d1d2bd
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pkg/cli/cve_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func searchCve(searchConfig searchConfig) error {
}

for _, searcher := range searchers {
// there can be CVE DB readyness issues on the server side
// there can be CVE DB readiness issues on the server side
// we need a retry mechanism for that specific type of errors
maxAttempts := 20

Expand Down
55 changes: 55 additions & 0 deletions pkg/extensions/search/convert/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,58 @@ func GetAnnotations(annotations, labels map[string]string) ImageAnnotations {
Authors: authors,
}
}

func GetIndexAnnotations(indexAnnotations, manifestAnnotations, manifestLabels map[string]string) ImageAnnotations {
annotationsFromManifest := GetAnnotations(manifestAnnotations, manifestLabels)

description := GetDescription(indexAnnotations)
if description == "" {
description = annotationsFromManifest.Description
}

title := GetTitle(indexAnnotations)
if title == "" {
title = annotationsFromManifest.Title
}

documentation := GetDocumentation(indexAnnotations)
if documentation == "" {
documentation = annotationsFromManifest.Documentation
}

source := GetSource(indexAnnotations)
if source == "" {
source = annotationsFromManifest.Source
}

licenses := GetLicenses(indexAnnotations)
if licenses == "" {
licenses = annotationsFromManifest.Licenses
}

categories := GetCategories(indexAnnotations)
if categories == "" {
categories = annotationsFromManifest.Labels
}

vendor := GetVendor(indexAnnotations)
if vendor == "" {
vendor = annotationsFromManifest.Vendor
}

authors := GetAuthors(indexAnnotations)
if authors == "" {
authors = annotationsFromManifest.Authors
}

return ImageAnnotations{
Description: description,
Title: title,
Documentation: documentation,
Source: source,
Licenses: licenses,
Labels: categories,
Vendor: vendor,
Authors: authors,
}
}
32 changes: 32 additions & 0 deletions pkg/extensions/search/convert/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -770,3 +770,35 @@ func TestPaginatedConvert(t *testing.T) {
So(pageInfo.ItemCount, ShouldEqual, 1)
})
}

func TestGetOneManifestAnnotations(t *testing.T) {
Convey("GetOneManifestAnnotations errors", t, func() {
manifestAnnotations, configLabels := convert.GetOneManifestAnnotations(
ispec.Index{Manifests: []ispec.Descriptor{
{Digest: "bad-manifest"}, {Digest: "dig2"},
}},
map[string]mTypes.ManifestMetadata{
"bad-manifest": {
ManifestBlob: []byte(`bad`),
ConfigBlob: []byte("{}"),
},
},
)
So(manifestAnnotations, ShouldBeEmpty)
So(configLabels, ShouldBeEmpty)

manifestAnnotations, configLabels = convert.GetOneManifestAnnotations(
ispec.Index{Manifests: []ispec.Descriptor{
{Digest: "bad-config"},
}},
map[string]mTypes.ManifestMetadata{
"bad-config": {
ManifestBlob: []byte("{}"),
ConfigBlob: []byte("bad"),
},
},
)
So(manifestAnnotations, ShouldBeEmpty)
So(configLabels, ShouldBeEmpty)
})
}
29 changes: 27 additions & 2 deletions pkg/extensions/search/convert/metadb.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,11 @@ func ImageIndex2ImageSummary(ctx context.Context, repo, tag string, indexDigest

indexSize = strconv.FormatInt(totalIndexSize, 10)

annotations := GetAnnotations(indexContent.Annotations, map[string]string{})

signaturesInfo := GetSignaturesInfo(isSigned, repoMeta, indexDigest)

manifestAnnotations, configLabels := GetOneManifestAnnotations(indexContent, manifestMetaMap)
annotations := GetIndexAnnotations(indexContent.Annotations, manifestAnnotations, configLabels)

indexSummary := gql_generated.ImageSummary{
RepoName: &repo,
Tag: &tag,
Expand Down Expand Up @@ -301,6 +302,30 @@ func ImageIndex2ImageSummary(ctx context.Context, repo, tag string, indexDigest
return &indexSummary, indexBlobs, nil
}

func GetOneManifestAnnotations(indexContent ispec.Index, manifestMetaMap map[string]mTypes.ManifestMetadata,
) (map[string]string, map[string]string) {
if len(manifestMetaMap) == 0 || len(indexContent.Manifests) == 0 {
return map[string]string{}, map[string]string{}
}

manifestMeta := manifestMetaMap[indexContent.Manifests[0].Digest.String()]

manifestContent := ispec.Manifest{}
configContent := ispec.Image{}

err := json.Unmarshal(manifestMeta.ManifestBlob, &manifestContent)
if err != nil {
return map[string]string{}, map[string]string{}
}

err = json.Unmarshal(manifestMeta.ConfigBlob, &configContent)
if err != nil {
return map[string]string{}, map[string]string{}
}

return manifestContent.Annotations, configContent.Config.Labels
}

func ImageManifest2ImageSummary(ctx context.Context, repo, tag string, digest godigest.Digest, skipCVE bool,
repoMeta mTypes.RepoMetadata, manifestMeta mTypes.ManifestMetadata, cveInfo cveinfo.CveInfo,
) (*gql_generated.ImageSummary, map[string]int64, error) {
Expand Down

0 comments on commit 4d1d2bd

Please sign in to comment.