Browse code

Return "invalid parameter" (4xx) errors for distribution

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2018/08/03 23:36:43
Showing 2 changed files
... ...
@@ -14,6 +14,7 @@ import (
14 14
 	"github.com/docker/docker/api/server/httputils"
15 15
 	"github.com/docker/docker/api/types"
16 16
 	registrytypes "github.com/docker/docker/api/types/registry"
17
+	"github.com/docker/docker/errdefs"
17 18
 	"github.com/opencontainers/image-spec/specs-go/v1"
18 19
 	"github.com/pkg/errors"
19 20
 )
... ...
@@ -42,9 +43,10 @@ func (s *distributionRouter) getDistributionInfo(ctx context.Context, w http.Res
42 42
 
43 43
 	image := vars["name"]
44 44
 
45
+	// TODO why is reference.ParseAnyReference() / reference.ParseNormalizedNamed() not using the reference.ErrTagInvalidFormat (and so on) errors?
45 46
 	ref, err := reference.ParseAnyReference(image)
46 47
 	if err != nil {
47
-		return err
48
+		return errdefs.InvalidParameter(err)
48 49
 	}
49 50
 	namedRef, ok := ref.(reference.Named)
50 51
 	if !ok {
... ...
@@ -52,7 +54,7 @@ func (s *distributionRouter) getDistributionInfo(ctx context.Context, w http.Res
52 52
 			// full image ID
53 53
 			return errors.Errorf("no manifest found for full image ID")
54 54
 		}
55
-		return errors.Errorf("unknown image reference format: %s", image)
55
+		return errdefs.InvalidParameter(errors.Errorf("unknown image reference format: %s", image))
56 56
 	}
57 57
 
58 58
 	distrepo, _, err := s.backend.GetRepository(ctx, namedRef, config)
... ...
@@ -66,7 +68,7 @@ func (s *distributionRouter) getDistributionInfo(ctx context.Context, w http.Res
66 66
 
67 67
 		taggedRef, ok := namedRef.(reference.NamedTagged)
68 68
 		if !ok {
69
-			return errors.Errorf("image reference not tagged: %s", image)
69
+			return errdefs.InvalidParameter(errors.Errorf("image reference not tagged: %s", image))
70 70
 		}
71 71
 
72 72
 		descriptor, err := distrepo.Tags(ctx).Get(ctx, taggedRef.Tag())
... ...
@@ -92,6 +94,16 @@ func (s *distributionRouter) getDistributionInfo(ctx context.Context, w http.Res
92 92
 	}
93 93
 	mnfst, err := mnfstsrvc.Get(ctx, distributionInspect.Descriptor.Digest)
94 94
 	if err != nil {
95
+		switch err {
96
+		case reference.ErrReferenceInvalidFormat,
97
+			reference.ErrTagInvalidFormat,
98
+			reference.ErrDigestInvalidFormat,
99
+			reference.ErrNameContainsUppercase,
100
+			reference.ErrNameEmpty,
101
+			reference.ErrNameTooLong,
102
+			reference.ErrNameNotCanonical:
103
+			return errdefs.InvalidParameter(err)
104
+		}
95 105
 		return err
96 106
 	}
97 107
 
... ...
@@ -92,7 +92,7 @@ func (i *ImageService) GetRepository(ctx context.Context, ref reference.Named, a
92 92
 	// get repository info
93 93
 	repoInfo, err := i.registryService.ResolveRepository(ref)
94 94
 	if err != nil {
95
-		return nil, false, err
95
+		return nil, false, errdefs.InvalidParameter(err)
96 96
 	}
97 97
 	// makes sure name is not empty or `scratch`
98 98
 	if err := distribution.ValidateRepoName(repoInfo.Name); err != nil {