2021-07-16 22:53:05 -05:00
|
|
|
package s3
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/json"
|
2021-12-13 14:23:31 -05:00
|
|
|
"errors"
|
2021-07-16 22:53:05 -05:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2021-12-21 08:19:40 -05:00
|
|
|
"time"
|
2021-07-16 22:53:05 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
// Add s3 support.
|
|
|
|
"github.com/docker/distribution/registry/storage/driver"
|
|
|
|
|
|
|
|
// Load s3 driver.
|
|
|
|
_ "github.com/docker/distribution/registry/storage/driver/s3-aws"
|
2021-07-16 22:53:05 -05:00
|
|
|
guuid "github.com/gofrs/uuid"
|
|
|
|
godigest "github.com/opencontainers/go-digest"
|
|
|
|
ispec "github.com/opencontainers/image-spec/specs-go/v1"
|
2022-01-31 16:33:07 -05:00
|
|
|
artifactspec "github.com/oras-project/artifacts-spec/specs-go/v1"
|
2021-07-16 22:53:05 -05:00
|
|
|
"github.com/rs/zerolog"
|
2021-12-13 14:23:31 -05:00
|
|
|
zerr "zotregistry.io/zot/errors"
|
2021-12-03 22:50:58 -05:00
|
|
|
"zotregistry.io/zot/pkg/extensions/monitoring"
|
|
|
|
zlog "zotregistry.io/zot/pkg/log"
|
|
|
|
"zotregistry.io/zot/pkg/storage"
|
2021-07-16 22:53:05 -05:00
|
|
|
)
|
|
|
|
|
2021-12-21 08:19:40 -05:00
|
|
|
const (
|
|
|
|
RLOCK = "RLock"
|
|
|
|
RWLOCK = "RWLock"
|
|
|
|
)
|
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
// ObjectStorage provides the image storage operations.
|
|
|
|
type ObjectStorage struct {
|
|
|
|
rootDir string
|
2021-12-13 14:23:31 -05:00
|
|
|
store driver.StorageDriver
|
2021-07-16 22:53:05 -05:00
|
|
|
lock *sync.RWMutex
|
|
|
|
blobUploads map[string]storage.BlobUpload
|
|
|
|
log zerolog.Logger
|
|
|
|
// We must keep track of multi part uploads to s3, because the lib
|
|
|
|
// which we are using doesn't cancel multiparts uploads
|
|
|
|
// see: https://github.com/distribution/distribution/blob/main/registry/storage/driver/s3-aws/s3.go#L545
|
2022-03-02 13:29:48 -05:00
|
|
|
multiPartUploads sync.Map
|
|
|
|
metrics monitoring.MetricServer
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (is *ObjectStorage) RootDir() string {
|
|
|
|
return is.rootDir
|
|
|
|
}
|
|
|
|
|
|
|
|
func (is *ObjectStorage) DirExists(d string) bool {
|
|
|
|
if fi, err := is.store.Stat(context.Background(), d); err == nil && fi.IsDir() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewObjectStorage returns a new image store backed by cloud storages.
|
|
|
|
// see https://github.com/docker/docker.github.io/tree/master/registry/storage-drivers
|
2022-02-09 19:51:35 -05:00
|
|
|
func NewImageStore(rootDir string, gc bool, gcDelay time.Duration, dedupe, commit bool,
|
|
|
|
log zlog.Logger, metrics monitoring.MetricServer,
|
2021-12-13 14:23:31 -05:00
|
|
|
store driver.StorageDriver) storage.ImageStore {
|
|
|
|
imgStore := &ObjectStorage{
|
2022-03-02 13:29:48 -05:00
|
|
|
rootDir: rootDir,
|
|
|
|
store: store,
|
|
|
|
lock: &sync.RWMutex{},
|
|
|
|
blobUploads: make(map[string]storage.BlobUpload),
|
|
|
|
log: log.With().Caller().Logger(),
|
|
|
|
multiPartUploads: sync.Map{},
|
|
|
|
metrics: metrics,
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
return imgStore
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// RLock read-lock.
|
2021-12-21 08:19:40 -05:00
|
|
|
func (is *ObjectStorage) RLock(lockStart *time.Time) {
|
|
|
|
*lockStart = time.Now()
|
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
is.lock.RLock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// RUnlock read-unlock.
|
2021-12-21 08:19:40 -05:00
|
|
|
func (is *ObjectStorage) RUnlock(lockStart *time.Time) {
|
2021-07-16 22:53:05 -05:00
|
|
|
is.lock.RUnlock()
|
2021-12-21 08:19:40 -05:00
|
|
|
|
|
|
|
lockEnd := time.Now()
|
|
|
|
// includes time spent in acquiring and holding a lock
|
|
|
|
latency := lockEnd.Sub(*lockStart)
|
|
|
|
monitoring.ObserveStorageLockLatency(is.metrics, latency, is.RootDir(), RLOCK) // histogram
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lock write-lock.
|
2021-12-21 08:19:40 -05:00
|
|
|
func (is *ObjectStorage) Lock(lockStart *time.Time) {
|
|
|
|
*lockStart = time.Now()
|
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
is.lock.Lock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unlock write-unlock.
|
2021-12-21 08:19:40 -05:00
|
|
|
func (is *ObjectStorage) Unlock(lockStart *time.Time) {
|
2021-07-16 22:53:05 -05:00
|
|
|
is.lock.Unlock()
|
2021-12-21 08:19:40 -05:00
|
|
|
|
|
|
|
lockEnd := time.Now()
|
|
|
|
// includes time spent in acquiring and holding a lock
|
|
|
|
latency := lockEnd.Sub(*lockStart)
|
|
|
|
monitoring.ObserveStorageLockLatency(is.metrics, latency, is.RootDir(), RWLOCK) // histogram
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (is *ObjectStorage) initRepo(name string) error {
|
|
|
|
repoDir := path.Join(is.rootDir, name)
|
|
|
|
|
|
|
|
// "oci-layout" file - create if it doesn't exist
|
|
|
|
ilPath := path.Join(repoDir, ispec.ImageLayoutFile)
|
|
|
|
if _, err := is.store.Stat(context.Background(), ilPath); err != nil {
|
|
|
|
il := ispec.ImageLayout{Version: ispec.ImageLayoutVersion}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
buf, err := json.Marshal(il)
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Msg("unable to marshal JSON")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := writeFile(is.store, ilPath, buf); err != nil {
|
|
|
|
is.log.Error().Err(err).Str("file", ilPath).Msg("unable to write file")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// "index.json" file - create if it doesn't exist
|
|
|
|
indexPath := path.Join(repoDir, "index.json")
|
|
|
|
if _, err := is.store.Stat(context.Background(), indexPath); err != nil {
|
|
|
|
index := ispec.Index{}
|
|
|
|
index.SchemaVersion = 2
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
buf, err := json.Marshal(index)
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Msg("unable to marshal JSON")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := writeFile(is.store, indexPath, buf); err != nil {
|
|
|
|
is.log.Error().Err(err).Str("file", ilPath).Msg("unable to write file")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// InitRepo creates an image repository under this store.
|
|
|
|
func (is *ObjectStorage) InitRepo(name string) error {
|
2021-12-21 08:19:40 -05:00
|
|
|
var lockLatency time.Time
|
|
|
|
|
|
|
|
is.Lock(&lockLatency)
|
|
|
|
defer is.Unlock(&lockLatency)
|
2021-07-16 22:53:05 -05:00
|
|
|
|
|
|
|
return is.initRepo(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValidateRepo validates that the repository layout is complaint with the OCI repo layout.
|
|
|
|
func (is *ObjectStorage) ValidateRepo(name string) (bool, error) {
|
|
|
|
// https://github.com/opencontainers/image-spec/blob/master/image-layout.md#content
|
|
|
|
// at least, expect at least 3 entries - ["blobs", "oci-layout", "index.json"]
|
|
|
|
// and an additional/optional BlobUploadDir in each image store
|
|
|
|
// for objects storage we can not create empty dirs, so we check only against index.json and oci-layout
|
|
|
|
dir := path.Join(is.rootDir, name)
|
|
|
|
if fi, err := is.store.Stat(context.Background(), dir); err != nil || !fi.IsDir() {
|
2021-12-13 14:23:31 -05:00
|
|
|
return false, zerr.ErrRepoNotFound
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
files, err := is.store.List(context.Background(), dir)
|
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Str("dir", dir).Msg("unable to read directory")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
|
|
|
return false, zerr.ErrRepoNotFound
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// nolint:gomnd
|
|
|
|
if len(files) < 2 {
|
2021-12-13 14:23:31 -05:00
|
|
|
return false, zerr.ErrRepoBadVersion
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
found := map[string]bool{
|
|
|
|
ispec.ImageLayoutFile: false,
|
|
|
|
"index.json": false,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
f, err := is.store.Stat(context.Background(), file)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasSuffix(file, "blobs") && !f.IsDir() {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
filename, err := filepath.Rel(dir, file)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
found[filename] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range found {
|
|
|
|
if !v && k != storage.BlobUploadDir {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
buf, err := is.store.GetContent(context.Background(), path.Join(dir, ispec.ImageLayoutFile))
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var il ispec.ImageLayout
|
|
|
|
if err := json.Unmarshal(buf, &il); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if il.Version != ispec.ImageLayoutVersion {
|
2021-12-13 14:23:31 -05:00
|
|
|
return false, zerr.ErrRepoBadVersion
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRepositories returns a list of all the repositories under this store.
|
|
|
|
func (is *ObjectStorage) GetRepositories() ([]string, error) {
|
2021-12-21 08:19:40 -05:00
|
|
|
var lockLatency time.Time
|
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
dir := is.rootDir
|
|
|
|
|
2021-12-21 08:19:40 -05:00
|
|
|
is.RLock(&lockLatency)
|
|
|
|
defer is.RUnlock(&lockLatency)
|
2021-07-16 22:53:05 -05:00
|
|
|
|
|
|
|
stores := make([]string, 0)
|
2021-12-13 14:23:31 -05:00
|
|
|
err := is.store.Walk(context.Background(), dir, func(fileInfo driver.FileInfo) error {
|
2021-07-16 22:53:05 -05:00
|
|
|
if !fileInfo.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
rel, err := filepath.Rel(is.rootDir, fileInfo.Path())
|
|
|
|
if err != nil {
|
2021-12-13 14:23:31 -05:00
|
|
|
return nil //nolint:nilerr // ignore paths that are not under root dir
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if ok, err := is.ValidateRepo(rel); !ok || err != nil {
|
2021-12-13 14:23:31 -05:00
|
|
|
return nil //nolint:nilerr // ignore invalid repos
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
stores = append(stores, rel)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
// if the root directory is not yet created then return an empty slice of repositories
|
2021-12-13 14:23:31 -05:00
|
|
|
var perr driver.PathNotFoundError
|
|
|
|
if errors.As(err, &perr) {
|
2021-07-16 22:53:05 -05:00
|
|
|
return stores, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return stores, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetImageTags returns a list of image tags available in the specified repository.
|
|
|
|
func (is *ObjectStorage) GetImageTags(repo string) ([]string, error) {
|
|
|
|
dir := path.Join(is.rootDir, repo)
|
|
|
|
if fi, err := is.store.Stat(context.Background(), dir); err != nil || !fi.IsDir() {
|
2021-12-13 14:23:31 -05:00
|
|
|
return nil, zerr.ErrRepoNotFound
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
buf, err := is.GetIndexContent(repo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var index ispec.Index
|
|
|
|
if err := json.Unmarshal(buf, &index); err != nil {
|
|
|
|
is.log.Error().Err(err).Str("dir", dir).Msg("invalid JSON")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
|
|
|
return nil, zerr.ErrRepoNotFound
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
tags := make([]string, 0)
|
|
|
|
|
|
|
|
for _, manifest := range index.Manifests {
|
|
|
|
v, ok := manifest.Annotations[ispec.AnnotationRefName]
|
|
|
|
if ok {
|
|
|
|
tags = append(tags, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tags, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetImageManifest returns the image manifest of an image in the specific repository.
|
|
|
|
func (is *ObjectStorage) GetImageManifest(repo string, reference string) ([]byte, string, string, error) {
|
2021-12-21 08:19:40 -05:00
|
|
|
var lockLatency time.Time
|
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
dir := path.Join(is.rootDir, repo)
|
|
|
|
if fi, err := is.store.Stat(context.Background(), dir); err != nil || !fi.IsDir() {
|
2021-12-13 14:23:31 -05:00
|
|
|
return nil, "", "", zerr.ErrRepoNotFound
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
buf, err := is.GetIndexContent(repo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
var index ispec.Index
|
|
|
|
if err := json.Unmarshal(buf, &index); err != nil {
|
|
|
|
is.log.Error().Err(err).Str("dir", dir).Msg("invalid JSON")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
return nil, "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
found := false
|
|
|
|
|
|
|
|
var digest godigest.Digest
|
|
|
|
|
|
|
|
mediaType := ""
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
for _, manifest := range index.Manifests {
|
|
|
|
if reference == manifest.Digest.String() {
|
|
|
|
digest = manifest.Digest
|
|
|
|
mediaType = manifest.MediaType
|
2021-07-16 22:53:05 -05:00
|
|
|
found = true
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
v, ok := manifest.Annotations[ispec.AnnotationRefName]
|
2021-07-16 22:53:05 -05:00
|
|
|
if ok && v == reference {
|
2021-12-13 14:23:31 -05:00
|
|
|
digest = manifest.Digest
|
|
|
|
mediaType = manifest.MediaType
|
2021-07-16 22:53:05 -05:00
|
|
|
found = true
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
2021-12-13 14:23:31 -05:00
|
|
|
return nil, "", "", zerr.ErrManifestNotFound
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
2022-03-02 13:29:48 -05:00
|
|
|
manifestPath := path.Join(dir, "blobs", digest.Algorithm().String(), digest.Encoded())
|
|
|
|
|
|
|
|
is.RLock(&lockLatency)
|
|
|
|
defer is.RUnlock(&lockLatency)
|
2021-07-16 22:53:05 -05:00
|
|
|
|
2022-03-02 13:29:48 -05:00
|
|
|
buf, err = is.store.GetContent(context.Background(), manifestPath)
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
2022-03-02 13:29:48 -05:00
|
|
|
is.log.Error().Err(err).Str("blob", manifestPath).Msg("failed to read manifest")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
return nil, "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
var manifest ispec.Manifest
|
|
|
|
if err := json.Unmarshal(buf, &manifest); err != nil {
|
|
|
|
is.log.Error().Err(err).Str("dir", dir).Msg("invalid JSON")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
return nil, "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
monitoring.IncDownloadCounter(is.metrics, repo)
|
|
|
|
|
|
|
|
return buf, digest.String(), mediaType, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutImageManifest adds an image manifest to the repository.
|
|
|
|
func (is *ObjectStorage) PutImageManifest(repo string, reference string, mediaType string,
|
|
|
|
body []byte) (string, error) {
|
|
|
|
if err := is.InitRepo(repo); err != nil {
|
|
|
|
is.log.Debug().Err(err).Msg("init repo")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if mediaType != ispec.MediaTypeImageManifest {
|
|
|
|
is.log.Debug().Interface("actual", mediaType).
|
|
|
|
Interface("expected", ispec.MediaTypeImageManifest).Msg("bad manifest media type")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
|
|
|
return "", zerr.ErrBadManifest
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(body) == 0 {
|
|
|
|
is.log.Debug().Int("len", len(body)).Msg("invalid body length")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
|
|
|
return "", zerr.ErrBadManifest
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
var m ispec.Manifest
|
|
|
|
if err := json.Unmarshal(body, &m); err != nil {
|
|
|
|
is.log.Error().Err(err).Msg("unable to unmarshal JSON")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
|
|
|
return "", zerr.ErrBadManifest
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if m.SchemaVersion != storage.SchemaVersion {
|
|
|
|
is.log.Error().Int("SchemaVersion", m.SchemaVersion).Msg("invalid manifest")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
|
|
|
return "", zerr.ErrBadManifest
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, l := range m.Layers {
|
|
|
|
digest := l.Digest
|
|
|
|
blobPath := is.BlobPath(repo, digest)
|
|
|
|
is.log.Info().Str("blobPath", blobPath).Str("reference", reference).Msg("manifest layers")
|
|
|
|
|
|
|
|
if _, err := is.store.Stat(context.Background(), blobPath); err != nil {
|
|
|
|
is.log.Error().Err(err).Str("blobPath", blobPath).Msg("unable to find blob")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
|
|
|
return digest.String(), zerr.ErrBlobNotFound
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mDigest := godigest.FromBytes(body)
|
|
|
|
refIsDigest := false
|
2021-12-13 14:23:31 -05:00
|
|
|
dgst, err := godigest.Parse(reference)
|
2021-07-16 22:53:05 -05:00
|
|
|
|
|
|
|
if err == nil {
|
2021-12-13 14:23:31 -05:00
|
|
|
if dgst.String() != mDigest.String() {
|
|
|
|
is.log.Error().Str("actual", mDigest.String()).Str("expected", dgst.String()).
|
2021-07-16 22:53:05 -05:00
|
|
|
Msg("manifest digest is not valid")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
|
|
|
return "", zerr.ErrBadManifest
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
refIsDigest = true
|
|
|
|
}
|
|
|
|
|
|
|
|
dir := path.Join(is.rootDir, repo)
|
|
|
|
|
|
|
|
buf, err := is.GetIndexContent(repo)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2022-03-02 13:29:48 -05:00
|
|
|
var lockLatency time.Time
|
|
|
|
|
|
|
|
is.Lock(&lockLatency)
|
|
|
|
defer is.Unlock(&lockLatency)
|
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
var index ispec.Index
|
|
|
|
if err := json.Unmarshal(buf, &index); err != nil {
|
|
|
|
is.log.Error().Err(err).Str("dir", dir).Msg("invalid JSON")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
|
|
|
return "", zerr.ErrRepoBadVersion
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
updateIndex := true
|
|
|
|
// create a new descriptor
|
2021-12-13 14:23:31 -05:00
|
|
|
desc := ispec.Descriptor{
|
|
|
|
MediaType: mediaType, Size: int64(len(body)), Digest: mDigest,
|
|
|
|
Platform: &ispec.Platform{Architecture: "amd64", OS: "linux"},
|
|
|
|
}
|
2021-07-16 22:53:05 -05:00
|
|
|
if !refIsDigest {
|
|
|
|
desc.Annotations = map[string]string{ispec.AnnotationRefName: reference}
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
for midx, manifest := range index.Manifests {
|
|
|
|
if reference == manifest.Digest.String() {
|
2021-07-16 22:53:05 -05:00
|
|
|
// nothing changed, so don't update
|
2021-12-13 14:23:31 -05:00
|
|
|
desc = manifest
|
2021-07-16 22:53:05 -05:00
|
|
|
updateIndex = false
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
v, ok := manifest.Annotations[ispec.AnnotationRefName]
|
2021-07-16 22:53:05 -05:00
|
|
|
if ok && v == reference {
|
2021-12-13 14:23:31 -05:00
|
|
|
if manifest.Digest.String() == mDigest.String() {
|
2021-07-16 22:53:05 -05:00
|
|
|
// nothing changed, so don't update
|
2021-12-13 14:23:31 -05:00
|
|
|
desc = manifest
|
2021-07-16 22:53:05 -05:00
|
|
|
updateIndex = false
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// manifest contents have changed for the same tag,
|
|
|
|
// so update index.json descriptor
|
2022-03-07 03:45:10 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
is.log.Info().
|
|
|
|
Int64("old size", desc.Size).
|
|
|
|
Int64("new size", int64(len(body))).
|
|
|
|
Str("old digest", desc.Digest.String()).
|
|
|
|
Str("new digest", mDigest.String()).
|
|
|
|
Msg("updating existing tag with new manifest contents")
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
desc = manifest
|
2021-07-16 22:53:05 -05:00
|
|
|
desc.Size = int64(len(body))
|
|
|
|
desc.Digest = mDigest
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
index.Manifests = append(index.Manifests[:midx], index.Manifests[midx+1:]...)
|
2021-07-16 22:53:05 -05:00
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !updateIndex {
|
|
|
|
return desc.Digest.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// write manifest to "blobs"
|
|
|
|
dir = path.Join(is.rootDir, repo, "blobs", mDigest.Algorithm().String())
|
|
|
|
manifestPath := path.Join(dir, mDigest.Encoded())
|
|
|
|
|
|
|
|
if err = is.store.PutContent(context.Background(), manifestPath, body); err != nil {
|
|
|
|
is.log.Error().Err(err).Str("file", manifestPath).Msg("unable to write")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// now update "index.json"
|
|
|
|
index.Manifests = append(index.Manifests, desc)
|
|
|
|
dir = path.Join(is.rootDir, repo)
|
|
|
|
indexPath := path.Join(dir, "index.json")
|
|
|
|
buf, err = json.Marshal(index)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Str("file", indexPath).Msg("unable to marshal JSON")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = is.store.PutContent(context.Background(), indexPath, buf); err != nil {
|
|
|
|
is.log.Error().Err(err).Str("file", manifestPath).Msg("unable to write")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
monitoring.SetStorageUsage(is.metrics, is.rootDir, repo)
|
|
|
|
monitoring.IncUploadCounter(is.metrics, repo)
|
|
|
|
|
|
|
|
return desc.Digest.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteImageManifest deletes the image manifest from the repository.
|
|
|
|
func (is *ObjectStorage) DeleteImageManifest(repo string, reference string) error {
|
2021-12-21 08:19:40 -05:00
|
|
|
var lockLatency time.Time
|
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
dir := path.Join(is.rootDir, repo)
|
|
|
|
if fi, err := is.store.Stat(context.Background(), dir); err != nil || !fi.IsDir() {
|
2021-12-13 14:23:31 -05:00
|
|
|
return zerr.ErrRepoNotFound
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
isTag := false
|
|
|
|
|
|
|
|
// as per spec "reference" can only be a digest and not a tag
|
2021-12-13 14:23:31 -05:00
|
|
|
dgst, err := godigest.Parse(reference)
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
is.log.Debug().Str("invalid digest: ", reference).Msg("storage: assuming tag")
|
|
|
|
|
|
|
|
isTag = true
|
|
|
|
}
|
|
|
|
|
|
|
|
buf, err := is.GetIndexContent(repo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var index ispec.Index
|
|
|
|
if err := json.Unmarshal(buf, &index); err != nil {
|
|
|
|
is.log.Error().Err(err).Str("dir", dir).Msg("invalid JSON")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
found := false
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
var manifest ispec.Descriptor
|
2021-07-16 22:53:05 -05:00
|
|
|
|
|
|
|
// we are deleting, so keep only those manifests that don't match
|
|
|
|
outIndex := index
|
|
|
|
outIndex.Manifests = []ispec.Descriptor{}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
for _, manifest = range index.Manifests {
|
2021-07-16 22:53:05 -05:00
|
|
|
if isTag {
|
2021-12-13 14:23:31 -05:00
|
|
|
tag, ok := manifest.Annotations[ispec.AnnotationRefName]
|
2021-07-16 22:53:05 -05:00
|
|
|
if ok && tag == reference {
|
|
|
|
is.log.Debug().Str("deleting tag", tag).Msg("")
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
dgst = manifest.Digest
|
2021-07-16 22:53:05 -05:00
|
|
|
|
|
|
|
found = true
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
} else if reference == manifest.Digest.String() {
|
2021-07-16 22:53:05 -05:00
|
|
|
is.log.Debug().Str("deleting reference", reference).Msg("")
|
|
|
|
found = true
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
outIndex.Manifests = append(outIndex.Manifests, manifest)
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
2021-12-13 14:23:31 -05:00
|
|
|
return zerr.ErrManifestNotFound
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
2022-03-02 13:29:48 -05:00
|
|
|
is.Lock(&lockLatency)
|
|
|
|
defer is.Unlock(&lockLatency)
|
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
// now update "index.json"
|
|
|
|
dir = path.Join(is.rootDir, repo)
|
|
|
|
file := path.Join(dir, "index.json")
|
|
|
|
buf, err = json.Marshal(outIndex)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := writeFile(is.store, file, buf); err != nil {
|
|
|
|
is.log.Debug().Str("deleting reference", reference).Msg("")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete blob only when blob digest not present in manifest entry.
|
|
|
|
// e.g. 1.0.1 & 1.0.2 have same blob digest so if we delete 1.0.1, blob should not be removed.
|
|
|
|
toDelete := true
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
for _, manifest = range outIndex.Manifests {
|
|
|
|
if dgst.String() == manifest.Digest.String() {
|
2021-07-16 22:53:05 -05:00
|
|
|
toDelete = false
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if toDelete {
|
2021-12-13 14:23:31 -05:00
|
|
|
p := path.Join(dir, "blobs", dgst.Algorithm().String(), dgst.Encoded())
|
2021-07-16 22:53:05 -05:00
|
|
|
|
|
|
|
err = is.store.Delete(context.Background(), p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
monitoring.SetStorageUsage(is.metrics, is.rootDir, repo)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// BlobUploadPath returns the upload path for a blob in this store.
|
|
|
|
func (is *ObjectStorage) BlobUploadPath(repo string, uuid string) string {
|
|
|
|
dir := path.Join(is.rootDir, repo)
|
|
|
|
blobUploadPath := path.Join(dir, storage.BlobUploadDir, uuid)
|
|
|
|
|
|
|
|
return blobUploadPath
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewBlobUpload returns the unique ID for an upload in progress.
|
|
|
|
func (is *ObjectStorage) NewBlobUpload(repo string) (string, error) {
|
|
|
|
if err := is.InitRepo(repo); err != nil {
|
|
|
|
is.log.Error().Err(err).Msg("error initializing repo")
|
|
|
|
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
uuid, err := guuid.NewV4()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
uid := uuid.String()
|
2021-07-16 22:53:05 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
blobUploadPath := is.BlobUploadPath(repo, uid)
|
2021-07-16 22:53:05 -05:00
|
|
|
|
|
|
|
// here we should create an empty multi part upload, but that's not possible
|
|
|
|
// so we just create a regular empty file which will be overwritten by FinishBlobUpload
|
|
|
|
err = is.store.PutContent(context.Background(), blobUploadPath, []byte{})
|
|
|
|
if err != nil {
|
2021-12-13 14:23:31 -05:00
|
|
|
return "", zerr.ErrRepoNotFound
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
return uid, nil
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlobUpload returns the current size of a blob upload.
|
|
|
|
func (is *ObjectStorage) GetBlobUpload(repo string, uuid string) (int64, error) {
|
|
|
|
var fileSize int64
|
|
|
|
|
|
|
|
blobUploadPath := is.BlobUploadPath(repo, uuid)
|
|
|
|
|
|
|
|
// if it's not a multipart upload check for the regular empty file
|
|
|
|
// created by NewBlobUpload, it should have 0 size every time
|
2022-03-02 13:29:48 -05:00
|
|
|
_, hasStarted := is.multiPartUploads.Load(blobUploadPath)
|
|
|
|
if !hasStarted {
|
2021-12-13 14:23:31 -05:00
|
|
|
binfo, err := is.store.Stat(context.Background(), blobUploadPath)
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
2021-12-13 14:23:31 -05:00
|
|
|
var perr driver.PathNotFoundError
|
|
|
|
if errors.As(err, &perr) {
|
|
|
|
return -1, zerr.ErrUploadNotFound
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
fileSize = binfo.Size()
|
2021-07-16 22:53:05 -05:00
|
|
|
} else {
|
|
|
|
// otherwise get the size of multi parts upload
|
|
|
|
fi, err := getMultipartFileWriter(is, blobUploadPath)
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
fileSize = fi.Size()
|
|
|
|
}
|
|
|
|
|
|
|
|
return fileSize, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutBlobChunkStreamed appends another chunk of data to the specified blob. It returns
|
|
|
|
// the number of actual bytes to the blob.
|
|
|
|
func (is *ObjectStorage) PutBlobChunkStreamed(repo string, uuid string, body io.Reader) (int64, error) {
|
|
|
|
if err := is.InitRepo(repo); err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
blobUploadPath := is.BlobUploadPath(repo, uuid)
|
|
|
|
|
|
|
|
_, err := is.store.Stat(context.Background(), blobUploadPath)
|
|
|
|
if err != nil {
|
2021-12-13 14:23:31 -05:00
|
|
|
return -1, zerr.ErrUploadNotFound
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
file, err := getMultipartFileWriter(is, blobUploadPath)
|
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Msg("failed to create multipart upload")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
|
|
|
|
_, err = buf.ReadFrom(body)
|
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Msg("failed to read blob")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
nbytes, err := file.Write(buf.Bytes())
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Msg("failed to append to file")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
return int64(nbytes), err
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// PutBlobChunk writes another chunk of data to the specified blob. It returns
|
|
|
|
// the number of actual bytes to the blob.
|
|
|
|
func (is *ObjectStorage) PutBlobChunk(repo string, uuid string, from int64, to int64,
|
|
|
|
body io.Reader) (int64, error) {
|
|
|
|
if err := is.InitRepo(repo); err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
blobUploadPath := is.BlobUploadPath(repo, uuid)
|
|
|
|
|
|
|
|
_, err := is.store.Stat(context.Background(), blobUploadPath)
|
|
|
|
if err != nil {
|
2021-12-13 14:23:31 -05:00
|
|
|
return -1, zerr.ErrUploadNotFound
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
file, err := getMultipartFileWriter(is, blobUploadPath)
|
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Msg("failed to create multipart upload")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
if from != file.Size() {
|
2022-03-02 13:29:48 -05:00
|
|
|
// cancel multipart upload
|
|
|
|
is.multiPartUploads.Delete(blobUploadPath)
|
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
err := file.Cancel()
|
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Msg("failed to cancel multipart upload")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
is.log.Error().Int64("expected", from).Int64("actual", file.Size()).
|
|
|
|
Msg("invalid range start for blob upload")
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
return -1, zerr.ErrBadUploadRange
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
|
|
|
|
_, err = buf.ReadFrom(body)
|
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Msg("failed to read blob")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
nbytes, err := file.Write(buf.Bytes())
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Msg("failed to append to file")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
return int64(nbytes), err
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// BlobUploadInfo returns the current blob size in bytes.
|
|
|
|
func (is *ObjectStorage) BlobUploadInfo(repo string, uuid string) (int64, error) {
|
|
|
|
var fileSize int64
|
|
|
|
|
|
|
|
blobUploadPath := is.BlobUploadPath(repo, uuid)
|
|
|
|
|
|
|
|
// if it's not a multipart upload check for the regular empty file
|
|
|
|
// created by NewBlobUpload, it should have 0 size every time
|
2022-03-02 13:29:48 -05:00
|
|
|
_, hasStarted := is.multiPartUploads.Load(blobUploadPath)
|
|
|
|
if !hasStarted {
|
2021-12-13 14:23:31 -05:00
|
|
|
uploadInfo, err := is.store.Stat(context.Background(), blobUploadPath)
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Str("blob", blobUploadPath).Msg("failed to stat blob")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
fileSize = uploadInfo.Size()
|
2021-07-16 22:53:05 -05:00
|
|
|
} else {
|
|
|
|
// otherwise get the size of multi parts upload
|
2021-12-13 14:23:31 -05:00
|
|
|
binfo, err := getMultipartFileWriter(is, blobUploadPath)
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Str("blob", blobUploadPath).Msg("failed to stat blob")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
fileSize = binfo.Size()
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return fileSize, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FinishBlobUpload finalizes the blob upload and moves blob the repository.
|
|
|
|
func (is *ObjectStorage) FinishBlobUpload(repo string, uuid string, body io.Reader, digest string) error {
|
|
|
|
dstDigest, err := godigest.Parse(digest)
|
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Str("digest", digest).Msg("failed to parse digest")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
|
|
|
return zerr.ErrBadBlobDigest
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
src := is.BlobUploadPath(repo, uuid)
|
|
|
|
|
|
|
|
// complete multiUploadPart
|
|
|
|
fileWriter, err := is.store.Writer(context.Background(), src, true)
|
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Str("blob", src).Msg("failed to open blob")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
|
|
|
return zerr.ErrBadBlobDigest
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
2022-03-02 13:29:48 -05:00
|
|
|
defer fileWriter.Close()
|
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
if err := fileWriter.Commit(); err != nil {
|
|
|
|
is.log.Error().Err(err).Msg("failed to commit file")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := fileWriter.Close(); err != nil {
|
|
|
|
is.log.Error().Err(err).Msg("failed to close file")
|
|
|
|
}
|
|
|
|
|
|
|
|
fileReader, err := is.store.Reader(context.Background(), src, 0)
|
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Str("blob", src).Msg("failed to open file")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
|
|
|
return zerr.ErrUploadNotFound
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
srcDigest, err := godigest.FromReader(fileReader)
|
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Str("blob", src).Msg("failed to open blob")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
|
|
|
return zerr.ErrBadBlobDigest
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if srcDigest != dstDigest {
|
|
|
|
is.log.Error().Str("srcDigest", srcDigest.String()).
|
|
|
|
Str("dstDigest", dstDigest.String()).Msg("actual digest not equal to expected digest")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
|
|
|
return zerr.ErrBadBlobDigest
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fileReader.Close()
|
|
|
|
|
|
|
|
dst := is.BlobPath(repo, dstDigest)
|
|
|
|
|
2022-03-02 13:29:48 -05:00
|
|
|
var lockLatency time.Time
|
|
|
|
|
|
|
|
is.Lock(&lockLatency)
|
|
|
|
defer is.Unlock(&lockLatency)
|
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
if err := is.store.Move(context.Background(), src, dst); err != nil {
|
|
|
|
is.log.Error().Err(err).Str("src", src).Str("dstDigest", dstDigest.String()).
|
|
|
|
Str("dst", dst).Msg("unable to finish blob")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-02 13:29:48 -05:00
|
|
|
is.multiPartUploads.Delete(src)
|
2021-07-16 22:53:05 -05:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FullBlobUpload handles a full blob upload, and no partial session is created.
|
|
|
|
func (is *ObjectStorage) FullBlobUpload(repo string, body io.Reader, digest string) (string, int64, error) {
|
|
|
|
if err := is.InitRepo(repo); err != nil {
|
|
|
|
return "", -1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
dstDigest, err := godigest.Parse(digest)
|
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Str("digest", digest).Msg("failed to parse digest")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
|
|
|
return "", -1, zerr.ErrBadBlobDigest
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
u, err := guuid.NewV4()
|
|
|
|
if err != nil {
|
|
|
|
return "", -1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
uuid := u.String()
|
|
|
|
src := is.BlobUploadPath(repo, uuid)
|
|
|
|
digester := sha256.New()
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
|
|
|
|
_, err = buf.ReadFrom(body)
|
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Msg("failed to read blob")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
return "", -1, err
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
nbytes, err := writeFile(is.store, src, buf.Bytes())
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Msg("failed to write blob")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
return "", -1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = digester.Write(buf.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Msg("digester failed to write")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
return "", -1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
srcDigest := godigest.NewDigestFromEncoded(godigest.SHA256, fmt.Sprintf("%x", digester.Sum(nil)))
|
|
|
|
if srcDigest != dstDigest {
|
|
|
|
is.log.Error().Str("srcDigest", srcDigest.String()).
|
|
|
|
Str("dstDigest", dstDigest.String()).Msg("actual digest not equal to expected digest")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
|
|
|
return "", -1, zerr.ErrBadBlobDigest
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
2021-12-21 08:19:40 -05:00
|
|
|
var lockLatency time.Time
|
|
|
|
|
|
|
|
is.Lock(&lockLatency)
|
|
|
|
defer is.Unlock(&lockLatency)
|
2021-07-16 22:53:05 -05:00
|
|
|
|
|
|
|
dst := is.BlobPath(repo, dstDigest)
|
|
|
|
|
|
|
|
if err := is.store.Move(context.Background(), src, dst); err != nil {
|
|
|
|
is.log.Error().Err(err).Str("src", src).Str("dstDigest", dstDigest.String()).
|
|
|
|
Str("dst", dst).Msg("unable to finish blob")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
return "", -1, err
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
return uuid, int64(nbytes), nil
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (is *ObjectStorage) DedupeBlob(src string, dstDigest godigest.Digest, dst string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteBlobUpload deletes an existing blob upload that is currently in progress.
|
|
|
|
func (is *ObjectStorage) DeleteBlobUpload(repo string, uuid string) error {
|
|
|
|
blobUploadPath := is.BlobUploadPath(repo, uuid)
|
|
|
|
if err := is.store.Delete(context.Background(), blobUploadPath); err != nil {
|
|
|
|
is.log.Error().Err(err).Str("blobUploadPath", blobUploadPath).Msg("error deleting blob upload")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// BlobPath returns the repository path of a blob.
|
|
|
|
func (is *ObjectStorage) BlobPath(repo string, digest godigest.Digest) string {
|
|
|
|
return path.Join(is.rootDir, repo, "blobs", digest.Algorithm().String(), digest.Encoded())
|
|
|
|
}
|
|
|
|
|
|
|
|
// CheckBlob verifies a blob and returns true if the blob is correct.
|
|
|
|
func (is *ObjectStorage) CheckBlob(repo string, digest string) (bool, int64, error) {
|
2021-12-21 08:19:40 -05:00
|
|
|
var lockLatency time.Time
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
dgst, err := godigest.Parse(digest)
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Str("digest", digest).Msg("failed to parse digest")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
|
|
|
return false, -1, zerr.ErrBadBlobDigest
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
blobPath := is.BlobPath(repo, dgst)
|
2021-07-16 22:53:05 -05:00
|
|
|
|
2021-12-21 08:19:40 -05:00
|
|
|
is.RLock(&lockLatency)
|
|
|
|
defer is.RUnlock(&lockLatency)
|
2021-07-16 22:53:05 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
binfo, err := is.store.Stat(context.Background(), blobPath)
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
2021-12-13 14:23:31 -05:00
|
|
|
var perr driver.PathNotFoundError
|
|
|
|
if errors.As(err, &perr) {
|
|
|
|
return false, -1, zerr.ErrBlobNotFound
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
is.log.Error().Err(err).Str("blob", blobPath).Msg("failed to stat blob")
|
|
|
|
|
|
|
|
return false, -1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
is.log.Debug().Str("blob path", blobPath).Msg("blob path found")
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
return true, binfo.Size(), nil
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlob returns a stream to read the blob.
|
|
|
|
// blob selector instead of directly downloading the blob.
|
|
|
|
func (is *ObjectStorage) GetBlob(repo string, digest string, mediaType string) (io.Reader, int64, error) {
|
2021-12-21 08:19:40 -05:00
|
|
|
var lockLatency time.Time
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
dgst, err := godigest.Parse(digest)
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Str("digest", digest).Msg("failed to parse digest")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
|
|
|
return nil, -1, zerr.ErrBadBlobDigest
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
blobPath := is.BlobPath(repo, dgst)
|
2021-07-16 22:53:05 -05:00
|
|
|
|
2021-12-21 08:19:40 -05:00
|
|
|
is.RLock(&lockLatency)
|
|
|
|
defer is.RUnlock(&lockLatency)
|
2021-07-16 22:53:05 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
binfo, err := is.store.Stat(context.Background(), blobPath)
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Str("blob", blobPath).Msg("failed to stat blob")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
|
|
|
return nil, -1, zerr.ErrBlobNotFound
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
blobReader, err := is.store.Reader(context.Background(), blobPath, 0)
|
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Str("blob", blobPath).Msg("failed to open blob")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
return nil, -1, err
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
return blobReader, binfo.Size(), nil
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (is *ObjectStorage) GetBlobContent(repo string, digest string) ([]byte, error) {
|
|
|
|
blob, _, err := is.GetBlob(repo, digest, ispec.MediaTypeImageManifest)
|
|
|
|
if err != nil {
|
|
|
|
return []byte{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
|
|
|
|
_, err = buf.ReadFrom(blob)
|
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Msg("failed to read blob")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
return []byte{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2022-01-31 16:33:07 -05:00
|
|
|
func (is *ObjectStorage) GetReferrers(repo, digest string, mediaType string) ([]artifactspec.Descriptor, error) {
|
2021-12-13 14:23:31 -05:00
|
|
|
return nil, zerr.ErrMethodNotSupported
|
2021-10-29 21:10:55 -05:00
|
|
|
}
|
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
func (is *ObjectStorage) GetIndexContent(repo string) ([]byte, error) {
|
2022-03-02 13:29:48 -05:00
|
|
|
var lockLatency time.Time
|
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
dir := path.Join(is.rootDir, repo)
|
|
|
|
|
2022-03-02 13:29:48 -05:00
|
|
|
is.RLock(&lockLatency)
|
|
|
|
defer is.RUnlock(&lockLatency)
|
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
buf, err := is.store.GetContent(context.Background(), path.Join(dir, "index.json"))
|
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Str("dir", dir).Msg("failed to read index.json")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
|
|
|
return []byte{}, zerr.ErrRepoNotFound
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return buf, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteBlob removes the blob from the repository.
|
|
|
|
func (is *ObjectStorage) DeleteBlob(repo string, digest string) error {
|
2021-12-21 08:19:40 -05:00
|
|
|
var lockLatency time.Time
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
dgst, err := godigest.Parse(digest)
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Str("digest", digest).Msg("failed to parse digest")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
|
|
|
return zerr.ErrBlobNotFound
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
blobPath := is.BlobPath(repo, dgst)
|
2021-07-16 22:53:05 -05:00
|
|
|
|
2021-12-21 08:19:40 -05:00
|
|
|
is.Lock(&lockLatency)
|
|
|
|
defer is.Unlock(&lockLatency)
|
2021-07-16 22:53:05 -05:00
|
|
|
|
|
|
|
_, err = is.store.Stat(context.Background(), blobPath)
|
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Str("blob", blobPath).Msg("failed to stat blob")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
|
|
|
return zerr.ErrBlobNotFound
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := is.store.Delete(context.Background(), blobPath); err != nil {
|
|
|
|
is.log.Error().Err(err).Str("blobPath", blobPath).Msg("unable to remove blob path")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do not use for multipart upload, buf must not be empty.
|
|
|
|
// If you want to create an empty file use is.store.PutContent().
|
2021-12-13 14:23:31 -05:00
|
|
|
func writeFile(store driver.StorageDriver, filepath string, buf []byte) (int, error) {
|
2021-07-16 22:53:05 -05:00
|
|
|
var n int
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
if stwr, err := store.Writer(context.Background(), filepath, false); err == nil {
|
|
|
|
defer stwr.Close()
|
2021-07-16 22:53:05 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
if n, err = stwr.Write(buf); err != nil {
|
2021-07-16 22:53:05 -05:00
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
if err := stwr.Commit(); err != nil {
|
2021-07-16 22:53:05 -05:00
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
2022-03-02 13:29:48 -05:00
|
|
|
// get a multipart upload FileWriter based on wheather or not one has already been started.
|
2021-12-13 14:23:31 -05:00
|
|
|
func getMultipartFileWriter(imgStore *ObjectStorage, filepath string) (driver.FileWriter, error) {
|
|
|
|
var file driver.FileWriter
|
2021-07-16 22:53:05 -05:00
|
|
|
|
|
|
|
var err error
|
|
|
|
|
2022-03-02 13:29:48 -05:00
|
|
|
_, hasStarted := imgStore.multiPartUploads.Load(filepath)
|
|
|
|
if !hasStarted {
|
|
|
|
// start multipart upload
|
2021-12-13 14:23:31 -05:00
|
|
|
file, err = imgStore.store.Writer(context.Background(), filepath, false)
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
return file, err
|
|
|
|
}
|
2022-03-02 13:29:48 -05:00
|
|
|
|
|
|
|
imgStore.multiPartUploads.Store(filepath, true)
|
2021-07-16 22:53:05 -05:00
|
|
|
} else {
|
2022-03-02 13:29:48 -05:00
|
|
|
// continue multipart upload
|
2021-12-13 14:23:31 -05:00
|
|
|
file, err = imgStore.store.Writer(context.Background(), filepath, true)
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
return file, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return file, nil
|
|
|
|
}
|