Auth: Remove userauth service (#58941)

* Auth: remove userauth service

* Use Revoke user tokens from UserAuthTokenService
* Add function to delete user auth info to UserAuthInfo service
This commit is contained in:
Karl Persson 2022-11-18 14:40:26 +01:00 committed by GitHub
parent e37fc888c0
commit b3406a8273
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 21 additions and 167 deletions

View File

@ -239,13 +239,13 @@ func (hs *HTTPServer) AdminDeleteUser(c *models.ReqContext) response.Response {
return nil
})
g.Go(func() error {
if err := hs.userAuthService.Delete(ctx, cmd.UserID); err != nil {
if err := hs.authInfoService.DeleteUserAuthInfo(ctx, cmd.UserID); err != nil {
return err
}
return nil
})
g.Go(func() error {
if err := hs.userAuthService.DeleteToken(ctx, cmd.UserID); err != nil {
if err := hs.AuthTokenService.RevokeAllUserTokens(ctx, cmd.UserID); err != nil {
return err
}
return nil

View File

@ -21,7 +21,6 @@ import (
"github.com/grafana/grafana/pkg/services/querylibrary"
"github.com/grafana/grafana/pkg/services/searchV2"
"github.com/grafana/grafana/pkg/services/store/object/httpobjectstore"
"github.com/grafana/grafana/pkg/services/userauth"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
@ -207,7 +206,6 @@ type HTTPServer struct {
accesscontrolService accesscontrol.Service
annotationsRepo annotations.Repository
tagService tag.Service
userAuthService userauth.Service
oauthTokenService oauthtoken.OAuthTokenService
}
@ -250,8 +248,7 @@ func ProvideHTTPServer(opts ServerOptions, cfg *setting.Cfg, routeRegister routi
loginAttemptService loginAttempt.Service, orgService org.Service, teamService team.Service,
accesscontrolService accesscontrol.Service, dashboardThumbsService thumbs.DashboardThumbService, navTreeService navtree.Service,
annotationRepo annotations.Repository, tagService tag.Service, searchv2HTTPService searchV2.SearchHTTPService,
userAuthService userauth.Service, queryLibraryHTTPService querylibrary.HTTPService, queryLibraryService querylibrary.Service,
oauthTokenService oauthtoken.OAuthTokenService,
queryLibraryHTTPService querylibrary.HTTPService, queryLibraryService querylibrary.Service, oauthTokenService oauthtoken.OAuthTokenService,
) (*HTTPServer, error) {
web.Env = cfg.Env
m := web.New()
@ -353,7 +350,6 @@ func ProvideHTTPServer(opts ServerOptions, cfg *setting.Cfg, routeRegister routi
accesscontrolService: accesscontrolService,
annotationsRepo: annotationRepo,
tagService: tagService,
userAuthService: userAuthService,
QueryLibraryHTTPService: queryLibraryHTTPService,
QueryLibraryService: queryLibraryService,
oauthTokenService: oauthTokenService,

View File

@ -7,7 +7,6 @@ import (
"context"
"github.com/google/wire"
"github.com/grafana/grafana/pkg/services/auth/authimpl"
"github.com/grafana/grafana/pkg/tsdb/parca"
"github.com/grafana/grafana/pkg/tsdb/phlare"
@ -53,6 +52,7 @@ import (
"github.com/grafana/grafana/pkg/services/accesscontrol/ossaccesscontrol"
"github.com/grafana/grafana/pkg/services/alerting"
"github.com/grafana/grafana/pkg/services/auth"
"github.com/grafana/grafana/pkg/services/auth/authimpl"
"github.com/grafana/grafana/pkg/services/auth/jwt"
"github.com/grafana/grafana/pkg/services/cleanup"
"github.com/grafana/grafana/pkg/services/comments"
@ -129,7 +129,6 @@ import (
"github.com/grafana/grafana/pkg/services/thumbs"
"github.com/grafana/grafana/pkg/services/updatechecker"
"github.com/grafana/grafana/pkg/services/user/userimpl"
"github.com/grafana/grafana/pkg/services/userauth/userauthimpl"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/tsdb/azuremonitor"
"github.com/grafana/grafana/pkg/tsdb/cloudmonitoring"
@ -327,7 +326,6 @@ var wireSet = wire.NewSet(
userimpl.ProvideService,
orgimpl.ProvideService,
teamimpl.ProvideService,
userauthimpl.ProvideService,
ngmetrics.ProvideServiceForTest,
notifications.MockNotificationService,
objectdummyserver.ProvideFakeObjectServer,

View File

@ -143,7 +143,6 @@ import (
"github.com/grafana/grafana/pkg/services/thumbs/dashboardthumbsimpl"
"github.com/grafana/grafana/pkg/services/updatechecker"
"github.com/grafana/grafana/pkg/services/user/userimpl"
"github.com/grafana/grafana/pkg/services/userauth/userauthimpl"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/tsdb/azuremonitor"
"github.com/grafana/grafana/pkg/tsdb/cloudmonitoring"
@ -368,7 +367,6 @@ var wireBasicSet = wire.NewSet(
teamimpl.ProvideService,
tempuserimpl.ProvideService,
loginattemptimpl.ProvideService,
userauthimpl.ProvideService,
secretsMigrations.ProvideDataSourceMigrationService,
secretsMigrations.ProvideMigrateToPluginService,
secretsMigrations.ProvideMigrateFromPluginService,

View File

@ -13,6 +13,7 @@ type AuthInfoService interface {
GetExternalUserInfoByLogin(ctx context.Context, query *models.GetExternalUserInfoByLoginQuery) error
SetAuthInfo(ctx context.Context, cmd *models.SetAuthInfoCommand) error
UpdateAuthInfo(ctx context.Context, cmd *models.UpdateAuthInfoCommand) error
DeleteUserAuthInfo(ctx context.Context, userID int64) error
}
const (

View File

@ -218,6 +218,14 @@ func (s *AuthInfoStore) DeleteAuthInfo(ctx context.Context, cmd *models.DeleteAu
})
}
func (s *AuthInfoStore) DeleteUserAuthInfo(ctx context.Context, userID int64) error {
return s.sqlStore.WithDbSession(ctx, func(sess *db.Session) error {
var rawSQL = "DELETE FROM user_auth WHERE user_id = ?"
_, err := sess.Exec(rawSQL, userID)
return err
})
}
func (s *AuthInfoStore) GetUserById(ctx context.Context, id int64) (*user.User, error) {
query := user.GetUserByIDQuery{ID: id}
user, err := s.userService.GetByID(ctx, &query)

View File

@ -197,6 +197,10 @@ func (s *Implementation) GetExternalUserInfoByLogin(ctx context.Context, query *
return s.authInfoStore.GetExternalUserInfoByLogin(ctx, query)
}
func (s *Implementation) DeleteUserAuthInfo(ctx context.Context, userID int64) error {
return nil
}
func (s *Implementation) Run(ctx context.Context) error {
s.logger.Debug("Started AuthInfo Metrics collection service")
return s.authInfoStore.RunMetricsCollection(ctx)

View File

@ -57,6 +57,10 @@ func (a *AuthInfoServiceFake) GetExternalUserInfoByLogin(ctx context.Context, qu
return a.ExpectedError
}
func (a *AuthInfoServiceFake) DeleteUserAuthInfo(ctx context.Context, userID int64) error {
return a.ExpectedError
}
type AuthenticatorFake struct {
ExpectedUser *user.User
ExpectedError error

View File

@ -1,8 +0,0 @@
package userauth
import "context"
type Service interface {
Delete(context.Context, int64) error
DeleteToken(context.Context, int64) error
}

View File

@ -1,32 +0,0 @@
package userauthimpl
import (
"context"
"github.com/grafana/grafana/pkg/infra/db"
)
type store interface {
Delete(context.Context, int64) error
DeleteToken(context.Context, int64) error
}
type sqlStore struct {
db db.DB
}
func (ss *sqlStore) Delete(ctx context.Context, userID int64) error {
return ss.db.WithDbSession(ctx, func(sess *db.Session) error {
var rawSQL = "DELETE FROM user_auth WHERE user_id = ?"
_, err := sess.Exec(rawSQL, userID)
return err
})
}
func (ss *sqlStore) DeleteToken(ctx context.Context, userID int64) error {
return ss.db.WithDbSession(ctx, func(sess *db.Session) error {
var rawSQL = "DELETE FROM user_auth_token WHERE user_id = ?"
_, err := sess.Exec(rawSQL, userID)
return err
})
}

View File

@ -1,31 +0,0 @@
package userauthimpl
import (
"context"
"testing"
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/infra/db"
)
func TestIntegrationUserAuthDataAccess(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
ss := db.InitTestDB(t)
userAuthStore := sqlStore{
db: ss,
}
t.Run("delete user auth", func(t *testing.T) {
err := userAuthStore.Delete(context.Background(), 1)
require.NoError(t, err)
})
t.Run("delete user auth token", func(t *testing.T) {
err := userAuthStore.DeleteToken(context.Background(), 1)
require.NoError(t, err)
})
}

View File

@ -1,28 +0,0 @@
package userauthimpl
import (
"context"
"github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/services/userauth"
)
type Service struct {
store store
}
func ProvideService(db db.DB) userauth.Service {
return &Service{
store: &sqlStore{
db: db,
},
}
}
func (s *Service) Delete(ctx context.Context, userID int64) error {
return s.store.Delete(ctx, userID)
}
func (s *Service) DeleteToken(ctx context.Context, userID int64) error {
return s.store.DeleteToken(ctx, userID)
}

View File

@ -1,37 +0,0 @@
package userauthimpl
import (
"context"
"testing"
"github.com/stretchr/testify/require"
)
func TestUserAuthService(t *testing.T) {
userAuthStore := &FakeUserAuthStore{}
userAuthService := Service{
store: userAuthStore,
}
t.Run("delete user", func(t *testing.T) {
err := userAuthService.Delete(context.Background(), 1)
require.NoError(t, err)
})
t.Run("delete token", func(t *testing.T) {
err := userAuthService.DeleteToken(context.Background(), 1)
require.NoError(t, err)
})
}
type FakeUserAuthStore struct {
ExpectedError error
}
func (f *FakeUserAuthStore) Delete(ctx context.Context, userID int64) error {
return f.ExpectedError
}
func (f *FakeUserAuthStore) DeleteToken(ctx context.Context, userID int64) error {
return f.ExpectedError
}

View File

@ -1,19 +0,0 @@
package userauthtest
import "context"
type FakeUserAuthService struct {
ExpectedError error
}
func NewFakeUserAuthService() *FakeUserAuthService {
return &FakeUserAuthService{}
}
func (f *FakeUserAuthService) Delete(ctx context.Context, userID int64) error {
return f.ExpectedError
}
func (f *FakeUserAuthService) DeleteToken(ctx context.Context, userID int64) error {
return f.ExpectedError
}