Use controller rather than manager in the API handler and middleware

Signed-off-by: wang yan <wangyan@vmware.com>
This commit is contained in:
wang yan 2020-02-06 13:17:13 +08:00
parent 72f9e02dd9
commit 0fbbd674c2
3 changed files with 32 additions and 161 deletions

View File

@ -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)
}
}

View File

@ -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)
}
}

View File

@ -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