mirror of
https://github.com/grafana/grafana.git
synced 2025-02-11 08:05:43 -06:00
Auth: Rename Sessions to Devices in counting (#72432)
* rename session to device * rename session to device
This commit is contained in:
parent
5f09d8f2a6
commit
a4a87f6228
@ -47,8 +47,8 @@ var wireExtsBasicSet = wire.NewSet(
|
||||
authimpl.ProvideUserAuthTokenService,
|
||||
wire.Bind(new(auth.UserTokenService), new(*authimpl.UserAuthTokenService)),
|
||||
wire.Bind(new(auth.UserTokenBackgroundService), new(*authimpl.UserAuthTokenService)),
|
||||
anonimpl.ProvideAnonymousSessionService,
|
||||
wire.Bind(new(anonymous.Service), new(*anonimpl.AnonSessionService)),
|
||||
anonimpl.ProvideAnonymousDeviceService,
|
||||
wire.Bind(new(anonymous.Service), new(*anonimpl.AnonDeviceService)),
|
||||
licensing.ProvideService,
|
||||
wire.Bind(new(licensing.Licensing), new(*licensing.OSSLicensingService)),
|
||||
setting.ProvideProvider,
|
||||
|
@ -20,12 +20,12 @@ import (
|
||||
const thirtyDays = 30 * 24 * time.Hour
|
||||
const anonCachePrefix = "anon-session"
|
||||
|
||||
type AnonSession struct {
|
||||
type Device struct {
|
||||
ip string
|
||||
userAgent string
|
||||
}
|
||||
|
||||
func (a *AnonSession) Key() (string, error) {
|
||||
func (a *Device) Key() (string, error) {
|
||||
key := strings.Builder{}
|
||||
key.WriteString(a.ip)
|
||||
key.WriteString(a.userAgent)
|
||||
@ -38,14 +38,14 @@ func (a *AnonSession) Key() (string, error) {
|
||||
return strings.Join([]string{anonCachePrefix, hex.EncodeToString(hash.Sum(nil))}, ":"), nil
|
||||
}
|
||||
|
||||
type AnonSessionService struct {
|
||||
type AnonDeviceService struct {
|
||||
remoteCache remotecache.CacheStorage
|
||||
log log.Logger
|
||||
localCache *localcache.CacheService
|
||||
}
|
||||
|
||||
func ProvideAnonymousSessionService(remoteCache remotecache.CacheStorage, usageStats usagestats.Service) *AnonSessionService {
|
||||
a := &AnonSessionService{
|
||||
func ProvideAnonymousDeviceService(remoteCache remotecache.CacheStorage, usageStats usagestats.Service) *AnonDeviceService {
|
||||
a := &AnonDeviceService{
|
||||
remoteCache: remoteCache,
|
||||
log: log.New("anonymous-session-service"),
|
||||
localCache: localcache.New(29*time.Minute, 15*time.Minute),
|
||||
@ -56,7 +56,7 @@ func ProvideAnonymousSessionService(remoteCache remotecache.CacheStorage, usageS
|
||||
return a
|
||||
}
|
||||
|
||||
func (a *AnonSessionService) usageStatFn(ctx context.Context) (map[string]interface{}, error) {
|
||||
func (a *AnonDeviceService) usageStatFn(ctx context.Context) (map[string]interface{}, error) {
|
||||
sessionCount, err := a.remoteCache.Count(ctx, anonCachePrefix)
|
||||
if err != nil {
|
||||
return nil, nil
|
||||
@ -67,7 +67,7 @@ func (a *AnonSessionService) usageStatFn(ctx context.Context) (map[string]interf
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (a *AnonSessionService) TagSession(ctx context.Context, httpReq *http.Request) error {
|
||||
func (a *AnonDeviceService) TagDevice(ctx context.Context, httpReq *http.Request) error {
|
||||
addr := web.RemoteAddr(httpReq)
|
||||
ip, err := network.GetIPFromAddress(addr)
|
||||
if err != nil {
|
||||
@ -80,12 +80,12 @@ func (a *AnonSessionService) TagSession(ctx context.Context, httpReq *http.Reque
|
||||
clientIPStr = ""
|
||||
}
|
||||
|
||||
anonSession := &AnonSession{
|
||||
anonDevice := &Device{
|
||||
ip: clientIPStr,
|
||||
userAgent: httpReq.UserAgent(),
|
||||
}
|
||||
|
||||
key, err := anonSession.Key()
|
||||
key, err := anonDevice.Key()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -12,15 +12,15 @@ import (
|
||||
"github.com/grafana/grafana/pkg/infra/usagestats"
|
||||
)
|
||||
|
||||
func TestAnonSessionKey(t *testing.T) {
|
||||
func TestAnonDeviceKey(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
session *AnonSession
|
||||
session *Device
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
name: "should hash correctly",
|
||||
session: &AnonSession{
|
||||
session: &Device{
|
||||
ip: "10.10.10.10",
|
||||
userAgent: "test",
|
||||
},
|
||||
@ -28,7 +28,7 @@ func TestAnonSessionKey(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "should hash correctly with different ip",
|
||||
session: &AnonSession{
|
||||
session: &Device{
|
||||
ip: "10.10.10.1",
|
||||
userAgent: "test",
|
||||
},
|
||||
@ -36,7 +36,7 @@ func TestAnonSessionKey(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "should hash correctly with different user agent",
|
||||
session: &AnonSession{
|
||||
session: &Device{
|
||||
ip: "10.10.10.1",
|
||||
userAgent: "test2",
|
||||
},
|
||||
@ -58,7 +58,7 @@ func TestAnonSessionKey(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestIntegrationAnonSessionService_tag(t *testing.T) {
|
||||
func TestIntegrationAnonDeviceService_tag(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
req []*http.Request
|
||||
@ -134,10 +134,10 @@ func TestIntegrationAnonSessionService_tag(t *testing.T) {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
fakeStore := remotecache.NewFakeStore(t)
|
||||
|
||||
anonService := ProvideAnonymousSessionService(fakeStore, &usagestats.UsageStatsMock{})
|
||||
anonService := ProvideAnonymousDeviceService(fakeStore, &usagestats.UsageStatsMock{})
|
||||
|
||||
for _, req := range tc.req {
|
||||
err := anonService.TagSession(context.Background(), req)
|
||||
err := anonService.TagDevice(context.Background(), req)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
@ -150,9 +150,9 @@ func TestIntegrationAnonSessionService_tag(t *testing.T) {
|
||||
}
|
||||
|
||||
// Ensure that the local cache prevents request from being tagged
|
||||
func TestIntegrationAnonSessionService_localCacheSafety(t *testing.T) {
|
||||
func TestIntegrationAnonDeviceService_localCacheSafety(t *testing.T) {
|
||||
fakeStore := remotecache.NewFakeStore(t)
|
||||
anonService := ProvideAnonymousSessionService(fakeStore, &usagestats.UsageStatsMock{})
|
||||
anonService := ProvideAnonymousDeviceService(fakeStore, &usagestats.UsageStatsMock{})
|
||||
|
||||
req := &http.Request{
|
||||
Header: http.Header{
|
||||
@ -161,17 +161,17 @@ func TestIntegrationAnonSessionService_localCacheSafety(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
anonSession := &AnonSession{
|
||||
anonDevice := &Device{
|
||||
ip: "10.30.30.2",
|
||||
userAgent: "test",
|
||||
}
|
||||
|
||||
key, err := anonSession.Key()
|
||||
key, err := anonDevice.Key()
|
||||
require.NoError(t, err)
|
||||
|
||||
anonService.localCache.SetDefault(key, true)
|
||||
|
||||
err = anonService.TagSession(context.Background(), req)
|
||||
err = anonService.TagDevice(context.Background(), req)
|
||||
require.NoError(t, err)
|
||||
|
||||
stats, err := anonService.usageStatFn(context.Background())
|
||||
|
@ -8,6 +8,6 @@ import (
|
||||
type FakeAnonymousSessionService struct {
|
||||
}
|
||||
|
||||
func (f *FakeAnonymousSessionService) TagSession(ctx context.Context, httpReq *http.Request) error {
|
||||
func (f *FakeAnonymousSessionService) TagDevice(ctx context.Context, httpReq *http.Request) error {
|
||||
return nil
|
||||
}
|
||||
|
@ -6,5 +6,5 @@ import (
|
||||
)
|
||||
|
||||
type Service interface {
|
||||
TagSession(context.Context, *http.Request) error
|
||||
TagDevice(context.Context, *http.Request) error
|
||||
}
|
||||
|
@ -57,7 +57,7 @@ func ProvideService(
|
||||
apikeyService apikey.Service, userService user.Service,
|
||||
jwtService auth.JWTVerifierService,
|
||||
usageStats usagestats.Service,
|
||||
anonSessionService anonymous.Service,
|
||||
anonDeviceService anonymous.Service,
|
||||
userProtectionService login.UserProtectionService,
|
||||
loginAttempts loginattempt.Service, quotaService quota.Service,
|
||||
authInfoService login.AuthInfoService, renderService rendering.Service,
|
||||
@ -88,7 +88,7 @@ func ProvideService(
|
||||
}
|
||||
|
||||
if s.cfg.AnonymousEnabled {
|
||||
s.RegisterClient(clients.ProvideAnonymous(cfg, orgService, anonSessionService))
|
||||
s.RegisterClient(clients.ProvideAnonymous(cfg, orgService, anonDeviceService))
|
||||
}
|
||||
|
||||
var proxyClients []authn.ProxyClient
|
||||
|
@ -14,20 +14,20 @@ import (
|
||||
|
||||
var _ authn.ContextAwareClient = new(Anonymous)
|
||||
|
||||
func ProvideAnonymous(cfg *setting.Cfg, orgService org.Service, anonSessionService anonymous.Service) *Anonymous {
|
||||
func ProvideAnonymous(cfg *setting.Cfg, orgService org.Service, anonDeviceService anonymous.Service) *Anonymous {
|
||||
return &Anonymous{
|
||||
cfg: cfg,
|
||||
log: log.New("authn.anonymous"),
|
||||
orgService: orgService,
|
||||
anonSessionService: anonSessionService,
|
||||
cfg: cfg,
|
||||
log: log.New("authn.anonymous"),
|
||||
orgService: orgService,
|
||||
anonDeviceService: anonDeviceService,
|
||||
}
|
||||
}
|
||||
|
||||
type Anonymous struct {
|
||||
cfg *setting.Cfg
|
||||
log log.Logger
|
||||
orgService org.Service
|
||||
anonSessionService anonymous.Service
|
||||
cfg *setting.Cfg
|
||||
log log.Logger
|
||||
orgService org.Service
|
||||
anonDeviceService anonymous.Service
|
||||
}
|
||||
|
||||
func (a *Anonymous) Name() string {
|
||||
@ -54,7 +54,7 @@ func (a *Anonymous) Authenticate(ctx context.Context, r *authn.Request) (*authn.
|
||||
a.log.Warn("tag anon session panic", "err", err)
|
||||
}
|
||||
}()
|
||||
if err := a.anonSessionService.TagSession(context.Background(), httpReqCopy); err != nil {
|
||||
if err := a.anonDeviceService.TagDevice(context.Background(), httpReqCopy); err != nil {
|
||||
a.log.Warn("failed to tag anonymous session", "error", err)
|
||||
}
|
||||
}()
|
||||
|
@ -46,10 +46,10 @@ func TestAnonymous_Authenticate(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.desc, func(t *testing.T) {
|
||||
c := Anonymous{
|
||||
cfg: tt.cfg,
|
||||
log: log.NewNopLogger(),
|
||||
orgService: &orgtest.FakeOrgService{ExpectedOrg: tt.org, ExpectedError: tt.err},
|
||||
anonSessionService: &anontest.FakeAnonymousSessionService{},
|
||||
cfg: tt.cfg,
|
||||
log: log.NewNopLogger(),
|
||||
orgService: &orgtest.FakeOrgService{ExpectedOrg: tt.org, ExpectedError: tt.err},
|
||||
anonDeviceService: &anontest.FakeAnonymousSessionService{},
|
||||
}
|
||||
|
||||
identity, err := c.Authenticate(context.Background(), &authn.Request{})
|
||||
|
@ -52,50 +52,50 @@ func ProvideService(cfg *setting.Cfg, tokenService auth.UserTokenService, jwtSer
|
||||
tracer tracing.Tracer, authProxy *authproxy.AuthProxy, loginService login.Service,
|
||||
apiKeyService apikey.Service, authenticator loginpkg.Authenticator, userService user.Service,
|
||||
orgService org.Service, oauthTokenService oauthtoken.OAuthTokenService, features *featuremgmt.FeatureManager,
|
||||
authnService authn.Service, anonSessionService anonymous.Service,
|
||||
authnService authn.Service, anonDeviceService anonymous.Service,
|
||||
) *ContextHandler {
|
||||
return &ContextHandler{
|
||||
Cfg: cfg,
|
||||
AuthTokenService: tokenService,
|
||||
JWTAuthService: jwtService,
|
||||
RemoteCache: remoteCache,
|
||||
RenderService: renderService,
|
||||
SQLStore: sqlStore,
|
||||
tracer: tracer,
|
||||
authProxy: authProxy,
|
||||
authenticator: authenticator,
|
||||
loginService: loginService,
|
||||
apiKeyService: apiKeyService,
|
||||
userService: userService,
|
||||
orgService: orgService,
|
||||
oauthTokenService: oauthTokenService,
|
||||
features: features,
|
||||
authnService: authnService,
|
||||
anonSessionService: anonSessionService,
|
||||
singleflight: new(singleflight.Group),
|
||||
Cfg: cfg,
|
||||
AuthTokenService: tokenService,
|
||||
JWTAuthService: jwtService,
|
||||
RemoteCache: remoteCache,
|
||||
RenderService: renderService,
|
||||
SQLStore: sqlStore,
|
||||
tracer: tracer,
|
||||
authProxy: authProxy,
|
||||
authenticator: authenticator,
|
||||
loginService: loginService,
|
||||
apiKeyService: apiKeyService,
|
||||
userService: userService,
|
||||
orgService: orgService,
|
||||
oauthTokenService: oauthTokenService,
|
||||
features: features,
|
||||
authnService: authnService,
|
||||
anonDeviceService: anonDeviceService,
|
||||
singleflight: new(singleflight.Group),
|
||||
}
|
||||
}
|
||||
|
||||
// ContextHandler is a middleware.
|
||||
type ContextHandler struct {
|
||||
Cfg *setting.Cfg
|
||||
AuthTokenService auth.UserTokenService
|
||||
JWTAuthService auth.JWTVerifierService
|
||||
RemoteCache *remotecache.RemoteCache
|
||||
RenderService rendering.Service
|
||||
SQLStore db.DB
|
||||
tracer tracing.Tracer
|
||||
authProxy *authproxy.AuthProxy
|
||||
authenticator loginpkg.Authenticator
|
||||
loginService login.Service
|
||||
apiKeyService apikey.Service
|
||||
userService user.Service
|
||||
orgService org.Service
|
||||
oauthTokenService oauthtoken.OAuthTokenService
|
||||
features *featuremgmt.FeatureManager
|
||||
authnService authn.Service
|
||||
singleflight *singleflight.Group
|
||||
anonSessionService anonymous.Service
|
||||
Cfg *setting.Cfg
|
||||
AuthTokenService auth.UserTokenService
|
||||
JWTAuthService auth.JWTVerifierService
|
||||
RemoteCache *remotecache.RemoteCache
|
||||
RenderService rendering.Service
|
||||
SQLStore db.DB
|
||||
tracer tracing.Tracer
|
||||
authProxy *authproxy.AuthProxy
|
||||
authenticator loginpkg.Authenticator
|
||||
loginService login.Service
|
||||
apiKeyService apikey.Service
|
||||
userService user.Service
|
||||
orgService org.Service
|
||||
oauthTokenService oauthtoken.OAuthTokenService
|
||||
features *featuremgmt.FeatureManager
|
||||
authnService authn.Service
|
||||
singleflight *singleflight.Group
|
||||
anonDeviceService anonymous.Service
|
||||
// GetTime returns the current time.
|
||||
// Stubbable by tests.
|
||||
GetTime func() time.Time
|
||||
@ -282,7 +282,7 @@ func (h *ContextHandler) initContextWithAnonymousUser(reqContext *contextmodel.R
|
||||
reqContext.Logger.Warn("tag anon session panic", "err", err)
|
||||
}
|
||||
}()
|
||||
if err := h.anonSessionService.TagSession(context.Background(), httpReqCopy); err != nil {
|
||||
if err := h.anonDeviceService.TagDevice(context.Background(), httpReqCopy); err != nil {
|
||||
reqContext.Logger.Warn("Failed to tag anonymous session", "error", err)
|
||||
}
|
||||
}()
|
||||
|
Loading…
Reference in New Issue
Block a user