2018-02-05 16:05:59 -05:00
|
|
|
package distribution // import "github.com/docker/docker/distribution"
|
2016-12-16 14:19:05 -05:00
|
|
|
|
|
|
|
import (
|
2018-04-19 18:30:59 -04:00
|
|
|
"context"
|
2016-12-16 14:19:05 -05:00
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"runtime"
|
|
|
|
|
|
|
|
"github.com/docker/distribution"
|
|
|
|
"github.com/docker/distribution/manifest/schema2"
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/distribution/metadata"
|
|
|
|
"github.com/docker/docker/distribution/xfer"
|
|
|
|
"github.com/docker/docker/image"
|
|
|
|
"github.com/docker/docker/layer"
|
|
|
|
"github.com/docker/docker/pkg/progress"
|
2017-04-24 13:28:21 -04:00
|
|
|
"github.com/docker/docker/pkg/system"
|
2017-01-25 19:54:18 -05:00
|
|
|
refstore "github.com/docker/docker/reference"
|
2016-12-16 14:19:05 -05:00
|
|
|
"github.com/docker/docker/registry"
|
2019-06-17 20:23:44 -04:00
|
|
|
"github.com/docker/libtrust"
|
2022-03-04 08:49:42 -05:00
|
|
|
"github.com/opencontainers/go-digest"
|
2018-02-15 16:17:27 -05:00
|
|
|
specs "github.com/opencontainers/image-spec/specs-go/v1"
|
2021-07-27 06:46:41 -04:00
|
|
|
"github.com/pkg/errors"
|
2016-12-16 14:19:05 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// Config stores configuration for communicating
|
|
|
|
// with a registry.
|
|
|
|
type Config struct {
|
|
|
|
// MetaHeaders stores HTTP headers with metadata about the image
|
|
|
|
MetaHeaders map[string][]string
|
|
|
|
// AuthConfig holds authentication credentials for authenticating with
|
|
|
|
// the registry.
|
|
|
|
AuthConfig *types.AuthConfig
|
|
|
|
// ProgressOutput is the interface for showing the status of the pull
|
|
|
|
// operation.
|
|
|
|
ProgressOutput progress.Output
|
|
|
|
// RegistryService is the registry service to use for TLS configuration
|
|
|
|
// and endpoint lookup.
|
|
|
|
RegistryService registry.Service
|
|
|
|
// ImageEventLogger notifies events for a given image
|
|
|
|
ImageEventLogger func(id, name, action string)
|
|
|
|
// MetadataStore is the storage backend for distribution-specific
|
|
|
|
// metadata.
|
|
|
|
MetadataStore metadata.Store
|
|
|
|
// ImageStore manages images.
|
|
|
|
ImageStore ImageConfigStore
|
|
|
|
// ReferenceStore manages tags. This value is optional, when excluded
|
|
|
|
// content will not be tagged.
|
2017-01-25 19:54:18 -05:00
|
|
|
ReferenceStore refstore.Store
|
2016-12-16 14:19:05 -05:00
|
|
|
// RequireSchema2 ensures that only schema2 manifests are used.
|
|
|
|
RequireSchema2 bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// ImagePullConfig stores pull configuration.
|
|
|
|
type ImagePullConfig struct {
|
|
|
|
Config
|
|
|
|
|
|
|
|
// DownloadManager manages concurrent pulls.
|
2022-01-21 07:48:44 -05:00
|
|
|
DownloadManager *xfer.LayerDownloadManager
|
2016-12-16 14:19:05 -05:00
|
|
|
// Schema2Types is the valid schema2 configuration types allowed
|
|
|
|
// by the pull operation.
|
|
|
|
Schema2Types []string
|
2018-06-26 17:49:33 -04:00
|
|
|
// Platform is the requested platform of the image being pulled
|
|
|
|
Platform *specs.Platform
|
2016-12-16 14:19:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ImagePushConfig stores push configuration.
|
|
|
|
type ImagePushConfig struct {
|
|
|
|
Config
|
|
|
|
|
|
|
|
// ConfigMediaType is the configuration media type for
|
|
|
|
// schema2 manifests.
|
|
|
|
ConfigMediaType string
|
2021-03-19 10:34:08 -04:00
|
|
|
// LayerStores manages layers.
|
|
|
|
LayerStores PushLayerProvider
|
2019-06-17 20:23:44 -04:00
|
|
|
// TrustKey is the private key for legacy signatures. This is typically
|
|
|
|
// an ephemeral key, since these signatures are no longer verified.
|
|
|
|
TrustKey libtrust.PrivateKey
|
2016-12-16 14:19:05 -05:00
|
|
|
// UploadManager dispatches uploads.
|
|
|
|
UploadManager *xfer.LayerUploadManager
|
|
|
|
}
|
|
|
|
|
|
|
|
// ImageConfigStore handles storing and getting image configurations
|
|
|
|
// by digest. Allows getting an image configurations rootfs from the
|
|
|
|
// configuration.
|
|
|
|
type ImageConfigStore interface {
|
2020-10-30 15:47:06 -04:00
|
|
|
Put(context.Context, []byte) (digest.Digest, error)
|
|
|
|
Get(context.Context, digest.Digest) ([]byte, error)
|
2018-02-15 16:17:27 -05:00
|
|
|
RootFSFromConfig([]byte) (*image.RootFS, error)
|
|
|
|
PlatformFromConfig([]byte) (*specs.Platform, error)
|
2016-12-16 14:19:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// PushLayerProvider provides layers to be pushed by ChainID.
|
|
|
|
type PushLayerProvider interface {
|
|
|
|
Get(layer.ChainID) (PushLayer, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PushLayer is a pushable layer with metadata about the layer
|
|
|
|
// and access to the content of the layer.
|
|
|
|
type PushLayer interface {
|
|
|
|
ChainID() layer.ChainID
|
|
|
|
DiffID() layer.DiffID
|
|
|
|
Parent() PushLayer
|
|
|
|
Open() (io.ReadCloser, error)
|
2022-01-24 10:34:56 -05:00
|
|
|
Size() int64
|
2016-12-16 14:19:05 -05:00
|
|
|
MediaType() string
|
|
|
|
Release()
|
|
|
|
}
|
|
|
|
|
|
|
|
type imageConfigStore struct {
|
|
|
|
image.Store
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewImageConfigStoreFromStore returns an ImageConfigStore backed
|
|
|
|
// by an image.Store for container images.
|
|
|
|
func NewImageConfigStoreFromStore(is image.Store) ImageConfigStore {
|
|
|
|
return &imageConfigStore{
|
|
|
|
Store: is,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-30 15:47:06 -04:00
|
|
|
func (s *imageConfigStore) Put(_ context.Context, c []byte) (digest.Digest, error) {
|
2016-12-16 14:19:05 -05:00
|
|
|
id, err := s.Store.Create(c)
|
|
|
|
return digest.Digest(id), err
|
|
|
|
}
|
|
|
|
|
2020-10-30 15:47:06 -04:00
|
|
|
func (s *imageConfigStore) Get(_ context.Context, d digest.Digest) ([]byte, error) {
|
2016-12-16 14:19:05 -05:00
|
|
|
img, err := s.Store.Get(image.IDFromDigest(d))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return img.RawJSON(), nil
|
|
|
|
}
|
|
|
|
|
2018-02-15 16:17:27 -05:00
|
|
|
func (s *imageConfigStore) RootFSFromConfig(c []byte) (*image.RootFS, error) {
|
2016-12-16 14:19:05 -05:00
|
|
|
var unmarshalledConfig image.Image
|
|
|
|
if err := json.Unmarshal(c, &unmarshalledConfig); err != nil {
|
2018-02-15 16:17:27 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return unmarshalledConfig.RootFS, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *imageConfigStore) PlatformFromConfig(c []byte) (*specs.Platform, error) {
|
|
|
|
var unmarshalledConfig image.Image
|
|
|
|
if err := json.Unmarshal(c, &unmarshalledConfig); err != nil {
|
|
|
|
return nil, err
|
2016-12-16 14:19:05 -05:00
|
|
|
}
|
|
|
|
|
2017-08-24 14:48:16 -04:00
|
|
|
os := unmarshalledConfig.OS
|
|
|
|
if os == "" {
|
|
|
|
os = runtime.GOOS
|
2017-04-25 19:45:42 -04:00
|
|
|
}
|
2017-11-20 11:33:20 -05:00
|
|
|
if !system.IsOSSupported(os) {
|
2021-07-27 06:46:41 -04:00
|
|
|
return nil, errors.Wrapf(system.ErrNotSupportedOperatingSystem, "image operating system %q cannot be used on this platform", os)
|
2017-11-20 11:33:20 -05:00
|
|
|
}
|
2019-04-26 18:12:43 -04:00
|
|
|
return &specs.Platform{OS: os, Architecture: unmarshalledConfig.Architecture, Variant: unmarshalledConfig.Variant, OSVersion: unmarshalledConfig.OSVersion}, nil
|
2016-12-16 14:19:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type storeLayerProvider struct {
|
|
|
|
ls layer.Store
|
|
|
|
}
|
|
|
|
|
2021-03-19 10:34:08 -04:00
|
|
|
// NewLayerProvidersFromStore returns layer providers backed by
|
2016-12-16 14:19:05 -05:00
|
|
|
// an instance of LayerStore. Only getting layers as gzipped
|
|
|
|
// tars is supported.
|
2021-03-19 10:34:08 -04:00
|
|
|
func NewLayerProvidersFromStore(ls layer.Store) PushLayerProvider {
|
|
|
|
return &storeLayerProvider{ls: ls}
|
2016-12-16 14:19:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *storeLayerProvider) Get(lid layer.ChainID) (PushLayer, error) {
|
|
|
|
if lid == "" {
|
|
|
|
return &storeLayer{
|
|
|
|
Layer: layer.EmptyLayer,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
l, err := p.ls.Get(lid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sl := storeLayer{
|
|
|
|
Layer: l,
|
|
|
|
ls: p.ls,
|
|
|
|
}
|
|
|
|
if d, ok := l.(distribution.Describable); ok {
|
|
|
|
return &describableStoreLayer{
|
|
|
|
storeLayer: sl,
|
|
|
|
describable: d,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &sl, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type storeLayer struct {
|
|
|
|
layer.Layer
|
|
|
|
ls layer.Store
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *storeLayer) Parent() PushLayer {
|
|
|
|
p := l.Layer.Parent()
|
|
|
|
if p == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2017-01-11 22:01:13 -05:00
|
|
|
sl := storeLayer{
|
2016-12-16 14:19:05 -05:00
|
|
|
Layer: p,
|
|
|
|
ls: l.ls,
|
|
|
|
}
|
2017-01-11 22:01:13 -05:00
|
|
|
if d, ok := p.(distribution.Describable); ok {
|
|
|
|
return &describableStoreLayer{
|
|
|
|
storeLayer: sl,
|
|
|
|
describable: d,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &sl
|
2016-12-16 14:19:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *storeLayer) Open() (io.ReadCloser, error) {
|
|
|
|
return l.Layer.TarStream()
|
|
|
|
}
|
|
|
|
|
2022-01-24 10:34:56 -05:00
|
|
|
func (l *storeLayer) Size() int64 {
|
2016-12-16 14:19:05 -05:00
|
|
|
return l.Layer.DiffSize()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *storeLayer) MediaType() string {
|
|
|
|
// layer store always returns uncompressed tars
|
|
|
|
return schema2.MediaTypeUncompressedLayer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *storeLayer) Release() {
|
|
|
|
if l.ls != nil {
|
|
|
|
layer.ReleaseAndLog(l.ls, l.Layer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type describableStoreLayer struct {
|
|
|
|
storeLayer
|
|
|
|
describable distribution.Describable
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *describableStoreLayer) Descriptor() distribution.Descriptor {
|
|
|
|
return l.describable.Descriptor()
|
|
|
|
}
|