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 <wangyan@vmware.com>
This commit is contained in:
Wang Yan 2020-05-18 17:20:23 +08:00 committed by GitHub
parent 8a944e0181
commit b1793e795c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 1 deletions

View File

@ -23,6 +23,8 @@ const (
ViolateForeignKeyConstraintCode = "VIOLATE_FOREIGN_KEY_CONSTRAINT" ViolateForeignKeyConstraintCode = "VIOLATE_FOREIGN_KEY_CONSTRAINT"
// DIGESTINVALID ... // DIGESTINVALID ...
DIGESTINVALID = "DIGEST_INVALID" DIGESTINVALID = "DIGEST_INVALID"
// MANIFESTINVALID ...
MANIFESTINVALID = "MANIFEST_INVALID"
) )
// NotFoundError is error for the case of object not found // NotFoundError is error for the case of object not found

View File

@ -30,6 +30,7 @@ var (
codeMap = map[string]int{ codeMap = map[string]int{
errors.BadRequestCode: http.StatusBadRequest, errors.BadRequestCode: http.StatusBadRequest,
errors.DIGESTINVALID: http.StatusBadRequest, errors.DIGESTINVALID: http.StatusBadRequest,
errors.MANIFESTINVALID: http.StatusBadRequest,
errors.UnAuthorizedCode: http.StatusUnauthorized, errors.UnAuthorizedCode: http.StatusUnauthorized,
errors.ForbiddenCode: http.StatusForbidden, errors.ForbiddenCode: http.StatusForbidden,
errors.DENIED: http.StatusForbidden, errors.DENIED: http.StatusForbidden,

View File

@ -19,6 +19,7 @@ import (
"strconv" "strconv"
"github.com/goharbor/harbor/src/controller/blob" "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/lib/log"
"github.com/goharbor/harbor/src/pkg/blob/models" "github.com/goharbor/harbor/src/pkg/blob/models"
"github.com/goharbor/harbor/src/pkg/types" "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) manifest, descriptor, err := unmarshalManifest(r)
if err != nil { if err != nil {
logger.Errorf("unmarshal manifest failed, error: %v", err) 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)) exist, err := blobController.Exist(r.Context(), descriptor.Digest.String(), blob.IsAssociatedWithProject(projectID))

View File

@ -16,8 +16,10 @@ package quota
import ( import (
"context" "context"
std_err "errors"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"strings"
"testing" "testing"
"github.com/docker/distribution/manifest/schema2" "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) { func TestPutManifestMiddlewareTestSuite(t *testing.T) {
suite.Run(t, &PutManifestMiddlewareTestSuite{}) suite.Run(t, &PutManifestMiddlewareTestSuite{})
} }