Merge pull request #11751 from wy65701436/fixes-11744

fix return code on getting non exist manifest
This commit is contained in:
Daniel Jiang 2020-04-27 12:06:37 +08:00 committed by GitHub
commit ef008fd4cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 15 additions and 6 deletions

View File

@ -14,7 +14,9 @@
package lib package lib
import "context" import (
"context"
)
type contextKey string type contextKey string

View File

@ -1,7 +1,6 @@
package contenttrust package contenttrust
import ( import (
"fmt"
"net/http" "net/http"
"github.com/goharbor/harbor/src/controller/artifact" "github.com/goharbor/harbor/src/controller/artifact"
@ -36,7 +35,7 @@ func Middleware() func(http.Handler) http.Handler {
none := lib.ArtifactInfo{} none := lib.ArtifactInfo{}
af := lib.GetArtifactInfo(ctx) af := lib.GetArtifactInfo(ctx)
if af == none { if af == none {
return fmt.Errorf("artifactinfo middleware required before this middleware") return errors.New("artifactinfo middleware required before this middleware").WithCode(errors.NotFoundCode)
} }
if len(af.Digest) == 0 { if len(af.Digest) == 0 {
art, err := artifact.Ctl.GetByReference(ctx, af.Repository, af.Reference, nil) art, err := artifact.Ctl.GetByReference(ctx, af.Repository, af.Reference, nil)

View File

@ -131,6 +131,14 @@ func (suite *MiddlewareTestSuite) TestContentTrustDisabled() {
suite.Equal(rr.Code, http.StatusOK) suite.Equal(rr.Code, http.StatusOK)
} }
func (suite *MiddlewareTestSuite) TestNoneArtifact() {
req := httptest.NewRequest("GET", "/v1/library/photon/manifests/nonexist", nil)
rr := httptest.NewRecorder()
Middleware()(suite.next).ServeHTTP(rr, req)
suite.Equal(rr.Code, http.StatusNotFound)
}
func (suite *MiddlewareTestSuite) TestAuthenticatedUserPulling() { func (suite *MiddlewareTestSuite) TestAuthenticatedUserPulling() {
mock.OnAnything(suite.artifactController, "GetByReference").Return(suite.artifact, nil) mock.OnAnything(suite.artifactController, "GetByReference").Return(suite.artifact, nil)
mock.OnAnything(suite.projectController, "GetByName").Return(suite.project, nil) mock.OnAnything(suite.projectController, "GetByName").Return(suite.project, nil)

View File

@ -40,7 +40,7 @@ func handlePush(req *http.Request) error {
none := lib.ArtifactInfo{} none := lib.ArtifactInfo{}
art := lib.GetArtifactInfo(req.Context()) art := lib.GetArtifactInfo(req.Context())
if art == none { if art == none {
return errors.New("cannot get the manifest information from request context") return errors.New("cannot get the manifest information from request context").WithCode(errors.NotFoundCode)
} }
af, err := artifact.Ctl.GetByReference(req.Context(), art.Repository, art.Tag, &artifact.Option{ af, err := artifact.Ctl.GetByReference(req.Context(), art.Repository, art.Tag, &artifact.Option{

View File

@ -48,7 +48,7 @@ func Middleware() func(http.Handler) http.Handler {
none := lib.ArtifactInfo{} none := lib.ArtifactInfo{}
info := lib.GetArtifactInfo(ctx) info := lib.GetArtifactInfo(ctx)
if info == none { if info == none {
return fmt.Errorf("artifactinfo middleware required before this middleware") return errors.New("artifactinfo middleware required before this middleware").WithCode(errors.NotFoundCode)
} }
art, err := artifactController.GetByReference(ctx, info.Repository, info.Reference, nil) art, err := artifactController.GetByReference(ctx, info.Repository, info.Reference, nil)

View File

@ -127,7 +127,7 @@ func (suite *MiddlewareTestSuite) TestNoArtifactInfo() {
rr := httptest.NewRecorder() rr := httptest.NewRecorder()
Middleware()(suite.next).ServeHTTP(rr, req) Middleware()(suite.next).ServeHTTP(rr, req)
suite.Equal(rr.Code, http.StatusInternalServerError) suite.Equal(rr.Code, http.StatusNotFound)
} }
func (suite *MiddlewareTestSuite) TestGetArtifactFailed() { func (suite *MiddlewareTestSuite) TestGetArtifactFailed() {