2016-07-20 09:46:01 +00:00
|
|
|
|
package docker
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"github.com/containers/image/docker/policyconfiguration"
|
2016-11-22 19:32:10 +00:00
|
|
|
|
"github.com/containers/image/docker/reference"
|
2017-03-13 16:33:17 +00:00
|
|
|
|
"github.com/containers/image/transports"
|
2016-07-20 09:46:01 +00:00
|
|
|
|
"github.com/containers/image/types"
|
2016-10-17 13:53:40 +00:00
|
|
|
|
"github.com/pkg/errors"
|
2016-07-20 09:46:01 +00:00
|
|
|
|
)
|
|
|
|
|
|
2017-03-13 16:33:17 +00:00
|
|
|
|
func init() {
|
|
|
|
|
transports.Register(Transport)
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-17 13:50:35 +00:00
|
|
|
|
// Transport is an ImageTransport for Docker registry-hosted images.
|
2016-07-20 09:46:01 +00:00
|
|
|
|
var Transport = dockerTransport{}
|
|
|
|
|
|
|
|
|
|
type dockerTransport struct{}
|
|
|
|
|
|
|
|
|
|
func (t dockerTransport) Name() string {
|
|
|
|
|
return "docker"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ParseReference converts a string, which should not start with the ImageTransport.Name prefix, into an ImageReference.
|
|
|
|
|
func (t dockerTransport) ParseReference(reference string) (types.ImageReference, error) {
|
|
|
|
|
return ParseReference(reference)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ValidatePolicyConfigurationScope checks that scope is a valid name for a signature.PolicyTransportScopes keys
|
|
|
|
|
// (i.e. a valid PolicyConfigurationIdentity() or PolicyConfigurationNamespaces() return value).
|
|
|
|
|
// It is acceptable to allow an invalid value which will never be matched, it can "only" cause user confusion.
|
|
|
|
|
// scope passed to this function will not be "", that value is always allowed.
|
|
|
|
|
func (t dockerTransport) ValidatePolicyConfigurationScope(scope string) error {
|
|
|
|
|
// FIXME? We could be verifying the various character set and length restrictions
|
|
|
|
|
// from docker/distribution/reference.regexp.go, but other than that there
|
|
|
|
|
// are few semantically invalid strings.
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// dockerReference is an ImageReference for Docker images.
|
|
|
|
|
type dockerReference struct {
|
|
|
|
|
ref reference.Named // By construction we know that !reference.IsNameOnly(ref)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ParseReference converts a string, which should not start with the ImageTransport.Name prefix, into an Docker ImageReference.
|
|
|
|
|
func ParseReference(refString string) (types.ImageReference, error) {
|
|
|
|
|
if !strings.HasPrefix(refString, "//") {
|
2016-10-17 13:53:40 +00:00
|
|
|
|
return nil, errors.Errorf("docker: image reference %s does not start with //", refString)
|
2016-07-20 09:46:01 +00:00
|
|
|
|
}
|
2017-03-13 16:33:17 +00:00
|
|
|
|
ref, err := reference.ParseNormalizedNamed(strings.TrimPrefix(refString, "//"))
|
2016-07-20 09:46:01 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2017-03-13 16:33:17 +00:00
|
|
|
|
ref = reference.TagNameOnly(ref)
|
2016-07-20 09:46:01 +00:00
|
|
|
|
return NewReference(ref)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewReference returns a Docker reference for a named reference. The reference must satisfy !reference.IsNameOnly().
|
|
|
|
|
func NewReference(ref reference.Named) (types.ImageReference, error) {
|
|
|
|
|
if reference.IsNameOnly(ref) {
|
2017-03-13 16:33:17 +00:00
|
|
|
|
return nil, errors.Errorf("Docker reference %s has neither a tag nor a digest", reference.FamiliarString(ref))
|
2016-07-20 09:46:01 +00:00
|
|
|
|
}
|
|
|
|
|
// A github.com/distribution/reference value can have a tag and a digest at the same time!
|
2017-03-13 16:33:17 +00:00
|
|
|
|
// The docker/distribution API does not really support that (we can’t ask for an image with a specific
|
|
|
|
|
// tag and digest), so fail. This MAY be accepted in the future.
|
2016-07-20 09:46:01 +00:00
|
|
|
|
// (Even if it were supported, the semantics of policy namespaces are unclear - should we drop
|
|
|
|
|
// the tag or the digest first?)
|
|
|
|
|
_, isTagged := ref.(reference.NamedTagged)
|
|
|
|
|
_, isDigested := ref.(reference.Canonical)
|
|
|
|
|
if isTagged && isDigested {
|
2016-10-17 13:53:40 +00:00
|
|
|
|
return nil, errors.Errorf("Docker references with both a tag and digest are currently not supported")
|
2016-07-20 09:46:01 +00:00
|
|
|
|
}
|
|
|
|
|
return dockerReference{
|
|
|
|
|
ref: ref,
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ref dockerReference) Transport() types.ImageTransport {
|
|
|
|
|
return Transport
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// StringWithinTransport returns a string representation of the reference, which MUST be such that
|
|
|
|
|
// reference.Transport().ParseReference(reference.StringWithinTransport()) returns an equivalent reference.
|
|
|
|
|
// NOTE: The returned string is not promised to be equal to the original input to ParseReference;
|
|
|
|
|
// e.g. default attribute values omitted by the user may be filled in in the return value, or vice versa.
|
|
|
|
|
// WARNING: Do not use the return value in the UI to describe an image, it does not contain the Transport().Name() prefix.
|
|
|
|
|
func (ref dockerReference) StringWithinTransport() string {
|
2017-03-13 16:33:17 +00:00
|
|
|
|
return "//" + reference.FamiliarString(ref.ref)
|
2016-07-20 09:46:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DockerReference returns a Docker reference associated with this reference
|
|
|
|
|
// (fully explicit, i.e. !reference.IsNameOnly, but reflecting user intent,
|
|
|
|
|
// not e.g. after redirect or alias processing), or nil if unknown/not applicable.
|
|
|
|
|
func (ref dockerReference) DockerReference() reference.Named {
|
|
|
|
|
return ref.ref
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// PolicyConfigurationIdentity returns a string representation of the reference, suitable for policy lookup.
|
|
|
|
|
// This MUST reflect user intent, not e.g. after processing of third-party redirects or aliases;
|
|
|
|
|
// The value SHOULD be fully explicit about its semantics, with no hidden defaults, AND canonical
|
|
|
|
|
// (i.e. various references with exactly the same semantics should return the same configuration identity)
|
|
|
|
|
// It is fine for the return value to be equal to StringWithinTransport(), and it is desirable but
|
|
|
|
|
// not required/guaranteed that it will be a valid input to Transport().ParseReference().
|
|
|
|
|
// Returns "" if configuration identities for these references are not supported.
|
|
|
|
|
func (ref dockerReference) PolicyConfigurationIdentity() string {
|
|
|
|
|
res, err := policyconfiguration.DockerReferenceIdentity(ref.ref)
|
|
|
|
|
if res == "" || err != nil { // Coverage: Should never happen, NewReference above should refuse values which could cause a failure.
|
|
|
|
|
panic(fmt.Sprintf("Internal inconsistency: policyconfiguration.DockerReferenceIdentity returned %#v, %v", res, err))
|
|
|
|
|
}
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// PolicyConfigurationNamespaces returns a list of other policy configuration namespaces to search
|
|
|
|
|
// for if explicit configuration for PolicyConfigurationIdentity() is not set. The list will be processed
|
|
|
|
|
// in order, terminating on first match, and an implicit "" is always checked at the end.
|
|
|
|
|
// It is STRONGLY recommended for the first element, if any, to be a prefix of PolicyConfigurationIdentity(),
|
|
|
|
|
// and each following element to be a prefix of the element preceding it.
|
|
|
|
|
func (ref dockerReference) PolicyConfigurationNamespaces() []string {
|
|
|
|
|
return policyconfiguration.DockerReferenceNamespaces(ref.ref)
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-22 19:32:10 +00:00
|
|
|
|
// NewImage returns a types.Image for this reference, possibly specialized for this ImageTransport.
|
2016-09-17 13:50:35 +00:00
|
|
|
|
// The caller must call .Close() on the returned Image.
|
2016-11-22 19:32:10 +00:00
|
|
|
|
// NOTE: If any kind of signature verification should happen, build an UnparsedImage from the value returned by NewImageSource,
|
|
|
|
|
// verify that UnparsedImage, and convert it into a real Image via image.FromUnparsedImage.
|
2016-09-17 13:50:35 +00:00
|
|
|
|
func (ref dockerReference) NewImage(ctx *types.SystemContext) (types.Image, error) {
|
|
|
|
|
return newImage(ctx, ref)
|
2016-07-20 09:46:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-17 13:50:35 +00:00
|
|
|
|
// NewImageSource returns a types.ImageSource for this reference,
|
|
|
|
|
// asking the backend to use a manifest from requestedManifestMIMETypes if possible.
|
|
|
|
|
// nil requestedManifestMIMETypes means manifest.DefaultRequestedManifestMIMETypes.
|
|
|
|
|
// The caller must call .Close() on the returned ImageSource.
|
|
|
|
|
func (ref dockerReference) NewImageSource(ctx *types.SystemContext, requestedManifestMIMETypes []string) (types.ImageSource, error) {
|
|
|
|
|
return newImageSource(ctx, ref, requestedManifestMIMETypes)
|
2016-07-20 09:46:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewImageDestination returns a types.ImageDestination for this reference.
|
2016-09-17 13:50:35 +00:00
|
|
|
|
// The caller must call .Close() on the returned ImageDestination.
|
|
|
|
|
func (ref dockerReference) NewImageDestination(ctx *types.SystemContext) (types.ImageDestination, error) {
|
|
|
|
|
return newImageDestination(ctx, ref)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DeleteImage deletes the named image from the registry, if supported.
|
|
|
|
|
func (ref dockerReference) DeleteImage(ctx *types.SystemContext) error {
|
|
|
|
|
return deleteImage(ctx, ref)
|
2016-07-20 09:46:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// tagOrDigest returns a tag or digest from the reference.
|
|
|
|
|
func (ref dockerReference) tagOrDigest() (string, error) {
|
|
|
|
|
if ref, ok := ref.ref.(reference.Canonical); ok {
|
|
|
|
|
return ref.Digest().String(), nil
|
|
|
|
|
}
|
|
|
|
|
if ref, ok := ref.ref.(reference.NamedTagged); ok {
|
|
|
|
|
return ref.Tag(), nil
|
|
|
|
|
}
|
|
|
|
|
// This should not happen, NewReference above refuses reference.IsNameOnly values.
|
2017-03-13 16:33:17 +00:00
|
|
|
|
return "", errors.Errorf("Internal inconsistency: Reference %s unexpectedly has neither a digest nor a tag", reference.FamiliarString(ref.ref))
|
2016-07-20 09:46:01 +00:00
|
|
|
|
}
|