Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: hide credentials in tg sender #1107

Merged
merged 4 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions senders/telegram/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ func (sender *Sender) Init(senderSettings interface{}, logger moira.Logger, loca

sender.logger = logger
sender.bot, err = telebot.NewBot(telebot.Settings{
Token: cfg.APIToken,
Poller: &telebot.LongPoller{Timeout: pollerTimeout},
Token: cfg.APIToken,
Poller: &telebot.LongPoller{Timeout: pollerTimeout},
OnError: sender.customOnErrorFunc,
})
if err != nil {
return sender.removeTokenFromError(err)
Expand Down Expand Up @@ -123,3 +124,13 @@ func (sender *Sender) runTelebot(contactType string) {
func telegramLockKey(contactType string) string {
return telegramLockPrefix + contactType
}

const errorInsideTelebotMsg = "Error inside telebot"

func (sender *Sender) customOnErrorFunc(err error, _ telebot.Context) {
err = sender.removeTokenFromError(err)

sender.logger.Warning().
Error(err).
Msg(errorInsideTelebotMsg)
}
28 changes: 28 additions & 0 deletions senders/telegram/init_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package telegram

import (
"errors"
"fmt"
"strings"
"testing"
"time"

mock_moira_alert "github.com/moira-alert/moira/mock/moira-alert"
"go.uber.org/mock/gomock"

logging "github.com/moira-alert/moira/logging/zerolog_adapter"
. "github.com/smartystreets/goconvey/convey"
)
Expand All @@ -31,3 +36,26 @@ func TestInit(t *testing.T) {
})
})
}

func Test_customOnErrorFunc(t *testing.T) {
Convey("test customOnErrorFunc hides credential and logs", t, func() {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

logger := mock_moira_alert.NewMockLogger(mockCtrl)
eventsBuilder := mock_moira_alert.NewMockEventBuilder(mockCtrl)

sender := Sender{
logger: logger,
apiToken: "1111111111:SecretTokenabc_987654321hellokonturmoira",
}

err := fmt.Errorf("https://some.api.of.telegram/bot%s/update failed to update", sender.apiToken)

logger.EXPECT().Warning().Return(eventsBuilder).AnyTimes()
eventsBuilder.EXPECT().Error(errors.New(strings.ReplaceAll(err.Error(), sender.apiToken, hidden))).Return(eventsBuilder)
eventsBuilder.EXPECT().Msg(errorInsideTelebotMsg)

sender.customOnErrorFunc(err, nil)
})
}
Loading