From edff816ec1a8aa7a94876826c1a48bed060429be Mon Sep 17 00:00:00 2001 From: adlerhurst Date: Tue, 17 Nov 2020 13:44:37 +0100 Subject: [PATCH] start idp config --- .../server/middleware/service_interceptor.go | 17 ++++++ internal/api/grpc/server/server.go | 2 +- internal/v2/business/iam/member.go | 3 +- internal/v2/repository/iam/aggregate.go | 17 ++++++ internal/v2/repository/idp/config.go | 59 +++++++++++++++++++ internal/v2/repository/idp/event_added.go | 31 ++++++++++ internal/v2/repository/idp/event_changed.go | 37 ++++++++++++ .../v2/repository/idp/event_deactivated.go | 28 +++++++++ .../v2/repository/idp/oidc/added_event.go | 3 + .../v2/repository/idp/oidc/changed_event.go | 3 + internal/v2/repository/idp/oidc/config.go | 35 +++++++++++ .../repository/idp/oidc/deactivated_event.go | 3 + .../v2/repository/idp/oidc/removed_event.go | 3 + 13 files changed, 239 insertions(+), 2 deletions(-) create mode 100644 internal/api/grpc/server/middleware/service_interceptor.go create mode 100644 internal/v2/repository/idp/event_added.go create mode 100644 internal/v2/repository/idp/event_changed.go create mode 100644 internal/v2/repository/idp/event_deactivated.go create mode 100644 internal/v2/repository/idp/oidc/added_event.go create mode 100644 internal/v2/repository/idp/oidc/changed_event.go create mode 100644 internal/v2/repository/idp/oidc/config.go create mode 100644 internal/v2/repository/idp/oidc/deactivated_event.go create mode 100644 internal/v2/repository/idp/oidc/removed_event.go diff --git a/internal/api/grpc/server/middleware/service_interceptor.go b/internal/api/grpc/server/middleware/service_interceptor.go new file mode 100644 index 0000000000..36b4e3470a --- /dev/null +++ b/internal/api/grpc/server/middleware/service_interceptor.go @@ -0,0 +1,17 @@ +package middleware + +import ( + "context" + + "github.com/caos/zitadel/internal/api/service" + _ "github.com/caos/zitadel/internal/statik" + "google.golang.org/grpc" +) + +func ServiceHandler() grpc.UnaryServerInterceptor { + return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { + namer := info.Server.(interface{ AppName() string }) + ctx = service.WithService(ctx, namer.AppName()) + return handler(ctx, req) + } +} diff --git a/internal/api/grpc/server/server.go b/internal/api/grpc/server/server.go index acb6165c2d..e3198cec78 100644 --- a/internal/api/grpc/server/server.go +++ b/internal/api/grpc/server/server.go @@ -34,10 +34,10 @@ func CreateServer(verifier *authz.TokenVerifier, authConfig authz.Config, lang l middleware.AuthorizationInterceptor(verifier, authConfig), middleware.TranslationHandler(lang), middleware.ValidationHandler(), + middleware.ServiceHandler(), ), ), ) - } func Serve(ctx context.Context, server *grpc.Server, port string) { diff --git a/internal/v2/business/iam/member.go b/internal/v2/business/iam/member.go index 3efb0a2c40..b80aabc388 100644 --- a/internal/v2/business/iam/member.go +++ b/internal/v2/business/iam/member.go @@ -25,7 +25,8 @@ func (r *Repository) AddIAMMember(ctx context.Context, member *iam_model.IAMMemb } iamAgg := iam_repo.AggregateFromReadModel(iam). - PushEvents(iam_repo.NewMemberAddedEvent(ctx, member.UserID, member.Roles...)) + PushMemberAdded(ctx, member.UserID, member.Roles...) + // PushEvents(iam_repo.NewMemberAddedEvent(ctx, member.UserID, member.Roles...)) events, err := r.eventstore.PushAggregates(ctx, iamAgg) if err != nil { diff --git a/internal/v2/repository/iam/aggregate.go b/internal/v2/repository/iam/aggregate.go index 316a6da7c3..7f8a09f607 100644 --- a/internal/v2/repository/iam/aggregate.go +++ b/internal/v2/repository/iam/aggregate.go @@ -1,6 +1,8 @@ package iam import ( + "context" + "github.com/caos/zitadel/internal/eventstore/v2" ) @@ -38,3 +40,18 @@ func AggregateFromReadModel(rm *ReadModel) *Aggregate { SetUpStarted: rm.SetUpStarted, } } + +func (a *Aggregate) PushMemberAdded(ctx context.Context, userID string, roles ...string) *Aggregate { + a.Aggregate = *a.PushEvents(NewMemberAddedEvent(ctx, userID, roles...)) + return a +} + +func (a *Aggregate) PushMemberChanged(ctx context.Context, userID string, roles ...string) *Aggregate { + a.Aggregate = *a.PushEvents(NewMemberChangedEvent(ctx, userID, roles...)) + return a +} + +func (a *Aggregate) PushMemberRemoved(ctx context.Context, userID string) *Aggregate { + a.Aggregate = *a.PushEvents(NewMemberRemovedEvent(ctx, userID)) + return a +} diff --git a/internal/v2/repository/idp/config.go b/internal/v2/repository/idp/config.go index dbd5a4b201..667a9c5869 100644 --- a/internal/v2/repository/idp/config.go +++ b/internal/v2/repository/idp/config.go @@ -1 +1,60 @@ package idp + +import ( + "github.com/caos/zitadel/internal/eventstore/v2" + "github.com/caos/zitadel/internal/v2/repository/idp/oidc" +) + +type ConfigAggregate struct { + eventstore.Aggregate + + ConfigID string + Type ConfigType + Name string + StylingType StylingType + State ConfigState + OIDCConfig *oidc.ConfigReadModel +} + +type ConfigReadModel struct { + eventstore.ReadModel + + ConfigID string + Type ConfigType + Name string + StylingType StylingType + State ConfigState + OIDCConfig *oidc.ConfigReadModel +} + +func (rm *ConfigReadModel) AppendEvents(events ...eventstore.EventReader) error { + rm.ReadModel.AppendEvents(events...) + + return nil +} + +func (rm *ConfigReadModel) Reduce() error { + return nil +} + +type ConfigType int32 + +const ( + ConfigTypeOIDC ConfigType = iota + ConfigTypeSAML +) + +type ConfigState int32 + +const ( + ConfigStateActive ConfigState = iota + ConfigStateInactive + ConfigStateRemoved +) + +type StylingType int32 + +const ( + StylingTypeUnspecified StylingType = iota + StylingTypeGoogle +) diff --git a/internal/v2/repository/idp/event_added.go b/internal/v2/repository/idp/event_added.go new file mode 100644 index 0000000000..433c672457 --- /dev/null +++ b/internal/v2/repository/idp/event_added.go @@ -0,0 +1,31 @@ +package idp + +import "github.com/caos/zitadel/internal/eventstore/v2" + +type AddedEvent struct { + eventstore.BaseEvent + + ID string `idpConfigId` + Name string `name` +} + +func NewAddedEvent( + base *eventstore.BaseEvent, + configID string, + name string, +) *AddedEvent { + + return &AddedEvent{ + BaseEvent: *base, + ID: configID, + Name: name, + } +} + +func (e *AddedEvent) CheckPrevious() bool { + return true +} + +func (e *AddedEvent) Data() interface{} { + return e +} diff --git a/internal/v2/repository/idp/event_changed.go b/internal/v2/repository/idp/event_changed.go new file mode 100644 index 0000000000..135ff8ba38 --- /dev/null +++ b/internal/v2/repository/idp/event_changed.go @@ -0,0 +1,37 @@ +package idp + +import "github.com/caos/zitadel/internal/eventstore/v2" + +type ChangedEdvent struct { + eventstore.BaseEvent `json:"-"` + + current *ConfigAggregate + changed *ConfigAggregate + + Name string `json:"name"` +} + +func ChangedEvent( + base *eventstore.BaseEvent, + current *ConfigAggregate, + changed *ConfigAggregate, +) (*ChangedEdvent, error) { + //TODO: who to handle chanes? + + return &ChangedEdvent{ + BaseEvent: *base, + current: current, + changed: changed, + }, nil +} + +func (e *ChangedEdvent) CheckPrevious() bool { + return true +} + +func (e *ChangedEdvent) Data() interface{} { + if e.current.Name != e.changed.Name { + e.Name = e.changed.Name + } + return e +} diff --git a/internal/v2/repository/idp/event_deactivated.go b/internal/v2/repository/idp/event_deactivated.go new file mode 100644 index 0000000000..476e9a7eea --- /dev/null +++ b/internal/v2/repository/idp/event_deactivated.go @@ -0,0 +1,28 @@ +package idp + +import "github.com/caos/zitadel/internal/eventstore/v2" + +type DeactivatedEvent struct { + eventstore.BaseEvent + + ID string `idpConfigId` +} + +func NewDeactivatedEvent( + base *eventstore.BaseEvent, + configID string, +) *DeactivatedEvent { + + return &DeactivatedEvent{ + BaseEvent: *base, + ID: configID, + } +} + +func (e *DeactivatedEvent) CheckPrevious() bool { + return true +} + +func (e *DeactivatedEvent) Data() interface{} { + return e +} diff --git a/internal/v2/repository/idp/oidc/added_event.go b/internal/v2/repository/idp/oidc/added_event.go new file mode 100644 index 0000000000..59dd574fff --- /dev/null +++ b/internal/v2/repository/idp/oidc/added_event.go @@ -0,0 +1,3 @@ +package oidc + +type AddedEvent struct{} diff --git a/internal/v2/repository/idp/oidc/changed_event.go b/internal/v2/repository/idp/oidc/changed_event.go new file mode 100644 index 0000000000..3c6cc2559e --- /dev/null +++ b/internal/v2/repository/idp/oidc/changed_event.go @@ -0,0 +1,3 @@ +package oidc + +type ChangedEvent struct{} diff --git a/internal/v2/repository/idp/oidc/config.go b/internal/v2/repository/idp/oidc/config.go new file mode 100644 index 0000000000..e8755c51c6 --- /dev/null +++ b/internal/v2/repository/idp/oidc/config.go @@ -0,0 +1,35 @@ +package oidc + +import ( + "github.com/caos/zitadel/internal/crypto" + "github.com/caos/zitadel/internal/eventstore/v2" +) + +type ConfigReadModel struct { + eventstore.ReadModel + + IDPConfigID string + ClientID string + ClientSecret *crypto.CryptoValue + ClientSecretString string + Issuer string + Scopes []string + IDPDisplayNameMapping MappingField + UsernameMapping MappingField +} + +func (rm *ConfigReadModel) AppendEvents(events ...eventstore.EventReader) error { + return nil +} + +func (rm *ConfigReadModel) Reduce() error { + return nil +} + +type MappingField int32 + +const ( + OIDCMappingFieldUnspecified MappingField = iota + OIDCMappingFieldPreferredLoginName + OIDCMappingFieldEmail +) diff --git a/internal/v2/repository/idp/oidc/deactivated_event.go b/internal/v2/repository/idp/oidc/deactivated_event.go new file mode 100644 index 0000000000..5871a60ce5 --- /dev/null +++ b/internal/v2/repository/idp/oidc/deactivated_event.go @@ -0,0 +1,3 @@ +package oidc + +type DeactivatedEvent struct{} diff --git a/internal/v2/repository/idp/oidc/removed_event.go b/internal/v2/repository/idp/oidc/removed_event.go new file mode 100644 index 0000000000..516eef5b07 --- /dev/null +++ b/internal/v2/repository/idp/oidc/removed_event.go @@ -0,0 +1,3 @@ +package oidc + +type RemovedEvent struct{}