From e3f9e9c05e1c8108ddb362fb6a55ba2f09f3f539 Mon Sep 17 00:00:00 2001 From: Fabi <38692350+fgerschwiler@users.noreply.github.com> Date: Thu, 28 May 2020 13:28:36 +0200 Subject: [PATCH] use global id generator (#142) * use global id generator * remove duplicate `UserRemoved` Co-authored-by: Livio Amstutz --- internal/eventstore/spooler/config.go | 7 ++--- .../repository/eventsourcing/eventstore.go | 13 +++++---- internal/org/repository/eventsourcing/org.go | 4 --- .../repository/eventsourcing/eventstore.go | 6 ++-- .../eventsourcing/eventstore_age.go | 6 ++-- .../eventsourcing/eventstore_age_mock_test.go | 6 ++++ .../eventsourcing/eventstore_complexity.go | 6 ++-- .../eventstore_complexity_mock_test.go | 1 + .../eventsourcing/eventstore_lockout.go | 6 ++-- .../eventstore_lockout_mock_test.go | 1 + .../repository/eventsourcing/eventstore.go | 28 ++++++++----------- .../eventsourcing/eventstore_mock_test.go | 6 ++-- .../project/repository/eventsourcing/oidc.go | 6 ++-- .../repository/eventsourcing/eventstore.go | 20 ++++++------- .../eventsourcing/eventstore_mock_test.go | 9 +++--- .../repository/eventsourcing/model/types.go | 1 - .../repository/eventsourcing/eventstore.go | 13 ++++----- .../eventsourcing/eventstore_mock_test.go | 6 ++-- 18 files changed, 69 insertions(+), 76 deletions(-) diff --git a/internal/eventstore/spooler/config.go b/internal/eventstore/spooler/config.go index 9d074d75a6..c63029a7d1 100644 --- a/internal/eventstore/spooler/config.go +++ b/internal/eventstore/spooler/config.go @@ -3,8 +3,7 @@ package spooler import ( "github.com/caos/logging" "github.com/caos/zitadel/internal/eventstore" - "github.com/sony/sonyflake" - "strconv" + "github.com/caos/zitadel/internal/id" ) type Config struct { @@ -15,12 +14,12 @@ type Config struct { } func (c *Config) New() *Spooler { - lockID, err := sonyflake.NewSonyflake(sonyflake.Settings{}).NextID() + lockID, err := id.SonyFlakeGenerator.Next() logging.Log("SPOOL-bdO56").OnError(err).Panic("unable to generate lockID") return &Spooler{ handlers: c.ViewHandlers, - lockID: strconv.FormatUint(lockID, 10), + lockID: lockID, eventstore: c.Eventstore, locker: c.Locker, queue: make(chan *spooledHandler), diff --git a/internal/org/repository/eventsourcing/eventstore.go b/internal/org/repository/eventsourcing/eventstore.go index 03b08bd509..5e5b22d1be 100644 --- a/internal/org/repository/eventsourcing/eventstore.go +++ b/internal/org/repository/eventsourcing/eventstore.go @@ -2,17 +2,17 @@ package eventsourcing import ( "context" - "strconv" - "github.com/caos/zitadel/internal/errors" "github.com/caos/zitadel/internal/eventstore" es_models "github.com/caos/zitadel/internal/eventstore/models" es_sdk "github.com/caos/zitadel/internal/eventstore/sdk" + "github.com/caos/zitadel/internal/id" org_model "github.com/caos/zitadel/internal/org/model" ) type OrgEventstore struct { eventstore.Eventstore + idGenerator id.Generator } type OrgConfig struct { @@ -20,18 +20,21 @@ type OrgConfig struct { } func StartOrg(conf OrgConfig) *OrgEventstore { - return &OrgEventstore{Eventstore: conf.Eventstore} + return &OrgEventstore{ + Eventstore: conf.Eventstore, + idGenerator: id.SonyFlakeGenerator, + } } func (es *OrgEventstore) PrepareCreateOrg(ctx context.Context, orgModel *org_model.Org) (*Org, []*es_models.Aggregate, error) { if orgModel == nil || !orgModel.IsValid() { return nil, nil, errors.ThrowInvalidArgument(nil, "EVENT-OeLSk", "org not valid") } - id, err := idGenerator.NextID() + id, err := es.idGenerator.Next() if err != nil { return nil, nil, errors.ThrowInternal(err, "EVENT-OwciI", "id gen failed") } - orgModel.AggregateID = strconv.FormatUint(id, 10) + orgModel.AggregateID = id org := OrgFromModel(orgModel) aggregates, err := orgCreatedAggregates(ctx, es.AggregateCreator(), org) diff --git a/internal/org/repository/eventsourcing/org.go b/internal/org/repository/eventsourcing/org.go index c663aa9983..8c266d052f 100644 --- a/internal/org/repository/eventsourcing/org.go +++ b/internal/org/repository/eventsourcing/org.go @@ -2,15 +2,11 @@ package eventsourcing import ( "context" - "github.com/caos/zitadel/internal/errors" es_models "github.com/caos/zitadel/internal/eventstore/models" org_model "github.com/caos/zitadel/internal/org/model" - "github.com/sony/sonyflake" ) -var idGenerator = sonyflake.NewSonyflake(sonyflake.Settings{}) - func OrgByIDQuery(id string, latestSequence uint64) (*es_models.SearchQuery, error) { if id == "" { return nil, errors.ThrowPreconditionFailed(nil, "EVENT-dke74", "id should be filled") diff --git a/internal/policy/repository/eventsourcing/eventstore.go b/internal/policy/repository/eventsourcing/eventstore.go index dfa68170fb..25922d4626 100644 --- a/internal/policy/repository/eventsourcing/eventstore.go +++ b/internal/policy/repository/eventsourcing/eventstore.go @@ -4,15 +4,14 @@ import ( "github.com/caos/zitadel/internal/cache/config" sd "github.com/caos/zitadel/internal/config/systemdefaults" es_int "github.com/caos/zitadel/internal/eventstore" + "github.com/caos/zitadel/internal/id" "github.com/caos/zitadel/internal/policy" - "github.com/sony/sonyflake" ) -var idGenerator = sonyflake.NewSonyflake(sonyflake.Settings{}) - type PolicyEventstore struct { es_int.Eventstore policyCache *PolicyCache + idGenerator id.Generator passwordAgePolicyDefault policy.PasswordAgePolicyDefault passwordComplexityPolicyDefault policy.PasswordComplexityPolicyDefault passwordLockoutPolicyDefault policy.PasswordLockoutPolicyDefault @@ -31,6 +30,7 @@ func StartPolicy(conf PolicyConfig, systemDefaults sd.SystemDefaults) (*PolicyEv return &PolicyEventstore{ Eventstore: conf.Eventstore, policyCache: policyCache, + idGenerator: id.SonyFlakeGenerator, passwordAgePolicyDefault: systemDefaults.DefaultPolicies.Age, passwordComplexityPolicyDefault: systemDefaults.DefaultPolicies.Complexity, passwordLockoutPolicyDefault: systemDefaults.DefaultPolicies.Lockout, diff --git a/internal/policy/repository/eventsourcing/eventstore_age.go b/internal/policy/repository/eventsourcing/eventstore_age.go index db9f2d40d7..1603deace6 100644 --- a/internal/policy/repository/eventsourcing/eventstore_age.go +++ b/internal/policy/repository/eventsourcing/eventstore_age.go @@ -2,8 +2,6 @@ package eventsourcing import ( "context" - "strconv" - "github.com/caos/zitadel/internal/api/auth" caos_errs "github.com/caos/zitadel/internal/errors" es_sdk "github.com/caos/zitadel/internal/eventstore/sdk" @@ -39,11 +37,11 @@ func (es *PolicyEventstore) CreatePasswordAgePolicy(ctx context.Context, policy return nil, caos_errs.ThrowPreconditionFailed(nil, "EVENT-yDJ5I", "Policy allready exists") } - id, err := idGenerator.NextID() + id, err := es.idGenerator.Next() if err != nil { return nil, err } - policy.AggregateID = strconv.FormatUint(id, 10) + policy.AggregateID = id repoPolicy := PasswordAgePolicyFromModel(policy) diff --git a/internal/policy/repository/eventsourcing/eventstore_age_mock_test.go b/internal/policy/repository/eventsourcing/eventstore_age_mock_test.go index 2edaec8eda..d306b7780b 100644 --- a/internal/policy/repository/eventsourcing/eventstore_age_mock_test.go +++ b/internal/policy/repository/eventsourcing/eventstore_age_mock_test.go @@ -2,6 +2,7 @@ package eventsourcing import ( "encoding/json" + "github.com/caos/zitadel/internal/id" mock_cache "github.com/caos/zitadel/internal/cache/mock" "github.com/caos/zitadel/internal/eventstore/mock" @@ -14,6 +15,7 @@ func GetMockedEventstoreAge(ctrl *gomock.Controller, mockEs *mock.MockEventstore return &PolicyEventstore{ Eventstore: mockEs, policyCache: GetMockCacheAge(ctrl), + idGenerator: GetSonyFlacke(), } } @@ -24,6 +26,10 @@ func GetMockCacheAge(ctrl *gomock.Controller) *PolicyCache { return &PolicyCache{policyCache: mockCache} } +func GetSonyFlacke() id.Generator { + return id.SonyFlakeGenerator +} + func GetMockGetPasswordAgePolicyOK(ctrl *gomock.Controller) *PolicyEventstore { data, _ := json.Marshal(model.PasswordAgePolicy{Description: "Name"}) events := []*es_models.Event{ diff --git a/internal/policy/repository/eventsourcing/eventstore_complexity.go b/internal/policy/repository/eventsourcing/eventstore_complexity.go index 62a8b1cda0..94bf8fe7d1 100644 --- a/internal/policy/repository/eventsourcing/eventstore_complexity.go +++ b/internal/policy/repository/eventsourcing/eventstore_complexity.go @@ -2,8 +2,6 @@ package eventsourcing import ( "context" - "strconv" - "github.com/caos/zitadel/internal/api/auth" caos_errs "github.com/caos/zitadel/internal/errors" es_sdk "github.com/caos/zitadel/internal/eventstore/sdk" @@ -42,11 +40,11 @@ func (es *PolicyEventstore) CreatePasswordComplexityPolicy(ctx context.Context, return nil, caos_errs.ThrowPreconditionFailed(nil, "EVENT-yDJ5I", "Policy allready exists") } - id, err := idGenerator.NextID() + id, err := es.idGenerator.Next() if err != nil { return nil, err } - policy.AggregateID = strconv.FormatUint(id, 10) + policy.AggregateID = id repoPolicy := PasswordComplexityPolicyFromModel(policy) diff --git a/internal/policy/repository/eventsourcing/eventstore_complexity_mock_test.go b/internal/policy/repository/eventsourcing/eventstore_complexity_mock_test.go index 507303e801..c0559c3015 100644 --- a/internal/policy/repository/eventsourcing/eventstore_complexity_mock_test.go +++ b/internal/policy/repository/eventsourcing/eventstore_complexity_mock_test.go @@ -14,6 +14,7 @@ func GetMockedEventstoreComplexity(ctrl *gomock.Controller, mockEs *mock.MockEve return &PolicyEventstore{ Eventstore: mockEs, policyCache: GetMockCacheComplexity(ctrl), + idGenerator: GetSonyFlacke(), } } diff --git a/internal/policy/repository/eventsourcing/eventstore_lockout.go b/internal/policy/repository/eventsourcing/eventstore_lockout.go index 486ce65d0e..ad7d61acbb 100644 --- a/internal/policy/repository/eventsourcing/eventstore_lockout.go +++ b/internal/policy/repository/eventsourcing/eventstore_lockout.go @@ -2,8 +2,6 @@ package eventsourcing import ( "context" - "strconv" - "github.com/caos/zitadel/internal/api/auth" caos_errs "github.com/caos/zitadel/internal/errors" es_sdk "github.com/caos/zitadel/internal/eventstore/sdk" @@ -39,11 +37,11 @@ func (es *PolicyEventstore) CreatePasswordLockoutPolicy(ctx context.Context, pol return nil, caos_errs.ThrowPreconditionFailed(nil, "EVENT-yDJ5I", "Policy allready exists") } - id, err := idGenerator.NextID() + id, err := es.idGenerator.Next() if err != nil { return nil, err } - policy.AggregateID = strconv.FormatUint(id, 10) + policy.AggregateID = id repoPolicy := PasswordLockoutPolicyFromModel(policy) diff --git a/internal/policy/repository/eventsourcing/eventstore_lockout_mock_test.go b/internal/policy/repository/eventsourcing/eventstore_lockout_mock_test.go index 830ca4cfcb..f2eab458d3 100644 --- a/internal/policy/repository/eventsourcing/eventstore_lockout_mock_test.go +++ b/internal/policy/repository/eventsourcing/eventstore_lockout_mock_test.go @@ -14,6 +14,7 @@ func GetMockedEventstoreLockout(ctrl *gomock.Controller, mockEs *mock.MockEvents return &PolicyEventstore{ Eventstore: mockEs, policyCache: GetMockCacheLockout(ctrl), + idGenerator: GetSonyFlacke(), } } diff --git a/internal/project/repository/eventsourcing/eventstore.go b/internal/project/repository/eventsourcing/eventstore.go index 9aef8e1acc..b87e13de53 100644 --- a/internal/project/repository/eventsourcing/eventstore.go +++ b/internal/project/repository/eventsourcing/eventstore.go @@ -2,25 +2,22 @@ package eventsourcing import ( "context" - sd "github.com/caos/zitadel/internal/config/systemdefaults" - "github.com/caos/zitadel/internal/project/repository/eventsourcing/model" - "strconv" - - "github.com/sony/sonyflake" - "github.com/caos/zitadel/internal/cache/config" + sd "github.com/caos/zitadel/internal/config/systemdefaults" "github.com/caos/zitadel/internal/crypto" caos_errs "github.com/caos/zitadel/internal/errors" es_int "github.com/caos/zitadel/internal/eventstore" es_sdk "github.com/caos/zitadel/internal/eventstore/sdk" + "github.com/caos/zitadel/internal/id" proj_model "github.com/caos/zitadel/internal/project/model" + "github.com/caos/zitadel/internal/project/repository/eventsourcing/model" ) type ProjectEventstore struct { es_int.Eventstore projectCache *ProjectCache pwGenerator crypto.Generator - idGenerator *sonyflake.Sonyflake + idGenerator id.Generator } type ProjectConfig struct { @@ -35,12 +32,11 @@ func StartProject(conf ProjectConfig, systemDefaults sd.SystemDefaults) (*Projec } passwordAlg := crypto.NewBCrypt(systemDefaults.SecretGenerators.PasswordSaltCost) pwGenerator := crypto.NewHashGenerator(systemDefaults.SecretGenerators.ClientSecretGenerator, passwordAlg) - idGenerator := sonyflake.NewSonyflake(sonyflake.Settings{}) return &ProjectEventstore{ Eventstore: conf.Eventstore, projectCache: projectCache, pwGenerator: pwGenerator, - idGenerator: idGenerator, + idGenerator: id.SonyFlakeGenerator, }, nil } @@ -63,11 +59,11 @@ func (es *ProjectEventstore) CreateProject(ctx context.Context, project *proj_mo if !project.IsValid() { return nil, caos_errs.ThrowPreconditionFailed(nil, "EVENT-9dk45", "Name is required") } - id, err := es.idGenerator.NextID() + id, err := es.idGenerator.Next() if err != nil { return nil, err } - project.AggregateID = strconv.FormatUint(id, 10) + project.AggregateID = id project.State = proj_model.PROJECTSTATE_ACTIVE repoProject := model.ProjectFromModel(project) @@ -331,16 +327,16 @@ func (es *ProjectEventstore) AddApplication(ctx context.Context, app *proj_model if err != nil { return nil, err } - id, err := es.idGenerator.NextID() + id, err := es.idGenerator.Next() if err != nil { return nil, err } - app.AppID = strconv.FormatUint(id, 10) + app.AppID = id var stringPw string var cryptoPw *crypto.CryptoValue if app.OIDCConfig != nil { - app.OIDCConfig.AppID = strconv.FormatUint(id, 10) + app.OIDCConfig.AppID = id stringPw, cryptoPw, err = generateNewClientSecret(es.pwGenerator) if err != nil { return nil, err @@ -567,11 +563,11 @@ func (es *ProjectEventstore) AddProjectGrant(ctx context.Context, grant *proj_mo if !existing.ContainsRoles(grant.RoleKeys) { return nil, caos_errs.ThrowPreconditionFailed(nil, "EVENT-di83d", "One role doesnt exist in Project") } - id, err := es.idGenerator.NextID() + id, err := es.idGenerator.Next() if err != nil { return nil, err } - grant.GrantID = strconv.FormatUint(id, 10) + grant.GrantID = id repoProject := model.ProjectFromModel(existing) repoGrant := model.GrantFromModel(grant) diff --git a/internal/project/repository/eventsourcing/eventstore_mock_test.go b/internal/project/repository/eventsourcing/eventstore_mock_test.go index 8a5d22150a..943e9d1d7f 100644 --- a/internal/project/repository/eventsourcing/eventstore_mock_test.go +++ b/internal/project/repository/eventsourcing/eventstore_mock_test.go @@ -6,10 +6,10 @@ import ( "github.com/caos/zitadel/internal/crypto" "github.com/caos/zitadel/internal/eventstore/mock" es_models "github.com/caos/zitadel/internal/eventstore/models" + "github.com/caos/zitadel/internal/id" proj_model "github.com/caos/zitadel/internal/project/model" "github.com/caos/zitadel/internal/project/repository/eventsourcing/model" "github.com/golang/mock/gomock" - "github.com/sony/sonyflake" ) func GetMockedEventstore(ctrl *gomock.Controller, mockEs *mock.MockEventstore) *ProjectEventstore { @@ -35,8 +35,8 @@ func GetMockCache(ctrl *gomock.Controller) *ProjectCache { return &ProjectCache{projectCache: mockCache} } -func GetSonyFlacke() *sonyflake.Sonyflake { - return sonyflake.NewSonyflake(sonyflake.Settings{}) +func GetSonyFlacke() id.Generator { + return id.SonyFlakeGenerator } func GetMockPwGenerator(ctrl *gomock.Controller) crypto.Generator { diff --git a/internal/project/repository/eventsourcing/oidc.go b/internal/project/repository/eventsourcing/oidc.go index 22022b736f..6b4840ec5f 100644 --- a/internal/project/repository/eventsourcing/oidc.go +++ b/internal/project/repository/eventsourcing/oidc.go @@ -5,14 +5,14 @@ import ( "github.com/caos/logging" "github.com/caos/zitadel/internal/crypto" "github.com/caos/zitadel/internal/errors" + "github.com/caos/zitadel/internal/id" "github.com/caos/zitadel/internal/project/model" - "github.com/sony/sonyflake" "strings" ) //ClientID random_number@projectname (eg. 495894098234@zitadel) -func generateNewClientID(idGenerator *sonyflake.Sonyflake, project *model.Project) (string, error) { - rndID, err := idGenerator.NextID() +func generateNewClientID(idGenerator id.Generator, project *model.Project) (string, error) { + rndID, err := idGenerator.Next() if err != nil { return "", err } diff --git a/internal/user/repository/eventsourcing/eventstore.go b/internal/user/repository/eventsourcing/eventstore.go index 2201e743fb..478109a5d7 100644 --- a/internal/user/repository/eventsourcing/eventstore.go +++ b/internal/user/repository/eventsourcing/eventstore.go @@ -2,10 +2,8 @@ package eventsourcing import ( "context" - "strconv" - + "github.com/caos/zitadel/internal/id" "github.com/pquerna/otp/totp" - "github.com/sony/sonyflake" req_model "github.com/caos/zitadel/internal/auth_request/model" "github.com/caos/zitadel/internal/cache/config" @@ -23,7 +21,7 @@ import ( type UserEventstore struct { es_int.Eventstore userCache *UserCache - idGenerator *sonyflake.Sonyflake + idGenerator id.Generator PasswordAlg crypto.HashAlgorithm InitializeUserCode crypto.Generator EmailVerificationCode crypto.Generator @@ -44,7 +42,6 @@ func StartUser(conf UserConfig, systemDefaults sd.SystemDefaults) (*UserEventsto if err != nil { return nil, err } - idGenerator := sonyflake.NewSonyflake(sonyflake.Settings{}) aesCrypto, err := crypto.NewAESCrypto(systemDefaults.UserVerificationKey) if err != nil { return nil, err @@ -59,7 +56,7 @@ func StartUser(conf UserConfig, systemDefaults sd.SystemDefaults) (*UserEventsto return &UserEventstore{ Eventstore: conf.Eventstore, userCache: userCache, - idGenerator: idGenerator, + idGenerator: id.SonyFlakeGenerator, InitializeUserCode: initCodeGen, EmailVerificationCode: emailVerificationCode, PhoneVerificationCode: phoneVerificationCode, @@ -95,11 +92,13 @@ func (es *UserEventstore) PrepareCreateUser(ctx context.Context, user *usr_model if !user.IsValid() { return nil, nil, caos_errs.ThrowPreconditionFailed(nil, "EVENT-9dk45", "User is invalid") } - id, err := es.idGenerator.NextID() + + //TODO: Check Uniqueness + id, err := es.idGenerator.Next() if err != nil { return nil, nil, err } - user.AggregateID = strconv.FormatUint(id, 10) + user.AggregateID = id err = user.HashPasswordIfExisting(es.PasswordAlg, true) if err != nil { @@ -143,11 +142,12 @@ func (es *UserEventstore) PrepareRegisterUser(ctx context.Context, user *usr_mod if !user.IsValid() || user.Password == nil || user.SecretString == "" { return nil, nil, caos_errs.ThrowPreconditionFailed(nil, "EVENT-9dk45", "user is invalid") } - id, err := es.idGenerator.NextID() + //TODO: Check Uniqueness + id, err := es.idGenerator.Next() if err != nil { return nil, nil, err } - user.AggregateID = strconv.FormatUint(id, 10) + user.AggregateID = id err = user.HashPasswordIfExisting(es.PasswordAlg, false) if err != nil { diff --git a/internal/user/repository/eventsourcing/eventstore_mock_test.go b/internal/user/repository/eventsourcing/eventstore_mock_test.go index c40960374e..53ac275597 100644 --- a/internal/user/repository/eventsourcing/eventstore_mock_test.go +++ b/internal/user/repository/eventsourcing/eventstore_mock_test.go @@ -2,17 +2,16 @@ package eventsourcing import ( "encoding/json" + "github.com/caos/zitadel/internal/id" "time" - "github.com/golang/mock/gomock" - "github.com/sony/sonyflake" - mock_cache "github.com/caos/zitadel/internal/cache/mock" "github.com/caos/zitadel/internal/crypto" "github.com/caos/zitadel/internal/eventstore/mock" es_models "github.com/caos/zitadel/internal/eventstore/models" global_model "github.com/caos/zitadel/internal/model" "github.com/caos/zitadel/internal/user/repository/eventsourcing/model" + "github.com/golang/mock/gomock" ) func GetMockedEventstore(ctrl *gomock.Controller, mockEs *mock.MockEventstore) *UserEventstore { @@ -52,8 +51,8 @@ func GetMockCache(ctrl *gomock.Controller) *UserCache { return &UserCache{userCache: mockCache} } -func GetSonyFlacke() *sonyflake.Sonyflake { - return sonyflake.NewSonyflake(sonyflake.Settings{}) +func GetSonyFlacke() id.Generator { + return id.SonyFlakeGenerator } func GetMockPwGenerator(ctrl *gomock.Controller) crypto.Generator { diff --git a/internal/user/repository/eventsourcing/model/types.go b/internal/user/repository/eventsourcing/model/types.go index 9838011625..f2cbe799b0 100644 --- a/internal/user/repository/eventsourcing/model/types.go +++ b/internal/user/repository/eventsourcing/model/types.go @@ -11,7 +11,6 @@ const ( UserRegistered models.EventType = "user.selfregistered" InitializedUserCodeAdded models.EventType = "user.initialization.code.added" InitializedUserCodeSent models.EventType = "user.initialization.code.sent" - UserRemoved models.EventType = "user.removed" UserUserNameReserved models.EventType = "user.username.reserved" UserUserNameReleased models.EventType = "user.username.released" diff --git a/internal/usergrant/repository/eventsourcing/eventstore.go b/internal/usergrant/repository/eventsourcing/eventstore.go index 376d00c7b6..171511d62e 100644 --- a/internal/usergrant/repository/eventsourcing/eventstore.go +++ b/internal/usergrant/repository/eventsourcing/eventstore.go @@ -7,16 +7,15 @@ import ( es_int "github.com/caos/zitadel/internal/eventstore" "github.com/caos/zitadel/internal/eventstore/models" es_sdk "github.com/caos/zitadel/internal/eventstore/sdk" + "github.com/caos/zitadel/internal/id" grant_model "github.com/caos/zitadel/internal/usergrant/model" "github.com/caos/zitadel/internal/usergrant/repository/eventsourcing/model" - "github.com/sony/sonyflake" - "strconv" ) type UserGrantEventStore struct { es_int.Eventstore userGrantCache *UserGrantCache - idGenerator *sonyflake.Sonyflake + idGenerator id.Generator } type UserGrantConfig struct { @@ -29,11 +28,10 @@ func StartUserGrant(conf UserGrantConfig) (*UserGrantEventStore, error) { if err != nil { return nil, err } - idGenerator := sonyflake.NewSonyflake(sonyflake.Settings{}) return &UserGrantEventStore{ Eventstore: conf.Eventstore, userGrantCache: userGrantCache, - idGenerator: idGenerator, + idGenerator: id.SonyFlakeGenerator, }, nil } @@ -59,11 +57,12 @@ func (es *UserGrantEventStore) AddUserGrant(ctx context.Context, grant *grant_mo if grant == nil || !grant.IsValid() { return nil, caos_errs.ThrowPreconditionFailed(nil, "EVENT-sdiw3", "User grant invalid") } - id, err := es.idGenerator.NextID() + //TODO: Check Uniqueness + id, err := es.idGenerator.Next() if err != nil { return nil, err } - grant.AggregateID = strconv.FormatUint(id, 10) + grant.AggregateID = id repoGrant := model.UserGrantFromModel(grant) diff --git a/internal/usergrant/repository/eventsourcing/eventstore_mock_test.go b/internal/usergrant/repository/eventsourcing/eventstore_mock_test.go index 35070fa2c5..5f421896e1 100644 --- a/internal/usergrant/repository/eventsourcing/eventstore_mock_test.go +++ b/internal/usergrant/repository/eventsourcing/eventstore_mock_test.go @@ -5,9 +5,9 @@ import ( mock_cache "github.com/caos/zitadel/internal/cache/mock" "github.com/caos/zitadel/internal/eventstore/mock" es_models "github.com/caos/zitadel/internal/eventstore/models" + "github.com/caos/zitadel/internal/id" "github.com/caos/zitadel/internal/usergrant/repository/eventsourcing/model" "github.com/golang/mock/gomock" - "github.com/sony/sonyflake" ) func GetMockedEventstore(ctrl *gomock.Controller, mockEs *mock.MockEventstore) *UserGrantEventStore { @@ -25,8 +25,8 @@ func GetMockCache(ctrl *gomock.Controller) *UserGrantCache { return &UserGrantCache{userGrantCache: mockCache} } -func GetSonyFlacke() *sonyflake.Sonyflake { - return sonyflake.NewSonyflake(sonyflake.Settings{}) +func GetSonyFlacke() id.Generator { + return id.SonyFlakeGenerator } func GetMockUserGrantByIDOK(ctrl *gomock.Controller) *UserGrantEventStore {