2014-11-22 03:39:52 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2015-02-12 00:49:49 +00:00
|
|
|
"github.com/docker/distribution"
|
2015-02-09 22:44:58 +00:00
|
|
|
ctxu "github.com/docker/distribution/context"
|
2015-01-02 21:21:29 +00:00
|
|
|
"github.com/docker/distribution/manifest"
|
2014-11-26 20:52:52 +00:00
|
|
|
"github.com/docker/libtrust"
|
2014-11-22 03:39:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type manifestStore struct {
|
2015-01-17 02:24:07 +00:00
|
|
|
repository *repository
|
|
|
|
|
2015-01-14 20:02:43 +00:00
|
|
|
revisionStore *revisionStore
|
|
|
|
tagStore *tagStore
|
2014-11-22 03:39:52 +00:00
|
|
|
}
|
|
|
|
|
2015-02-12 00:49:49 +00:00
|
|
|
var _ distribution.ManifestService = &manifestStore{}
|
2014-11-22 03:39:52 +00:00
|
|
|
|
2015-01-17 02:24:07 +00:00
|
|
|
// func (ms *manifestStore) Repository() Repository {
|
|
|
|
// return ms.repository
|
|
|
|
// }
|
|
|
|
|
|
|
|
func (ms *manifestStore) Tags() ([]string, error) {
|
2015-02-09 22:44:58 +00:00
|
|
|
ctxu.GetLogger(ms.repository.ctx).Debug("(*manifestStore).Tags")
|
2015-01-17 02:24:07 +00:00
|
|
|
return ms.tagStore.tags()
|
2014-12-09 19:06:51 +00:00
|
|
|
}
|
|
|
|
|
2015-01-17 02:24:07 +00:00
|
|
|
func (ms *manifestStore) Exists(tag string) (bool, error) {
|
2015-02-09 22:44:58 +00:00
|
|
|
ctxu.GetLogger(ms.repository.ctx).Debug("(*manifestStore).Exists")
|
2015-01-17 02:24:07 +00:00
|
|
|
return ms.tagStore.exists(tag)
|
2014-11-22 03:39:52 +00:00
|
|
|
}
|
|
|
|
|
2015-01-17 02:24:07 +00:00
|
|
|
func (ms *manifestStore) Get(tag string) (*manifest.SignedManifest, error) {
|
2015-02-09 22:44:58 +00:00
|
|
|
ctxu.GetLogger(ms.repository.ctx).Debug("(*manifestStore).Get")
|
2015-01-17 02:24:07 +00:00
|
|
|
dgst, err := ms.tagStore.resolve(tag)
|
2014-11-22 03:39:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-01-17 02:24:07 +00:00
|
|
|
return ms.revisionStore.get(dgst)
|
2014-11-22 03:39:52 +00:00
|
|
|
}
|
|
|
|
|
2015-01-17 02:24:07 +00:00
|
|
|
func (ms *manifestStore) Put(tag string, manifest *manifest.SignedManifest) error {
|
2015-02-09 22:44:58 +00:00
|
|
|
ctxu.GetLogger(ms.repository.ctx).Debug("(*manifestStore).Put")
|
|
|
|
|
2015-01-28 22:54:09 +00:00
|
|
|
// TODO(stevvooe): Add check here to see if the revision is already
|
|
|
|
// present in the repository. If it is, we should merge the signatures, do
|
|
|
|
// a shallow verify (or a full one, doesn't matter) and return an error
|
|
|
|
// indicating what happened.
|
|
|
|
|
2015-01-14 20:02:43 +00:00
|
|
|
// Verify the manifest.
|
2015-01-17 02:24:07 +00:00
|
|
|
if err := ms.verifyManifest(tag, manifest); err != nil {
|
2014-11-22 03:39:52 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-01-14 20:02:43 +00:00
|
|
|
// Store the revision of the manifest
|
2015-01-17 02:24:07 +00:00
|
|
|
revision, err := ms.revisionStore.put(manifest)
|
2015-01-14 20:02:43 +00:00
|
|
|
if err != nil {
|
2014-11-22 03:39:52 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-01-14 20:02:43 +00:00
|
|
|
// Now, tag the manifest
|
2015-01-17 02:24:07 +00:00
|
|
|
return ms.tagStore.tag(tag, revision)
|
2014-11-22 03:39:52 +00:00
|
|
|
}
|
|
|
|
|
2015-01-14 20:02:43 +00:00
|
|
|
// Delete removes all revisions of the given tag. We may want to change these
|
|
|
|
// semantics in the future, but this will maintain consistency. The underlying
|
|
|
|
// blobs are left alone.
|
2015-01-17 02:24:07 +00:00
|
|
|
func (ms *manifestStore) Delete(tag string) error {
|
2015-02-09 22:44:58 +00:00
|
|
|
ctxu.GetLogger(ms.repository.ctx).Debug("(*manifestStore).Delete")
|
|
|
|
|
2015-01-17 02:24:07 +00:00
|
|
|
revisions, err := ms.tagStore.revisions(tag)
|
2014-11-26 20:52:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-01-14 20:02:43 +00:00
|
|
|
for _, revision := range revisions {
|
2015-01-17 02:24:07 +00:00
|
|
|
if err := ms.revisionStore.delete(revision); err != nil {
|
2014-11-26 20:52:52 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-17 02:24:07 +00:00
|
|
|
return ms.tagStore.delete(tag)
|
2014-11-22 03:39:52 +00:00
|
|
|
}
|
|
|
|
|
2015-01-14 20:02:43 +00:00
|
|
|
// verifyManifest ensures that the manifest content is valid from the
|
|
|
|
// perspective of the registry. It ensures that the name and tag match and
|
|
|
|
// that the signature is valid for the enclosed payload. As a policy, the
|
|
|
|
// registry only tries to store valid content, leaving trust policies of that
|
|
|
|
// content up to consumers.
|
2015-01-17 02:24:07 +00:00
|
|
|
func (ms *manifestStore) verifyManifest(tag string, mnfst *manifest.SignedManifest) error {
|
2015-02-13 21:59:50 +00:00
|
|
|
var errs distribution.ErrManifestVerification
|
2015-01-17 02:24:07 +00:00
|
|
|
if mnfst.Name != ms.repository.Name() {
|
2014-12-02 00:13:01 +00:00
|
|
|
// TODO(stevvooe): This needs to be an exported error
|
2015-01-17 02:24:07 +00:00
|
|
|
errs = append(errs, fmt.Errorf("repository name does not match manifest name"))
|
2014-11-22 03:39:52 +00:00
|
|
|
}
|
|
|
|
|
2015-01-02 23:46:47 +00:00
|
|
|
if mnfst.Tag != tag {
|
2014-12-02 00:13:01 +00:00
|
|
|
// TODO(stevvooe): This needs to be an exported error.
|
|
|
|
errs = append(errs, fmt.Errorf("tag does not match manifest tag"))
|
2014-11-22 03:39:52 +00:00
|
|
|
}
|
|
|
|
|
2015-01-02 23:46:47 +00:00
|
|
|
if _, err := manifest.Verify(mnfst); err != nil {
|
2014-11-26 20:52:52 +00:00
|
|
|
switch err {
|
|
|
|
case libtrust.ErrMissingSignatureKey, libtrust.ErrInvalidJSONContent, libtrust.ErrMissingSignatureKey:
|
2015-02-13 21:59:50 +00:00
|
|
|
errs = append(errs, distribution.ErrManifestUnverified{})
|
2014-11-26 20:52:52 +00:00
|
|
|
default:
|
2014-12-02 00:13:01 +00:00
|
|
|
if err.Error() == "invalid signature" { // TODO(stevvooe): This should be exported by libtrust
|
2015-02-13 21:59:50 +00:00
|
|
|
errs = append(errs, distribution.ErrManifestUnverified{})
|
2014-12-02 00:13:01 +00:00
|
|
|
} else {
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
2014-11-26 20:52:52 +00:00
|
|
|
}
|
|
|
|
}
|
2014-11-22 03:39:52 +00:00
|
|
|
|
2015-01-02 23:46:47 +00:00
|
|
|
for _, fsLayer := range mnfst.FSLayers {
|
2015-01-17 02:24:07 +00:00
|
|
|
exists, err := ms.repository.Layers().Exists(fsLayer.BlobSum)
|
2014-11-22 03:39:52 +00:00
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !exists {
|
2015-02-12 00:49:49 +00:00
|
|
|
errs = append(errs, distribution.ErrUnknownLayer{FSLayer: fsLayer})
|
2014-11-22 03:39:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(errs) != 0 {
|
|
|
|
// TODO(stevvooe): These need to be recoverable by a caller.
|
2014-11-26 20:52:52 +00:00
|
|
|
return errs
|
2014-11-22 03:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|