Skip to content

Commit

Permalink
Merge pull request #2688 from owncloud/fix_accounts_backend
Browse files Browse the repository at this point in the history
fix accounts backend regression from #2590
  • Loading branch information
wkloucek authored Oct 27, 2021
2 parents 1a01d21 + f45254b commit 5456f13
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 57 deletions.
2 changes: 2 additions & 0 deletions accounts/pkg/command/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion accounts/pkg/flagset/flagset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions accounts/pkg/proto/v0/accounts.pb.micro_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions accounts/pkg/service/v0/accounts_permission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
93 changes: 51 additions & 42 deletions accounts/pkg/service/v0/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
},
}

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,
},
}
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.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
Expand Down Expand Up @@ -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)
}
repo, err := storage.NewCS3Repo(cfg)
if err != nil {
logger.Fatal().Err(err).Msg("cs3 storage was configured but failed to start")
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")
}
return repo
}

// Service implements the AccountsServiceHandler interface
Expand Down
5 changes: 3 additions & 2 deletions ocis-pkg/indexer/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions ocis-pkg/indexer/index/disk/non_unique_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down
1 change: 1 addition & 0 deletions ocis-pkg/indexer/index/disk/unique_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down
16 changes: 4 additions & 12 deletions ocis-pkg/indexer/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -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),
Expand Down
1 change: 1 addition & 0 deletions ocis-pkg/indexer/indexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down
1 change: 1 addition & 0 deletions ocs/pkg/server/http/svc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,7 @@ func init() {
DemoUsersAndGroups: true,
},
Repo: accountsCfg.Repo{
Backend: "disk",
Disk: accountsCfg.Disk{
Path: dataPath,
},
Expand Down

0 comments on commit 5456f13

Please sign in to comment.