fix: removed duplicate structures from service.go and moved them to pkg/common (#1436)

Signed-off-by: Ana-Roberta Lisca <ana.kagome@yahoo.com>
This commit is contained in:
Lisca Ana-Roberta
2023-05-25 21:27:49 +03:00
committed by GitHub
parent 4970f8814d
commit 6a7035c599
18 changed files with 707 additions and 721 deletions
+18 -18
View File
@@ -217,6 +217,8 @@ func (p *requestsPool) doJob(ctx context.Context, job *httpJob) {
p.outputCh <- stringResult{"", err}
}
verbose := *job.config.verbose
switch header.Get("Content-Type") {
case ispec.MediaTypeImageManifest:
image, err := fetchImageManifestStruct(ctx, job)
@@ -230,7 +232,7 @@ func (p *requestsPool) doJob(ctx context.Context, job *httpJob) {
}
platformStr := getPlatformStr(image.Manifests[0].Platform)
str, err := image.string(*job.config.outputFormat, len(job.imageName), len(job.tagName), len(platformStr))
str, err := image.string(*job.config.outputFormat, len(job.imageName), len(job.tagName), len(platformStr), verbose)
if err != nil {
if isContextDone(ctx) {
return
@@ -258,7 +260,7 @@ func (p *requestsPool) doJob(ctx context.Context, job *httpJob) {
platformStr := getPlatformStr(image.Manifests[0].Platform)
str, err := image.string(*job.config.outputFormat, len(job.imageName), len(job.tagName), len(platformStr))
str, err := image.string(*job.config.outputFormat, len(job.imageName), len(job.tagName), len(platformStr), verbose)
if err != nil {
if isContextDone(ctx) {
return
@@ -300,7 +302,7 @@ func fetchImageIndexStruct(ctx context.Context, job *httpJob) (*imageStruct, err
imageSize := indexSize
manifestList := make([]manifestStruct, 0, len(indexContent.Manifests))
manifestList := make([]common.ManifestSummary, 0, len(indexContent.Manifests))
for _, manifestDescriptor := range indexContent.Manifests {
manifest, err := fetchManifestStruct(ctx, job.imageName, manifestDescriptor.Digest.String(),
@@ -312,7 +314,7 @@ func fetchImageIndexStruct(ctx context.Context, job *httpJob) (*imageStruct, err
imageSize += int64(atoiWithDefault(manifest.Size, 0))
if manifestDescriptor.Platform != nil {
manifest.Platform = platform{
manifest.Platform = common.Platform{
Os: manifestDescriptor.Platform.OS,
Arch: manifestDescriptor.Platform.Architecture,
Variant: manifestDescriptor.Platform.Variant,
@@ -333,7 +335,6 @@ func fetchImageIndexStruct(ctx context.Context, job *httpJob) (*imageStruct, err
Manifests: manifestList,
Size: strconv.FormatInt(imageSize, 10),
IsSigned: isIndexSigned,
verbose: *job.config.verbose,
}, nil
}
@@ -357,18 +358,17 @@ func fetchImageManifestStruct(ctx context.Context, job *httpJob) (*imageStruct,
Tag: job.tagName,
Digest: manifest.Digest,
MediaType: ispec.MediaTypeImageManifest,
Manifests: []manifestStruct{
Manifests: []common.ManifestSummary{
manifest,
},
Size: manifest.Size,
IsSigned: manifest.IsSigned,
verbose: *job.config.verbose,
}, nil
}
func fetchManifestStruct(ctx context.Context, repo, manifestReference string, searchConf searchConfig,
username, password string,
) (manifestStruct, error) {
) (common.ManifestSummary, error) {
manifestResp := ispec.Manifest{}
URL := fmt.Sprintf("%s/v2/%s/manifests/%s",
@@ -378,10 +378,10 @@ func fetchManifestStruct(ctx context.Context, repo, manifestReference string, se
*searchConf.verifyTLS, *searchConf.debug, &manifestResp, searchConf.resultWriter)
if err != nil {
if isContextDone(ctx) {
return manifestStruct{}, context.Canceled
return common.ManifestSummary{}, context.Canceled
}
return manifestStruct{}, err
return common.ManifestSummary{}, err
}
manifestDigest := header.Get("docker-content-digest")
@@ -390,10 +390,10 @@ func fetchManifestStruct(ctx context.Context, repo, manifestReference string, se
configContent, err := fetchConfig(ctx, repo, configDigest, searchConf, username, password)
if err != nil {
if isContextDone(ctx) {
return manifestStruct{}, context.Canceled
return common.ManifestSummary{}, context.Canceled
}
return manifestStruct{}, err
return common.ManifestSummary{}, err
}
opSys := ""
@@ -420,7 +420,7 @@ func fetchManifestStruct(ctx context.Context, repo, manifestReference string, se
manifestSize, err := strconv.ParseInt(header.Get("Content-Length"), 10, 64)
if err != nil {
return manifestStruct{}, err
return common.ManifestSummary{}, err
}
var imageSize int64
@@ -428,15 +428,15 @@ func fetchManifestStruct(ctx context.Context, repo, manifestReference string, se
imageSize += manifestResp.Config.Size
imageSize += manifestSize
layers := []layer{}
layers := []common.LayerSummary{}
for _, entry := range manifestResp.Layers {
imageSize += entry.Size
layers = append(
layers,
layer{
Size: entry.Size,
common.LayerSummary{
Size: fmt.Sprintf("%v", entry.Size),
Digest: entry.Digest.String(),
},
)
@@ -445,11 +445,11 @@ func fetchManifestStruct(ctx context.Context, repo, manifestReference string, se
isSigned := isCosignSigned(ctx, repo, manifestDigest, searchConf, username, password) ||
isNotationSigned(ctx, repo, manifestDigest, searchConf, username, password)
return manifestStruct{
return common.ManifestSummary{
ConfigDigest: configDigest,
Digest: manifestDigest,
Layers: layers,
Platform: platform{Os: opSys, Arch: arch, Variant: variant},
Platform: common.Platform{Os: opSys, Arch: arch, Variant: variant},
Size: strconv.FormatInt(imageSize, 10),
IsSigned: isSigned,
}, nil
+1 -1
View File
@@ -25,7 +25,7 @@ type schemaList struct {
} `json:"queryType"` //nolint:tagliatelle // graphQL schema
} `json:"__schema"` //nolint:tagliatelle // graphQL schema
} `json:"data"`
Errors []common.ErrorGraphQL `json:"errors"`
Errors []common.ErrorGQL `json:"errors"`
}
func containsGQLQuery(queryList []field, query string) bool {
+80 -66
View File
@@ -29,6 +29,7 @@ import (
zotErrors "zotregistry.io/zot/errors"
"zotregistry.io/zot/pkg/api"
"zotregistry.io/zot/pkg/api/config"
"zotregistry.io/zot/pkg/common"
extconf "zotregistry.io/zot/pkg/extensions/config"
"zotregistry.io/zot/pkg/test"
)
@@ -753,13 +754,18 @@ func TestOutputFormat(t *testing.T) {
space := regexp.MustCompile(`\s+`)
str := space.ReplaceAllString(buff.String(), " ")
So(strings.TrimSpace(str), ShouldEqual, `{ "repoName": "dummyImageName", "tag": "tag", `+
`"Manifests": [ { "configDigest": "sha256:4c10985c40365538426f2ba8cf0c21384a7769be502a550dcc0601b3736625e0", `+
`"digest": "sha256:6e2f80bf9cfaabad474fbaf8ad68fdb652f776ea80b63492ecca404e5f6446a6", `+
`"layers": [ { "size": "0", "digest": "sha256:c122a146f0d02349be211bb95cc2530f4a5793f96edbdfa00860f741e5d8c0e6" } ], `+ //nolint:lll
`"platform": { "os": "os", "arch": "arch", "variant": "" }, `+
`"size": "123445", "isSigned": false } ], `+
`"size": "123445", "digest": "sha256:9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08", `+
`"mediaType": "application/vnd.oci.image.manifest.v1+json", "isSigned": false }`)
`"digest": "sha256:9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08", `+
`"mediaType": "application/vnd.oci.image.manifest.v1+json", `+
`"manifests": [ { "digest": "sha256:6e2f80bf9cfaabad474fbaf8ad68fdb652f776ea80b63492ecca404e5f6446a6", `+
`"configDigest": "sha256:4c10985c40365538426f2ba8cf0c21384a7769be502a550dcc0601b3736625e0", `+
`"lastUpdated": "0001-01-01T00:00:00Z", "size": "123445", "platform": { "os": "os", "arch": "arch", `+
`"variant": "" }, "isSigned": false, "downloadCount": 0, `+
`"layers": [ { "size": "", "digest": "sha256:c122a146f0d02349be211bb95cc2530f4a5793f96edbdfa00860f741e5d8c0e6", `+
`"score": 0 } ], "history": null, "vulnerabilities": { "maxSeverity": "", "count": 0 }, `+
`"referrers": null, "artifactType": "" } ], "size": "123445", `+
`"downloadCount": 0, "lastUpdated": "0001-01-01T00:00:00Z", "description": "", "isSigned": false, "licenses": "", `+
`"labels": "", "title": "", "source": "", "documentation": "", "authors": "", "vendor": "", `+
`"vulnerabilities": { "maxSeverity": "", "count": 0 }, "referrers": null }`)
So(err, ShouldBeNil)
})
@@ -779,14 +785,18 @@ func TestOutputFormat(t *testing.T) {
strings.TrimSpace(str),
ShouldEqual,
`reponame: dummyImageName tag: tag `+
`manifests: - `+
`configdigest: sha256:4c10985c40365538426f2ba8cf0c21384a7769be502a550dcc0601b3736625e0 `+
`digest: sha256:9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08 `+
`mediatype: application/vnd.oci.image.manifest.v1+json manifests: - `+
`digest: sha256:6e2f80bf9cfaabad474fbaf8ad68fdb652f776ea80b63492ecca404e5f6446a6 `+
`layers: - size: 0 digest: sha256:c122a146f0d02349be211bb95cc2530f4a5793f96edbdfa00860f741e5d8c0e6 `+
`platform: os: os arch: arch variant: "" `+
`size: "123445" issigned: false `+
`size: "123445" digest: sha256:9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08 `+
`mediatype: application/vnd.oci.image.manifest.v1+json issigned: false`,
`configdigest: sha256:4c10985c40365538426f2ba8cf0c21384a7769be502a550dcc0601b3736625e0 `+
`lastupdated: 0001-01-01T00:00:00Z size: "123445" platform: os: os arch: arch variant: "" `+
`issigned: false downloadcount: 0 layers: - size: "" `+
`digest: sha256:c122a146f0d02349be211bb95cc2530f4a5793f96edbdfa00860f741e5d8c0e6 score: 0 `+
`history: [] vulnerabilities: maxseverity: "" count: 0 referrers: [] artifacttype: "" `+
`size: "123445" downloadcount: 0 `+
`lastupdated: 0001-01-01T00:00:00Z description: "" issigned: false licenses: "" labels: "" `+
`title: "" source: "" documentation: "" authors: "" vendor: "" vulnerabilities: maxseverity: "" `+
`count: 0 referrers: []`,
)
So(err, ShouldBeNil)
@@ -809,14 +819,18 @@ func TestOutputFormat(t *testing.T) {
strings.TrimSpace(str),
ShouldEqual,
`reponame: dummyImageName tag: tag `+
`manifests: - `+
`digest: sha256:9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08 `+
`mediatype: application/vnd.oci.image.manifest.v1+json `+
`manifests: - digest: sha256:6e2f80bf9cfaabad474fbaf8ad68fdb652f776ea80b63492ecca404e5f6446a6 `+
`configdigest: sha256:4c10985c40365538426f2ba8cf0c21384a7769be502a550dcc0601b3736625e0 `+
`digest: sha256:6e2f80bf9cfaabad474fbaf8ad68fdb652f776ea80b63492ecca404e5f6446a6 `+
`layers: - size: 0 digest: sha256:c122a146f0d02349be211bb95cc2530f4a5793f96edbdfa00860f741e5d8c0e6 `+
`platform: os: os arch: arch variant: "" `+
`size: "123445" issigned: false `+
`size: "123445" digest: sha256:9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08 `+
`mediatype: application/vnd.oci.image.manifest.v1+json issigned: false`,
`lastupdated: 0001-01-01T00:00:00Z size: "123445" platform: os: os arch: arch variant: "" `+
`issigned: false downloadcount: 0 layers: - size: "" `+
`digest: sha256:c122a146f0d02349be211bb95cc2530f4a5793f96edbdfa00860f741e5d8c0e6 score: 0 `+
`history: [] vulnerabilities: maxseverity: "" count: 0 referrers: [] artifacttype: "" `+
`size: "123445" downloadcount: 0 `+
`lastupdated: 0001-01-01T00:00:00Z description: "" issigned: false licenses: "" labels: "" `+
`title: "" source: "" documentation: "" authors: "" vendor: "" vulnerabilities: maxseverity: `+
`"" count: 0 referrers: []`,
)
So(err, ShouldBeNil)
})
@@ -1698,18 +1712,18 @@ func (service mockService) getRepos(ctx context.Context, config searchConfig, us
func (service mockService) getDerivedImageListGQL(ctx context.Context, config searchConfig, username, password string,
derivedImage string,
) (*imageListStructForDerivedImagesGQL, error) {
imageListGQLResponse := &imageListStructForDerivedImagesGQL{}
imageListGQLResponse.Data.Results = []imageStruct{
) (*common.DerivedImageListResponse, error) {
imageListGQLResponse := &common.DerivedImageListResponse{}
imageListGQLResponse.DerivedImageList.Results = []common.ImageSummary{
{
RepoName: "dummyImageName",
Tag: "tag",
Manifests: []manifestStruct{
Manifests: []common.ManifestSummary{
{
Digest: godigest.FromString("Digest").String(),
ConfigDigest: godigest.FromString("ConfigDigest").String(),
Size: "123445",
Layers: []layer{{Digest: godigest.FromString("LayerDigest").String()}},
Layers: []common.LayerSummary{{Digest: godigest.FromString("LayerDigest").String()}},
},
},
Size: "123445",
@@ -1721,18 +1735,18 @@ func (service mockService) getDerivedImageListGQL(ctx context.Context, config se
func (service mockService) getBaseImageListGQL(ctx context.Context, config searchConfig, username, password string,
derivedImage string,
) (*imageListStructForBaseImagesGQL, error) {
imageListGQLResponse := &imageListStructForBaseImagesGQL{}
imageListGQLResponse.Data.Results = []imageStruct{
) (*common.BaseImageListResponse, error) {
imageListGQLResponse := &common.BaseImageListResponse{}
imageListGQLResponse.BaseImageList.Results = []common.ImageSummary{
{
RepoName: "dummyImageName",
Tag: "tag",
Manifests: []manifestStruct{
Manifests: []common.ManifestSummary{
{
Digest: godigest.FromString("Digest").String(),
ConfigDigest: godigest.FromString("ConfigDigest").String(),
Size: "123445",
Layers: []layer{{Digest: godigest.FromString("LayerDigest").String()}},
Layers: []common.LayerSummary{{Digest: godigest.FromString("LayerDigest").String()}},
},
},
Size: "123445",
@@ -1744,20 +1758,20 @@ func (service mockService) getBaseImageListGQL(ctx context.Context, config searc
func (service mockService) getImagesGQL(ctx context.Context, config searchConfig, username, password string,
imageName string,
) (*imageListStructGQL, error) {
imageListGQLResponse := &imageListStructGQL{}
imageListGQLResponse.Data.Results = []imageStruct{
) (*common.ImageListResponse, error) {
imageListGQLResponse := &common.ImageListResponse{}
imageListGQLResponse.PaginatedImagesResult.Results = []common.ImageSummary{
{
RepoName: "dummyImageName",
Tag: "tag",
MediaType: ispec.MediaTypeImageManifest,
Digest: godigest.FromString("test").String(),
Manifests: []manifestStruct{
Manifests: []common.ManifestSummary{
{
Digest: godigest.FromString("Digest").String(),
ConfigDigest: godigest.FromString("ConfigDigest").String(),
Size: "123445",
Layers: []layer{{Digest: godigest.FromString("LayerDigest").String()}},
Layers: []common.LayerSummary{{Digest: godigest.FromString("LayerDigest").String()}},
},
},
Size: "123445",
@@ -1769,19 +1783,19 @@ func (service mockService) getImagesGQL(ctx context.Context, config searchConfig
func (service mockService) getImagesByDigestGQL(ctx context.Context, config searchConfig, username, password string,
digest string,
) (*imageListStructForDigestGQL, error) {
imageListGQLResponse := &imageListStructForDigestGQL{}
imageListGQLResponse.Data.Results = []imageStruct{
) (*common.ImagesForDigest, error) {
imageListGQLResponse := &common.ImagesForDigest{}
imageListGQLResponse.Results = []common.ImageSummary{
{
RepoName: "randomimageName",
Tag: "tag",
MediaType: ispec.MediaTypeImageManifest,
Digest: godigest.FromString("test").String(),
Manifests: []manifestStruct{
Manifests: []common.ManifestSummary{
{
Digest: godigest.FromString("Digest").String(),
ConfigDigest: godigest.FromString("ConfigDigest").String(),
Layers: []layer{{Digest: godigest.FromString("LayerDigest").String()}},
Layers: []common.LayerSummary{{Digest: godigest.FromString("LayerDigest").String()}},
Size: "123445",
},
},
@@ -1794,54 +1808,54 @@ func (service mockService) getImagesByDigestGQL(ctx context.Context, config sear
func (service mockService) getImagesByCveIDGQL(ctx context.Context, config searchConfig, username, password string,
digest string,
) (*imagesForCve, error) {
imagesForCve := &imagesForCve{
) (*common.ImagesForCve, error) {
imagesForCve := &common.ImagesForCve{
Errors: nil,
Data: struct {
PaginatedImagesResult `json:"ImageListForCVE"` //nolint:tagliatelle
ImagesForCVEList: struct {
common.PaginatedImagesResult `json:"ImageListForCVE"` //nolint:tagliatelle
}{},
}
imagesForCve.Errors = nil
mockedImage := service.getMockedImageByName("anImage")
imagesForCve.Data.Results = []imageStruct{mockedImage}
imagesForCve.Results = []common.ImageSummary{common.ImageSummary(mockedImage)}
return imagesForCve, nil
}
func (service mockService) getTagsForCVEGQL(ctx context.Context, config searchConfig, username, password,
imageName, cveID string,
) (*imagesForCve, error) {
images := &imagesForCve{
) (*common.ImagesForCve, error) {
images := &common.ImagesForCve{
Errors: nil,
Data: struct {
PaginatedImagesResult `json:"ImageListForCVE"` //nolint:tagliatelle // graphQL schema
ImagesForCVEList: struct {
common.PaginatedImagesResult `json:"ImageListForCVE"` //nolint:tagliatelle // graphQL schema
}{},
}
images.Errors = nil
mockedImage := service.getMockedImageByName(imageName)
images.Data.Results = []imageStruct{mockedImage}
images.Results = []common.ImageSummary{common.ImageSummary(mockedImage)}
return images, nil
}
func (service mockService) getFixedTagsForCVEGQL(ctx context.Context, config searchConfig, username, password,
imageName, cveID string,
) (*fixedTags, error) {
fixedTags := &fixedTags{
) (*common.FixedTags, error) {
fixedTags := &common.FixedTags{
Errors: nil,
Data: struct {
PaginatedImagesResult `json:"ImageListWithCVEFixed"` //nolint:tagliatelle // graphQL schema
ImageListWithCVEFixed: struct {
common.PaginatedImagesResult `json:"ImageListWithCVEFixed"` //nolint:tagliatelle // graphQL schema
}{},
}
fixedTags.Errors = nil
mockedImage := service.getMockedImageByName(imageName)
fixedTags.Data.Results = []imageStruct{mockedImage}
fixedTags.Results = []common.ImageSummary{common.ImageSummary(mockedImage)}
return fixedTags, nil
}
@@ -1879,11 +1893,11 @@ func (service mockService) getMockedImageByName(imageName string) imageStruct {
image := imageStruct{}
image.RepoName = imageName
image.Tag = "tag"
image.Manifests = []manifestStruct{
image.Manifests = []common.ManifestSummary{
{
Digest: godigest.FromString("Digest").String(),
ConfigDigest: godigest.FromString("ConfigDigest").String(),
Layers: []layer{{Digest: godigest.FromString("LayerDigest").String()}},
Layers: []common.LayerSummary{{Digest: godigest.FromString("LayerDigest").String()}},
Size: "123445",
},
}
@@ -1903,18 +1917,18 @@ func (service mockService) getAllImages(ctx context.Context, config searchConfig
image.Tag = "tag"
image.Digest = godigest.FromString("test").String()
image.MediaType = ispec.MediaTypeImageManifest
image.Manifests = []manifestStruct{
image.Manifests = []common.ManifestSummary{
{
Digest: godigest.FromString("Digest").String(),
ConfigDigest: godigest.FromString("ConfigDigest").String(),
Layers: []layer{{Digest: godigest.FromString("LayerDigest").String()}},
Layers: []common.LayerSummary{{Digest: godigest.FromString("LayerDigest").String()}},
Size: "123445",
Platform: platform{Os: "os", Arch: "arch"},
Platform: common.Platform{Os: "os", Arch: "arch"},
},
}
image.Size = "123445"
str, err := image.string(*config.outputFormat, len(image.RepoName), len(image.Tag), len("os/Arch"))
str, err := image.string(*config.outputFormat, len(image.RepoName), len(image.Tag), len("os/Arch"), *config.verbose)
if err != nil {
channel <- stringResult{"", err}
@@ -1935,18 +1949,18 @@ func (service mockService) getImageByName(ctx context.Context, config searchConf
image.Tag = "tag"
image.Digest = godigest.FromString("test").String()
image.MediaType = ispec.MediaTypeImageManifest
image.Manifests = []manifestStruct{
image.Manifests = []common.ManifestSummary{
{
Digest: godigest.FromString("Digest").String(),
ConfigDigest: godigest.FromString("ConfigDigest").String(),
Layers: []layer{{Digest: godigest.FromString("LayerDigest").String()}},
Layers: []common.LayerSummary{{Digest: godigest.FromString("LayerDigest").String()}},
Size: "123445",
Platform: platform{Os: "os", Arch: "arch"},
Platform: common.Platform{Os: "os", Arch: "arch"},
},
}
image.Size = "123445"
str, err := image.string(*config.outputFormat, len(image.RepoName), len(image.Tag), len("os/Arch"))
str, err := image.string(*config.outputFormat, len(image.RepoName), len(image.Tag), len("os/Arch"), *config.verbose)
if err != nil {
channel <- stringResult{"", err}
+44 -9
View File
@@ -188,7 +188,13 @@ func getImages(config searchConfig) error {
return err
}
return printResult(config, imageList.Data.Results)
imageListData := []imageStruct{}
for _, image := range imageList.Results {
imageListData = append(imageListData, imageStruct(image))
}
return printResult(config, imageListData)
}
type imagesByDigestSearcher struct{}
@@ -241,7 +247,13 @@ func (search derivedImageListSearcherGQL) search(config searchConfig) (bool, err
return true, err
}
if err := printResult(config, imageList.Data.Results); err != nil {
imageListData := []imageStruct{}
for _, image := range imageList.DerivedImageList.Results {
imageListData = append(imageListData, imageStruct(image))
}
if err := printResult(config, imageListData); err != nil {
return true, err
}
@@ -266,7 +278,13 @@ func (search baseImageListSearcherGQL) search(config searchConfig) (bool, error)
return true, err
}
if err := printResult(config, imageList.Data.Results); err != nil {
imageListData := []imageStruct{}
for _, image := range imageList.BaseImageList.Results {
imageListData = append(imageListData, imageStruct(image))
}
if err := printResult(config, imageListData); err != nil {
return true, err
}
@@ -292,7 +310,13 @@ func (search imagesByDigestSearcherGQL) search(config searchConfig) (bool, error
return true, err
}
if err := printResult(config, imageList.Data.Results); err != nil {
imageListData := []imageStruct{}
for _, image := range imageList.Results {
imageListData = append(imageListData, imageStruct(image))
}
if err := printResult(config, imageListData); err != nil {
return true, err
}
@@ -431,7 +455,13 @@ func (search imagesByCVEIDSearcherGQL) search(config searchConfig) (bool, error)
return true, err
}
if err := printResult(config, imageList.Data.Results); err != nil {
imageListData := []imageStruct{}
for _, image := range imageList.Results {
imageListData = append(imageListData, imageStruct(image))
}
if err := printResult(config, imageListData); err != nil {
return true, err
}
@@ -557,7 +587,9 @@ func getTagsByCVE(config searchConfig) error {
return err
}
imageList = fixedTags.Data.Results
for _, image := range fixedTags.Results {
imageList = append(imageList, imageStruct(image))
}
} else {
tags, err := config.searchService.getTagsForCVEGQL(ctx, config, username, password,
*config.params["imageName"], *config.params["cveID"])
@@ -565,7 +597,10 @@ func getTagsByCVE(config searchConfig) error {
return err
}
imageList = tags.Data.Results
imageList = nil
for _, image := range tags.Results {
imageList = append(imageList, imageStruct(image))
}
}
return printResult(config, imageList)
@@ -798,9 +833,9 @@ func printResult(config searchConfig, imageList []imageStruct) error {
for i := range imageList {
img := imageList[i]
img.verbose = *config.verbose
verbose := *config.verbose
out, err := img.string(*config.outputFormat, maxImgNameLen, maxTagLen, maxPlatformLen)
out, err := img.string(*config.outputFormat, maxImgNameLen, maxTagLen, maxPlatformLen, verbose)
if err != nil {
return err
}
+48 -137
View File
@@ -27,21 +27,21 @@ import (
type SearchService interface { //nolint:interfacebloat
getImagesGQL(ctx context.Context, config searchConfig, username, password string,
imageName string) (*imageListStructGQL, error)
imageName string) (*common.ImageListResponse, error)
getImagesByDigestGQL(ctx context.Context, config searchConfig, username, password string,
digest string) (*imageListStructForDigestGQL, error)
digest string) (*common.ImagesForDigest, error)
getCveByImageGQL(ctx context.Context, config searchConfig, username, password,
imageName string, searchedCVE string) (*cveResult, error)
getImagesByCveIDGQL(ctx context.Context, config searchConfig, username, password string,
digest string) (*imagesForCve, error)
digest string) (*common.ImagesForCve, error)
getTagsForCVEGQL(ctx context.Context, config searchConfig, username, password, imageName,
cveID string) (*imagesForCve, error)
cveID string) (*common.ImagesForCve, error)
getFixedTagsForCVEGQL(ctx context.Context, config searchConfig, username, password, imageName,
cveID string) (*fixedTags, error)
cveID string) (*common.FixedTags, error)
getDerivedImageListGQL(ctx context.Context, config searchConfig, username, password string,
derivedImage string) (*imageListStructForDerivedImagesGQL, error)
derivedImage string) (*common.DerivedImageListResponse, error)
getBaseImageListGQL(ctx context.Context, config searchConfig, username, password string,
baseImage string) (*imageListStructForBaseImagesGQL, error)
baseImage string) (*common.BaseImageListResponse, error)
getAllImages(ctx context.Context, config searchConfig, username, password string,
channel chan stringResult, wtgrp *sync.WaitGroup)
@@ -69,7 +69,7 @@ func NewSearchService() SearchService {
func (service searchService) getDerivedImageListGQL(ctx context.Context, config searchConfig, username, password string,
derivedImage string,
) (*imageListStructForDerivedImagesGQL, error) {
) (*common.DerivedImageListResponse, error) {
query := fmt.Sprintf(`
{
DerivedImageList(image:"%s"){
@@ -93,7 +93,7 @@ func (service searchService) getDerivedImageListGQL(ctx context.Context, config
}
}`, derivedImage)
result := &imageListStructForDerivedImagesGQL{}
result := &common.DerivedImageListResponse{}
err := service.makeGraphQLQuery(ctx, config, username, password, query, result)
if errResult := checkResultGraphQLQuery(ctx, err, result.Errors); errResult != nil {
@@ -105,7 +105,7 @@ func (service searchService) getDerivedImageListGQL(ctx context.Context, config
func (service searchService) getBaseImageListGQL(ctx context.Context, config searchConfig, username, password string,
baseImage string,
) (*imageListStructForBaseImagesGQL, error) {
) (*common.BaseImageListResponse, error) {
query := fmt.Sprintf(`
{
BaseImageList(image:"%s"){
@@ -129,7 +129,7 @@ func (service searchService) getBaseImageListGQL(ctx context.Context, config sea
}
}`, baseImage)
result := &imageListStructForBaseImagesGQL{}
result := &common.BaseImageListResponse{}
err := service.makeGraphQLQuery(ctx, config, username, password, query, result)
if errResult := checkResultGraphQLQuery(ctx, err, result.Errors); errResult != nil {
@@ -141,7 +141,7 @@ func (service searchService) getBaseImageListGQL(ctx context.Context, config sea
func (service searchService) getImagesGQL(ctx context.Context, config searchConfig, username, password string,
imageName string,
) (*imageListStructGQL, error) {
) (*common.ImageListResponse, error) {
query := fmt.Sprintf(`
{
ImageList(repo: "%s") {
@@ -163,7 +163,7 @@ func (service searchService) getImagesGQL(ctx context.Context, config searchConf
}
}`,
imageName)
result := &imageListStructGQL{}
result := &common.ImageListResponse{}
err := service.makeGraphQLQuery(ctx, config, username, password, query, result)
@@ -176,7 +176,7 @@ func (service searchService) getImagesGQL(ctx context.Context, config searchConf
func (service searchService) getImagesByDigestGQL(ctx context.Context, config searchConfig, username, password string,
digest string,
) (*imageListStructForDigestGQL, error) {
) (*common.ImagesForDigest, error) {
query := fmt.Sprintf(`
{
ImageListForDigest(id: "%s") {
@@ -197,7 +197,7 @@ func (service searchService) getImagesByDigestGQL(ctx context.Context, config se
}
}`,
digest)
result := &imageListStructForDigestGQL{}
result := &common.ImagesForDigest{}
err := service.makeGraphQLQuery(ctx, config, username, password, query, result)
@@ -210,7 +210,7 @@ func (service searchService) getImagesByDigestGQL(ctx context.Context, config se
func (service searchService) getImagesByCveIDGQL(ctx context.Context, config searchConfig, username,
password, cveID string,
) (*imagesForCve, error) {
) (*common.ImagesForCve, error) {
query := fmt.Sprintf(`
{
ImageListForCVE(id: "%s") {
@@ -231,7 +231,7 @@ func (service searchService) getImagesByCveIDGQL(ctx context.Context, config sea
}
}`,
cveID)
result := &imagesForCve{}
result := &common.ImagesForCve{}
err := service.makeGraphQLQuery(ctx, config, username, password, query, result)
@@ -263,7 +263,7 @@ func (service searchService) getCveByImageGQL(ctx context.Context, config search
func (service searchService) getTagsForCVEGQL(ctx context.Context, config searchConfig,
username, password, imageName, cveID string,
) (*imagesForCve, error) {
) (*common.ImagesForCve, error) {
query := fmt.Sprintf(`
{
ImageListForCVE(id: "%s") {
@@ -283,7 +283,7 @@ func (service searchService) getTagsForCVEGQL(ctx context.Context, config search
}
}`,
cveID)
result := &imagesForCve{}
result := &common.ImagesForCve{}
err := service.makeGraphQLQuery(ctx, config, username, password, query, result)
@@ -296,7 +296,7 @@ func (service searchService) getTagsForCVEGQL(ctx context.Context, config search
func (service searchService) getFixedTagsForCVEGQL(ctx context.Context, config searchConfig,
username, password, imageName, cveID string,
) (*fixedTags, error) {
) (*common.FixedTags, error) {
query := fmt.Sprintf(`
{
ImageListWithCVEFixed(id: "%s", image: "%s") {
@@ -317,7 +317,7 @@ func (service searchService) getFixedTagsForCVEGQL(ctx context.Context, config s
}`,
cveID, imageName)
result := &fixedTags{}
result := &common.FixedTags{}
err := service.makeGraphQLQuery(ctx, config, username, password, query, result)
@@ -464,7 +464,7 @@ func (service searchService) getImagesByCveID(ctx context.Context, config search
}`,
cvid)
result := &imagesForCve{}
result := &common.ImagesForCve{}
err := service.makeGraphQLQuery(ctx, config, username, password, query, result)
if err != nil {
@@ -498,7 +498,7 @@ func (service searchService) getImagesByCveID(ctx context.Context, config search
go rlim.startRateLimiter(ctx)
for _, image := range result.Data.Results {
for _, image := range result.Results {
localWg.Add(1)
go addManifestCallToPool(ctx, config, rlim, username, password, image.RepoName, image.Tag, rch, &localWg)
@@ -533,7 +533,7 @@ func (service searchService) getImagesByDigest(ctx context.Context, config searc
}`,
digest)
result := &imagesForDigest{}
result := &common.ImagesForDigest{}
err := service.makeGraphQLQuery(ctx, config, username, password, query, result)
if err != nil {
@@ -567,7 +567,7 @@ func (service searchService) getImagesByDigest(ctx context.Context, config searc
go rlim.startRateLimiter(ctx)
for _, image := range result.Data.Results {
for _, image := range result.Results {
localWg.Add(1)
go addManifestCallToPool(ctx, config, rlim, username, password, image.RepoName, image.Tag, rch, &localWg)
@@ -602,7 +602,7 @@ func (service searchService) getImageByNameAndCVEID(ctx context.Context, config
}`,
cvid)
result := &imagesForCve{}
result := &common.ImagesForCve{}
err := service.makeGraphQLQuery(ctx, config, username, password, query, result)
if err != nil {
@@ -636,7 +636,7 @@ func (service searchService) getImageByNameAndCVEID(ctx context.Context, config
go rlim.startRateLimiter(ctx)
for _, image := range result.Data.Results {
for _, image := range result.Results {
if !strings.EqualFold(imageName, image.RepoName) {
continue
}
@@ -728,7 +728,7 @@ func (service searchService) getFixedTagsForCVE(ctx context.Context, config sear
}
}`, cvid, imageName)
result := &fixedTags{}
result := &common.FixedTags{}
err := service.makeGraphQLQuery(ctx, config, username, password, query, result)
if err != nil {
@@ -762,7 +762,7 @@ func (service searchService) getFixedTagsForCVE(ctx context.Context, config sear
go rlim.startRateLimiter(ctx)
for _, img := range result.Data.Results {
for _, img := range result.Results {
localWg.Add(1)
go addManifestCallToPool(ctx, config, rlim, username, password, imageName, img.Tag, rch, &localWg)
@@ -844,7 +844,7 @@ func (service searchService) makeGraphQLQuery(ctx context.Context,
return nil
}
func checkResultGraphQLQuery(ctx context.Context, err error, resultErrors []common.ErrorGraphQL,
func checkResultGraphQLQuery(ctx context.Context, err error, resultErrors []common.ErrorGQL,
) error {
if err != nil {
if isContextDone(ctx) {
@@ -900,8 +900,8 @@ func addManifestCallToPool(ctx context.Context, config searchConfig, pool *reque
}
type cveResult struct {
Errors []common.ErrorGraphQL `json:"errors"`
Data cveData `json:"data"`
Errors []common.ErrorGQL `json:"errors"`
Data cveData `json:"data"`
}
type tagListResp struct {
@@ -991,101 +991,12 @@ func (cve cveResult) stringYAML() (string, error) {
return string(body), nil
}
type fixedTags struct {
Errors []common.ErrorGraphQL `json:"errors"`
Data struct {
PaginatedImagesResult `json:"ImageListWithCVEFixed"` //nolint:tagliatelle // graphQL schema
} `json:"data"`
}
type imageStruct common.ImageSummary
type imagesForCve struct {
Errors []common.ErrorGraphQL `json:"errors"`
Data struct {
PaginatedImagesResult `json:"ImageListForCVE"` //nolint:tagliatelle // graphQL schema
} `json:"data"`
}
type PaginatedImagesResult struct {
Results []imageStruct `json:"results"`
}
type imageStruct struct {
RepoName string `json:"repoName"`
Tag string `json:"tag"`
Manifests []manifestStruct
Size string `json:"size"`
Digest string `json:"digest"`
MediaType string `json:"mediaType"`
IsSigned bool `json:"isSigned"`
verbose bool
}
type manifestStruct struct {
ConfigDigest string `json:"configDigest"`
Digest string `json:"digest"`
Layers []layer `json:"layers"`
Platform platform `json:"platform"`
Size string `json:"size"`
IsSigned bool `json:"isSigned"`
}
type platform struct {
Os string `json:"os"`
Arch string `json:"arch"`
Variant string `json:"variant"`
}
type DerivedImageList struct {
Results []imageStruct `json:"results"`
}
type BaseImageList struct {
Results []imageStruct `json:"results"`
}
type imageListStructGQL struct {
Errors []common.ErrorGraphQL `json:"errors"`
Data struct {
PaginatedImagesResult `json:"ImageList"` //nolint:tagliatelle
} `json:"data"`
}
type imageListStructForDigestGQL struct {
Errors []common.ErrorGraphQL `json:"errors"`
Data struct {
PaginatedImagesResult `json:"ImageListForDigest"` //nolint:tagliatelle
} `json:"data"`
}
type imageListStructForDerivedImagesGQL struct {
Errors []common.ErrorGraphQL `json:"errors"`
Data struct {
PaginatedImagesResult `json:"DerivedImageList"` //nolint:tagliatelle
} `json:"data"`
}
type imageListStructForBaseImagesGQL struct {
Errors []common.ErrorGraphQL `json:"errors"`
Data struct {
PaginatedImagesResult `json:"BaseImageList"` //nolint:tagliatelle
} `json:"data"`
}
type imagesForDigest struct {
Errors []common.ErrorGraphQL `json:"errors"`
Data struct {
PaginatedImagesResult `json:"ImageListForDigest"` //nolint:tagliatelle // graphQL schema
} `json:"data"`
}
type layer struct {
Size int64 `json:"size,string"`
Digest string `json:"digest"`
}
func (img imageStruct) string(format string, maxImgNameLen, maxTagLen, maxPlatformLen int) (string, error) {
func (img imageStruct) string(format string, maxImgNameLen, maxTagLen, maxPlatformLen int, verbose bool) (string, error) { //nolint: lll
switch strings.ToLower(format) {
case "", defaultOutoutFormat:
return img.stringPlainText(maxImgNameLen, maxTagLen, maxPlatformLen)
return img.stringPlainText(maxImgNameLen, maxTagLen, maxPlatformLen, verbose)
case "json":
return img.stringJSON()
case "yml", "yaml":
@@ -1095,7 +1006,7 @@ func (img imageStruct) string(format string, maxImgNameLen, maxTagLen, maxPlatfo
}
}
func (img imageStruct) stringPlainText(maxImgNameLen, maxTagLen, maxPlatformLen int) (string, error) {
func (img imageStruct) stringPlainText(maxImgNameLen, maxTagLen, maxPlatformLen int, verbose bool) (string, error) {
var builder strings.Builder
table := getImageTableWriter(&builder)
@@ -1107,7 +1018,7 @@ func (img imageStruct) stringPlainText(maxImgNameLen, maxTagLen, maxPlatformLen
table.SetColMinWidth(colSizeIndex, sizeWidth)
table.SetColMinWidth(colIsSignedIndex, isSignedWidth)
if img.verbose {
if verbose {
table.SetColMinWidth(colConfigIndex, configWidth)
table.SetColMinWidth(colLayersIndex, layersWidth)
}
@@ -1138,7 +1049,7 @@ func (img imageStruct) stringPlainText(maxImgNameLen, maxTagLen, maxPlatformLen
tagName += offset
}
err := addImageToTable(table, &img, maxPlatformLen, imageName, tagName)
err := addImageToTable(table, &img, maxPlatformLen, imageName, tagName, verbose)
if err != nil {
return "", err
}
@@ -1149,20 +1060,20 @@ func (img imageStruct) stringPlainText(maxImgNameLen, maxTagLen, maxPlatformLen
}
func addImageToTable(table *tablewriter.Table, img *imageStruct, maxPlatformLen int,
imageName, tagName string,
imageName, tagName string, verbose bool,
) error {
switch img.MediaType {
case ispec.MediaTypeImageManifest:
return addManifestToTable(table, imageName, tagName, &img.Manifests[0], maxPlatformLen, img.verbose)
return addManifestToTable(table, imageName, tagName, &img.Manifests[0], maxPlatformLen, verbose)
case ispec.MediaTypeImageIndex:
return addImageIndexToTable(table, img, maxPlatformLen, imageName, tagName)
return addImageIndexToTable(table, img, maxPlatformLen, imageName, tagName, verbose)
}
return nil
}
func addImageIndexToTable(table *tablewriter.Table, img *imageStruct, maxPlatformLen int,
imageName, tagName string,
imageName, tagName string, verbose bool,
) error {
indexDigest, err := godigest.Parse(img.Digest)
if err != nil {
@@ -1178,7 +1089,7 @@ func addImageIndexToTable(table *tablewriter.Table, img *imageStruct, maxPlatfor
row[colSizeIndex] = ellipsize(strings.ReplaceAll(humanize.Bytes(imgSize), " ", ""), sizeWidth, ellipsis)
row[colIsSignedIndex] = strconv.FormatBool(img.IsSigned)
if img.verbose {
if verbose {
row[colConfigIndex] = ""
row[colLayersIndex] = ""
}
@@ -1186,7 +1097,7 @@ func addImageIndexToTable(table *tablewriter.Table, img *imageStruct, maxPlatfor
table.Append(row)
for i := range img.Manifests {
err := addManifestToTable(table, "", "", &img.Manifests[i], maxPlatformLen, img.verbose)
err := addManifestToTable(table, "", "", &img.Manifests[i], maxPlatformLen, verbose)
if err != nil {
return err
}
@@ -1195,7 +1106,7 @@ func addImageIndexToTable(table *tablewriter.Table, img *imageStruct, maxPlatfor
return nil
}
func addManifestToTable(table *tablewriter.Table, imageName, tagName string, manifest *manifestStruct,
func addManifestToTable(table *tablewriter.Table, imageName, tagName string, manifest *common.ManifestSummary,
maxPlatformLen int, verbose bool,
) error {
manifestDigest, err := godigest.Parse(manifest.Digest)
@@ -1238,8 +1149,8 @@ func addManifestToTable(table *tablewriter.Table, imageName, tagName string, man
if verbose {
for _, entry := range manifest.Layers {
layerSize := entry.Size
size := ellipsize(strings.ReplaceAll(humanize.Bytes(uint64(layerSize)), " ", ""), sizeWidth, ellipsis)
layerSize, _ := strconv.ParseUint(entry.Size, 10, 64)
size := ellipsize(strings.ReplaceAll(humanize.Bytes(layerSize), " ", ""), sizeWidth, ellipsis)
layerDigest, err := godigest.Parse(entry.Digest)
if err != nil {
@@ -1264,7 +1175,7 @@ func addManifestToTable(table *tablewriter.Table, imageName, tagName string, man
return nil
}
func getPlatformStr(platf platform) string {
func getPlatformStr(platf common.Platform) string {
if platf.Arch == "" && platf.Os == "" {
return ""
}