Skip to content

Commit f0a377c

Browse files
authored
Merge pull request #107 from codex-team/new-token
Support of the Hawk Token verification and decoding
2 parents b487670 + ec75255 commit f0a377c

File tree

5 files changed

+52
-8
lines changed

5 files changed

+52
-8
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@
1414
.idea
1515
venv
1616
.DS_Store
17-
hawk.collector
17+
bin/hawk.collector
1818
.env

pkg/accounts/cache.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ package accounts
22

33
import (
44
"context"
5+
"encoding/base64"
6+
"encoding/json"
7+
"strings"
58
"time"
69

710
"go.mongodb.org/mongo-driver/bson/primitive"
@@ -14,6 +17,11 @@ import (
1417
const projectsCollectionName = "projects"
1518
const contextTimeout = 5 * time.Second
1619

20+
type acountToken struct {
21+
IntegrationId string `json:"integrationId"`
22+
Secret string `json:"secret"`
23+
}
24+
1725
type accountProject struct {
1826
ProjectID primitive.ObjectID `bson:"_id"`
1927
Token string `bson:"token"`
@@ -39,11 +47,33 @@ func (client *AccountsMongoDBClient) UpdateTokenCache() error {
3947

4048
client.ValidTokens = make(map[string]string)
4149
for _, project := range projects {
42-
client.ValidTokens[project.Token] = project.ProjectID.Hex()
50+
integrationSecret, err := DecodeToken(project.Token)
51+
if err == nil {
52+
client.ValidTokens[integrationSecret] = project.ProjectID.Hex()
53+
} else {
54+
log.Errorf("Integration token %s is invalid: %s", project.Token, err)
55+
}
4356
}
4457

4558
log.Debugf("Cache for MongoDB tokens successfully updates with %d tokens", len(client.ValidTokens))
4659
log.Tracef("Current token cache state: %s", client.ValidTokens)
4760

4861
return nil
4962
}
63+
64+
// decodeToken decodes token from base64 to integrationId + secret
65+
func DecodeToken(token string) (string, error) {
66+
decoded, err := base64.StdEncoding.DecodeString(token)
67+
if err != nil {
68+
return "", err
69+
}
70+
var data acountToken
71+
err = json.Unmarshal(decoded, &data)
72+
if err != nil {
73+
return "", err
74+
}
75+
76+
integrationId := strings.ReplaceAll(data.IntegrationId, "-", "")
77+
secret := strings.ReplaceAll(data.Secret, "-", "")
78+
return integrationId + secret, nil
79+
}

pkg/server/errorshandler/handler.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,18 @@ func (handler *Handler) process(body []byte) ResponseMessage {
5252
return ResponseMessage{400, true, "CatcherType is empty"}
5353
}
5454

55-
projectId, ok := handler.AccountsMongoDBClient.ValidTokens[message.Token]
55+
integrationSecret, err := accounts.DecodeToken(string(message.Token))
56+
if err != nil {
57+
log.Warnf("[release] Token decoding error: %s", err)
58+
return ResponseMessage{400, true, "Token decoding error"}
59+
}
60+
61+
projectId, ok := handler.AccountsMongoDBClient.ValidTokens[integrationSecret]
5662
if !ok {
57-
log.Debugf("Token %s is not in the accounts cache", message.Token)
58-
return ResponseMessage{400, true, fmt.Sprintf("Integration token invalid: %s", message.Token)}
63+
log.Debugf("Token %s is not in the accounts cache", integrationSecret)
64+
return ResponseMessage{400, true, fmt.Sprintf("Integration token invalid: %s", integrationSecret)}
5965
}
60-
log.Debugf("Found project with ID %s for integration token %s", projectId, message.Token)
66+
log.Debugf("Found project with ID %s for integration token %s", projectId, integrationSecret)
6167

6268
if handler.RedisClient.IsBlocked(projectId) {
6369
handler.ErrorsBlockedByLimit.Inc()

pkg/server/errorshandler/handler_sentry.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"github.com/valyala/fasthttp"
1010
)
1111

12-
const SentryQueueName = "errors/sentry"
12+
const SentryQueueName = "external/sentry"
1313
const CatcherType = "sentry"
1414

1515
// HandleHTTP processes HTTP requests with JSON body

pkg/server/releasehandler/handler_http.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"errors"
66

7+
"github.com/codex-team/hawk.collector/pkg/accounts"
78
"github.com/codex-team/hawk.collector/pkg/hawk"
89
log "github.com/sirupsen/logrus"
910
"github.com/valyala/fasthttp"
@@ -39,8 +40,15 @@ func (handler *Handler) HandleHTTP(ctx *fasthttp.RequestCtx) {
3940

4041
log.Debugf("[release] Multipart form with token: %s", token)
4142

43+
integrationSecret, err := accounts.DecodeToken(string(token))
44+
if err != nil {
45+
log.Warnf("[release] Token decoding error: %s", err)
46+
sendAnswerHTTP(ctx, ResponseMessage{400, true, "Token decoding error"})
47+
return
48+
}
49+
4250
// process raw body via unified sourcemap handler
43-
response := handler.process(form, string(token))
51+
response := handler.process(form, integrationSecret)
4452
log.Debugf("[release] Multipart form response: %s", response.Message)
4553

4654
sendAnswerHTTP(ctx, response)

0 commit comments

Comments
 (0)