From e71e56f7fee184a3a0c252b005dfd01e2e70dda9 Mon Sep 17 00:00:00 2001 From: James Houlahan Date: Tue, 29 Nov 2022 12:59:00 +0100 Subject: [PATCH] Other: Ensure context is string in sentry reports --- go.mod | 2 +- go.sum | 4 +- internal/bridge/bridge_test.go | 11 ++--- internal/bridge/main_test.go | 36 ++++++++++++++++ internal/bridge/sentry_test.go | 78 ++++++++++++++++++++++++++++++++++ internal/sentry/reporter.go | 12 +++++- 6 files changed, 131 insertions(+), 12 deletions(-) create mode 100644 internal/bridge/main_test.go create mode 100644 internal/bridge/sentry_test.go diff --git a/go.mod b/go.mod index a18c8490..44452413 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.18 require ( github.com/0xAX/notificator v0.0.0-20220220101646-ee9b8921e557 github.com/Masterminds/semver/v3 v3.1.1 - github.com/ProtonMail/gluon v0.14.1 + github.com/ProtonMail/gluon v0.14.2-0.20221129115938-cf5d444d3015 github.com/ProtonMail/go-autostart v0.0.0-20210130080809-00ed301c8e9a github.com/ProtonMail/go-proton-api v0.1.2 github.com/ProtonMail/go-rfc5322 v0.11.0 diff --git a/go.sum b/go.sum index 48637b42..c8be1e5b 100644 --- a/go.sum +++ b/go.sum @@ -28,8 +28,8 @@ github.com/ProtonMail/bcrypt v0.0.0-20211005172633-e235017c1baf h1:yc9daCCYUefEs github.com/ProtonMail/bcrypt v0.0.0-20211005172633-e235017c1baf/go.mod h1:o0ESU9p83twszAU8LBeJKFAAMX14tISa0yk4Oo5TOqo= github.com/ProtonMail/docker-credential-helpers v1.1.0 h1:+kvUIpwWcbtP3WFv5sSvkFn/XLzSqPOB5AAthuk9xPk= github.com/ProtonMail/docker-credential-helpers v1.1.0/go.mod h1:mK0aBveCxhnQ756AmaTfXMZDeULvheYVhF/MWMErN5g= -github.com/ProtonMail/gluon v0.14.1 h1:7iXLasYoBGaOW88k4p29w7bPAOpY6aR6GErdVRu8MZs= -github.com/ProtonMail/gluon v0.14.1/go.mod h1:z2AxLIiBCT1K+0OBHyaDI7AEaO5qI6/BEC2TE42vs4Q= +github.com/ProtonMail/gluon v0.14.2-0.20221129115938-cf5d444d3015 h1:fA6ggn06yfSXTppTjmlYeBk9lxeURx8ARBKLHId5v7Q= +github.com/ProtonMail/gluon v0.14.2-0.20221129115938-cf5d444d3015/go.mod h1:z2AxLIiBCT1K+0OBHyaDI7AEaO5qI6/BEC2TE42vs4Q= github.com/ProtonMail/go-autostart v0.0.0-20210130080809-00ed301c8e9a h1:D+aZah+k14Gn6kmL7eKxoo/4Dr/lK3ChBcwce2+SQP4= github.com/ProtonMail/go-autostart v0.0.0-20210130080809-00ed301c8e9a/go.mod h1:oTGdE7/DlWIr23G0IKW3OXK9wZ5Hw1GGiaJFccTvZi4= github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7/go.mod h1:z4/9nQmJSSwwds7ejkxaJwO37dru3geImFUdJlaLzQo= diff --git a/internal/bridge/bridge_test.go b/internal/bridge/bridge_test.go index 60e8bdd9..48698ed5 100644 --- a/internal/bridge/bridge_test.go +++ b/internal/bridge/bridge_test.go @@ -45,7 +45,6 @@ import ( "github.com/ProtonMail/proton-bridge/v3/tests" "github.com/bradenaw/juniper/xslices" "github.com/stretchr/testify/require" - "go.uber.org/goleak" ) var ( @@ -56,10 +55,6 @@ var ( v2_4_0 = semver.MustParse("2.4.0") ) -func TestMain(m *testing.M) { - goleak.VerifyTestMain(m, goleak.IgnoreCurrent()) -} - func init() { user.EventPeriod = 100 * time.Millisecond user.EventJitter = 0 @@ -561,9 +556,9 @@ func withBridge( mocks.Reporter, // The logging stuff. - false, - false, - false, + os.Getenv("BRIDGE_LOG_IMAP_CLIENT") == "1", + os.Getenv("BRIDGE_LOG_IMAP_SERVER") == "1", + os.Getenv("BRIDGE_LOG_SMTP") == "1", ) require.NoError(t, err) require.Empty(t, bridge.GetErrors()) diff --git a/internal/bridge/main_test.go b/internal/bridge/main_test.go new file mode 100644 index 00000000..b63aa3e6 --- /dev/null +++ b/internal/bridge/main_test.go @@ -0,0 +1,36 @@ +// Copyright (c) 2022 Proton AG +// +// This file is part of Proton Mail Bridge. +// +// Proton Mail Bridge is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Proton Mail Bridge is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Proton Mail Bridge. If not, see . + +package bridge_test + +import ( + "os" + "testing" + + "github.com/sirupsen/logrus" + "go.uber.org/goleak" +) + +func TestMain(m *testing.M) { + if level := os.Getenv("BRIDGE_LOG_LEVEL"); level != "" { + if parsed, err := logrus.ParseLevel(level); err == nil { + logrus.SetLevel(parsed) + } + } + + goleak.VerifyTestMain(m, goleak.IgnoreCurrent()) +} diff --git a/internal/bridge/sentry_test.go b/internal/bridge/sentry_test.go new file mode 100644 index 00000000..a85cdb49 --- /dev/null +++ b/internal/bridge/sentry_test.go @@ -0,0 +1,78 @@ +// Copyright (c) 2022 Proton AG +// +// This file is part of Proton Mail Bridge. +// +// Proton Mail Bridge is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Proton Mail Bridge is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Proton Mail Bridge. If not, see . + +package bridge_test + +import ( + "context" + "fmt" + "net" + "testing" + + "github.com/ProtonMail/gluon/liner" + "github.com/ProtonMail/go-proton-api" + "github.com/ProtonMail/go-proton-api/server" + "github.com/ProtonMail/proton-bridge/v3/internal/bridge" + "github.com/ProtonMail/proton-bridge/v3/internal/constants" + "github.com/ProtonMail/proton-bridge/v3/internal/events" + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/require" +) + +func TestBridge_Report(t *testing.T) { + withEnv(t, func(ctx context.Context, s *server.Server, netCtl *proton.NetCtl, locator bridge.Locator, storeKey []byte) { + withBridge(ctx, t, s.GetHostURL(), netCtl, locator, storeKey, func(b *bridge.Bridge, mocks *bridge.Mocks) { + syncCh, done := chToType[events.Event, events.SyncFinished](b.GetEvents(events.SyncFinished{})) + defer done() + + // Log in the user. + userID, err := b.LoginFull(ctx, username, password, nil, nil) + require.NoError(t, err) + + // Wait until the sync has finished. + require.Equal(t, userID, (<-syncCh).UserID) + + // Get the IMAP info. + info, err := b.GetUserInfo(userID) + require.NoError(t, err) + require.True(t, info.State == bridge.Connected) + + // Dial the IMAP port. + conn, err := net.Dial("tcp", fmt.Sprintf("%v:%v", constants.Host, b.GetIMAPPort())) + require.NoError(t, err) + defer func() { require.NoError(t, conn.Close()) }() + + // Sending garbage to the IMAP port should cause the bridge to report it. + mocks.Reporter.EXPECT().ReportMessageWithContext( + gomock.Eq("Failed to parse imap command"), + gomock.Any(), + ).Return(nil) + + // Read lines from the IMAP port. + lineCh := liner.New(conn).Lines(func() error { return nil }) + + // On connection, we should get the greeting. + require.Contains(t, string((<-lineCh).Line), "* OK") + + // Send garbage data. + must(conn.Write([]byte("tag garbage\r\n"))) + + // Bridge will reply with BAD. + require.Contains(t, string((<-lineCh).Line), "tag BAD") + }) + }) +} diff --git a/internal/sentry/reporter.go b/internal/sentry/reporter.go index b4a23c5e..bbae1732 100644 --- a/internal/sentry/reporter.go +++ b/internal/sentry/reporter.go @@ -138,7 +138,7 @@ func (r *Reporter) scopedReport(context map[string]interface{}, doReport func()) scope.SetTags(tags) if len(context) != 0 { scope.SetContexts( - map[string]map[string]interface{}{"bridge": context}, + map[string]sentry.Context{"bridge": contextToString(context)}, ) } doReport() @@ -204,3 +204,13 @@ func isFunctionFilteredOut(function string) bool { func Flush(maxWaiTime time.Duration) { sentry.Flush(maxWaiTime) } + +func contextToString(context sentry.Context) sentry.Context { + res := make(sentry.Context) + + for k, v := range context { + res[k] = fmt.Sprintf("%v", v) + } + + return res +}