2020-09-18 11:26:28 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-12-18 15:47:45 +00:00
|
|
|
|
2020-09-18 11:26:28 +00:00
|
|
|
"github.com/caos/logging"
|
|
|
|
"github.com/caos/zitadel/internal/config/systemdefaults"
|
2020-12-18 15:47:45 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
2020-09-18 11:26:28 +00:00
|
|
|
"github.com/caos/zitadel/internal/iam/repository/eventsourcing"
|
|
|
|
org_es "github.com/caos/zitadel/internal/org/repository/eventsourcing"
|
|
|
|
org_es_model "github.com/caos/zitadel/internal/org/repository/eventsourcing/model"
|
|
|
|
|
|
|
|
"github.com/caos/zitadel/internal/eventstore/models"
|
|
|
|
es_models "github.com/caos/zitadel/internal/eventstore/models"
|
2020-12-18 15:47:45 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/query"
|
2020-09-18 11:26:28 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/spooler"
|
|
|
|
iam_model "github.com/caos/zitadel/internal/iam/model"
|
|
|
|
"github.com/caos/zitadel/internal/iam/repository/eventsourcing/model"
|
|
|
|
iam_view_model "github.com/caos/zitadel/internal/iam/repository/view/model"
|
|
|
|
)
|
|
|
|
|
2020-12-18 15:47:45 +00:00
|
|
|
const (
|
|
|
|
idpProviderTable = "auth.idp_providers"
|
|
|
|
)
|
|
|
|
|
2020-09-18 11:26:28 +00:00
|
|
|
type IDPProvider struct {
|
|
|
|
handler
|
|
|
|
systemDefaults systemdefaults.SystemDefaults
|
|
|
|
iamEvents *eventsourcing.IAMEventstore
|
|
|
|
orgEvents *org_es.OrgEventstore
|
2020-12-18 15:47:45 +00:00
|
|
|
subscription *eventstore.Subscription
|
2020-09-18 11:26:28 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 15:47:45 +00:00
|
|
|
func newIDPProvider(
|
|
|
|
h handler,
|
|
|
|
defaults systemdefaults.SystemDefaults,
|
|
|
|
iamEvents *eventsourcing.IAMEventstore,
|
|
|
|
orgEvents *org_es.OrgEventstore,
|
|
|
|
) *IDPProvider {
|
|
|
|
idpProvider := &IDPProvider{
|
|
|
|
handler: h,
|
|
|
|
systemDefaults: defaults,
|
|
|
|
iamEvents: iamEvents,
|
|
|
|
orgEvents: orgEvents,
|
|
|
|
}
|
|
|
|
|
|
|
|
idpProvider.subscribe()
|
|
|
|
|
|
|
|
return idpProvider
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *IDPProvider) subscribe() {
|
|
|
|
i.subscription = i.es.Subscribe(i.AggregateTypes()...)
|
|
|
|
go func() {
|
|
|
|
for event := range i.subscription.Events {
|
|
|
|
query.ReduceEvent(i, event)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2020-09-18 11:26:28 +00:00
|
|
|
|
2020-12-02 07:50:59 +00:00
|
|
|
func (i *IDPProvider) ViewModel() string {
|
2020-09-18 11:26:28 +00:00
|
|
|
return idpProviderTable
|
|
|
|
}
|
|
|
|
|
2020-12-18 15:47:45 +00:00
|
|
|
func (_ *IDPProvider) AggregateTypes() []models.AggregateType {
|
|
|
|
return []models.AggregateType{model.IAMAggregate, org_es_model.OrgAggregate}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *IDPProvider) CurrentSequence(event *models.Event) (uint64, error) {
|
|
|
|
sequence, err := i.view.GetLatestIDPProviderSequence(string(event.AggregateType))
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return sequence.CurrentSequence, nil
|
|
|
|
}
|
|
|
|
|
2020-12-02 07:50:59 +00:00
|
|
|
func (i *IDPProvider) EventQuery() (*models.SearchQuery, error) {
|
2020-12-18 15:47:45 +00:00
|
|
|
sequence, err := i.view.GetLatestIDPProviderSequence("")
|
2020-09-18 11:26:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return es_models.NewSearchQuery().
|
2020-12-18 15:47:45 +00:00
|
|
|
AggregateTypeFilter(i.AggregateTypes()...).
|
2020-09-18 11:26:28 +00:00
|
|
|
LatestSequenceFilter(sequence.CurrentSequence), nil
|
|
|
|
}
|
|
|
|
|
2020-12-02 07:50:59 +00:00
|
|
|
func (i *IDPProvider) Reduce(event *models.Event) (err error) {
|
2020-09-18 11:26:28 +00:00
|
|
|
switch event.AggregateType {
|
|
|
|
case model.IAMAggregate, org_es_model.OrgAggregate:
|
2020-12-02 07:50:59 +00:00
|
|
|
err = i.processIdpProvider(event)
|
2020-09-18 11:26:28 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-02 07:50:59 +00:00
|
|
|
func (i *IDPProvider) processIdpProvider(event *models.Event) (err error) {
|
2020-09-18 11:26:28 +00:00
|
|
|
provider := new(iam_view_model.IDPProviderView)
|
|
|
|
switch event.Type {
|
|
|
|
case model.LoginPolicyIDPProviderAdded, org_es_model.LoginPolicyIDPProviderAdded:
|
|
|
|
err = provider.AppendEvent(event)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-02 07:50:59 +00:00
|
|
|
err = i.fillData(provider)
|
2020-09-18 11:26:28 +00:00
|
|
|
case model.LoginPolicyIDPProviderRemoved, model.LoginPolicyIDPProviderCascadeRemoved,
|
|
|
|
org_es_model.LoginPolicyIDPProviderRemoved, org_es_model.LoginPolicyIDPProviderCascadeRemoved:
|
|
|
|
err = provider.SetData(event)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-18 15:47:45 +00:00
|
|
|
return i.view.DeleteIDPProvider(event.AggregateID, provider.IDPConfigID, event)
|
2020-09-18 11:26:28 +00:00
|
|
|
case model.IDPConfigChanged, org_es_model.IDPConfigChanged:
|
2020-10-20 12:26:19 +00:00
|
|
|
esConfig := new(iam_view_model.IDPConfigView)
|
|
|
|
providerType := iam_model.IDPProviderTypeSystem
|
2020-12-02 07:50:59 +00:00
|
|
|
if event.AggregateID != i.systemDefaults.IamID {
|
2020-10-20 12:26:19 +00:00
|
|
|
providerType = iam_model.IDPProviderTypeOrg
|
|
|
|
}
|
|
|
|
esConfig.AppendEvent(providerType, event)
|
2020-12-02 07:50:59 +00:00
|
|
|
providers, err := i.view.IDPProvidersByIDPConfigID(esConfig.IDPConfigID)
|
2020-09-18 11:26:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-20 12:26:19 +00:00
|
|
|
config := new(iam_model.IDPConfig)
|
2020-12-02 07:50:59 +00:00
|
|
|
if event.AggregateID == i.systemDefaults.IamID {
|
|
|
|
config, err = i.iamEvents.GetIDPConfig(context.Background(), event.AggregateID, esConfig.IDPConfigID)
|
2020-09-18 11:26:28 +00:00
|
|
|
} else {
|
2020-12-02 07:50:59 +00:00
|
|
|
config, err = i.orgEvents.GetIDPConfig(context.Background(), event.AggregateID, esConfig.IDPConfigID)
|
2020-09-18 11:26:28 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, provider := range providers {
|
2020-12-02 07:50:59 +00:00
|
|
|
i.fillConfigData(provider, config)
|
2020-09-18 11:26:28 +00:00
|
|
|
}
|
2020-12-18 15:47:45 +00:00
|
|
|
return i.view.PutIDPProviders(event, providers...)
|
2020-09-18 11:26:28 +00:00
|
|
|
case org_es_model.LoginPolicyRemoved:
|
2020-12-18 15:47:45 +00:00
|
|
|
return i.view.DeleteIDPProvidersByAggregateID(event.AggregateID, event)
|
2020-09-18 11:26:28 +00:00
|
|
|
default:
|
2020-12-18 15:47:45 +00:00
|
|
|
return i.view.ProcessedIDPProviderSequence(event)
|
2020-09-18 11:26:28 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-18 15:47:45 +00:00
|
|
|
return i.view.PutIDPProvider(provider, event)
|
2020-09-18 11:26:28 +00:00
|
|
|
}
|
|
|
|
|
2020-12-02 07:50:59 +00:00
|
|
|
func (i *IDPProvider) fillData(provider *iam_view_model.IDPProviderView) (err error) {
|
2020-09-18 11:26:28 +00:00
|
|
|
var config *iam_model.IDPConfig
|
|
|
|
if provider.IDPProviderType == int32(iam_model.IDPProviderTypeSystem) {
|
2020-12-02 07:50:59 +00:00
|
|
|
config, err = i.iamEvents.GetIDPConfig(context.Background(), i.systemDefaults.IamID, provider.IDPConfigID)
|
2020-09-18 11:26:28 +00:00
|
|
|
} else {
|
2020-12-02 07:50:59 +00:00
|
|
|
config, err = i.orgEvents.GetIDPConfig(context.Background(), provider.AggregateID, provider.IDPConfigID)
|
2020-09-18 11:26:28 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-02 07:50:59 +00:00
|
|
|
i.fillConfigData(provider, config)
|
2020-09-18 11:26:28 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-02 07:50:59 +00:00
|
|
|
func (i *IDPProvider) fillConfigData(provider *iam_view_model.IDPProviderView, config *iam_model.IDPConfig) {
|
2020-09-18 11:26:28 +00:00
|
|
|
provider.Name = config.Name
|
2020-10-19 15:10:02 +00:00
|
|
|
provider.StylingType = int32(config.StylingType)
|
2020-09-18 11:26:28 +00:00
|
|
|
provider.IDPConfigType = int32(config.Type)
|
2020-09-23 14:52:19 +00:00
|
|
|
provider.IDPState = int32(config.State)
|
2020-09-18 11:26:28 +00:00
|
|
|
}
|
|
|
|
|
2020-12-02 07:50:59 +00:00
|
|
|
func (i *IDPProvider) OnError(event *models.Event, err error) error {
|
2020-09-18 11:26:28 +00:00
|
|
|
logging.LogWithFields("SPOOL-Fjd89", "id", event.AggregateID).WithError(err).Warn("something went wrong in idp provider handler")
|
2020-12-02 07:50:59 +00:00
|
|
|
return spooler.HandleError(event, err, i.view.GetLatestIDPProviderFailedEvent, i.view.ProcessedIDPProviderFailedEvent, i.view.ProcessedIDPProviderSequence, i.errorCountUntilSkip)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *IDPProvider) OnSuccess() error {
|
|
|
|
return spooler.HandleSuccess(i.view.UpdateIDPProviderSpoolerRunTimestamp)
|
2020-09-18 11:26:28 +00:00
|
|
|
}
|