From b1793e795c273f4764da8b13180b85a5811f9ea4 Mon Sep 17 00:00:00 2001 From: Wang Yan Date: Mon, 18 May 2020 17:20:23 +0800 Subject: [PATCH] fix conformance test ErrorsCodes failure (#11961) fixes #11945 When client calls PUT with an invalid digtest, Harbor has to give a 400 instead of 500. Signed-off-by: wang yan --- src/lib/errors/const.go | 2 ++ src/server/error/error.go | 1 + src/server/middleware/quota/put_manifest.go | 3 ++- .../middleware/quota/put_manifest_test.go | 21 +++++++++++++++++++ 4 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/lib/errors/const.go b/src/lib/errors/const.go index eb5add52a..0ff4c56c3 100644 --- a/src/lib/errors/const.go +++ b/src/lib/errors/const.go @@ -23,6 +23,8 @@ const ( ViolateForeignKeyConstraintCode = "VIOLATE_FOREIGN_KEY_CONSTRAINT" // DIGESTINVALID ... DIGESTINVALID = "DIGEST_INVALID" + // MANIFESTINVALID ... + MANIFESTINVALID = "MANIFEST_INVALID" ) // NotFoundError is error for the case of object not found diff --git a/src/server/error/error.go b/src/server/error/error.go index c4e669ff1..b5a559598 100644 --- a/src/server/error/error.go +++ b/src/server/error/error.go @@ -30,6 +30,7 @@ var ( codeMap = map[string]int{ errors.BadRequestCode: http.StatusBadRequest, errors.DIGESTINVALID: http.StatusBadRequest, + errors.MANIFESTINVALID: http.StatusBadRequest, errors.UnAuthorizedCode: http.StatusUnauthorized, errors.ForbiddenCode: http.StatusForbidden, errors.DENIED: http.StatusForbidden, diff --git a/src/server/middleware/quota/put_manifest.go b/src/server/middleware/quota/put_manifest.go index bfb8bec13..174d6f69b 100644 --- a/src/server/middleware/quota/put_manifest.go +++ b/src/server/middleware/quota/put_manifest.go @@ -19,6 +19,7 @@ import ( "strconv" "github.com/goharbor/harbor/src/controller/blob" + "github.com/goharbor/harbor/src/lib/errors" "github.com/goharbor/harbor/src/lib/log" "github.com/goharbor/harbor/src/pkg/blob/models" "github.com/goharbor/harbor/src/pkg/types" @@ -42,7 +43,7 @@ func putManifestResources(r *http.Request, reference, referenceID string) (types manifest, descriptor, err := unmarshalManifest(r) if err != nil { logger.Errorf("unmarshal manifest failed, error: %v", err) - return nil, err + return nil, errors.Wrap(err, "unmarshal manifest failed").WithCode(errors.MANIFESTINVALID) } exist, err := blobController.Exist(r.Context(), descriptor.Digest.String(), blob.IsAssociatedWithProject(projectID)) diff --git a/src/server/middleware/quota/put_manifest_test.go b/src/server/middleware/quota/put_manifest_test.go index 5d6a988cd..52b575c3d 100644 --- a/src/server/middleware/quota/put_manifest_test.go +++ b/src/server/middleware/quota/put_manifest_test.go @@ -16,8 +16,10 @@ package quota import ( "context" + std_err "errors" "net/http" "net/http/httptest" + "strings" "testing" "github.com/docker/distribution/manifest/schema2" @@ -272,6 +274,25 @@ func (suite *PutManifestMiddlewareTestSuite) TestResourcesWarning() { } } +func (suite *PutManifestMiddlewareTestSuite) TestPutInvalid() { + unmarshalManifest = func(r *http.Request) (distribution.Manifest, distribution.Descriptor, error) { + return nil, distribution.Descriptor{}, std_err.New("json: cannot unmarshal string into Go value of type map") + } + + mock.OnAnything(suite.quotaController, "IsEnabled").Return(true, nil) + + next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + }) + + req := httptest.NewRequest(http.MethodPut, "/v2/library/photon/manifests/sha256:totallywrong", strings.NewReader("YmxhYmxhYmxh")) + rr := httptest.NewRecorder() + + PutManifestMiddleware()(next).ServeHTTP(rr, req) + suite.Equal(http.StatusBadRequest, rr.Code) + +} + func TestPutManifestMiddlewareTestSuite(t *testing.T) { suite.Run(t, &PutManifestMiddlewareTestSuite{}) }