/
ecr_helper.go
353 lines (318 loc) · 13.1 KB
/
ecr_helper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package main
import (
"crypto/sha256"
"fmt"
"os"
"os/exec"
"path/filepath"
"reflect"
"regexp"
"strings"
"time"
ecrtypes "github.com/aws/aws-sdk-go-v2/service/ecr/types"
ecrpublictypes "github.com/aws/aws-sdk-go-v2/service/ecrpublic/types"
"github.com/go-logr/logr"
"github.com/jinzhu/copier"
"github.com/pkg/errors"
"golang.org/x/exp/slices"
api "github.com/aws/eks-anywhere-packages/api/v1alpha1"
)
const imageIndexMediaType = "application/vnd.oci.image.index.v1+json"
// removeDuplicates removes any duplicates from Version list, useful for scenarios when
// multiple tags for an image are present, this would cause duplicates on the bundle CRD,
// so we remove the first one in this case since they are the same thing.
// EX sha1234 is tagged 1.1 and 1.2 and sha5678 is tagged 1.2 this would result in a double match of 1.2 so we run this.
func removeDuplicates(s []api.SourceVersion) []api.SourceVersion {
k := make(map[string]bool)
l := []api.SourceVersion{}
for _, i := range s {
if _, j := k[i.Name]; !j {
k[i.Name] = true
l = append(l, api.SourceVersion{Name: i.Name, Digest: i.Digest})
}
}
return l
}
// stringInSlice checks to see if a string is in a slice
func stringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
// removeStringSlice removes a named string from a slice, without knowing it's index or it being ordered.
func removeStringSlice(l []string, item string) []string {
for i, other := range l {
if other == item {
return append(l[:i], l[i+1:]...)
}
}
return l
}
func deleteEmptyStringSlice(s []string) []string {
var r []string
for _, str := range s {
if str != "" {
r = append(r, str)
}
}
return r
}
func printSlice[v any](s []v) {
if len(s) == 0 {
return
}
fmt.Println(s[0])
printSlice(s[1:])
}
func printMap[k comparable, v any](m map[k]v) {
if len(m) == 0 {
return
}
for k, v := range m {
fmt.Println(k, ":", v)
}
}
// ExecCommand runs a given command, and constructs the log/output.
func ExecCommand(cmd *exec.Cmd) (string, error) {
commandOutput, err := cmd.CombinedOutput()
commandOutputStr := strings.TrimSpace(string(commandOutput))
if err != nil {
return commandOutputStr, errors.Cause(err)
}
return commandOutputStr, nil
}
// splitECRName is a helper function where some ECR repo's are formatted with "org/repo", and for aws repos it's just "repo"
func splitECRName(s string) (string, string, error) {
chartNameList := strings.Split(s, "/")
// Scenarios for ECR Public which contain and extra "/"
if strings.Contains(chartNameList[0], "public.ecr.aws") {
return strings.Join(chartNameList[2:], "/"), chartNameList[len(chartNameList)-1], nil
}
if len(chartNameList) > 1 {
return strings.Join(chartNameList[1:], "/"), chartNameList[len(chartNameList)-1], nil
}
return "", "", fmt.Errorf("Error: %s", "Failed parsing chartName, check the input URI is a valid ECR URI")
}
// imageTagFilter is used when filtering a list of images for a specific tag or tag substring
func ImageTagFilter(details []ImageDetailsBothECR, version string) []ImageDetailsBothECR {
var filteredDetails []ImageDetailsBothECR
for _, detail := range details {
for _, tag := range detail.ImageTags {
if strings.HasPrefix(tag, version) {
filteredDetails = append(filteredDetails, detail)
}
}
}
return filteredDetails
}
type ImageDetailsECR struct {
PrivateImageDetails ecrtypes.ImageDetail
PublicImageDetails ecrpublictypes.ImageDetail
}
// ImageDetailsBothECR is used so we can share some functions between private and public ECR bundle creation.
type ImageDetailsBothECR struct {
// The sha256 digest of the image manifest.
ImageDigest *string `copier:"ImageDigest"`
// The media type of the image manifest.
ImageManifestMediaType *string `copier:"ImageManifestMediaType"`
// The date and time, expressed in standard JavaScript date format, at which the
// current image was pushed to the repository.
ImagePushedAt *time.Time `copier:"ImagePushedAt"`
// The list of tags associated with this image.
ImageTags []string `copier:"ImageTags"`
// The Amazon Web Services account ID associated with the registry to which this
// image belongs.
RegistryId *string `copier:"RegistryId"`
// The name of the repository to which this image belongs.
RepositoryName *string `copier:"RepositoryName"`
}
func createECRImageDetails(images ImageDetailsECR) (ImageDetailsBothECR, error) {
t := &ImageDetailsBothECR{}
// Check for empty structs, if non empty copy to new common struct for ECR imagedetails.
if reflect.DeepEqual(images.PublicImageDetails, ecrpublictypes.ImageDetail{}) {
if images.PrivateImageDetails.ImageDigest != nil && images.PrivateImageDetails.ImagePushedAt != nil && images.PrivateImageDetails.RegistryId != nil && images.PrivateImageDetails.RepositoryName != nil {
copier.Copy(&t, &images.PrivateImageDetails)
return *t, nil
}
return ImageDetailsBothECR{}, fmt.Errorf("Error marshalling image details from ECR lookup.")
}
if reflect.DeepEqual(images.PrivateImageDetails, ecrtypes.ImageDetail{}) {
if images.PublicImageDetails.ImageDigest != nil && images.PublicImageDetails.ImagePushedAt != nil && images.PublicImageDetails.RegistryId != nil && images.PublicImageDetails.RepositoryName != nil {
copier.Copy(&t, &images.PublicImageDetails)
return *t, nil
}
return ImageDetailsBothECR{}, fmt.Errorf("Error marshalling image details from ECR lookup.")
}
return ImageDetailsBothECR{}, fmt.Errorf("Error no data passed to createImageDetails")
}
// getLatestHelmTagandSha Iterates list of ECR Helm Charts, to find latest pushed image and return tag/sha of the latest pushed image
func getLatestHelmTagandSha(details []ImageDetailsBothECR) (string, string, error) {
var latest ImageDetailsBothECR
latest.ImagePushedAt = &time.Time{}
for _, detail := range details {
if len(details) < 1 || detail.ImagePushedAt == nil || detail.ImageDigest == nil || detail.ImageTags == nil || len(detail.ImageTags) == 0 || *detail.ImageManifestMediaType != "application/vnd.oci.image.manifest.v1+json" {
continue
}
if detail.ImagePushedAt != nil && latest.ImagePushedAt.Before(*detail.ImagePushedAt) {
latest = detail
}
}
// Check if latest is equal to empty struct, and return error if that's the case.
if reflect.DeepEqual(latest, ImageDetailsBothECR{}) {
return "", "", fmt.Errorf("error no images found")
}
return latest.ImageTags[0], *latest.ImageDigest, nil
}
// getLatestImageSha Iterates list of ECR Public Helm Charts, to find latest pushed image and return tag/sha of the latest pushed image
func getLatestImageSha(details []ImageDetailsBothECR) (*api.SourceVersion, error) {
var latest ImageDetailsBothECR
latest.ImagePushedAt = &time.Time{}
for _, detail := range details {
if len(details) < 1 || detail.ImagePushedAt == nil || detail.ImageDigest == nil || detail.ImageTags == nil || len(detail.ImageTags) == 0 || *detail.ImageManifestMediaType != "application/vnd.oci.image.manifest.v1+json" {
continue
}
if detail.ImagePushedAt != nil && latest.ImagePushedAt.Before(*detail.ImagePushedAt) {
latest = detail
}
}
// Check if latest is equal to empty struct, and return error if that's the case.
if reflect.DeepEqual(latest, ecrpublictypes.ImageDetail{}) {
return nil, fmt.Errorf("error no images found")
}
return &api.SourceVersion{Name: latest.ImageTags[0], Digest: *latest.ImageDigest}, nil
}
// copyImage will copy an OCI artifact from one registry to another registry.
func copyImage(log logr.Logger, authFile, source, destination string) error {
// Create temporary directory for copying image artifacts locally
currentDir, err := os.Getwd()
if err != nil {
return err
}
tempImageDir := filepath.Join(currentDir, "temp-image-dir")
defer os.RemoveAll(tempImageDir)
// Copy image from source registry to local directory
log.Info("Copying source image to local directory", "Source", source, "Directory", fmt.Sprintf("dir://%s", tempImageDir))
cmd := exec.Command("skopeo", "copy", "--authfile", authFile, source, fmt.Sprintf("dir://%s", tempImageDir), "-f", "oci", "--all")
stdout, err := ExecCommand(cmd)
fmt.Printf("%s\n", stdout)
if err != nil {
return err
}
manifestFile := filepath.Join(tempImageDir, "manifest.json")
// Fetch manifest media type from manifest.json present inside the
// copied image directory
log.Info("Getting media type from root manifest JSON")
cmd = exec.Command("bash", "-c", fmt.Sprintf("cat %s | jq -r '.mediaType'", manifestFile))
mediaType, err := ExecCommand(cmd)
if err != nil {
return err
}
if mediaType == imageIndexMediaType {
// Remove manifest.json files corresponding to all artifacts that are //not images.
// These might be SBOMs, attributions which `skopeo copy` cannot handle. We filter
// these out by checking for artifacts that have an "unknown" architecture value.
log.Info("Removing manifest JSON files for all non-image artifacts")
log.Info("Getting non-image artifact digests")
cmd = exec.Command("bash", "-c", fmt.Sprintf("cat %s | jq -r '.manifests[] | select(.platform.architecture == \"unknown\").digest'", manifestFile))
stdout, err = ExecCommand(cmd)
if err != nil {
return err
}
nonImageDigests := strings.Split(stdout, "\n")
for _, digest := range nonImageDigests {
if digest != "" {
trimmedDigest := strings.TrimPrefix(digest, "sha256:")
manifestFile := filepath.Join(tempImageDir, fmt.Sprintf("%s.manifest.json", trimmedDigest))
err = os.Remove(manifestFile)
if err != nil {
return err
}
}
}
// Next we move on to the gzipped files representing artifact layers. We need to delete
// the layer files for all artifacts expect those corresponding to the images. So we filter
// each artifact that is of image type and compile the list of layer files to retain. Then we
// iterate over all the files in the local directory and delete everything except this list.
log.Info("Removing compressed layer files for all non-image artifacts")
filesToRetain := []string{"manifest.json", "version"}
log.Info("Getting image artifact digests")
cmd = exec.Command("bash", "-c", fmt.Sprintf("cat %s | jq -r '.manifests[] | select(.platform.architecture != \"unknown\").digest'", manifestFile))
stdout, err = ExecCommand(cmd)
if err != nil {
return err
}
imageDigests := strings.Split(stdout, "\n")
for _, digest := range imageDigests {
trimmedDigest := strings.TrimPrefix(digest, "sha256:")
filesToRetain = append(filesToRetain, fmt.Sprintf("%s.manifest.json", trimmedDigest))
manifestFile := filepath.Join(tempImageDir, fmt.Sprintf("%s.manifest.json", trimmedDigest))
log.Info("Getting config digest for image")
cmd = exec.Command("bash", "-c", fmt.Sprintf("cat %s | jq -r '.config.digest'", manifestFile))
stdout, err = ExecCommand(cmd)
if err != nil {
return err
}
configDigest := strings.TrimPrefix(stdout, "sha256:")
filesToRetain = append(filesToRetain, configDigest)
log.Info("Getting layer digests for image")
cmd = exec.Command("bash", "-c", fmt.Sprintf("cat %s | jq -r '.layers[].digest'", manifestFile))
stdout, err = ExecCommand(cmd)
if err != nil {
return err
}
layerDigests := strings.Split(stdout, "\n")
for _, digest := range layerDigests {
layerDigest := strings.TrimPrefix(digest, "sha256:")
if !slices.Contains(filesToRetain, layerDigest) {
filesToRetain = append(filesToRetain, layerDigest)
}
}
}
tempImageDirFiles, err := os.ReadDir(tempImageDir)
if err != nil {
return err
}
for _, file := range tempImageDirFiles {
if !slices.Contains(filesToRetain, file.Name()) {
err = os.Remove(filepath.Join(tempImageDir, file.Name()))
if err != nil {
return err
}
}
}
// Finally we update the root manifest.json to include only the image artifacts
// by deleting all other media types.
log.Info("Updating root manifest JSON contents to remove all non-image artifacts")
cmd = exec.Command("bash", "-c", fmt.Sprintf("cat %s | jq 'del(.manifests[] | select(.platform.architecture == \"unknown\"))'", manifestFile))
updatedManifestContents, err := ExecCommand(cmd)
if err != nil {
return err
}
err = os.WriteFile(manifestFile, []byte(updatedManifestContents), 0o644)
if err != nil {
return err
}
// When using digest references as URIs, Skopeo complains if the manifest digest
// does not not match the destination reference. So we update the destination
// digest reference to the actual digest of the manifest to avoid this issue.
if strings.Contains(destination, "@sha256:") {
imageDigestRegex := regexp.MustCompile("sha256:.*")
h := sha256.New()
h.Write([]byte(updatedManifestContents))
updatedManifestDigest := fmt.Sprintf("%x", h.Sum(nil))
destination = imageDigestRegex.ReplaceAllString(destination, fmt.Sprintf("sha256:%s", updatedManifestDigest))
}
}
// Copy image from local directory to destination registry
log.Info("Copying image from local directory to destination", "Directory", fmt.Sprintf("dir://%s", tempImageDir), "Destination", destination)
cmd = exec.Command("skopeo", "copy", "--authfile", authFile, fmt.Sprintf("dir://%s", tempImageDir), destination, "-f", "oci", "--all")
stdout, err = ExecCommand(cmd)
fmt.Printf("%s\n", stdout)
if err != nil {
return err
}
return nil
}