-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathresolver.go
212 lines (178 loc) · 6.76 KB
/
resolver.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
/*
Copyright 2020 The Tekton 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 oci
import (
"archive/tar"
"context"
"fmt"
"io"
"io/ioutil"
"strings"
"time"
"github.com/google/go-containerregistry/pkg/authn"
imgname "github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
ociremote "github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/tektoncd/pipeline/pkg/client/clientset/versioned/scheme"
"github.com/tektoncd/pipeline/pkg/remote"
"k8s.io/apimachinery/pkg/runtime"
)
const (
KindAnnotation = "dev.tekton.image.kind"
APIVersionAnnotation = "dev.tekton.image.apiVersion"
TitleAnnotation = "dev.tekton.image.name"
MaximumBundleObjects = 10
)
// Resolver implements the Resolver interface using OCI images.
type Resolver struct {
imageReference string
keychain authn.Keychain
timeout time.Duration
}
// NewResolver is a convenience function to return a new OCI resolver instance as a remote.Resolver with a short, 1m
// timeout for resolving an individual image.
func NewResolver(ref string, keychain authn.Keychain) remote.Resolver {
return &Resolver{imageReference: ref, keychain: keychain, timeout: time.Second * 60}
}
func (o *Resolver) List() ([]remote.ResolvedObject, error) {
timeoutCtx, cancel := context.WithTimeout(context.Background(), o.timeout)
defer cancel()
img, err := o.retrieveImage(timeoutCtx)
if err != nil {
return nil, err
}
manifest, err := img.Manifest()
if err != nil {
return nil, fmt.Errorf("Could not parse image manifest: %w", err)
}
if err := o.checkImageCompliance(manifest); err != nil {
return nil, err
}
contents := make([]remote.ResolvedObject, 0, len(manifest.Layers))
for _, l := range manifest.Layers {
contents = append(contents, remote.ResolvedObject{
Kind: l.Annotations[KindAnnotation],
APIVersion: l.Annotations[APIVersionAnnotation],
Name: l.Annotations[TitleAnnotation],
})
}
return contents, nil
}
func (o *Resolver) Get(kind, name string) (runtime.Object, error) {
timeoutCtx, cancel := context.WithTimeout(context.Background(), o.timeout)
defer cancel()
img, err := o.retrieveImage(timeoutCtx)
if err != nil {
return nil, err
}
manifest, err := img.Manifest()
if err != nil {
return nil, fmt.Errorf("could not parse image manifest: %w", err)
}
if err := o.checkImageCompliance(manifest); err != nil {
return nil, err
}
layers, err := img.Layers()
if err != nil {
return nil, fmt.Errorf("could not read image layers: %w", err)
}
layerMap := map[string]v1.Layer{}
for _, l := range layers {
digest, err := l.Digest()
if err != nil {
return nil, fmt.Errorf("failed to find digest for layer: %w", err)
}
layerMap[digest.String()] = l
}
for idx, l := range manifest.Layers {
lKind := l.Annotations[KindAnnotation]
lName := l.Annotations[TitleAnnotation]
if kind == lKind && name == lName {
obj, err := readTarLayer(layerMap[l.Digest.String()])
if err != nil {
// This could still be a raw layer so try to read it as that instead.
return readRawLayer(layers[idx])
}
return obj, nil
}
}
return nil, fmt.Errorf("could not find object in image with kind: %s and name: %s", kind, name)
}
// retrieveImage will fetch the image's contents and manifest.
func (o *Resolver) retrieveImage(ctx context.Context) (v1.Image, error) {
imgRef, err := imgname.ParseReference(o.imageReference)
if err != nil {
return nil, fmt.Errorf("%s is an unparseable image reference: %w", o.imageReference, err)
}
return ociremote.Image(imgRef, ociremote.WithAuthFromKeychain(o.keychain), ociremote.WithContext(ctx))
}
// checkImageCompliance will perform common checks to ensure the Tekton Bundle is compliant to our spec.
func (o *Resolver) checkImageCompliance(manifest *v1.Manifest) error {
// Check the manifest's layers to ensure there are a maximum of 10.
if len(manifest.Layers) > MaximumBundleObjects {
return fmt.Errorf("bundle %s contained more than the maximum %d allow objects", o.imageReference, MaximumBundleObjects)
}
// Ensure each layer complies to the spec.
for _, l := range manifest.Layers {
refDigest := fmt.Sprintf("%s:%s", o.imageReference, l.Digest.String())
if _, ok := l.Annotations[APIVersionAnnotation]; !ok {
return fmt.Errorf("invalid tekton bundle: %s does not contain a %s annotation", refDigest, APIVersionAnnotation)
}
if _, ok := l.Annotations[TitleAnnotation]; !ok {
return fmt.Errorf("invalid tekton bundle: %s does not contain a %s annotation", refDigest, TitleAnnotation)
}
kind, ok := l.Annotations[KindAnnotation]
if !ok {
return fmt.Errorf("invalid tekton bundle: %s does not contain a %s annotation", refDigest, KindAnnotation)
}
if strings.TrimSuffix(strings.ToLower(kind), "s") != kind {
return fmt.Errorf("invalid tekton bundle: %s annotation for %s must be lowercased and singular, found %s", KindAnnotation, refDigest, kind)
}
}
return nil
}
// Utility function to read out the contents of an image layer, assumed to be a tarball, as a parsed Tekton resource.
func readTarLayer(layer v1.Layer) (runtime.Object, error) {
rc, err := layer.Uncompressed()
if err != nil {
return nil, fmt.Errorf("Failed to read image layer: %w", err)
}
defer rc.Close()
// If the user bundled this up as a tar file then we need to untar it.
treader := tar.NewReader(rc)
header, err := treader.Next()
if err != nil {
return nil, fmt.Errorf("layer is not a tarball")
}
contents := make([]byte, header.Size)
if _, err := treader.Read(contents); err != nil && err != io.EOF {
// We only allow 1 resource per layer so this tar bundle should have one and only one file.
return nil, fmt.Errorf("failed to read tar bundle: %w", err)
}
obj, _, err := scheme.Codecs.UniversalDeserializer().Decode(contents, nil, nil)
return obj, err
}
// Utility function to read out the contents of an image layer, assumed to be raw bytes, as a parsed Tekton resource.
func readRawLayer(layer v1.Layer) (runtime.Object, error) {
rc, err := layer.Uncompressed()
if err != nil {
return nil, fmt.Errorf("failed to read image layer: %w", err)
}
defer rc.Close()
contents, err := ioutil.ReadAll(rc)
if err != nil {
return nil, fmt.Errorf("could not read contents of image layer: %w", err)
}
obj, _, err := scheme.Codecs.UniversalDeserializer().Decode(contents, nil, nil)
return obj, err
}