2021-05-26 12:22:31 -05:00
|
|
|
// Package common ...
|
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2021-12-13 14:23:31 -05:00
|
|
|
goerrors "errors"
|
2021-05-26 12:22:31 -05:00
|
|
|
"path"
|
|
|
|
"strings"
|
2021-01-25 13:04:03 -05:00
|
|
|
"time"
|
2021-05-26 12:22:31 -05:00
|
|
|
|
|
|
|
v1 "github.com/google/go-containerregistry/pkg/v1"
|
2021-01-25 13:04:03 -05:00
|
|
|
"github.com/google/go-containerregistry/pkg/v1/types"
|
2021-05-26 12:22:31 -05:00
|
|
|
godigest "github.com/opencontainers/go-digest"
|
|
|
|
ispec "github.com/opencontainers/image-spec/specs-go/v1"
|
2021-12-03 22:50:58 -05:00
|
|
|
"zotregistry.io/zot/errors"
|
|
|
|
"zotregistry.io/zot/pkg/log"
|
|
|
|
"zotregistry.io/zot/pkg/storage"
|
2021-05-26 12:22:31 -05:00
|
|
|
)
|
|
|
|
|
2021-01-25 13:04:03 -05:00
|
|
|
// OciLayoutInfo ...
|
2021-05-26 12:22:31 -05:00
|
|
|
type OciLayoutUtils struct {
|
2021-09-30 08:27:13 -05:00
|
|
|
Log log.Logger
|
|
|
|
StoreController storage.StoreController
|
2021-05-26 12:22:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewOciLayoutUtils initializes a new OciLayoutUtils object.
|
2021-09-30 08:27:13 -05:00
|
|
|
func NewOciLayoutUtils(storeController storage.StoreController, log log.Logger) *OciLayoutUtils {
|
|
|
|
return &OciLayoutUtils{Log: log, StoreController: storeController}
|
2021-05-26 12:22:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Below method will return image path including root dir, root dir is determined by splitting.
|
2021-09-30 08:27:13 -05:00
|
|
|
func (olu OciLayoutUtils) GetImageManifests(image string) ([]ispec.Descriptor, error) {
|
|
|
|
imageStore := olu.StoreController.GetImageStore(image)
|
2021-05-26 12:22:31 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
buf, err := imageStore.GetIndexContent(image)
|
2021-05-26 12:22:31 -05:00
|
|
|
if err != nil {
|
2021-09-30 08:27:13 -05:00
|
|
|
if goerrors.Is(errors.ErrRepoNotFound, err) {
|
2021-05-26 12:22:31 -05:00
|
|
|
olu.Log.Error().Err(err).Msg("index.json doesn't exist")
|
|
|
|
|
|
|
|
return nil, errors.ErrRepoNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
olu.Log.Error().Err(err).Msg("unable to open index.json")
|
|
|
|
|
|
|
|
return nil, errors.ErrRepoNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
var index ispec.Index
|
|
|
|
|
|
|
|
if err := json.Unmarshal(buf, &index); err != nil {
|
2021-09-30 08:27:13 -05:00
|
|
|
olu.Log.Error().Err(err).Str("dir", path.Join(imageStore.RootDir(), image)).Msg("invalid JSON")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-05-26 12:22:31 -05:00
|
|
|
return nil, errors.ErrRepoNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return index.Manifests, nil
|
|
|
|
}
|
|
|
|
|
2021-09-30 08:27:13 -05:00
|
|
|
//nolint: interfacer
|
2021-05-26 12:22:31 -05:00
|
|
|
func (olu OciLayoutUtils) GetImageBlobManifest(imageDir string, digest godigest.Digest) (v1.Manifest, error) {
|
|
|
|
var blobIndex v1.Manifest
|
|
|
|
|
2021-09-30 08:27:13 -05:00
|
|
|
imageStore := olu.StoreController.GetImageStore(imageDir)
|
|
|
|
|
|
|
|
blobBuf, err := imageStore.GetBlobContent(imageDir, digest.String())
|
2021-05-26 12:22:31 -05:00
|
|
|
if err != nil {
|
|
|
|
olu.Log.Error().Err(err).Msg("unable to open image metadata file")
|
|
|
|
|
|
|
|
return blobIndex, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.Unmarshal(blobBuf, &blobIndex); err != nil {
|
|
|
|
olu.Log.Error().Err(err).Msg("unable to marshal blob index")
|
|
|
|
|
|
|
|
return blobIndex, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return blobIndex, nil
|
|
|
|
}
|
|
|
|
|
2021-09-30 08:27:13 -05:00
|
|
|
//nolint: interfacer
|
2021-05-26 12:22:31 -05:00
|
|
|
func (olu OciLayoutUtils) GetImageInfo(imageDir string, hash v1.Hash) (ispec.Image, error) {
|
|
|
|
var imageInfo ispec.Image
|
|
|
|
|
2021-09-30 08:27:13 -05:00
|
|
|
imageStore := olu.StoreController.GetImageStore(imageDir)
|
|
|
|
|
|
|
|
blobBuf, err := imageStore.GetBlobContent(imageDir, hash.String())
|
2021-05-26 12:22:31 -05:00
|
|
|
if err != nil {
|
|
|
|
olu.Log.Error().Err(err).Msg("unable to open image layers file")
|
|
|
|
|
|
|
|
return imageInfo, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.Unmarshal(blobBuf, &imageInfo); err != nil {
|
|
|
|
olu.Log.Error().Err(err).Msg("unable to marshal blob index")
|
|
|
|
|
|
|
|
return imageInfo, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return imageInfo, err
|
|
|
|
}
|
|
|
|
|
2021-09-30 08:27:13 -05:00
|
|
|
func (olu OciLayoutUtils) IsValidImageFormat(image string) (bool, error) {
|
|
|
|
imageDir, inputTag := GetImageDirAndTag(image)
|
2021-01-25 13:04:03 -05:00
|
|
|
|
|
|
|
manifests, err := olu.GetImageManifests(imageDir)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
for _, manifest := range manifests {
|
|
|
|
tag, ok := manifest.Annotations[ispec.AnnotationRefName]
|
2021-01-25 13:04:03 -05:00
|
|
|
|
|
|
|
if ok && inputTag != "" && tag != inputTag {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
blobManifest, err := olu.GetImageBlobManifest(imageDir, manifest.Digest)
|
2021-01-25 13:04:03 -05:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
imageLayers := blobManifest.Layers
|
|
|
|
|
|
|
|
for _, imageLayer := range imageLayers {
|
|
|
|
switch imageLayer.MediaType {
|
|
|
|
case types.OCILayer, types.DockerLayer:
|
|
|
|
return true, nil
|
|
|
|
|
|
|
|
default:
|
|
|
|
olu.Log.Debug().Msg("image media type not supported for scanning")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-01-25 13:04:03 -05:00
|
|
|
return false, errors.ErrScanNotSupported
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetImageTagsWithTimestamp returns a list of image tags with timestamp available in the specified repository.
|
|
|
|
func (olu OciLayoutUtils) GetImageTagsWithTimestamp(repo string) ([]TagInfo, error) {
|
|
|
|
tagsInfo := make([]TagInfo, 0)
|
|
|
|
|
|
|
|
manifests, err := olu.GetImageManifests(repo)
|
|
|
|
if err != nil {
|
|
|
|
olu.Log.Error().Err(err).Msg("unable to read image manifests")
|
|
|
|
|
|
|
|
return tagsInfo, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, manifest := range manifests {
|
|
|
|
digest := manifest.Digest
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
val, ok := manifest.Annotations[ispec.AnnotationRefName]
|
2021-01-25 13:04:03 -05:00
|
|
|
if ok {
|
|
|
|
imageBlobManifest, err := olu.GetImageBlobManifest(repo, digest)
|
|
|
|
if err != nil {
|
|
|
|
olu.Log.Error().Err(err).Msg("unable to read image blob manifest")
|
|
|
|
|
|
|
|
return tagsInfo, err
|
|
|
|
}
|
|
|
|
|
|
|
|
imageInfo, err := olu.GetImageInfo(repo, imageBlobManifest.Config.Digest)
|
|
|
|
if err != nil {
|
|
|
|
olu.Log.Error().Err(err).Msg("unable to read image info")
|
|
|
|
|
|
|
|
return tagsInfo, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var timeStamp time.Time
|
|
|
|
|
|
|
|
if len(imageInfo.History) != 0 {
|
|
|
|
timeStamp = *imageInfo.History[0].Created
|
|
|
|
} else {
|
|
|
|
timeStamp = time.Time{}
|
|
|
|
}
|
2021-05-26 12:22:31 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
tagsInfo = append(tagsInfo, TagInfo{Name: val, Timestamp: timeStamp, Digest: digest.String()})
|
2021-05-26 12:22:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-25 13:04:03 -05:00
|
|
|
return tagsInfo, nil
|
2021-05-26 12:22:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetImageDirAndTag(imageName string) (string, string) {
|
|
|
|
var imageDir string
|
|
|
|
|
|
|
|
var imageTag string
|
|
|
|
|
|
|
|
if strings.Contains(imageName, ":") {
|
|
|
|
splitImageName := strings.Split(imageName, ":")
|
|
|
|
imageDir = splitImageName[0]
|
|
|
|
imageTag = splitImageName[1]
|
|
|
|
} else {
|
|
|
|
imageDir = imageName
|
|
|
|
}
|
|
|
|
|
|
|
|
return imageDir, imageTag
|
|
|
|
}
|