Skip to content

Commit 9a93cca

Browse files
authored
Merge pull request #262 from runcom/fixies
oci-image-tool: validate descriptors MediaType
2 parents d3cfb99 + 4829143 commit 9a93cca

File tree

7 files changed

+40
-35
lines changed

7 files changed

+40
-35
lines changed

image/config.go

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,12 @@ import (
2626
"strings"
2727

2828
"github.com/opencontainers/image-spec/schema"
29+
"github.com/opencontainers/image-spec/specs-go/v1"
2930
"github.com/opencontainers/runtime-spec/specs-go"
3031
"github.com/pkg/errors"
3132
)
3233

33-
type cfg struct {
34-
User string
35-
Memory int64
36-
MemorySwap int64
37-
CPUShares int64 `json:"CpuShares"`
38-
ExposedPorts map[string]struct{}
39-
Env []string
40-
Entrypoint []string
41-
Cmd []string
42-
Volumes map[string]struct{}
43-
WorkingDir string
44-
}
45-
46-
type config struct {
47-
Architecture string `json:"architecture"`
48-
OS string `json:"os"`
49-
Config cfg `json:"config"`
50-
}
34+
type config v1.Image
5135

5236
func findConfig(w walker, d *descriptor) (*config, error) {
5337
var c config

image/descriptor.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,17 @@ func findDescriptor(w walker, name string) (*descriptor, error) {
7373
}
7474
}
7575

76-
func (d *descriptor) validate(w walker) error {
76+
func (d *descriptor) validate(w walker, mts []string) error {
77+
var found bool
78+
for _, mt := range mts {
79+
if d.MediaType == mt {
80+
found = true
81+
break
82+
}
83+
}
84+
if !found {
85+
return fmt.Errorf("invalid descriptor MediaType %q", d.MediaType)
86+
}
7787
switch err := w.walk(func(path string, info os.FileInfo, r io.Reader) error {
7888
if info.IsDir() {
7989
return nil

image/image.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"os"
2121
"path/filepath"
2222

23+
"github.com/opencontainers/image-spec/specs-go/v1"
2324
"github.com/pkg/errors"
2425
)
2526

@@ -43,14 +44,19 @@ func Validate(tarFile string, refs []string, out *log.Logger) error {
4344
return validate(newTarWalker(f), refs, out)
4445
}
4546

47+
var validRefMediaTypes = []string{
48+
v1.MediaTypeImageManifest,
49+
v1.MediaTypeImageManifestList,
50+
}
51+
4652
func validate(w walker, refs []string, out *log.Logger) error {
4753
for _, r := range refs {
4854
ref, err := findDescriptor(w, r)
4955
if err != nil {
5056
return err
5157
}
5258

53-
if err = ref.validate(w); err != nil {
59+
if err = ref.validate(w, validRefMediaTypes); err != nil {
5460
return err
5561
}
5662

@@ -97,7 +103,7 @@ func unpack(w walker, dest, refName string) error {
97103
return err
98104
}
99105

100-
if err = ref.validate(w); err != nil {
106+
if err = ref.validate(w, validRefMediaTypes); err != nil {
101107
return err
102108
}
103109

@@ -139,7 +145,7 @@ func createRuntimeBundle(w walker, dest, refName, rootfs string) error {
139145
return err
140146
}
141147

142-
if err = ref.validate(w); err != nil {
148+
if err = ref.validate(w, validRefMediaTypes); err != nil {
143149
return err
144150
}
145151

image/manifest.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"time"
2929

3030
"github.com/opencontainers/image-spec/schema"
31+
"github.com/opencontainers/image-spec/specs-go/v1"
3132
"github.com/pkg/errors"
3233
)
3334

@@ -74,12 +75,12 @@ func findManifest(w walker, d *descriptor) (*manifest, error) {
7475
}
7576

7677
func (m *manifest) validate(w walker) error {
77-
if err := m.Config.validate(w); err != nil {
78+
if err := m.Config.validate(w, []string{v1.MediaTypeImageConfig}); err != nil {
7879
return errors.Wrap(err, "config validation failed")
7980
}
8081

8182
for _, d := range m.Layers {
82-
if err := d.validate(w); err != nil {
83+
if err := d.validate(w, []string{v1.MediaTypeImageLayer}); err != nil {
8384
return errors.Wrap(err, "layer validation failed")
8485
}
8586
}

schema/schema.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,19 @@
1414

1515
package schema
1616

17-
import "net/http"
17+
import (
18+
"net/http"
19+
20+
"github.com/opencontainers/image-spec/specs-go/v1"
21+
)
1822

1923
// Media types for the OCI image formats
2024
const (
21-
MediaTypeDescriptor Validator = `application/vnd.oci.descriptor.v1+json`
22-
MediaTypeManifest Validator = `application/vnd.oci.image.manifest.v1+json`
23-
MediaTypeManifestList Validator = `application/vnd.oci.image.manifest.list.v1+json`
24-
MediaTypeImageConfig Validator = `application/vnd.oci.image.config.v1+json`
25-
MediaTypeImageLayer unimplemented = `application/vnd.oci.image.layer.tar+gzip`
25+
MediaTypeDescriptor Validator = v1.MediaTypeDescriptor
26+
MediaTypeManifest Validator = v1.MediaTypeImageManifest
27+
MediaTypeManifestList Validator = v1.MediaTypeImageManifestList
28+
MediaTypeImageConfig Validator = v1.MediaTypeImageConfig
29+
MediaTypeImageLayer unimplemented = v1.MediaTypeImageLayer
2630
)
2731

2832
var (

specs-go/v1/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ type ImageConfig struct {
3535
Env []string `json:"Env"`
3636

3737
// Entrypoint defines a list of arguments to use as the command to execute when the container starts.
38-
EntryPoint []string `json:"EntryPoint"`
38+
Entrypoint []string `json:"Entrypoint"`
3939

4040
// Cmd defines the default arguments to the entrypoint of the container.
4141
Cmd []string `json:"Cmd"`

specs-go/v1/mediatype.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ const (
2424
// MediaTypeImageManifestList specifies the mediaType for an image manifest list.
2525
MediaTypeImageManifestList = "application/vnd.oci.image.manifest.list.v1+json"
2626

27-
// MediaTypeImageSerialization is the mediaType used for layers referenced by the manifest.
28-
MediaTypeImageSerialization = "application/vnd.oci.image.layer.tar+gzip"
27+
// MediaTypeImageLayer is the mediaType used for layers referenced by the manifest.
28+
MediaTypeImageLayer = "application/vnd.oci.image.layer.tar+gzip"
2929

30-
// MediaTypeImageSerializationConfig specifies the mediaType for the image configuration.
31-
MediaTypeImageSerializationConfig = "application/vnd.oci.image.config.v1+json"
30+
// MediaTypeImageConfig specifies the mediaType for the image configuration.
31+
MediaTypeImageConfig = "application/vnd.oci.image.config.v1+json"
3232
)

0 commit comments

Comments
 (0)