Debian Codegraph @

cmd/oci-image-tool/validate.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 main
16
17 import (
18 "fmt"
19 "log"
20 "os"
21 "strings"
22
23 "github.com/opencontainers/image-spec/schema"
24 "github.com/opencontainers/image-tools/image"
25 "github.com/pkg/errors"
26 "github.com/urfave/cli"
27 )
28
29 // supported validation types
30 var validateTypes = []string{
31 image.TypeImage,
32 image.TypeManifest,
33 image.TypeImageIndex,
34 image.TypeConfig,
35 }
36
37 type validateCmd struct {
38 stdout *log.Logger
39 typ string // the type to validate, can be empty string
40 refs []string
41 }
42
43 var v validateCmd
44
45 func validateAction(context *cli.Context) error {
46 if len(context.Args()) < 1 {
47 return fmt.Errorf("no files specified")
48 }
49
50 v = validateCmd{
51 typ: context.String("type"),
52 refs: context.StringSlice("ref"),
53 }
54
55 if v.typ == "" {
56 return fmt.Errorf("--type must be set")
57 }
58
59 for index, ref := range v.refs {
60 for i := index + 1; i < len(v.refs); i++ {
61 if ref == v.refs[i] {
62 fmt.Printf("WARNING: refs contains duplicate reference %q.\n", v.refs[i])
63 }
64 }
65 }
66
67 var errs []string
68 for _, arg := range context.Args() {
69 err := validatePath(arg)
70
71 if err == nil {
72 fmt.Printf("%s: OK\n", arg)
73 continue
74 }
75
76 if verr, ok := errors.Cause(err).(schema.ValidationError); ok {
77 errs = append(errs, fmt.Sprintf("%v", verr.Errs))
78 } else if serr, ok := errors.Cause(err).(*schema.SyntaxError); ok {
79 errs = append(errs, fmt.Sprintf("%s:%d:%d: %v", arg, serr.Line, serr.Col, err))
80 } else {
81 errs = append(errs, fmt.Sprintf("%s: %v", arg, err))
82 }
83
84 }
85
86 if len(errs) > 0 {
87 return fmt.Errorf("%d errors detected: \n%s", len(errs), strings.Join(errs, "\n"))
88 }
89
90 fmt.Println("Validation succeeded")
91 return nil
92 }
93
94 func validatePath(name string) error {
95 var (
96 err error
97 typ = v.typ
98 )
99
100 if v.stdout == nil {
101 v.stdout = log.New(os.Stdout, "oci-image-tool: ", 0)
102 }
103
104 if typ == image.TypeImage {
105 imageType, err := image.Autodetect(name)
106 if err != nil {
107 return errors.Wrap(err, "unable to determine image type")
108 }
109 fmt.Println("autodetected image file type is:", imageType)
110 switch imageType {
111 case image.TypeImageLayout:
112 return image.ValidateLayout(name, v.refs, v.stdout)
113 case image.TypeImageZip:
114 return image.ValidateZip(name, v.refs, v.stdout)
115 case image.TypeImage:
116 return image.ValidateFile(name, v.refs, v.stdout)
117 }
118 }
119
120 if len(v.refs) != 0 {
121 fmt.Println("WARNING: refs are only appropriate if type is image")
122 }
123 f, err := os.Open(name)
124 if err != nil {
125 return errors.Wrap(err, "unable to open file")
126 }
127 defer f.Close()
128
129 switch typ {
130 case image.TypeManifest:
131 return schema.ValidatorMediaTypeManifest.Validate(f)
132 case image.TypeImageIndex:
133 return schema.ValidatorMediaTypeImageIndex.Validate(f)
134 case image.TypeConfig:
135 return schema.ValidatorMediaTypeImageConfig.Validate(f)
136 }
137
138 return fmt.Errorf("type %q unimplemented", typ)
139 }
140
141 var validateCommand = cli.Command{
142 Name: "validate",
143 Usage: "Validate one or more image files",
144 Action: validateAction,
145 Flags: []cli.Flag{
146 cli.StringFlag{
147 Name: "type",
148 Usage: fmt.Sprintf(
149 `Type of the file to validate. One of "%s".`,
150 strings.Join(validateTypes, ","),
151 ),
152 },
153 cli.StringSliceFlag{
154 Name: "ref",
155 Usage: "A set of ref specify the search criteria for the validated reference. Format is A=B. Only support 'name', 'platform.os' and 'digest' three cases. Only applicable if type is image",
156 },
157 },
158 }