Chore: add context to login (#41316)
* Chore: add context to login attempt file and tests * Chore: add context * Chore: add context to login and login tests * Chore: continue adding context to login * Chore: add context to login query
This commit is contained in:
@@ -50,11 +50,11 @@ func (srv *CleanUpService) Run(ctx context.Context) error {
|
||||
srv.deleteExpiredSnapshots()
|
||||
srv.deleteExpiredDashboardVersions()
|
||||
srv.cleanUpOldAnnotations(ctxWithTimeout)
|
||||
srv.expireOldUserInvites()
|
||||
srv.deleteStaleShortURLs()
|
||||
srv.expireOldUserInvites(ctx)
|
||||
srv.deleteStaleShortURLs(ctx)
|
||||
err := srv.ServerLockService.LockAndExecute(ctx, "delete old login attempts",
|
||||
time.Minute*10, func(context.Context) {
|
||||
srv.deleteOldLoginAttempts()
|
||||
srv.deleteOldLoginAttempts(ctx)
|
||||
})
|
||||
if err != nil {
|
||||
srv.log.Error("failed to lock and execute cleanup of old login attempts", "error", err)
|
||||
@@ -143,7 +143,7 @@ func (srv *CleanUpService) deleteExpiredDashboardVersions() {
|
||||
}
|
||||
}
|
||||
|
||||
func (srv *CleanUpService) deleteOldLoginAttempts() {
|
||||
func (srv *CleanUpService) deleteOldLoginAttempts(ctx context.Context) {
|
||||
if srv.Cfg.DisableBruteForceLoginProtection {
|
||||
return
|
||||
}
|
||||
@@ -151,31 +151,31 @@ func (srv *CleanUpService) deleteOldLoginAttempts() {
|
||||
cmd := models.DeleteOldLoginAttemptsCommand{
|
||||
OlderThan: time.Now().Add(time.Minute * -10),
|
||||
}
|
||||
if err := bus.Dispatch(&cmd); err != nil {
|
||||
if err := bus.DispatchCtx(ctx, &cmd); err != nil {
|
||||
srv.log.Error("Problem deleting expired login attempts", "error", err.Error())
|
||||
} else {
|
||||
srv.log.Debug("Deleted expired login attempts", "rows affected", cmd.DeletedRows)
|
||||
}
|
||||
}
|
||||
|
||||
func (srv *CleanUpService) expireOldUserInvites() {
|
||||
func (srv *CleanUpService) expireOldUserInvites(ctx context.Context) {
|
||||
maxInviteLifetime := srv.Cfg.UserInviteMaxLifetime
|
||||
|
||||
cmd := models.ExpireTempUsersCommand{
|
||||
OlderThan: time.Now().Add(-maxInviteLifetime),
|
||||
}
|
||||
if err := bus.Dispatch(&cmd); err != nil {
|
||||
if err := bus.DispatchCtx(ctx, &cmd); err != nil {
|
||||
srv.log.Error("Problem expiring user invites", "error", err.Error())
|
||||
} else {
|
||||
srv.log.Debug("Expired user invites", "rows affected", cmd.NumExpired)
|
||||
}
|
||||
}
|
||||
|
||||
func (srv *CleanUpService) deleteStaleShortURLs() {
|
||||
func (srv *CleanUpService) deleteStaleShortURLs(ctx context.Context) {
|
||||
cmd := models.DeleteShortUrlCommand{
|
||||
OlderThan: time.Now().Add(-time.Hour * 24 * 7),
|
||||
}
|
||||
if err := srv.ShortURLService.DeleteStaleShortURLs(context.Background(), &cmd); err != nil {
|
||||
if err := srv.ShortURLService.DeleteStaleShortURLs(ctx, &cmd); err != nil {
|
||||
srv.log.Error("Problem deleting stale short urls", "error", err.Error())
|
||||
} else {
|
||||
srv.log.Debug("Deleted short urls", "rows affected", cmd.NumDeleted)
|
||||
|
||||
@@ -272,7 +272,7 @@ func (h *ContextHandler) initContextWithBasicAuth(reqContext *models.ReqContext,
|
||||
Password: password,
|
||||
Cfg: h.Cfg,
|
||||
}
|
||||
if err := bus.Dispatch(&authQuery); err != nil {
|
||||
if err := bus.DispatchCtx(reqContext.Req.Context(), &authQuery); err != nil {
|
||||
reqContext.Logger.Debug(
|
||||
"Failed to authorize the user",
|
||||
"username", username,
|
||||
|
||||
@@ -13,15 +13,15 @@ import (
|
||||
|
||||
var getTime = time.Now
|
||||
|
||||
func (s *Implementation) GetExternalUserInfoByLogin(query *models.GetExternalUserInfoByLoginQuery) error {
|
||||
func (s *Implementation) GetExternalUserInfoByLogin(ctx context.Context, query *models.GetExternalUserInfoByLoginQuery) error {
|
||||
userQuery := models.GetUserByLoginQuery{LoginOrEmail: query.LoginOrEmail}
|
||||
err := s.Bus.Dispatch(&userQuery)
|
||||
err := s.Bus.DispatchCtx(ctx, &userQuery)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
authInfoQuery := &models.GetAuthInfoQuery{UserId: userQuery.Result.Id}
|
||||
if err := s.Bus.Dispatch(authInfoQuery); err != nil {
|
||||
if err := s.Bus.DispatchCtx(context.TODO(), authInfoQuery); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ func ProvideAuthInfoService(bus bus.Bus, store *sqlstore.SQLStore, userProtectio
|
||||
logger: log.New("login.authinfo"),
|
||||
}
|
||||
|
||||
s.Bus.AddHandler(s.GetExternalUserInfoByLogin)
|
||||
s.Bus.AddHandlerCtx(s.GetExternalUserInfoByLogin)
|
||||
s.Bus.AddHandler(s.GetAuthInfo)
|
||||
s.Bus.AddHandler(s.SetAuthInfo)
|
||||
s.Bus.AddHandler(s.UpdateAuthInfo)
|
||||
|
||||
@@ -32,7 +32,7 @@ func ProvideService(bus bus.Bus, cfg *setting.Cfg) (*NotificationService, error)
|
||||
}
|
||||
|
||||
ns.Bus.AddHandler(ns.sendResetPasswordEmail)
|
||||
ns.Bus.AddHandler(ns.validateResetPasswordCode)
|
||||
ns.Bus.AddHandlerCtx(ns.validateResetPasswordCode)
|
||||
ns.Bus.AddHandler(ns.sendEmailCommandHandler)
|
||||
|
||||
ns.Bus.AddHandlerCtx(ns.sendEmailCommandHandlerSync)
|
||||
@@ -163,14 +163,14 @@ func (ns *NotificationService) sendResetPasswordEmail(cmd *models.SendResetPassw
|
||||
})
|
||||
}
|
||||
|
||||
func (ns *NotificationService) validateResetPasswordCode(query *models.ValidateResetPasswordCodeQuery) error {
|
||||
func (ns *NotificationService) validateResetPasswordCode(ctx context.Context, query *models.ValidateResetPasswordCodeQuery) error {
|
||||
login := getLoginForEmailCode(query.Code)
|
||||
if login == "" {
|
||||
return models.ErrInvalidEmailCode
|
||||
}
|
||||
|
||||
userQuery := models.GetUserByLoginQuery{LoginOrEmail: login}
|
||||
if err := bus.Dispatch(&userQuery); err != nil {
|
||||
if err := bus.DispatchCtx(ctx, &userQuery); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ func (o *Service) GetCurrentOAuthToken(ctx context.Context, user *models.SignedI
|
||||
}
|
||||
|
||||
authInfoQuery := &models.GetAuthInfoQuery{UserId: user.UserId}
|
||||
if err := bus.Dispatch(authInfoQuery); err != nil {
|
||||
if err := bus.DispatchCtx(ctx, authInfoQuery); err != nil {
|
||||
if errors.Is(err, models.ErrUserNotFound) {
|
||||
// Not necessarily an error. User may be logged in another way.
|
||||
logger.Debug("no OAuth token for user found", "userId", user.UserId, "username", user.Login)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package sqlstore
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
@@ -11,12 +12,12 @@ import (
|
||||
var getTimeNow = time.Now
|
||||
|
||||
func init() {
|
||||
bus.AddHandler("sql", CreateLoginAttempt)
|
||||
bus.AddHandler("sql", DeleteOldLoginAttempts)
|
||||
bus.AddHandler("sql", GetUserLoginAttemptCount)
|
||||
bus.AddHandlerCtx("sql", CreateLoginAttempt)
|
||||
bus.AddHandlerCtx("sql", DeleteOldLoginAttempts)
|
||||
bus.AddHandlerCtx("sql", GetUserLoginAttemptCount)
|
||||
}
|
||||
|
||||
func CreateLoginAttempt(cmd *models.CreateLoginAttemptCommand) error {
|
||||
func CreateLoginAttempt(ctx context.Context, cmd *models.CreateLoginAttemptCommand) error {
|
||||
return inTransaction(func(sess *DBSession) error {
|
||||
loginAttempt := models.LoginAttempt{
|
||||
Username: cmd.Username,
|
||||
@@ -34,7 +35,7 @@ func CreateLoginAttempt(cmd *models.CreateLoginAttemptCommand) error {
|
||||
})
|
||||
}
|
||||
|
||||
func DeleteOldLoginAttempts(cmd *models.DeleteOldLoginAttemptsCommand) error {
|
||||
func DeleteOldLoginAttempts(ctx context.Context, cmd *models.DeleteOldLoginAttemptsCommand) error {
|
||||
return inTransaction(func(sess *DBSession) error {
|
||||
var maxId int64
|
||||
sql := "SELECT max(id) as id FROM login_attempt WHERE created < ?"
|
||||
@@ -64,7 +65,7 @@ func DeleteOldLoginAttempts(cmd *models.DeleteOldLoginAttemptsCommand) error {
|
||||
})
|
||||
}
|
||||
|
||||
func GetUserLoginAttemptCount(query *models.GetUserLoginAttemptCountQuery) error {
|
||||
func GetUserLoginAttemptCount(ctx context.Context, query *models.GetUserLoginAttemptCountQuery) error {
|
||||
loginAttempt := new(models.LoginAttempt)
|
||||
total, err := x.
|
||||
Where("username = ?", query.Username).
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
package sqlstore
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -24,19 +25,19 @@ func TestLoginAttempts(t *testing.T) {
|
||||
setup := func(t *testing.T) {
|
||||
InitTestDB(t)
|
||||
beginningOfTime = mockTime(time.Date(2017, 10, 22, 8, 0, 0, 0, time.Local))
|
||||
err := CreateLoginAttempt(&models.CreateLoginAttemptCommand{
|
||||
err := CreateLoginAttempt(context.Background(), &models.CreateLoginAttemptCommand{
|
||||
Username: user,
|
||||
IpAddress: "192.168.0.1",
|
||||
})
|
||||
require.Nil(t, err)
|
||||
timePlusOneMinute = mockTime(beginningOfTime.Add(time.Minute * 1))
|
||||
err = CreateLoginAttempt(&models.CreateLoginAttemptCommand{
|
||||
err = CreateLoginAttempt(context.Background(), &models.CreateLoginAttemptCommand{
|
||||
Username: user,
|
||||
IpAddress: "192.168.0.1",
|
||||
})
|
||||
require.Nil(t, err)
|
||||
timePlusTwoMinutes = mockTime(beginningOfTime.Add(time.Minute * 2))
|
||||
err = CreateLoginAttempt(&models.CreateLoginAttemptCommand{
|
||||
err = CreateLoginAttempt(context.Background(), &models.CreateLoginAttemptCommand{
|
||||
Username: user,
|
||||
IpAddress: "192.168.0.1",
|
||||
})
|
||||
@@ -49,7 +50,7 @@ func TestLoginAttempts(t *testing.T) {
|
||||
Username: user,
|
||||
Since: timePlusTwoMinutes.Add(time.Second * 1),
|
||||
}
|
||||
err := GetUserLoginAttemptCount(&query)
|
||||
err := GetUserLoginAttemptCount(context.Background(), &query)
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, int64(0), query.Result)
|
||||
})
|
||||
@@ -60,7 +61,7 @@ func TestLoginAttempts(t *testing.T) {
|
||||
Username: user,
|
||||
Since: beginningOfTime,
|
||||
}
|
||||
err := GetUserLoginAttemptCount(&query)
|
||||
err := GetUserLoginAttemptCount(context.Background(), &query)
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, int64(3), query.Result)
|
||||
})
|
||||
@@ -71,7 +72,7 @@ func TestLoginAttempts(t *testing.T) {
|
||||
Username: user,
|
||||
Since: timePlusOneMinute,
|
||||
}
|
||||
err := GetUserLoginAttemptCount(&query)
|
||||
err := GetUserLoginAttemptCount(context.Background(), &query)
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, int64(2), query.Result)
|
||||
})
|
||||
@@ -82,7 +83,7 @@ func TestLoginAttempts(t *testing.T) {
|
||||
Username: user,
|
||||
Since: timePlusTwoMinutes,
|
||||
}
|
||||
err := GetUserLoginAttemptCount(&query)
|
||||
err := GetUserLoginAttemptCount(context.Background(), &query)
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, int64(1), query.Result)
|
||||
})
|
||||
@@ -92,7 +93,7 @@ func TestLoginAttempts(t *testing.T) {
|
||||
cmd := models.DeleteOldLoginAttemptsCommand{
|
||||
OlderThan: beginningOfTime,
|
||||
}
|
||||
err := DeleteOldLoginAttempts(&cmd)
|
||||
err := DeleteOldLoginAttempts(context.Background(), &cmd)
|
||||
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, int64(0), cmd.DeletedRows)
|
||||
@@ -103,7 +104,7 @@ func TestLoginAttempts(t *testing.T) {
|
||||
cmd := models.DeleteOldLoginAttemptsCommand{
|
||||
OlderThan: timePlusOneMinute,
|
||||
}
|
||||
err := DeleteOldLoginAttempts(&cmd)
|
||||
err := DeleteOldLoginAttempts(context.Background(), &cmd)
|
||||
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, int64(1), cmd.DeletedRows)
|
||||
@@ -114,7 +115,7 @@ func TestLoginAttempts(t *testing.T) {
|
||||
cmd := models.DeleteOldLoginAttemptsCommand{
|
||||
OlderThan: timePlusTwoMinutes,
|
||||
}
|
||||
err := DeleteOldLoginAttempts(&cmd)
|
||||
err := DeleteOldLoginAttempts(context.Background(), &cmd)
|
||||
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, int64(2), cmd.DeletedRows)
|
||||
@@ -125,7 +126,7 @@ func TestLoginAttempts(t *testing.T) {
|
||||
cmd := models.DeleteOldLoginAttemptsCommand{
|
||||
OlderThan: timePlusTwoMinutes.Add(time.Second * 1),
|
||||
}
|
||||
err := DeleteOldLoginAttempts(&cmd)
|
||||
err := DeleteOldLoginAttempts(context.Background(), &cmd)
|
||||
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, int64(3), cmd.DeletedRows)
|
||||
|
||||
Reference in New Issue
Block a user