Skip to content

Commit 396646a

Browse files
authored
fix: standardise logging format to use json (#562)
* fix: standardise logging to use json formatter * sample logging format * fix: disable default timestamp * update sample logging format * refactor: pass request into NewAuditLogEntry * refactor: remove additional logging * test: change token rate limit to 30
1 parent 36ff42d commit 396646a

24 files changed

+101
-81
lines changed

api/admin.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/go-chi/chi"
1212
"github.com/gofrs/uuid"
13+
"github.com/netlify/gotrue/logger"
1314
"github.com/netlify/gotrue/models"
1415
"github.com/netlify/gotrue/storage"
1516
"github.com/sethvargo/go-password/password"
@@ -34,7 +35,7 @@ func (a *API) loadUser(w http.ResponseWriter, r *http.Request) (context.Context,
3435
return nil, badRequestError("user_id must be an UUID")
3536
}
3637

37-
logEntrySetField(r, "user_id", userID)
38+
logger.LogEntrySetField(r, "user_id", userID)
3839
instanceID := getInstanceID(r.Context())
3940

4041
u, err := models.FindUserByInstanceIDAndID(a.db, instanceID, userID)
@@ -175,7 +176,7 @@ func (a *API) adminUserUpdate(w http.ResponseWriter, r *http.Request) error {
175176
}
176177
}
177178

