-
Notifications
You must be signed in to change notification settings - Fork 0
/
remote.go
178 lines (157 loc) · 4.86 KB
/
remote.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
//
// Copyright 2021 The Sigstore Authors.
//
// 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 remote
import (
"fmt"
"io"
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/google/go-containerregistry/pkg/v1/types"
"github.com/matthogan/zc/pkg/oci"
)
// These enable mocking for unit testing without faking an entire registry.
var (
remoteImage = remote.Image
remoteIndex = remote.Index
remoteGet = remote.Get
remoteWrite = remote.Write
remoteDelete = remote.Delete
)
// SignedEntity provides access to a remote reference, and its signatures.
// The SignedEntity will be one of SignedImage or SignedImageIndex.
func SignedEntity(ref name.Reference, options ...Option) (oci.SignedEntity, error) {
o := makeOptions(ref.Context(), options...)
got, err := remoteGet(ref, o.ROpt...)
if err != nil {
return nil, err
}
switch got.MediaType {
case types.OCIImageIndex, types.DockerManifestList:
ii, err := got.ImageIndex()
if err != nil {
return nil, err
}
return &index{
v1Index: ii,
ref: ref.Context().Digest(got.Digest.String()),
opt: o,
}, nil
case types.OCIManifestSchema1, types.DockerManifestSchema2:
i, err := got.Image()
if err != nil {
return nil, err
}
return &image{
Image: i,
opt: o,
}, nil
default:
return nil, fmt.Errorf("unknown mime type: %v", got.MediaType)
}
}
// normalize turns image digests into tags with optional prefix & suffix:
// sha256:d34db33f -> [prefix]sha256-d34db33f[.suffix]
func normalize(h v1.Hash, prefix string, suffix string) string {
if suffix == "" {
return fmt.Sprint(prefix, h.Algorithm, "-", h.Hex)
}
return fmt.Sprint(prefix, h.Algorithm, "-", h.Hex, ".", suffix)
}
// SBOMTag returns the name.Tag that associated SBOMs with a particular digest.
func SBOMTag(ref name.Reference, opts ...Option) (name.Tag, error) {
o := makeOptions(ref.Context(), opts...)
return suffixTag(ref, o.SBOMSuffix, o)
}
func suffixTag(ref name.Reference, suffix string, o *options) (name.Tag, error) {
var h v1.Hash
if digest, ok := ref.(name.Digest); ok {
var err error
h, err = v1.NewHash(digest.DigestStr())
if err != nil { // This is effectively impossible.
return name.Tag{}, err
}
} else {
desc, err := remoteGet(ref, o.ROpt...)
if err != nil {
return name.Tag{}, err
}
h = desc.Digest
}
return o.TargetRepository.Tag(normalize(h, o.TagPrefix, suffix)), nil
}
type digestable interface {
Digest() (v1.Hash, error)
}
// signatures is a shared implementation of the oci.Signed* Signatures method.
func signatures(digestable digestable, o *options) (oci.Signatures, error) {
h, err := digestable.Digest()
if err != nil {
return nil, err
}
return Signatures(o.TargetRepository.Tag(normalize(h, o.TagPrefix, o.SignatureSuffix)), o.OriginalOptions...)
}
// attestations is a shared implementation of the oci.Signed* Attestations method.
func attestations(digestable digestable, o *options) (oci.Signatures, error) {
h, err := digestable.Digest()
if err != nil {
return nil, err
}
return Signatures(o.TargetRepository.Tag(normalize(h, o.TagPrefix, o.AttestationSuffix)), o.OriginalOptions...)
}
// attachment is a shared implementation of the oci.Signed* Attachment method.
func attachment(digestable digestable, attName string, o *options) (oci.File, error) {
h, err := digestable.Digest()
if err != nil {
return nil, err
}
img, err := SignedImage(o.TargetRepository.Tag(normalize(h, o.TagPrefix, attName)), o.OriginalOptions...)
if err != nil {
return nil, err
}
ls, err := img.Layers()
if err != nil {
return nil, err
}
if len(ls) != 1 {
return nil, fmt.Errorf("expected exactly one layer in attachment, got %d", len(ls))
}
return &attache{
SignedImage: img,
layer: ls[0],
}, nil
}
type attache struct {
oci.SignedImage
layer v1.Layer
}
var _ oci.File = (*attache)(nil)
// FileMediaType implements oci.File
func (f *attache) FileMediaType() (types.MediaType, error) {
return f.layer.MediaType()
}
// Payload implements oci.File
func (f *attache) Payload() ([]byte, error) {
// remote layers are believed to be stored
// compressed, but we don't compress attachments
// so use "Compressed" to access the raw byte
// stream.
rc, err := f.layer.Compressed()
if err != nil {
return nil, err
}
defer rc.Close()
return io.ReadAll(rc)
}