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