Unverified Commit 815ab61b authored by He Weiwei's avatar He Weiwei Committed by GitHub
Browse files

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

refactor(event): change default operator to username of security
parents c3fde4e4 793c5ca5
......@@ -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()
}
......@@ -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
}
......
......@@ -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 {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment