Skip to content

Commit 647e355

Browse files
committed
bundle validate update to 0.3.0
Signed-off-by: liangchenye <[email protected]>
1 parent a8db914 commit 647e355

1 file changed

Lines changed: 39 additions & 55 deletions

File tree

bvalidate.go

Lines changed: 39 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -64,41 +64,27 @@ var bundleValidateCommand = cli.Command{
6464
if err = json.NewDecoder(sf).Decode(&spec); err != nil {
6565
logrus.Fatal(err)
6666
} else {
67-
if spec.Platform.OS != "linux" {
68-
logrus.Fatalf("Operation system '%s' of the bundle is not supported yet.", spec.Platform.OS)
67+
if spec.Spec.Platform.OS != "linux" {
68+
logrus.Fatalf("Operation system '%s' of the bundle is not supported yet.", spec.Spec.Platform.OS)
6969
}
7070
}
7171

72-
rf, err := os.Open(path.Join(inputPath, "runtime.json"))
73-
if err != nil {
74-
logrus.Fatal(err)
75-
}
76-
defer rf.Close()
77-
78-
var runtime specs.LinuxRuntimeSpec
79-
if err = json.NewDecoder(rf).Decode(&runtime); err != nil {
80-
logrus.Fatal(err)
81-
}
82-
83-
rootfsPath := path.Join(inputPath, spec.Root.Path)
72+
rootfsPath := path.Join(inputPath, spec.Spec.Root.Path)
8473
if fi, err := os.Stat(rootfsPath); err != nil {
8574
logrus.Fatalf("Cannot find the rootfs: %v", rootfsPath)
8675
} else if !fi.IsDir() {
87-
logrus.Fatalf("Rootfs: %v is not a directory.", spec.Root.Path)
76+
logrus.Fatalf("Rootfs: %v is not a directory.", spec.Spec.Root.Path)
8877
}
89-
bundleValidate(spec, runtime, rootfsPath)
78+
bundleValidate(spec, rootfsPath)
9079
logrus.Infof("Bundle validation succeeded.")
9180
},
9281
}
9382

