forked from rkt/rkt
/
validate.go
199 lines (188 loc) · 4.38 KB
/
validate.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
package main
import (
"archive/tar"
"compress/bzip2"
"compress/gzip"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"github.com/coreos/rocket/app-container/aci"
"github.com/coreos/rocket/app-container/schema"
)
const (
typeAppImage = "appimage"
typeImageLayout = "layout"
typeManifest = "manifest"
)
var (
valType string
cmdValidate = &Command{
Name: "validate",
Description: "Validate one or more AppContainer files",
Summary: "Validate that one or more images or manifests meet the AppContainer specification",
Usage: "[--type=TYPE] FILE...",
Run: runValidate,
}
types = []string{
typeAppImage,
typeImageLayout,
typeManifest,
}
)
func init() {
cmdValidate.Flags.StringVar(&valType, "type", "",
fmt.Sprintf(`Type of file to validate. If unset, actool will try to detect the type. One of "%s"`, strings.Join(types, ",")))
}
func runValidate(args []string) (exit int) {
q := globalFlags.Quiet
if len(args) < 1 {
stderr(q, "must pass one or more files")
return 1
}
for _, path := range args {
vt := valType
fi, err := os.Stat(path)
if err != nil {
stderr(q, "unable to access %s: %v", path, err)
return 1
}
var fh *os.File
if fi.IsDir() {
switch vt {
case typeImageLayout:
case "":
vt = typeImageLayout
case typeManifest, typeAppImage:
stderr(q, "%s is a directory (wrong --type?)", path)
return 1
default:
// should never happen
panic(fmt.Sprintf("unexpected type: %v", vt))
}
} else {
fh, err = os.Open(path)
if err != nil {
stderr(q, "%s: unable to open: %v", path, err)
return 1
}
}
if vt == "" {
vt, err = detectValType(fh)
if err != nil {
stderr(q, "%s: error detecting file type: %v", path, err)
return 1
}
}
switch vt {
case typeImageLayout:
err = aci.ValidateLayout(path)
if err != nil {
stderr(q, "%s: invalid image layout: %v", path, err)
exit = 1
} else {
stderr(q, "%s: valid image layout", path)
}
case typeAppImage:
fr, err := maybeDecompress(fh)
if err != nil {
stderr(q, "%s: error decompressing file: %v", path, err)
return 1
}
tr := tar.NewReader(fr)
err = aci.ValidateArchive(tr)
fh.Close()
if err != nil {
stderr(q, "%s: error validating: %v", path, err)
exit = 1
} else {
stderr(q, "%s: valid app container image", path)
}
case typeManifest:
b, err := ioutil.ReadAll(fh)
fh.Close()
if err != nil {
stderr(q, "%s: unable to read file %s", path, err)
return 1
}
k := schema.Kind{}
if err := k.UnmarshalJSON(b); err != nil {
stderr(q, "%s: error unmarshaling manifest: %v", path, err)
return 1
}
switch k.ACKind {
case "AppManifest":
m := schema.AppManifest{}
err = m.UnmarshalJSON(b)
case "ContainerRuntimeManifest":
m := schema.ContainerRuntimeManifest{}
err = m.UnmarshalJSON(b)
case "FilesetManifest":
m := schema.FilesetManifest{}
err = m.UnmarshalJSON(b)
default:
// Should not get here; schema.Kind unmarshal should fail
panic("bad ACKind")
}
if err != nil {
stderr(q, "%s: invalid %s: %v", path, k.ACKind, err)
exit = 1
} else {
stderr(q, "%s: valid %s", path, k.ACKind)
}
default:
stderr(q, "%s: unable to detect filetype (try --type)", path)
return 1
}
}
return
}
func detectValType(file *os.File) (string, error) {
typ, err := aci.DetectFileType(file)
if err != nil {
return "", err
}
if _, err := file.Seek(0, 0); err != nil {
return "", err
}
switch typ {
case aci.TypeXz, aci.TypeGzip, aci.TypeBzip2, aci.TypeTar:
return typeAppImage, nil
case aci.TypeText:
return typeManifest, nil
default:
return "", nil
}
}
func maybeDecompress(rs io.ReadSeeker) (io.Reader, error) {
// TODO(jonboulle): this is a bit redundant with detectValType
typ, err := aci.DetectFileType(rs)
if err != nil {
return nil, err
}
if _, err := rs.Seek(0, 0); err != nil {
return nil, err
}
var r io.Reader
switch typ {
case aci.TypeGzip:
r, err = gzip.NewReader(rs)
if err != nil {
return nil, fmt.Errorf("error reading gzip: %v", err)
}
case aci.TypeBzip2:
r = bzip2.NewReader(rs)
case aci.TypeXz:
r = aci.XzReader(rs)
case aci.TypeTar:
r = rs
case aci.TypeUnknown:
return nil, errors.New("unknown filetype")
default:
// should never happen
panic(fmt.Sprintf("bad type returned from DetectFileType: %v", typ))
}
return r, nil
}