Skip to content

Commit

Permalink
Merge pull request #27 from lidofinance/new-finding
Browse files Browse the repository at this point in the history
feat: support new Finding object
  • Loading branch information
sergeyWh1te authored Oct 3, 2024
2 parents 0f9615c + bb0c8b4 commit 2ed06e8
Show file tree
Hide file tree
Showing 13 changed files with 283 additions and 47 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ generate-proto:
brief/proto/*.proto

generate-databus-objects:
bin/jsonschema -p databus -o generated/databaus/block.dto.go ./brief/databus/block.dto.json
for file in ./brief/databus/*.dto.json; do \
base_name=$$(basename $$file .dto.json); \
bin/jsonschema -p databus -o generated/databus/$$base_name.dto.go $$file; \
done
.PHONY: generate-databus-objects

.PHONY: generate-proto
44 changes: 44 additions & 0 deletions brief/databus/finding.dto.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Finding",
"type": "object",
"properties": {
"severity": {
"$ref": "#/definitions/Severity"
},
"alertId": {
"type": "string"
},
"name": {
"type": "string"
},
"description": {
"type": "string"
},
"uniqueKey": {
"type": "string"
},
"blockTimestamp": {
"type": "integer"
},
"blockNumber": {
"type": "integer"
},
"txHash": {
"type": "string"
},
"botName": {
"type": "string"
},
"team": {
"type": "string"
}
},
"required": ["severity", "alertId", "name", "description", "botName", "team"],
"definitions": {
"Severity": {
"type": "string",
"enum": ["Unknown", "Info", "Low", "Medium", "High", "Critical"]
}
}
}
File renamed without changes.
112 changes: 112 additions & 0 deletions generated/databus/finding.dto.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/app/feeder/feeder.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/nats-io/nats.go/jetstream"
"github.com/prometheus/client_golang/prometheus"

databus "github.com/lidofinance/finding-forwarder/generated/databaus"
"github.com/lidofinance/finding-forwarder/generated/databus"
"github.com/lidofinance/finding-forwarder/internal/connectors/metrics"
"github.com/lidofinance/finding-forwarder/internal/pkg/chain/entity"
)
Expand Down
19 changes: 14 additions & 5 deletions internal/app/worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
"github.com/prometheus/client_golang/prometheus"
"golang.org/x/sync/errgroup"

"github.com/lidofinance/finding-forwarder/generated/databus"
"github.com/lidofinance/finding-forwarder/generated/forta/models"
"github.com/lidofinance/finding-forwarder/generated/proto"
"github.com/lidofinance/finding-forwarder/internal/connectors/metrics"
"github.com/lidofinance/finding-forwarder/internal/pkg/notifiler"
"github.com/lidofinance/finding-forwarder/internal/utils/registry"
Expand Down Expand Up @@ -183,7 +183,7 @@ func (w *findingWorker) Run(ctx context.Context, g *errgroup.Group) error {
consumers = append(consumers, Consumer{
name: consumer.Name,
handler: func(msg jetstream.Msg) {
finding := new(proto.Finding)
finding := new(databus.FindingDtoJson)

if alertErr := json.Unmarshal(msg.Data(), finding); alertErr != nil {
w.log.Error(fmt.Sprintf(`Broken message: %v`, alertErr))
Expand All @@ -200,7 +200,7 @@ func (w *findingWorker) Run(ctx context.Context, g *errgroup.Group) error {
return
}

if finding.Severity == proto.Finding_UNKNOWN {
if finding.Severity == databus.SeverityUnknown {
if sendErr := consumer.notifiler.SendFinding(ctx, finding); sendErr != nil {
w.log.Error(fmt.Sprintf(`Could not send bot-finding: %v`, sendErr),
slog.Attr{
Expand Down Expand Up @@ -268,8 +268,13 @@ func (w *findingWorker) Run(ctx context.Context, g *errgroup.Group) error {

touchTimes, _ := w.cache.Get(countKey)

msgInfo := fmt.Sprintf("%s: AlertId %s read %d times. %s...%s", consumer.Name, finding.AlertId, touchTimes, key[0:4], key[len(key)-4:])
if finding.BlockNumber != nil {
msgInfo += fmt.Sprintf(" blockNumber %d", *finding.BlockNumber)
}

// TODO add finding.blockNumber
w.log.Info(fmt.Sprintf("Consumer: %s AlertId %s read %d times", consumer.Name, finding.AlertId, touchTimes),
w.log.Info(msgInfo,
slog.Attr{
Key: `desc`,
Value: slog.StringValue(finding.Description),
Expand All @@ -284,7 +289,11 @@ func (w *findingWorker) Run(ctx context.Context, g *errgroup.Group) error {
},
slog.Attr{
Key: `severity`,
Value: slog.StringValue(finding.Severity.String()),
Value: slog.StringValue(string(finding.Severity)),
},
slog.Attr{
Key: `hash`,
Value: slog.StringValue(key),
},
)

Expand Down
4 changes: 2 additions & 2 deletions internal/pkg/notifiler/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package notifiler
import (
"context"

"github.com/lidofinance/finding-forwarder/generated/databus"
"github.com/lidofinance/finding-forwarder/generated/forta/models"
"github.com/lidofinance/finding-forwarder/generated/proto"
)

type FindingSender interface {
SendFinding(ctx context.Context, alert *proto.Finding) error
SendFinding(ctx context.Context, alert *databus.FindingDtoJson) error
SendAlert(ctx context.Context, alert *models.Alert) error
}
6 changes: 3 additions & 3 deletions internal/pkg/notifiler/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (

"github.com/prometheus/client_golang/prometheus"

"github.com/lidofinance/finding-forwarder/generated/databus"
"github.com/lidofinance/finding-forwarder/generated/forta/models"
"github.com/lidofinance/finding-forwarder/generated/proto"
"github.com/lidofinance/finding-forwarder/internal/connectors/metrics"
)

Expand All @@ -35,8 +35,8 @@ func NewDiscord(webhookURL string, httpClient *http.Client, metricsStore *metric
}
}

func (d *discord) SendFinding(ctx context.Context, alert *proto.Finding) error {
message := fmt.Sprintf("%s\n\n%s\nAlertId: %s\nSource: %s", alert.Name, alert.Description, alert.AlertId, d.source)
func (d *discord) SendFinding(ctx context.Context, alert *databus.FindingDtoJson) error {
message := fmt.Sprintf("%s\n\n%s", alert.Name, FormatAlert(alert, d.source))

return d.send(ctx, message)
}
Expand Down
47 changes: 47 additions & 0 deletions internal/pkg/notifiler/format_alert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package notifiler

import (
"fmt"
"time"

"github.com/lidofinance/finding-forwarder/generated/databus"
)

func FormatAlert(alert *databus.FindingDtoJson, source string) string {
var (
body string
footer string
)

if len(alert.Description) != 0 {
body = alert.Description
footer += "\n\n"
}

footer += fmt.Sprintf("Alert Id: %s", alert.AlertId)
footer += fmt.Sprintf("\nBot name: %s", alert.BotName)
footer += fmt.Sprintf("\nTeam: %s", alert.Team)

if alert.BlockNumber != nil {
footer += fmt.Sprintf("\nBlock number: [%d](https://etherscan.io/block/%d)", *alert.BlockNumber, *alert.BlockNumber)
}

if alert.TxHash != nil {
footer += fmt.Sprintf("\nTx hash: [%s](https://etherscan.io/tx/%s)", shortenHex(*alert.TxHash), *alert.TxHash)
}

footer += fmt.Sprintf("\nSource: %s", source)
footer += fmt.Sprintf("\nServer timestamp: %s", time.Now().Format("15:04:05.000 MST"))
if alert.BlockTimestamp != nil {
footer += fmt.Sprintf("\nBlock timestamp: %s", time.Unix(int64(*alert.BlockTimestamp), 0).Format("15:04:05.000 MST"))
}

return fmt.Sprintf("%s%s", body, footer)
}

func shortenHex(input string) string {
if len(input) <= 10 {
return input
}
return fmt.Sprintf("x%s...%s", input[2:5], input[len(input)-3:])
}
12 changes: 6 additions & 6 deletions internal/pkg/notifiler/opsgenia.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (

"github.com/prometheus/client_golang/prometheus"

"github.com/lidofinance/finding-forwarder/generated/databus"
"github.com/lidofinance/finding-forwarder/generated/forta/models"
"github.com/lidofinance/finding-forwarder/generated/proto"
"github.com/lidofinance/finding-forwarder/internal/connectors/metrics"
)

Expand All @@ -38,12 +38,12 @@ func NewOpsGenia(opsGenieKey string, httpClient *http.Client, metricsStore *metr
}
}

func (u *opsGenia) SendFinding(ctx context.Context, alert *proto.Finding) error {
func (u *opsGenia) SendFinding(ctx context.Context, alert *databus.FindingDtoJson) error {
opsGeniaPriority := ""
switch alert.Severity {
case proto.Finding_CRITICAL:
case databus.SeverityCritical:
opsGeniaPriority = "P2"
case proto.Finding_HIGH:
case databus.SeverityHigh:
opsGeniaPriority = "P3"
}

Expand All @@ -52,12 +52,12 @@ func (u *opsGenia) SendFinding(ctx context.Context, alert *proto.Finding) error
return nil
}

message := fmt.Sprintf("%s\n\nAlertId:%s\nSource: %s", alert.Description, alert.GetAlertId(), u.source)
message := FormatAlert(alert, u.source)

payload := AlertPayload{
Message: alert.Name,
Description: message,
Alias: alert.GetAlertId(),
Alias: alert.AlertId,
Priority: opsGeniaPriority,
}

Expand Down
10 changes: 5 additions & 5 deletions internal/pkg/notifiler/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (

"github.com/prometheus/client_golang/prometheus"

"github.com/lidofinance/finding-forwarder/generated/databus"
"github.com/lidofinance/finding-forwarder/generated/forta/models"
"github.com/lidofinance/finding-forwarder/generated/proto"
"github.com/lidofinance/finding-forwarder/internal/connectors/metrics"
)

Expand All @@ -32,10 +32,10 @@ func NewTelegram(botToken, chatID string, httpClient *http.Client, metricsStore
}
}

func (u *telegram) SendFinding(ctx context.Context, alert *proto.Finding) error {
message := fmt.Sprintf("%s\n\n%s\n\nAlertId: %s\nSource: %s", alert.Name, alert.Description, alert.GetAlertId(), u.source)
func (u *telegram) SendFinding(ctx context.Context, alert *databus.FindingDtoJson) error {
message := fmt.Sprintf("%s\n\n%s", alert.Name, FormatAlert(alert, u.source))

if alert.Severity != proto.Finding_UNKNOWN {
if alert.Severity != databus.SeverityUnknown {
return u.send(ctx, message, true)
}

Expand All @@ -53,7 +53,7 @@ func (u *telegram) SendAlert(ctx context.Context, alert *models.Alert) error {
}

func (u *telegram) send(ctx context.Context, message string, useMarkdown bool) error {
//nolint

requestURL := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage?chat_id=-%s&text=%s", u.botToken, u.chatID, url.QueryEscape(message))
if useMarkdown {
requestURL += `&parse_mode=markdown`
Expand Down
Loading

0 comments on commit 2ed06e8

Please sign in to comment.