diff --git a/accounts/pkg/command/server.go b/accounts/pkg/command/server.go index 0378c39cd9..6dbabfaffa 100644 --- a/accounts/pkg/command/server.go +++ b/accounts/pkg/command/server.go @@ -30,6 +30,8 @@ func Server(cfg *config.Config) *cli.Command { cfg.HTTP.Root = strings.TrimSuffix(cfg.HTTP.Root, "/") } + cfg.Repo.Backend = strings.ToLower(cfg.Repo.Backend) + // When running on single binary mode the before hook from the root command won't get called. We manually // call this before hook from ocis command, so the configuration can be loaded. if !cfg.Supervised { diff --git a/accounts/pkg/flagset/flagset.go b/accounts/pkg/flagset/flagset.go index fc29ca6d1c..abc32ec571 100644 --- a/accounts/pkg/flagset/flagset.go +++ b/accounts/pkg/flagset/flagset.go @@ -184,7 +184,7 @@ func ServerWithConfig(cfg *config.Config) []cli.Flag { }, &cli.StringFlag{ Name: "storage-backend", - Value: flags.OverrideDefaultString(cfg.Repo.Disk.Path, "CS3"), + Value: flags.OverrideDefaultString(cfg.Repo.Backend, "CS3"), Usage: "Which backend to use to store accounts data (CS3 or disk)", EnvVars: []string{"ACCOUNTS_STORAGE_BACKEND"}, Destination: &cfg.Repo.Backend, diff --git a/accounts/pkg/proto/v0/accounts.pb.micro_test.go b/accounts/pkg/proto/v0/accounts.pb.micro_test.go index fa2ec37580..bcd53c9746 100644 --- a/accounts/pkg/proto/v0/accounts.pb.micro_test.go +++ b/accounts/pkg/proto/v0/accounts.pb.micro_test.go @@ -80,6 +80,7 @@ func init() { ) cfg := config.New() + cfg.Repo.Backend = "disk" cfg.Repo.Disk.Path = dataPath cfg.Server.DemoUsersAndGroups = true var hdlr *svc.Service diff --git a/accounts/pkg/service/v0/accounts_permission_test.go b/accounts/pkg/service/v0/accounts_permission_test.go index 6ebe751404..477369b663 100644 --- a/accounts/pkg/service/v0/accounts_permission_test.go +++ b/accounts/pkg/service/v0/accounts_permission_test.go @@ -33,6 +33,7 @@ var ( func init() { cfg := config.New() cfg.Server.Name = "accounts" + cfg.Repo.Backend = "disk" cfg.Repo.Disk.Path = dataPath logger := olog.NewLogger(olog.Color(true), olog.Pretty(true)) roleServiceMock = buildRoleServiceMock() diff --git a/accounts/pkg/service/v0/service.go b/accounts/pkg/service/v0/service.go index ffedcb5f6e..b95455204b 100644 --- a/accounts/pkg/service/v0/service.go +++ b/accounts/pkg/service/v0/service.go @@ -2,13 +2,14 @@ package service import ( "context" - "errors" "path" "path/filepath" "strconv" "strings" "time" + "github.com/pkg/errors" + "github.com/owncloud/ocis/ocis-pkg/service/grpc" "github.com/owncloud/ocis/accounts/pkg/storage" @@ -48,17 +49,22 @@ func New(opts ...Option) (s *Service, err error) { roleManager = &m } + storage, err := createMetadataStorage(cfg, logger) + if err != nil { + return nil, errors.Wrap(err, "could not create metadata storage") + } + s = &Service{ id: cfg.GRPC.Namespace + "." + cfg.Server.Name, log: logger, Config: cfg, RoleService: roleService, RoleManager: roleManager, - repo: createMetadataStorage(cfg, logger), + repo: storage, } r := oreg.GetRegistry() - if strings.ToLower(cfg.Repo.Backend) != "disk" { + if cfg.Repo.Backend == "cs3" { if _, err := r.GetService("com.owncloud.storage.metadata"); err != nil { logger.Error().Err(err).Msg("index: storage-metadata service not present") return nil, err @@ -113,40 +119,41 @@ func configFromSvc(cfg *config.Config) (*idxcfg.Config, error) { } }(cfg) - if (config.Repo{}) != cfg.Repo { - if (config.Disk{}) != cfg.Repo.Disk { - c.Repo = idxcfg.Repo{ - Disk: idxcfg.Disk{ - Path: cfg.Repo.Disk.Path, - }, - } + switch cfg.Repo.Backend { + case "disk": + c.Repo = idxcfg.Repo{ + Backend: cfg.Repo.Backend, + Disk: idxcfg.Disk{ + Path: cfg.Repo.Disk.Path, + }, } + case "cs3": + c.Repo = idxcfg.Repo{ + Backend: cfg.Repo.Backend, + CS3: idxcfg.CS3{ + ProviderAddr: cfg.Repo.CS3.ProviderAddr, + DataURL: cfg.Repo.CS3.DataURL, + DataPrefix: cfg.Repo.CS3.DataPrefix, + JWTSecret: cfg.Repo.CS3.JWTSecret, + }, + } + default: + return nil, errors.New("index backend " + cfg.Repo.Backend + " is not supported") + } - if (config.CS3{}) != cfg.Repo.CS3 { - c.Repo = idxcfg.Repo{ - CS3: idxcfg.CS3{ - ProviderAddr: cfg.Repo.CS3.ProviderAddr, - DataURL: cfg.Repo.CS3.DataURL, - DataPrefix: cfg.Repo.CS3.DataPrefix, - JWTSecret: cfg.Repo.CS3.JWTSecret, - }, - } + if (config.Index{}) != cfg.Index { + c.Index = idxcfg.Index{ + UID: idxcfg.Bound{ + Lower: cfg.Index.UID.Lower, + }, + GID: idxcfg.Bound{ + Lower: cfg.Index.GID.Lower, + }, } + } - if (config.Index{}) != cfg.Index { - c.Index = idxcfg.Index{ - UID: idxcfg.Bound{ - Lower: cfg.Index.UID.Lower, - }, - GID: idxcfg.Bound{ - Lower: cfg.Index.GID.Lower, - }, - } - } - - if (config.ServiceUser{}) != cfg.ServiceUser { - c.ServiceUser = cfg.ServiceUser - } + if (config.ServiceUser{}) != cfg.ServiceUser { + c.ServiceUser = cfg.ServiceUser } return c, nil @@ -417,17 +424,19 @@ func (s Service) createDefaultGroups(withDemoGroups bool) (err error) { return nil } -func createMetadataStorage(cfg *config.Config, logger log.Logger) storage.Repo { - // for now we detect the used storage implementation based on which storage is configured - // the config with defaults needs to be checked last - if cfg.Repo.Disk.Path != "" { - return storage.NewDiskRepo(cfg, logger) +func createMetadataStorage(cfg *config.Config, logger log.Logger) (storage.Repo, error) { + switch cfg.Repo.Backend { + case "disk": + return storage.NewDiskRepo(cfg, logger), nil + case "cs3": + repo, err := storage.NewCS3Repo(cfg) + if err != nil { + return nil, errors.Wrap(err, "cs3 backend was configured but failed to start") + } + return repo, nil + default: + return nil, errors.New("backend type " + cfg.Repo.Backend + " is not supported") } - repo, err := storage.NewCS3Repo(cfg) - if err != nil { - logger.Fatal().Err(err).Msg("cs3 storage was configured but failed to start") - } - return repo } // Service implements the AccountsServiceHandler interface diff --git a/ocis-pkg/indexer/config/config.go b/ocis-pkg/indexer/config/config.go index b718f560c6..5d2b23f716 100644 --- a/ocis-pkg/indexer/config/config.go +++ b/ocis-pkg/indexer/config/config.go @@ -7,8 +7,9 @@ import ( // Repo defines which storage implementation is to be used. type Repo struct { - Disk Disk - CS3 CS3 + Backend string + Disk Disk + CS3 CS3 } // Disk is the local disk implementation of the storage. diff --git a/ocis-pkg/indexer/index/disk/non_unique_test.go b/ocis-pkg/indexer/index/disk/non_unique_test.go index b9f592d612..5d0a25ca08 100644 --- a/ocis-pkg/indexer/index/disk/non_unique_test.go +++ b/ocis-pkg/indexer/index/disk/non_unique_test.go @@ -83,6 +83,7 @@ func getNonUniqueIdxSut(t *testing.T, entity interface{}, indexBy string) (index dataPath, _ := WriteIndexTestData(Data, "ID", "") cfg := config.Config{ Repo: config.Repo{ + Backend: "disk", Disk: config.Disk{ Path: dataPath, }, diff --git a/ocis-pkg/indexer/index/disk/unique_test.go b/ocis-pkg/indexer/index/disk/unique_test.go index 2ecbfbe17e..fca28895ec 100644 --- a/ocis-pkg/indexer/index/disk/unique_test.go +++ b/ocis-pkg/indexer/index/disk/unique_test.go @@ -103,6 +103,7 @@ func getUniqueIdxSut(t *testing.T, indexBy string, entityType interface{}) (inde dataPath, _ := WriteIndexTestData(Data, "ID", "") cfg := config.Config{ Repo: config.Repo{ + Backend: "disk", Disk: config.Disk{ Path: dataPath, }, diff --git a/ocis-pkg/indexer/indexer.go b/ocis-pkg/indexer/indexer.go index 51a9967d44..1517734429 100644 --- a/ocis-pkg/indexer/indexer.go +++ b/ocis-pkg/indexer/indexer.go @@ -3,10 +3,11 @@ package indexer import ( "fmt" - "github.com/owncloud/ocis/ocis-pkg/sync" "path" "strings" + "github.com/owncloud/ocis/ocis-pkg/sync" + "github.com/CiscoM31/godata" "github.com/iancoleman/strcase" "github.com/owncloud/ocis/ocis-pkg/indexer/config" @@ -39,14 +40,6 @@ func CreateIndexer(cfg *config.Config) *Indexer { } } -func getRegistryStrategy(cfg *config.Config) string { - if cfg.Repo.Disk.Path != "" { - return "disk" - } - - return "cs3" -} - // Reset takes care of deleting all indices from storage and from the internal map of indices func (i *Indexer) Reset() error { for j := range i.indices { @@ -66,11 +59,10 @@ func (i *Indexer) Reset() error { // AddIndex adds a new index to the indexer receiver. func (i *Indexer) AddIndex(t interface{}, indexBy, pkName, entityDirName, indexType string, bound *option.Bound, caseInsensitive bool) error { - strategy := getRegistryStrategy(i.config) - f := registry.IndexConstructorRegistry[strategy][indexType] + f := registry.IndexConstructorRegistry[i.config.Repo.Backend][indexType] var idx index.Index - if strategy == "cs3" { + if i.config.Repo.Backend == "cs3" { idx = f( option.CaseInsensitive(caseInsensitive), option.WithEntity(t), diff --git a/ocis-pkg/indexer/indexer_test.go b/ocis-pkg/indexer/indexer_test.go index 47440f1a27..baf27a5117 100644 --- a/ocis-pkg/indexer/indexer_test.go +++ b/ocis-pkg/indexer/indexer_test.go @@ -300,6 +300,7 @@ func TestQueryDiskImpl(t *testing.T) { func createDiskIndexer(dataDir string) *Indexer { return CreateIndexer(&config.Config{ Repo: config.Repo{ + Backend: "disk", Disk: config.Disk{ Path: dataDir, }, diff --git a/ocs/pkg/server/http/svc_test.go b/ocs/pkg/server/http/svc_test.go index 28d4f4d1fd..ee896414fc 100644 --- a/ocs/pkg/server/http/svc_test.go +++ b/ocs/pkg/server/http/svc_test.go @@ -542,6 +542,7 @@ func init() { DemoUsersAndGroups: true, }, Repo: accountsCfg.Repo{ + Backend: "disk", Disk: accountsCfg.Disk{ Path: dataPath, },