2019-02-06 17:02:57 +01:00
|
|
|
package auth
|
2019-01-21 17:05:42 +01:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"time"
|
2019-04-30 14:42:01 +02:00
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/services/sqlstore"
|
2019-01-21 17:05:42 +01:00
|
|
|
)
|
|
|
|
|
|
2020-11-05 15:37:11 +01:00
|
|
|
func (s *UserAuthTokenService) Run(ctx context.Context) error {
|
2019-02-07 10:51:35 +01:00
|
|
|
ticker := time.NewTicker(time.Hour)
|
2020-11-05 15:37:11 +01:00
|
|
|
maxInactiveLifetime := s.Cfg.LoginMaxInactiveLifetime
|
|
|
|
|
maxLifetime := s.Cfg.LoginMaxLifetime
|
2019-01-21 17:05:42 +01:00
|
|
|
|
2021-10-04 15:46:09 +02:00
|
|
|
err := s.ServerLockService.LockAndExecute(ctx, "cleanup expired auth tokens", time.Hour*12, func(context.Context) {
|
2020-11-05 15:37:11 +01:00
|
|
|
if _, err := s.deleteExpiredTokens(ctx, maxInactiveLifetime, maxLifetime); err != nil {
|
|
|
|
|
s.log.Error("An error occurred while deleting expired tokens", "err", err)
|
2019-10-22 14:08:18 +02:00
|
|
|
}
|
2019-02-06 22:27:08 +01:00
|
|
|
})
|
|
|
|
|
if err != nil {
|
2020-11-05 15:37:11 +01:00
|
|
|
s.log.Error("failed to lock and execute cleanup of expired auth token", "error", err)
|
2019-02-06 22:27:08 +01:00
|
|
|
}
|
|
|
|
|
|
2019-01-21 17:05:42 +01:00
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case <-ticker.C:
|
2021-10-04 15:46:09 +02:00
|
|
|
err = s.ServerLockService.LockAndExecute(ctx, "cleanup expired auth tokens", time.Hour*12, func(context.Context) {
|
2020-11-05 15:37:11 +01:00
|
|
|
if _, err := s.deleteExpiredTokens(ctx, maxInactiveLifetime, maxLifetime); err != nil {
|
|
|
|
|
s.log.Error("An error occurred while deleting expired tokens", "err", err)
|
2019-10-22 14:08:18 +02:00
|
|
|
}
|
2019-01-21 17:05:42 +01:00
|
|
|
})
|
2019-02-06 22:27:08 +01:00
|
|
|
if err != nil {
|
2020-11-05 15:37:11 +01:00
|
|
|
s.log.Error("failed to lock and execute cleanup of expired auth token", "error", err)
|
2019-02-06 22:27:08 +01:00
|
|
|
}
|
|
|
|
|
|
2019-01-21 17:05:42 +01:00
|
|
|
case <-ctx.Done():
|
|
|
|
|
return ctx.Err()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-05 15:37:11 +01:00
|
|
|
func (s *UserAuthTokenService) deleteExpiredTokens(ctx context.Context, maxInactiveLifetime, maxLifetime time.Duration) (int64, error) {
|
2019-02-05 21:20:11 +01:00
|
|
|
createdBefore := getTime().Add(-maxLifetime)
|
|
|
|
|
rotatedBefore := getTime().Add(-maxInactiveLifetime)
|
2019-01-21 17:05:42 +01:00
|
|
|
|
2020-11-05 15:37:11 +01:00
|
|
|
s.log.Debug("starting cleanup of expired auth tokens", "createdBefore", createdBefore, "rotatedBefore", rotatedBefore)
|
2019-02-05 21:20:11 +01:00
|
|
|
|
2019-04-30 14:42:01 +02:00
|
|
|
var affected int64
|
2020-11-05 15:37:11 +01:00
|
|
|
err := s.SQLStore.WithDbSession(ctx, func(dbSession *sqlstore.DBSession) error {
|
2019-04-30 14:42:01 +02:00
|
|
|
sql := `DELETE from user_auth_token WHERE created_at <= ? OR rotated_at <= ?`
|
|
|
|
|
res, err := dbSession.Exec(sql, createdBefore.Unix(), rotatedBefore.Unix())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2019-01-21 17:05:42 +01:00
|
|
|
|
2019-04-30 14:42:01 +02:00
|
|
|
affected, err = res.RowsAffected()
|
|
|
|
|
if err != nil {
|
2020-11-05 15:37:11 +01:00
|
|
|
s.log.Error("failed to cleanup expired auth tokens", "error", err)
|
2019-04-30 14:42:01 +02:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-05 15:37:11 +01:00
|
|
|
s.log.Debug("cleanup of expired auth tokens done", "count", affected)
|
2019-04-30 14:42:01 +02:00
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
})
|
2019-01-21 17:05:42 +01:00
|
|
|
|
|
|
|
|
return affected, err
|
|
|
|
|
}
|