From 2ce3e81490251c71a0e7adccb5ec5cfd3e5d2472 Mon Sep 17 00:00:00 2001 From: Ibrahim Serdar Acikgoz Date: Fri, 21 Oct 2022 12:24:36 +0300 Subject: [PATCH] move file backend to the platform service (#21365) --- app/notification_push_test.go | 9 ++++---- app/options.go | 2 +- app/platform/options.go | 8 +++++++ app/platform/service.go | 19 ++++++++++++++++ app/server.go | 15 +++--------- app/server_test.go | 43 +++++++++++++++++++---------------- 6 files changed, 58 insertions(+), 38 deletions(-) diff --git a/app/notification_push_test.go b/app/notification_push_test.go index 6dd1b5a2ff..73bf38e72b 100644 --- a/app/notification_push_test.go +++ b/app/notification_push_test.go @@ -1440,20 +1440,19 @@ func TestPushNotificationRace(t *testing.T) { Return(&model.Preference{Value: "test"}, nil) mockStore.On("Preference").Return(&mockPreferenceStore) s := &Server{ - products: make(map[string]Product), - Router: mux.NewRouter(), - filestore: &fmocks.FileBackend{}, + products: make(map[string]Product), + Router: mux.NewRouter(), } var err error s.platform, err = platform.New(platform.ServiceConfig{ ConfigStore: memoryStore, - }) + }, platform.SetFileStore(&fmocks.FileBackend{})) s.SetStore(mockStore) require.NoError(t, err) serviceMap := map[ServiceKey]any{ ConfigKey: s.platform, LicenseKey: &licenseWrapper{s}, - FilestoreKey: s.filestore, + FilestoreKey: s.FileBackend(), } ch, err := NewChannels(s, serviceMap) require.NoError(t, err) diff --git a/app/options.go b/app/options.go index 15725bf4c3..19133c42fc 100644 --- a/app/options.go +++ b/app/options.go @@ -54,7 +54,7 @@ func ConfigStore(configStore *config.Store) Option { func SetFileStore(filestore filestore.FileBackend) Option { return func(s *Server) error { - s.filestore = filestore + s.platformOptions = append(s.platformOptions, platform.SetFileStore(filestore)) return nil } } diff --git a/app/platform/options.go b/app/platform/options.go index 072914e104..65471b63f6 100644 --- a/app/platform/options.go +++ b/app/platform/options.go @@ -11,6 +11,7 @@ import ( "github.com/mattermost/mattermost-server/v6/config" "github.com/mattermost/mattermost-server/v6/einterfaces" "github.com/mattermost/mattermost-server/v6/model" + "github.com/mattermost/mattermost-server/v6/shared/filestore" "github.com/mattermost/mattermost-server/v6/shared/mlog" "github.com/mattermost/mattermost-server/v6/store" "github.com/mattermost/mattermost-server/v6/store/localcachelayer" @@ -74,6 +75,13 @@ func Config(dsn string, readOnly bool, configDefaults *model.Config) Option { } } +func SetFileStore(filestore filestore.FileBackend) Option { + return func(ps *PlatformService) error { + ps.filestore = filestore + return nil + } +} + // ConfigStore applies the given config store, typically to replace the traditional sources with a memory store for testing. func ConfigStore(configStore *config.Store) Option { return func(ps *PlatformService) error { diff --git a/app/platform/service.go b/app/platform/service.go index d2104fb2bb..6620b7f487 100644 --- a/app/platform/service.go +++ b/app/platform/service.go @@ -20,6 +20,7 @@ import ( "github.com/mattermost/mattermost-server/v6/services/cache" "github.com/mattermost/mattermost-server/v6/services/searchengine" "github.com/mattermost/mattermost-server/v6/services/searchengine/bleveengine" + "github.com/mattermost/mattermost-server/v6/shared/filestore" "github.com/mattermost/mattermost-server/v6/shared/mlog" "github.com/mattermost/mattermost-server/v6/store" "github.com/mattermost/mattermost-server/v6/store/localcachelayer" @@ -41,6 +42,8 @@ type PlatformService struct { configStore *config.Store + filestore filestore.FileBackend + cacheProvider cache.Provider statusCache cache.Cache sessionCache cache.Cache @@ -213,6 +216,18 @@ func New(sc ServiceConfig, options ...Option) (*PlatformService, error) { } } + license := ps.License() + // Step 3: Initialize filestore + if ps.filestore == nil { + insecure := ps.Config().ServiceSettings.EnableInsecureOutgoingConnections + backend, err2 := filestore.NewFileBackend(ps.Config().FileSettings.ToFileBackendSettings(license != nil && *license.Features.Compliance, insecure != nil && *insecure)) + if err2 != nil { + return nil, fmt.Errorf("failed to initialize filebackend: %w", err2) + } + + ps.filestore = backend + } + var err error ps.Store, err = ps.newStore() if err != nil { @@ -431,3 +446,7 @@ func (ps *PlatformService) GetPluginStatuses() (model.PluginStatuses, *model.App return pluginStatuses, nil } + +func (ps *PlatformService) FileBackend() filestore.FileBackend { + return ps.filestore +} diff --git a/app/server.go b/app/server.go index a4688c8b43..0216fa240f 100644 --- a/app/server.go +++ b/app/server.go @@ -137,7 +137,6 @@ type Server struct { openGraphDataCache cache.Cache clusterLeaderListenerId string loggerLicenseListenerId string - filestore filestore.FileBackend platform *platform.PlatformService platformOptions []platform.Option @@ -239,15 +238,6 @@ func NewServer(options ...Option) (*Server, error) { s.LoadLicense() } - license := s.License() - insecure := s.platform.Config().ServiceSettings.EnableInsecureOutgoingConnections - // Step 3: Initialize filestore - backend, err := filestore.NewFileBackend(s.platform.Config().FileSettings.ToFileBackendSettings(license != nil && *license.Features.Compliance, insecure != nil && *insecure)) - if err != nil { - return nil, errors.Wrap(err, "failed to initialize filebackend") - } - s.filestore = backend - s.licenseWrapper = &licenseWrapper{ srv: s, } @@ -272,7 +262,7 @@ func NewServer(options ...Option) (*Server, error) { ChannelKey: &channelsWrapper{srv: s}, ConfigKey: s.platform, LicenseKey: s.licenseWrapper, - FilestoreKey: s.filestore, + FilestoreKey: s.platform.FileBackend(), FileInfoStoreKey: &fileInfoWrapper{srv: s}, ClusterKey: s.platform, UserKey: New(ServerConnector(s.Channels())), @@ -439,6 +429,7 @@ func NewServer(options ...Option) (*Server, error) { mlog.Info("Printing current working", mlog.String("directory", pwd)) mlog.Info("Loaded config", mlog.String("source", s.platform.DescribeConfig())) + license := s.License() allowAdvancedLogging := license != nil && *license.Features.AdvancedLogging if s.Audit == nil { @@ -1425,7 +1416,7 @@ func (s *Server) SendRemoveExpiredLicenseEmail(email string, renewalLink, locale } func (s *Server) FileBackend() filestore.FileBackend { - return s.filestore + return s.platform.FileBackend() } func (s *Server) TotalWebsocketConnections() int { diff --git a/app/server_test.go b/app/server_test.go index f8135d0461..64b85ce938 100644 --- a/app/server_test.go +++ b/app/server_test.go @@ -90,28 +90,29 @@ func TestStartServerNoS3Bucket(t *testing.T) { } s3Endpoint := fmt.Sprintf("%s:%s", s3Host, s3Port) + configStore, _ := config.NewFileStore("config.json", true) + store, _ := config.NewStoreFromBacking(configStore, nil, false) + + cfg := store.Get() + cfg.FileSettings = model.FileSettings{ + DriverName: model.NewString(model.ImageDriverS3), + AmazonS3AccessKeyId: model.NewString(model.MinioAccessKey), + AmazonS3SecretAccessKey: model.NewString(model.MinioSecretKey), + AmazonS3Bucket: model.NewString("nosuchbucket"), + AmazonS3Endpoint: model.NewString(s3Endpoint), + AmazonS3Region: model.NewString(""), + AmazonS3PathPrefix: model.NewString(""), + AmazonS3SSL: model.NewBool(false), + } + *cfg.ServiceSettings.ListenAddress = ":0" + _, _, err := store.Set(cfg) + require.NoError(t, err) s, err := NewServer(func(server *Server) error { - configStore, _ := config.NewFileStore("config.json", true) - store, _ := config.NewStoreFromBacking(configStore, nil, false) - var err error - server.platform, err = platform.New(platform.ServiceConfig{ - ConfigStore: store, - }) - require.NoError(t, err) - server.platform.UpdateConfig(func(cfg *model.Config) { - cfg.FileSettings = model.FileSettings{ - DriverName: model.NewString(model.ImageDriverS3), - AmazonS3AccessKeyId: model.NewString(model.MinioAccessKey), - AmazonS3SecretAccessKey: model.NewString(model.MinioSecretKey), - AmazonS3Bucket: model.NewString("nosuchbucket"), - AmazonS3Endpoint: model.NewString(s3Endpoint), - AmazonS3Region: model.NewString(""), - AmazonS3PathPrefix: model.NewString(""), - AmazonS3SSL: model.NewBool(false), - } - *cfg.ServiceSettings.ListenAddress = ":0" - }) + var err2 error + server.platform, err2 = platform.New(platform.ServiceConfig{}, platform.ConfigStore(store)) + require.NoError(t, err2) + return nil }) require.NoError(t, err) @@ -120,6 +121,8 @@ func TestStartServerNoS3Bucket(t *testing.T) { defer s.Shutdown() // ensure that a new bucket was created + require.IsType(t, &filestore.S3FileBackend{}, s.FileBackend()) + err = s.FileBackend().(*filestore.S3FileBackend).TestConnection() require.NoError(t, err) }