| 
									
										
										
										
											2017-02-11 00:48:15 +08:00
										 |  |  | package buildah | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2018-04-12 22:20:36 +08:00
										 |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2019-08-13 12:23:28 +08:00
										 |  |  | 	"encoding/json" | 
					
						
							| 
									
										
										
										
											2017-09-29 23:07:32 +08:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2017-05-09 23:56:44 +08:00
										 |  |  | 	"io" | 
					
						
							| 
									
										
										
										
											2018-04-25 22:00:46 +08:00
										 |  |  | 	"io/ioutil" | 
					
						
							| 
									
										
										
										
											2019-08-13 12:23:28 +08:00
										 |  |  | 	"os" | 
					
						
							| 
									
										
										
										
											2019-03-21 01:06:29 +08:00
										 |  |  | 	"strings" | 
					
						
							| 
									
										
										
										
											2017-06-07 02:11:46 +08:00
										 |  |  | 	"time" | 
					
						
							| 
									
										
										
										
											2017-04-11 02:25:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-18 06:06:16 +08:00
										 |  |  | 	"github.com/containers/buildah/pkg/blobcache" | 
					
						
							| 
									
										
										
										
											2018-09-18 03:20:16 +08:00
										 |  |  | 	"github.com/containers/buildah/util" | 
					
						
							| 
									
										
										
										
											2019-10-26 05:19:30 +08:00
										 |  |  | 	cp "github.com/containers/image/v5/copy" | 
					
						
							|  |  |  | 	"github.com/containers/image/v5/docker" | 
					
						
							|  |  |  | 	"github.com/containers/image/v5/docker/reference" | 
					
						
							|  |  |  | 	"github.com/containers/image/v5/manifest" | 
					
						
							|  |  |  | 	"github.com/containers/image/v5/signature" | 
					
						
							|  |  |  | 	is "github.com/containers/image/v5/storage" | 
					
						
							|  |  |  | 	"github.com/containers/image/v5/transports" | 
					
						
							|  |  |  | 	"github.com/containers/image/v5/types" | 
					
						
							| 
									
										
										
										
											2017-06-02 00:11:14 +08:00
										 |  |  | 	"github.com/containers/storage" | 
					
						
							| 
									
										
										
										
											2017-02-11 00:48:15 +08:00
										 |  |  | 	"github.com/containers/storage/pkg/archive" | 
					
						
							| 
									
										
										
										
											2019-03-21 01:06:29 +08:00
										 |  |  | 	"github.com/containers/storage/pkg/stringid" | 
					
						
							| 
									
										
										
										
											2018-10-12 04:34:13 +08:00
										 |  |  | 	digest "github.com/opencontainers/go-digest" | 
					
						
							| 
									
										
										
										
											2019-08-13 12:23:28 +08:00
										 |  |  | 	configv1 "github.com/openshift/api/config/v1" | 
					
						
							| 
									
										
										
										
											2017-06-02 03:23:02 +08:00
										 |  |  | 	"github.com/pkg/errors" | 
					
						
							| 
									
										
										
										
											2017-10-10 03:05:56 +08:00
										 |  |  | 	"github.com/sirupsen/logrus" | 
					
						
							| 
									
										
										
										
											2017-02-11 00:48:15 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-13 00:50:21 +08:00
										 |  |  | const ( | 
					
						
							|  |  |  | 	// BuilderIdentityAnnotation is the name of the annotation key containing
 | 
					
						
							|  |  |  | 	// the name and version of the producer of the image stored as an
 | 
					
						
							|  |  |  | 	// annotation on commit.
 | 
					
						
							|  |  |  | 	BuilderIdentityAnnotation = "io.buildah.version" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-11 03:45:06 +08:00
										 |  |  | // CommitOptions can be used to alter how an image is committed.
 | 
					
						
							| 
									
										
										
										
											2017-02-11 00:48:15 +08:00
										 |  |  | type CommitOptions struct { | 
					
						
							| 
									
										
										
										
											2017-05-18 05:02:40 +08:00
										 |  |  | 	// PreferredManifestType is the preferred type of image manifest.  The
 | 
					
						
							|  |  |  | 	// image configuration format will be of a compatible type.
 | 
					
						
							|  |  |  | 	PreferredManifestType string | 
					
						
							| 
									
										
										
										
											2017-02-11 03:45:06 +08:00
										 |  |  | 	// Compression specifies the type of compression which is applied to
 | 
					
						
							|  |  |  | 	// layer blobs.  The default is to not use compression, but
 | 
					
						
							|  |  |  | 	// archive.Gzip is recommended.
 | 
					
						
							|  |  |  | 	Compression archive.Compression | 
					
						
							|  |  |  | 	// SignaturePolicyPath specifies an override location for the signature
 | 
					
						
							|  |  |  | 	// policy which should be used for verifying the new image as it is
 | 
					
						
							|  |  |  | 	// being written.  Except in specific circumstances, no value should be
 | 
					
						
							|  |  |  | 	// specified, indicating that the shared, system-wide default policy
 | 
					
						
							|  |  |  | 	// should be used.
 | 
					
						
							| 
									
										
										
										
											2017-02-11 00:48:15 +08:00
										 |  |  | 	SignaturePolicyPath string | 
					
						
							| 
									
										
										
										
											2017-04-11 02:25:07 +08:00
										 |  |  | 	// AdditionalTags is a list of additional names to add to the image, if
 | 
					
						
							|  |  |  | 	// the transport to which we're writing the image gives us a way to add
 | 
					
						
							|  |  |  | 	// them.
 | 
					
						
							|  |  |  | 	AdditionalTags []string | 
					
						
							| 
									
										
										
										
											2017-05-09 23:56:44 +08:00
										 |  |  | 	// ReportWriter is an io.Writer which will be used to log the writing
 | 
					
						
							|  |  |  | 	// of the new image.
 | 
					
						
							|  |  |  | 	ReportWriter io.Writer | 
					
						
							| 
									
										
										
										
											2017-06-07 02:11:46 +08:00
										 |  |  | 	// HistoryTimestamp is the timestamp used when creating new items in the
 | 
					
						
							|  |  |  | 	// image's history.  If unset, the current time will be used.
 | 
					
						
							|  |  |  | 	HistoryTimestamp *time.Time | 
					
						
							| 
									
										
										
										
											2017-08-25 05:44:32 +08:00
										 |  |  | 	// github.com/containers/image/types SystemContext to hold credentials
 | 
					
						
							|  |  |  | 	// and other authentication/authorization information.
 | 
					
						
							|  |  |  | 	SystemContext *types.SystemContext | 
					
						
							| 
									
										
										
										
											2018-04-25 22:00:46 +08:00
										 |  |  | 	// IIDFile tells the builder to write the image ID to the specified file
 | 
					
						
							|  |  |  | 	IIDFile string | 
					
						
							| 
									
										
										
										
											2018-05-22 05:02:50 +08:00
										 |  |  | 	// Squash tells the builder to produce an image with a single layer
 | 
					
						
							|  |  |  | 	// instead of with possibly more than one layer.
 | 
					
						
							|  |  |  | 	Squash bool | 
					
						
							| 
									
										
										
										
											2018-10-18 06:06:16 +08:00
										 |  |  | 	// BlobDirectory is the name of a directory in which we'll look for
 | 
					
						
							|  |  |  | 	// prebuilt copies of layer blobs that we might otherwise need to
 | 
					
						
							|  |  |  | 	// regenerate from on-disk layers.  If blobs are available, the
 | 
					
						
							|  |  |  | 	// manifest of the new image will reference the blobs rather than
 | 
					
						
							|  |  |  | 	// on-disk layers.
 | 
					
						
							|  |  |  | 	BlobDirectory string | 
					
						
							| 
									
										
										
										
											2019-04-09 10:59:52 +08:00
										 |  |  | 	// EmptyLayer tells the builder to omit the diff for the working
 | 
					
						
							|  |  |  | 	// container.
 | 
					
						
							|  |  |  | 	EmptyLayer bool | 
					
						
							| 
									
										
										
										
											2019-01-19 01:39:49 +08:00
										 |  |  | 	// OmitTimestamp forces epoch 0 as created timestamp to allow for
 | 
					
						
							|  |  |  | 	// deterministic, content-addressable builds.
 | 
					
						
							|  |  |  | 	OmitTimestamp bool | 
					
						
							| 
									
										
										
										
											2017-04-11 02:25:07 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-02 00:11:14 +08:00
										 |  |  | // PushOptions can be used to alter how an image is copied somewhere.
 | 
					
						
							|  |  |  | type PushOptions struct { | 
					
						
							|  |  |  | 	// Compression specifies the type of compression which is applied to
 | 
					
						
							|  |  |  | 	// layer blobs.  The default is to not use compression, but
 | 
					
						
							|  |  |  | 	// archive.Gzip is recommended.
 | 
					
						
							|  |  |  | 	Compression archive.Compression | 
					
						
							|  |  |  | 	// SignaturePolicyPath specifies an override location for the signature
 | 
					
						
							|  |  |  | 	// policy which should be used for verifying the new image as it is
 | 
					
						
							|  |  |  | 	// being written.  Except in specific circumstances, no value should be
 | 
					
						
							|  |  |  | 	// specified, indicating that the shared, system-wide default policy
 | 
					
						
							|  |  |  | 	// should be used.
 | 
					
						
							|  |  |  | 	SignaturePolicyPath string | 
					
						
							|  |  |  | 	// ReportWriter is an io.Writer which will be used to log the writing
 | 
					
						
							|  |  |  | 	// of the new image.
 | 
					
						
							|  |  |  | 	ReportWriter io.Writer | 
					
						
							|  |  |  | 	// Store is the local storage store which holds the source image.
 | 
					
						
							|  |  |  | 	Store storage.Store | 
					
						
							| 
									
										
										
										
											2017-08-25 05:44:32 +08:00
										 |  |  | 	// github.com/containers/image/types SystemContext to hold credentials
 | 
					
						
							|  |  |  | 	// and other authentication/authorization information.
 | 
					
						
							|  |  |  | 	SystemContext *types.SystemContext | 
					
						
							| 
									
										
										
										
											2019-09-11 22:19:13 +08:00
										 |  |  | 	// ManifestType is the format to use when saving the image using the 'dir' transport
 | 
					
						
							| 
									
										
										
										
											2017-11-10 01:52:50 +08:00
										 |  |  | 	// possible options are oci, v2s1, and v2s2
 | 
					
						
							|  |  |  | 	ManifestType string | 
					
						
							| 
									
										
										
										
											2018-10-18 06:06:16 +08:00
										 |  |  | 	// BlobDirectory is the name of a directory in which we'll look for
 | 
					
						
							|  |  |  | 	// prebuilt copies of layer blobs that we might otherwise need to
 | 
					
						
							|  |  |  | 	// regenerate from on-disk layers, substituting them in the list of
 | 
					
						
							|  |  |  | 	// blobs to copy whenever possible.
 | 
					
						
							|  |  |  | 	BlobDirectory string | 
					
						
							| 
									
										
										
										
											2019-01-24 02:33:09 +08:00
										 |  |  | 	// Quiet is a boolean value that determines if minimal output to
 | 
					
						
							|  |  |  | 	// the user will be displayed, this is best used for logging.
 | 
					
						
							|  |  |  | 	// The default is false.
 | 
					
						
							|  |  |  | 	Quiet bool | 
					
						
							| 
									
										
										
										
											2017-06-02 00:11:14 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-09 03:53:05 +08:00
										 |  |  | var ( | 
					
						
							| 
									
										
										
										
											2019-08-13 12:23:28 +08:00
										 |  |  | 	// storageAllowedPolicyScopes overrides the policy for local storage
 | 
					
						
							|  |  |  | 	// to ensure that we can read images from it.
 | 
					
						
							|  |  |  | 	storageAllowedPolicyScopes = signature.PolicyTransportScopes{ | 
					
						
							|  |  |  | 		"": []signature.PolicyRequirement{ | 
					
						
							|  |  |  | 			signature.NewPRInsecureAcceptAnything(), | 
					
						
							| 
									
										
										
										
											2019-05-09 03:53:05 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-13 12:23:28 +08:00
										 |  |  | // checkRegistrySourcesAllows checks the $BUILD_REGISTRY_SOURCES environment
 | 
					
						
							|  |  |  | // variable, if it's set.  The contents are expected to be a JSON-encoded
 | 
					
						
							|  |  |  | // github.com/openshift/api/config/v1.Image, set by an OpenShift build
 | 
					
						
							|  |  |  | // controller that arranged for us to be run in a container.
 | 
					
						
							|  |  |  | func checkRegistrySourcesAllows(forWhat string, dest types.ImageReference) error { | 
					
						
							|  |  |  | 	transport := dest.Transport() | 
					
						
							|  |  |  | 	if transport == nil { | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if transport.Name() != docker.Transport.Name() { | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	dref := dest.DockerReference() | 
					
						
							|  |  |  | 	if dref == nil || reference.Domain(dref) == "" { | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if registrySources, ok := os.LookupEnv("BUILD_REGISTRY_SOURCES"); ok && len(registrySources) > 0 { | 
					
						
							|  |  |  | 		var sources configv1.RegistrySources | 
					
						
							|  |  |  | 		if err := json.Unmarshal([]byte(registrySources), &sources); err != nil { | 
					
						
							|  |  |  | 			return errors.Wrapf(err, "error parsing $BUILD_REGISTRY_SOURCES (%q) as JSON", registrySources) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		blocked := false | 
					
						
							|  |  |  | 		if len(sources.BlockedRegistries) > 0 { | 
					
						
							|  |  |  | 			for _, blockedDomain := range sources.BlockedRegistries { | 
					
						
							|  |  |  | 				if blockedDomain == reference.Domain(dref) { | 
					
						
							|  |  |  | 					blocked = true | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if blocked { | 
					
						
							|  |  |  | 			return errors.Errorf("%s registry at %q denied by policy: it is in the blocked registries list", forWhat, reference.Domain(dref)) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		allowed := true | 
					
						
							|  |  |  | 		if len(sources.AllowedRegistries) > 0 { | 
					
						
							|  |  |  | 			allowed = false | 
					
						
							|  |  |  | 			for _, allowedDomain := range sources.AllowedRegistries { | 
					
						
							|  |  |  | 				if allowedDomain == reference.Domain(dref) { | 
					
						
							|  |  |  | 					allowed = true | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if !allowed { | 
					
						
							|  |  |  | 			return errors.Errorf("%s registry at %q denied by policy: not in allowed registries list", forWhat, reference.Domain(dref)) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-11 03:45:06 +08:00
										 |  |  | // Commit writes the contents of the container, along with its updated
 | 
					
						
							| 
									
										
										
										
											2017-04-11 02:25:07 +08:00
										 |  |  | // configuration, to a new image in the specified location, and if we know how,
 | 
					
						
							| 
									
										
										
										
											2018-05-02 01:20:16 +08:00
										 |  |  | // add any additional tags that were specified. Returns the ID of the new image
 | 
					
						
							| 
									
										
										
										
											2019-03-21 01:06:29 +08:00
										 |  |  | // if commit was successful and the image destination was local.
 | 
					
						
							| 
									
										
										
										
											2018-10-12 04:34:13 +08:00
										 |  |  | func (b *Builder) Commit(ctx context.Context, dest types.ImageReference, options CommitOptions) (string, reference.Canonical, digest.Digest, error) { | 
					
						
							| 
									
										
										
										
											2018-05-02 01:20:16 +08:00
										 |  |  | 	var imgID string | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-21 01:06:29 +08:00
										 |  |  | 	// If we weren't given a name, build a destination reference using a
 | 
					
						
							|  |  |  | 	// temporary name that we'll remove later.  The correct thing to do
 | 
					
						
							|  |  |  | 	// would be to read the manifest and configuration blob, and ask the
 | 
					
						
							|  |  |  | 	// manifest for the ID that we'd give the image, but that computation
 | 
					
						
							|  |  |  | 	// requires that we know the digests of the layer blobs, which we don't
 | 
					
						
							|  |  |  | 	// want to compute here because we'll have to do it again when
 | 
					
						
							|  |  |  | 	// cp.Image() instantiates a source image, and we don't want to do the
 | 
					
						
							|  |  |  | 	// work twice.
 | 
					
						
							|  |  |  | 	nameToRemove := "" | 
					
						
							|  |  |  | 	if dest == nil { | 
					
						
							|  |  |  | 		nameToRemove = stringid.GenerateRandomID() + "-tmp" | 
					
						
							|  |  |  | 		dest2, err := is.Transport.ParseStoreReference(b.store, nameToRemove) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return imgID, nil, "", errors.Wrapf(err, "error creating temporary destination reference for image") | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		dest = dest2 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-26 04:28:19 +08:00
										 |  |  | 	systemContext := getSystemContext(b.store, options.SystemContext, options.SignaturePolicyPath) | 
					
						
							| 
									
										
										
										
											2018-10-03 02:48:11 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	blocked, err := isReferenceBlocked(dest, systemContext) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2018-10-12 04:34:13 +08:00
										 |  |  | 		return "", nil, "", errors.Wrapf(err, "error checking if committing to registry for %q is blocked", transports.ImageName(dest)) | 
					
						
							| 
									
										
										
										
											2018-10-03 02:48:11 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if blocked { | 
					
						
							| 
									
										
										
										
											2018-10-12 04:34:13 +08:00
										 |  |  | 		return "", nil, "", errors.Errorf("commit access to registry for %q is blocked by configuration", transports.ImageName(dest)) | 
					
						
							| 
									
										
										
										
											2018-10-03 02:48:11 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-13 12:23:28 +08:00
										 |  |  | 	// Load the system signing policy.
 | 
					
						
							|  |  |  | 	commitPolicy, err := signature.DefaultPolicy(systemContext) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return "", nil, "", errors.Wrapf(err, "error obtaining default signature policy") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	// Override the settings for local storage to make sure that we can always read the source "image".
 | 
					
						
							|  |  |  | 	commitPolicy.Transports[is.Transport.Name()] = storageAllowedPolicyScopes | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-09 03:53:05 +08:00
										 |  |  | 	policyContext, err := signature.NewPolicyContext(commitPolicy) | 
					
						
							| 
									
										
										
										
											2017-02-11 00:48:15 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2018-10-12 04:34:13 +08:00
										 |  |  | 		return imgID, nil, "", errors.Wrapf(err, "error creating new signature policy context") | 
					
						
							| 
									
										
										
										
											2017-02-11 00:48:15 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-07-29 00:58:29 +08:00
										 |  |  | 	defer func() { | 
					
						
							|  |  |  | 		if err2 := policyContext.Destroy(); err2 != nil { | 
					
						
							| 
									
										
										
										
											2017-06-29 05:07:58 +08:00
										 |  |  | 			logrus.Debugf("error destroying signature policy context: %v", err2) | 
					
						
							| 
									
										
										
										
											2017-07-29 00:58:29 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	}() | 
					
						
							| 
									
										
										
										
											2019-08-13 12:23:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Check if the commit is blocked by $BUILDER_REGISTRY_SOURCES.
 | 
					
						
							|  |  |  | 	if err := checkRegistrySourcesAllows("commit to", dest); err != nil { | 
					
						
							|  |  |  | 		return imgID, nil, "", err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if len(options.AdditionalTags) > 0 { | 
					
						
							|  |  |  | 		names, err := util.ExpandNames(options.AdditionalTags, "", systemContext, b.store) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return imgID, nil, "", err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		for _, name := range names { | 
					
						
							|  |  |  | 			additionalDest, err := docker.Transport.ParseReference(name) | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				return imgID, nil, "", errors.Wrapf(err, "error parsing image name %q as an image reference", name) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			if err := checkRegistrySourcesAllows("commit to", additionalDest); err != nil { | 
					
						
							|  |  |  | 				return imgID, nil, "", err | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	logrus.Debugf("committing image with reference %q is allowed by policy", transports.ImageName(dest)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-22 23:20:02 +08:00
										 |  |  | 	// Check if the base image is already in the destination and it's some kind of local
 | 
					
						
							|  |  |  | 	// storage.  If so, we can skip recompressing any layers that come from the base image.
 | 
					
						
							|  |  |  | 	exportBaseLayers := true | 
					
						
							|  |  |  | 	if transport, destIsStorage := dest.Transport().(is.StoreTransport); destIsStorage && b.FromImageID != "" { | 
					
						
							|  |  |  | 		if baseref, err := transport.ParseReference(b.FromImageID); baseref != nil && err == nil { | 
					
						
							|  |  |  | 			if img, err := transport.GetImage(baseref); img != nil && err == nil { | 
					
						
							| 
									
										
										
										
											2018-10-03 22:05:46 +08:00
										 |  |  | 				logrus.Debugf("base image %q is already present in local storage, no need to copy its layers", b.FromImageID) | 
					
						
							| 
									
										
										
										
											2018-06-22 23:20:02 +08:00
										 |  |  | 				exportBaseLayers = false | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-03-21 01:06:29 +08:00
										 |  |  | 	// Build an image reference from which we can copy the finished image.
 | 
					
						
							| 
									
										
										
										
											2019-04-24 21:12:01 +08:00
										 |  |  | 	src, err := b.makeImageRef(options, exportBaseLayers) | 
					
						
							| 
									
										
										
										
											2017-02-11 00:48:15 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2018-10-12 04:34:13 +08:00
										 |  |  | 		return imgID, nil, "", errors.Wrapf(err, "error computing layer digests and building metadata for container %q", b.ContainerID) | 
					
						
							| 
									
										
										
										
											2017-02-11 00:48:15 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-03-21 01:06:29 +08:00
										 |  |  | 	// In case we're using caching, decide how to handle compression for a cache.
 | 
					
						
							|  |  |  | 	// If we're using blob caching, set it up for the source.
 | 
					
						
							| 
									
										
										
										
											2019-07-18 16:37:38 +08:00
										 |  |  | 	maybeCachedSrc := src | 
					
						
							|  |  |  | 	maybeCachedDest := dest | 
					
						
							| 
									
										
										
										
											2018-10-18 06:06:16 +08:00
										 |  |  | 	if options.BlobDirectory != "" { | 
					
						
							|  |  |  | 		compress := types.PreserveOriginal | 
					
						
							|  |  |  | 		if options.Compression != archive.Uncompressed { | 
					
						
							|  |  |  | 			compress = types.Compress | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		cache, err := blobcache.NewBlobCache(src, options.BlobDirectory, compress) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return imgID, nil, "", errors.Wrapf(err, "error wrapping image reference %q in blob cache at %q", transports.ImageName(src), options.BlobDirectory) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		maybeCachedSrc = cache | 
					
						
							|  |  |  | 		cache, err = blobcache.NewBlobCache(dest, options.BlobDirectory, compress) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return imgID, nil, "", errors.Wrapf(err, "error wrapping image reference %q in blob cache at %q", transports.ImageName(dest), options.BlobDirectory) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		maybeCachedDest = cache | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-03-06 03:23:23 +08:00
										 |  |  | 	// "Copy" our image to where it needs to be.
 | 
					
						
							| 
									
										
										
										
											2018-11-17 11:23:01 +08:00
										 |  |  | 	switch options.Compression { | 
					
						
							|  |  |  | 	case archive.Uncompressed: | 
					
						
							|  |  |  | 		systemContext.OCIAcceptUncompressedLayers = true | 
					
						
							|  |  |  | 	case archive.Gzip: | 
					
						
							|  |  |  | 		systemContext.DirForceCompress = true | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-10-12 04:34:13 +08:00
										 |  |  | 	var manifestBytes []byte | 
					
						
							| 
									
										
										
										
											2019-07-16 16:47:48 +08:00
										 |  |  | 	if manifestBytes, err = cp.Image(ctx, policyContext, maybeCachedDest, maybeCachedSrc, getCopyOptions(b.store, options.ReportWriter, nil, systemContext, "")); err != nil { | 
					
						
							| 
									
										
										
										
											2018-10-12 04:34:13 +08:00
										 |  |  | 		return imgID, nil, "", errors.Wrapf(err, "error copying layers and metadata for container %q", b.ContainerID) | 
					
						
							| 
									
										
										
										
											2017-05-20 02:13:15 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-03-21 01:06:29 +08:00
										 |  |  | 	// If we've got more names to attach, and we know how to do that for
 | 
					
						
							|  |  |  | 	// the transport that we're writing the new image to, add them now.
 | 
					
						
							| 
									
										
										
										
											2017-05-20 02:13:15 +08:00
										 |  |  | 	if len(options.AdditionalTags) > 0 { | 
					
						
							|  |  |  | 		switch dest.Transport().Name() { | 
					
						
							| 
									
										
										
										
											2017-06-02 00:11:14 +08:00
										 |  |  | 		case is.Transport.Name(): | 
					
						
							|  |  |  | 			img, err := is.Transport.GetStoreImage(b.store, dest) | 
					
						
							| 
									
										
										
										
											2017-05-20 02:13:15 +08:00
										 |  |  | 			if err != nil { | 
					
						
							| 
									
										
										
										
											2018-10-12 04:34:13 +08:00
										 |  |  | 				return imgID, nil, "", errors.Wrapf(err, "error locating just-written image %q", transports.ImageName(dest)) | 
					
						
							| 
									
										
										
										
											2017-05-20 02:13:15 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-10-03 22:05:46 +08:00
										 |  |  | 			if err = util.AddImageNames(b.store, "", systemContext, img, options.AdditionalTags); err != nil { | 
					
						
							| 
									
										
										
										
											2018-10-12 04:34:13 +08:00
										 |  |  | 				return imgID, nil, "", errors.Wrapf(err, "error setting image names to %v", append(img.Names, options.AdditionalTags...)) | 
					
						
							| 
									
										
										
										
											2017-05-20 02:13:15 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
											
												Take a shortcut when writing to local storage
When writing to local storage, take a couple of shortcuts: instead of
recompressing layers to ensure that the values we store in the image
manifest will be correct for content-addressibility, just pretend that
the layer ID is a blob hash value, and that it's a valid layer diffID.
Local storage doesn't generally care if these values are correct, and we
already have to recompute these values when exporting an image, but this
saves us quite a bit of time.
The image library's Copy() routine actually cares about and
sanity-checks these things, so if we're going to take advantage of the
shortcuts, we need to use its higher-level APIs to write a layer, write
the configuration, and write the manifest, then move those items that it
writes to an image with the right set of layers.
Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
Closes: #141
Approved by: rhatdan
											
										 
											2017-06-01 01:56:25 +08:00
										 |  |  | 			logrus.Debugf("assigned names %v to image %q", img.Names, img.ID) | 
					
						
							| 
									
										
										
										
											2017-05-20 02:13:15 +08:00
										 |  |  | 		default: | 
					
						
							|  |  |  | 			logrus.Warnf("don't know how to add tags to images stored in %q transport", dest.Transport().Name()) | 
					
						
							| 
									
										
										
										
											2017-04-11 02:25:07 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-04-11 01:07:31 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	img, err := is.Transport.GetStoreImage(b.store, dest) | 
					
						
							| 
									
										
										
										
											2019-09-11 22:19:13 +08:00
										 |  |  | 	if err != nil && errors.Cause(err) != storage.ErrImageUnknown { | 
					
						
							| 
									
										
										
										
											2018-10-12 04:34:13 +08:00
										 |  |  | 		return imgID, nil, "", errors.Wrapf(err, "error locating image %q in local storage", transports.ImageName(dest)) | 
					
						
							| 
									
										
										
										
											2018-05-02 01:20:16 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-04-11 01:07:31 +08:00
										 |  |  | 	if err == nil { | 
					
						
							| 
									
										
										
										
											2018-05-02 01:20:16 +08:00
										 |  |  | 		imgID = img.ID | 
					
						
							| 
									
										
										
										
											2019-03-21 01:06:29 +08:00
										 |  |  | 		prunedNames := make([]string, 0, len(img.Names)) | 
					
						
							|  |  |  | 		for _, name := range img.Names { | 
					
						
							|  |  |  | 			if !(nameToRemove != "" && strings.Contains(name, nameToRemove)) { | 
					
						
							|  |  |  | 				prunedNames = append(prunedNames, name) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if len(prunedNames) < len(img.Names) { | 
					
						
							|  |  |  | 			if err = b.store.SetNames(imgID, prunedNames); err != nil { | 
					
						
							|  |  |  | 				return imgID, nil, "", errors.Wrapf(err, "failed to prune temporary name from image %q", imgID) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			logrus.Debugf("reassigned names %v to image %q", prunedNames, img.ID) | 
					
						
							|  |  |  | 			dest2, err := is.Transport.ParseStoreReference(b.store, "@"+imgID) | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				return imgID, nil, "", errors.Wrapf(err, "error creating unnamed destination reference for image") | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			dest = dest2 | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-04-25 22:00:46 +08:00
										 |  |  | 		if options.IIDFile != "" { | 
					
						
							| 
									
										
										
										
											2018-10-03 22:05:46 +08:00
										 |  |  | 			if err = ioutil.WriteFile(options.IIDFile, []byte(img.ID), 0644); err != nil { | 
					
						
							| 
									
										
										
										
											2018-10-12 04:34:13 +08:00
										 |  |  | 				return imgID, nil, "", errors.Wrapf(err, "failed to write image ID to file %q", options.IIDFile) | 
					
						
							| 
									
										
										
										
											2018-04-25 22:00:46 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-04-11 01:07:31 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-05-02 01:20:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-12 04:34:13 +08:00
										 |  |  | 	manifestDigest, err := manifest.Digest(manifestBytes) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return imgID, nil, "", errors.Wrapf(err, "error computing digest of manifest of new image %q", transports.ImageName(dest)) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var ref reference.Canonical | 
					
						
							|  |  |  | 	if name := dest.DockerReference(); name != nil { | 
					
						
							|  |  |  | 		ref, err = reference.WithDigest(name, manifestDigest) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			logrus.Warnf("error generating canonical reference with name %q and digest %s: %v", name, manifestDigest.String(), err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return imgID, ref, manifestDigest, nil | 
					
						
							| 
									
										
										
										
											2017-02-11 00:48:15 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-06-02 00:11:14 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Push copies the contents of the image to a new location.
 | 
					
						
							| 
									
										
										
										
											2018-10-12 04:34:13 +08:00
										 |  |  | func Push(ctx context.Context, image string, dest types.ImageReference, options PushOptions) (reference.Canonical, digest.Digest, error) { | 
					
						
							| 
									
										
										
										
											2019-03-14 04:03:13 +08:00
										 |  |  | 	systemContext := getSystemContext(options.Store, options.SystemContext, options.SignaturePolicyPath) | 
					
						
							| 
									
										
										
										
											2018-10-03 02:48:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-24 02:33:09 +08:00
										 |  |  | 	if options.Quiet { | 
					
						
							|  |  |  | 		options.ReportWriter = nil // Turns off logging output
 | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-10-03 02:48:11 +08:00
										 |  |  | 	blocked, err := isReferenceBlocked(dest, systemContext) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2018-10-12 04:34:13 +08:00
										 |  |  | 		return nil, "", errors.Wrapf(err, "error checking if pushing to registry for %q is blocked", transports.ImageName(dest)) | 
					
						
							| 
									
										
										
										
											2018-10-03 02:48:11 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if blocked { | 
					
						
							| 
									
										
										
										
											2018-10-12 04:34:13 +08:00
										 |  |  | 		return nil, "", errors.Errorf("push access to registry for %q is blocked by configuration", transports.ImageName(dest)) | 
					
						
							| 
									
										
										
										
											2018-10-03 02:48:11 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-13 12:23:28 +08:00
										 |  |  | 	// Load the system signing policy.
 | 
					
						
							|  |  |  | 	pushPolicy, err := signature.DefaultPolicy(systemContext) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, "", errors.Wrapf(err, "error obtaining default signature policy") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	// Override the settings for local storage to make sure that we can always read the source "image".
 | 
					
						
							|  |  |  | 	pushPolicy.Transports[is.Transport.Name()] = storageAllowedPolicyScopes | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-09 03:53:05 +08:00
										 |  |  | 	policyContext, err := signature.NewPolicyContext(pushPolicy) | 
					
						
							| 
									
										
										
										
											2017-06-02 00:11:14 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2018-10-12 04:34:13 +08:00
										 |  |  | 		return nil, "", errors.Wrapf(err, "error creating new signature policy context") | 
					
						
							| 
									
										
										
										
											2017-06-02 00:11:14 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-08-13 12:23:28 +08:00
										 |  |  | 	defer func() { | 
					
						
							|  |  |  | 		if err2 := policyContext.Destroy(); err2 != nil { | 
					
						
							|  |  |  | 			logrus.Debugf("error destroying signature policy context: %v", err2) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-29 05:07:58 +08:00
										 |  |  | 	// Look up the image.
 | 
					
						
							| 
									
										
										
										
											2018-10-12 04:34:13 +08:00
										 |  |  | 	src, _, err := util.FindImage(options.Store, "", systemContext, image) | 
					
						
							| 
									
										
										
										
											2017-06-02 00:11:14 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2018-10-12 04:34:13 +08:00
										 |  |  | 		return nil, "", err | 
					
						
							| 
									
										
										
										
											2017-06-02 00:11:14 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-07-18 16:37:38 +08:00
										 |  |  | 	maybeCachedSrc := src | 
					
						
							| 
									
										
										
										
											2018-10-18 06:06:16 +08:00
										 |  |  | 	if options.BlobDirectory != "" { | 
					
						
							|  |  |  | 		compress := types.PreserveOriginal | 
					
						
							|  |  |  | 		if options.Compression != archive.Uncompressed { | 
					
						
							|  |  |  | 			compress = types.Compress | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		cache, err := blobcache.NewBlobCache(src, options.BlobDirectory, compress) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return nil, "", errors.Wrapf(err, "error wrapping image reference %q in blob cache at %q", transports.ImageName(src), options.BlobDirectory) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		maybeCachedSrc = cache | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-08-13 12:23:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Check if the push is blocked by $BUILDER_REGISTRY_SOURCES.
 | 
					
						
							|  |  |  | 	if err := checkRegistrySourcesAllows("push to", dest); err != nil { | 
					
						
							|  |  |  | 		return nil, "", err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	logrus.Debugf("pushing image to reference %q is allowed by policy", transports.ImageName(dest)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-02 00:11:14 +08:00
										 |  |  | 	// Copy everything.
 | 
					
						
							| 
									
										
										
										
											2018-11-17 11:23:01 +08:00
										 |  |  | 	switch options.Compression { | 
					
						
							|  |  |  | 	case archive.Uncompressed: | 
					
						
							|  |  |  | 		systemContext.OCIAcceptUncompressedLayers = true | 
					
						
							|  |  |  | 	case archive.Gzip: | 
					
						
							|  |  |  | 		systemContext.DirForceCompress = true | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-10-12 04:34:13 +08:00
										 |  |  | 	var manifestBytes []byte | 
					
						
							| 
									
										
										
										
											2019-07-16 16:47:48 +08:00
										 |  |  | 	if manifestBytes, err = cp.Image(ctx, policyContext, dest, maybeCachedSrc, getCopyOptions(options.Store, options.ReportWriter, nil, systemContext, options.ManifestType)); err != nil { | 
					
						
							| 
									
										
										
										
											2018-10-18 06:06:16 +08:00
										 |  |  | 		return nil, "", errors.Wrapf(err, "error copying layers and metadata from %q to %q", transports.ImageName(maybeCachedSrc), transports.ImageName(dest)) | 
					
						
							| 
									
										
										
										
											2017-06-02 00:11:14 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-09-29 23:07:32 +08:00
										 |  |  | 	if options.ReportWriter != nil { | 
					
						
							| 
									
										
										
										
											2018-09-07 16:56:24 +08:00
										 |  |  | 		fmt.Fprintf(options.ReportWriter, "") | 
					
						
							| 
									
										
										
										
											2017-09-29 23:07:32 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-10-12 04:34:13 +08:00
										 |  |  | 	manifestDigest, err := manifest.Digest(manifestBytes) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, "", errors.Wrapf(err, "error computing digest of manifest of new image %q", transports.ImageName(dest)) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	var ref reference.Canonical | 
					
						
							|  |  |  | 	if name := dest.DockerReference(); name != nil { | 
					
						
							|  |  |  | 		ref, err = reference.WithDigest(name, manifestDigest) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			logrus.Warnf("error generating canonical reference with name %q and digest %s: %v", name, manifestDigest.String(), err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return ref, manifestDigest, nil | 
					
						
							| 
									
										
										
										
											2017-06-02 00:11:14 +08:00
										 |  |  | } |