Skip to content

Commit

Permalink
Add manifestlist validation
Browse files Browse the repository at this point in the history
Signed-off-by: zhouhao <zhouhao@cn.fujitsu.com>
  • Loading branch information
zhouhao committed Nov 2, 2016
1 parent f24d27b commit 17713f4
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
10 changes: 10 additions & 0 deletions image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ func validate(w walker, refs []string, out *log.Logger) error {
if err := m.validate(w); err != nil {
return err
}

ml, err := findManifestList(w, d)
if ml != nil {
if err := ml.validate(w); err != nil {
return err
}
} else if err != nil {
return err
}

if out != nil {
out.Printf("reference %q: OK", ref)
}
Expand Down
17 changes: 17 additions & 0 deletions image/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,23 @@ var (
"mediaType": "application/vnd.oci.image.manifest.v1+json",
"schemaVersion": 2
}
`
manifestlistStr = `{
"schemaVersion": 2,
"mediaType": "application/vnd.oci.image.manifest.list.v1+json",
"manifests": [
{
"mediaType": "application/vnd.oci.image.manifest.v1+json",
"size": "<manifest_size>",
"digest": "<manifest_digest>",
"platform": {
"architecture": "ppc64le",
"os": "linux"
}
},
],
"annotations": null
}
`
)

Expand Down
83 changes: 83 additions & 0 deletions image/manifest_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright 2016 The Linux Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package image

import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"

"github.com/opencontainers/image-spec/schema"
"github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
)

type manifestlist struct {
Manifests []ManifestDescriptor `json:"manifests"`
}
type ManifestDescriptor struct {
Manifest descriptor
}

func findManifestList(w walker, d *descriptor) (*manifestlist, error) {
var ml manifestlist
mlpath := filepath.Join("blobs", d.algo(), d.hash())

switch err := w.walk(func(path string, info os.FileInfo, r io.Reader) error {
if info.IsDir() || filepath.Clean(path) != mlpath {
return nil
}

buf, err := ioutil.ReadAll(r)
if err != nil {
return errors.Wrapf(err, "%s: error reading manifestlist", path)
}

if err := schema.MediaTypeManifestList.Validate(bytes.NewReader(buf)); err != nil {
return errors.Wrapf(err, "%s: manifestlist validation failed", path)
}

if err := json.Unmarshal(buf, &ml); err != nil {
return err
}

if len(ml.Manifests) == 0 {
return fmt.Errorf("%s: no manifests found", path)
}

return errEOW
}); err {
case nil:
return nil, nil
case errEOW:
return &ml, nil
default:
return nil, err
}
}

func (ml *manifestlist) validate(w walker) error {
for _, d := range ml.Manifests {
if err := d.Manifest.validate(w, []string{v1.MediaTypeImageManifest}); err != nil {
return errors.Wrap(err, "Manifest validation failed")
}
}

return nil
}

0 comments on commit 17713f4

Please sign in to comment.