-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
n0str
authored and
n0str
committed
Nov 29, 2024
1 parent
6794387
commit 2df0bb1
Showing
3 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package errorshandler | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/codex-team/hawk.collector/pkg/broker" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/valyala/fasthttp" | ||
) | ||
|
||
const SentryQueueName = "errors/sentry" | ||
const CatcherType = "sentry" | ||
|
||
// HandleHTTP processes HTTP requests with JSON body | ||
func (handler *Handler) HandleSentry(ctx *fasthttp.RequestCtx) { | ||
if ctx.Request.Header.ContentLength() > handler.MaxErrorCatcherMessageSize { | ||
log.Warnf("Incoming request with size %d", ctx.Request.Header.ContentLength()) | ||
sendAnswerHTTP(ctx, ResponseMessage{Code: 400, Error: true, Message: "Request is too large"}) | ||
return | ||
} | ||
|
||
// check that X-Sentry-Auth header is available | ||
auth := ctx.Request.Header.Peek("X-Sentry-Auth") | ||
if auth == nil { | ||
log.Warnf("Incoming request without X-Sentry-Auth header") | ||
sendAnswerHTTP(ctx, ResponseMessage{Code: 400, Error: true, Message: "X-Sentry-Auth header is missing"}) | ||
return | ||
} | ||
|
||
hawkToken, err := getSentryKeyFromAuth(string(auth)) | ||
if err != nil { | ||
log.Warnf("Incoming request with invalid X-Sentry-Auth header: %s", err) | ||
sendAnswerHTTP(ctx, ResponseMessage{Code: 400, Error: true, Message: err.Error()}) | ||
return | ||
} | ||
|
||
log.Debugf("Incoming request with hawk integration token: %s", hawkToken) | ||
|
||
body := ctx.PostBody() | ||
|
||
jsonBody, err := decompressGzipString(body) | ||
if err != nil { | ||
log.Warnf("Failed to decompress gzip body: %s", err) | ||
sendAnswerHTTP(ctx, ResponseMessage{Code: 400, Error: true, Message: "Failed to decompress gzip body"}) | ||
return | ||
} | ||
log.Debugf("Decompressed body: %s", jsonBody) | ||
|
||
projectId, ok := handler.AccountsMongoDBClient.ValidTokens[hawkToken] | ||
if !ok { | ||
log.Debugf("Token %s is not in the accounts cache", hawkToken) | ||
sendAnswerHTTP(ctx, ResponseMessage{400, true, fmt.Sprintf("Integration token invalid: %s", hawkToken)}) | ||
return | ||
} | ||
log.Debugf("Found project with ID %s for integration token %s", projectId, hawkToken) | ||
|
||
if handler.RedisClient.IsBlocked(projectId) { | ||
handler.ErrorsBlockedByLimit.Inc() | ||
sendAnswerHTTP(ctx, ResponseMessage{402, true, "Project has exceeded the events limit"}) | ||
return | ||
} | ||
|
||
// send serialized message to a broker | ||
brokerMessage := broker.Message{Payload: jsonBody, Route: SentryQueueName} | ||
log.Debugf("Send to queue: %s", brokerMessage) | ||
handler.Broker.Chan <- brokerMessage | ||
|
||
// increment processed errors counter | ||
handler.ErrorsProcessed.Inc() | ||
|
||
sendAnswerHTTP(ctx, ResponseMessage{200, false, "OK"}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package errorshandler | ||
|
||
import ( | ||
"bytes" | ||
"compress/gzip" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"strings" | ||
) | ||
|
||
func decompressGzipString(gzipString []byte) ([]byte, error) { | ||
reader, err := gzip.NewReader(bytes.NewReader(gzipString)) | ||
if err != nil { | ||
return []byte(""), fmt.Errorf("failed to create gzip reader: %w", err) | ||
} | ||
defer reader.Close() | ||
|
||
var result bytes.Buffer | ||
_, err = io.Copy(&result, reader) | ||
if err != nil { | ||
return []byte(""), fmt.Errorf("failed to decompress data: %w", err) | ||
} | ||
|
||
return result.Bytes(), nil | ||
} | ||
|
||
func getSentryKeyFromAuth(auth string) (string, error) { | ||
auth = strings.TrimPrefix(auth, "Sentry ") | ||
pairs := strings.Split(auth, ", ") | ||
for _, pair := range pairs { | ||
kv := strings.SplitN(pair, "=", 2) | ||
if len(kv) == 2 && kv[0] == "sentry_key" { | ||
return kv[1], nil | ||
} | ||
} | ||
|
||
return "", errors.New("sentry_key not found") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters