|
| 1 | +package imagelist |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "strings" |
| 9 | + "sync" |
| 10 | + |
| 11 | + "github.com/google/go-containerregistry/pkg/crane" |
| 12 | + "github.com/google/go-containerregistry/pkg/name" |
| 13 | +) |
| 14 | + |
| 15 | +var externalImages = map[string]string{ |
| 16 | + "sig-storage/snapshot-controller": "registry.k8s.io/sig-storage/snapshot-controller", |
| 17 | + "sig-storage/snapshot-validation-webhook": "registry.k8s.io/sig-storage/snapshot-validation-webhook", |
| 18 | + "rancher/mirrored-sig-storage-csi-node-driver-registrar": "registry.k8s.io/sig-storage/csi-node-driver-registrar", |
| 19 | + "rancher/mirrored-sig-storage-csi-attacher": "registry.k8s.io/sig-storage/csi-attacher", |
| 20 | + "rancher/mirrored-longhornio-csi-attacher": "registry.k8s.io/sig-storage/csi-attacher", |
| 21 | + "rancher/mirrored-sig-storage-csi-provisioner": "registry.k8s.io/sig-storage/csi-provisioner", |
| 22 | + "rancher/mirrored-sig-storage-csi-resizer": "registry.k8s.io/sig-storage/csi-resizer", |
| 23 | + "rancher/mirrored-sig-storage-csi-snapshotter": "registry.k8s.io/sig-storage/csi-snapshotter", |
| 24 | + "rancher/mirrored-sig-storage-livenessprobe": "registry.k8s.io/sig-storage/rancher/mirrored-sig-storage-livenessprobe", |
| 25 | + "rancher/mirrored-sig-storage-snapshot-controller": "registry.k8s.io/sig-storage/snapshot-controller", |
| 26 | + "rancher/appco-redis": "dp.apps.rancher.io/containers/redis", |
| 27 | + "rancher/mirrored-cilium-cilium": "quay.io/cilium/cilium", |
| 28 | + "rancher/mirrored-cilium-cilium-envoy": "quay.io/cilium/cilium-envoy", |
| 29 | + "rancher/mirrored-cilium-clustermesh-apiserver": "quay.io/cilium/clustermesh-apiserver", |
| 30 | + "rancher/mirrored-cilium-hubble-relay": "quay.io/cilium/hubble-relay", |
| 31 | + "rancher/mirrored-cilium-operator-aws": "quay.io/cilium/operator-aws", |
| 32 | + "rancher/mirrored-cilium-operator-azure": "quay.io/cilium/operator-azure", |
| 33 | + "rancher/mirrored-cilium-operator-generic": "quay.io/cilium/operator-generic", |
| 34 | + "rancher/mirrored-kube-logging-config-reloader": "ghcr.io/kube-logging/config-reloader", |
| 35 | + "rancher/mirrored-kube-logging-logging-operator": "ghcr.io/kube-logging/logging-operator", |
| 36 | + "rancher/mirrored-kube-state-metrics-kube-state-metrics": "registry.k8s.io/kube-state-metrics/kube-state-metrics", |
| 37 | + "rancher/mirrored-elemental-operator": "registry.suse.com/rancher/elemental-operator", |
| 38 | + "rancher/mirrored-elemental-seedimage-builder": "registry.suse.com/rancher/seedimage-builder", |
| 39 | + "rancher/mirrored-cluster-api-controller": "registry.k8s.io/cluster-api/cluster-api-controller", |
| 40 | +} |
| 41 | + |
| 42 | +type imageCopier struct { |
| 43 | + m sync.Mutex |
| 44 | + mirroredOnly bool |
| 45 | +} |
| 46 | + |
| 47 | +func (i *imageCopier) Copy(srcImg, dstRegistry string) Entry { |
| 48 | + entry := Entry{ |
| 49 | + Image: srcImg, |
| 50 | + } |
| 51 | + |
| 52 | + if i.mirroredOnly && !strings.Contains(srcImg, "mirrored") { |
| 53 | + entry.Error = errors.New("skipping non-mirrored image: " + srcImg) |
| 54 | + return entry |
| 55 | + } |
| 56 | + |
| 57 | + ref, err := name.ParseReference(srcImg, name.WeakValidation) |
| 58 | + if err != nil { |
| 59 | + entry.Error = err |
| 60 | + return entry |
| 61 | + } |
| 62 | + |
| 63 | + reg, err := name.NewRegistry(dstRegistry) |
| 64 | + if err != nil { |
| 65 | + entry.Error = err |
| 66 | + return entry |
| 67 | + } |
| 68 | + |
| 69 | + repo := reg.Repo(ref.Context().RepositoryStr()) |
| 70 | + dst := repo.Tag(ref.Identifier()).String() |
| 71 | + |
| 72 | + ctx := context.TODO() |
| 73 | + |
| 74 | + // Reset stdout/stderr to avoid verbose output from cosign. |
| 75 | + i.m.Lock() |
| 76 | + stdout := os.Stdout |
| 77 | + stderr := os.Stderr |
| 78 | + os.Stdout = nil |
| 79 | + os.Stderr = nil |
| 80 | + |
| 81 | + // We shouldn't copy signatures if the image isn't there, so copy images |
| 82 | + // but don't override them if they are already present. |
| 83 | + err = copySignature(ctx, srcImg, dst, true) |
| 84 | + if err != nil { |
| 85 | + entry.Error = err |
| 86 | + } |
| 87 | + entry.Signed = (err == nil) |
| 88 | + |
| 89 | + os.Stdout = stdout |
| 90 | + os.Stderr = stderr |
| 91 | + i.m.Unlock() |
| 92 | + |
| 93 | + return entry |
| 94 | +} |
| 95 | + |
| 96 | +func signatureSource(srcRef name.Reference, tag string) string { |
| 97 | + repo := srcRef.Context().RepositoryStr() |
| 98 | + if upstream, found := externalImages[repo]; found { |
| 99 | + return fmt.Sprintf("%s:%s", upstream, tag) |
| 100 | + } |
| 101 | + |
| 102 | + // Fully qualified reference: <registry>/<repository>:<signature_tag> |
| 103 | + return fmt.Sprintf("%s:%s", srcRef.Context().Name(), tag) |
| 104 | +} |
| 105 | + |
| 106 | +func copySignature(ctx context.Context, srcImgRef, dstImgRef string, copyImage bool) error { |
| 107 | + fmt.Println(srcImgRef, dstImgRef) |
| 108 | + if copyImage { |
| 109 | + err := crane.Copy(srcImgRef, dstImgRef, |
| 110 | + crane.WithContext(ctx), |
| 111 | + crane.WithNoClobber(true)) // ensure tags won't be overwritten. |
| 112 | + |
| 113 | + if err != nil && !strings.Contains(err.Error(), "refusing to clobber existing tag") { |
| 114 | + return fmt.Errorf("failed to copy image from %q to %q: %w", |
| 115 | + srcImgRef, dstImgRef, err) |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + digest, err := crane.Digest(srcImgRef) |
| 120 | + if err != nil { |
| 121 | + return fmt.Errorf("failed to get signed image digest for %q: %w", srcImgRef, err) |
| 122 | + } |
| 123 | + |
| 124 | + sourceRef, err := name.ParseReference(srcImgRef) |
| 125 | + if err != nil { |
| 126 | + return fmt.Errorf("failed to parse source image reference: %w", err) |
| 127 | + } |
| 128 | + |
| 129 | + hex := strings.TrimPrefix(digest, "sha256:") |
| 130 | + signatureTag := fmt.Sprintf("sha256-%s.sig", hex) |
| 131 | + sourceSigRef := signatureSource(sourceRef, signatureTag) |
| 132 | + |
| 133 | + targetRef, err := name.ParseReference(dstImgRef) |
| 134 | + if err != nil { |
| 135 | + return fmt.Errorf("failed to parse target image reference: %w", err) |
| 136 | + } |
| 137 | + |
| 138 | + dstSigRef := fmt.Sprintf("%s:%s", targetRef.Context().Name(), signatureTag) |
| 139 | + err = crane.Copy(sourceSigRef, dstSigRef, |
| 140 | + crane.WithContext(ctx), |
| 141 | + crane.WithNoClobber(true), // ensure existing signatures won't be overwritten. |
| 142 | + ) |
| 143 | + if err != nil && !strings.Contains(err.Error(), "refusing to clobber existing tag") { |
| 144 | + return fmt.Errorf("failed to copy signature from %q to %q: %w", sourceSigRef, dstSigRef, err) |
| 145 | + } |
| 146 | + |
| 147 | + return nil |
| 148 | +} |
0 commit comments