[v10.1.x] Auth: Optimize auth token operations (#74833)
Auth: Optimize auth token operations (#74602)
* add token count
* wip
* user count method for tag reporting
* remove non functioning mysql clientFoundRows check
* Update pkg/services/auth/authtest/testing.go
Co-authored-by: Misi <mgyongyosi@users.noreply.github.com>
* add user ID guard
---------
Co-authored-by: Misi <mgyongyosi@users.noreply.github.com>
(cherry picked from commit 77e4d477e5)
Co-authored-by: Jo <joao.guerreiro@grafana.com>
This commit is contained in:
@@ -26,6 +26,7 @@ import (
|
||||
var (
|
||||
getTime = time.Now
|
||||
errTokenNotRotated = errors.New("token was not rotated")
|
||||
errUserIDInvalid = errors.New("invalid user ID")
|
||||
)
|
||||
|
||||
func ProvideUserAuthTokenService(sqlStore db.DB,
|
||||
@@ -547,6 +548,27 @@ func (s *UserAuthTokenService) DeleteUserRevokedTokens(ctx context.Context, user
|
||||
})
|
||||
}
|
||||
|
||||
// ActiveTokenCount returns the number of active tokens. If userID is nil, the count is for all users.
|
||||
func (s *UserAuthTokenService) ActiveTokenCount(ctx context.Context, userID *int64) (int64, error) {
|
||||
if userID != nil && *userID < 1 {
|
||||
return 0, errUserIDInvalid
|
||||
}
|
||||
|
||||
var count int64
|
||||
err := s.sqlStore.WithDbSession(ctx, func(dbSession *db.Session) error {
|
||||
query := `SELECT COUNT(*) FROM user_auth_token WHERE created_at > ? AND rotated_at > ? AND revoked_at = 0`
|
||||
args := []interface{}{s.createdAfterParam(), s.rotatedAfterParam()}
|
||||
if userID != nil {
|
||||
query += " AND user_id = ?"
|
||||
args = append(args, *userID)
|
||||
}
|
||||
_, err := dbSession.SQL(query, args...).Get(&count)
|
||||
return err
|
||||
})
|
||||
|
||||
return count, err
|
||||
}
|
||||
|
||||
func (s *UserAuthTokenService) GetUserRevokedTokens(ctx context.Context, userId int64) ([]*auth.UserToken, error) {
|
||||
result := []*auth.UserToken{}
|
||||
err := s.sqlStore.WithDbSession(ctx, func(dbSession *db.Session) error {
|
||||
@@ -571,22 +593,16 @@ func (s *UserAuthTokenService) GetUserRevokedTokens(ctx context.Context, userId
|
||||
}
|
||||
|
||||
func (s *UserAuthTokenService) reportActiveTokenCount(ctx context.Context, _ *quota.ScopeParameters) (*quota.Map, error) {
|
||||
var count int64
|
||||
var err error
|
||||
err = s.sqlStore.WithDbSession(ctx, func(dbSession *db.Session) error {
|
||||
var model userAuthToken
|
||||
count, err = dbSession.Where(`created_at > ? AND rotated_at > ? AND revoked_at = 0`,
|
||||
getTime().Add(-s.cfg.LoginMaxLifetime).Unix(),
|
||||
getTime().Add(-s.cfg.LoginMaxInactiveLifetime).Unix()).
|
||||
Count(&model)
|
||||
|
||||
return err
|
||||
})
|
||||
count, err := s.ActiveTokenCount(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tag, err := quota.NewTag(auth.QuotaTargetSrv, auth.QuotaTarget, quota.GlobalScope)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
u := "a.Map{}
|
||||
u.Set(tag, count)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user