diff --git a/src/controller/event/handler/auditlog/auditlog.go b/src/controller/event/handler/auditlog/auditlog.go index f94890d5d..56ee90f66 100644 --- a/src/controller/event/handler/auditlog/auditlog.go +++ b/src/controller/event/handler/auditlog/auditlog.go @@ -33,6 +33,11 @@ type AuditResolver interface { ResolveToAuditLog() (*am.AuditLog, error) } +// Name ... +func (h *Handler) Name() string { + return "AuditLog" +} + // Handle ... func (h *Handler) Handle(value interface{}) error { ctx := orm.NewContext(context.Background(), beegorm.NewOrm()) diff --git a/src/controller/event/handler/auditlog/auditlog_test.go b/src/controller/event/handler/auditlog/auditlog_test.go index ffc3227f9..24eb3a81a 100644 --- a/src/controller/event/handler/auditlog/auditlog_test.go +++ b/src/controller/event/handler/auditlog/auditlog_test.go @@ -91,6 +91,10 @@ func (suite *AuditLogHandlerTestSuite) TestSubscribeTagEvent() { } +func (suite *AuditLogHandlerTestSuite) TestName() { + suite.Equal("AuditLog", suite.auditLogHandler.Name()) +} + func TestAuditLogHandlerTestSuite(t *testing.T) { suite.Run(t, &AuditLogHandlerTestSuite{}) } diff --git a/src/controller/event/handler/internal/artifact.go b/src/controller/event/handler/internal/artifact.go index 876ae9563..a658aea95 100644 --- a/src/controller/event/handler/internal/artifact.go +++ b/src/controller/event/handler/internal/artifact.go @@ -31,6 +31,11 @@ type Handler struct { Context func() context.Context } +// Name ... +func (a *Handler) Name() string { + return "InternalArtifact" +} + // Handle ... func (a *Handler) Handle(value interface{}) error { switch v := value.(type) { diff --git a/src/controller/event/handler/p2p/preheat.go b/src/controller/event/handler/p2p/preheat.go index f985f6190..240e348d7 100644 --- a/src/controller/event/handler/p2p/preheat.go +++ b/src/controller/event/handler/p2p/preheat.go @@ -32,6 +32,11 @@ type Handler struct { Context func() context.Context } +// Name ... +func (p *Handler) Name() string { + return "P2PPreheat" +} + // Handle ... func (p *Handler) Handle(value interface{}) error { switch value.(type) { diff --git a/src/controller/event/handler/p2p/preheat_test.go b/src/controller/event/handler/p2p/preheat_test.go index c2210fa6f..48f5a7efe 100644 --- a/src/controller/event/handler/p2p/preheat_test.go +++ b/src/controller/event/handler/p2p/preheat_test.go @@ -75,6 +75,10 @@ func (suite *PreheatTestSuite) TestIsStateful() { suite.False(b, "handler is stateful") } +func (suite *PreheatTestSuite) TestName() { + suite.Equal("P2PPreheat", suite.handler.Name()) +} + // TestHandle ... func (suite *PreheatTestSuite) TestHandle() { type args struct { diff --git a/src/controller/event/handler/replication/replication.go b/src/controller/event/handler/replication/replication.go index d46e7b727..9156eaaeb 100644 --- a/src/controller/event/handler/replication/replication.go +++ b/src/controller/event/handler/replication/replication.go @@ -30,6 +30,11 @@ import ( type Handler struct { } +// Name ... +func (r *Handler) Name() string { + return "Replication" +} + // Handle ... func (r *Handler) Handle(value interface{}) error { pushArtEvent, ok := value.(*event.PushArtifactEvent) diff --git a/src/controller/event/handler/webhook/artifact/artifact.go b/src/controller/event/handler/webhook/artifact/artifact.go index 2f12079d9..d4d839182 100644 --- a/src/controller/event/handler/webhook/artifact/artifact.go +++ b/src/controller/event/handler/webhook/artifact/artifact.go @@ -35,6 +35,11 @@ import ( type Handler struct { } +// Name ... +func (a *Handler) Name() string { + return "ArtifactWebhook" +} + // Handle preprocess artifact event data and then publish hook event func (a *Handler) Handle(value interface{}) error { switch v := value.(type) { diff --git a/src/controller/event/handler/webhook/artifact/replication.go b/src/controller/event/handler/webhook/artifact/replication.go index c811d8b79..8b62b415e 100644 --- a/src/controller/event/handler/webhook/artifact/replication.go +++ b/src/controller/event/handler/webhook/artifact/replication.go @@ -24,6 +24,11 @@ import ( type ReplicationHandler struct { } +// Name ... +func (r *ReplicationHandler) Name() string { + return "ReplicationWebhook" +} + // Handle ... func (r *ReplicationHandler) Handle(value interface{}) error { if !config.NotificationEnable() { diff --git a/src/controller/event/handler/webhook/artifact/replication_test.go b/src/controller/event/handler/webhook/artifact/replication_test.go index 8e9fcea06..a89254911 100644 --- a/src/controller/event/handler/webhook/artifact/replication_test.go +++ b/src/controller/event/handler/webhook/artifact/replication_test.go @@ -288,3 +288,8 @@ func TestReplicationHandler_IsStateful(t *testing.T) { handler := &ReplicationHandler{} assert.False(t, handler.IsStateful()) } + +func TestReplicationHandler_Name(t *testing.T) { + handler := &ReplicationHandler{} + assert.Equal(t, "ReplicationWebhook", handler.Name()) +} diff --git a/src/controller/event/handler/webhook/artifact/retention.go b/src/controller/event/handler/webhook/artifact/retention.go index 11114e7e7..d0cae40d7 100644 --- a/src/controller/event/handler/webhook/artifact/retention.go +++ b/src/controller/event/handler/webhook/artifact/retention.go @@ -29,6 +29,11 @@ func NewRetentionController() retention.APIController { return api.GetRetentionController() } +// Name ... +func (r *RetentionHandler) Name() string { + return "RetentionWebhook" +} + // Handle ... func (r *RetentionHandler) Handle(value interface{}) error { if !config.NotificationEnable() { diff --git a/src/controller/event/handler/webhook/chart/chart.go b/src/controller/event/handler/webhook/chart/chart.go index 9a32958ee..44b46f1e0 100644 --- a/src/controller/event/handler/webhook/chart/chart.go +++ b/src/controller/event/handler/webhook/chart/chart.go @@ -34,6 +34,11 @@ import ( type Handler struct { } +// Name ... +func (cph *Handler) Name() string { + return "ChartWebhook" +} + // Handle preprocess chart event data and then publish hook event func (cph *Handler) Handle(value interface{}) error { chartEvent, ok := value.(*event.ChartEvent) diff --git a/src/controller/event/handler/webhook/chart/chart_test.go b/src/controller/event/handler/webhook/chart/chart_test.go index 545290fde..720fa1803 100644 --- a/src/controller/event/handler/webhook/chart/chart_test.go +++ b/src/controller/event/handler/webhook/chart/chart_test.go @@ -134,3 +134,8 @@ func TestChartPreprocessHandler_IsStateful(t *testing.T) { handler := &Handler{} assert.False(t, handler.IsStateful()) } + +func TestChartPreprocessHandler_Name(t *testing.T) { + handler := &Handler{} + assert.Equal(t, "ChartWebhook", handler.Name()) +} diff --git a/src/controller/event/handler/webhook/quota/quota.go b/src/controller/event/handler/webhook/quota/quota.go index 1757acaae..a34b62166 100644 --- a/src/controller/event/handler/webhook/quota/quota.go +++ b/src/controller/event/handler/webhook/quota/quota.go @@ -33,6 +33,11 @@ import ( type Handler struct { } +// Name ... +func (qp *Handler) Name() string { + return "QuotaWebhook" +} + // Handle ... func (qp *Handler) Handle(value interface{}) error { quotaEvent, ok := value.(*event.QuotaEvent) diff --git a/src/controller/event/handler/webhook/quota/quota_test.go b/src/controller/event/handler/webhook/quota/quota_test.go index 5462b5e16..0e751647d 100644 --- a/src/controller/event/handler/webhook/quota/quota_test.go +++ b/src/controller/event/handler/webhook/quota/quota_test.go @@ -93,6 +93,11 @@ func (suite *QuotaPreprocessHandlerSuite) TestHandle() { // MockHandler ... type MockHandler struct{} +// Name ... +func (m *MockHandler) Name() string { + return "Mock" +} + // Handle ... func (m *MockHandler) Handle(value interface{}) error { return nil diff --git a/src/controller/event/handler/webhook/scan/delete.go b/src/controller/event/handler/webhook/scan/delete.go index 0df958bd0..f87bf2cf5 100644 --- a/src/controller/event/handler/webhook/scan/delete.go +++ b/src/controller/event/handler/webhook/scan/delete.go @@ -30,6 +30,11 @@ import ( type DelArtHandler struct { } +// Name ... +func (o *DelArtHandler) Name() string { + return "DeleteArtifactWebhook" +} + // Handle ... func (o *DelArtHandler) Handle(value interface{}) error { if value == nil { diff --git a/src/controller/event/handler/webhook/scan/scan.go b/src/controller/event/handler/webhook/scan/scan.go index 302523a4b..33e245872 100644 --- a/src/controller/event/handler/webhook/scan/scan.go +++ b/src/controller/event/handler/webhook/scan/scan.go @@ -37,6 +37,11 @@ import ( type Handler struct { } +// Name ... +func (si *Handler) Name() string { + return "ScanWebhook" +} + // Handle preprocess chart event data and then publish hook event func (si *Handler) Handle(value interface{}) error { if value == nil { diff --git a/src/controller/event/handler/webhook/scan/scan_test.go b/src/controller/event/handler/webhook/scan/scan_test.go index b57ffe6a7..29268e743 100644 --- a/src/controller/event/handler/webhook/scan/scan_test.go +++ b/src/controller/event/handler/webhook/scan/scan_test.go @@ -137,6 +137,11 @@ func (suite *ScanImagePreprocessHandlerSuite) TestHandle() { // MockHTTPHandler ... type MockHTTPHandler struct{} +// Name ... +func (m *MockHTTPHandler) Name() string { + return "MockHTTP" +} + // Handle ... func (m *MockHTTPHandler) Handle(value interface{}) error { return nil diff --git a/src/controller/event/topic.go b/src/controller/event/topic.go index efd336920..ca3a76127 100644 --- a/src/controller/event/topic.go +++ b/src/controller/event/topic.go @@ -71,6 +71,11 @@ func (c *CreateProjectEvent) ResolveToAuditLog() (*model.AuditLog, error) { return auditLog, nil } +func (c *CreateProjectEvent) String() string { + return fmt.Sprintf("ID-%d Name-%s Operator-%s OccurAt-%s", + c.ProjectID, c.Project, c.Operator, c.OccurAt.Format("2006-01-02 15:04:05")) +} + // DeleteProjectEvent is the deleting project event type DeleteProjectEvent struct { EventType string @@ -92,6 +97,11 @@ func (d *DeleteProjectEvent) ResolveToAuditLog() (*model.AuditLog, error) { return auditLog, nil } +func (d *DeleteProjectEvent) String() string { + return fmt.Sprintf("ID-%d Name-%s Operator-%s OccurAt-%s", + d.ProjectID, d.Project, d.Operator, d.OccurAt.Format("2006-01-02 15:04:05")) +} + // DeleteRepositoryEvent is the deleting repository event type DeleteRepositoryEvent struct { EventType string @@ -114,6 +124,11 @@ func (d *DeleteRepositoryEvent) ResolveToAuditLog() (*model.AuditLog, error) { return auditLog, nil } +func (d *DeleteRepositoryEvent) String() string { + return fmt.Sprintf("ID-%d Repository-%s Operator-%s OccurAt-%s", + d.ProjectID, d.Repository, d.Operator, d.OccurAt.Format("2006-01-02 15:04:05")) +} + // ArtifactEvent is the pushing/pulling artifact event type ArtifactEvent struct { EventType string @@ -124,6 +139,12 @@ type ArtifactEvent struct { OccurAt time.Time } +func (a *ArtifactEvent) String() string { + return fmt.Sprintf("ID-%d, Repository-%s Tags-%s Digest-%s Operator-%s OccurAt-%s", + a.Artifact.ID, a.Repository, a.Tags, a.Artifact.Digest, a.Operator, + a.OccurAt.Format("2006-01-02 15:04:05")) +} + // PushArtifactEvent is the pushing artifact event type PushArtifactEvent struct { *ArtifactEvent @@ -149,6 +170,10 @@ func (p *PushArtifactEvent) ResolveToAuditLog() (*model.AuditLog, error) { return auditLog, nil } +func (p *PushArtifactEvent) String() string { + return p.ArtifactEvent.String() +} + // PullArtifactEvent is the pulling artifact event type PullArtifactEvent struct { *ArtifactEvent @@ -181,23 +206,31 @@ func (p *PullArtifactEvent) ResolveToAuditLog() (*model.AuditLog, error) { return auditLog, nil } +func (p *PullArtifactEvent) String() string { + return p.ArtifactEvent.String() +} + // DeleteArtifactEvent is the deleting artifact event type DeleteArtifactEvent struct { *ArtifactEvent } // ResolveToAuditLog ... -func (p *DeleteArtifactEvent) ResolveToAuditLog() (*model.AuditLog, error) { +func (d *DeleteArtifactEvent) ResolveToAuditLog() (*model.AuditLog, error) { auditLog := &model.AuditLog{ - ProjectID: p.Artifact.ProjectID, - OpTime: p.OccurAt, + ProjectID: d.Artifact.ProjectID, + OpTime: d.OccurAt, Operation: "delete", - Username: p.Operator, + Username: d.Operator, ResourceType: "artifact", - Resource: fmt.Sprintf("%s:%s", p.Artifact.RepositoryName, p.Artifact.Digest)} + Resource: fmt.Sprintf("%s:%s", d.Artifact.RepositoryName, d.Artifact.Digest)} return auditLog, nil } +func (d *DeleteArtifactEvent) String() string { + return d.ArtifactEvent.String() +} + // CreateTagEvent is the creating tag event type CreateTagEvent struct { EventType string @@ -220,6 +253,12 @@ func (c *CreateTagEvent) ResolveToAuditLog() (*model.AuditLog, error) { return auditLog, nil } +func (c *CreateTagEvent) String() string { + return fmt.Sprintf("ArtifactID-%d, Repository-%s Tag-%s Digest-%s Operator-%s OccurAt-%s", + c.AttachedArtifact.ID, c.Repository, c.Tag, c.AttachedArtifact.Digest, c.Operator, + c.OccurAt.Format("2006-01-02 15:04:05")) +} + // DeleteTagEvent is the deleting tag event type DeleteTagEvent struct { EventType string @@ -242,6 +281,12 @@ func (d *DeleteTagEvent) ResolveToAuditLog() (*model.AuditLog, error) { return auditLog, nil } +func (d *DeleteTagEvent) String() string { + return fmt.Sprintf("ArtifactID-%d, Repository-%s Tag-%s Digest-%s Operator-%s OccurAt-%s", + d.AttachedArtifact.ID, d.Repository, d.Tag, d.AttachedArtifact.Digest, d.Operator, + d.OccurAt.Format("2006-01-02 15:04:05")) +} + // ScanImageEvent is scanning image related event data to publish type ScanImageEvent struct { EventType string @@ -250,6 +295,11 @@ type ScanImageEvent struct { Operator string } +func (s *ScanImageEvent) String() string { + return fmt.Sprintf("Artifact-%+v Operator-%s OccurAt-%s", + s.Artifact, s.Operator, s.OccurAt.Format("2006-01-02 15:04:05")) +} + // ChartEvent is chart related event data to publish type ChartEvent struct { EventType string @@ -260,6 +310,11 @@ type ChartEvent struct { Operator string } +func (c *ChartEvent) String() string { + return fmt.Sprintf("ProjectName-%s ChartName-%s Versions-%s Operator-%s OccurAt-%s", + c.ProjectName, c.ChartName, c.Versions, c.Operator, c.OccurAt.Format("2006-01-02 15:04:05")) +} + // QuotaEvent is project quota related event data to publish type QuotaEvent struct { EventType string @@ -270,6 +325,11 @@ type QuotaEvent struct { Msg string } +func (q *QuotaEvent) String() string { + return fmt.Sprintf("ProjectID-%d RepoName-%s Resource-%+v Msg-%s OccurAt-%s", + q.Project.ProjectID, q.RepoName, q.Resource, q.Msg, q.OccurAt.Format("2006-01-02 15:04:05")) +} + // ImgResource include image digest and tag type ImgResource struct { Digest string @@ -284,6 +344,11 @@ type ReplicationEvent struct { Status string } +func (r *ReplicationEvent) String() string { + return fmt.Sprintf("ReplicationTaskID-%d Status-%s OccurAt-%s", + r.ReplicationTaskID, r.Status, r.OccurAt.Format("2006-01-02 15:04:05")) +} + // ArtifactLabeledEvent is event data of artifact labeled type ArtifactLabeledEvent struct { ArtifactID int64 @@ -292,6 +357,11 @@ type ArtifactLabeledEvent struct { Operator string } +func (al *ArtifactLabeledEvent) String() string { + return fmt.Sprintf("ArtifactID-%d LabelID-%d Operator-%s OccurAt-%s", + al.ArtifactID, al.LabelID, al.Operator, al.OccurAt.Format("2006-01-02 15:04:05")) +} + // RetentionEvent is tag retention related event data to publish type RetentionEvent struct { TaskID int64 @@ -300,3 +370,14 @@ type RetentionEvent struct { Status string Deleted []*selector.Result } + +func (r *RetentionEvent) String() string { + candidates := []string{} + for _, candidate := range r.Deleted { + candidates = append(candidates, fmt.Sprintf("%s:%s:%s", candidate.Target.Namespace, + candidate.Target.Repository, candidate.Target.Tags)) + } + + return fmt.Sprintf("TaskID-%d Status-%s Deleted-%s OccurAt-%s", + r.TaskID, r.Status, candidates, r.OccurAt.Format("2006-01-02 15:04:05")) +} diff --git a/src/pkg/notifier/handler/notification/http_handler.go b/src/pkg/notifier/handler/notification/http_handler.go index 9ea5ba431..00af463b9 100755 --- a/src/pkg/notifier/handler/notification/http_handler.go +++ b/src/pkg/notifier/handler/notification/http_handler.go @@ -15,6 +15,11 @@ import ( type HTTPHandler struct { } +// Name ... +func (h *HTTPHandler) Name() string { + return "HTTP" +} + // Handle handles http event func (h *HTTPHandler) Handle(value interface{}) error { if value == nil { diff --git a/src/pkg/notifier/handler/notification/http_handler_test.go b/src/pkg/notifier/handler/notification/http_handler_test.go index 1bd5874b0..c4313369c 100644 --- a/src/pkg/notifier/handler/notification/http_handler_test.go +++ b/src/pkg/notifier/handler/notification/http_handler_test.go @@ -95,3 +95,8 @@ func TestHTTPHandler_IsStateful(t *testing.T) { handler := &HTTPHandler{} assert.False(t, handler.IsStateful()) } + +func TestHTTPHandler_Name(t *testing.T) { + handler := &HTTPHandler{} + assert.Equal(t, "HTTP", handler.Name()) +} diff --git a/src/pkg/notifier/handler/notification/slack_handler.go b/src/pkg/notifier/handler/notification/slack_handler.go index 51bc50c40..e228ef3ff 100644 --- a/src/pkg/notifier/handler/notification/slack_handler.go +++ b/src/pkg/notifier/handler/notification/slack_handler.go @@ -64,6 +64,11 @@ const ( type SlackHandler struct { } +// Name ... +func (s *SlackHandler) Name() string { + return "Slack" +} + // Handle handles event to slack func (s *SlackHandler) Handle(value interface{}) error { if value == nil { diff --git a/src/pkg/notifier/handler/notification/slack_handler_test.go b/src/pkg/notifier/handler/notification/slack_handler_test.go index d9316e32a..99f2be64f 100644 --- a/src/pkg/notifier/handler/notification/slack_handler_test.go +++ b/src/pkg/notifier/handler/notification/slack_handler_test.go @@ -99,3 +99,8 @@ func TestSlackHandler_IsStateful(t *testing.T) { handler := &SlackHandler{} assert.False(t, handler.IsStateful()) } + +func TestSlackHandler_Name(t *testing.T) { + handler := &SlackHandler{} + assert.Equal(t, "Slack", handler.Name()) +} diff --git a/src/pkg/notifier/notification_handler.go b/src/pkg/notifier/notification_handler.go index 876521edb..9b6134a16 100644 --- a/src/pkg/notifier/notification_handler.go +++ b/src/pkg/notifier/notification_handler.go @@ -3,6 +3,9 @@ package notifier // NotificationHandler defines what operations a notification handler // should have. type NotificationHandler interface { + // The name of the Handler + Name() string + // Handle the event when it coming. // value might be optional, it depends on usages. Handle(value interface{}) error diff --git a/src/pkg/notifier/notifier.go b/src/pkg/notifier/notifier.go index 79f6931dc..6dddc36e9 100644 --- a/src/pkg/notifier/notifier.go +++ b/src/pkg/notifier/notifier.go @@ -201,7 +201,7 @@ func (nw *NotificationWatcher) Notify(notification Notification) error { // Currently, we just log the error log.Errorf("Error occurred when triggering handler %s of topic %s: %s\n", reflect.TypeOf(hd).String(), notification.Topic, err.Error()) } else { - log.Infof("Handle notification with topic '%s': %#v\n", notification.Topic, notification.Value) + log.Infof("Handle notification with Handler '%s' on topic '%s': %s\n", hd.Name(), notification.Topic, notification.Value) } }() }(h, handlerChan) diff --git a/src/pkg/notifier/notifier_test.go b/src/pkg/notifier/notifier_test.go index 2ed7789ed..eb7ef95d2 100644 --- a/src/pkg/notifier/notifier_test.go +++ b/src/pkg/notifier/notifier_test.go @@ -13,6 +13,10 @@ type fakeStatefulHandler struct { number int } +func (fsh *fakeStatefulHandler) Name() string { + return "fakeStateful" +} + func (fsh *fakeStatefulHandler) IsStateful() bool { return true } @@ -32,6 +36,10 @@ func (fsh *fakeStatelessHandler) IsStateful() bool { return false } +func (fsh *fakeStatelessHandler) Name() string { + return "fakeStateless" +} + func (fsh *fakeStatelessHandler) Handle(v interface{}) error { return nil }