2015-01-14 20:02:43 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
2015-02-13 21:59:50 +00:00
|
|
|
"github.com/docker/distribution"
|
2015-01-14 20:02:43 +00:00
|
|
|
"github.com/docker/distribution/digest"
|
|
|
|
"github.com/docker/distribution/manifest"
|
|
|
|
"github.com/docker/libtrust"
|
|
|
|
)
|
|
|
|
|
|
|
|
// revisionStore supports storing and managing manifest revisions.
|
|
|
|
type revisionStore struct {
|
2015-01-17 02:24:07 +00:00
|
|
|
*repository
|
2015-01-14 20:02:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// exists returns true if the revision is available in the named repository.
|
2015-01-17 02:24:07 +00:00
|
|
|
func (rs *revisionStore) exists(revision digest.Digest) (bool, error) {
|
|
|
|
revpath, err := rs.pm.path(manifestRevisionPathSpec{
|
|
|
|
name: rs.Name(),
|
2015-01-14 20:02:43 +00:00
|
|
|
revision: revision,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2015-04-27 22:58:58 +00:00
|
|
|
exists, err := exists(rs.repository.ctx, rs.driver, revpath)
|
2015-01-14 20:02:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return exists, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// get retrieves the manifest, keyed by revision digest.
|
2015-01-17 02:24:07 +00:00
|
|
|
func (rs *revisionStore) get(revision digest.Digest) (*manifest.SignedManifest, error) {
|
2015-01-14 20:02:43 +00:00
|
|
|
// Ensure that this revision is available in this repository.
|
2015-01-17 02:24:07 +00:00
|
|
|
if exists, err := rs.exists(revision); err != nil {
|
2015-01-14 20:02:43 +00:00
|
|
|
return nil, err
|
|
|
|
} else if !exists {
|
2015-02-13 21:59:50 +00:00
|
|
|
return nil, distribution.ErrUnknownManifestRevision{
|
2015-01-17 02:24:07 +00:00
|
|
|
Name: rs.Name(),
|
2015-01-14 20:02:43 +00:00
|
|
|
Revision: revision,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
content, err := rs.blobStore.get(revision)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the signatures for the manifest
|
2015-03-04 20:32:22 +00:00
|
|
|
signatures, err := rs.Signatures().Get(revision)
|
2015-01-14 20:02:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
jsig, err := libtrust.NewJSONSignature(content, signatures...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract the pretty JWS
|
|
|
|
raw, err := jsig.PrettySignature("signatures")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var sm manifest.SignedManifest
|
|
|
|
if err := json.Unmarshal(raw, &sm); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &sm, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// put stores the manifest in the repository, if not already present. Any
|
|
|
|
// updated signatures will be stored, as well.
|
2015-01-17 02:24:07 +00:00
|
|
|
func (rs *revisionStore) put(sm *manifest.SignedManifest) (digest.Digest, error) {
|
2015-01-14 20:02:43 +00:00
|
|
|
// Resolve the payload in the manifest.
|
2015-01-28 22:54:09 +00:00
|
|
|
payload, err := sm.Payload()
|
2015-01-14 20:02:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Digest and store the manifest payload in the blob store.
|
|
|
|
revision, err := rs.blobStore.put(payload)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("error putting payload into blobstore: %v", err)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Link the revision into the repository.
|
2015-01-17 02:24:07 +00:00
|
|
|
if err := rs.link(revision); err != nil {
|
2015-01-14 20:02:43 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Grab each json signature and store them.
|
2015-01-28 22:54:09 +00:00
|
|
|
signatures, err := sm.Signatures()
|
2015-01-14 20:02:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2015-03-04 20:32:22 +00:00
|
|
|
if err := rs.Signatures().Put(revision, signatures...); err != nil {
|
|
|
|
return "", err
|
2015-01-14 20:02:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return revision, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// link links the revision into the repository.
|
2015-01-17 02:24:07 +00:00
|
|
|
func (rs *revisionStore) link(revision digest.Digest) error {
|
|
|
|
revisionPath, err := rs.pm.path(manifestRevisionLinkPathSpec{
|
|
|
|
name: rs.Name(),
|
2015-01-14 20:02:43 +00:00
|
|
|
revision: revision,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-04-27 22:58:58 +00:00
|
|
|
if exists, err := exists(rs.repository.ctx, rs.driver, revisionPath); err != nil {
|
2015-01-14 20:02:43 +00:00
|
|
|
return err
|
|
|
|
} else if exists {
|
|
|
|
// Revision has already been linked!
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return rs.blobStore.link(revisionPath, revision)
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete removes the specified manifest revision from storage.
|
2015-01-17 02:24:07 +00:00
|
|
|
func (rs *revisionStore) delete(revision digest.Digest) error {
|
|
|
|
revisionPath, err := rs.pm.path(manifestRevisionPathSpec{
|
|
|
|
name: rs.Name(),
|
2015-01-14 20:02:43 +00:00
|
|
|
revision: revision,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-04-27 22:58:58 +00:00
|
|
|
return rs.driver.Delete(rs.repository.ctx, revisionPath)
|
2015-01-14 20:02:43 +00:00
|
|
|
}
|