2016-05-14 23:49:08 +02:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2016-07-09 00:44:52 +02:00
|
|
|
"regexp"
|
2016-05-14 23:49:08 +02:00
|
|
|
"testing"
|
|
|
|
|
2020-08-24 13:18:39 +02:00
|
|
|
"github.com/distribution/distribution/v3"
|
|
|
|
"github.com/distribution/distribution/v3/context"
|
|
|
|
"github.com/distribution/distribution/v3/manifest"
|
|
|
|
"github.com/distribution/distribution/v3/manifest/schema2"
|
|
|
|
"github.com/distribution/distribution/v3/registry/storage/driver/inmemory"
|
2016-05-14 23:49:08 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestVerifyManifestForeignLayer(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
inmemoryDriver := inmemory.New()
|
2016-07-09 00:44:52 +02:00
|
|
|
registry := createRegistry(t, inmemoryDriver,
|
|
|
|
ManifestURLsAllowRegexp(regexp.MustCompile("^https?://foo")),
|
|
|
|
ManifestURLsDenyRegexp(regexp.MustCompile("^https?://foo/nope")))
|
2016-05-14 23:49:08 +02:00
|
|
|
repo := makeRepository(t, registry, "test")
|
|
|
|
manifestService := makeManifestService(t, repo)
|
|
|
|
|
2016-12-15 01:17:20 +01:00
|
|
|
config, err := repo.Blobs(ctx).Put(ctx, schema2.MediaTypeImageConfig, nil)
|
2016-05-14 23:49:08 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
layer, err := repo.Blobs(ctx).Put(ctx, schema2.MediaTypeLayer, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
foreignLayer := distribution.Descriptor{
|
|
|
|
Digest: "sha256:463435349086340864309863409683460843608348608934092322395278926a",
|
|
|
|
Size: 6323,
|
|
|
|
MediaType: schema2.MediaTypeForeignLayer,
|
|
|
|
}
|
|
|
|
|
|
|
|
template := schema2.Manifest{
|
|
|
|
Versioned: manifest.Versioned{
|
|
|
|
SchemaVersion: 2,
|
|
|
|
MediaType: schema2.MediaTypeManifest,
|
|
|
|
},
|
|
|
|
Config: config,
|
|
|
|
}
|
|
|
|
|
|
|
|
type testcase struct {
|
|
|
|
BaseLayer distribution.Descriptor
|
|
|
|
URLs []string
|
|
|
|
Err error
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := []testcase{
|
|
|
|
{
|
|
|
|
foreignLayer,
|
|
|
|
nil,
|
|
|
|
errMissingURL,
|
|
|
|
},
|
|
|
|
{
|
2016-10-13 02:20:27 +02:00
|
|
|
// regular layers may have foreign urls
|
2016-05-14 23:49:08 +02:00
|
|
|
layer,
|
|
|
|
[]string{"http://foo/bar"},
|
2016-10-13 02:20:27 +02:00
|
|
|
nil,
|
2016-05-14 23:49:08 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
foreignLayer,
|
|
|
|
[]string{"file:///local/file"},
|
|
|
|
errInvalidURL,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
foreignLayer,
|
|
|
|
[]string{"http://foo/bar#baz"},
|
|
|
|
errInvalidURL,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
foreignLayer,
|
|
|
|
[]string{""},
|
|
|
|
errInvalidURL,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
foreignLayer,
|
|
|
|
[]string{"https://foo/bar", ""},
|
|
|
|
errInvalidURL,
|
|
|
|
},
|
2016-07-11 21:13:42 +02:00
|
|
|
{
|
|
|
|
foreignLayer,
|
|
|
|
[]string{"", "https://foo/bar"},
|
|
|
|
errInvalidURL,
|
|
|
|
},
|
2016-07-09 00:44:52 +02:00
|
|
|
{
|
|
|
|
foreignLayer,
|
|
|
|
[]string{"http://nope/bar"},
|
|
|
|
errInvalidURL,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
foreignLayer,
|
|
|
|
[]string{"http://foo/nope"},
|
|
|
|
errInvalidURL,
|
|
|
|
},
|
2016-05-14 23:49:08 +02:00
|
|
|
{
|
|
|
|
foreignLayer,
|
|
|
|
[]string{"http://foo/bar"},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
foreignLayer,
|
|
|
|
[]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 := schema2.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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|