94-
func bundleValidate(spec specs.LinuxSpec, runtime specs.LinuxRuntimeSpec, rootfs string) {
95-
//Open after 0.3.0
96-
//CheckMandatoryField(spec)
97-
//CheckMandatoryField(runtime)
83+
func bundleValidate(spec specs.LinuxSpec, rootfs string) {
84+
CheckMandatoryField(spec)
9885
CheckSemVer(spec.Version)
99-
CheckMountPoints(spec.Mounts, runtime.Mounts)
100-
CheckLinuxSpec(spec, runtime)
101-
CheckLinuxRuntime(runtime.Linux, rootfs)
86+
CheckMounts(spec.Mounts, rootfs)
87+
CheckLinux(spec.Linux, rootfs)
10288
}
10389

10490
func CheckSemVer(version string) {
@@ -108,60 +94,62 @@ func CheckSemVer(version string) {
10894
}
10995
}
11096

111-
func CheckMountPoints(mps []specs.MountPoint, rmps map[string]specs.Mount) {
112-
for index := 0; index < len(mps); index++ {
113-
if _, ok := rmps[mps[index].Name]; !ok {
114-
logrus.Fatalf("%s in config/mount does not exist in runtime/mount", mps[index].Name)
97+
func CheckMounts(mounts []specs.Mount, rootfs string) {
98+
for _, mount := range mounts {
99+
rootfsPath := path.Join(rootfs, mount.Destination)
100+
if fi, err := os.Stat(rootfsPath); err != nil {
101+
logrus.Fatalf("Cannot find the mount point: %v", rootfsPath)
102+
} else if !fi.IsDir() {
103+
logrus.Fatalf("Mount point: %v is not a directory.", rootfsPath)
115104
}
116105
}
117106
}
118107

119108
//Linux only
120-
func CheckLinuxSpec(spec specs.LinuxSpec, runtime specs.LinuxRuntimeSpec) {
121-
for index := 0; index < len(spec.Linux.Capabilities); index++ {
122-
capability := spec.Linux.Capabilities[index]
109+
func CheckLinux(spec specs.Linux, rootfs string) {
110+
for index := 0; index < len(spec.Capabilities); index++ {
111+
capability := spec.Capabilities[index]
123112
if !capValid(capability) {
124-
logrus.Fatalf("%s is not valid, man capabilities(7)", spec.Linux.Capabilities[index])
113+
logrus.Fatalf("%s is not valid, man capabilities(7)", spec.Capabilities[index])
125114
}
126115
}
127-
}
128116

129-
//Linux only
130-
func CheckLinuxRuntime(runtime specs.LinuxRuntime, rootfs string) {
131-
if len(runtime.UIDMappings) > 5 {
117+
if len(spec.UIDMappings) > 5 {
132118
logrus.Fatalf("Only 5 UID mappings are allowed (linux kernel restriction).")
133119
}
134-
if len(runtime.GIDMappings) > 5 {
120+
if len(spec.GIDMappings) > 5 {
135121
logrus.Fatalf("Only 5 GID mappings are allowed (linux kernel restriction).")
136122
}
137123

138-
for index := 0; index < len(runtime.Rlimits); index++ {
139-
if !rlimitValid(runtime.Rlimits[index].Type) {
140-
logrus.Fatalf("Rlimit %s is invalid.", runtime.Rlimits[index])
124+
for index := 0; index < len(spec.Rlimits); index++ {
125+
if !rlimitValid(spec.Rlimits[index].Type) {
126+
logrus.Fatalf("Rlimit %s is invalid.", spec.Rlimits[index])
141127
}
142128
}
143129

144-
for index := 0; index < len(runtime.Namespaces); index++ {
145-
if !namespaceValid(runtime.Namespaces[index]) {
146-
logrus.Fatalf("Namespace %s is invalid.", runtime.Namespaces[index])
130+
for index := 0; index < len(spec.Namespaces); index++ {
131+
if !namespaceValid(spec.Namespaces[index]) {
132+
logrus.Fatalf("Namespace %s is invalid.", spec.Namespaces[index])
147133
}
148134
}
149135

150-
for index := 0; index < len(runtime.Devices); index++ {
151-
if !deviceValid(runtime.Devices[index]) {
152-
logrus.Fatalf("Device %s is invalid.", runtime.Devices[index].Path)
136+
for index := 0; index < len(spec.Devices); index++ {
137+
if !deviceValid(spec.Devices[index]) {
138+
logrus.Fatalf("Device %s is invalid.", spec.Devices[index].Path)
153139
}
154140
}
155141

156-
if len(runtime.ApparmorProfile) > 0 {
157-
profilePath := path.Join(rootfs, "/etc/apparmor.d", runtime.ApparmorProfile)
142+
if len(spec.ApparmorProfile) > 0 {
143+
profilePath := path.Join(rootfs, "/etc/apparmor.d", spec.ApparmorProfile)
158144
_, err := os.Stat(profilePath)
159145
if err != nil {
160146
logrus.Fatal(err)
161147
}
162148
}
163149

164-
switch runtime.RootfsPropagation {
150+
CheckSeccomp(spec.Seccomp)
151+
152+
switch spec.RootfsPropagation {
165153
case "":
166154
case "private":
167155
case "rprivate":
@@ -172,19 +160,15 @@ func CheckLinuxRuntime(runtime specs.LinuxRuntime, rootfs string) {
172160
default:
173161
logrus.Fatalf("rootfs-propagation must be empty or one of private|rprivate|slave|rslave|shared|rshared")
174162
}
175-
176-
CheckSeccomp(runtime.Seccomp)
177163
}
178164

179165
func CheckSeccomp(s specs.Seccomp) {
180166
if !seccompActionValid(s.DefaultAction) {
181167
logrus.Fatalf("Seccomp.DefaultAction is invalid.")
182168
}
183169
for index := 0; index < len(s.Syscalls); index++ {
184-
if s.Syscalls[index] != nil {
185-
if !syscallValid(*(s.Syscalls[index])) {
186-
logrus.Fatalf("Syscall action is invalid.")
187-
}
170+
if !syscallValid(s.Syscalls[index]) {
171+
logrus.Fatalf("Syscall action is invalid.")
188172
}
189173
}
190174
for index := 0; index < len(s.Architectures); index++ {
@@ -278,7 +262,7 @@ func syscallValid(s specs.Syscall) bool {
278262
return false
279263
}
280264
for index := 0; index < len(s.Args); index++ {
281-
arg := *(s.Args[index])
265+
arg := s.Args[index]
282266
switch arg.Op {
283267
case specs.OpNotEqual:
284268
case specs.OpLessEqual:

0 commit comments

Comments
 (0)