2017-07-11 01:09:10 +02:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"regexp"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/distribution"
|
|
|
|
"github.com/docker/distribution/context"
|
|
|
|
"github.com/docker/distribution/manifest"
|
|
|
|
"github.com/docker/distribution/manifest/ocischema"
|
|
|
|
"github.com/docker/distribution/registry/storage/driver/inmemory"
|
2017-07-11 21:19:47 +02:00
|
|
|
"github.com/opencontainers/image-spec/specs-go/v1"
|
2017-07-11 01:09:10 +02:00
|
|
|
)
|
|
|
|
|
2017-07-11 21:19:47 +02:00
|
|
|
func TestVerifyOCIManifestNonDistributableLayer(t *testing.T) {
|
2017-07-11 01:09:10 +02:00
|
|
|
ctx := context.Background()
|
|
|
|
inmemoryDriver := inmemory.New()
|
|
|
|
registry := createRegistry(t, inmemoryDriver,
|
|
|
|
ManifestURLsAllowRegexp(regexp.MustCompile("^https?://foo")),
|
|
|
|
ManifestURLsDenyRegexp(regexp.MustCompile("^https?://foo/nope")))
|
|
|
|
repo := makeRepository(t, registry, "test")
|
|
|
|
manifestService := makeManifestService(t, repo)
|
|
|
|
|
2017-07-11 21:19:47 +02:00
|
|
|
config, err := repo.Blobs(ctx).Put(ctx, v1.MediaTypeImageConfig, nil)
|
2017-07-11 01:09:10 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-07-11 21:19:47 +02:00
|
|
|
layer, err := repo.Blobs(ctx).Put(ctx, v1.MediaTypeImageLayerGzip, nil)
|
2017-07-11 01:09:10 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-07-11 21:19:47 +02:00
|
|
|
nonDistributableLayer := distribution.Descriptor{
|
2017-07-11 01:09:10 +02:00
|
|
|
Digest: "sha256:463435349086340864309863409683460843608348608934092322395278926a",
|
|
|
|
Size: 6323,
|
2017-07-11 21:19:47 +02:00
|
|
|
MediaType: v1.MediaTypeImageLayerNonDistributableGzip,
|
2017-07-11 01:09:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template := ocischema.Manifest{
|
|
|
|
Versioned: manifest.Versioned{
|
|
|
|
SchemaVersion: 2,
|
2017-07-11 21:19:47 +02:00
|
|
|
MediaType: v1.MediaTypeImageManifest,
|
2017-07-11 01:09:10 +02:00
|
|
|
},
|
|
|
|
Config: config,
|
|
|
|
}
|
|
|
|
|
|
|
|
type testcase struct {
|
|
|
|
BaseLayer distribution.Descriptor
|
|
|
|
URLs []string
|
|
|
|
Err error
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := []testcase{
|
|
|
|
{
|
2017-07-11 21:19:47 +02:00
|
|
|
nonDistributableLayer,
|
2017-07-11 01:09:10 +02:00
|
|
|
nil,
|
|
|
|
errMissingURL,
|
|
|
|
},
|
|
|
|
{
|
2017-07-11 21:19:47 +02:00
|
|
|
// regular layers may have foreign urls (non-Distributable Layers)
|
2017-07-11 01:09:10 +02:00
|
|
|
layer,
|
|
|
|
[]string{"http://foo/bar"},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
2017-07-11 21:19:47 +02:00
|
|
|
nonDistributableLayer,
|
2017-07-11 01:09:10 +02:00
|
|
|
[]string{"file:///local/file"},
|
|
|
|
errInvalidURL,
|
|
|
|
},
|
|
|
|
{
|
2017-07-11 21:19:47 +02:00
|
|
|
nonDistributableLayer,
|
2017-07-11 01:09:10 +02:00
|
|
|
[]string{"http://foo/bar#baz"},
|
|
|
|
errInvalidURL,
|
|
|
|
},
|
|
|
|
{
|
2017-07-11 21:19:47 +02:00
|
|
|
nonDistributableLayer,
|
2017-07-11 01:09:10 +02:00
|
|
|
[]string{""},
|
|
|
|
errInvalidURL,
|
|
|
|
},
|
|
|
|
{
|
2017-07-11 21:19:47 +02:00
|
|
|
nonDistributableLayer,
|
2017-07-11 01:09:10 +02:00
|
|
|
[]string{"https://foo/bar", ""},
|
|
|
|
errInvalidURL,
|
|
|
|
},
|
|
|
|
{
|
2017-07-11 21:19:47 +02:00
|
|
|
nonDistributableLayer,
|
2017-07-11 01:09:10 +02:00
|
|
|
[]string{"", "https://foo/bar"},
|
|
|
|
errInvalidURL,
|
|
|
|
},
|
|
|
|
{
|
2017-07-11 21:19:47 +02:00
|
|
|
nonDistributableLayer,
|
2017-07-11 01:09:10 +02:00
|
|
|
[]string{"http://nope/bar"},
|
|
|
|
errInvalidURL,
|
|
|
|
},
|
|
|
|
{
|
2017-07-11 21:19:47 +02:00
|
|
|
nonDistributableLayer,
|
2017-07-11 01:09:10 +02:00
|
|
|
[]string{"http://foo/nope"},
|
|
|
|
errInvalidURL,
|
|
|
|
},
|
|
|
|
{
|
2017-07-11 21:19:47 +02:00
|
|
|
nonDistributableLayer,
|
2017-07-11 01:09:10 +02:00
|
|
|
[]string{"http://foo/bar"},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
2017-07-11 21:19:47 +02:00
|
|
|
nonDistributableLayer,
|
2017-07-11 01:09:10 +02:00
|
|
|
[]string{"https://foo/bar"},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
m := template
|
|
|
|
l := c.BaseLayer
|
|
|
|
l.URLs = c.URLs
|
|
|
|
m.Layers = []distribution.Descriptor{l}
|
|
|
|
dm, err := ocischema.FromStruct(m)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = manifestService.Put(ctx, dm)
|
|
|
|
if verr, ok := err.(distribution.ErrManifestVerification); ok {
|
|
|
|
// Extract the first error
|
|
|
|
if len(verr) == 2 {
|
|
|
|
if _, ok = verr[1].(distribution.ErrManifestBlobUnknown); ok {
|
|
|
|
err = verr[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != c.Err {
|
|
|
|
t.Errorf("%#v: expected %v, got %v", l, c.Err, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|