Skip to content

Commit

Permalink
refactor(proto): change proto structs
Browse files Browse the repository at this point in the history
Signed-off-by: Laurentiu Niculae <niculae.laurentiu1@gmail.com>
  • Loading branch information
laurentiuNiculae committed Oct 20, 2023
1 parent 0599330 commit d35f06e
Show file tree
Hide file tree
Showing 23 changed files with 1,380 additions and 855 deletions.
2 changes: 1 addition & 1 deletion pkg/cli/client/cve_cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,7 @@ func getMockCveScanner(metaDB mTypes.MetaDB) cveinfo.Scanner {
return false, err
}

for _, imageLayer := range manifestData.Manifests[0].Layers {
for _, imageLayer := range manifestData.Manifests[0].Manifest.Layers {
switch imageLayer.MediaType {
case ispec.MediaTypeImageLayerGzip, ispec.MediaTypeImageLayer, string(regTypes.DockerLayer):

Expand Down
42 changes: 22 additions & 20 deletions pkg/extensions/search/convert/metadb.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ func ImageIndex2ImageSummary(ctx context.Context, repo, tag, digest string, repo
indexLastUpdated = *imageManifestSummary.LastUpdated
}

annotations := GetAnnotations(imageManifest.Annotations, imageManifest.ConfigContent.Config.Labels)
annotations := GetAnnotations(imageManifest.Manifest.Annotations, imageManifest.Config.Config.Labels)

if manifestAnnotations == nil {
manifestAnnotations = &annotations
Expand Down Expand Up @@ -586,7 +586,7 @@ func FullImageIndex2ImageSummary(ctx context.Context, imageData mTypes.FullImage
indexLastUpdated = *imageManifestSummary.LastUpdated
}

annotations := GetAnnotations(imageManifest.Annotations, imageManifest.ConfigContent.Config.Labels)
annotations := GetAnnotations(imageManifest.Manifest.Annotations, imageManifest.Config.Config.Labels)
if manifestAnnotations == nil {
manifestAnnotations = &annotations
}
Expand Down Expand Up @@ -636,28 +636,29 @@ func ImageManifest2ImageSummary(ctx context.Context, repo, tag string, repoMeta

var (
repoName = repo
configDigest = manifest.Config.Digest.String()
configSize = manifest.Config.Size
configDigest = manifest.Manifest.Config.Digest.String()
configSize = manifest.Manifest.Config.Size
manifestDigest = manifest.Digest.String()
manifestSize = manifest.Size
mediaType = manifest.MediaType
mediaType = manifest.Manifest.MediaType
artifactType = zcommon.GetManifestArtifactType(imageData.Manifests[0].Manifest)
platform = getPlatform(manifest.ConfigContent.Platform)
imageLastUpdated = zcommon.GetImageLastUpdated(manifest.ConfigContent)
platform = getPlatform(manifest.Config.Platform)
imageLastUpdated = zcommon.GetImageLastUpdated(manifest.Config)
downloadCount = repoMeta.Statistics[manifest.Digest.String()].DownloadCount
isSigned = isImageSigned(repoMeta.Signatures[manifest.Digest.String()])
)

imageSize, imageBlobsMap := getImageBlobsInfo(manifestDigest, manifestSize, configDigest, configSize, manifest.Layers)
imageSize, imageBlobsMap := getImageBlobsInfo(manifestDigest, manifestSize, configDigest, configSize,
manifest.Manifest.Layers)
imageSizeStr := strconv.FormatInt(imageSize, 10)
annotations := GetAnnotations(manifest.Annotations, manifest.ConfigContent.Config.Labels)
annotations := GetAnnotations(manifest.Manifest.Annotations, manifest.Config.Config.Labels)

authors := annotations.Authors
if authors == "" {
authors = manifest.ConfigContent.Author
authors = manifest.Config.Author
}

historyEntries, err := getAllHistory(manifest.Manifest, manifest.ConfigContent)
historyEntries, err := getAllHistory(manifest.Manifest, manifest.Config)
if err != nil {
graphql.AddError(ctx, gqlerror.Errorf("error generating history on tag %s in repo %s: "+
"manifest digest: %s, error: %s", tag, repo, manifest.Digest, err.Error()))
Expand Down Expand Up @@ -712,28 +713,29 @@ func FullImageManifest2ImageSummary(ctx context.Context, imageData mTypes.FullIm
var (
repoName = imageData.Repo
tag = imageData.Tag
configDigest = manifest.Config.Digest.String()
configSize = manifest.Config.Size
configDigest = manifest.Manifest.Config.Digest.String()
configSize = manifest.Manifest.Config.Size
manifestDigest = manifest.Digest.String()
manifestSize = manifest.Size
mediaType = manifest.MediaType
mediaType = manifest.Manifest.MediaType
artifactType = zcommon.GetManifestArtifactType(imageData.Manifests[0].Manifest)
platform = getPlatform(manifest.ConfigContent.Platform)
imageLastUpdated = zcommon.GetImageLastUpdated(manifest.ConfigContent)
platform = getPlatform(manifest.Config.Platform)
imageLastUpdated = zcommon.GetImageLastUpdated(manifest.Config)
downloadCount = imageData.Statistics.DownloadCount
isSigned = isImageSigned(imageData.Signatures)
)

imageSize, imageBlobsMap := getImageBlobsInfo(manifestDigest, manifestSize, configDigest, configSize, manifest.Layers)
imageSize, imageBlobsMap := getImageBlobsInfo(manifestDigest, manifestSize, configDigest, configSize,
manifest.Manifest.Layers)
imageSizeStr := strconv.FormatInt(imageSize, 10)
annotations := GetAnnotations(manifest.Annotations, manifest.ConfigContent.Config.Labels)
annotations := GetAnnotations(manifest.Manifest.Annotations, manifest.Config.Config.Labels)

authors := annotations.Authors
if authors == "" {
authors = manifest.ConfigContent.Author
authors = manifest.Config.Author
}

historyEntries, err := getAllHistory(manifest.Manifest, manifest.ConfigContent)
historyEntries, err := getAllHistory(manifest.Manifest, manifest.Config)
if err != nil {
graphql.AddError(ctx, gqlerror.Errorf("error generating history on tag %s in repo %s: "+
"manifest digest: %s, error: %s", tag, repoName, manifest.Digest, err.Error()))
Expand Down
6 changes: 3 additions & 3 deletions pkg/extensions/search/cve/cve.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,12 +310,12 @@ func getConfigAndDigest(metaDB mTypes.MetaDB, manifestDigestStr string) (ispec.I
return ispec.Image{}, "", err
}

// we'll fail the execution if the config is not compatibe with ispec.Image because we can't scan this type of images.
if manifestData.Manifests[0].Config.MediaType != ispec.MediaTypeImageConfig {
// we'll fail the execution if the config is not compatible with ispec.Image because we can't scan this type of images.
if manifestData.Manifests[0].Manifest.Config.MediaType != ispec.MediaTypeImageConfig {
return ispec.Image{}, "", zerr.ErrWrongMediaType
}

Check warning on line 316 in pkg/extensions/search/cve/cve.go

View check run for this annotation

Codecov / codecov/patch

pkg/extensions/search/cve/cve.go#L315-L316

Added lines #L315 - L316 were not covered by tests

return manifestData.Manifests[0].ConfigContent, manifestDigest, err
return manifestData.Manifests[0].Config, manifestDigest, err
}

func filterCVEList(cveMap map[string]cvemodel.CVE, searchedCVE string, pageFinder *CvePageFinder) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/extensions/search/cve/cve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1082,7 +1082,7 @@ func TestCVEStruct(t *testing.T) { //nolint:gocyclo
return false, err
}

for _, imageLayer := range manifestData.Manifests[0].Layers {
for _, imageLayer := range manifestData.Manifests[0].Manifest.Layers {
switch imageLayer.MediaType {
case ispec.MediaTypeImageLayerGzip, ispec.MediaTypeImageLayer, string(regTypes.DockerLayer):

Expand Down
2 changes: 1 addition & 1 deletion pkg/extensions/search/cve/scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ func TestScanGeneratorWithMockedData(t *testing.T) { //nolint: gocyclo
return false, err
}

for _, imageLayer := range manifestData.Manifests[0].Layers {
for _, imageLayer := range manifestData.Manifests[0].Manifest.Layers {
switch imageLayer.MediaType {
case ispec.MediaTypeImageLayerGzip, ispec.MediaTypeImageLayer, string(regTypes.DockerLayer):

Expand Down
6 changes: 3 additions & 3 deletions pkg/extensions/search/cve/trivy/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ func (scanner Scanner) isManifestScanable(digestStr string) (bool, error) {
return false, zerr.ErrWrongMediaType
}

Check warning on line 248 in pkg/extensions/search/cve/trivy/scanner.go

View check run for this annotation

Codecov / codecov/patch

pkg/extensions/search/cve/trivy/scanner.go#L247-L248

Added lines #L247 - L248 were not covered by tests

for _, imageLayer := range manifestData.Manifests[0].Layers {
for _, imageLayer := range manifestData.Manifests[0].Manifest.Layers {
switch imageLayer.MediaType {
case ispec.MediaTypeImageLayerGzip, ispec.MediaTypeImageLayer, string(regTypes.DockerLayer):
continue
Expand All @@ -264,11 +264,11 @@ func (scanner Scanner) isManifestDataScannable(manifestData mTypes.ManifestData)
return true, nil
}

Check warning on line 265 in pkg/extensions/search/cve/trivy/scanner.go

View check run for this annotation

Codecov / codecov/patch

pkg/extensions/search/cve/trivy/scanner.go#L264-L265

Added lines #L264 - L265 were not covered by tests

if manifestData.MediaType != ispec.MediaTypeImageManifest {
if manifestData.Manifest.MediaType != ispec.MediaTypeImageManifest {
return false, zerr.ErrScanNotSupported
}

for _, imageLayer := range manifestData.Layers {
for _, imageLayer := range manifestData.Manifest.Layers {
switch imageLayer.MediaType {
case ispec.MediaTypeImageLayerGzip, ispec.MediaTypeImageLayer, string(regTypes.DockerLayer):
continue
Expand Down
76 changes: 38 additions & 38 deletions pkg/extensions/search/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ func FilterByDigest(digest string) mTypes.FilterFunc {

// Check the image config matches the search digest
// This is a blob with mediaType application/vnd.oci.image.config.v1+json
if strings.Contains(manifest.Config.Digest.String(), lookupDigest) {
if strings.Contains(manifest.Manifest.Config.Digest.String(), lookupDigest) {
contains = true
}

// Check to see if the individual layers in the oci image manifest match the digest
// These are blobs with mediaType application/vnd.oci.image.layer.v1.tar+gzip
for _, layer := range manifest.Layers {
for _, layer := range manifest.Manifest.Layers {
if strings.Contains(layer.Digest.String(), lookupDigest) {
contains = true
}
Expand All @@ -118,10 +118,10 @@ func getImageListForDigest(ctx context.Context, digest string, metaDB mTypes.Met
}

pageInput := pagination.PageInput{
Limit: safeDereferencing(requestedPage.Limit, 0),
Offset: safeDereferencing(requestedPage.Offset, 0),
Limit: deref(requestedPage.Limit, 0),
Offset: deref(requestedPage.Offset, 0),
SortBy: pagination.SortCriteria(
safeDereferencing(requestedPage.SortBy, gql_generated.SortCriteriaRelevance),
deref(requestedPage.SortBy, gql_generated.SortCriteriaRelevance),
),
}

Expand Down Expand Up @@ -198,10 +198,10 @@ func getCVEListForImage(
}

pageInput := cvemodel.PageInput{
Limit: safeDereferencing(requestedPage.Limit, 0),
Offset: safeDereferencing(requestedPage.Offset, 0),
Limit: deref(requestedPage.Limit, 0),
Offset: deref(requestedPage.Offset, 0),
SortBy: cvemodel.SortCriteria(
safeDereferencing(requestedPage.SortBy, gql_generated.SortCriteriaSeverity),
deref(requestedPage.SortBy, gql_generated.SortCriteriaSeverity),
),
}

Expand Down Expand Up @@ -365,10 +365,10 @@ func getImageListForCVE(

// Actual page requested by user
pageInput := pagination.PageInput{
Limit: safeDereferencing(requestedPage.Limit, 0),
Offset: safeDereferencing(requestedPage.Offset, 0),
Limit: deref(requestedPage.Limit, 0),
Offset: deref(requestedPage.Offset, 0),
SortBy: pagination.SortCriteria(
safeDereferencing(requestedPage.SortBy, gql_generated.SortCriteriaUpdateTime),
deref(requestedPage.SortBy, gql_generated.SortCriteriaUpdateTime),
),
}

Expand Down Expand Up @@ -438,10 +438,10 @@ func getImageListWithCVEFixed(

// Actual page requested by user
pageInput := pagination.PageInput{
Limit: safeDereferencing(requestedPage.Limit, 0),
Offset: safeDereferencing(requestedPage.Offset, 0),
Limit: deref(requestedPage.Limit, 0),
Offset: deref(requestedPage.Offset, 0),
SortBy: pagination.SortCriteria(
safeDereferencing(requestedPage.SortBy, gql_generated.SortCriteriaUpdateTime),
deref(requestedPage.SortBy, gql_generated.SortCriteriaUpdateTime),
),
}

Expand Down Expand Up @@ -484,10 +484,10 @@ func repoListWithNewestImage(
}

pageInput := pagination.PageInput{
Limit: safeDereferencing(requestedPage.Limit, 0),
Offset: safeDereferencing(requestedPage.Offset, 0),
Limit: deref(requestedPage.Limit, 0),
Offset: deref(requestedPage.Offset, 0),
SortBy: pagination.SortCriteria(
safeDereferencing(requestedPage.SortBy, gql_generated.SortCriteriaUpdateTime),
deref(requestedPage.SortBy, gql_generated.SortCriteriaUpdateTime),
),
}

Expand Down Expand Up @@ -576,10 +576,10 @@ func getFilteredPaginatedRepos(
}

pageInput := pagination.PageInput{
Limit: safeDereferencing(requestedPage.Limit, 0),
Offset: safeDereferencing(requestedPage.Offset, 0),
Limit: deref(requestedPage.Limit, 0),
Offset: deref(requestedPage.Offset, 0),
SortBy: pagination.SortCriteria(
safeDereferencing(requestedPage.SortBy, gql_generated.SortCriteriaUpdateTime),
deref(requestedPage.SortBy, gql_generated.SortCriteriaUpdateTime),
),
}

Expand Down Expand Up @@ -638,10 +638,10 @@ func globalSearch(ctx context.Context, query string, metaDB mTypes.MetaDB, filte
}

pageInput := pagination.PageInput{
Limit: safeDereferencing(requestedPage.Limit, 0),
Offset: safeDereferencing(requestedPage.Offset, 0),
Limit: deref(requestedPage.Limit, 0),
Offset: deref(requestedPage.Offset, 0),
SortBy: pagination.SortCriteria(
safeDereferencing(requestedPage.SortBy, gql_generated.SortCriteriaRelevance),
deref(requestedPage.SortBy, gql_generated.SortCriteriaRelevance),
),
}

Expand Down Expand Up @@ -677,10 +677,10 @@ func globalSearch(ctx context.Context, query string, metaDB mTypes.MetaDB, filte
}

pageInput := pagination.PageInput{
Limit: safeDereferencing(requestedPage.Limit, 0),
Offset: safeDereferencing(requestedPage.Offset, 0),
Limit: deref(requestedPage.Limit, 0),
Offset: deref(requestedPage.Offset, 0),
SortBy: pagination.SortCriteria(
safeDereferencing(requestedPage.SortBy, gql_generated.SortCriteriaRelevance),
deref(requestedPage.SortBy, gql_generated.SortCriteriaRelevance),
),
}

Expand Down Expand Up @@ -721,10 +721,10 @@ func derivedImageList(ctx context.Context, image string, digest *string, metaDB
}

pageInput := pagination.PageInput{
Limit: safeDereferencing(requestedPage.Limit, 0),
Offset: safeDereferencing(requestedPage.Offset, 0),
Limit: deref(requestedPage.Limit, 0),
Offset: deref(requestedPage.Offset, 0),
SortBy: pagination.SortCriteria(
safeDereferencing(requestedPage.SortBy, gql_generated.SortCriteriaUpdateTime),
deref(requestedPage.SortBy, gql_generated.SortCriteriaUpdateTime),
),
}

Expand Down Expand Up @@ -785,7 +785,7 @@ func filterDerivedImages(image *gql_generated.ImageSummary) mTypes.FilterFunc {
imageLayers := image.Manifests[i].Layers

addImageToList = false
layers := imageManifest.Layers
layers := imageManifest.Manifest.Layers

sameLayer := 0

Expand Down Expand Up @@ -821,10 +821,10 @@ func baseImageList(ctx context.Context, image string, digest *string, metaDB mTy
}

pageInput := pagination.PageInput{
Limit: safeDereferencing(requestedPage.Limit, 0),
Offset: safeDereferencing(requestedPage.Offset, 0),
Limit: deref(requestedPage.Limit, 0),
Offset: deref(requestedPage.Offset, 0),
SortBy: pagination.SortCriteria(
safeDereferencing(requestedPage.SortBy, gql_generated.SortCriteriaUpdateTime),
deref(requestedPage.SortBy, gql_generated.SortCriteriaUpdateTime),
),
}

Expand Down Expand Up @@ -886,7 +886,7 @@ func filterBaseImages(image *gql_generated.ImageSummary) mTypes.FilterFunc {

addImageToList = true

for _, l := range manifest.Layers {
for _, l := range manifest.Manifest.Layers {
foundLayer := false

for _, k := range image.Manifests[i].Layers {
Expand Down Expand Up @@ -1096,7 +1096,7 @@ func (p timeSlice) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
}

func safeDereferencing[T any](pointer *T, defaultVal T) T {
func deref[T any](pointer *T, defaultVal T) T {
if pointer != nil {
return *pointer
}
Expand All @@ -1120,10 +1120,10 @@ func getImageList(ctx context.Context, repo string, metaDB mTypes.MetaDB, cveInf
}

pageInput := pagination.PageInput{
Limit: safeDereferencing(requestedPage.Limit, 0),
Offset: safeDereferencing(requestedPage.Offset, 0),
Limit: deref(requestedPage.Limit, 0),
Offset: deref(requestedPage.Offset, 0),
SortBy: pagination.SortCriteria(
safeDereferencing(requestedPage.SortBy, gql_generated.SortCriteriaRelevance),
deref(requestedPage.SortBy, gql_generated.SortCriteriaRelevance),
),
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/extensions/search/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2065,7 +2065,7 @@ func TestMockedBaseImageList(t *testing.T) {
})
}

func TestExpandedRepoInfo(t *testing.T) {
func TestExpandedRepoInfoErrors(t *testing.T) {
log := log.NewLogger("debug", "")

Convey("Access error", t, func() {
Expand Down
Loading

0 comments on commit d35f06e

Please sign in to comment.