2021-05-26 12:22:31 -05:00
|
|
|
package digestinfo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
godigest "github.com/opencontainers/go-digest"
|
2021-05-26 12:22:31 -05:00
|
|
|
ispec "github.com/opencontainers/image-spec/specs-go/v1"
|
2022-10-20 11:39:20 -05:00
|
|
|
|
2021-12-03 22:50:58 -05:00
|
|
|
"zotregistry.io/zot/pkg/extensions/search/common"
|
|
|
|
"zotregistry.io/zot/pkg/log"
|
|
|
|
"zotregistry.io/zot/pkg/storage"
|
2021-05-26 12:22:31 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// DigestInfo implements searching by manifes/config/layer digest.
|
|
|
|
type DigestInfo struct {
|
|
|
|
Log log.Logger
|
2022-07-12 07:58:04 -05:00
|
|
|
LayoutUtils *common.BaseOciLayoutUtils
|
2021-05-26 12:22:31 -05:00
|
|
|
}
|
|
|
|
|
2022-01-19 10:57:10 -05:00
|
|
|
type ImageInfoByDigest struct {
|
|
|
|
Tag string
|
2022-10-22 15:46:13 -05:00
|
|
|
Digest godigest.Digest
|
|
|
|
Manifest ispec.Manifest
|
2022-01-19 10:57:10 -05:00
|
|
|
}
|
|
|
|
|
2021-05-26 12:22:31 -05:00
|
|
|
// NewDigestInfo initializes a new DigestInfo object.
|
2021-09-30 08:27:13 -05:00
|
|
|
func NewDigestInfo(storeController storage.StoreController, log log.Logger) *DigestInfo {
|
2022-07-12 07:58:04 -05:00
|
|
|
layoutUtils := common.NewBaseOciLayoutUtils(storeController, log)
|
2021-05-26 12:22:31 -05:00
|
|
|
|
|
|
|
return &DigestInfo{Log: log, LayoutUtils: layoutUtils}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FilterImagesByDigest returns a list of image tags in a repository matching a specific divest.
|
2022-01-19 10:57:10 -05:00
|
|
|
func (digestinfo DigestInfo) GetImageTagsByDigest(repo, digest string) ([]ImageInfoByDigest, error) {
|
|
|
|
imageTags := []ImageInfoByDigest{}
|
2021-05-26 12:22:31 -05:00
|
|
|
|
2021-01-25 13:04:03 -05:00
|
|
|
manifests, err := digestinfo.LayoutUtils.GetImageManifests(repo)
|
2021-05-26 12:22:31 -05:00
|
|
|
if err != nil {
|
|
|
|
digestinfo.Log.Error().Err(err).Msg("unable to read image manifests")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2022-01-19 10:57:10 -05:00
|
|
|
return imageTags, err
|
2021-05-26 12:22:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, manifest := range manifests {
|
|
|
|
imageDigest := manifest.Digest
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
val, ok := manifest.Annotations[ispec.AnnotationRefName]
|
2021-05-26 12:22:31 -05:00
|
|
|
if ok {
|
2021-01-25 13:04:03 -05:00
|
|
|
imageBlobManifest, err := digestinfo.LayoutUtils.GetImageBlobManifest(repo, imageDigest)
|
2021-05-26 12:22:31 -05:00
|
|
|
if err != nil {
|
|
|
|
digestinfo.Log.Error().Err(err).Msg("unable to read image blob manifest")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2022-01-19 10:57:10 -05:00
|
|
|
return imageTags, err
|
2021-05-26 12:22:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
tags := []*string{}
|
|
|
|
|
|
|
|
// Check the image manigest in index.json matches the search digest
|
|
|
|
// This is a blob with mediaType application/vnd.oci.image.manifest.v1+json
|
|
|
|
if strings.Contains(manifest.Digest.String(), digest) {
|
2021-12-13 14:23:31 -05:00
|
|
|
tags = append(tags, &val)
|
2021-05-26 12:22:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check the image config matches the search digest
|
|
|
|
// This is a blob with mediaType application/vnd.oci.image.config.v1+json
|
2022-10-22 15:46:13 -05:00
|
|
|
if strings.Contains(imageBlobManifest.Config.Digest.String(), digest) {
|
2021-12-13 14:23:31 -05:00
|
|
|
tags = append(tags, &val)
|
2021-05-26 12:22:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 imageBlobManifest.Layers {
|
2022-10-22 15:46:13 -05:00
|
|
|
if strings.Contains(layer.Digest.String(), digest) {
|
2021-12-13 14:23:31 -05:00
|
|
|
tags = append(tags, &val)
|
2021-05-26 12:22:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
keys := make(map[string]bool)
|
|
|
|
|
|
|
|
for _, entry := range tags {
|
|
|
|
if _, value := keys[*entry]; !value {
|
2022-01-19 10:57:10 -05:00
|
|
|
imageTags = append(imageTags, ImageInfoByDigest{Tag: *entry, Digest: imageDigest, Manifest: imageBlobManifest})
|
2021-05-26 12:22:31 -05:00
|
|
|
keys[*entry] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-19 10:57:10 -05:00
|
|
|
return imageTags, nil
|
2021-05-26 12:22:31 -05:00
|
|
|
}
|