diff --git a/accounts/pkg/config/config.go b/accounts/pkg/config/config.go index bb22ecf6a4..358d0ac895 100644 --- a/accounts/pkg/config/config.go +++ b/accounts/pkg/config/config.go @@ -80,7 +80,7 @@ type CS3 struct { JWTSecret string } -// ServiceUser defines the user required for EOS +// ServiceUser defines the user required for EOS. type ServiceUser struct { UUID string Username string @@ -88,6 +88,15 @@ type ServiceUser struct { GID int64 } +// Index defines config for indexes. +type Index struct { + UID, GID Bound +} + +type Bound struct { + Lower, Upper int64 +} + // Config merges all Account config parameters. type Config struct { LDAP LDAP @@ -98,6 +107,7 @@ type Config struct { Log Log TokenManager TokenManager Repo Repo + Index Index ServiceUser ServiceUser } diff --git a/accounts/pkg/flagset/flagset.go b/accounts/pkg/flagset/flagset.go index e236426368..f478ee0a01 100644 --- a/accounts/pkg/flagset/flagset.go +++ b/accounts/pkg/flagset/flagset.go @@ -155,6 +155,34 @@ func ServerWithConfig(cfg *config.Config) []cli.Flag { EnvVars: []string{"ACCOUNTS_SERVICE_USER_GID"}, Destination: &cfg.ServiceUser.GID, }, + &cli.Int64Flag{ + Name: "uid-index-lower-bound", + Value: 0, + Usage: "define a starting point for the account UID", + EnvVars: []string{"ACCOUNTS_UID_INDEX_LOWER_BOUND"}, + Destination: &cfg.Index.UID.Lower, + }, + &cli.Int64Flag{ + Name: "gid-index-lower-bound", + Value: 1000, + Usage: "define a starting point for the account GID", + EnvVars: []string{"ACCOUNTS_GID_INDEX_LOWER_BOUND"}, + Destination: &cfg.Index.GID.Lower, + }, + &cli.Int64Flag{ + Name: "uid-index-upper-bound", + Value: 0, + Usage: "define an ending point for the account UID", + EnvVars: []string{"ACCOUNTS_UID_INDEX_UPPER_BOUND"}, + Destination: &cfg.Index.UID.Upper, + }, + &cli.Int64Flag{ + Name: "gid-index-upper-bound", + Value: 1000, + Usage: "define an ending point for the account GID", + EnvVars: []string{"ACCOUNTS_GID_INDEX_UPPER_BOUND"}, + Destination: &cfg.Index.GID.Upper, + }, } } diff --git a/accounts/pkg/indexer/index/disk/autoincrement.go b/accounts/pkg/indexer/index/disk/autoincrement.go index 5e6c29db07..e019f4f4a1 100644 --- a/accounts/pkg/indexer/index/disk/autoincrement.go +++ b/accounts/pkg/indexer/index/disk/autoincrement.go @@ -69,6 +69,7 @@ var ( reflect.Int8, reflect.Int16, reflect.Int32, + reflect.Int64, } ) @@ -200,8 +201,6 @@ func isValidKind(k reflect.Kind) bool { func getKind(i interface{}, field string) (reflect.Kind, error) { r := reflect.ValueOf(i) - // TODO reflect.FieldByName panics. Recover from it. - // further read: https://blog.golang.org/defer-panic-and-recover return reflect.Indirect(r).FieldByName(field).Kind(), nil } @@ -238,5 +237,9 @@ func (idx Autoincrement) next() (int, error) { return -1, err } + if int64(latest) < idx.bound.Lower { + return int(idx.bound.Lower), nil + } + return latest + 1, nil } diff --git a/accounts/pkg/indexer/index/disk/autoincrement_test.go b/accounts/pkg/indexer/index/disk/autoincrement_test.go index def0304bec..cd0ecb2cca 100644 --- a/accounts/pkg/indexer/index/disk/autoincrement_test.go +++ b/accounts/pkg/indexer/index/disk/autoincrement_test.go @@ -8,6 +8,7 @@ import ( "github.com/owncloud/ocis/accounts/pkg/indexer/option" //. "github.com/owncloud/ocis/accounts/pkg/indexer/test" + "github.com/owncloud/ocis/accounts/pkg/proto/v0" "github.com/stretchr/testify/assert" ) @@ -217,6 +218,38 @@ func TestLowerBound(t *testing.T) { } } +func TestAdd(t *testing.T) { + tmpDir, err := createTmpDirStr() + assert.NoError(t, err) + + err = os.MkdirAll(filepath.Join(tmpDir, "data"), 0777) + assert.NoError(t, err) + + tmpFile, err := os.Create(filepath.Join(tmpDir, "data", "test-example")) + assert.NoError(t, err) + assert.NoError(t, tmpFile.Close()) + + i := NewAutoincrementIndex( + option.WithBounds(&option.Bound{ + Lower: 0, + Upper: 0, + }), + option.WithDataDir(tmpDir), + option.WithFilesDir(filepath.Join(tmpDir, "data")), + option.WithEntity(&proto.Account{}), + option.WithTypeName("owncloud.Account"), + option.WithIndexBy("UidNumber"), + ) + + err = i.Init() + assert.NoError(t, err) + + _, err = i.Add("test-example", "") + if err != nil { + t.Error(err) + } +} + func BenchmarkAdd(b *testing.B) { tmpDir, err := createTmpDirStr() assert.NoError(b, err) diff --git a/accounts/pkg/indexer/indexer.go b/accounts/pkg/indexer/indexer.go index 89fd4b1a6e..0b4e5e60c7 100644 --- a/accounts/pkg/indexer/indexer.go +++ b/accounts/pkg/indexer/indexer.go @@ -42,13 +42,15 @@ func getRegistryStrategy(cfg *config.Config) string { } // AddIndex adds a new index to the indexer receiver. -func (i Indexer) AddIndex(t interface{}, indexBy, pkName, entityDirName, indexType string) error { +func (i Indexer) AddIndex(t interface{}, indexBy, pkName, entityDirName, indexType string, bound *option.Bound) error { strategy := getRegistryStrategy(i.config) f := registry.IndexConstructorRegistry[strategy][indexType] var idx index.Index if strategy == "disk" { idx = f( + option.WithEntity(t), + option.WithBounds(bound), option.WithTypeName(getTypeFQN(t)), option.WithIndexBy(indexBy), option.WithFilesDir(path.Join(i.config.Repo.Disk.Path, entityDirName)), @@ -56,6 +58,8 @@ func (i Indexer) AddIndex(t interface{}, indexBy, pkName, entityDirName, indexTy ) } else if strategy == "cs3" { idx = f( + option.WithEntity(t), + option.WithBounds(bound), option.WithTypeName(getTypeFQN(t)), option.WithIndexBy(indexBy), option.WithFilesDir(path.Join(i.config.Repo.Disk.Path, entityDirName)), @@ -84,7 +88,7 @@ func (i Indexer) Add(t interface{}) ([]IdxAddResult, error) { if err != nil { return []IdxAddResult{}, err } - results = append(results, IdxAddResult{Field: fields.PKFieldName, Value: value}) + results = append(results, IdxAddResult{Field: idx.IndexBy(), Value: value}) } } } diff --git a/accounts/pkg/indexer/indexer_test.go b/accounts/pkg/indexer/indexer_test.go index 418a06bb09..b3cbbe5c2e 100644 --- a/accounts/pkg/indexer/indexer_test.go +++ b/accounts/pkg/indexer/indexer_test.go @@ -1,14 +1,15 @@ package indexer import ( + "os" + "path" + "testing" + "github.com/owncloud/ocis/accounts/pkg/config" _ "github.com/owncloud/ocis/accounts/pkg/indexer/index/cs3" _ "github.com/owncloud/ocis/accounts/pkg/indexer/index/disk" . "github.com/owncloud/ocis/accounts/pkg/indexer/test" "github.com/stretchr/testify/assert" - "os" - "path" - "testing" ) func TestIndexer_CS3_AddWithUniqueIndex(t *testing.T) { @@ -24,7 +25,7 @@ func TestIndexer_CS3_AddWithUniqueIndex(t *testing.T) { }, }) - err := indexer.AddIndex(&User{}, "UserName", "ID", "users", "unique") + err := indexer.AddIndex(&User{}, "UserName", "ID", "users", "unique", nil) assert.NoError(t, err) u := &User{ID: "abcdefg-123", UserName: "mikey", Email: "mikey@example.com"} @@ -47,7 +48,7 @@ func TestIndexer_CS3_AddWithNonUniqueIndex(t *testing.T) { }, }) - err := indexer.AddIndex(&User{}, "UserName", "ID", "users", "non_unique") + err := indexer.AddIndex(&User{}, "UserName", "ID", "users", "non_unique", nil) assert.NoError(t, err) u := &User{ID: "abcdefg-123", UserName: "mikey", Email: "mikey@example.com"} @@ -67,7 +68,7 @@ func TestIndexer_Disk_FindByWithUniqueIndex(t *testing.T) { }, }) - err := indexer.AddIndex(&User{}, "UserName", "ID", "users", "unique") + err := indexer.AddIndex(&User{}, "UserName", "ID", "users", "unique", nil) assert.NoError(t, err) u := &User{ID: "abcdefg-123", UserName: "mikey", Email: "mikey@example.com"} @@ -91,7 +92,7 @@ func TestIndexer_Disk_AddWithUniqueIndex(t *testing.T) { }, }) - err := indexer.AddIndex(&User{}, "UserName", "ID", "users", "unique") + err := indexer.AddIndex(&User{}, "UserName", "ID", "users", "unique", nil) assert.NoError(t, err) u := &User{ID: "abcdefg-123", UserName: "mikey", Email: "mikey@example.com"} @@ -111,7 +112,7 @@ func TestIndexer_Disk_AddWithNonUniqueIndex(t *testing.T) { }, }) - err := indexer.AddIndex(&Pet{}, "Kind", "ID", "pets", "non_unique") + err := indexer.AddIndex(&Pet{}, "Kind", "ID", "pets", "non_unique", nil) assert.NoError(t, err) pet1 := Pet{ID: "goefe-789", Kind: "Hog", Color: "Green", Name: "Dicky"} @@ -141,7 +142,7 @@ func TestIndexer_Disk_AddWithAutoincrementIndex(t *testing.T) { }, }) - err := indexer.AddIndex(&User{}, "UID", "ID", "users", "autoincrement") + err := indexer.AddIndex(&User{}, "UID", "ID", "users", "autoincrement", nil) assert.NoError(t, err) res1, err := indexer.Add(Data["users"][0]) @@ -172,7 +173,7 @@ func TestIndexer_Disk_DeleteWithNonUniqueIndex(t *testing.T) { }, }) - err := indexer.AddIndex(&Pet{}, "Kind", "ID", "pets", "non_unique") + err := indexer.AddIndex(&Pet{}, "Kind", "ID", "pets", "non_unique", nil) assert.NoError(t, err) pet1 := Pet{ID: "goefe-789", Kind: "Hog", Color: "Green", Name: "Dicky"} @@ -200,7 +201,7 @@ func TestIndexer_Disk_SearchWithNonUniqueIndex(t *testing.T) { }, }) - err := indexer.AddIndex(&Pet{}, "Name", "ID", "pets", "non_unique") + err := indexer.AddIndex(&Pet{}, "Name", "ID", "pets", "non_unique", nil) assert.NoError(t, err) pet1 := Pet{ID: "goefe-789", Kind: "Hog", Color: "Green", Name: "Dicky"} @@ -229,10 +230,10 @@ func TestIndexer_Disk_UpdateWithUniqueIndex(t *testing.T) { }, }) - err := indexer.AddIndex(&User{}, "UserName", "ID", "users", "unique") + err := indexer.AddIndex(&User{}, "UserName", "ID", "users", "unique", nil) assert.NoError(t, err) - err = indexer.AddIndex(&User{}, "Email", "ID", "users", "unique") + err = indexer.AddIndex(&User{}, "Email", "ID", "users", "unique", nil) assert.NoError(t, err) user1 := &User{ID: "abcdefg-123", UserName: "mikey", Email: "mikey@example.com"} @@ -287,7 +288,7 @@ func TestIndexer_Disk_UpdateWithNonUniqueIndex(t *testing.T) { }, }) - err := indexer.AddIndex(&Pet{}, "Name", "ID", "pets", "non_unique") + err := indexer.AddIndex(&Pet{}, "Name", "ID", "pets", "non_unique", nil) assert.NoError(t, err) pet1 := Pet{ID: "goefe-789", Kind: "Hog", Color: "Green", Name: "Dicky"} diff --git a/accounts/pkg/service/v0/accounts.go b/accounts/pkg/service/v0/accounts.go index d448742211..e8076365eb 100644 --- a/accounts/pkg/service/v0/accounts.go +++ b/accounts/pkg/service/v0/accounts.go @@ -351,14 +351,7 @@ func (s Service) CreateAccount(ctx context.Context, in *proto.CreateAccountReque acc.PasswordProfile.Password = "" } - { - out.Id = acc.Id - out.Mail = acc.Mail - out.PreferredName = acc.PreferredName - out.AccountEnabled = acc.AccountEnabled - out.DisplayName = acc.DisplayName - out.OnPremisesSamAccountName = acc.OnPremisesSamAccountName - } + out = acc // TODO: assign user role to all new users for now, as create Account request does not have any role field if s.RoleService == nil { diff --git a/accounts/pkg/service/v0/service.go b/accounts/pkg/service/v0/service.go index 19e23a53a5..e658e7b471 100644 --- a/accounts/pkg/service/v0/service.go +++ b/accounts/pkg/service/v0/service.go @@ -3,14 +3,17 @@ package service import ( "context" "errors" - "github.com/owncloud/ocis/accounts/pkg/indexer" - "github.com/owncloud/ocis/accounts/pkg/storage" "path" "path/filepath" "strconv" "strings" "time" + "github.com/owncloud/ocis/accounts/pkg/indexer/option" + + "github.com/owncloud/ocis/accounts/pkg/indexer" + "github.com/owncloud/ocis/accounts/pkg/storage" + mclient "github.com/micro/go-micro/v2/client" "github.com/owncloud/ocis/accounts/pkg/config" idxerrs "github.com/owncloud/ocis/accounts/pkg/indexer/errors" @@ -73,38 +76,41 @@ func (s Service) buildIndex() (*indexer.Indexer, error) { idx := indexer.CreateIndexer(s.Config) // Accounts - if err := idx.AddIndex(&proto.Account{}, "DisplayName", "Id", "accounts", "non_unique"); err != nil { + if err := idx.AddIndex(&proto.Account{}, "DisplayName", "Id", "accounts", "non_unique", nil); err != nil { return nil, err } - if err := idx.AddIndex(&proto.Account{}, "Mail", "Id", "accounts", "unique"); err != nil { + if err := idx.AddIndex(&proto.Account{}, "Mail", "Id", "accounts", "unique", nil); err != nil { return nil, err } - if err := idx.AddIndex(&proto.Account{}, "OnPremisesSamAccountName", "Id", "accounts", "unique"); err != nil { + if err := idx.AddIndex(&proto.Account{}, "OnPremisesSamAccountName", "Id", "accounts", "unique", nil); err != nil { return nil, err } - if err := idx.AddIndex(&proto.Account{}, "PreferredName", "Id", "accounts", "unique"); err != nil { + if err := idx.AddIndex(&proto.Account{}, "PreferredName", "Id", "accounts", "unique", nil); err != nil { return nil, err } - if err := idx.AddIndex(&proto.Account{}, "UidNumber", "Id", "accounts", "autoincrement"); err != nil { - return nil, err - } - if err := idx.AddIndex(&proto.Account{}, "GidNumber", "Id", "accounts", "autoincrement"); err != nil { + if err := idx.AddIndex(&proto.Account{}, "UidNumber", "Id", "accounts", "autoincrement", &option.Bound{ + Lower: s.Config.Index.UID.Lower, + Upper: s.Config.Index.UID.Upper, + }); err != nil { return nil, err } // Groups - if err := idx.AddIndex(&proto.Group{}, "OnPremisesSamAccountName", "Id", "groups", "unique"); err != nil { + if err := idx.AddIndex(&proto.Group{}, "OnPremisesSamAccountName", "Id", "groups", "unique", nil); err != nil { return nil, err } - if err := idx.AddIndex(&proto.Group{}, "DisplayName", "Id", "groups", "non_unique"); err != nil { + if err := idx.AddIndex(&proto.Group{}, "DisplayName", "Id", "groups", "non_unique", nil); err != nil { return nil, err } - if err := idx.AddIndex(&proto.Group{}, "GidNumber", "Id", "groups", "autoincrement"); err != nil { + if err := idx.AddIndex(&proto.Group{}, "GidNumber", "Id", "groups", "autoincrement", &option.Bound{ + Lower: s.Config.Index.GID.Lower, + Upper: s.Config.Index.GID.Upper, + }); err != nil { return nil, err }