From 00ea1f186a4caec8c072e5f038222a9d2274692a Mon Sep 17 00:00:00 2001 From: Benedikt Kulmann Date: Wed, 15 Jul 2020 08:25:27 +0200 Subject: [PATCH 1/2] Refactor indexing of new entities into own function This also fixes that on Create and Update calls the index was not updated properly. --- pkg/service/v0/accounts.go | 35 +++++++++++++++++++---------------- pkg/service/v0/groups.go | 33 ++++++++++++++++++--------------- 2 files changed, 37 insertions(+), 31 deletions(-) diff --git a/pkg/service/v0/accounts.go b/pkg/service/v0/accounts.go index deb23e2e34..e940e72ef6 100644 --- a/pkg/service/v0/accounts.go +++ b/pkg/service/v0/accounts.go @@ -41,23 +41,28 @@ func (s Service) indexAccounts(path string) (err error) { return } for _, file := range list { - a := &proto.BleveAccount{ - BleveType: "account", - } - if err = s.loadAccount(file.Name(), &a.Account); err != nil { - s.log.Error().Err(err).Str("account", file.Name()).Msg("could not load account") - continue - } - s.log.Debug().Interface("account", a).Msg("found account") - if err = s.index.Index(a.Id, a); err != nil { - s.log.Error().Err(err).Interface("account", a).Msg("could not index account") - continue - } + _ = s.indexAccount(file.Name()) } return } +func (s Service) indexAccount(id string) error { + a := &proto.BleveAccount{ + BleveType: "account", + } + if err := s.loadAccount(id, &a.Account); err != nil { + s.log.Error().Err(err).Str("account", id).Msg("could not load account") + return err + } + s.log.Debug().Interface("account", a).Msg("found account") + if err := s.index.Index(a.Id, a); err != nil { + s.log.Error().Err(err).Interface("account", a).Msg("could not index account") + return err + } + return nil +} + // an auth request is currently hardcoded and has to match this regex // login eq \"teddy\" and password eq \"F&1!b90t111!\" var authQuery = regexp.MustCompile(`^login eq '(.*)' and password eq '(.*)'$`) // TODO how is ' escaped in the password? @@ -272,7 +277,6 @@ func (s Service) CreateAccount(c context.Context, in *proto.CreateAccountRequest if id, err = cleanupID(in.Account.Id); err != nil { return merrors.InternalServerError(s.id, "could not clean up account id: %v", err.Error()) } - path := filepath.Join(s.Config.Server.AccountsDataPath, "accounts", id) if in.Account.PasswordProfile != nil && in.Account.PasswordProfile.Password != "" { // encrypt password @@ -290,8 +294,7 @@ func (s Service) CreateAccount(c context.Context, in *proto.CreateAccountRequest return } - if err = s.index.Index(id, in.Account); err != nil { - s.log.Error().Err(err).Str("id", id).Str("path", path).Interface("account", loggableAccount(in.Account)).Msg("could not index new account") + if err = s.indexAccount(in.Account.Id); err != nil { return merrors.InternalServerError(s.id, "could not index new account: %v", err.Error()) } @@ -373,7 +376,7 @@ func (s Service) UpdateAccount(c context.Context, in *proto.UpdateAccountRequest return } - if err = s.index.Index(id, out); err != nil { + if err = s.indexAccount(id); err != nil { s.log.Error().Err(err).Str("id", id).Str("path", path).Interface("account", loggableAccount(out)).Msg("could not index new account") return merrors.InternalServerError(s.id, "could not index updated account: %v", err.Error()) } diff --git a/pkg/service/v0/groups.go b/pkg/service/v0/groups.go index 91c379c5b0..83989ecb97 100644 --- a/pkg/service/v0/groups.go +++ b/pkg/service/v0/groups.go @@ -29,23 +29,28 @@ func (s Service) indexGroups(path string) (err error) { return } for _, file := range list { - g := &proto.BleveGroup{ - BleveType: "group", - } - if err = s.loadGroup(file.Name(), &g.Group); err != nil { - s.log.Error().Err(err).Str("group", file.Name()).Msg("could not load group") - continue - } - s.log.Debug().Interface("group", g).Msg("found group") - if err = s.index.Index(g.Id, g); err != nil { - s.log.Error().Err(err).Interface("group", g).Msg("could not index group") - continue - } + _ = s.indexGroup(file.Name()) } return } +func (s Service) indexGroup(id string) error { + g := &proto.BleveGroup{ + BleveType: "group", + } + if err := s.loadGroup(id, &g.Group); err != nil { + s.log.Error().Err(err).Str("group", id).Msg("could not load group") + return err + } + s.log.Debug().Interface("group", g).Msg("found group") + if err := s.index.Index(g.Id, g); err != nil { + s.log.Error().Err(err).Interface("group", g).Msg("could not index group") + return err + } + return nil +} + func (s Service) loadGroup(id string, g *proto.Group) (err error) { path := filepath.Join(s.Config.Server.AccountsDataPath, "groups", id) @@ -205,7 +210,6 @@ func (s Service) CreateGroup(c context.Context, in *proto.CreateGroupRequest, ou if id, err = cleanupID(in.Group.Id); err != nil { return merrors.InternalServerError(s.id, "could not clean up account id: %v", err.Error()) } - path := filepath.Join(s.Config.Server.AccountsDataPath, "groups", id) // extract member id s.deflateMembers(in.Group) @@ -215,8 +219,7 @@ func (s Service) CreateGroup(c context.Context, in *proto.CreateGroupRequest, ou return } - if err = s.index.Index(id, in.Group); err != nil { - s.log.Error().Err(err).Str("id", id).Str("path", path).Interface("group", in.Group).Msg("could not index new group") + if err = s.indexGroup(id); err != nil { return merrors.InternalServerError(s.id, "could not index new group: %v", err.Error()) } From 0fba802f572f5265dcaade441691d62cfe6e2a73 Mon Sep 17 00:00:00 2001 From: Benedikt Kulmann Date: Wed, 15 Jul 2020 08:31:35 +0200 Subject: [PATCH 2/2] Add changelog item --- changelog/unreleased/fix-indexing.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 changelog/unreleased/fix-indexing.md diff --git a/changelog/unreleased/fix-indexing.md b/changelog/unreleased/fix-indexing.md new file mode 100644 index 0000000000..01ebab8a76 --- /dev/null +++ b/changelog/unreleased/fix-indexing.md @@ -0,0 +1,6 @@ +Change: Fix index update on create/update + +We fixed a bug in creating/updating accounts and groups, that caused new entities not to show up in list queries. + +https://github.com/owncloud/ocis-accounts/issues/57 +https://github.com/owncloud/ocis-accounts/pull/59