mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
adds cleanup job for old session tokens
This commit is contained in:
parent
f040f9a400
commit
777bd9ea18
@ -8,6 +8,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/bus"
|
"github.com/grafana/grafana/pkg/bus"
|
||||||
|
"github.com/grafana/grafana/pkg/infra/serverlock"
|
||||||
"github.com/grafana/grafana/pkg/log"
|
"github.com/grafana/grafana/pkg/log"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
"github.com/grafana/grafana/pkg/registry"
|
"github.com/grafana/grafana/pkg/registry"
|
||||||
@ -29,8 +30,9 @@ var (
|
|||||||
|
|
||||||
// UserAuthTokenService are used for generating and validating user auth tokens
|
// UserAuthTokenService are used for generating and validating user auth tokens
|
||||||
type UserAuthTokenService struct {
|
type UserAuthTokenService struct {
|
||||||
SQLStore *sqlstore.SqlStore `inject:""`
|
SQLStore *sqlstore.SqlStore `inject:""`
|
||||||
log log.Logger
|
ServerLockService *serverlock.ServerLockService `inject:""`
|
||||||
|
log log.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
// Init this service
|
// Init this service
|
||||||
@ -239,7 +241,7 @@ func (s *UserAuthTokenService) RefreshToken(token *models.UserAuthToken, clientI
|
|||||||
}
|
}
|
||||||
|
|
||||||
affected, _ := res.RowsAffected()
|
affected, _ := res.RowsAffected()
|
||||||
s.log.Debug("rotated", "affected", affected, "auth_token_id", token.Id, "userId", token.UserId, "user_agent", userAgent, "client_ip", clientIP)
|
s.log.Debug("rotated", "affected", affected, "auth_token_id", token.Id, "userId", token.UserId)
|
||||||
if affected > 0 {
|
if affected > 0 {
|
||||||
token.UnhashedToken = newToken
|
token.UnhashedToken = newToken
|
||||||
return true, nil
|
return true, nil
|
||||||
|
38
pkg/services/auth/session_cleanup.go
Normal file
38
pkg/services/auth/session_cleanup.go
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
package auth
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (srv *UserAuthTokenService) Run(ctx context.Context) error {
|
||||||
|
ticker := time.NewTicker(time.Hour * 12)
|
||||||
|
deleteSessionAfter := time.Hour * 24 * 7 * 30
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ticker.C:
|
||||||
|
srv.ServerLockService.LockAndExecute(ctx, "delete old sessions", time.Hour*12, func() {
|
||||||
|
srv.deleteOldSession(deleteSessionAfter)
|
||||||
|
})
|
||||||
|
|
||||||
|
case <-ctx.Done():
|
||||||
|
return ctx.Err()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (srv *UserAuthTokenService) deleteOldSession(deleteSessionAfter time.Duration) (int64, error) {
|
||||||
|
sql := `DELETE from user_auth_token WHERE rotated_at < ?`
|
||||||
|
|
||||||
|
deleteBefore := getTime().Add(-deleteSessionAfter)
|
||||||
|
res, err := srv.SQLStore.NewSession().Exec(sql, deleteBefore.Unix())
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
affected, err := res.RowsAffected()
|
||||||
|
srv.log.Info("deleted old sessions", "count", affected)
|
||||||
|
|
||||||
|
return affected, err
|
||||||
|
}
|
37
pkg/services/auth/session_cleanup_test.go
Normal file
37
pkg/services/auth/session_cleanup_test.go
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
package auth
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/grafana/grafana/pkg/models"
|
||||||
|
. "github.com/smartystreets/goconvey/convey"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestUserAuthTokenCleanup(t *testing.T) {
|
||||||
|
|
||||||
|
Convey("Test user auth token cleanup", t, func() {
|
||||||
|
ctx := createTestContext(t)
|
||||||
|
|
||||||
|
insertToken := func(token string, prev string, rotatedAt int64) {
|
||||||
|
ut := models.UserAuthToken{AuthToken: token, PrevAuthToken: prev, RotatedAt: rotatedAt, UserAgent: "", ClientIp: ""}
|
||||||
|
_, err := ctx.sqlstore.NewSession().Insert(&ut)
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
}
|
||||||
|
|
||||||
|
// insert three old tokens that should be deleted
|
||||||
|
for i := 0; i < 3; i++ {
|
||||||
|
insertToken(fmt.Sprintf("oldA%d", i), fmt.Sprintf("oldB%d", i), int64(i))
|
||||||
|
}
|
||||||
|
|
||||||
|
// insert three active tokens that should not be deleted
|
||||||
|
for i := 0; i < 3; i++ {
|
||||||
|
insertToken(fmt.Sprintf("newA%d", i), fmt.Sprintf("newB%d", i), getTime().Unix())
|
||||||
|
}
|
||||||
|
|
||||||
|
affected, err := ctx.tokenService.deleteOldSession(time.Hour)
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
So(affected, ShouldEqual, 3)
|
||||||
|
})
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user