2014-11-22 04:39:52 +01:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-12-09 22:40:44 +01:00
|
|
|
"strings"
|
2014-11-22 04:39:52 +01:00
|
|
|
|
2015-01-14 21:02:43 +01:00
|
|
|
"github.com/docker/distribution/digest"
|
2015-01-02 22:21:29 +01:00
|
|
|
"github.com/docker/distribution/manifest"
|
2014-12-24 01:01:38 +01:00
|
|
|
"github.com/docker/distribution/storagedriver"
|
2014-11-26 21:52:52 +01:00
|
|
|
"github.com/docker/libtrust"
|
2014-11-22 04:39:52 +01:00
|
|
|
)
|
|
|
|
|
2014-12-09 22:40:44 +01:00
|
|
|
// ErrUnknownRepository is returned if the named repository is not known by
|
|
|
|
// the registry.
|
|
|
|
type ErrUnknownRepository struct {
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err ErrUnknownRepository) Error() string {
|
|
|
|
return fmt.Sprintf("unknown respository name=%s", err.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ErrUnknownManifest is returned if the manifest is not known by the
|
|
|
|
// registry.
|
|
|
|
type ErrUnknownManifest struct {
|
|
|
|
Name string
|
|
|
|
Tag string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err ErrUnknownManifest) Error() string {
|
|
|
|
return fmt.Sprintf("unknown manifest name=%s tag=%s", err.Name, err.Tag)
|
|
|
|
}
|
|
|
|
|
2015-01-14 21:02:43 +01:00
|
|
|
// ErrUnknownManifestRevision is returned when a manifest cannot be found by
|
|
|
|
// revision within a repository.
|
|
|
|
type ErrUnknownManifestRevision struct {
|
|
|
|
Name string
|
|
|
|
Revision digest.Digest
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err ErrUnknownManifestRevision) Error() string {
|
|
|
|
return fmt.Sprintf("unknown manifest name=%s revision=%s", err.Name, err.Revision)
|
|
|
|
}
|
|
|
|
|
2014-12-09 22:40:44 +01:00
|
|
|
// ErrManifestUnverified is returned when the registry is unable to verify
|
|
|
|
// the manifest.
|
|
|
|
type ErrManifestUnverified struct{}
|
|
|
|
|
|
|
|
func (ErrManifestUnverified) Error() string {
|
|
|
|
return fmt.Sprintf("unverified manifest")
|
|
|
|
}
|
|
|
|
|
|
|
|
// ErrManifestVerification provides a type to collect errors encountered
|
|
|
|
// during manifest verification. Currently, it accepts errors of all types,
|
|
|
|
// but it may be narrowed to those involving manifest verification.
|
|
|
|
type ErrManifestVerification []error
|
|
|
|
|
|
|
|
func (errs ErrManifestVerification) Error() string {
|
|
|
|
var parts []string
|
|
|
|
for _, err := range errs {
|
|
|
|
parts = append(parts, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("errors verifying manifest: %v", strings.Join(parts, ","))
|
|
|
|
}
|
|
|
|
|
2014-11-22 04:39:52 +01:00
|
|
|
type manifestStore struct {
|
2015-01-14 21:02:43 +01:00
|
|
|
driver storagedriver.StorageDriver
|
|
|
|
pathMapper *pathMapper
|
|
|
|
revisionStore *revisionStore
|
|
|
|
tagStore *tagStore
|
|
|
|
blobStore *blobStore
|
|
|
|
layerService LayerService
|
2014-11-22 04:39:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ ManifestService = &manifestStore{}
|
|
|
|
|
2014-12-09 20:06:51 +01:00
|
|
|
func (ms *manifestStore) Tags(name string) ([]string, error) {
|
2015-01-14 21:02:43 +01:00
|
|
|
return ms.tagStore.tags(name)
|
2014-12-09 20:06:51 +01:00
|
|
|
}
|
|
|
|
|
2014-11-22 04:39:52 +01:00
|
|
|
func (ms *manifestStore) Exists(name, tag string) (bool, error) {
|
2015-01-14 21:02:43 +01:00
|
|
|
return ms.tagStore.exists(name, tag)
|
2014-11-22 04:39:52 +01:00
|
|
|
}
|
|
|
|
|
2015-01-02 22:21:29 +01:00
|
|
|
func (ms *manifestStore) Get(name, tag string) (*manifest.SignedManifest, error) {
|
2015-01-14 21:02:43 +01:00
|
|
|
dgst, err := ms.tagStore.resolve(name, tag)
|
2014-11-22 04:39:52 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-01-14 21:02:43 +01:00
|
|
|
return ms.revisionStore.get(name, dgst)
|
2014-11-22 04:39:52 +01:00
|
|
|
}
|
|
|
|
|
2015-01-02 22:21:29 +01:00
|
|
|
func (ms *manifestStore) Put(name, tag string, manifest *manifest.SignedManifest) error {
|
2015-01-14 21:02:43 +01:00
|
|
|
// Verify the manifest.
|
|
|
|
if err := ms.verifyManifest(name, tag, manifest); err != nil {
|
2014-11-22 04:39:52 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-01-14 21:02:43 +01:00
|
|
|
// Store the revision of the manifest
|
|
|
|
revision, err := ms.revisionStore.put(name, manifest)
|
|
|
|
if err != nil {
|
2014-11-22 04:39:52 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-01-14 21:02:43 +01:00
|
|
|
// Now, tag the manifest
|
|
|
|
return ms.tagStore.tag(name, tag, revision)
|
2014-11-22 04:39:52 +01:00
|
|
|
}
|
|
|
|
|
2015-01-14 21:02:43 +01: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.
|
2014-11-22 04:39:52 +01:00
|
|
|
func (ms *manifestStore) Delete(name, tag string) error {
|
2015-01-14 21:02:43 +01:00
|
|
|
revisions, err := ms.tagStore.revisions(name, tag)
|
2014-11-26 21:52:52 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-01-14 21:02:43 +01:00
|
|
|
for _, revision := range revisions {
|
|
|
|
if err := ms.revisionStore.delete(name, revision); err != nil {
|
2014-11-26 21:52:52 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-14 21:02:43 +01:00
|
|
|
return ms.tagStore.delete(name, tag)
|
2014-11-22 04:39:52 +01:00
|
|
|
}
|
|
|
|
|
2015-01-14 21:02:43 +01: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-03 00:46:47 +01:00
|
|
|
func (ms *manifestStore) verifyManifest(name, tag string, mnfst *manifest.SignedManifest) error {
|
2014-11-26 21:52:52 +01:00
|
|
|
var errs ErrManifestVerification
|
2015-01-03 00:46:47 +01:00
|
|
|
if mnfst.Name != name {
|
2014-12-02 01:13:01 +01:00
|
|
|
// TODO(stevvooe): This needs to be an exported error
|
|
|
|
errs = append(errs, fmt.Errorf("name does not match manifest name"))
|
2014-11-22 04:39:52 +01:00
|
|
|
}
|
|
|
|
|
2015-01-03 00:46:47 +01:00
|
|
|
if mnfst.Tag != tag {
|
2014-12-02 01:13:01 +01:00
|
|
|
// TODO(stevvooe): This needs to be an exported error.
|
|
|
|
errs = append(errs, fmt.Errorf("tag does not match manifest tag"))
|
2014-11-22 04:39:52 +01:00
|
|
|
}
|
|
|
|
|
2015-01-03 00:46:47 +01:00
|
|
|
if _, err := manifest.Verify(mnfst); err != nil {
|
2014-11-26 21:52:52 +01:00
|
|
|
switch err {
|
|
|
|
case libtrust.ErrMissingSignatureKey, libtrust.ErrInvalidJSONContent, libtrust.ErrMissingSignatureKey:
|
|
|
|
errs = append(errs, ErrManifestUnverified{})
|
|
|
|
default:
|
2014-12-02 01:13:01 +01:00
|
|
|
if err.Error() == "invalid signature" { // TODO(stevvooe): This should be exported by libtrust
|
|
|
|
errs = append(errs, ErrManifestUnverified{})
|
|
|
|
} else {
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
2014-11-26 21:52:52 +01:00
|
|
|
}
|
|
|
|
}
|
2014-11-22 04:39:52 +01:00
|
|
|
|
2015-01-03 00:46:47 +01:00
|
|
|
for _, fsLayer := range mnfst.FSLayers {
|
2014-11-22 04:39:52 +01:00
|
|
|
exists, err := ms.layerService.Exists(name, fsLayer.BlobSum)
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !exists {
|
2014-11-26 21:52:52 +01:00
|
|
|
errs = append(errs, ErrUnknownLayer{FSLayer: fsLayer})
|
2014-11-22 04:39:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(errs) != 0 {
|
|
|
|
// TODO(stevvooe): These need to be recoverable by a caller.
|
2014-11-26 21:52:52 +01:00
|
|
|
return errs
|
2014-11-22 04:39:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|