| 
									
										
										
										
											2017-02-11 00:48:15 +08:00
										 |  |  | package buildah | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2019-11-28 00:31:02 +08:00
										 |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2017-05-09 23:56:44 +08:00
										 |  |  | 	"io" | 
					
						
							| 
									
										
										
										
											2018-10-23 18:42:35 +08:00
										 |  |  | 	"os" | 
					
						
							|  |  |  | 	"path/filepath" | 
					
						
							| 
									
										
										
										
											2020-08-29 00:58:41 +08:00
										 |  |  | 	"time" | 
					
						
							| 
									
										
										
										
											2018-10-03 01:48:45 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-07 06:49:40 +08:00
										 |  |  | 	"github.com/containers/buildah/define" | 
					
						
							| 
									
										
										
										
											2020-08-14 03:19:38 +08:00
										 |  |  | 	"github.com/containers/common/pkg/retry" | 
					
						
							| 
									
										
										
										
											2019-10-26 05:19:30 +08:00
										 |  |  | 	cp "github.com/containers/image/v5/copy" | 
					
						
							| 
									
										
										
										
											2019-11-28 00:31:02 +08:00
										 |  |  | 	"github.com/containers/image/v5/docker" | 
					
						
							|  |  |  | 	"github.com/containers/image/v5/signature" | 
					
						
							| 
									
										
										
										
											2019-10-26 05:19:30 +08:00
										 |  |  | 	"github.com/containers/image/v5/types" | 
					
						
							| 
									
										
										
										
											2020-04-02 02:15:56 +08:00
										 |  |  | 	encconfig "github.com/containers/ocicrypt/config" | 
					
						
							| 
									
										
										
										
											2019-03-14 04:03:13 +08:00
										 |  |  | 	"github.com/containers/storage" | 
					
						
							| 
									
										
										
										
											2020-03-31 21:56:18 +08:00
										 |  |  | 	"github.com/containers/storage/pkg/unshare" | 
					
						
							| 
									
										
										
										
											2017-02-11 00:48:15 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-24 00:10:17 +08:00
										 |  |  | const ( | 
					
						
							|  |  |  | 	// OCI used to define the "oci" image format
 | 
					
						
							| 
									
										
										
										
											2021-02-07 06:49:40 +08:00
										 |  |  | 	OCI = define.OCI | 
					
						
							| 
									
										
										
										
											2018-08-24 00:10:17 +08:00
										 |  |  | 	// DOCKER used to define the "docker" image format
 | 
					
						
							| 
									
										
										
										
											2021-02-07 06:49:40 +08:00
										 |  |  | 	DOCKER = define.DOCKER | 
					
						
							| 
									
										
										
										
											2018-08-24 00:10:17 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-02 02:15:56 +08:00
										 |  |  | func getCopyOptions(store storage.Store, reportWriter io.Writer, sourceSystemContext *types.SystemContext, destinationSystemContext *types.SystemContext, manifestType string, removeSignatures bool, addSigner string, ociEncryptLayers *[]int, ociEncryptConfig *encconfig.EncryptConfig, ociDecryptConfig *encconfig.DecryptConfig) *cp.Options { | 
					
						
							| 
									
										
										
										
											2019-03-14 04:03:13 +08:00
										 |  |  | 	sourceCtx := getSystemContext(store, nil, "") | 
					
						
							| 
									
										
										
										
											2018-10-03 01:48:45 +08:00
										 |  |  | 	if sourceSystemContext != nil { | 
					
						
							|  |  |  | 		*sourceCtx = *sourceSystemContext | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-14 04:03:13 +08:00
										 |  |  | 	destinationCtx := getSystemContext(store, nil, "") | 
					
						
							| 
									
										
										
										
											2018-10-03 01:48:45 +08:00
										 |  |  | 	if destinationSystemContext != nil { | 
					
						
							|  |  |  | 		*destinationCtx = *destinationSystemContext | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-06-05 21:42:30 +08:00
										 |  |  | 	return &cp.Options{ | 
					
						
							| 
									
										
										
										
											2017-11-10 01:52:50 +08:00
										 |  |  | 		ReportWriter:          reportWriter, | 
					
						
							| 
									
										
										
										
											2018-10-03 01:48:45 +08:00
										 |  |  | 		SourceCtx:             sourceCtx, | 
					
						
							|  |  |  | 		DestinationCtx:        destinationCtx, | 
					
						
							| 
									
										
										
										
											2017-11-10 01:52:50 +08:00
										 |  |  | 		ForceManifestMIMEType: manifestType, | 
					
						
							| 
									
										
										
										
											2020-01-16 01:23:38 +08:00
										 |  |  | 		RemoveSignatures:      removeSignatures, | 
					
						
							|  |  |  | 		SignBy:                addSigner, | 
					
						
							| 
									
										
										
										
											2020-04-02 02:15:56 +08:00
										 |  |  | 		OciEncryptConfig:      ociEncryptConfig, | 
					
						
							|  |  |  | 		OciDecryptConfig:      ociDecryptConfig, | 
					
						
							|  |  |  | 		OciEncryptLayers:      ociEncryptLayers, | 
					
						
							| 
									
										
										
										
											2017-05-09 23:56:44 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-02-11 00:48:15 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-14 04:03:13 +08:00
										 |  |  | func getSystemContext(store storage.Store, defaults *types.SystemContext, signaturePolicyPath string) *types.SystemContext { | 
					
						
							| 
									
										
										
										
											2017-02-11 00:48:15 +08:00
										 |  |  | 	sc := &types.SystemContext{} | 
					
						
							| 
									
										
										
										
											2017-06-29 05:07:58 +08:00
										 |  |  | 	if defaults != nil { | 
					
						
							|  |  |  | 		*sc = *defaults | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-02-11 00:48:15 +08:00
										 |  |  | 	if signaturePolicyPath != "" { | 
					
						
							|  |  |  | 		sc.SignaturePolicyPath = signaturePolicyPath | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-03-14 04:03:13 +08:00
										 |  |  | 	if store != nil { | 
					
						
							| 
									
										
										
										
											2019-03-25 18:23:56 +08:00
										 |  |  | 		if sc.SystemRegistriesConfPath == "" && unshare.IsRootless() { | 
					
						
							| 
									
										
										
										
											2019-03-14 04:03:13 +08:00
										 |  |  | 			userRegistriesFile := filepath.Join(store.GraphRoot(), "registries.conf") | 
					
						
							|  |  |  | 			if _, err := os.Stat(userRegistriesFile); err == nil { | 
					
						
							|  |  |  | 				sc.SystemRegistriesConfPath = userRegistriesFile | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-10-23 18:42:35 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-02-11 00:48:15 +08:00
										 |  |  | 	return sc | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-11-28 00:31:02 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-29 00:58:41 +08:00
										 |  |  | func retryCopyImage(ctx context.Context, policyContext *signature.PolicyContext, dest, src, registry types.ImageReference, copyOptions *cp.Options, maxRetries int, retryDelay time.Duration) ([]byte, error) { | 
					
						
							| 
									
										
										
										
											2020-08-14 03:19:38 +08:00
										 |  |  | 	var ( | 
					
						
							|  |  |  | 		manifestBytes []byte | 
					
						
							|  |  |  | 		err           error | 
					
						
							|  |  |  | 		lastErr       error | 
					
						
							|  |  |  | 	) | 
					
						
							|  |  |  | 	err = retry.RetryIfNecessary(ctx, func() error { | 
					
						
							| 
									
										
										
										
											2019-11-28 00:31:02 +08:00
										 |  |  | 		manifestBytes, err = cp.Image(ctx, policyContext, dest, src, copyOptions) | 
					
						
							| 
									
										
										
										
											2020-08-14 03:19:38 +08:00
										 |  |  | 		if registry != nil && registry.Transport().Name() != docker.Transport.Name() { | 
					
						
							|  |  |  | 			lastErr = err | 
					
						
							|  |  |  | 			return nil | 
					
						
							| 
									
										
										
										
											2019-11-28 00:31:02 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-08-14 03:19:38 +08:00
										 |  |  | 		return err | 
					
						
							| 
									
										
										
										
											2020-08-29 00:58:41 +08:00
										 |  |  | 	}, &retry.RetryOptions{MaxRetry: maxRetries, Delay: retryDelay}) | 
					
						
							| 
									
										
										
										
											2020-08-14 03:19:38 +08:00
										 |  |  | 	if lastErr != nil { | 
					
						
							|  |  |  | 		err = lastErr | 
					
						
							| 
									
										
										
										
											2019-11-28 00:31:02 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return manifestBytes, err | 
					
						
							|  |  |  | } |