mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Sync: Move ActiveTokenCount to a new service (#52991)
* Move ActiveTokenCount to a new service * Fixing tests * fix `RootSystem` Co-authored-by: Artur Wierzbicki <artur.wierzbicki@grafana.com>
This commit is contained in:
parent
1eb3513781
commit
085ae014cd
@ -71,12 +71,15 @@ type UserTokenService interface {
|
|||||||
TryRotateToken(ctx context.Context, token *UserToken, clientIP net.IP, userAgent string) (bool, error)
|
TryRotateToken(ctx context.Context, token *UserToken, clientIP net.IP, userAgent string) (bool, error)
|
||||||
RevokeToken(ctx context.Context, token *UserToken, soft bool) error
|
RevokeToken(ctx context.Context, token *UserToken, soft bool) error
|
||||||
RevokeAllUserTokens(ctx context.Context, userId int64) error
|
RevokeAllUserTokens(ctx context.Context, userId int64) error
|
||||||
ActiveTokenCount(ctx context.Context) (int64, error)
|
|
||||||
GetUserToken(ctx context.Context, userId, userTokenId int64) (*UserToken, error)
|
GetUserToken(ctx context.Context, userId, userTokenId int64) (*UserToken, error)
|
||||||
GetUserTokens(ctx context.Context, userId int64) ([]*UserToken, error)
|
GetUserTokens(ctx context.Context, userId int64) ([]*UserToken, error)
|
||||||
GetUserRevokedTokens(ctx context.Context, userId int64) ([]*UserToken, error)
|
GetUserRevokedTokens(ctx context.Context, userId int64) ([]*UserToken, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ActiveTokenService interface {
|
||||||
|
ActiveTokenCount(ctx context.Context) (int64, error)
|
||||||
|
}
|
||||||
|
|
||||||
type UserTokenBackgroundService interface {
|
type UserTokenBackgroundService interface {
|
||||||
registry.BackgroundService
|
registry.BackgroundService
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,7 @@ package server
|
|||||||
import (
|
import (
|
||||||
"github.com/google/wire"
|
"github.com/google/wire"
|
||||||
sdkhttpclient "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
|
sdkhttpclient "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
|
||||||
|
"github.com/grafana/grafana/pkg/services/auth"
|
||||||
"github.com/grafana/grafana/pkg/services/playlist/playlistimpl"
|
"github.com/grafana/grafana/pkg/services/playlist/playlistimpl"
|
||||||
"github.com/grafana/grafana/pkg/services/store/sanitizer"
|
"github.com/grafana/grafana/pkg/services/store/sanitizer"
|
||||||
|
|
||||||
@ -222,6 +223,8 @@ var wireBasicSet = wire.NewSet(
|
|||||||
influxdb.ProvideService,
|
influxdb.ProvideService,
|
||||||
wire.Bind(new(social.Service), new(*social.SocialService)),
|
wire.Bind(new(social.Service), new(*social.SocialService)),
|
||||||
oauthtoken.ProvideService,
|
oauthtoken.ProvideService,
|
||||||
|
auth.ProvideActiveAuthTokenService,
|
||||||
|
wire.Bind(new(models.ActiveTokenService), new(*auth.ActiveAuthTokenService)),
|
||||||
wire.Bind(new(oauthtoken.OAuthTokenService), new(*oauthtoken.Service)),
|
wire.Bind(new(oauthtoken.OAuthTokenService), new(*oauthtoken.Service)),
|
||||||
tempo.ProvideService,
|
tempo.ProvideService,
|
||||||
loki.ProvideService,
|
loki.ProvideService,
|
||||||
|
@ -42,14 +42,26 @@ type UserAuthTokenService struct {
|
|||||||
log log.Logger
|
log log.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *UserAuthTokenService) ActiveTokenCount(ctx context.Context) (int64, error) {
|
type ActiveAuthTokenService struct {
|
||||||
|
cfg *setting.Cfg
|
||||||
|
sqlStore sqlstore.Store
|
||||||
|
}
|
||||||
|
|
||||||
|
func ProvideActiveAuthTokenService(cfg *setting.Cfg, sqlStore sqlstore.Store) *ActiveAuthTokenService {
|
||||||
|
return &ActiveAuthTokenService{
|
||||||
|
cfg: cfg,
|
||||||
|
sqlStore: sqlStore,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *ActiveAuthTokenService) ActiveTokenCount(ctx context.Context) (int64, error) {
|
||||||
var count int64
|
var count int64
|
||||||
var err error
|
var err error
|
||||||
err = s.SQLStore.WithDbSession(ctx, func(dbSession *sqlstore.DBSession) error {
|
err = a.sqlStore.WithDbSession(ctx, func(dbSession *sqlstore.DBSession) error {
|
||||||
var model userAuthToken
|
var model userAuthToken
|
||||||
count, err = dbSession.Where(`created_at > ? AND rotated_at > ? AND revoked_at = 0`,
|
count, err = dbSession.Where(`created_at > ? AND rotated_at > ? AND revoked_at = 0`,
|
||||||
s.createdAfterParam(),
|
getTime().Add(-a.cfg.LoginMaxLifetime).Unix(),
|
||||||
s.rotatedAfterParam()).
|
getTime().Add(-a.cfg.LoginMaxInactiveLifetime).Unix()).
|
||||||
Count(&model)
|
Count(&model)
|
||||||
|
|
||||||
return err
|
return err
|
||||||
|
@ -41,7 +41,7 @@ func TestUserAuthToken(t *testing.T) {
|
|||||||
userToken := createToken()
|
userToken := createToken()
|
||||||
|
|
||||||
t.Run("Can count active tokens", func(t *testing.T) {
|
t.Run("Can count active tokens", func(t *testing.T) {
|
||||||
count, err := ctx.tokenService.ActiveTokenCount(context.Background())
|
count, err := ctx.activeTokenService.ActiveTokenCount(context.Background())
|
||||||
require.Nil(t, err)
|
require.Nil(t, err)
|
||||||
require.Equal(t, int64(1), count)
|
require.Equal(t, int64(1), count)
|
||||||
})
|
})
|
||||||
@ -209,7 +209,7 @@ func TestUserAuthToken(t *testing.T) {
|
|||||||
require.Nil(t, notGood)
|
require.Nil(t, notGood)
|
||||||
|
|
||||||
t.Run("should not find active token when expired", func(t *testing.T) {
|
t.Run("should not find active token when expired", func(t *testing.T) {
|
||||||
count, err := ctx.tokenService.ActiveTokenCount(context.Background())
|
count, err := ctx.activeTokenService.ActiveTokenCount(context.Background())
|
||||||
require.Nil(t, err)
|
require.Nil(t, err)
|
||||||
require.Equal(t, int64(0), count)
|
require.Equal(t, int64(0), count)
|
||||||
})
|
})
|
||||||
@ -534,25 +534,35 @@ func createTestContext(t *testing.T) *testContext {
|
|||||||
maxInactiveDurationVal, _ := time.ParseDuration("168h")
|
maxInactiveDurationVal, _ := time.ParseDuration("168h")
|
||||||
maxLifetimeDurationVal, _ := time.ParseDuration("720h")
|
maxLifetimeDurationVal, _ := time.ParseDuration("720h")
|
||||||
sqlstore := sqlstore.InitTestDB(t)
|
sqlstore := sqlstore.InitTestDB(t)
|
||||||
|
|
||||||
|
cfg := &setting.Cfg{
|
||||||
|
LoginMaxInactiveLifetime: maxInactiveDurationVal,
|
||||||
|
LoginMaxLifetime: maxLifetimeDurationVal,
|
||||||
|
TokenRotationIntervalMinutes: 10,
|
||||||
|
}
|
||||||
|
|
||||||
tokenService := &UserAuthTokenService{
|
tokenService := &UserAuthTokenService{
|
||||||
SQLStore: sqlstore,
|
SQLStore: sqlstore,
|
||||||
Cfg: &setting.Cfg{
|
Cfg: cfg,
|
||||||
LoginMaxInactiveLifetime: maxInactiveDurationVal,
|
log: log.New("test-logger"),
|
||||||
LoginMaxLifetime: maxLifetimeDurationVal,
|
}
|
||||||
TokenRotationIntervalMinutes: 10,
|
|
||||||
},
|
activeTokenService := &ActiveAuthTokenService{
|
||||||
log: log.New("test-logger"),
|
cfg: cfg,
|
||||||
|
sqlStore: sqlstore,
|
||||||
}
|
}
|
||||||
|
|
||||||
return &testContext{
|
return &testContext{
|
||||||
sqlstore: sqlstore,
|
sqlstore: sqlstore,
|
||||||
tokenService: tokenService,
|
tokenService: tokenService,
|
||||||
|
activeTokenService: activeTokenService,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type testContext struct {
|
type testContext struct {
|
||||||
sqlstore *sqlstore.SQLStore
|
sqlstore *sqlstore.SQLStore
|
||||||
tokenService *UserAuthTokenService
|
tokenService *UserAuthTokenService
|
||||||
|
activeTokenService *ActiveAuthTokenService
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *testContext) getAuthTokenByID(id int64) (*userAuthToken, error) {
|
func (c *testContext) getAuthTokenByID(id int64) (*userAuthToken, error) {
|
||||||
|
@ -13,17 +13,17 @@ import (
|
|||||||
|
|
||||||
type Service struct {
|
type Service struct {
|
||||||
store store
|
store store
|
||||||
AuthTokenService models.UserTokenService
|
authTokenService models.ActiveTokenService
|
||||||
Cfg *setting.Cfg
|
Cfg *setting.Cfg
|
||||||
SQLStore sqlstore.Store
|
SQLStore sqlstore.Store
|
||||||
Logger log.Logger
|
Logger log.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
func ProvideService(db db.DB, cfg *setting.Cfg, tokenService models.UserTokenService, ss *sqlstore.SQLStore) quota.Service {
|
func ProvideService(db db.DB, cfg *setting.Cfg, tokenService models.ActiveTokenService, ss *sqlstore.SQLStore) quota.Service {
|
||||||
return &Service{
|
return &Service{
|
||||||
store: &sqlStore{db: db},
|
store: &sqlStore{db: db},
|
||||||
Cfg: cfg,
|
Cfg: cfg,
|
||||||
AuthTokenService: tokenService,
|
authTokenService: tokenService,
|
||||||
SQLStore: ss,
|
SQLStore: ss,
|
||||||
Logger: log.New("quota_service"),
|
Logger: log.New("quota_service"),
|
||||||
}
|
}
|
||||||
@ -71,7 +71,7 @@ func (s *Service) CheckQuotaReached(ctx context.Context, target string, scopePar
|
|||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
if target == "session" {
|
if target == "session" {
|
||||||
usedSessions, err := s.AuthTokenService.ActiveTokenCount(ctx)
|
usedSessions, err := s.authTokenService.ActiveTokenCount(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
@ -171,7 +171,7 @@ func ProvideService(
|
|||||||
storages = append(storages,
|
storages = append(storages,
|
||||||
newSQLStorage(RootStorageMeta{
|
newSQLStorage(RootStorageMeta{
|
||||||
Builtin: true,
|
Builtin: true,
|
||||||
}, RootResources,
|
}, RootSystem,
|
||||||
"System",
|
"System",
|
||||||
"Grafana system storage",
|
"Grafana system storage",
|
||||||
&StorageSQLConfig{}, sql, orgId))
|
&StorageSQLConfig{}, sql, orgId))
|
||||||
|
Loading…
Reference in New Issue
Block a user