From 0fbbd674c2c74c9cbced1f80d9e3af009d34de54 Mon Sep 17 00:00:00 2001 From: wang yan Date: Thu, 6 Feb 2020 13:17:13 +0800 Subject: [PATCH] Use controller rather than manager in the API handler and middleware Signed-off-by: wang yan --- src/server/middleware/immutable/deletemf.go | 65 ++++------------- src/server/middleware/immutable/pushmf.go | 80 +++++---------------- src/server/middleware/util.go | 48 ++----------- 3 files changed, 32 insertions(+), 161 deletions(-) diff --git a/src/server/middleware/immutable/deletemf.go b/src/server/middleware/immutable/deletemf.go index 32d2c9e6f..dc908d0fd 100644 --- a/src/server/middleware/immutable/deletemf.go +++ b/src/server/middleware/immutable/deletemf.go @@ -3,14 +3,9 @@ package immutable import ( "errors" "fmt" + "github.com/goharbor/harbor/src/api/artifact" common_util "github.com/goharbor/harbor/src/common/utils" internal_errors "github.com/goharbor/harbor/src/internal/error" - "github.com/goharbor/harbor/src/pkg/art" - "github.com/goharbor/harbor/src/pkg/artifact" - "github.com/goharbor/harbor/src/pkg/immutabletag/match/rule" - "github.com/goharbor/harbor/src/pkg/q" - "github.com/goharbor/harbor/src/pkg/repository" - "github.com/goharbor/harbor/src/pkg/tag" "github.com/goharbor/harbor/src/server/middleware" "net/http" ) @@ -43,56 +38,20 @@ func handleDelete(req *http.Request) error { return errors.New("cannot get the manifest information from request context") } - _, repoName := common_util.ParseRepository(mf.Repository) - total, repos, err := repository.Mgr.List(req.Context(), &q.Query{ - Keywords: map[string]interface{}{ - "Name": mf.Repository, - }, + af, err := artifact.Ctl.GetByReference(req.Context(), mf.Repository, mf.Digest, &artifact.Option{ + WithTag: true, + TagOption: &artifact.TagOption{WithImmutableStatus: true}, }) if err != nil { - return err - } - if total == 0 { - return nil - } - - total, afs, err := artifact.Mgr.List(req.Context(), &q.Query{ - Keywords: map[string]interface{}{ - "ProjectID": mf.ProjectID, - "RepositoryID": repos[0].RepositoryID, - "Digest": mf.Digest, - }, - }) - if err != nil { - return err - } - if total == 0 { - return nil - } - - total, tags, err := tag.Mgr.List(req.Context(), &q.Query{ - Keywords: map[string]interface{}{ - "ArtifactID": afs[0].ID, - }, - }) - if err != nil { - return err - } - if total == 0 { - return nil - } - - for _, tag := range tags { - var matched bool - matched, err = rule.NewRuleMatcher().Match(mf.ProjectID, art.Candidate{ - Repository: repoName, - Tag: tag.Name, - NamespaceID: mf.ProjectID, - }) - if err != nil { - return err + if internal_errors.IsErr(err, internal_errors.NotFoundCode) { + return nil } - if matched { + return err + } + + _, repoName := common_util.ParseRepository(mf.Repository) + for _, tag := range af.Tags { + if tag.Immutable { return NewErrImmutable(repoName, tag.Name) } } diff --git a/src/server/middleware/immutable/pushmf.go b/src/server/middleware/immutable/pushmf.go index 3e3f57ba5..6964fa4ae 100644 --- a/src/server/middleware/immutable/pushmf.go +++ b/src/server/middleware/immutable/pushmf.go @@ -3,14 +3,9 @@ package immutable import ( "errors" "fmt" + "github.com/goharbor/harbor/src/api/artifact" common_util "github.com/goharbor/harbor/src/common/utils" internal_errors "github.com/goharbor/harbor/src/internal/error" - "github.com/goharbor/harbor/src/pkg/art" - "github.com/goharbor/harbor/src/pkg/artifact" - "github.com/goharbor/harbor/src/pkg/immutabletag/match/rule" - "github.com/goharbor/harbor/src/pkg/q" - "github.com/goharbor/harbor/src/pkg/repository" - "github.com/goharbor/harbor/src/pkg/tag" "github.com/goharbor/harbor/src/server/middleware" "net/http" ) @@ -45,65 +40,22 @@ func handlePush(req *http.Request) error { return errors.New("cannot get the manifest information from request context") } + af, err := artifact.Ctl.GetByReference(req.Context(), mf.Repository, mf.Tag, &artifact.Option{ + WithTag: true, + TagOption: &artifact.TagOption{WithImmutableStatus: true}, + }) + if err != nil { + if internal_errors.IsErr(err, internal_errors.NotFoundCode) { + return nil + } + return err + } + _, repoName := common_util.ParseRepository(mf.Repository) - var matched bool - matched, err := rule.NewRuleMatcher().Match(mf.ProjectID, art.Candidate{ - Repository: repoName, - Tag: mf.Tag, - NamespaceID: mf.ProjectID, - }) - if err != nil { - return err - } - if !matched { - return nil - } - - // match repository ... - total, repos, err := repository.Mgr.List(req.Context(), &q.Query{ - Keywords: map[string]interface{}{ - "Name": mf.Repository, - }, - }) - if err != nil { - return err - } - if total == 0 { - return nil - } - - // match artifacts ... - total, afs, err := artifact.Mgr.List(req.Context(), &q.Query{ - Keywords: map[string]interface{}{ - "ProjectID": mf.ProjectID, - "RepositoryID": repos[0].RepositoryID, - }, - }) - if err != nil { - return err - } - if total == 0 { - return nil - } - - // match tags ... - for _, af := range afs { - total, tags, err := tag.Mgr.List(req.Context(), &q.Query{ - Keywords: map[string]interface{}{ - "ArtifactID": af.ID, - }, - }) - if err != nil { - return err - } - if total == 0 { - continue - } - for _, tag := range tags { - // push a existing immutable tag, reject the request - if tag.Name == mf.Tag { - return NewErrImmutable(repoName, mf.Tag) - } + for _, tag := range af.Tags { + // push a existing immutable tag, reject th e request + if tag.Name == mf.Tag && tag.Immutable { + return NewErrImmutable(repoName, mf.Tag) } } diff --git a/src/server/middleware/util.go b/src/server/middleware/util.go index 76e8d9a3a..30930b876 100644 --- a/src/server/middleware/util.go +++ b/src/server/middleware/util.go @@ -4,16 +4,13 @@ import ( "context" "fmt" "github.com/docker/distribution/reference" + "github.com/goharbor/harbor/src/api/artifact" "github.com/goharbor/harbor/src/common/models" "github.com/goharbor/harbor/src/common/utils/log" "github.com/goharbor/harbor/src/core/config" "github.com/goharbor/harbor/src/core/promgr" - "github.com/goharbor/harbor/src/pkg/artifact" - "github.com/goharbor/harbor/src/pkg/q" - "github.com/goharbor/harbor/src/pkg/repository" "github.com/goharbor/harbor/src/pkg/scan/vuln" "github.com/goharbor/harbor/src/pkg/scan/whitelist" - "github.com/goharbor/harbor/src/pkg/tag" "github.com/opencontainers/go-digest" "github.com/pkg/errors" "net/http" @@ -70,50 +67,13 @@ type ManifestInfo struct { // ManifestExists ... func (info *ManifestInfo) ManifestExists(ctx context.Context) (bool, error) { info.manifestExistOnce.Do(func() { - - // ToDo: use the artifact controller method - total, repos, err := repository.Mgr.List(ctx, &q.Query{ - Keywords: map[string]interface{}{ - "Name": info.Repository, - }, - }) + af, err := artifact.Ctl.GetByReference(ctx, info.Repository, info.Tag, nil) if err != nil { info.manifestExistErr = err return } - if total == 0 { - return - } - - total, tags, err := tag.Mgr.List(ctx, &q.Query{ - Keywords: map[string]interface{}{ - "Name": info.Tag, - "RepositoryID": repos[0].RepositoryID, - }, - }) - if err != nil { - info.manifestExistErr = err - return - } - if total == 0 { - return - } - - total, afs, err := artifact.Mgr.List(ctx, &q.Query{ - Keywords: map[string]interface{}{ - "ID": tags[0].ArtifactID, - }, - }) - if err != nil { - info.manifestExistErr = err - return - } - if total == 0 { - return - } - - info.Digest = afs[0].Digest - info.manifestExist = total > 0 + info.manifestExist = true + info.Digest = af.Digest }) return info.manifestExist, info.manifestExistErr