From b72b5cec13a58166482e24ab07f2c8ab365e4f70 Mon Sep 17 00:00:00 2001 From: Wang Yan Date: Tue, 3 Dec 2024 16:44:42 +0800 Subject: [PATCH] [cherry-pick] fix robot deletion event (#21234) (#21272) fix robot deletion event (#21234) * fix robot deletion event * resolve comments --------- Signed-off-by: wang yan --- src/controller/event/metadata/robot.go | 15 ++++++++++----- src/controller/robot/controller.go | 12 ++++++++---- src/controller/robot/model.go | 1 + src/controller/scan/callback.go | 3 ++- src/controller/scan/callback_test.go | 4 ++-- src/testing/controller/robot/controller.go | 17 ++++++++++++----- 6 files changed, 35 insertions(+), 17 deletions(-) diff --git a/src/controller/event/metadata/robot.go b/src/controller/event/metadata/robot.go index a4b325b34d9..ac62d8ec4cd 100644 --- a/src/controller/event/metadata/robot.go +++ b/src/controller/event/metadata/robot.go @@ -51,8 +51,9 @@ func (c *CreateRobotEventMetadata) Resolve(event *event.Event) error { // DeleteRobotEventMetadata is the metadata from which the delete robot event can be resolved type DeleteRobotEventMetadata struct { - Ctx context.Context - Robot *model.Robot + Ctx context.Context + Robot *model.Robot + Operator string } // Resolve to the event from the metadata @@ -62,9 +63,13 @@ func (d *DeleteRobotEventMetadata) Resolve(event *event.Event) error { Robot: d.Robot, OccurAt: time.Now(), } - cx, exist := security.FromContext(d.Ctx) - if exist { - data.Operator = cx.GetUsername() + if d.Operator != "" { + data.Operator = d.Operator + } else { + cx, exist := security.FromContext(d.Ctx) + if exist { + data.Operator = cx.GetUsername() + } } data.Robot.Name = fmt.Sprintf("%s%s", config.RobotPrefix(d.Ctx), data.Robot.Name) event.Topic = event2.TopicDeleteRobot diff --git a/src/controller/robot/controller.go b/src/controller/robot/controller.go index 0132eba5cd4..6f3b40d8246 100644 --- a/src/controller/robot/controller.go +++ b/src/controller/robot/controller.go @@ -56,7 +56,7 @@ type Controller interface { Create(ctx context.Context, r *Robot) (int64, string, error) // Delete ... - Delete(ctx context.Context, id int64) error + Delete(ctx context.Context, id int64, option ...*Option) error // Update ... Update(ctx context.Context, r *Robot, option *Option) error @@ -149,7 +149,7 @@ func (d *controller) Create(ctx context.Context, r *Robot) (int64, string, error } // Delete ... -func (d *controller) Delete(ctx context.Context, id int64) error { +func (d *controller) Delete(ctx context.Context, id int64, option ...*Option) error { rDelete, err := d.robotMgr.Get(ctx, id) if err != nil { return err @@ -161,10 +161,14 @@ func (d *controller) Delete(ctx context.Context, id int64) error { return err } // fire event - notification.AddEvent(ctx, &metadata.DeleteRobotEventMetadata{ + deleteMetadata := &metadata.DeleteRobotEventMetadata{ Ctx: ctx, Robot: rDelete, - }) + } + if len(option) != 0 && option[0].Operator != "" { + deleteMetadata.Operator = option[0].Operator + } + notification.AddEvent(ctx, deleteMetadata) return nil } diff --git a/src/controller/robot/model.go b/src/controller/robot/model.go index 375483cdd84..32cf15c1136 100644 --- a/src/controller/robot/model.go +++ b/src/controller/robot/model.go @@ -84,4 +84,5 @@ func (p *Permission) IsCoverAll() bool { // Option ... type Option struct { WithPermission bool + Operator string } diff --git a/src/controller/scan/callback.go b/src/controller/scan/callback.go index 5229ca0b184..2d6b92daaa5 100644 --- a/src/controller/scan/callback.go +++ b/src/controller/scan/callback.go @@ -18,6 +18,7 @@ import ( "context" "encoding/json" + "github.com/goharbor/harbor/src/common/secret" "github.com/goharbor/harbor/src/controller/artifact" "github.com/goharbor/harbor/src/controller/event/metadata" "github.com/goharbor/harbor/src/controller/event/operator" @@ -92,7 +93,7 @@ func scanTaskStatusChange(ctx context.Context, taskID int64, status string) (err robotID := getRobotID(t.ExtraAttrs) if robotID > 0 { - if err := robotCtl.Delete(ctx, robotID); err != nil { + if err := robotCtl.Delete(ctx, robotID, &robot.Option{Operator: secret.JobserviceUser}); err != nil { // Should not block the main flow, just logged logger.WithFields(log.Fields{"robot_id": robotID, "error": err}).Error("delete robot account failed") } else { diff --git a/src/controller/scan/callback_test.go b/src/controller/scan/callback_test.go index 165524e458f..3744468e622 100644 --- a/src/controller/scan/callback_test.go +++ b/src/controller/scan/callback_test.go @@ -96,7 +96,7 @@ func (suite *CallbackTestSuite) TestScanTaskStatusChange() { }, nil, ).Once() - suite.robotCtl.On("Delete", mock.Anything, int64(1)).Return(nil).Once() + suite.robotCtl.On("Delete", mock.Anything, int64(1), mock.Anything).Return(nil).Once() suite.NoError(scanTaskStatusChange(suite.ctx, 1, job.SuccessStatus.String())) } @@ -108,7 +108,7 @@ func (suite *CallbackTestSuite) TestScanTaskStatusChange() { }, nil, ).Once() - suite.robotCtl.On("Delete", mock.Anything, int64(1)).Return(fmt.Errorf("failed")).Once() + suite.robotCtl.On("Delete", mock.Anything, int64(1), mock.Anything).Return(fmt.Errorf("failed")).Once() suite.NoError(scanTaskStatusChange(suite.ctx, 1, job.SuccessStatus.String())) } diff --git a/src/testing/controller/robot/controller.go b/src/testing/controller/robot/controller.go index 442c7fb91ab..32e09ea4e6f 100644 --- a/src/testing/controller/robot/controller.go +++ b/src/testing/controller/robot/controller.go @@ -79,17 +79,24 @@ func (_m *Controller) Create(ctx context.Context, r *robot.Robot) (int64, string return r0, r1, r2 } -// Delete provides a mock function with given fields: ctx, id -func (_m *Controller) Delete(ctx context.Context, id int64) error { - ret := _m.Called(ctx, id) +// Delete provides a mock function with given fields: ctx, id, option +func (_m *Controller) Delete(ctx context.Context, id int64, option ...*robot.Option) error { + _va := make([]interface{}, len(option)) + for _i := range option { + _va[_i] = option[_i] + } + var _ca []interface{} + _ca = append(_ca, ctx, id) + _ca = append(_ca, _va...) + ret := _m.Called(_ca...) if len(ret) == 0 { panic("no return value specified for Delete") } var r0 error - if rf, ok := ret.Get(0).(func(context.Context, int64) error); ok { - r0 = rf(ctx, id) + if rf, ok := ret.Get(0).(func(context.Context, int64, ...*robot.Option) error); ok { + r0 = rf(ctx, id, option...) } else { r0 = ret.Error(0) }