|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/google/go-containerregistry/pkg/authn" |
| 9 | + gname "github.com/google/go-containerregistry/pkg/name" |
| 10 | + gv1 "github.com/google/go-containerregistry/pkg/v1" |
| 11 | + "github.com/google/go-containerregistry/pkg/v1/remote" |
| 12 | + "github.com/spf13/cobra" |
| 13 | + |
| 14 | + "hauler.dev/go/hauler/v2/internal/flags" |
| 15 | + "hauler.dev/go/hauler/v2/pkg/audit" |
| 16 | + "hauler.dev/go/hauler/v2/pkg/content" |
| 17 | + "hauler.dev/go/hauler/v2/pkg/log" |
| 18 | + "hauler.dev/go/hauler/v2/pkg/retry" |
| 19 | +) |
| 20 | + |
| 21 | +func addCopy(parent *cobra.Command, ro *flags.CliRootOpts) { |
| 22 | + o := &flags.ImageCopyOpts{} |
| 23 | + |
| 24 | + cmd := &cobra.Command{ |
| 25 | + Use: "copy SRC DST", |
| 26 | + Aliases: []string{"cp"}, |
| 27 | + Short: "(EXPERIMENTAL) Copy an artifact between registries", |
| 28 | + Example: ` # copy an image to another registry |
| 29 | + hauler copy busybox:latest registry.example.com/busybox:latest |
| 30 | +
|
| 31 | + # copy a specific platform out of a multi-arch image |
| 32 | + hauler copy ghcr.io/hauler-dev/hauler-debug:v2.0.3 registry.example.com/hauler-debug:v2.0.3 --platform linux/amd64 |
| 33 | +
|
| 34 | + # copy to a registry with a self-signed certificate |
| 35 | + hauler copy busybox:latest registry.example.com/busybox:latest --insecure-skip-tls-verify |
| 36 | +
|
| 37 | + # copy to a registry with no TLS at all |
| 38 | + hauler copy busybox:latest registry.example.com/busybox:latest --plain-http`, |
| 39 | + Args: cobra.ExactArgs(2), |
| 40 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 41 | + return CopyImageCmd(cmd.Context(), o, args[0], args[1], ro) |
| 42 | + }, |
| 43 | + } |
| 44 | + o.AddFlags(cmd) |
| 45 | + parent.AddCommand(cmd) |
| 46 | +} |
| 47 | + |
| 48 | +// CopyImageCmd copies src to dst directly, registry to registry, no store involved. |
| 49 | +func CopyImageCmd(ctx context.Context, o *flags.ImageCopyOpts, src, dst string, ro *flags.CliRootOpts) error { |
| 50 | + l := log.FromContext(ctx) |
| 51 | + |
| 52 | + retries, err := flags.ResolveRetries(o.Retries) |
| 53 | + if err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + |
| 57 | + tr, err := content.BuildTransport(o.InsecureSkipTLSVerify, o.CaFile) |
| 58 | + if err != nil { |
| 59 | + return err |
| 60 | + } |
| 61 | + |
| 62 | + opts := []remote.Option{ |
| 63 | + remote.WithAuthFromKeychain(authn.DefaultKeychain), |
| 64 | + remote.WithContext(ctx), |
| 65 | + remote.WithTransport(tr), |
| 66 | + } |
| 67 | + |
| 68 | + var nameOpts []gname.Option |
| 69 | + if o.PlainHTTP { |
| 70 | + nameOpts = append(nameOpts, gname.Insecure) |
| 71 | + } |
| 72 | + |
| 73 | + srcRef, err := gname.ParseReference(src, nameOpts...) |
| 74 | + if err != nil { |
| 75 | + return fmt.Errorf("parsing source reference %q: %w", src, err) |
| 76 | + } |
| 77 | + dstRef, err := gname.ParseReference(dst, nameOpts...) |
| 78 | + if err != nil { |
| 79 | + return fmt.Errorf("parsing destination reference %q: %w", dst, err) |
| 80 | + } |
| 81 | + |
| 82 | + l.Infof("copying [%s] to [%s]", src, dst) |
| 83 | + |
| 84 | + start := time.Now() |
| 85 | + var digest string |
| 86 | + err = retry.Operation(ctx, &flags.StoreRootOpts{Retries: retries}, ro, func() error { |
| 87 | + d, copyErr := copyOnce(srcRef, dstRef, o.Platform, opts) |
| 88 | + if copyErr == nil { |
| 89 | + digest = d |
| 90 | + } |
| 91 | + return copyErr |
| 92 | + }) |
| 93 | + if err != nil { |
| 94 | + l.Errorf("unable to copy [%s] to [%s]: %v", src, dst, err) |
| 95 | + return err |
| 96 | + } |
| 97 | + |
| 98 | + if flags.AuditLevel(ro) != "none" { |
| 99 | + e := audit.Entry{ |
| 100 | + Command: "copy", |
| 101 | + Args: []string{src, dst}, |
| 102 | + Type: "image", |
| 103 | + Reference: dst, |
| 104 | + Digest: digest, |
| 105 | + } |
| 106 | + if flags.AuditLevel(ro) == "verbose" { |
| 107 | + sys := audit.BuildSystem() |
| 108 | + g := audit.BuildGlobal(ro, nil) |
| 109 | + e.System = &sys |
| 110 | + e.Global = &g |
| 111 | + e.Flags = map[string]any{ |
| 112 | + "insecure-skip-tls-verify": o.InsecureSkipTLSVerify, |
| 113 | + "plain-http": o.PlainHTTP, |
| 114 | + "ca-file": o.CaFile, |
| 115 | + "platform": o.Platform, |
| 116 | + } |
| 117 | + } |
| 118 | + if err := audit.Append(ro.HaulerDir, e); err != nil { |
| 119 | + l.Warnf("failed to write audit entry: %v", err) |
| 120 | + } |
| 121 | + l.Debugf("generated audit id of [%s]", audit.ID()) |
| 122 | + } else { |
| 123 | + l.Debugf("generated audit id of [none]") |
| 124 | + } |
| 125 | + |
| 126 | + l.Infof("✓ copied [%s] to [%s] (%.1fs)", src, dst, time.Since(start).Seconds()) |
| 127 | + |
| 128 | + return nil |
| 129 | +} |
| 130 | + |
| 131 | +// copyOnce copies srcRef to dstRef and returns the digest copied. No |
| 132 | +// platform filter keeps a multi-arch index intact; platform picks one child. |
| 133 | +func copyOnce(srcRef, dstRef gname.Reference, platform string, opts []remote.Option) (string, error) { |
| 134 | + desc, err := remote.Get(srcRef, opts...) |
| 135 | + if err != nil { |
| 136 | + return "", fmt.Errorf("fetching descriptor for %q: %w", srcRef.Name(), err) |
| 137 | + } |
| 138 | + |
| 139 | + if idx, idxErr := desc.ImageIndex(); idxErr == nil && platform == "" { |
| 140 | + if err := remote.WriteIndex(dstRef, idx, opts...); err != nil { |
| 141 | + return "", fmt.Errorf("writing index for %q: %w", dstRef.Name(), err) |
| 142 | + } |
| 143 | + d, err := idx.Digest() |
| 144 | + if err != nil { |
| 145 | + return "", fmt.Errorf("getting index digest for %q: %w", srcRef.Name(), err) |
| 146 | + } |
| 147 | + return d.String(), nil |
| 148 | + } |
| 149 | + |
| 150 | + var img gv1.Image |
| 151 | + if platform != "" { |
| 152 | + p, err := gv1.ParsePlatform(platform) |
| 153 | + if err != nil { |
| 154 | + return "", err |
| 155 | + } |
| 156 | + img, err = remote.Image(srcRef, append(append([]remote.Option{}, opts...), remote.WithPlatform(*p))...) |
| 157 | + if err != nil { |
| 158 | + return "", fmt.Errorf("fetching image %q: %w", srcRef.Name(), err) |
| 159 | + } |
| 160 | + } else { |
| 161 | + img, err = desc.Image() |
| 162 | + if err != nil { |
| 163 | + return "", fmt.Errorf("fetching image %q: %w", srcRef.Name(), err) |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + if err := remote.Write(dstRef, img, opts...); err != nil { |
| 168 | + return "", fmt.Errorf("writing image for %q: %w", dstRef.Name(), err) |
| 169 | + } |
| 170 | + d, err := img.Digest() |
| 171 | + if err != nil { |
| 172 | + return "", fmt.Errorf("getting image digest for %q: %w", srcRef.Name(), err) |
| 173 | + } |
| 174 | + return d.String(), nil |
| 175 | +} |
0 commit comments