Merge pull request #12881 from heww/operator-from-security-username

refactor(event): change default operator to username of security
This commit is contained in:
He Weiwei 2020-08-26 15:42:36 +08:00 committed by GitHub
commit 815ab61bc6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 30 deletions

View File

@ -18,8 +18,6 @@ import (
"context"
"github.com/goharbor/harbor/src/common/security"
"github.com/goharbor/harbor/src/lib/log"
"github.com/goharbor/harbor/src/pkg/user"
)
// FromContext return the event operator from context
@ -29,13 +27,5 @@ func FromContext(ctx context.Context) string {
return ""
}
if sc.IsSolutionUser() {
user, err := user.Mgr.Get(ctx, 1)
if err == nil {
return user.Username
}
log.G(ctx).Errorf("failed to get operator for security %s, error: %v", sc.Name(), err)
}
return sc.GetUsername()
}

View File

@ -16,9 +16,6 @@ package proxy
import (
"context"
"github.com/goharbor/harbor/src/common/security"
"github.com/goharbor/harbor/src/lib/orm"
"github.com/opencontainers/go-digest"
"io"
"strings"
"sync"
@ -29,10 +26,13 @@ import (
"github.com/goharbor/harbor/src/common/models"
"github.com/goharbor/harbor/src/controller/artifact"
"github.com/goharbor/harbor/src/controller/blob"
"github.com/goharbor/harbor/src/controller/event/operator"
"github.com/goharbor/harbor/src/lib"
"github.com/goharbor/harbor/src/lib/errors"
"github.com/goharbor/harbor/src/lib/log"
"github.com/opencontainers/image-spec/specs-go/v1"
"github.com/goharbor/harbor/src/lib/orm"
"github.com/opencontainers/go-digest"
v1 "github.com/opencontainers/image-spec/specs-go/v1"
)
const (
@ -122,12 +122,9 @@ func (c *controller) ProxyManifest(ctx context.Context, p *models.Project, art l
if err != nil {
return man, err
}
operator := ""
if secCtx, ok := security.FromContext(ctx); ok {
operator = secCtx.GetUsername()
}
// Push manifest in background
go func() {
go func(operator string) {
bCtx := orm.Context()
a, err := c.local.GetManifest(bCtx, art)
if err != nil {
@ -145,11 +142,10 @@ func (c *controller) ProxyManifest(ctx context.Context, p *models.Project, art l
if err != nil {
log.Errorf("failed to get manifest, error %v", err)
}
}
if a != nil {
} else {
SendPullEvent(a, art.Tag, operator)
}
}()
}(operator.FromContext(ctx))
return man, nil
}

View File

@ -15,9 +15,12 @@
package registry
import (
"github.com/goharbor/harbor/src/common/security"
"net/http"
"strings"
"github.com/goharbor/harbor/src/controller/artifact"
"github.com/goharbor/harbor/src/controller/event/metadata"
"github.com/goharbor/harbor/src/controller/event/operator"
"github.com/goharbor/harbor/src/controller/repository"
"github.com/goharbor/harbor/src/lib"
"github.com/goharbor/harbor/src/lib/errors"
@ -27,8 +30,6 @@ import (
"github.com/goharbor/harbor/src/pkg/registry"
"github.com/goharbor/harbor/src/server/router"
"github.com/opencontainers/go-digest"
"net/http"
"strings"
)
// make sure the artifact exist before proxying the request to the backend registry
@ -55,13 +56,10 @@ func getManifest(w http.ResponseWriter, req *http.Request) {
req.UserAgent() == registry.UserAgent {
return
}
operator := ""
if secCtx, exist := security.FromContext(req.Context()); exist {
operator = secCtx.GetUsername()
}
e := &metadata.PullArtifactEventMetadata{
Artifact: &art.Artifact,
Operator: operator,
Operator: operator.FromContext(req.Context()),
}
// the reference is tag
if _, err = digest.Parse(reference); err != nil {