Skip to content

Commit a5ea430

Browse files
authored
Merge pull request #146 from sil-org/sentry
set up Sentry logger to report all 500 errors
2 parents 860efa4 + bab477d commit a5ea430

File tree

8 files changed

+61
-15
lines changed

8 files changed

+61
-15
lines changed

.github/workflows/test-deploy-publish.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ jobs:
9494
WEBAUTHN_TABLE: ${{ vars.WEBAUTHN_TABLE }}
9595
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
9696
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
97+
SENTRY_DSN: ${{ secrets.SENTRY_DSN }}
9798
run: cd cdk && cdk deploy --require-approval never
9899

99100
build-and-publish:

cdk/cdk.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ func NewCdkStack(scope constructs.Construct, id string, props *CdkStackProps) aw
4848
"TOTP_TABLE": jsii.String(totpTable),
4949
"WEBAUTHN_TABLE": jsii.String(webauthnTable),
5050
"AWS_ENDPOINT": jsii.String(""),
51+
"SENTRY_DSN": jsii.String(os.Getenv("SENTRY_DSN")),
5152
},
5253
FunctionName: &functionName,
5354
Handler: jsii.String("bootstrap"),

config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ type EnvConfig struct {
2222
AwsDefaultRegion string `default:"" split_words:"true"`
2323

2424
AWSConfig aws.Config `json:"-"`
25+
26+
SentryDSN string `split_words:"true"`
2527
}
2628

