2016-05-14 21:49:08 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2016-07-08 22:44:52 +00:00
|
|
|
"regexp"
|
2021-04-15 06:44:04 +00:00
|
|
|
"strings"
|
2016-05-14 21:49:08 +00:00
|
|
|
"testing"
|
|
|
|
|
2020-08-24 11:18:39 +00:00
|
|
|
"github.com/distribution/distribution/v3"
|
2023-10-24 17:16:58 +00:00
|
|
|
"github.com/distribution/distribution/v3/internal/dcontext"
|
2020-08-24 11:18:39 +00:00
|
|
|
"github.com/distribution/distribution/v3/manifest"
|
|
|
|
"github.com/distribution/distribution/v3/manifest/schema2"
|
|
|
|
"github.com/distribution/distribution/v3/registry/storage/driver/inmemory"
|
2021-04-15 06:44:04 +00:00
|
|
|
"github.com/opencontainers/go-digest"
|
2016-05-14 21:49:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestVerifyManifestForeignLayer(t *testing.T) {
|
2023-10-24 17:16:58 +00:00
|
|
|
ctx := dcontext.Background()
|
2016-05-14 21:49:08 +00:00
|
|
|
inmemoryDriver := inmemory.New()
|
2016-07-08 22:44:52 +00:00
|
|
|
registry := createRegistry(t, inmemoryDriver,
|
|
|
|
ManifestURLsAllowRegexp(regexp.MustCompile("^https?://foo")),
|
2023-08-15 13:37:43 +00:00
|
|
|
ManifestURLsDenyRegexp(regexp.MustCompile("^https?://foo/nope")),
|
|
|
|
EnableValidateImageIndexImagesExist,
|
|
|
|
)
|
2016-05-14 21:49:08 +00:00
|
|
|
repo := makeRepository(t, registry, "test")
|
|
|
|
manifestService := makeManifestService(t, repo)
|
|
|
|
|
2016-12-15 00:17:20 +00:00
|
|
|
config, err := repo.Blobs(ctx).Put(ctx, schema2.MediaTypeImageConfig, nil)
|
2016-05-14 21:49:08 +00: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,
|
|
|
|
}
|
|
|
|
|
2021-04-15 06:44:04 +00:00
|
|
|
emptyLayer := distribution.Descriptor{
|
|
|
|
Digest: "",
|
|
|
|
}
|
|
|
|
|
2016-05-14 21:49:08 +00:00
|
|
|
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 00:20:27 +00:00
|
|
|
// regular layers may have foreign urls
|
2016-05-14 21:49:08 +00:00
|
|
|
layer,
|
|
|
|
[]string{"http://foo/bar"},
|
2016-10-13 00:20:27 +00:00
|
|
|
nil,
|
2016-05-14 21:49:08 +00: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 19:13:42 +00:00
|
|
|
{
|
|
|
|
foreignLayer,
|
|
|
|
[]string{"", "https://foo/bar"},
|
|
|
|
errInvalidURL,
|
|
|
|
},
|
2016-07-08 22:44:52 +00:00
|
|
|
{
|
|
|
|
foreignLayer,
|
|
|
|
[]string{"http://nope/bar"},
|
|
|
|
errInvalidURL,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
foreignLayer,
|
|
|
|
[]string{"http://foo/nope"},
|
|
|
|
errInvalidURL,
|
|
|
|
},
|
2016-05-14 21:49:08 +00:00
|
|
|
{
|
|
|
|
foreignLayer,
|
|
|
|
[]string{"http://foo/bar"},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
foreignLayer,
|
|
|
|
[]string{"https://foo/bar"},
|
|
|
|
nil,
|
|
|
|
},
|
2021-04-15 06:44:04 +00:00
|
|
|
{
|
|
|
|
emptyLayer,
|
|
|
|
[]string{"https://foo/empty"},
|
|
|
|
digest.ErrDigestInvalidFormat,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
emptyLayer,
|
|
|
|
[]string{},
|
|
|
|
digest.ErrDigestInvalidFormat,
|
|
|
|
},
|
2016-05-14 21:49:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-15 06:44:04 +00:00
|
|
|
|
|
|
|
func TestVerifyManifestBlobLayerAndConfig(t *testing.T) {
|
2023-10-24 17:16:58 +00:00
|
|
|
ctx := dcontext.Background()
|
2021-04-15 06:44:04 +00:00
|
|
|
inmemoryDriver := inmemory.New()
|
|
|
|
registry := createRegistry(t, inmemoryDriver,
|
|
|
|
ManifestURLsAllowRegexp(regexp.MustCompile("^https?://foo")),
|
2023-08-15 13:37:43 +00:00
|
|
|
ManifestURLsDenyRegexp(regexp.MustCompile("^https?://foo/nope")),
|
|
|
|
EnableValidateImageIndexImagesExist,
|
|
|
|
)
|
2021-04-15 06:44:04 +00:00
|
|
|
|
|
|
|
repo := makeRepository(t, registry, strings.ToLower(t.Name()))
|
|
|
|
manifestService := makeManifestService(t, repo)
|
|
|
|
|
|
|
|
config, err := repo.Blobs(ctx).Put(ctx, schema2.MediaTypeImageConfig, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
layer, err := repo.Blobs(ctx).Put(ctx, schema2.MediaTypeLayer, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
template := schema2.Manifest{
|
|
|
|
Versioned: manifest.Versioned{
|
|
|
|
SchemaVersion: 2,
|
|
|
|
MediaType: schema2.MediaTypeManifest,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
checkFn := func(m schema2.Manifest, rerr error) {
|
|
|
|
dm, err := schema2.FromStruct(m)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, 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]
|
|
|
|
}
|
|
|
|
} else if len(verr) == 1 {
|
|
|
|
err = verr[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != rerr {
|
|
|
|
t.Errorf("%#v: expected %v, got %v", m, rerr, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type testcase struct {
|
|
|
|
Desc distribution.Descriptor
|
|
|
|
URLs []string
|
|
|
|
Err error
|
|
|
|
}
|
|
|
|
|
|
|
|
layercases := []testcase{
|
|
|
|
// empty media type
|
|
|
|
{
|
|
|
|
distribution.Descriptor{},
|
|
|
|
[]string{"http://foo/bar"},
|
|
|
|
digest.ErrDigestInvalidFormat,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
distribution.Descriptor{},
|
|
|
|
nil,
|
|
|
|
digest.ErrDigestInvalidFormat,
|
|
|
|
},
|
|
|
|
// unknown media type, but blob is present
|
|
|
|
{
|
|
|
|
distribution.Descriptor{
|
|
|
|
Digest: layer.Digest,
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
distribution.Descriptor{
|
|
|
|
Digest: layer.Digest,
|
|
|
|
},
|
|
|
|
[]string{"http://foo/bar"},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
// gzip layer, but invalid digest
|
|
|
|
{
|
|
|
|
distribution.Descriptor{
|
|
|
|
MediaType: schema2.MediaTypeLayer,
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
digest.ErrDigestInvalidFormat,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
distribution.Descriptor{
|
|
|
|
MediaType: schema2.MediaTypeLayer,
|
|
|
|
},
|
|
|
|
[]string{"https://foo/bar"},
|
|
|
|
digest.ErrDigestInvalidFormat,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
distribution.Descriptor{
|
|
|
|
MediaType: schema2.MediaTypeLayer,
|
|
|
|
Digest: digest.Digest("invalid"),
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
digest.ErrDigestInvalidFormat,
|
|
|
|
},
|
|
|
|
// normal uploaded gzip layer
|
|
|
|
{
|
|
|
|
layer,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
layer,
|
|
|
|
[]string{"https://foo/bar"},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range layercases {
|
|
|
|
m := template
|
|
|
|
m.Config = config
|
|
|
|
|
|
|
|
l := c.Desc
|
|
|
|
l.URLs = c.URLs
|
|
|
|
|
|
|
|
m.Layers = []distribution.Descriptor{l}
|
|
|
|
|
|
|
|
checkFn(m, c.Err)
|
|
|
|
}
|
|
|
|
|
|
|
|
configcases := []testcase{
|
|
|
|
// valid config
|
|
|
|
{
|
|
|
|
config,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
// invalid digest
|
|
|
|
{
|
|
|
|
distribution.Descriptor{
|
|
|
|
MediaType: schema2.MediaTypeImageConfig,
|
|
|
|
},
|
|
|
|
[]string{"https://foo/bar"},
|
|
|
|
digest.ErrDigestInvalidFormat,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
distribution.Descriptor{
|
|
|
|
MediaType: schema2.MediaTypeImageConfig,
|
|
|
|
Digest: digest.Digest("invalid"),
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
digest.ErrDigestInvalidFormat,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range configcases {
|
|
|
|
m := template
|
|
|
|
m.Config = c.Desc
|
|
|
|
m.Config.URLs = c.URLs
|
|
|
|
|
|
|
|
checkFn(m, c.Err)
|
|
|
|
}
|
|
|
|
}
|