Skip to content

Commit

Permalink
Merge pull request #10652 from wy65701436/fix-10552
Browse files Browse the repository at this point in the history
Use controller rather than manager in the API handler and middleware
  • Loading branch information
ywk253100 committed Feb 7, 2020
2 parents 6bad9f6 + 0fbbd67 commit 8a74fcb
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 155 deletions.
61 changes: 10 additions & 51 deletions src/server/middleware/immutable/deletemf.go
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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,
},
})
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,
},
af, err := artifact.Ctl.GetByReference(req.Context(), mf.Repository, mf.Digest, &artifact.Option{
WithTag: true,
TagOption: &artifact.TagOption{WithImmutableStatus: true},
})
if err != nil {
if internal_errors.IsErr(err, internal_errors.NotFoundCode) {
return 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 matched {
_, repoName := common_util.ParseRepository(mf.Repository)
for _, tag := range af.Tags {
if tag.Immutable {
return NewErrImmutable(repoName, tag.Name)
}
}
Expand Down
72 changes: 12 additions & 60 deletions src/server/middleware/immutable/pushmf.go
Expand Up @@ -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"
)
Expand Down Expand Up @@ -45,65 +40,22 @@ func handlePush(req *http.Request) error {
return errors.New("cannot get the manifest information from request context")
}

_, 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,
},
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
}
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)
}
_, repoName := common_util.ParseRepository(mf.Repository)
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)
}
}

Expand Down
48 changes: 4 additions & 44 deletions src/server/middleware/util.go
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
},
})
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,
},
})
af, err := artifact.Ctl.GetByReference(ctx, info.Repository, info.Tag, nil)
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
Expand Down

0 comments on commit 8a74fcb

Please sign in to comment.