2729
func (e *EnvConfig) InitAWS() {

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue v1.19.2
1010
github.com/aws/aws-sdk-go-v2/service/dynamodb v1.43.3
1111
github.com/fxamacker/cbor/v2 v2.8.0
12+
github.com/getsentry/sentry-go v0.35.3
1213
github.com/go-webauthn/webauthn v0.11.2
1314
github.com/google/uuid v1.6.0
1415
github.com/kelseyhightower/envconfig v1.4.0
@@ -42,6 +43,6 @@ require (
4243
github.com/rogpeppe/go-internal v1.14.1 // indirect
4344
github.com/x448/float16 v0.8.4 // indirect
4445
golang.org/x/sys v0.33.0 // indirect
45-
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
46+
golang.org/x/text v0.26.0 // indirect
4647
gopkg.in/yaml.v3 v3.0.1 // indirect
4748
)

go.sum

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,28 +42,35 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
4242
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4343
github.com/fxamacker/cbor/v2 v2.8.0 h1:fFtUGXUzXPHTIUdne5+zzMPTfffl3RD5qYnkY40vtxU=
4444
github.com/fxamacker/cbor/v2 v2.8.0/go.mod h1:vM4b+DJCtHn+zz7h3FFp/hDAI9WNWCsZj23V5ytsSxQ=
45+
github.com/getsentry/sentry-go v0.35.3 h1:u5IJaEqZyPdWqe/hKlBKBBnMTSxB/HenCqF3QLabeds=
46+
github.com/getsentry/sentry-go v0.35.3/go.mod h1:mdL49ixwT2yi57k5eh7mpnDyPybixPzlzEJFu0Z76QA=
47+
github.com/go-errors/errors v1.4.2 h1:J6MZopCL4uSllY1OfXM374weqZFFItUbrImctkmUxIA=
48+
github.com/go-errors/errors v1.4.2/go.mod h1:sIVyrIiJhuEF+Pj9Ebtd6P/rEYROXFi3BopGUQ5a5Og=
4549
github.com/go-webauthn/webauthn v0.11.2 h1:Fgx0/wlmkClTKlnOsdOQ+K5HcHDsDcYIvtYmfhEOSUc=
4650
github.com/go-webauthn/webauthn v0.11.2/go.mod h1:aOtudaF94pM71g3jRwTYYwQTG1KyTILTcZqN1srkmD0=
4751
github.com/go-webauthn/x v0.1.21 h1:nFbckQxudvHEJn2uy1VEi713MeSpApoAv9eRqsb9AdQ=
4852
github.com/go-webauthn/x v0.1.21/go.mod h1:sEYohtg1zL4An1TXIUIQ5csdmoO+WO0R4R2pGKaHYKA=
4953
github.com/golang-jwt/jwt/v5 v5.2.2 h1:Rl4B7itRWVtYIHFrSNd7vhTiz9UpLdi6gZhZ3wEeDy8=
5054
github.com/golang-jwt/jwt/v5 v5.2.2/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
55+
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
56+
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
5157
github.com/google/go-tpm v0.9.3 h1:+yx0/anQuGzi+ssRqeD6WpXjW2L/V0dItUayO0i9sRc=
5258
github.com/google/go-tpm v0.9.3/go.mod h1:h9jEsEECg7gtLis0upRBQU+GhYVH6jMjrFxI8u6bVUY=
5359
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
5460
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
5561
github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8=
5662
github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg=
57-
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
5863
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
5964
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
60-
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
61-
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
6265
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
6366
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
6467
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
6568
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
69+
github.com/pingcap/errors v0.11.4 h1:lFuQV/oaUMGcD2tqt+01ROSmJs75VG1ToEOkZIZ4nE4=
70+
github.com/pingcap/errors v0.11.4/go.mod h1:Oi8TUi2kEtXXLMJk9l1cGmz20kV3TaQ0usTwv5KuLY8=
6671
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
72+
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
73+
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
6774
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
6875
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
6976
github.com/pquerna/otp v1.5.0 h1:NMMR+WrmaqXU4EzdGJEE1aUUI0AMRzsp96fFFWNPwxs=
@@ -77,10 +84,14 @@ github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOf
7784
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
7885
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
7986
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
87+
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
88+
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
8089
golang.org/x/crypto v0.39.0 h1:SHs+kF4LP+f+p14esP5jAoDpHU8Gu/v9lFRK6IT5imM=
8190
golang.org/x/crypto v0.39.0/go.mod h1:L+Xg3Wf6HoL4Bn4238Z6ft6KfEpN0tJGo53AAPC632U=
8291
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
8392
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
93+
golang.org/x/text v0.26.0 h1:P42AVeLghgTYr4+xUnTRKDMqpar+PtX7KWuNQL21L8M=
94+
golang.org/x/text v0.26.0/go.mod h1:QK15LZJUUQVJxhz7wXgxSy/CJaTFjd0G+YLonydOVQA=
8495
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
8596
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
8697
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=

lambda/main.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ import (
88
"net/url"
99
"os"
1010
"strings"
11+
"time"
1112

1213
"github.com/aws/aws-lambda-go/events"
1314
"github.com/aws/aws-lambda-go/lambda"
15+
"github.com/getsentry/sentry-go"
1416
"github.com/kelseyhightower/envconfig"
1517

1618
mfa "github.com/silinternational/serverless-mfa-api-go"
@@ -39,6 +41,8 @@ func main() {
3941
envConfig.InitAWS()
4042
mfa.SetConfig(envConfig)
4143

44+
sentryInit()
45+
4246
lambda.Start(handler)
4347
}
4448

@@ -56,6 +60,12 @@ func handler(ctx context.Context, req events.APIGatewayProxyRequest) (events.API
5660
headers[k] = v[0]
5761
}
5862

63+
if w.Status == http.StatusInternalServerError && envConfig.SentryDSN != "" {
64+
logger := sentry.NewLogger(ctx)
65+
logger.Error().Emit(string(w.Body))
66+
defer sentry.Flush(2 * time.Second)
67+
}
68+
5969
return events.APIGatewayProxyResponse{
6070
StatusCode: w.Status,
6171
Headers: headers,
@@ -82,3 +92,16 @@ func httpRequestFromProxyRequest(ctx context.Context, req events.APIGatewayProxy
8292

8393
return r.WithContext(ctx)
8494
}
95+
96+
func sentryInit() {
97+
if envConfig.SentryDSN == "" {
98+
return
99+
}
100+
101+
if err := sentry.Init(sentry.ClientOptions{
102+
Dsn: envConfig.SentryDSN,
103+
EnableLogs: true,
104+
}); err != nil {
105+
log.Printf("Sentry initialization failed: %v\n", err)
106+
}
107+
}

lambda/main_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package main
2+
3+
import "testing"
4+
5+
func Test_sentryInit(t *testing.T) {
6+
sentryInit()
7+
}

router/router.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@ func NewMux(app *mfa.App) *http.ServeMux {
1919
// getRoutes returns a list of routes for the server
2020
func getRoutes(app *mfa.App) map[string]http.HandlerFunc {
2121
return map[string]http.HandlerFunc{
22-
"POST /api-key/activate": app.ActivateApiKey,
23-
"POST /api-key/rotate": app.RotateApiKey,
24-
"POST /api-key": app.CreateApiKey,
25-
"POST /totp": app.CreateTOTP,
26-
"DELETE /totp/{" + mfa.UUIDParam + "}": app.DeleteTOTP,
27-
"POST /totp/{" + mfa.UUIDParam + "}/validate": app.ValidateTOTP,
28-
"POST /webauthn/register": app.BeginRegistration,
29-
"PUT /webauthn/register": app.FinishRegistration,
30-
"POST /webauthn/login": app.BeginLogin,
31-
"PUT /webauthn/login": app.FinishLogin,
32-
"DELETE /webauthn/user": app.DeleteUser,
22+
"POST /api-key/activate": app.ActivateApiKey,
23+
"POST /api-key/rotate": app.RotateApiKey,
24+
"POST /api-key": app.CreateApiKey,
25+
"POST /totp": app.CreateTOTP,
26+
"DELETE /totp/{" + mfa.UUIDParam + "}": app.DeleteTOTP,
27+
"POST /totp/{" + mfa.UUIDParam + "}/validate": app.ValidateTOTP,
28+
"POST /webauthn/register": app.BeginRegistration,
29+
"PUT /webauthn/register": app.FinishRegistration,
30+
"POST /webauthn/login": app.BeginLogin,
31+
"PUT /webauthn/login": app.FinishLogin,
32+
"DELETE /webauthn/user": app.DeleteUser,
3333
"DELETE /webauthn/credential/{" + mfa.IDParam + "}": app.DeleteCredential,
3434
}
3535
}

0 commit comments

Comments
 (0)