From 762941f0ea94748f00b334df436fe8b21c17757e Mon Sep 17 00:00:00 2001 From: Fabiennne Date: Tue, 15 Dec 2020 12:00:53 +0100 Subject: [PATCH] fix: idp provider --- internal/v2/business/iam/converter.go | 6 +- internal/v2/business/iam/policy_login.go | 10 +-- internal/v2/repository/iam/aggregate.go | 6 +- .../iam/policy/login/idpprovider/events.go | 3 +- .../policy/login/idpprovider/write_model.go | 40 ++++-------- .../v2/repository/idp/config_read_model.go | 4 +- .../v2/repository/idp/provider/event_added.go | 50 -------------- .../repository/idp/provider/event_removed.go | 47 -------------- .../v2/repository/idp/provider/write_model.go | 21 ------ internal/v2/repository/policy/login/events.go | 6 +- .../policy/login/idpprovider/events.go | 65 +++++++++++++------ .../login/idpprovider/provider.go} | 2 +- .../policy/login/idpprovider/write_model.go | 14 ++-- 13 files changed, 82 insertions(+), 192 deletions(-) delete mode 100644 internal/v2/repository/idp/provider/event_added.go delete mode 100644 internal/v2/repository/idp/provider/event_removed.go delete mode 100644 internal/v2/repository/idp/provider/write_model.go rename internal/v2/repository/{idp/provider/type.go => policy/login/idpprovider/provider.go} (87%) diff --git a/internal/v2/business/iam/converter.go b/internal/v2/business/iam/converter.go index 35f3c1a012..f3fd5f0547 100644 --- a/internal/v2/business/iam/converter.go +++ b/internal/v2/business/iam/converter.go @@ -273,8 +273,8 @@ func writeModelToIDPOIDCConfig(wm *oidc.ConfigWriteModel) *model.OIDCIDPConfig { func writeModelToIDPProvider(wm *idpprovider.WriteModel) *model.IDPProvider { return &model.IDPProvider{ - ObjectRoot: writeModelToObjectRoot(wm.WriteModel), - IDPConfigID: wm.Provider.IDPConfigID, - Type: model.IDPProviderType(wm.Provider.IDPProviderType), + ObjectRoot: writeModelToObjectRoot(wm.WriteModel.WriteModel), + IDPConfigID: wm.IDPConfigID, + Type: model.IDPProviderType(wm.IDPProviderType), } } diff --git a/internal/v2/business/iam/policy_login.go b/internal/v2/business/iam/policy_login.go index 48ffdd066c..ef4ce35f07 100644 --- a/internal/v2/business/iam/policy_login.go +++ b/internal/v2/business/iam/policy_login.go @@ -9,9 +9,9 @@ import ( iam_login "github.com/caos/zitadel/internal/v2/repository/iam/policy/login" iam_factor "github.com/caos/zitadel/internal/v2/repository/iam/policy/login/factors" "github.com/caos/zitadel/internal/v2/repository/iam/policy/login/idpprovider" - "github.com/caos/zitadel/internal/v2/repository/idp/provider" "github.com/caos/zitadel/internal/v2/repository/policy/login" "github.com/caos/zitadel/internal/v2/repository/policy/login/factors" + idpprovider2 "github.com/caos/zitadel/internal/v2/repository/policy/login/idpprovider" ) func (r *Repository) AddLoginPolicy(ctx context.Context, policy *iam_model.LoginPolicy) (*iam_model.LoginPolicy, error) { @@ -67,8 +67,8 @@ func (r *Repository) AddIDPProviderToLoginPolicy(ctx context.Context, idpProvide return nil, err } - aggregate := iam_repo.AggregateFromWriteModel(&writeModel.WriteModel). - PushLoginPolicyIDPProviderAddedEvent(ctx, idpProvider.IDPConfigID, provider.Type(idpProvider.Type)) + aggregate := iam_repo.AggregateFromWriteModel(&writeModel.WriteModel.WriteModel). + PushLoginPolicyIDPProviderAddedEvent(ctx, idpProvider.IDPConfigID, idpprovider2.Type(idpProvider.Type)) if err = r.eventstore.PushAggregate(ctx, writeModel, aggregate); err != nil { return nil, err @@ -83,8 +83,8 @@ func (r *Repository) RemoveIDPProviderFromLoginPolicy(ctx context.Context, idpPr if err != nil { return err } - aggregate := iam_repo.AggregateFromWriteModel(&writeModel.WriteModel). - PushLoginPolicyIDPProviderAddedEvent(ctx, idpProvider.IDPConfigID, provider.Type(idpProvider.Type)) + aggregate := iam_repo.AggregateFromWriteModel(&writeModel.WriteModel.WriteModel). + PushLoginPolicyIDPProviderAddedEvent(ctx, idpProvider.IDPConfigID, idpprovider2.Type(idpProvider.Type)) return r.eventstore.PushAggregate(ctx, writeModel, aggregate) } diff --git a/internal/v2/repository/iam/aggregate.go b/internal/v2/repository/iam/aggregate.go index fb25cd2f1a..aaf1c2cc67 100644 --- a/internal/v2/repository/iam/aggregate.go +++ b/internal/v2/repository/iam/aggregate.go @@ -15,9 +15,9 @@ import ( "github.com/caos/zitadel/internal/v2/repository/iam/policy/password_lockout" "github.com/caos/zitadel/internal/v2/repository/idp" "github.com/caos/zitadel/internal/v2/repository/idp/oidc" - "github.com/caos/zitadel/internal/v2/repository/idp/provider" "github.com/caos/zitadel/internal/v2/repository/policy/login" "github.com/caos/zitadel/internal/v2/repository/policy/login/factors" + idpprovider2 "github.com/caos/zitadel/internal/v2/repository/policy/login/idpprovider" ) const ( @@ -283,7 +283,7 @@ func (a *Aggregate) PushIDPOIDCConfigChanged( func (a *Aggregate) PushLoginPolicyIDPProviderAddedEvent( ctx context.Context, idpConfigID string, - providerType provider.Type, + providerType idpprovider2.Type, ) *Aggregate { a.Aggregate = *a.PushEvents(idpprovider.NewAddedEvent(ctx, idpConfigID, providerType)) @@ -293,7 +293,7 @@ func (a *Aggregate) PushLoginPolicyIDPProviderAddedEvent( func (a *Aggregate) PushLoginPolicyIDPProviderRemovedEvent( ctx context.Context, idpConfigID string, - providerType provider.Type, + providerType idpprovider2.Type, ) *Aggregate { a.Aggregate = *a.PushEvents(idpprovider.NewRemovedEvent(ctx, idpConfigID)) diff --git a/internal/v2/repository/iam/policy/login/idpprovider/events.go b/internal/v2/repository/iam/policy/login/idpprovider/events.go index c293bddc13..c86df181a1 100644 --- a/internal/v2/repository/iam/policy/login/idpprovider/events.go +++ b/internal/v2/repository/iam/policy/login/idpprovider/events.go @@ -7,7 +7,6 @@ import ( "github.com/caos/zitadel/internal/eventstore/v2" "github.com/caos/zitadel/internal/eventstore/v2/repository" - "github.com/caos/zitadel/internal/v2/repository/idp/provider" ) type AddedEvent struct { @@ -17,7 +16,7 @@ type AddedEvent struct { func NewAddedEvent( ctx context.Context, idpConfigID string, - idpProviderType provider.Type, + idpProviderType idpprovider.Type, ) *AddedEvent { return &AddedEvent{ diff --git a/internal/v2/repository/iam/policy/login/idpprovider/write_model.go b/internal/v2/repository/iam/policy/login/idpprovider/write_model.go index e6bfb5129d..b75911c0f0 100644 --- a/internal/v2/repository/iam/policy/login/idpprovider/write_model.go +++ b/internal/v2/repository/iam/policy/login/idpprovider/write_model.go @@ -10,57 +10,39 @@ const ( ) type WriteModel struct { - eventstore.WriteModel - Provider idpprovider.WriteModel - - idpConfigID string - iamID string - + idpprovider.WriteModel IsRemoved bool } func NewWriteModel(iamID, idpConfigID string) *WriteModel { return &WriteModel{ - iamID: iamID, - idpConfigID: idpConfigID, + WriteModel: idpprovider.WriteModel{ + WriteModel: eventstore.WriteModel{ + AggregateID: iamID, + }, + IDPConfigID: idpConfigID, + }, + IsRemoved: false, } } func (wm *WriteModel) AppendEvents(events ...eventstore.EventReader) { - wm.WriteModel.AppendEvents(events...) for _, event := range events { switch e := event.(type) { case *AddedEvent: - if e.IDPConfigID != wm.idpConfigID { + if e.IDPConfigID != wm.IDPConfigID { continue } - wm.Provider.AppendEvents(&e.AddedEvent) + wm.WriteModel.AppendEvents(&e.AddedEvent) } } } func (wm *WriteModel) Reduce() error { - for _, event := range wm.Events { - switch e := event.(type) { - case *AddedEvent: - if e.IDPConfigID != wm.idpConfigID { - continue - } - wm.IsRemoved = false - case *RemovedEvent: - if e.IDPConfigID != wm.idpConfigID { - continue - } - wm.IsRemoved = true - } - } - if err := wm.Provider.Reduce(); err != nil { - return err - } return wm.WriteModel.Reduce() } func (wm *WriteModel) Query() *eventstore.SearchQueryBuilder { return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent, AggregateType). - AggregateIDs(wm.iamID) + AggregateIDs(wm.AggregateID) } diff --git a/internal/v2/repository/idp/config_read_model.go b/internal/v2/repository/idp/config_read_model.go index 06bab1be73..f1ddd19a96 100644 --- a/internal/v2/repository/idp/config_read_model.go +++ b/internal/v2/repository/idp/config_read_model.go @@ -3,7 +3,7 @@ package idp import ( "github.com/caos/zitadel/internal/eventstore/v2" "github.com/caos/zitadel/internal/v2/repository/idp/oidc" - "github.com/caos/zitadel/internal/v2/repository/idp/provider" + "github.com/caos/zitadel/internal/v2/repository/policy/login/idpprovider" ) type ConfigReadModel struct { @@ -13,7 +13,7 @@ type ConfigReadModel struct { ConfigID string Name string StylingType StylingType - ProviderType provider.Type + ProviderType idpprovider.Type OIDCConfig *oidc.ConfigReadModel } diff --git a/internal/v2/repository/idp/provider/event_added.go b/internal/v2/repository/idp/provider/event_added.go deleted file mode 100644 index dd3e132e43..0000000000 --- a/internal/v2/repository/idp/provider/event_added.go +++ /dev/null @@ -1,50 +0,0 @@ -package provider - -import ( - "encoding/json" - - "github.com/caos/zitadel/internal/errors" - "github.com/caos/zitadel/internal/eventstore/v2" - "github.com/caos/zitadel/internal/eventstore/v2/repository" -) - -const ( - AddedEventType = "idpprovider.added" -) - -type AddedEvent struct { - eventstore.BaseEvent - - IDPConfigID string `json:"idpConfigId"` - IDPProviderType Type `json:"idpProviderType"` -} - -func (e *AddedEvent) Data() interface{} { - return e -} - -func NewAddedEvent( - base *eventstore.BaseEvent, - idpConfigID string, - idpProviderType Type, -) *AddedEvent { - - return &AddedEvent{ - BaseEvent: *base, - IDPConfigID: idpConfigID, - IDPProviderType: idpProviderType, - } -} - -func AddedEventEventMapper(event *repository.Event) (eventstore.EventReader, error) { - e := &AddedEvent{ - BaseEvent: *eventstore.BaseEventFromRepo(event), - } - - err := json.Unmarshal(event.Data, e) - if err != nil { - return nil, errors.ThrowInternal(err, "PROVI-bfNnp", "Errors.Internal") - } - - return e, nil -} diff --git a/internal/v2/repository/idp/provider/event_removed.go b/internal/v2/repository/idp/provider/event_removed.go deleted file mode 100644 index 5fcbc2693c..0000000000 --- a/internal/v2/repository/idp/provider/event_removed.go +++ /dev/null @@ -1,47 +0,0 @@ -package provider - -import ( - "encoding/json" - - "github.com/caos/zitadel/internal/errors" - "github.com/caos/zitadel/internal/eventstore/v2" - "github.com/caos/zitadel/internal/eventstore/v2/repository" -) - -const ( - RemovedEventType = "idpprovider.removed" -) - -type RemovedEvent struct { - eventstore.BaseEvent - - IDPConfigID string `json:"idpConfigId"` -} - -func (e *RemovedEvent) Data() interface{} { - return e -} - -func NewRemovedEvent( - base *eventstore.BaseEvent, - idpConfigID string, -) *RemovedEvent { - - return &RemovedEvent{ - BaseEvent: *base, - IDPConfigID: idpConfigID, - } -} - -func RemovedEventEventMapper(event *repository.Event) (eventstore.EventReader, error) { - e := &RemovedEvent{ - BaseEvent: *eventstore.BaseEventFromRepo(event), - } - - err := json.Unmarshal(event.Data, e) - if err != nil { - return nil, errors.ThrowInternal(err, "PROVI-6H0KQ", "Errors.Internal") - } - - return e, nil -} diff --git a/internal/v2/repository/idp/provider/write_model.go b/internal/v2/repository/idp/provider/write_model.go deleted file mode 100644 index 3268d65409..0000000000 --- a/internal/v2/repository/idp/provider/write_model.go +++ /dev/null @@ -1,21 +0,0 @@ -package provider - -import "github.com/caos/zitadel/internal/eventstore/v2" - -type WriteModel struct { - eventstore.WriteModel - - IDPConfigID string - IDPProviderType Type -} - -func (wm *WriteModel) Reduce() error { - for _, event := range wm.Events { - switch e := event.(type) { - case *AddedEvent: - wm.IDPConfigID = e.IDPConfigID - wm.IDPProviderType = e.IDPProviderType - } - } - return wm.WriteModel.Reduce() -} diff --git a/internal/v2/repository/policy/login/events.go b/internal/v2/repository/policy/login/events.go index 88d97d8d77..f792d97226 100644 --- a/internal/v2/repository/policy/login/events.go +++ b/internal/v2/repository/policy/login/events.go @@ -5,7 +5,7 @@ import ( "github.com/caos/zitadel/internal/errors" "github.com/caos/zitadel/internal/eventstore/v2" "github.com/caos/zitadel/internal/eventstore/v2/repository" - "github.com/caos/zitadel/internal/v2/repository/idp/provider" + "github.com/caos/zitadel/internal/v2/repository/policy/login/idpprovider" ) const ( @@ -13,8 +13,8 @@ const ( LoginPolicyAddedEventType = loginPolicyPrefix + "added" LoginPolicyChangedEventType = loginPolicyPrefix + "changed" LoginPolicyRemovedEventType = loginPolicyPrefix + "removed" - LoginPolicyIDPProviderAddedEventType = loginPolicyPrefix + provider.AddedEventType - LoginPolicyIDPProviderRemovedEventType = loginPolicyPrefix + provider.RemovedEventType + LoginPolicyIDPProviderAddedEventType = loginPolicyPrefix + idpprovider.AddedEventType + LoginPolicyIDPProviderRemovedEventType = loginPolicyPrefix + idpprovider.RemovedEventType ) type AddedEvent struct { diff --git a/internal/v2/repository/policy/login/idpprovider/events.go b/internal/v2/repository/policy/login/idpprovider/events.go index c8fa109211..0e74e03563 100644 --- a/internal/v2/repository/policy/login/idpprovider/events.go +++ b/internal/v2/repository/policy/login/idpprovider/events.go @@ -1,42 +1,62 @@ package idpprovider import ( + "encoding/json" + "github.com/caos/zitadel/internal/errors" "github.com/caos/zitadel/internal/eventstore/v2" "github.com/caos/zitadel/internal/eventstore/v2/repository" - "github.com/caos/zitadel/internal/v2/repository/idp/provider" +) + +const ( + AddedEventType = "idpprovider.added" + RemovedEventType = "idpprovider.removed" ) type AddedEvent struct { - provider.AddedEvent + eventstore.BaseEvent + + IDPConfigID string `json:"idpConfigId"` + IDPProviderType Type `json:"idpProviderType"` +} + +func (e *AddedEvent) Data() interface{} { + return e } func NewAddedEvent( base *eventstore.BaseEvent, idpConfigID string, - idpProviderType provider.Type, + idpProviderType Type, ) *AddedEvent { return &AddedEvent{ - AddedEvent: *provider.NewAddedEvent( - base, - idpConfigID, - idpProviderType), + *base, + idpConfigID, + idpProviderType, } } func AddedEventMapper(event *repository.Event) (eventstore.EventReader, error) { - e, err := provider.AddedEventEventMapper(event) - if err != nil { - return nil, err + e := &AddedEvent{ + BaseEvent: *eventstore.BaseEventFromRepo(event), } - return &AddedEvent{ - AddedEvent: *e.(*provider.AddedEvent), - }, nil + err := json.Unmarshal(event.Data, e) + if err != nil { + return nil, errors.ThrowInternal(err, "PROVI-bfNnp", "Errors.Internal") + } + + return e, nil } type RemovedEvent struct { - provider.RemovedEvent + eventstore.BaseEvent + + IDPConfigID string `json:"idpConfigId"` +} + +func (e *RemovedEvent) Data() interface{} { + return e } func NewRemovedEvent( @@ -44,17 +64,20 @@ func NewRemovedEvent( idpConfigID string, ) *RemovedEvent { return &RemovedEvent{ - RemovedEvent: *provider.NewRemovedEvent(base, idpConfigID), + BaseEvent: *base, + IDPConfigID: idpConfigID, } } func RemovedEventMapper(event *repository.Event) (eventstore.EventReader, error) { - e, err := provider.RemovedEventEventMapper(event) - if err != nil { - return nil, err + e := &RemovedEvent{ + BaseEvent: *eventstore.BaseEventFromRepo(event), } - return &RemovedEvent{ - RemovedEvent: *e.(*provider.RemovedEvent), - }, nil + err := json.Unmarshal(event.Data, e) + if err != nil { + return nil, errors.ThrowInternal(err, "PROVI-6H0KQ", "Errors.Internal") + } + + return e, nil } diff --git a/internal/v2/repository/idp/provider/type.go b/internal/v2/repository/policy/login/idpprovider/provider.go similarity index 87% rename from internal/v2/repository/idp/provider/type.go rename to internal/v2/repository/policy/login/idpprovider/provider.go index 7240067d15..0eb1239255 100644 --- a/internal/v2/repository/idp/provider/type.go +++ b/internal/v2/repository/policy/login/idpprovider/provider.go @@ -1,4 +1,4 @@ -package provider +package idpprovider type Type int8 diff --git a/internal/v2/repository/policy/login/idpprovider/write_model.go b/internal/v2/repository/policy/login/idpprovider/write_model.go index f7526afc56..9db97ae52a 100644 --- a/internal/v2/repository/policy/login/idpprovider/write_model.go +++ b/internal/v2/repository/policy/login/idpprovider/write_model.go @@ -2,18 +2,22 @@ package idpprovider import ( "github.com/caos/zitadel/internal/eventstore/v2" - "github.com/caos/zitadel/internal/v2/repository/idp/provider" ) type WriteModel struct { - provider.WriteModel + eventstore.WriteModel + + IDPConfigID string + IDPProviderType Type } -func (wm *WriteModel) AppendEvents(events ...eventstore.EventReader) { - for _, event := range events { +func (wm *WriteModel) Reduce() error { + for _, event := range wm.Events { switch e := event.(type) { case *AddedEvent: - wm.WriteModel.AppendEvents(&e.AddedEvent) + wm.IDPConfigID = e.IDPConfigID + wm.IDPProviderType = e.IDPProviderType } } + return wm.WriteModel.Reduce() }