-
Notifications
You must be signed in to change notification settings - Fork 101
/
oci.go
787 lines (701 loc) · 26.9 KB
/
oci.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
// Copyright 2022, 2023 Chainguard, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package oci
import (
"archive/tar"
"context"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"sort"
"strings"
"time"
"github.com/avast/retry-go"
ecr "github.com/awslabs/amazon-ecr-credential-helper/ecr-login"
"github.com/chrismellard/docker-credential-acr-env/pkg/credhelper"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/authn/github"
"github.com/google/go-containerregistry/pkg/crane"
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/daemon"
"github.com/google/go-containerregistry/pkg/v1/empty"
"github.com/google/go-containerregistry/pkg/v1/google"
"github.com/google/go-containerregistry/pkg/v1/mutate"
"github.com/google/go-containerregistry/pkg/v1/remote"
v1tar "github.com/google/go-containerregistry/pkg/v1/tarball"
ggcrtypes "github.com/google/go-containerregistry/pkg/v1/types"
"github.com/google/shlex"
"github.com/sigstore/cosign/v2/pkg/oci"
ocimutate "github.com/sigstore/cosign/v2/pkg/oci/mutate"
ociremote "github.com/sigstore/cosign/v2/pkg/oci/remote"
"github.com/sigstore/cosign/v2/pkg/oci/signed"
"github.com/sigstore/cosign/v2/pkg/oci/static"
"github.com/sigstore/cosign/v2/pkg/oci/walk"
ctypes "github.com/sigstore/cosign/v2/pkg/types"
"github.com/sirupsen/logrus"
"chainguard.dev/apko/pkg/build/types"
"chainguard.dev/apko/pkg/options"
)
const (
LocalDomain = "apko.local"
LocalRepo = "cache"
)
var keychain = authn.NewMultiKeychain(
authn.DefaultKeychain,
google.Keychain,
authn.NewKeychainFromHelper(ecr.NewECRHelper(ecr.WithLogger(io.Discard))),
authn.NewKeychainFromHelper(credhelper.NewACRCredentialsHelper()),
github.Keychain,
)
func BuildImageFromLayer(layerTarGZ string, ic types.ImageConfiguration, logger *logrus.Entry, opts options.Options) (oci.SignedImage, error) {
return buildImageFromLayerWithMediaType(ggcrtypes.OCILayer, layerTarGZ, ic, opts.SourceDateEpoch, opts.Arch, logger, opts.SBOMPath, opts.SBOMFormats)
}
func BuildDockerImageFromLayer(layerTarGZ string, ic types.ImageConfiguration, logger *logrus.Entry, opts options.Options) (oci.SignedImage, error) {
return buildImageFromLayerWithMediaType(ggcrtypes.DockerLayer, layerTarGZ, ic, opts.SourceDateEpoch, opts.Arch, logger, opts.SBOMPath, opts.SBOMFormats)
}
func buildImageFromLayerWithMediaType(mediaType ggcrtypes.MediaType, layerTarGZ string, ic types.ImageConfiguration, created time.Time, arch types.Architecture, logger *logrus.Entry, sbomPath string, sbomFormats []string) (oci.SignedImage, error) {
imageType := humanReadableImageType(mediaType)
logger.Printf("building %s image from layer '%s'", imageType, layerTarGZ)
v1Layer, err := v1tar.LayerFromFile(layerTarGZ, v1tar.WithMediaType(mediaType))
if err != nil {
return nil, fmt.Errorf("failed to create %s layer from tar.gz: %w", imageType, err)
}
digest, err := v1Layer.Digest()
if err != nil {
return nil, fmt.Errorf("could not calculate layer digest: %w", err)
}
diffid, err := v1Layer.DiffID()
if err != nil {
return nil, fmt.Errorf("could not calculate layer diff id: %w", err)
}
logger.Printf("%s layer digest: %v", imageType, digest)
logger.Printf("%s layer diffID: %v", imageType, diffid)
adds := make([]mutate.Addendum, 0, 1)
adds = append(adds, mutate.Addendum{
Layer: v1Layer,
History: v1.History{
Author: "apko",
Comment: "This is an apko single-layer image",
CreatedBy: "apko",
Created: v1.Time{Time: created},
},
})
emptyImage := empty.Image
if mediaType == ggcrtypes.OCILayer {
// If building an OCI layer, then we should assume OCI manifest and config too
emptyImage = mutate.MediaType(emptyImage, ggcrtypes.OCIManifestSchema1)
emptyImage = mutate.ConfigMediaType(emptyImage, ggcrtypes.OCIConfigJSON)
}
v1Image, err := mutate.Append(emptyImage, adds...)
if err != nil {
return nil, fmt.Errorf("unable to append %s layer to empty image: %w", imageType, err)
}
annotations := ic.Annotations
if annotations == nil {
annotations = map[string]string{}
}
if ic.VCSUrl != "" {
if url, hash, ok := strings.Cut(ic.VCSUrl, "@"); ok {
annotations["org.opencontainers.image.source"] = url
annotations["org.opencontainers.image.revision"] = hash
}
}
if mediaType != ggcrtypes.DockerLayer && len(annotations) > 0 {
v1Image = mutate.Annotations(v1Image, annotations).(v1.Image)
}
cfg, err := v1Image.ConfigFile()
if err != nil {
return nil, fmt.Errorf("unable to get %s config file: %w", imageType, err)
}
cfg = cfg.DeepCopy()
cfg.Author = "github.com/chainguard-dev/apko"
platform := arch.ToOCIPlatform()
cfg.Architecture = platform.Architecture
cfg.Variant = platform.Variant
cfg.Created = v1.Time{Time: created}
cfg.Config.Labels = make(map[string]string)
cfg.OS = "linux"
// NOTE: Need to allow empty Entrypoints. The runtime will override to `/bin/sh -c` and handle quoting
switch {
case ic.Entrypoint.ShellFragment != "":
cfg.Config.Entrypoint = []string{"/bin/sh", "-c", ic.Entrypoint.ShellFragment}
case ic.Entrypoint.Command != "":
splitcmd, err := shlex.Split(ic.Entrypoint.Command)
if err != nil {
return nil, fmt.Errorf("unable to parse entrypoint command: %w", err)
}
cfg.Config.Entrypoint = splitcmd
}
if ic.Cmd != "" {
splitcmd, err := shlex.Split(ic.Cmd)
if err != nil {
return nil, fmt.Errorf("unable to parse cmd: %w", err)
}
cfg.Config.Cmd = splitcmd
}
if ic.WorkDir != "" {
cfg.Config.WorkingDir = ic.WorkDir
}
if len(ic.Environment) > 0 {
envs := []string{}
for k, v := range ic.Environment {
envs = append(envs, fmt.Sprintf("%s=%s", k, v))
}
sort.Strings(envs)
cfg.Config.Env = envs
} else {
cfg.Config.Env = []string{
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"SSL_CERT_FILE=/etc/ssl/certs/ca-certificates.crt",
}
}
if ic.Accounts.RunAs != "" {
cfg.Config.User = ic.Accounts.RunAs
}
v1Image, err = mutate.ConfigFile(v1Image, cfg)
if err != nil {
return nil, fmt.Errorf("unable to update %s config file: %w", imageType, err)
}
si := signed.Image(v1Image)
var ent oci.SignedEntity
var err2 error
if ent, err2 = attachSBOM(si, sbomPath, sbomFormats, arch, logger); err2 != nil {
return nil, fmt.Errorf("attaching SBOM to image: %w", err2)
}
return ent.(oci.SignedImage), nil
}
func Copy(src, dst string) error {
logrus.Infof("Copying %s to %s", src, dst)
if err := crane.Copy(src, dst, crane.WithAuthFromKeychain(keychain)); err != nil {
return fmt.Errorf("tagging %s with tag %s: %w", src, dst, err)
}
return nil
}
// PostAttachSBOM attaches the sboms to an already published image
func PostAttachSBOM(si oci.SignedEntity, sbomPath string, sbomFormats []string,
arch types.Architecture, logger *logrus.Entry, tags ...string,
) (oci.SignedEntity, error) {
var err2 error
if si, err2 = attachSBOM(si, sbomPath, sbomFormats, arch, logger); err2 != nil {
return nil, err2
}
for _, tag := range tags {
ref, err := name.ParseReference(tag)
if err != nil {
return nil, fmt.Errorf("parsing reference: %w", err)
}
// Write any attached SBOMs/signatures.
wp := writePeripherals(ref, logger, remote.WithAuthFromKeychain(keychain))
if err := wp(context.Background(), si); err != nil {
return nil, err
}
}
return si, nil
}
func attachSBOM(
si oci.SignedEntity, sbomPath string, sbomFormats []string,
arch types.Architecture, logger *logrus.Entry,
) (oci.SignedEntity, error) {
// Attach the SBOM, e.g.
// TODO(kaniini): Allow all SBOM types to be uploaded.
if len(sbomFormats) == 0 {
logrus.Debug("Not building sboms, no formats requested")
return si, nil
}
var mt ggcrtypes.MediaType
var path string
archName := arch.ToAPK()
if archName == "" {
archName = "index"
}
switch sbomFormats[0] {
case "spdx":
mt = ctypes.SPDXJSONMediaType
path = filepath.Join(sbomPath, fmt.Sprintf("sbom-%s.spdx.json", archName))
case "cyclonedx":
mt = ctypes.CycloneDXJSONMediaType
path = filepath.Join(sbomPath, fmt.Sprintf("sbom-%s.cdx", archName))
case "idb":
mt = "application/vnd.apko.installed-db"
path = filepath.Join(sbomPath, fmt.Sprintf("sbom-%s.idb", archName))
default:
return nil, fmt.Errorf("unsupported SBOM format: %s", sbomFormats[0])
}
if len(sbomFormats) > 1 {
// When we have multiple formats, warn that we're picking the first.
logger.Warnf("multiple SBOM formats requested, uploading SBOM with media type: %s", mt)
}
sbom, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("reading sbom: %w", err)
}
f, err := static.NewFile(sbom, static.WithLayerMediaType(mt))
if err != nil {
return nil, err
}
var aterr error
if i, ok := si.(oci.SignedImage); ok {
si, aterr = ocimutate.AttachFileToImage(i, "sbom", f)
} else if ii, ok := si.(oci.SignedImageIndex); ok {
si, aterr = ocimutate.AttachFileToImageIndex(ii, "sbom", f)
} else {
return nil, errors.New("unable to cast signed signedentity as image or index")
}
if aterr != nil {
return nil, fmt.Errorf("attaching file to image: %w", aterr)
}
return si, nil
}
func BuildImageTarballFromLayer(imageRef string, layerTarGZ string, outputTarGZ string, ic types.ImageConfiguration, logger *logrus.Entry, opts options.Options) error {
if opts.UseDockerMediaTypes {
return buildImageTarballFromLayerWithMediaType(ggcrtypes.DockerLayer, imageRef, layerTarGZ, outputTarGZ, ic, logger, opts)
}
return buildImageTarballFromLayerWithMediaType(ggcrtypes.OCILayer, imageRef, layerTarGZ, outputTarGZ, ic, logger, opts)
}
func buildImageTarballFromLayerWithMediaType(mediaType ggcrtypes.MediaType, imageRef string, layerTarGZ string, outputTarGZ string, ic types.ImageConfiguration, logger *logrus.Entry, opts options.Options) error {
imageType := humanReadableImageType(mediaType)
v1Image, err := buildImageFromLayerWithMediaType(mediaType, layerTarGZ, ic, opts.SourceDateEpoch, opts.Arch, logger, opts.SBOMPath, opts.SBOMFormats)
if err != nil {
return err
}
if v1Image == nil {
return errors.New("image build from layer returned nil")
}
imgRefTag, err := name.NewTag(imageRef)
if err != nil {
return fmt.Errorf("unable to validate image reference tag: %w", err)
}
if err := v1tar.WriteToFile(outputTarGZ, imgRefTag, v1Image); err != nil {
return fmt.Errorf("unable to write %s image to disk: %w", imageType, err)
}
logger.Printf("output %s image file to %s", imageType, outputTarGZ)
return nil
}
func publishTagFromImage(image oci.SignedImage, imageRef string, hash v1.Hash, local bool, logger *logrus.Entry) (name.Digest, error) {
imgRef, err := name.ParseReference(imageRef)
if err != nil {
return name.Digest{}, fmt.Errorf("unable to parse reference: %w", err)
}
if local {
localSrcTagStr := fmt.Sprintf("%s/%s:%s", LocalDomain, LocalRepo, hash.Hex)
localSrcTag, err := name.NewTag(localSrcTagStr)
if err != nil {
return name.Digest{}, err
}
logger.Infof("saving OCI image locally: %s", localSrcTag.Name())
resp, err := daemon.Write(localSrcTag, image)
if err != nil {
logger.Errorf("docker daemon error: %s", strings.ReplaceAll(resp, "\n", "\\n"))
return name.Digest{}, fmt.Errorf("failed to save OCI image locally: %w", err)
}
logger.Debugf("docker daemon response: %s", strings.ReplaceAll(resp, "\n", "\\n"))
localDstTag, err := name.NewTag(imageRef)
if err != nil {
return name.Digest{}, err
}
if strings.HasPrefix(localSrcTag.Name(), fmt.Sprintf("%s/", LocalDomain)) {
logger.Warnf("skipping local domain tagging %s as %s", localSrcTag.Name(), localDstTag.Name())
} else {
logger.Printf("tagging local image %s as %s", localSrcTag.Name(), localDstTag.Name())
if err := daemon.Tag(localSrcTag, localDstTag); err != nil {
return name.Digest{}, err
}
}
return name.NewDigest(fmt.Sprintf("%s@%s", localSrcTag.Name(), hash))
}
// Write any attached SBOMs/signatures.
wp := writePeripherals(imgRef, logger, remote.WithAuthFromKeychain(keychain))
if err := wp(context.Background(), image); err != nil {
return name.Digest{}, err
}
if err := retry.Do(func() error {
return remote.Write(imgRef, image, remote.WithAuthFromKeychain(keychain))
}); err != nil {
return name.Digest{}, fmt.Errorf("failed to publish: %w", err)
}
return imgRef.Context().Digest(hash.String()), nil
}
func PublishImageFromLayer(layerTarGZ string, ic types.ImageConfiguration, created time.Time, arch types.Architecture, logger *logrus.Entry, sbomPath string, sbomFormats []string, local bool, shouldPushTags bool, tags ...string) (name.Digest, oci.SignedImage, error) {
return publishImageFromLayerWithMediaType(ggcrtypes.OCILayer, layerTarGZ, ic, created, arch, logger, sbomPath, sbomFormats, local, shouldPushTags, tags...)
}
func PublishDockerImageFromLayer(layerTarGZ string, ic types.ImageConfiguration, created time.Time, arch types.Architecture, logger *logrus.Entry, sbomPath string, sbomFormats []string, local bool, shouldPushTags bool, tags ...string) (name.Digest, oci.SignedImage, error) {
return publishImageFromLayerWithMediaType(ggcrtypes.DockerLayer, layerTarGZ, ic, created, arch, logger, sbomPath, sbomFormats, local, shouldPushTags, tags...)
}
func publishImageFromLayerWithMediaType(mediaType ggcrtypes.MediaType, layerTarGZ string, ic types.ImageConfiguration, created time.Time, arch types.Architecture, logger *logrus.Entry, sbomPath string, sbomFormats []string, local bool, shouldPushTags bool, tags ...string) (name.Digest, oci.SignedImage, error) {
v1Image, err := buildImageFromLayerWithMediaType(mediaType, layerTarGZ, ic, created, arch, logger, sbomPath, sbomFormats)
if err != nil {
return name.Digest{}, nil, err
}
h, err := v1Image.Digest()
if err != nil {
return name.Digest{}, nil, fmt.Errorf("failed to compute digest: %w", err)
}
digest := name.Digest{}
if shouldPushTags {
for _, tag := range tags {
logger.Printf("publishing image tag %v", tag)
digest, err = publishTagFromImage(v1Image, tag, h, local, logger)
if err != nil {
return name.Digest{}, nil, err
}
}
} else {
logger.Println("publishing image without tag (digest only)")
digestOnly := fmt.Sprintf("%s@%s", strings.Split(tags[0], ":")[0], h.String())
digest, err = publishTagFromImage(v1Image, digestOnly, h, local, logger)
if err != nil {
return name.Digest{}, nil, err
}
}
return digest, v1Image, nil
}
func PublishIndex(ic types.ImageConfiguration, imgs map[types.Architecture]oci.SignedImage, logger *logrus.Entry, local bool, shouldPushTags bool, tags ...string) (name.Digest, oci.SignedImageIndex, error) {
return publishIndexWithMediaType(ggcrtypes.OCIImageIndex, ic, imgs, logger, local, shouldPushTags, tags...)
}
func PublishDockerIndex(ic types.ImageConfiguration, imgs map[types.Architecture]oci.SignedImage, logger *logrus.Entry, local bool, shouldPushTags bool, tags ...string) (name.Digest, oci.SignedImageIndex, error) {
return publishIndexWithMediaType(ggcrtypes.DockerManifestList, ic, imgs, logger, local, shouldPushTags, tags...)
}
func publishIndexWithMediaType(mediaType ggcrtypes.MediaType, _ types.ImageConfiguration, imgs map[types.Architecture]oci.SignedImage, logger *logrus.Entry, local bool, shouldPushTags bool, tags ...string) (name.Digest, oci.SignedImageIndex, error) {
idx := signed.ImageIndex(mutate.IndexMediaType(empty.Index, mediaType))
archs := make([]types.Architecture, 0, len(imgs))
for arch := range imgs {
archs = append(archs, arch)
}
sort.Slice(archs, func(i, j int) bool {
return archs[i].String() < archs[j].String()
})
for _, arch := range archs {
img := imgs[arch]
mt, err := img.MediaType()
if err != nil {
return name.Digest{}, nil, fmt.Errorf("failed to get mediatype: %w", err)
}
h, err := img.Digest()
if err != nil {
return name.Digest{}, nil, fmt.Errorf("failed to compute digest: %w", err)
}
size, err := img.Size()
if err != nil {
return name.Digest{}, nil, fmt.Errorf("failed to compute size: %w", err)
}
idx = ocimutate.AppendManifests(idx, ocimutate.IndexAddendum{
Add: img,
Descriptor: v1.Descriptor{
MediaType: mt,
Digest: h,
Size: size,
Platform: arch.ToOCIPlatform(),
},
})
}
// TODO(jason): Also set annotations on the index. ggcr's
// pkg/v1/mutate.Annotations will drop the interface methods from
// oci.SignedImageIndex, so we may need to reimplement
// mutate.Annotations in ocimutate to keep it for now.
// If attempting to save locally, pick the native architecture
// and use that cached image for local tags
// Ported from https://github.com/ko-build/ko/blob/main/pkg/publish/daemon.go#L92-L168
if local {
im, err := idx.IndexManifest()
if err != nil {
return name.Digest{}, nil, err
}
goos, goarch := os.Getenv("GOOS"), os.Getenv("GOARCH")
if goos == "" {
goos = "linux"
}
if goarch == "" {
goarch = "amd64"
}
// Default to just using the first one in the list if we cannot match
useManifest := im.Manifests[0]
for _, manifest := range im.Manifests {
if manifest.Platform == nil {
continue
}
if manifest.Platform.OS != goos {
continue
}
if manifest.Platform.Architecture != goarch {
continue
}
useManifest = manifest
}
localSrcTagStr := fmt.Sprintf("%s/%s:%s", LocalDomain, LocalRepo, useManifest.Digest.Hex)
logger.Printf("using best guess single-arch image for local tags: %s (%s/%s)", localSrcTagStr, goos, goarch)
localSrcTag, err := name.NewTag(localSrcTagStr)
if err != nil {
return name.Digest{}, nil, err
}
for _, tag := range tags {
localDstTag, err := name.NewTag(tag)
if err != nil {
return name.Digest{}, nil, err
}
if strings.HasPrefix(localSrcTag.Name(), fmt.Sprintf("%s/", LocalDomain)) {
logger.Warnf("skipping local domain tagging %s as %s", localSrcTag.Name(), localDstTag.Name())
} else {
logger.Printf("tagging local image %s as %s", localSrcTag.Name(), localDstTag.Name())
if err := daemon.Tag(localSrcTag, localDstTag); err != nil {
return name.Digest{}, nil, err
}
}
}
digest, err := name.NewDigest(fmt.Sprintf("%s@%s", localSrcTag.Name(), useManifest.Digest.String()))
if err != nil {
return name.Digest{}, nil, err
}
return digest, idx, nil
}
h, err := idx.Digest()
if err != nil {
return name.Digest{}, nil, err
}
digest := name.Digest{}
if shouldPushTags {
for _, tag := range tags {
logger.Printf("publishing index tag %v", tag)
digest, err = publishTagFromIndex(idx, tag, h, logger)
if err != nil {
return name.Digest{}, nil, err
}
}
} else {
logger.Println("publishing index without tag (digest only)")
digestOnly := fmt.Sprintf("%s@%s", strings.Split(tags[0], ":")[0], h.String())
digest, err = publishTagFromIndex(idx, digestOnly, h, logger)
if err != nil {
return name.Digest{}, nil, err
}
}
return digest, idx, nil
}
func publishTagFromIndex(index oci.SignedImageIndex, imageRef string, hash v1.Hash, logger *logrus.Entry) (name.Digest, error) {
ref, err := name.ParseReference(imageRef)
if err != nil {
return name.Digest{}, fmt.Errorf("unable to parse reference: %w", err)
}
// Write any attached SBOMs/signatures (recursively)
wp := writePeripherals(ref, logger, remote.WithAuthFromKeychain(keychain))
if err := walk.SignedEntity(context.Background(), index, wp); err != nil {
return name.Digest{}, err
}
if err := retry.Do(func() error {
return remote.WriteIndex(ref, index, remote.WithAuthFromKeychain(keychain))
}); err != nil {
return name.Digest{}, fmt.Errorf("failed to publish: %w", err)
}
return ref.Context().Digest(hash.String()), nil
}
// BuildIndex builds an index in a tar.gz file containing all architectures, given their individual image tar.gz files.
// Uses the standard OCI media type for the image index.
// Returns the digest and the path to the combined tar.gz.
func BuildIndex(outfile string, ic types.ImageConfiguration, imgs map[types.Architecture]oci.SignedImage, tags []string, logger *logrus.Entry) (name.Digest, error) {
return buildIndexWithMediaType(outfile, ggcrtypes.OCIImageIndex, ic, imgs, tags, logger)
}
// BuildIndex builds an index in a tar.gz file containing all architectures, given their individual image tar.gz files.
// Uses the legacy docker media type for the image index, i.e. multiarch manifest.
// Returns the digest and the path to the combined tar.gz.
func BuildDockerIndex(outfile string, ic types.ImageConfiguration, imgs map[types.Architecture]oci.SignedImage, tags []string, logger *logrus.Entry) (name.Digest, error) {
return buildIndexWithMediaType(outfile, ggcrtypes.DockerManifestList, ic, imgs, tags, logger)
}
func buildIndexWithMediaType(outfile string, mediaType ggcrtypes.MediaType, _ types.ImageConfiguration, imgs map[types.Architecture]oci.SignedImage, tags []string, logger *logrus.Entry) (name.Digest, error) {
idx := signed.ImageIndex(mutate.IndexMediaType(empty.Index, mediaType))
tagsToImages := make(map[name.Tag]v1.Image)
archs := make([]types.Architecture, 0, len(imgs))
for arch := range imgs {
archs = append(archs, arch)
}
sort.Slice(archs, func(i, j int) bool {
return archs[i].String() < archs[j].String()
})
for _, arch := range archs {
logger.Printf("adding %s to index", arch)
img := imgs[arch]
mt, err := img.MediaType()
if err != nil {
return name.Digest{}, fmt.Errorf("failed to get mediatype for image: %w", err)
}
h, err := img.Digest()
if err != nil {
return name.Digest{}, fmt.Errorf("failed to compute digest for image: %w", err)
}
size, err := img.Size()
if err != nil {
return name.Digest{}, fmt.Errorf("failed to compute size for image: %w", err)
}
for _, tagName := range tags {
ref, err := name.NewTag(tagName)
if err != nil {
return name.Digest{}, fmt.Errorf("failed to parse tag %s: %w", tagName, err)
}
ref, err = name.NewTag(fmt.Sprintf("%s-%s", ref.Name(), strings.ReplaceAll(arch.String(), "/", "_")))
if err != nil {
return name.Digest{}, fmt.Errorf("failed to parse tag %s: %w", tagName, err)
}
if err != nil {
return name.Digest{}, fmt.Errorf("failed to create tag for image: %w", err)
}
tagsToImages[ref] = img
}
idx = ocimutate.AppendManifests(idx, ocimutate.IndexAddendum{
Add: img,
Descriptor: v1.Descriptor{
MediaType: mt,
Digest: h,
Size: size,
Platform: arch.ToOCIPlatform(),
},
})
}
f, err := os.OpenFile(outfile, os.O_CREATE|os.O_RDWR, 0o644)
if err != nil {
return name.Digest{}, fmt.Errorf("failed to open outfile %s: %w", outfile, err)
}
defer f.Close()
h, err := idx.Digest()
if err != nil {
return name.Digest{}, err
}
digest, err := name.NewDigest(fmt.Sprintf("%s@%s", "image", h.String()))
if err != nil {
return name.Digest{}, err
}
if err := v1tar.MultiWrite(tagsToImages, f); err != nil {
return name.Digest{}, fmt.Errorf("failed to write index to tgz: %w", err)
}
// to append to a tar archive, you need to find the last file in the archive
if _, err = f.Seek(0, io.SeekStart); err != nil {
return name.Digest{}, fmt.Errorf("failed to seek beginning of file: %w", err)
}
tr := tar.NewReader(f)
var lastFileSize, lastStreamPos int64
for {
hdr, err := tr.Next()
if errors.Is(err, io.EOF) {
break
}
if err != nil {
return name.Digest{}, fmt.Errorf("failed to read tar header: %w", err)
}
lastStreamPos, err = f.Seek(0, io.SeekCurrent)
if err != nil {
return name.Digest{}, fmt.Errorf("failed to get current position in file: %w", err)
}
lastFileSize = hdr.Size
}
const blockSize = 512
newOffset := lastStreamPos + lastFileSize
newOffset += blockSize - (newOffset % blockSize) // shift to next-nearest block boundary
if _, err := f.Seek(newOffset, io.SeekStart); err != nil {
return name.Digest{}, fmt.Errorf("failed to seek to new offset: %w", err)
}
tw := tar.NewWriter(f)
defer tw.Close()
// write each manifest file
for _, arch := range archs {
img := imgs[arch]
raw, err := img.RawManifest()
if err != nil {
return name.Digest{}, fmt.Errorf("failed to get raw manifest: %w", err)
}
dig, err := img.Digest()
if err != nil {
return name.Digest{}, fmt.Errorf("failed to get digest for manifest: %w", err)
}
if err := tw.WriteHeader(&tar.Header{
Name: dig.String(),
Size: int64(len(raw)),
Mode: 0o644,
}); err != nil {
return name.Digest{}, fmt.Errorf("failed to write manifest header: %w", err)
}
if _, err := tw.Write(raw); err != nil {
return name.Digest{}, fmt.Errorf("failed to write manifest: %w", err)
}
}
// Write the index.json
index, err := idx.RawManifest()
if err != nil {
return name.Digest{}, fmt.Errorf("failed to get raw index: %w", err)
}
if err := tw.WriteHeader(&tar.Header{
Name: "index.json",
Size: int64(len(index)),
Mode: 0o644,
}); err != nil {
return name.Digest{}, fmt.Errorf("failed to write index.json header: %w", err)
}
if _, err := tw.Write(index); err != nil {
return name.Digest{}, fmt.Errorf("failed to write index.json: %w", err)
}
return digest, nil
}
func writePeripherals(tag name.Reference, logger *logrus.Entry, opt ...remote.Option) walk.Fn {
ociOpts := []ociremote.Option{ociremote.WithRemoteOptions(opt...)}
// Respect COSIGN_REPOSITORY
targetRepoOverride, err := ociremote.GetEnvTargetRepository()
if err != nil {
return func(ctx context.Context, se oci.SignedEntity) error { return err }
}
if (targetRepoOverride != name.Repository{}) {
ociOpts = append(ociOpts, ociremote.WithTargetRepository(targetRepoOverride))
}
return func(ctx context.Context, se oci.SignedEntity) error {
h, err := se.(interface{ Digest() (v1.Hash, error) }).Digest()
if err != nil {
return err
}
// TODO(mattmoor): We should have a WriteSBOM helper upstream.
digest := tag.Context().Digest(h.String()) // Don't *get* the tag, we know the digest
ref, err := ociremote.SBOMTag(digest, ociOpts...)
if err != nil {
return err
}
f, err := se.Attachment("sbom")
if err != nil {
// Some levels (e.g. the index) may not have an SBOM,
// just like some levels may not have signatures/attestations.
return nil
}
if err := retry.Do(func() error {
return remote.Write(ref, f, opt...)
}); err != nil {
return fmt.Errorf("writing sbom: %w", err)
}
// TODO(mattmoor): Don't enable this until we start signing or it
// will publish empty signatures!
// if err := ociremote.WriteSignatures(tag.Context(), se, ociOpts...); err != nil {
// return err
// }
// TODO(mattmoor): Are there any attestations we want to write?
// if err := ociremote.WriteAttestations(tag.Context(), se, ociOpts...); err != nil {
// return err
// }
logger.Printf("Published SBOM %v", ref)
return nil
}
}
func humanReadableImageType(mediaType ggcrtypes.MediaType) string {
switch mediaType {
case ggcrtypes.DockerLayer:
return "Docker"
case ggcrtypes.OCILayer:
return "OCI"
}
return "unknown"
}