From ff937e90d3924de649dbc3061d876821d3f3aeba Mon Sep 17 00:00:00 2001 From: stonezdj Date: Thu, 13 Aug 2020 11:52:03 +0800 Subject: [PATCH] Use orm.Context instead request context in background go routing Fixes #12741, Avoid use ctx info in event handler because it is background go routine Signed-off-by: stonezdj --- src/controller/event/metadata/artifact.go | 7 ++----- src/controller/event/metadata/artifact_test.go | 2 +- src/controller/proxy/controller.go | 11 +++++++++-- src/controller/proxy/local.go | 4 ++-- src/server/registry/manifest.go | 7 ++++++- 5 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/controller/event/metadata/artifact.go b/src/controller/event/metadata/artifact.go index dcc668ede..9086899b6 100644 --- a/src/controller/event/metadata/artifact.go +++ b/src/controller/event/metadata/artifact.go @@ -55,9 +55,9 @@ func (p *PushArtifactEventMetadata) Resolve(event *event.Event) error { // PullArtifactEventMetadata is the metadata from which the pull artifact event can be resolved type PullArtifactEventMetadata struct { - Ctx context.Context Artifact *artifact.Artifact Tag string + Operator string } // Resolve to the event from the metadata @@ -74,10 +74,7 @@ func (p *PullArtifactEventMetadata) Resolve(event *event.Event) error { data := &event2.PullArtifactEvent{ ArtifactEvent: ae, } - ctx, exist := security.FromContext(p.Ctx) - if exist { - data.Operator = ctx.GetUsername() - } + data.Operator = p.Operator event.Topic = event2.TopicPullArtifact event.Data = data return nil diff --git a/src/controller/event/metadata/artifact_test.go b/src/controller/event/metadata/artifact_test.go index 1bb51e2a0..dacff82fa 100644 --- a/src/controller/event/metadata/artifact_test.go +++ b/src/controller/event/metadata/artifact_test.go @@ -47,9 +47,9 @@ func (a *artifactEventTestSuite) TestResolveOfPushArtifactEventMetadata() { func (a *artifactEventTestSuite) TestResolveOfPullArtifactEventMetadata() { e := &event.Event{} metadata := &PullArtifactEventMetadata{ - Ctx: context.Background(), Artifact: &artifact.Artifact{ID: 1}, Tag: "latest", + Operator: "admin", } err := metadata.Resolve(e) a.Require().Nil(err) diff --git a/src/controller/proxy/controller.go b/src/controller/proxy/controller.go index f2276a786..8e2f9fe3c 100644 --- a/src/controller/proxy/controller.go +++ b/src/controller/proxy/controller.go @@ -16,6 +16,8 @@ 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" @@ -120,9 +122,14 @@ 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() { - a, err := c.local.GetManifest(ctx, art) + bCtx := orm.Context() + a, err := c.local.GetManifest(bCtx, art) if err != nil { log.Errorf("failed to get manifest, error %v", err) } @@ -140,7 +147,7 @@ func (c *controller) ProxyManifest(ctx context.Context, p *models.Project, art l } } if a != nil { - SendPullEvent(ctx, a, art.Tag) + SendPullEvent(a, art.Tag, operator) } }() diff --git a/src/controller/proxy/local.go b/src/controller/proxy/local.go index 55cc5ecf8..1e4621881 100644 --- a/src/controller/proxy/local.go +++ b/src/controller/proxy/local.go @@ -210,11 +210,11 @@ func (l *localHelper) CheckDependencies(ctx context.Context, repo string, man di } // SendPullEvent send a pull image event -func SendPullEvent(ctx context.Context, a *artifact.Artifact, tag string) { +func SendPullEvent(a *artifact.Artifact, tag, operator string) { e := &metadata.PullArtifactEventMetadata{ - Ctx: ctx, Artifact: &a.Artifact, Tag: tag, + Operator: operator, } event.BuildAndPublish(e) } diff --git a/src/server/registry/manifest.go b/src/server/registry/manifest.go index a8d864645..12f1b81a3 100644 --- a/src/server/registry/manifest.go +++ b/src/server/registry/manifest.go @@ -15,6 +15,7 @@ package registry import ( + "github.com/goharbor/harbor/src/common/security" "github.com/goharbor/harbor/src/controller/artifact" "github.com/goharbor/harbor/src/controller/event/metadata" "github.com/goharbor/harbor/src/controller/repository" @@ -54,9 +55,13 @@ 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{ - Ctx: req.Context(), Artifact: &art.Artifact, + Operator: operator, } // the reference is tag if _, err = digest.Parse(reference); err != nil {