Debian Codegraph @

image/image.go

raw symbols SCIP
1 // Copyright 2016 The Linux Foundation
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package image
16
17 import (
18 "encoding/json"
19 "fmt"
20 "io"
21 "log"
22 "os"
23 "path/filepath"
24 "strings"
25
26 "github.com/opencontainers/image-spec/specs-go/v1"
27 "github.com/pkg/errors"
28 )
29
30 // ValidateLayout walks through the given file tree and validates the manifest
31 // pointed to by the given refs or returns an error if the validation failed.
32 func ValidateLayout(src string, refs []string, out *log.Logger) error {
33 return validate(newPathWalker(src), refs, out)
34 }
35
36 // ValidateZip walks through the given file tree and validates the manifest
37 // pointed to by the given refs or returns an error if the validation failed.
38 func ValidateZip(src string, refs []string, out *log.Logger) error {
39 return validate(newZipWalker(src), refs, out)
40 }
41
42 // ValidateFile opens the tar file given by the filename, then calls ValidateReader
43 func ValidateFile(tarFile string, refs []string, out *log.Logger) error {
44 f, err := os.Open(tarFile)
45 if err != nil {
46 return errors.Wrap(err, "unable to open file")
47 }
48 defer f.Close()
49
50 return Validate(f, refs, out)
51 }
52
53 // Validate walks through a tar stream and validates the manifest.
54 // * Check that all refs point to extant blobs
55 // * Checks that all referred blobs are valid
56 // * Checks that mime-types are correct
57 // returns error on validation failure
58 func Validate(r io.ReadSeeker, refs []string, out *log.Logger) error {
59 return validate(newTarWalker(r), refs, out)
60 }
61
62 var validRefMediaTypes = []string{
63 v1.MediaTypeImageManifest,
64 v1.MediaTypeImageIndex,
65 }
66
67 func validate(w walker, refs []string, out *log.Logger) error {
68 var descs []v1.Descriptor
69 var err error
70
71 if err = layoutValidate(w); err != nil {
72 return err
73 }
74
75 if len(refs) == 0 {
76 out.Print("No ref specified, verify all refs")
77 descs, err = listReferences(w)
78 if err != nil {
79 return err
80 }
81 if len(descs) == 0 {
82 // TODO(runcom): ugly, we'll need a better way and library
83 // to express log levels.
84 // see https://github.com/opencontainers/image-spec/issues/288
85 out.Print("WARNING: no descriptors found")
86 return nil
87 }
88 } else {
89 descs, err = findDescriptor(w, refs)
90 if err != nil {
91 return err
92 }
93 }
94
95 for _, desc := range descs {
96 d := &desc
97 if err = validateDescriptor(d, w, validRefMediaTypes); err != nil {
98 return err
99 }
100
101 if d.MediaType == validRefMediaTypes[0] {
102 m, err := findManifest(w, d)
103 if err != nil {
104 return err
105 }
106
107 if err := validateManifest(m, w); err != nil {
108 return err
109 }
110 }
111
112 if d.MediaType == validRefMediaTypes[1] {
113 index, err := findIndex(w, d)
114 if err != nil {
115 return err
116 }
117
118 if err := validateIndex(index, w); err != nil {
119 return err
120 }
121
122 if len(index.Manifests) == 0 {
123 fmt.Println("warning: no manifests found")
124 return nil
125 }
126
127 for _, manifest := range index.Manifests {
128 m, err := findManifest(w, &manifest)
129 if err != nil {
130 return err
131 }
132
133 if err := validateManifest(m, w); err != nil {
134 return err
135 }
136 }
137 }
138 }
139
140 if out != nil && len(refs) > 0 {
141 out.Printf("reference %v: OK", refs)
142 }
143
144 return nil
145 }
146
147 // UnpackLayout walks through the file tree given by src and, using the layers
148 // specified in the manifest pointed to by the given ref, unpacks all layers in
149 // the given destination directory or returns an error if the unpacking failed.
150 func UnpackLayout(src, dest, platform string, refs []string) error {
151 return unpack(newPathWalker(src), dest, platform, refs)
152 }
153
154 // UnpackZip opens and walks through the zip file given by src and, using the layers
155 // specified in the manifest pointed to by the given ref, unpacks all layers in
156 // the given destination directory or returns an error if the unpacking failed.
157 func UnpackZip(src, dest, platform string, refs []string) error {
158 return unpack(newZipWalker(src), dest, platform, refs)
159 }
160
161 // UnpackFile opens the file pointed by tarFileName and calls Unpack on it.
162 func UnpackFile(tarFileName, dest, platform string, refs []string) error {
163 f, err := os.Open(tarFileName)
164 if err != nil {
165 return errors.Wrap(err, "unable to open file")
166 }
167 defer f.Close()
168
169 return Unpack(f, dest, platform, refs)
170 }
171
172 // Unpack walks through the tar stream and, using the layers specified in
173 // the manifest pointed to by the given ref, unpacks all layers in the given
174 // destination directory or returns an error if the unpacking failed.
175 // The destination will be created if it does not exist.
176 func Unpack(r io.ReadSeeker, dest, platform string, refs []string) error {
177 return unpack(newTarWalker(r), dest, platform, refs)
178 }
179
180 func unpack(w walker, dest, platform string, refs []string) error {
181 if err := layoutValidate(w); err != nil {
182 return err
183 }
184
185 descs, err := findDescriptor(w, refs)
186 if err != nil {
187 return err
188 }
189
190 ref := &descs[0]
191 if err = validateDescriptor(ref, w, validRefMediaTypes); err != nil {
192 return err
193 }
194
195 if ref.MediaType == validRefMediaTypes[0] {
196 m, err := findManifest(w, ref)
197 if err != nil {
198 return err
199 }
200
201 if err := validateManifest(m, w); err != nil {
202 return err
203 }
204
205 return unpackManifest(m, w, dest)
206 }
207
208 if ref.MediaType == validRefMediaTypes[1] {
209 index, err := findIndex(w, ref)
210 if err != nil {
211 return err
212 }
213
214 if err = validateIndex(index, w); err != nil {
215 return err
216 }
217
218 manifests, err := filterManifest(w, index.Manifests, platform)
219 if err != nil {
220 return err
221 }
222
223 for _, m := range manifests {
224 return unpackManifest(m, w, dest)
225 }
226 }
227
228 return nil
229 }
230
231 // CreateRuntimeBundleLayout walks through the file tree given by src and
232 // creates an OCI runtime bundle in the given destination dest
233 // or returns an error if the unpacking failed.
234 func CreateRuntimeBundleLayout(src, dest, root, platform string, refs []string) error {
235 return createRuntimeBundle(newPathWalker(src), dest, root, platform, refs)
236 }
237
238 // CreateRuntimeBundleZip opens and walks through the zip file given by src
239 // and creates an OCI runtime bundle in the given destination dest
240 // or returns an error if the unpacking failed.
241 func CreateRuntimeBundleZip(src, dest, root, platform string, refs []string) error {
242 return createRuntimeBundle(newZipWalker(src), dest, root, platform, refs)
243 }
244
245 // CreateRuntimeBundleFile opens the file pointed by tarFile and calls
246 // CreateRuntimeBundle.
247 func CreateRuntimeBundleFile(tarFile, dest, root, platform string, refs []string) error {
248 f, err := os.Open(tarFile)
249 if err != nil {
250 return errors.Wrap(err, "unable to open file")
251 }
252 defer f.Close()
253
254 return createRuntimeBundle(newTarWalker(f), dest, root, platform, refs)
255 }
256
257 // CreateRuntimeBundle walks through the given tar stream and
258 // creates an OCI runtime bundle in the given destination dest
259 // or returns an error if the unpacking failed.
260 func CreateRuntimeBundle(r io.ReadSeeker, dest, root, platform string, refs []string) error {
261 return createRuntimeBundle(newTarWalker(r), dest, root, platform, refs)
262 }
263
264 func createRuntimeBundle(w walker, dest, rootfs, platform string, refs []string) error {
265 if err := layoutValidate(w); err != nil {
266 return err
267 }
268
269 descs, err := findDescriptor(w, refs)
270 if err != nil {
271 return err
272 }
273
274 ref := &descs[0]
275 if err = validateDescriptor(ref, w, validRefMediaTypes); err != nil {
276 return err
277 }
278
279 if ref.MediaType == validRefMediaTypes[0] {
280 m, err := findManifest(w, ref)
281 if err != nil {
282 return err
283 }
284
285 if err := validateManifest(m, w); err != nil {
286 return err
287 }
288
289 return createBundle(w, m, dest, rootfs)
290 }
291
292 if ref.MediaType == validRefMediaTypes[1] {
293 index, err := findIndex(w, ref)
294 if err != nil {
295 return err
296 }
297
298 if err = validateIndex(index, w); err != nil {
299 return err
300 }
301
302 manifests, err := filterManifest(w, index.Manifests, platform)
303 if err != nil {
304 return err
305 }
306
307 for _, m := range manifests {
308 return createBundle(w, m, dest, rootfs)
309 }
310 }
311
312 return nil
313 }
314
315 func createBundle(w walker, m *v1.Manifest, dest, rootfs string) (retErr error) {
316 c, err := findConfig(w, &m.Config)
317 if err != nil {
318 return err
319 }
320
321 if _, err = os.Stat(dest); err != nil {
322 if os.IsNotExist(err) {
323 if err2 := os.MkdirAll(dest, 0755); err2 != nil {
324 return err2
325 }
326 defer func() {
327 if retErr != nil {
328 if err3 := os.RemoveAll(dest); err3 != nil {
329 fmt.Printf("Failed to clean up %q: %s\n", dest, err3.Error())
330 }
331 }
332 }()
333 } else {
334 return err
335 }
336 }
337
338 if err = unpackManifest(m, w, filepath.Join(dest, rootfs)); err != nil {
339 return err
340 }
341
342 spec, err := runtimeSpec(c, rootfs)
343 if err != nil {
344 return err
345 }
346
347 f, err := os.Create(filepath.Join(dest, "config.json"))
348 if err != nil {
349 return err
350 }
351 defer f.Close()
352
353 return json.NewEncoder(f).Encode(spec)
354 }
355
356 // filertManifest returns a filtered list of manifests
357 func filterManifest(w walker, Manifests []v1.Descriptor, platform string) ([]*v1.Manifest, error) {
358 var manifests []*v1.Manifest
359
360 argsParts := strings.Split(platform, ":")
361 if len(argsParts) != 2 {
362 return manifests, fmt.Errorf("platform must have os and arch when reftype is index")
363 }
364
365 if len(Manifests) == 0 {
366 fmt.Println("warning: no manifests found")
367 return manifests, nil
368 }
369
370 for _, manifest := range Manifests {
371 m, err := findManifest(w, &manifest)
372 if err != nil {
373 return manifests, err
374 }
375
376 if err := validateManifest(m, w); err != nil {
377 return manifests, err
378 }
379 if strings.EqualFold(manifest.Platform.OS, argsParts[0]) && strings.EqualFold(manifest.Platform.Architecture, argsParts[1]) {
380 manifests = append(manifests, m)
381 }
382 }
383
384 if len(manifests) == 0 {
385 return manifests, fmt.Errorf("There is no matching manifest")
386 }
387
388 return manifests, nil
389 }