diff --git a/protobuffersrc/Alerts.proto b/protobuffersrc/Alerts.proto index 5b0c5214..8147374e 100644 --- a/protobuffersrc/Alerts.proto +++ b/protobuffersrc/Alerts.proto @@ -32,4 +32,5 @@ message Alert { AlertSeverity severity = 2; string message = 3; map params = 4; + int64 timestamp = 5; } diff --git a/services/alerts/publish.go b/services/alerts/publish.go index 10bcccc4..c33b4f08 100644 --- a/services/alerts/publish.go +++ b/services/alerts/publish.go @@ -2,11 +2,13 @@ package alerts import ( "errors" + "sync" + "time" + zmq "github.com/pebbe/zmq4" "github.com/untangle/golang-shared/services/logger" "github.com/untangle/golang-shared/structs/protocolbuffers/Alerts" "google.golang.org/protobuf/proto" - "sync" ) var alertPublisherSingleton *ZmqAlertPublisher @@ -81,6 +83,11 @@ func (publisher *ZmqAlertPublisher) Shutdown() error { // Send publishes the alert to on the ZMQ publishing socket. func (publisher *ZmqAlertPublisher) Send(alert *Alerts.Alert) { + // 2 reasons to set the timestamp here: + // - the caller isn't responsible for setting the timestamp so we just need to set it in one place (here) + // - we set it before putting it in queue, which means we have the timestamp of the alert creation, not the timestamp when it was processed + alert.Timestamp = time.Now().Unix() + logger.Debug("Publish alert %v\n", alert) alertMessage, err := proto.Marshal(alert) if err != nil { diff --git a/structs/protocolbuffers/Alerts/Alerts.pb.go b/structs/protocolbuffers/Alerts/Alerts.pb.go index 0a55045c..f82e824e 100644 --- a/structs/protocolbuffers/Alerts/Alerts.pb.go +++ b/structs/protocolbuffers/Alerts/Alerts.pb.go @@ -156,10 +156,11 @@ type Alert struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Type AlertType `protobuf:"varint,1,opt,name=type,proto3,enum=alerts.AlertType" json:"type,omitempty"` - Severity AlertSeverity `protobuf:"varint,2,opt,name=severity,proto3,enum=alerts.AlertSeverity" json:"severity,omitempty"` - Message string `protobuf:"bytes,3,opt,name=message,proto3" json:"message,omitempty"` - Params map[string]string `protobuf:"bytes,4,rep,name=params,proto3" json:"params,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"` + Type AlertType `protobuf:"varint,1,opt,name=type,proto3,enum=alerts.AlertType" json:"type,omitempty"` + Severity AlertSeverity `protobuf:"varint,2,opt,name=severity,proto3,enum=alerts.AlertSeverity" json:"severity,omitempty"` + Message string `protobuf:"bytes,3,opt,name=message,proto3" json:"message,omitempty"` + Params map[string]string `protobuf:"bytes,4,rep,name=params,proto3" json:"params,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"` + Timestamp int64 `protobuf:"varint,5,opt,name=timestamp,proto3" json:"timestamp,omitempty"` } func (x *Alert) Reset() { @@ -222,11 +223,18 @@ func (x *Alert) GetParams() map[string]string { return nil } +func (x *Alert) GetTimestamp() int64 { + if x != nil { + return x.Timestamp + } + return 0 +} + var File_Alerts_proto protoreflect.FileDescriptor var file_Alerts_proto_rawDesc = []byte{ 0x0a, 0x0c, 0x41, 0x6c, 0x65, 0x72, 0x74, 0x73, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x06, - 0x61, 0x6c, 0x65, 0x72, 0x74, 0x73, 0x22, 0xe9, 0x01, 0x0a, 0x05, 0x41, 0x6c, 0x65, 0x72, 0x74, + 0x61, 0x6c, 0x65, 0x72, 0x74, 0x73, 0x22, 0x87, 0x02, 0x0a, 0x05, 0x41, 0x6c, 0x65, 0x72, 0x74, 0x12, 0x25, 0x0a, 0x04, 0x74, 0x79, 0x70, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x11, 0x2e, 0x61, 0x6c, 0x65, 0x72, 0x74, 0x73, 0x2e, 0x41, 0x6c, 0x65, 0x72, 0x74, 0x54, 0x79, 0x70, 0x65, 0x52, 0x04, 0x74, 0x79, 0x70, 0x65, 0x12, 0x31, 0x0a, 0x08, 0x73, 0x65, 0x76, 0x65, 0x72, @@ -237,31 +245,33 @@ var file_Alerts_proto_rawDesc = []byte{ 0x73, 0x61, 0x67, 0x65, 0x12, 0x31, 0x0a, 0x06, 0x70, 0x61, 0x72, 0x61, 0x6d, 0x73, 0x18, 0x04, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x19, 0x2e, 0x61, 0x6c, 0x65, 0x72, 0x74, 0x73, 0x2e, 0x41, 0x6c, 0x65, 0x72, 0x74, 0x2e, 0x50, 0x61, 0x72, 0x61, 0x6d, 0x73, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x52, - 0x06, 0x70, 0x61, 0x72, 0x61, 0x6d, 0x73, 0x1a, 0x39, 0x0a, 0x0b, 0x50, 0x61, 0x72, 0x61, 0x6d, - 0x73, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, - 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, - 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x3a, 0x02, - 0x38, 0x01, 0x2a, 0xb6, 0x01, 0x0a, 0x09, 0x41, 0x6c, 0x65, 0x72, 0x74, 0x54, 0x79, 0x70, 0x65, - 0x12, 0x0b, 0x0a, 0x07, 0x55, 0x4e, 0x4b, 0x4e, 0x4f, 0x57, 0x4e, 0x10, 0x00, 0x12, 0x08, 0x0a, - 0x04, 0x55, 0x53, 0x45, 0x52, 0x10, 0x01, 0x12, 0x08, 0x0a, 0x04, 0x4c, 0x49, 0x4e, 0x4b, 0x10, - 0x02, 0x12, 0x14, 0x0a, 0x10, 0x54, 0x48, 0x52, 0x45, 0x41, 0x54, 0x50, 0x52, 0x45, 0x56, 0x45, - 0x4e, 0x54, 0x49, 0x4f, 0x4e, 0x10, 0x03, 0x12, 0x0d, 0x0a, 0x09, 0x57, 0x45, 0x42, 0x46, 0x49, - 0x4c, 0x54, 0x45, 0x52, 0x10, 0x04, 0x12, 0x15, 0x0a, 0x11, 0x57, 0x45, 0x42, 0x43, 0x4c, 0x41, - 0x53, 0x53, 0x49, 0x46, 0x49, 0x43, 0x41, 0x54, 0x49, 0x4f, 0x4e, 0x10, 0x05, 0x12, 0x09, 0x0a, - 0x05, 0x47, 0x45, 0x4f, 0x49, 0x50, 0x10, 0x06, 0x12, 0x0c, 0x0a, 0x08, 0x53, 0x45, 0x54, 0x54, - 0x49, 0x4e, 0x47, 0x53, 0x10, 0x07, 0x12, 0x0d, 0x0a, 0x09, 0x44, 0x49, 0x53, 0x43, 0x4f, 0x56, - 0x45, 0x52, 0x59, 0x10, 0x08, 0x12, 0x08, 0x0a, 0x04, 0x44, 0x48, 0x43, 0x50, 0x10, 0x09, 0x12, - 0x11, 0x0a, 0x0d, 0x43, 0x52, 0x49, 0x54, 0x49, 0x43, 0x41, 0x4c, 0x45, 0x52, 0x52, 0x4f, 0x52, - 0x10, 0x0a, 0x12, 0x07, 0x0a, 0x03, 0x56, 0x50, 0x4e, 0x10, 0x0b, 0x2a, 0x47, 0x0a, 0x0d, 0x41, - 0x6c, 0x65, 0x72, 0x74, 0x53, 0x65, 0x76, 0x65, 0x72, 0x69, 0x74, 0x79, 0x12, 0x08, 0x0a, 0x04, - 0x49, 0x4e, 0x46, 0x4f, 0x10, 0x00, 0x12, 0x08, 0x0a, 0x04, 0x57, 0x41, 0x52, 0x4e, 0x10, 0x01, - 0x12, 0x09, 0x0a, 0x05, 0x45, 0x52, 0x52, 0x4f, 0x52, 0x10, 0x02, 0x12, 0x09, 0x0a, 0x05, 0x44, - 0x45, 0x42, 0x55, 0x47, 0x10, 0x03, 0x12, 0x0c, 0x0a, 0x08, 0x43, 0x52, 0x49, 0x54, 0x49, 0x43, - 0x41, 0x4c, 0x10, 0x04, 0x42, 0x42, 0x5a, 0x40, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, - 0x6f, 0x6d, 0x2f, 0x75, 0x6e, 0x74, 0x61, 0x6e, 0x67, 0x6c, 0x65, 0x2f, 0x67, 0x6f, 0x6c, 0x61, - 0x6e, 0x67, 0x2d, 0x73, 0x68, 0x61, 0x72, 0x65, 0x64, 0x2f, 0x73, 0x74, 0x72, 0x75, 0x63, 0x74, - 0x73, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x6f, 0x6c, 0x62, 0x75, 0x66, 0x66, 0x65, 0x72, - 0x73, 0x2f, 0x41, 0x6c, 0x65, 0x72, 0x74, 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x06, 0x70, 0x61, 0x72, 0x61, 0x6d, 0x73, 0x12, 0x1c, 0x0a, 0x09, 0x74, 0x69, 0x6d, 0x65, 0x73, + 0x74, 0x61, 0x6d, 0x70, 0x18, 0x05, 0x20, 0x01, 0x28, 0x03, 0x52, 0x09, 0x74, 0x69, 0x6d, 0x65, + 0x73, 0x74, 0x61, 0x6d, 0x70, 0x1a, 0x39, 0x0a, 0x0b, 0x50, 0x61, 0x72, 0x61, 0x6d, 0x73, 0x45, + 0x6e, 0x74, 0x72, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, + 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, + 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x3a, 0x02, 0x38, 0x01, + 0x2a, 0xb6, 0x01, 0x0a, 0x09, 0x41, 0x6c, 0x65, 0x72, 0x74, 0x54, 0x79, 0x70, 0x65, 0x12, 0x0b, + 0x0a, 0x07, 0x55, 0x4e, 0x4b, 0x4e, 0x4f, 0x57, 0x4e, 0x10, 0x00, 0x12, 0x08, 0x0a, 0x04, 0x55, + 0x53, 0x45, 0x52, 0x10, 0x01, 0x12, 0x08, 0x0a, 0x04, 0x4c, 0x49, 0x4e, 0x4b, 0x10, 0x02, 0x12, + 0x14, 0x0a, 0x10, 0x54, 0x48, 0x52, 0x45, 0x41, 0x54, 0x50, 0x52, 0x45, 0x56, 0x45, 0x4e, 0x54, + 0x49, 0x4f, 0x4e, 0x10, 0x03, 0x12, 0x0d, 0x0a, 0x09, 0x57, 0x45, 0x42, 0x46, 0x49, 0x4c, 0x54, + 0x45, 0x52, 0x10, 0x04, 0x12, 0x15, 0x0a, 0x11, 0x57, 0x45, 0x42, 0x43, 0x4c, 0x41, 0x53, 0x53, + 0x49, 0x46, 0x49, 0x43, 0x41, 0x54, 0x49, 0x4f, 0x4e, 0x10, 0x05, 0x12, 0x09, 0x0a, 0x05, 0x47, + 0x45, 0x4f, 0x49, 0x50, 0x10, 0x06, 0x12, 0x0c, 0x0a, 0x08, 0x53, 0x45, 0x54, 0x54, 0x49, 0x4e, + 0x47, 0x53, 0x10, 0x07, 0x12, 0x0d, 0x0a, 0x09, 0x44, 0x49, 0x53, 0x43, 0x4f, 0x56, 0x45, 0x52, + 0x59, 0x10, 0x08, 0x12, 0x08, 0x0a, 0x04, 0x44, 0x48, 0x43, 0x50, 0x10, 0x09, 0x12, 0x11, 0x0a, + 0x0d, 0x43, 0x52, 0x49, 0x54, 0x49, 0x43, 0x41, 0x4c, 0x45, 0x52, 0x52, 0x4f, 0x52, 0x10, 0x0a, + 0x12, 0x07, 0x0a, 0x03, 0x56, 0x50, 0x4e, 0x10, 0x0b, 0x2a, 0x47, 0x0a, 0x0d, 0x41, 0x6c, 0x65, + 0x72, 0x74, 0x53, 0x65, 0x76, 0x65, 0x72, 0x69, 0x74, 0x79, 0x12, 0x08, 0x0a, 0x04, 0x49, 0x4e, + 0x46, 0x4f, 0x10, 0x00, 0x12, 0x08, 0x0a, 0x04, 0x57, 0x41, 0x52, 0x4e, 0x10, 0x01, 0x12, 0x09, + 0x0a, 0x05, 0x45, 0x52, 0x52, 0x4f, 0x52, 0x10, 0x02, 0x12, 0x09, 0x0a, 0x05, 0x44, 0x45, 0x42, + 0x55, 0x47, 0x10, 0x03, 0x12, 0x0c, 0x0a, 0x08, 0x43, 0x52, 0x49, 0x54, 0x49, 0x43, 0x41, 0x4c, + 0x10, 0x04, 0x42, 0x42, 0x5a, 0x40, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, + 0x2f, 0x75, 0x6e, 0x74, 0x61, 0x6e, 0x67, 0x6c, 0x65, 0x2f, 0x67, 0x6f, 0x6c, 0x61, 0x6e, 0x67, + 0x2d, 0x73, 0x68, 0x61, 0x72, 0x65, 0x64, 0x2f, 0x73, 0x74, 0x72, 0x75, 0x63, 0x74, 0x73, 0x2f, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x6f, 0x6c, 0x62, 0x75, 0x66, 0x66, 0x65, 0x72, 0x73, 0x2f, + 0x41, 0x6c, 0x65, 0x72, 0x74, 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( diff --git a/testing/mocks/credentials_manager.go b/testing/mocks/credentials_manager.go index 4dc8d576..3f6962bc 100644 --- a/testing/mocks/credentials_manager.go +++ b/testing/mocks/credentials_manager.go @@ -1,16 +1,24 @@ package mocks -import "github.com/untangle/golang-shared/services/credentialsmanager" +import ( + "github.com/stretchr/testify/mock" +) -type mockCredentialsManager struct{} +type mockCredentialsManager struct { + mock.Mock +} -func NewMockCredentialsManager() credentialsmanager.CredentialsManager { +func NewMockCredentialsManager() *mockCredentialsManager { return &mockCredentialsManager{} } -func (m *mockCredentialsManager) Startup() error { return nil } -func (m *mockCredentialsManager) Shutdown() error { return nil } -func (m *mockCredentialsManager) GetToken(key string) string { return "" } +func (m *mockCredentialsManager) Startup() error { return nil } +func (m *mockCredentialsManager) Shutdown() error { return nil } +func (m *mockCredentialsManager) GetToken(key string) string { + // this is required for mocking this function's call result + args := m.Called(key) + return args.String(0) +} func (m *mockCredentialsManager) Name() string { return "Mocked Credentials Manager" }