2016-07-20 09:46:01 +00:00
|
|
|
|
package docker
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
2017-08-05 11:40:46 +00:00
|
|
|
|
"context"
|
2017-04-03 07:22:44 +00:00
|
|
|
|
"crypto/rand"
|
|
|
|
|
"encoding/json"
|
2016-07-20 09:46:01 +00:00
|
|
|
|
"fmt"
|
|
|
|
|
"io"
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
"net/http"
|
2016-09-17 13:50:35 +00:00
|
|
|
|
"net/url"
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
2016-07-20 09:46:01 +00:00
|
|
|
|
|
2017-03-13 16:33:17 +00:00
|
|
|
|
"github.com/containers/image/docker/reference"
|
2016-07-20 09:46:01 +00:00
|
|
|
|
"github.com/containers/image/manifest"
|
|
|
|
|
"github.com/containers/image/types"
|
2017-05-17 17:18:35 +00:00
|
|
|
|
"github.com/docker/distribution/registry/api/errcode"
|
|
|
|
|
"github.com/docker/distribution/registry/api/v2"
|
|
|
|
|
"github.com/docker/distribution/registry/client"
|
2016-10-17 13:53:40 +00:00
|
|
|
|
"github.com/opencontainers/go-digest"
|
2017-09-13 17:01:06 +00:00
|
|
|
|
imgspecv1 "github.com/opencontainers/image-spec/specs-go/v1"
|
2016-10-17 13:53:40 +00:00
|
|
|
|
"github.com/pkg/errors"
|
2017-08-05 11:40:46 +00:00
|
|
|
|
"github.com/sirupsen/logrus"
|
2016-07-20 09:46:01 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type dockerImageDestination struct {
|
|
|
|
|
ref dockerReference
|
|
|
|
|
c *dockerClient
|
2016-09-17 13:50:35 +00:00
|
|
|
|
// State
|
2016-11-22 19:32:10 +00:00
|
|
|
|
manifestDigest digest.Digest // or "" if not yet known.
|
2016-07-20 09:46:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-17 13:50:35 +00:00
|
|
|
|
// newImageDestination creates a new ImageDestination for the specified image reference.
|
|
|
|
|
func newImageDestination(ctx *types.SystemContext, ref dockerReference) (types.ImageDestination, error) {
|
2017-08-29 14:54:45 +00:00
|
|
|
|
c, err := newDockerClientFromRef(ctx, ref, true, "pull,push")
|
2016-07-20 09:46:01 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return &dockerImageDestination{
|
|
|
|
|
ref: ref,
|
|
|
|
|
c: c,
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Reference returns the reference used to set up this destination. Note that this should directly correspond to user's intent,
|
|
|
|
|
// e.g. it should use the public hostname instead of the result of resolving CNAMEs or following redirects.
|
|
|
|
|
func (d *dockerImageDestination) Reference() types.ImageReference {
|
|
|
|
|
return d.ref
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-17 13:50:35 +00:00
|
|
|
|
// Close removes resources associated with an initialized ImageDestination, if any.
|
2017-03-13 16:33:17 +00:00
|
|
|
|
func (d *dockerImageDestination) Close() error {
|
|
|
|
|
return nil
|
2016-09-17 13:50:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-20 09:46:01 +00:00
|
|
|
|
func (d *dockerImageDestination) SupportedManifestMIMETypes() []string {
|
2017-09-13 17:01:06 +00:00
|
|
|
|
return []string{
|
|
|
|
|
imgspecv1.MediaTypeImageManifest,
|
|
|
|
|
manifest.DockerV2Schema2MediaType,
|
|
|
|
|
manifest.DockerV2Schema1SignedMediaType,
|
|
|
|
|
manifest.DockerV2Schema1MediaType,
|
|
|
|
|
}
|
2016-09-17 13:50:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SupportsSignatures returns an error (to be displayed to the user) if the destination certainly can't store signatures.
|
|
|
|
|
// Note: It is still possible for PutSignatures to fail if SupportsSignatures returns nil.
|
|
|
|
|
func (d *dockerImageDestination) SupportsSignatures() error {
|
2017-08-05 11:40:46 +00:00
|
|
|
|
if err := d.c.detectProperties(context.TODO()); err != nil {
|
2017-04-03 07:22:44 +00:00
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
switch {
|
|
|
|
|
case d.c.signatureBase != nil:
|
|
|
|
|
return nil
|
|
|
|
|
case d.c.supportsSignatures:
|
|
|
|
|
return nil
|
|
|
|
|
default:
|
|
|
|
|
return errors.Errorf("X-Registry-Supports-Signatures extension not supported, and lookaside is not configured")
|
|
|
|
|
}
|
2016-09-17 13:50:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-22 19:32:10 +00:00
|
|
|
|
// ShouldCompressLayers returns true iff it is desirable to compress layer blobs written to this destination.
|
|
|
|
|
func (d *dockerImageDestination) ShouldCompressLayers() bool {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// AcceptsForeignLayerURLs returns false iff foreign layers in manifest should be actually
|
|
|
|
|
// uploaded to the image destination, true otherwise.
|
|
|
|
|
func (d *dockerImageDestination) AcceptsForeignLayerURLs() bool {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-20 20:31:51 +00:00
|
|
|
|
// MustMatchRuntimeOS returns true iff the destination can store only images targeted for the current runtime OS. False otherwise.
|
|
|
|
|
func (d *dockerImageDestination) MustMatchRuntimeOS() bool {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-22 19:32:10 +00:00
|
|
|
|
// sizeCounter is an io.Writer which only counts the total size of its input.
|
|
|
|
|
type sizeCounter struct{ size int64 }
|
|
|
|
|
|
|
|
|
|
func (c *sizeCounter) Write(p []byte) (n int, err error) {
|
|
|
|
|
c.size += int64(len(p))
|
|
|
|
|
return len(p), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// PutBlob writes contents of stream and returns data representing the result (with all data filled in).
|
|
|
|
|
// inputInfo.Digest can be optionally provided if known; it is not mandatory for the implementation to verify it.
|
|
|
|
|
// inputInfo.Size is the expected length of stream, if known.
|
2016-09-17 13:50:35 +00:00
|
|
|
|
// WARNING: The contents of stream are being verified on the fly. Until stream.Read() returns io.EOF, the contents of the data SHOULD NOT be available
|
|
|
|
|
// to any other readers for download using the supplied digest.
|
|
|
|
|
// If stream.Read() at any time, ESPECIALLY at end of input, returns an error, PutBlob MUST 1) fail, and 2) delete any data stored so far.
|
2016-11-22 19:32:10 +00:00
|
|
|
|
func (d *dockerImageDestination) PutBlob(stream io.Reader, inputInfo types.BlobInfo) (types.BlobInfo, error) {
|
|
|
|
|
if inputInfo.Digest.String() != "" {
|
2017-03-13 16:33:17 +00:00
|
|
|
|
haveBlob, size, err := d.HasBlob(inputInfo)
|
2017-04-03 07:22:44 +00:00
|
|
|
|
if err != nil {
|
2016-11-22 19:32:10 +00:00
|
|
|
|
return types.BlobInfo{}, err
|
2016-09-17 13:50:35 +00:00
|
|
|
|
}
|
2017-04-03 07:22:44 +00:00
|
|
|
|
if haveBlob {
|
2017-03-13 16:33:17 +00:00
|
|
|
|
return types.BlobInfo{Digest: inputInfo.Digest, Size: size}, nil
|
2016-09-17 13:50:35 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FIXME? Chunked upload, progress reporting, etc.
|
2017-04-03 07:22:44 +00:00
|
|
|
|
uploadPath := fmt.Sprintf(blobUploadPath, reference.Path(d.ref.ref))
|
|
|
|
|
logrus.Debugf("Uploading %s", uploadPath)
|
2017-08-05 11:40:46 +00:00
|
|
|
|
res, err := d.c.makeRequest(context.TODO(), "POST", uploadPath, nil, nil)
|
2016-09-17 13:50:35 +00:00
|
|
|
|
if err != nil {
|
2016-11-22 19:32:10 +00:00
|
|
|
|
return types.BlobInfo{}, err
|
2016-09-17 13:50:35 +00:00
|
|
|
|
}
|
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
if res.StatusCode != http.StatusAccepted {
|
|
|
|
|
logrus.Debugf("Error initiating layer upload, response %#v", *res)
|
2018-02-19 17:53:21 +00:00
|
|
|
|
return types.BlobInfo{}, errors.Wrapf(client.HandleErrorResponse(res), "Error initiating layer upload to %s", uploadPath)
|
2016-09-17 13:50:35 +00:00
|
|
|
|
}
|
|
|
|
|
uploadLocation, err := res.Location()
|
|
|
|
|
if err != nil {
|
2016-10-17 13:53:40 +00:00
|
|
|
|
return types.BlobInfo{}, errors.Wrap(err, "Error determining upload URL")
|
2016-09-17 13:50:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-17 13:53:40 +00:00
|
|
|
|
digester := digest.Canonical.Digester()
|
2016-11-22 19:32:10 +00:00
|
|
|
|
sizeCounter := &sizeCounter{}
|
|
|
|
|
tee := io.TeeReader(stream, io.MultiWriter(digester.Hash(), sizeCounter))
|
2017-08-05 11:40:46 +00:00
|
|
|
|
res, err = d.c.makeRequestToResolvedURL(context.TODO(), "PATCH", uploadLocation.String(), map[string][]string{"Content-Type": {"application/octet-stream"}}, tee, inputInfo.Size, true)
|
2016-09-17 13:50:35 +00:00
|
|
|
|
if err != nil {
|
2017-04-03 07:22:44 +00:00
|
|
|
|
logrus.Debugf("Error uploading layer chunked, response %#v", res)
|
2016-11-22 19:32:10 +00:00
|
|
|
|
return types.BlobInfo{}, err
|
2016-09-17 13:50:35 +00:00
|
|
|
|
}
|
|
|
|
|
defer res.Body.Close()
|
2016-11-22 19:32:10 +00:00
|
|
|
|
computedDigest := digester.Digest()
|
2016-09-17 13:50:35 +00:00
|
|
|
|
|
|
|
|
|
uploadLocation, err = res.Location()
|
|
|
|
|
if err != nil {
|
2016-10-17 13:53:40 +00:00
|
|
|
|
return types.BlobInfo{}, errors.Wrap(err, "Error determining upload URL")
|
2016-09-17 13:50:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FIXME: DELETE uploadLocation on failure
|
|
|
|
|
|
|
|
|
|
locationQuery := uploadLocation.Query()
|
2016-11-22 19:32:10 +00:00
|
|
|
|
// TODO: check inputInfo.Digest == computedDigest https://github.com/containers/image/pull/70#discussion_r77646717
|
|
|
|
|
locationQuery.Set("digest", computedDigest.String())
|
2016-09-17 13:50:35 +00:00
|
|
|
|
uploadLocation.RawQuery = locationQuery.Encode()
|
2017-08-05 11:40:46 +00:00
|
|
|
|
res, err = d.c.makeRequestToResolvedURL(context.TODO(), "PUT", uploadLocation.String(), map[string][]string{"Content-Type": {"application/octet-stream"}}, nil, -1, true)
|
2016-09-17 13:50:35 +00:00
|
|
|
|
if err != nil {
|
2016-11-22 19:32:10 +00:00
|
|
|
|
return types.BlobInfo{}, err
|
2016-07-20 09:46:01 +00:00
|
|
|
|
}
|
2016-09-17 13:50:35 +00:00
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
if res.StatusCode != http.StatusCreated {
|
|
|
|
|
logrus.Debugf("Error uploading layer, response %#v", *res)
|
2018-02-19 17:53:21 +00:00
|
|
|
|
return types.BlobInfo{}, errors.Wrapf(client.HandleErrorResponse(res), "Error uploading layer to %s", uploadLocation)
|
2016-11-22 19:32:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logrus.Debugf("Upload of layer %s complete", computedDigest)
|
|
|
|
|
return types.BlobInfo{Digest: computedDigest, Size: sizeCounter.size}, nil
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-03 07:22:44 +00:00
|
|
|
|
// HasBlob returns true iff the image destination already contains a blob with the matching digest which can be reapplied using ReapplyBlob.
|
|
|
|
|
// Unlike PutBlob, the digest can not be empty. If HasBlob returns true, the size of the blob must also be returned.
|
|
|
|
|
// If the destination does not contain the blob, or it is unknown, HasBlob ordinarily returns (false, -1, nil);
|
|
|
|
|
// it returns a non-nil error only on an unexpected failure.
|
2016-11-22 19:32:10 +00:00
|
|
|
|
func (d *dockerImageDestination) HasBlob(info types.BlobInfo) (bool, int64, error) {
|
|
|
|
|
if info.Digest == "" {
|
2016-10-17 13:53:40 +00:00
|
|
|
|
return false, -1, errors.Errorf(`"Can not check for a blob with unknown digest`)
|
2016-09-17 13:50:35 +00:00
|
|
|
|
}
|
2017-04-03 07:22:44 +00:00
|
|
|
|
checkPath := fmt.Sprintf(blobsPath, reference.Path(d.ref.ref), info.Digest.String())
|
2016-09-17 13:50:35 +00:00
|
|
|
|
|
2017-04-03 07:22:44 +00:00
|
|
|
|
logrus.Debugf("Checking %s", checkPath)
|
2017-08-05 11:40:46 +00:00
|
|
|
|
res, err := d.c.makeRequest(context.TODO(), "HEAD", checkPath, nil, nil)
|
2016-11-22 19:32:10 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return false, -1, err
|
|
|
|
|
}
|
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
switch res.StatusCode {
|
|
|
|
|
case http.StatusOK:
|
|
|
|
|
logrus.Debugf("... already exists")
|
|
|
|
|
return true, getBlobSize(res), nil
|
|
|
|
|
case http.StatusUnauthorized:
|
|
|
|
|
logrus.Debugf("... not authorized")
|
2018-02-19 17:53:21 +00:00
|
|
|
|
return false, -1, client.HandleErrorResponse(res)
|
2016-11-22 19:32:10 +00:00
|
|
|
|
case http.StatusNotFound:
|
|
|
|
|
logrus.Debugf("... not present")
|
2017-04-03 07:22:44 +00:00
|
|
|
|
return false, -1, nil
|
2016-11-22 19:32:10 +00:00
|
|
|
|
default:
|
2017-03-13 16:33:17 +00:00
|
|
|
|
return false, -1, errors.Errorf("failed to read from destination repository %s: %v", reference.Path(d.ref.ref), http.StatusText(res.StatusCode))
|
2016-11-22 19:32:10 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *dockerImageDestination) ReapplyBlob(info types.BlobInfo) (types.BlobInfo, error) {
|
|
|
|
|
return info, nil
|
2016-07-20 09:46:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-17 17:18:35 +00:00
|
|
|
|
// PutManifest writes manifest to the destination.
|
|
|
|
|
// FIXME? This should also receive a MIME type if known, to differentiate between schema versions.
|
|
|
|
|
// If the destination is in principle available, refuses this manifest type (e.g. it does not recognize the schema),
|
|
|
|
|
// but may accept a different manifest type, the returned error must be an ManifestTypeRejectedError.
|
2016-07-20 09:46:01 +00:00
|
|
|
|
func (d *dockerImageDestination) PutManifest(m []byte) error {
|
|
|
|
|
digest, err := manifest.Digest(m)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2016-09-17 13:50:35 +00:00
|
|
|
|
d.manifestDigest = digest
|
2016-11-22 19:32:10 +00:00
|
|
|
|
|
2017-03-13 16:33:17 +00:00
|
|
|
|
refTail, err := d.ref.tagOrDigest()
|
2016-11-22 19:32:10 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2017-04-03 07:22:44 +00:00
|
|
|
|
path := fmt.Sprintf(manifestPath, reference.Path(d.ref.ref), refTail)
|
2016-07-20 09:46:01 +00:00
|
|
|
|
|
|
|
|
|
headers := map[string][]string{}
|
|
|
|
|
mimeType := manifest.GuessMIMEType(m)
|
|
|
|
|
if mimeType != "" {
|
|
|
|
|
headers["Content-Type"] = []string{mimeType}
|
|
|
|
|
}
|
2017-08-05 11:40:46 +00:00
|
|
|
|
res, err := d.c.makeRequest(context.TODO(), "PUT", path, headers, bytes.NewReader(m))
|
2016-07-20 09:46:01 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
defer res.Body.Close()
|
2017-11-03 17:36:13 +00:00
|
|
|
|
if !successStatus(res.StatusCode) {
|
2017-05-17 17:18:35 +00:00
|
|
|
|
err = errors.Wrapf(client.HandleErrorResponse(res), "Error uploading manifest to %s", path)
|
|
|
|
|
if isManifestInvalidError(errors.Cause(err)) {
|
|
|
|
|
err = types.ManifestTypeRejectedError{Err: err}
|
2016-07-20 09:46:01 +00:00
|
|
|
|
}
|
2017-05-17 17:18:35 +00:00
|
|
|
|
return err
|
2016-07-20 09:46:01 +00:00
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-03 17:36:13 +00:00
|
|
|
|
// successStatus returns true if the argument is a successful HTTP response
|
|
|
|
|
// code (in the range 200 - 399 inclusive).
|
|
|
|
|
func successStatus(status int) bool {
|
|
|
|
|
return status >= 200 && status <= 399
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-17 17:18:35 +00:00
|
|
|
|
// isManifestInvalidError returns true iff err from client.HandleErrorReponse is a “manifest invalid” error.
|
|
|
|
|
func isManifestInvalidError(err error) bool {
|
|
|
|
|
errors, ok := err.(errcode.Errors)
|
|
|
|
|
if !ok || len(errors) == 0 {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
ec, ok := errors[0].(errcode.ErrorCoder)
|
|
|
|
|
if !ok {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
// ErrorCodeManifestInvalid is returned by OpenShift with acceptschema2=false.
|
|
|
|
|
// ErrorCodeTagInvalid is returned by docker/distribution (at least as of commit ec87e9b6971d831f0eff752ddb54fb64693e51cd)
|
|
|
|
|
// when uploading to a tag (because it can’t find a matching tag inside the manifest)
|
|
|
|
|
return ec.ErrorCode() == v2.ErrorCodeManifestInvalid || ec.ErrorCode() == v2.ErrorCodeTagInvalid
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-17 13:50:35 +00:00
|
|
|
|
func (d *dockerImageDestination) PutSignatures(signatures [][]byte) error {
|
2017-04-03 07:22:44 +00:00
|
|
|
|
// Do not fail if we don’t really need to support signatures.
|
|
|
|
|
if len(signatures) == 0 {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2017-08-05 11:40:46 +00:00
|
|
|
|
if err := d.c.detectProperties(context.TODO()); err != nil {
|
2017-04-03 07:22:44 +00:00
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
switch {
|
|
|
|
|
case d.c.signatureBase != nil:
|
|
|
|
|
return d.putSignaturesToLookaside(signatures)
|
|
|
|
|
case d.c.supportsSignatures:
|
|
|
|
|
return d.putSignaturesToAPIExtension(signatures)
|
|
|
|
|
default:
|
|
|
|
|
return errors.Errorf("X-Registry-Supports-Signatures extension not supported, and lookaside is not configured")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// putSignaturesToLookaside implements PutSignatures() from the lookaside location configured in s.c.signatureBase,
|
|
|
|
|
// which is not nil.
|
|
|
|
|
func (d *dockerImageDestination) putSignaturesToLookaside(signatures [][]byte) error {
|
2016-09-17 13:50:35 +00:00
|
|
|
|
// FIXME? This overwrites files one at a time, definitely not atomic.
|
|
|
|
|
// A failure when updating signatures with a reordered copy could lose some of them.
|
2016-07-20 09:46:01 +00:00
|
|
|
|
|
2016-09-17 13:50:35 +00:00
|
|
|
|
// Skip dealing with the manifest digest if not necessary.
|
|
|
|
|
if len(signatures) == 0 {
|
2016-07-20 09:46:01 +00:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-22 19:32:10 +00:00
|
|
|
|
if d.manifestDigest.String() == "" {
|
|
|
|
|
// This shouldn’t happen, ImageDestination users are required to call PutManifest before PutSignatures
|
2016-10-17 13:53:40 +00:00
|
|
|
|
return errors.Errorf("Unknown manifest digest, can't add signatures")
|
2016-09-17 13:50:35 +00:00
|
|
|
|
}
|
2016-07-20 09:46:01 +00:00
|
|
|
|
|
2017-04-03 07:22:44 +00:00
|
|
|
|
// NOTE: Keep this in sync with docs/signature-protocols.md!
|
2016-09-17 13:50:35 +00:00
|
|
|
|
for i, signature := range signatures {
|
|
|
|
|
url := signatureStorageURL(d.c.signatureBase, d.manifestDigest, i)
|
|
|
|
|
if url == nil {
|
2016-10-17 13:53:40 +00:00
|
|
|
|
return errors.Errorf("Internal error: signatureStorageURL with non-nil base returned nil")
|
2016-09-17 13:50:35 +00:00
|
|
|
|
}
|
|
|
|
|
err := d.putOneSignature(url, signature)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2016-07-20 09:46:01 +00:00
|
|
|
|
}
|
2016-09-17 13:50:35 +00:00
|
|
|
|
// Remove any other signatures, if present.
|
|
|
|
|
// We stop at the first missing signature; if a previous deleting loop aborted
|
|
|
|
|
// prematurely, this may not clean up all of them, but one missing signature
|
|
|
|
|
// is enough for dockerImageSource to stop looking for other signatures, so that
|
|
|
|
|
// is sufficient.
|
|
|
|
|
for i := len(signatures); ; i++ {
|
|
|
|
|
url := signatureStorageURL(d.c.signatureBase, d.manifestDigest, i)
|
|
|
|
|
if url == nil {
|
2016-10-17 13:53:40 +00:00
|
|
|
|
return errors.Errorf("Internal error: signatureStorageURL with non-nil base returned nil")
|
2016-09-17 13:50:35 +00:00
|
|
|
|
}
|
|
|
|
|
missing, err := d.c.deleteOneSignature(url)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if missing {
|
|
|
|
|
break
|
|
|
|
|
}
|
2016-07-20 09:46:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-17 13:50:35 +00:00
|
|
|
|
// putOneSignature stores one signature to url.
|
2017-04-03 07:22:44 +00:00
|
|
|
|
// NOTE: Keep this in sync with docs/signature-protocols.md!
|
2016-09-17 13:50:35 +00:00
|
|
|
|
func (d *dockerImageDestination) putOneSignature(url *url.URL, signature []byte) error {
|
|
|
|
|
switch url.Scheme {
|
|
|
|
|
case "file":
|
|
|
|
|
logrus.Debugf("Writing to %s", url.Path)
|
|
|
|
|
err := os.MkdirAll(filepath.Dir(url.Path), 0755)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
err = ioutil.WriteFile(url.Path, signature, 0644)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
|
2016-11-22 19:32:10 +00:00
|
|
|
|
case "http", "https":
|
2016-10-17 13:53:40 +00:00
|
|
|
|
return errors.Errorf("Writing directly to a %s sigstore %s is not supported. Configure a sigstore-staging: location", url.Scheme, url.String())
|
2016-09-17 13:50:35 +00:00
|
|
|
|
default:
|
2016-10-17 13:53:40 +00:00
|
|
|
|
return errors.Errorf("Unsupported scheme when writing signature to %s", url.String())
|
2016-07-20 09:46:01 +00:00
|
|
|
|
}
|
2016-09-17 13:50:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// deleteOneSignature deletes a signature from url, if it exists.
|
|
|
|
|
// If it successfully determines that the signature does not exist, returns (true, nil)
|
2017-04-03 07:22:44 +00:00
|
|
|
|
// NOTE: Keep this in sync with docs/signature-protocols.md!
|
2016-09-17 13:50:35 +00:00
|
|
|
|
func (c *dockerClient) deleteOneSignature(url *url.URL) (missing bool, err error) {
|
|
|
|
|
switch url.Scheme {
|
|
|
|
|
case "file":
|
|
|
|
|
logrus.Debugf("Deleting %s", url.Path)
|
|
|
|
|
err := os.Remove(url.Path)
|
|
|
|
|
if err != nil && os.IsNotExist(err) {
|
|
|
|
|
return true, nil
|
|
|
|
|
}
|
|
|
|
|
return false, err
|
|
|
|
|
|
2016-11-22 19:32:10 +00:00
|
|
|
|
case "http", "https":
|
2016-10-17 13:53:40 +00:00
|
|
|
|
return false, errors.Errorf("Writing directly to a %s sigstore %s is not supported. Configure a sigstore-staging: location", url.Scheme, url.String())
|
2016-09-17 13:50:35 +00:00
|
|
|
|
default:
|
2016-10-17 13:53:40 +00:00
|
|
|
|
return false, errors.Errorf("Unsupported scheme when deleting signature from %s", url.String())
|
2016-09-17 13:50:35 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-03 07:22:44 +00:00
|
|
|
|
// putSignaturesToAPIExtension implements PutSignatures() using the X-Registry-Supports-Signatures API extension.
|
|
|
|
|
func (d *dockerImageDestination) putSignaturesToAPIExtension(signatures [][]byte) error {
|
|
|
|
|
// Skip dealing with the manifest digest, or reading the old state, if not necessary.
|
|
|
|
|
if len(signatures) == 0 {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if d.manifestDigest.String() == "" {
|
|
|
|
|
// This shouldn’t happen, ImageDestination users are required to call PutManifest before PutSignatures
|
|
|
|
|
return errors.Errorf("Unknown manifest digest, can't add signatures")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Because image signatures are a shared resource in Atomic Registry, the default upload
|
|
|
|
|
// always adds signatures. Eventually we should also allow removing signatures,
|
|
|
|
|
// but the X-Registry-Supports-Signatures API extension does not support that yet.
|
|
|
|
|
|
2017-08-05 11:40:46 +00:00
|
|
|
|
existingSignatures, err := d.c.getExtensionsSignatures(context.TODO(), d.ref, d.manifestDigest)
|
2017-04-03 07:22:44 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
existingSigNames := map[string]struct{}{}
|
|
|
|
|
for _, sig := range existingSignatures.Signatures {
|
|
|
|
|
existingSigNames[sig.Name] = struct{}{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sigExists:
|
|
|
|
|
for _, newSig := range signatures {
|
|
|
|
|
for _, existingSig := range existingSignatures.Signatures {
|
|
|
|
|
if existingSig.Version == extensionSignatureSchemaVersion && existingSig.Type == extensionSignatureTypeAtomic && bytes.Equal(existingSig.Content, newSig) {
|
|
|
|
|
continue sigExists
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The API expect us to invent a new unique name. This is racy, but hopefully good enough.
|
|
|
|
|
var signatureName string
|
|
|
|
|
for {
|
|
|
|
|
randBytes := make([]byte, 16)
|
|
|
|
|
n, err := rand.Read(randBytes)
|
|
|
|
|
if err != nil || n != 16 {
|
|
|
|
|
return errors.Wrapf(err, "Error generating random signature len %d", n)
|
|
|
|
|
}
|
|
|
|
|
signatureName = fmt.Sprintf("%s@%032x", d.manifestDigest.String(), randBytes)
|
|
|
|
|
if _, ok := existingSigNames[signatureName]; !ok {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
sig := extensionSignature{
|
|
|
|
|
Version: extensionSignatureSchemaVersion,
|
|
|
|
|
Name: signatureName,
|
|
|
|
|
Type: extensionSignatureTypeAtomic,
|
|
|
|
|
Content: newSig,
|
|
|
|
|
}
|
|
|
|
|
body, err := json.Marshal(sig)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
path := fmt.Sprintf(extensionsSignaturePath, reference.Path(d.ref.ref), d.manifestDigest.String())
|
2017-08-05 11:40:46 +00:00
|
|
|
|
res, err := d.c.makeRequest(context.TODO(), "PUT", path, nil, bytes.NewReader(body))
|
2017-04-03 07:22:44 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
if res.StatusCode != http.StatusCreated {
|
|
|
|
|
body, err := ioutil.ReadAll(res.Body)
|
|
|
|
|
if err == nil {
|
|
|
|
|
logrus.Debugf("Error body %s", string(body))
|
|
|
|
|
}
|
|
|
|
|
logrus.Debugf("Error uploading signature, status %d, %#v", res.StatusCode, res)
|
2018-02-19 17:53:21 +00:00
|
|
|
|
return errors.Wrapf(client.HandleErrorResponse(res), "Error uploading signature to %s", path)
|
2017-04-03 07:22:44 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-17 13:50:35 +00:00
|
|
|
|
// Commit marks the process of storing the image as successful and asks for the image to be persisted.
|
|
|
|
|
// WARNING: This does not have any transactional semantics:
|
|
|
|
|
// - Uploaded data MAY be visible to others before Commit() is called
|
|
|
|
|
// - Uploaded data MAY be removed or MAY remain around if Close() is called without Commit() (i.e. rollback is allowed but not guaranteed)
|
|
|
|
|
func (d *dockerImageDestination) Commit() error {
|
2016-07-20 09:46:01 +00:00
|
|
|
|
return nil
|
|
|
|
|
}
|