2015-03-04 21:32:22 +01:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path"
|
2015-04-07 04:10:50 +02:00
|
|
|
"sync"
|
2015-03-04 21:32:22 +01:00
|
|
|
|
|
|
|
"github.com/docker/distribution"
|
2015-04-07 04:10:50 +02:00
|
|
|
"github.com/docker/distribution/context"
|
2015-03-04 21:32:22 +01:00
|
|
|
"github.com/docker/distribution/digest"
|
|
|
|
)
|
|
|
|
|
|
|
|
type signatureStore struct {
|
|
|
|
*repository
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ distribution.SignatureService = &signatureStore{}
|
|
|
|
|
|
|
|
func (s *signatureStore) Get(dgst digest.Digest) ([][]byte, error) {
|
|
|
|
signaturesPath, err := s.pm.path(manifestSignaturesPathSpec{
|
|
|
|
name: s.Name(),
|
|
|
|
revision: dgst,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Need to append signature digest algorithm to path to get all items.
|
|
|
|
// Perhaps, this should be in the pathMapper but it feels awkward. This
|
|
|
|
// can be eliminated by implementing listAll on drivers.
|
|
|
|
signaturesPath = path.Join(signaturesPath, "sha256")
|
|
|
|
|
|
|
|
signaturePaths, err := s.driver.List(signaturesPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-04-07 04:10:50 +02:00
|
|
|
var wg sync.WaitGroup
|
2015-04-09 23:08:24 +02:00
|
|
|
type result struct {
|
|
|
|
index int
|
|
|
|
signature []byte
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
ch := make(chan result)
|
|
|
|
|
2015-04-07 04:10:50 +02:00
|
|
|
for i, sigPath := range signaturePaths {
|
2015-03-04 21:32:22 +01:00
|
|
|
// Append the link portion
|
|
|
|
sigPath = path.Join(sigPath, "link")
|
|
|
|
|
2015-04-07 04:10:50 +02:00
|
|
|
wg.Add(1)
|
|
|
|
go func(idx int, sigPath string) {
|
|
|
|
defer wg.Done()
|
|
|
|
context.GetLogger(s.ctx).
|
|
|
|
Debugf("fetching signature from %q", sigPath)
|
2015-04-09 23:08:24 +02:00
|
|
|
|
|
|
|
r := result{index: idx}
|
|
|
|
if p, err := s.blobStore.linked(sigPath); err != nil {
|
2015-04-07 04:10:50 +02:00
|
|
|
context.GetLogger(s.ctx).
|
|
|
|
Errorf("error fetching signature from %q: %v", sigPath, err)
|
2015-04-09 23:08:24 +02:00
|
|
|
r.err = err
|
|
|
|
} else {
|
|
|
|
r.signature = p
|
2015-04-07 04:10:50 +02:00
|
|
|
}
|
2015-04-09 23:08:24 +02:00
|
|
|
|
|
|
|
ch <- r
|
2015-04-07 04:10:50 +02:00
|
|
|
}(i, sigPath)
|
|
|
|
}
|
2015-04-09 23:08:24 +02:00
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
wg.Wait()
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// aggregrate the results
|
|
|
|
signatures := make([][]byte, len(signaturePaths))
|
|
|
|
loop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case result := <-ch:
|
|
|
|
signatures[result.index] = result.signature
|
|
|
|
if result.err != nil && err == nil {
|
|
|
|
// only set the first one.
|
|
|
|
err = result.err
|
|
|
|
}
|
|
|
|
case <-done:
|
|
|
|
break loop
|
|
|
|
}
|
2015-03-04 21:32:22 +01:00
|
|
|
}
|
|
|
|
|
2015-04-09 23:08:24 +02:00
|
|
|
return signatures, err
|
2015-03-04 21:32:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *signatureStore) Put(dgst digest.Digest, signatures ...[]byte) error {
|
|
|
|
for _, signature := range signatures {
|
|
|
|
signatureDigest, err := s.blobStore.put(signature)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
signaturePath, err := s.pm.path(manifestSignatureLinkPathSpec{
|
|
|
|
name: s.Name(),
|
|
|
|
revision: dgst,
|
|
|
|
signature: signatureDigest,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.blobStore.link(signaturePath, signatureDigest); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|