178-
if terr := models.NewAuditLogEntry(tx, instanceID, adminUser, models.UserModifiedAction, "", map[string]interface{}{
179+
if terr := models.NewAuditLogEntry(r, tx, instanceID, adminUser, models.UserModifiedAction, "", map[string]interface{}{
179180
"user_id": user.ID,
180181
"user_email": user.Email,
181182
"user_phone": user.Phone,
@@ -270,7 +271,7 @@ func (a *API) adminUserCreate(w http.ResponseWriter, r *http.Request) error {
270271
}
271272

272273
err = a.db.Transaction(func(tx *storage.Connection) error {
273-
if terr := models.NewAuditLogEntry(tx, instanceID, adminUser, models.UserSignedUpAction, "", map[string]interface{}{
274+
if terr := models.NewAuditLogEntry(r, tx, instanceID, adminUser, models.UserSignedUpAction, "", map[string]interface{}{
274275
"user_id": user.ID,
275276
"user_email": user.Email,
276277
"user_phone": user.Phone,
@@ -323,7 +324,7 @@ func (a *API) adminUserDelete(w http.ResponseWriter, r *http.Request) error {
323324
adminUser := getAdminUser(ctx)
324325

325326
err := a.db.Transaction(func(tx *storage.Connection) error {
326-
if terr := models.NewAuditLogEntry(tx, instanceID, adminUser, models.UserDeletedAction, "", map[string]interface{}{
327+
if terr := models.NewAuditLogEntry(r, tx, instanceID, adminUser, models.UserDeletedAction, "", map[string]interface{}{
327328
"user_id": user.ID,
328329
"user_email": user.Email,
329330
"user_phone": user.Phone,

api/api.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/gofrs/uuid"
1616
"github.com/imdario/mergo"
1717
"github.com/netlify/gotrue/conf"
18+
"github.com/netlify/gotrue/logger"
1819
"github.com/netlify/gotrue/mailer"
1920
"github.com/netlify/gotrue/storage"
2021
"github.com/rs/cors"
@@ -81,7 +82,7 @@ func NewAPIWithVersion(ctx context.Context, globalConfig *conf.GlobalConfigurati
8182
api := &API{config: globalConfig, db: db, version: version}
8283

8384
xffmw, _ := xff.Default()
84-
logger := newStructuredLogger(logrus.StandardLogger())
85+
logger := logger.NewStructuredLogger(logrus.StandardLogger())
8586

8687
r := newRouter()
8788
r.UseBypass(xffmw.Handler)

api/errors.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"runtime/debug"
99

1010
"github.com/netlify/gotrue/conf"
11+
"github.com/netlify/gotrue/logger"
1112
"github.com/netlify/gotrue/utilities"
1213
"github.com/pkg/errors"
1314
)
@@ -214,7 +215,7 @@ func recoverer(w http.ResponseWriter, r *http.Request) (context.Context, error)
214215
defer func() {
215216
if rvr := recover(); rvr != nil {
216217

217-
logEntry := getLogEntry(r)
218+
logEntry := logger.GetLogEntry(r)
218219
if logEntry != nil {
219220
logEntry.Panic(rvr, debug.Stack())
220221
} else {
@@ -239,7 +240,7 @@ type ErrorCause interface {
239240
}
240241

241242
func handleError(err error, w http.ResponseWriter, r *http.Request) {
242-
log := getLogEntry(r)
243+
log := logger.GetLogEntry(r)
243244
errorID := getRequestID(r.Context())
244245
switch e := err.(type) {
245246
case *HTTPError:

api/external.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
jwt "github.com/golang-jwt/jwt"
1515
"github.com/netlify/gotrue/api/provider"
1616
"github.com/netlify/gotrue/conf"
17+
"github.com/netlify/gotrue/logger"
1718
"github.com/netlify/gotrue/models"
1819
"github.com/netlify/gotrue/storage"
1920
"github.com/netlify/gotrue/utilities"
@@ -60,7 +61,7 @@ func (a *API) ExternalProviderRedirect(w http.ResponseWriter, r *http.Request) e
6061
}
6162

6263
redirectURL := a.getRedirectURLOrReferrer(r, query.Get("redirect_to"))
63-
log := getLogEntry(r)
64+
log := logger.GetLogEntry(r)
6465
log.WithField("provider", providerType).Info("Redirecting to external provider")
6566

6667
token := jwt.NewWithClaims(jwt.SigningMethodHS256, ExternalProviderClaims{
@@ -140,7 +141,7 @@ func (a *API) internalExternalProviderCallback(w http.ResponseWriter, r *http.Re
140141
var terr error
141142
inviteToken := getInviteToken(ctx)
142143
if inviteToken != "" {
143-
if user, terr = a.processInvite(ctx, tx, userData, instanceID, inviteToken, providerType); terr != nil {
144+
if user, terr = a.processInvite(r, ctx, tx, userData, instanceID, inviteToken, providerType); terr != nil {
144145
return terr
145146
}
146147
} else {
@@ -255,7 +256,7 @@ func (a *API) internalExternalProviderCallback(w http.ResponseWriter, r *http.Re
255256
return nil
256257
}
257258

258-
if terr := models.NewAuditLogEntry(tx, instanceID, user, models.UserSignedUpAction, "", map[string]interface{}{
259+
if terr := models.NewAuditLogEntry(r, tx, instanceID, user, models.UserSignedUpAction, "", map[string]interface{}{
259260
"provider": providerType,
260261
}); terr != nil {
261262
return terr
@@ -269,7 +270,7 @@ func (a *API) internalExternalProviderCallback(w http.ResponseWriter, r *http.Re
269270
return internalServerError("Error updating user").WithInternalError(terr)
270271
}
271272
} else {
272-
if terr := models.NewAuditLogEntry(tx, instanceID, user, models.LoginAction, "", map[string]interface{}{
273+
if terr := models.NewAuditLogEntry(r, tx, instanceID, user, models.LoginAction, "", map[string]interface{}{
273274
"provider": providerType,
274275
}); terr != nil {
275276
return terr
@@ -311,7 +312,7 @@ func (a *API) internalExternalProviderCallback(w http.ResponseWriter, r *http.Re
311312
return nil
312313
}
313314

314-
func (a *API) processInvite(ctx context.Context, tx *storage.Connection, userData *provider.UserProvidedData, instanceID uuid.UUID, inviteToken, providerType string) (*models.User, error) {
315+
func (a *API) processInvite(r *http.Request, ctx context.Context, tx *storage.Connection, userData *provider.UserProvidedData, instanceID uuid.UUID, inviteToken, providerType string) (*models.User, error) {
315316
config := a.getConfig(ctx)
316317
user, err := models.FindUserByConfirmationToken(tx, inviteToken)
317318
if err != nil {
@@ -357,7 +358,7 @@ func (a *API) processInvite(ctx context.Context, tx *storage.Connection, userDat
357358
return nil, internalServerError("Database error updating user").WithInternalError(err)
358359
}
359360

360-
if err := models.NewAuditLogEntry(tx, instanceID, user, models.InviteAcceptedAction, "", map[string]interface{}{
361+
if err := models.NewAuditLogEntry(r, tx, instanceID, user, models.InviteAcceptedAction, "", map[string]interface{}{
361362
"provider": providerType,
362363
}); err != nil {
363364
return nil, err
@@ -443,7 +444,7 @@ func (a *API) Provider(ctx context.Context, name string, scopes string, query *u
443444

444445
func (a *API) redirectErrors(handler apiHandler, w http.ResponseWriter, r *http.Request) {
445446
ctx := r.Context()
446-
log := getLogEntry(r)
447+
log := logger.GetLogEntry(r)
447448
errorID := getRequestID(ctx)
448449
err := handler(w, r)
449450
if err != nil {

api/external_oauth.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/mrjones/oauth"
99
"github.com/netlify/gotrue/api/provider"
10+
"github.com/netlify/gotrue/logger"
1011
"github.com/netlify/gotrue/storage"
1112
"github.com/sirupsen/logrus"
1213
)
@@ -66,7 +67,7 @@ func (a *API) oAuthCallback(ctx context.Context, r *http.Request, providerType s
6667
return nil, badRequestError("Unsupported provider: %+v", err).WithInternalError(err)
6768
}
6869

69-
log := getLogEntry(r)
70+
log := logger.GetLogEntry(r)
7071
log.WithFields(logrus.Fields{
7172
"provider": providerType,
7273
"code": oauthCode,

api/instance.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/go-chi/chi"
99
"github.com/gofrs/uuid"
1010
"github.com/netlify/gotrue/conf"
11+
"github.com/netlify/gotrue/logger"
1112
"github.com/netlify/gotrue/models"
1213
"github.com/pkg/errors"
1314
)
@@ -17,7 +18,7 @@ func (a *API) loadInstance(w http.ResponseWriter, r *http.Request) (context.Cont
1718
if err != nil {
1819
return nil, badRequestError("Invalid instance ID")
1920
}
20-
logEntrySetField(r, "instance_id", instanceID)
21+
logger.LogEntrySetField(r, "instance_id", instanceID)
2122

2223
i, err := models.GetInstance(a.db, instanceID)
2324
if err != nil {

api/invite.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (a *API) Invite(w http.ResponseWriter, r *http.Request) error {
5656
}
5757
}
5858

59-
if terr := models.NewAuditLogEntry(tx, instanceID, adminUser, models.UserInvitedAction, "", map[string]interface{}{
59+
if terr := models.NewAuditLogEntry(r, tx, instanceID, adminUser, models.UserInvitedAction, "", map[string]interface{}{
6060
"user_id": user.ID,
6161
"user_email": user.Email,
6262
}); terr != nil {

api/logout.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func (a *API) Logout(w http.ResponseWriter, r *http.Request) error {
2121
}
2222

2323
err = a.db.Transaction(func(tx *storage.Connection) error {
24-
if terr := models.NewAuditLogEntry(tx, instanceID, u, models.LogoutAction, "", nil); terr != nil {
24+
if terr := models.NewAuditLogEntry(r, tx, instanceID, u, models.LogoutAction, "", nil); terr != nil {
2525
return terr
2626
}
2727
return models.Logout(tx, instanceID, u.ID)

api/magic_link.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func (a *API) MagicLink(w http.ResponseWriter, r *http.Request) error {
9797
}
9898

9999
err = a.db.Transaction(func(tx *storage.Connection) error {
100-
if terr := models.NewAuditLogEntry(tx, instanceID, user, models.UserRecoveryRequestedAction, "", nil); terr != nil {
100+
if terr := models.NewAuditLogEntry(r, tx, instanceID, user, models.UserRecoveryRequestedAction, "", nil); terr != nil {
101101
return terr
102102
}
103103

api/mail.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func (a *API) GenerateLink(w http.ResponseWriter, r *http.Request) error {
7979
var terr error
8080
switch params.Type {
8181
case magicLinkVerification, recoveryVerification:
82-
if terr = models.NewAuditLogEntry(tx, instanceID, user, models.UserRecoveryRequestedAction, "", nil); terr != nil {
82+
if terr = models.NewAuditLogEntry(r, tx, instanceID, user, models.UserRecoveryRequestedAction, "", nil); terr != nil {
8383
return terr
8484
}
8585
user.RecoveryToken = hashedToken
@@ -102,7 +102,7 @@ func (a *API) GenerateLink(w http.ResponseWriter, r *http.Request) error {
102102
return terr
103103
}
104104
}
105-
if terr = models.NewAuditLogEntry(tx, instanceID, adminUser, models.UserInvitedAction, "", map[string]interface{}{
105+
if terr = models.NewAuditLogEntry(r, tx, instanceID, adminUser, models.UserInvitedAction, "", map[string]interface{}{
106106
"user_id": user.ID,
107107
"user_email": user.Email,
108108
}); terr != nil {

0 commit comments

Comments
 (0)