use global id generator (#142)

* use global id generator

* remove duplicate `UserRemoved`

Co-authored-by: Livio Amstutz <livio.a@gmail.com>
This commit is contained in:
Fabi 2020-05-28 13:28:36 +02:00 committed by GitHub
parent 2758bf30b1
commit e3f9e9c05e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 69 additions and 76 deletions

View File

@ -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),

View File

@ -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)

View File

@ -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")

View File

@ -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,

View File

@ -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)

View File

@ -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{

View File

@ -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)

View File

@ -14,6 +14,7 @@ func GetMockedEventstoreComplexity(ctrl *gomock.Controller, mockEs *mock.MockEve
return &PolicyEventstore{
Eventstore: mockEs,
policyCache: GetMockCacheComplexity(ctrl),
idGenerator: GetSonyFlacke(),
}
}

View File

@ -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)

View File

@ -14,6 +14,7 @@ func GetMockedEventstoreLockout(ctrl *gomock.Controller, mockEs *mock.MockEvents
return &PolicyEventstore{
Eventstore: mockEs,
policyCache: GetMockCacheLockout(ctrl),
idGenerator: GetSonyFlacke(),
}
}

View File

@ -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)

View File

@ -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 {

View File

@ -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
}

View File

@ -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 {

View File

@ -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 {

View File

@ -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"

View File

@ -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)

View File

@ -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 {