2016-07-20 09:46:01 +00:00
|
|
|
package openshift
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
2016-09-17 13:50:35 +00:00
|
|
|
"strings"
|
2016-07-20 09:46:01 +00:00
|
|
|
|
|
|
|
"github.com/containers/image/docker/policyconfiguration"
|
2016-11-22 19:32:10 +00:00
|
|
|
"github.com/containers/image/docker/reference"
|
|
|
|
genericImage "github.com/containers/image/image"
|
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 OpenShift registry-hosted images.
|
2016-07-20 09:46:01 +00:00
|
|
|
var Transport = openshiftTransport{}
|
|
|
|
|
|
|
|
type openshiftTransport struct{}
|
|
|
|
|
|
|
|
func (t openshiftTransport) Name() string {
|
|
|
|
return "atomic"
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseReference converts a string, which should not start with the ImageTransport.Name prefix, into an ImageReference.
|
|
|
|
func (t openshiftTransport) ParseReference(reference string) (types.ImageReference, error) {
|
|
|
|
return ParseReference(reference)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note that imageNameRegexp is namespace/stream:tag, this
|
|
|
|
// is HOSTNAME/namespace/stream:tag or parent prefixes.
|
|
|
|
// Keep this in sync with imageNameRegexp!
|
|
|
|
var scopeRegexp = regexp.MustCompile("^[^/]*(/[^:/]*(/[^:/]*(:[^:/]*)?)?)?$")
|
|
|
|
|
|
|
|
// 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 openshiftTransport) ValidatePolicyConfigurationScope(scope string) error {
|
|
|
|
if scopeRegexp.FindStringIndex(scope) == nil {
|
2016-10-17 13:53:40 +00:00
|
|
|
return errors.Errorf("Invalid scope name %s", scope)
|
2016-07-20 09:46:01 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// openshiftReference is an ImageReference for OpenShift images.
|
|
|
|
type openshiftReference struct {
|
2016-09-17 13:50:35 +00:00
|
|
|
dockerReference reference.NamedTagged
|
|
|
|
namespace string // Computed from dockerReference in advance.
|
|
|
|
stream string // Computed from dockerReference in advance.
|
2016-07-20 09:46:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ParseReference converts a string, which should not start with the ImageTransport.Name prefix, into an OpenShift ImageReference.
|
2016-09-17 13:50:35 +00:00
|
|
|
func ParseReference(ref string) (types.ImageReference, error) {
|
2017-03-13 16:33:17 +00:00
|
|
|
r, err := reference.ParseNormalizedNamed(ref)
|
2016-07-20 09:46:01 +00:00
|
|
|
if err != nil {
|
2016-10-17 13:53:40 +00:00
|
|
|
return nil, errors.Wrapf(err, "failed to parse image reference %q", ref)
|
2016-07-20 09:46:01 +00:00
|
|
|
}
|
2016-09-17 13:50:35 +00:00
|
|
|
tagged, ok := r.(reference.NamedTagged)
|
|
|
|
if !ok {
|
2017-03-13 16:33:17 +00:00
|
|
|
return nil, errors.Errorf("invalid image reference %s, expected format: 'hostname/namespace/stream:tag'", ref)
|
2016-07-20 09:46:01 +00:00
|
|
|
}
|
2016-09-17 13:50:35 +00:00
|
|
|
return NewReference(tagged)
|
2016-07-20 09:46:01 +00:00
|
|
|
}
|
|
|
|
|
2016-09-17 13:50:35 +00:00
|
|
|
// NewReference returns an OpenShift reference for a reference.NamedTagged
|
|
|
|
func NewReference(dockerRef reference.NamedTagged) (types.ImageReference, error) {
|
2017-03-13 16:33:17 +00:00
|
|
|
r := strings.SplitN(reference.Path(dockerRef), "/", 3)
|
2016-09-17 13:50:35 +00:00
|
|
|
if len(r) != 2 {
|
2017-03-13 16:33:17 +00:00
|
|
|
return nil, errors.Errorf("invalid image reference: %s, expected format: 'hostname/namespace/stream:tag'",
|
|
|
|
reference.FamiliarString(dockerRef))
|
2016-07-20 09:46:01 +00:00
|
|
|
}
|
|
|
|
return openshiftReference{
|
2016-09-17 13:50:35 +00:00
|
|
|
namespace: r[0],
|
|
|
|
stream: r[1],
|
2016-07-20 09:46:01 +00:00
|
|
|
dockerReference: dockerRef,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ref openshiftReference) 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 openshiftReference) StringWithinTransport() string {
|
2017-03-13 16:33:17 +00:00
|
|
|
return reference.FamiliarString(ref.dockerReference)
|
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 openshiftReference) DockerReference() reference.Named {
|
|
|
|
return ref.dockerReference
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 openshiftReference) PolicyConfigurationIdentity() string {
|
|
|
|
res, err := policyconfiguration.DockerReferenceIdentity(ref.dockerReference)
|
|
|
|
if res == "" || err != nil { // Coverage: Should never happen, NewReference constructs a valid tagged reference.
|
|
|
|
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 openshiftReference) PolicyConfigurationNamespaces() []string {
|
|
|
|
return policyconfiguration.DockerReferenceNamespaces(ref.dockerReference)
|
|
|
|
}
|
|
|
|
|
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 openshiftReference) NewImage(ctx *types.SystemContext) (types.Image, error) {
|
2017-09-13 17:01:06 +00:00
|
|
|
src, err := newImageSource(ctx, ref)
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return genericImage.FromSource(src)
|
2016-07-20 09:46:01 +00:00
|
|
|
}
|
|
|
|
|
2017-09-13 17:01:06 +00:00
|
|
|
// NewImageSource returns a types.ImageSource for this reference.
|
2016-09-17 13:50:35 +00:00
|
|
|
// The caller must call .Close() on the returned ImageSource.
|
2017-09-13 17:01:06 +00:00
|
|
|
func (ref openshiftReference) NewImageSource(ctx *types.SystemContext) (types.ImageSource, error) {
|
|
|
|
return newImageSource(ctx, ref)
|
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 openshiftReference) NewImageDestination(ctx *types.SystemContext) (types.ImageDestination, error) {
|
|
|
|
return newImageDestination(ctx, ref)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteImage deletes the named image from the registry, if supported.
|
|
|
|
func (ref openshiftReference) DeleteImage(ctx *types.SystemContext) error {
|
2016-10-17 13:53:40 +00:00
|
|
|
return errors.Errorf("Deleting images not implemented for atomic: images")
|
2016-07-20 09:46:01 +00:00
|
|
|
}
|