2020-09-18 11:26:28 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/logging"
|
2022-03-31 09:36:26 +00:00
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
v1 "github.com/zitadel/zitadel/internal/eventstore/v1"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/v1/models"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/v1/query"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/v1/spooler"
|
|
|
|
iam_model "github.com/zitadel/zitadel/internal/iam/model"
|
|
|
|
iam_view_model "github.com/zitadel/zitadel/internal/iam/repository/view/model"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/org"
|
2020-09-18 11:26:28 +00:00
|
|
|
)
|
|
|
|
|
2020-12-18 15:47:45 +00:00
|
|
|
const (
|
|
|
|
idpConfigTable = "auth.idp_configs"
|
|
|
|
)
|
|
|
|
|
2020-09-18 11:26:28 +00:00
|
|
|
type IDPConfig struct {
|
|
|
|
handler
|
2021-02-23 14:13:04 +00:00
|
|
|
subscription *v1.Subscription
|
2020-09-18 11:26:28 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 15:47:45 +00:00
|
|
|
func newIDPConfig(h handler) *IDPConfig {
|
|
|
|
idpConfig := &IDPConfig{
|
|
|
|
handler: h,
|
|
|
|
}
|
|
|
|
|
|
|
|
idpConfig.subscribe()
|
|
|
|
|
|
|
|
return idpConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *IDPConfig) 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 *IDPConfig) ViewModel() string {
|
2020-09-18 11:26:28 +00:00
|
|
|
return idpConfigTable
|
|
|
|
}
|
|
|
|
|
2021-07-06 11:36:35 +00:00
|
|
|
func (i *IDPConfig) Subscription() *v1.Subscription {
|
|
|
|
return i.subscription
|
|
|
|
}
|
|
|
|
|
2022-03-31 09:36:26 +00:00
|
|
|
func (_ *IDPConfig) AggregateTypes() []models.AggregateType {
|
|
|
|
return []models.AggregateType{org.AggregateType, instance.AggregateType}
|
2020-12-18 15:47:45 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 06:26:12 +00:00
|
|
|
func (i *IDPConfig) CurrentSequence(instanceID string) (uint64, error) {
|
|
|
|
sequence, err := i.view.GetLatestIDPConfigSequence(instanceID)
|
2020-12-18 15:47:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return sequence.CurrentSequence, nil
|
|
|
|
}
|
|
|
|
|
2022-03-31 09:36:26 +00:00
|
|
|
func (i *IDPConfig) EventQuery() (*models.SearchQuery, error) {
|
2022-04-19 06:26:12 +00:00
|
|
|
sequences, err := i.view.GetLatestIDPConfigSequences()
|
2020-09-18 11:26:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-04-19 06:26:12 +00:00
|
|
|
|
|
|
|
query := models.NewSearchQuery()
|
|
|
|
instances := make([]string, 0)
|
|
|
|
for _, sequence := range sequences {
|
|
|
|
for _, instance := range instances {
|
|
|
|
if sequence.InstanceID == instance {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
instances = append(instances, sequence.InstanceID)
|
|
|
|
query.AddQuery().
|
|
|
|
AggregateTypeFilter(i.AggregateTypes()...).
|
|
|
|
LatestSequenceFilter(sequence.CurrentSequence).
|
|
|
|
InstanceIDFilter(sequence.InstanceID)
|
|
|
|
}
|
|
|
|
return query.AddQuery().
|
2020-12-18 15:47:45 +00:00
|
|
|
AggregateTypeFilter(i.AggregateTypes()...).
|
2022-04-19 06:26:12 +00:00
|
|
|
LatestSequenceFilter(0).
|
|
|
|
ExcludedInstanceIDsFilter(instances...).
|
|
|
|
SearchQuery(), nil
|
2020-09-18 11:26:28 +00:00
|
|
|
}
|
|
|
|
|
2022-03-31 09:36:26 +00:00
|
|
|
func (i *IDPConfig) Reduce(event *models.Event) (err error) {
|
2020-09-18 11:26:28 +00:00
|
|
|
switch event.AggregateType {
|
2022-03-31 09:36:26 +00:00
|
|
|
case org.AggregateType:
|
2020-12-02 07:50:59 +00:00
|
|
|
err = i.processIdpConfig(iam_model.IDPProviderTypeOrg, event)
|
2022-03-31 09:36:26 +00:00
|
|
|
case instance.AggregateType:
|
2020-12-02 07:50:59 +00:00
|
|
|
err = i.processIdpConfig(iam_model.IDPProviderTypeSystem, event)
|
2020-09-18 11:26:28 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-31 09:36:26 +00:00
|
|
|
func (i *IDPConfig) processIdpConfig(providerType iam_model.IDPProviderType, event *models.Event) (err error) {
|
2020-09-18 11:26:28 +00:00
|
|
|
idp := new(iam_view_model.IDPConfigView)
|
2022-03-31 09:36:26 +00:00
|
|
|
switch eventstore.EventType(event.Type) {
|
|
|
|
case org.IDPConfigAddedEventType,
|
|
|
|
instance.IDPConfigAddedEventType:
|
2020-09-18 11:26:28 +00:00
|
|
|
err = idp.AppendEvent(providerType, event)
|
2022-03-31 09:36:26 +00:00
|
|
|
case org.IDPConfigChangedEventType, instance.IDPConfigChangedEventType,
|
|
|
|
org.IDPOIDCConfigAddedEventType, instance.IDPOIDCConfigAddedEventType,
|
|
|
|
org.IDPOIDCConfigChangedEventType, instance.IDPOIDCConfigChangedEventType,
|
|
|
|
org.IDPJWTConfigAddedEventType, instance.IDPJWTConfigAddedEventType,
|
|
|
|
org.IDPJWTConfigChangedEventType, instance.IDPJWTConfigChangedEventType:
|
2020-09-18 11:26:28 +00:00
|
|
|
err = idp.SetData(event)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-19 06:26:12 +00:00
|
|
|
idp, err = i.view.IDPConfigByID(idp.IDPConfigID, idp.InstanceID)
|
2020-09-18 11:26:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = idp.AppendEvent(providerType, event)
|
2022-03-31 09:36:26 +00:00
|
|
|
case org.IDPConfigDeactivatedEventType, instance.IDPConfigDeactivatedEventType,
|
|
|
|
org.IDPConfigReactivatedEventType, instance.IDPConfigReactivatedEventType:
|
fix: Merge master (#1373)
* feat(console): app infos, api apps, fix redirects on create, fix role update, redesign idps, policy, prettier history (#1310)
* idp fixes
* idp cleanup and rehaul, complexity policy preview
* policy fixes, orthodox redirect
* link component, add links to policies
* redirect pipe, state labels
* Cnsl map changes (#1315)
* map changes to different format
* fix changes layout, cursor
* set asc values
* fix user appearance in changes, index
* changes
* app create with api
* api app create
* auth method for api config
* authmethods, app card for api, authmethod in dev create
* move machine keys to own module
* jwt method for oidc
* fix app edit
* save toast
* fix changes, change det in app detail
* regenerate secret
* chore(deps-dev): bump @angular-devkit/build-angular in /console (#1324)
Bumps [@angular-devkit/build-angular](https://github.com/angular/angular-cli) from 0.1102.0 to 0.1102.1.
- [Release notes](https://github.com/angular/angular-cli/releases)
- [Commits](https://github.com/angular/angular-cli/commits)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
* fix policy backlink
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
(cherry picked from commit 40a7e958d712f83585e25f18f4fff620de5e3269)
* fix: i18n refs, unnecessary logs (#1343)
(cherry picked from commit 2e04c977eba295cab6aa533379f6075d2e27ab42)
* fix: tos link (#1345)
(cherry picked from commit 5333ef10c169e3656e3ca8dfb8edf7f738fa0a26)
* fix: reactivate/deactivate idp, remove idp provider (#1348)
* fix: reactivate/deactivate idp, remove idp provider
* fix build
* fix(console): add jwt to selection, idp deactivate reactivate (#1347)
* fix: log error on idp change
* add jwt to method selection
Co-authored-by: Max Peintner <max@caos.ch>
(cherry picked from commit c8b9888427f4ea6b513d370f9fed555208594edf)
* fix: reactivate/deactivate idp (#1351)
(cherry picked from commit 54f395e2e05d4adb29321e9597d049800c033b61)
Co-authored-by: Max Peintner <max@caos.ch>
2021-03-01 08:01:34 +00:00
|
|
|
err = idp.SetData(event)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-19 06:26:12 +00:00
|
|
|
idp, err = i.view.IDPConfigByID(idp.IDPConfigID, idp.InstanceID)
|
fix: Merge master (#1373)
* feat(console): app infos, api apps, fix redirects on create, fix role update, redesign idps, policy, prettier history (#1310)
* idp fixes
* idp cleanup and rehaul, complexity policy preview
* policy fixes, orthodox redirect
* link component, add links to policies
* redirect pipe, state labels
* Cnsl map changes (#1315)
* map changes to different format
* fix changes layout, cursor
* set asc values
* fix user appearance in changes, index
* changes
* app create with api
* api app create
* auth method for api config
* authmethods, app card for api, authmethod in dev create
* move machine keys to own module
* jwt method for oidc
* fix app edit
* save toast
* fix changes, change det in app detail
* regenerate secret
* chore(deps-dev): bump @angular-devkit/build-angular in /console (#1324)
Bumps [@angular-devkit/build-angular](https://github.com/angular/angular-cli) from 0.1102.0 to 0.1102.1.
- [Release notes](https://github.com/angular/angular-cli/releases)
- [Commits](https://github.com/angular/angular-cli/commits)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
* fix policy backlink
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
(cherry picked from commit 40a7e958d712f83585e25f18f4fff620de5e3269)
* fix: i18n refs, unnecessary logs (#1343)
(cherry picked from commit 2e04c977eba295cab6aa533379f6075d2e27ab42)
* fix: tos link (#1345)
(cherry picked from commit 5333ef10c169e3656e3ca8dfb8edf7f738fa0a26)
* fix: reactivate/deactivate idp, remove idp provider (#1348)
* fix: reactivate/deactivate idp, remove idp provider
* fix build
* fix(console): add jwt to selection, idp deactivate reactivate (#1347)
* fix: log error on idp change
* add jwt to method selection
Co-authored-by: Max Peintner <max@caos.ch>
(cherry picked from commit c8b9888427f4ea6b513d370f9fed555208594edf)
* fix: reactivate/deactivate idp (#1351)
(cherry picked from commit 54f395e2e05d4adb29321e9597d049800c033b61)
Co-authored-by: Max Peintner <max@caos.ch>
2021-03-01 08:01:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = idp.AppendEvent(providerType, event)
|
2022-03-31 09:36:26 +00:00
|
|
|
case org.IDPConfigRemovedEventType, instance.IDPConfigRemovedEventType:
|
2020-09-18 11:26:28 +00:00
|
|
|
err = idp.SetData(event)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-18 15:47:45 +00:00
|
|
|
return i.view.DeleteIDPConfig(idp.IDPConfigID, event)
|
2020-09-18 11:26:28 +00:00
|
|
|
default:
|
2020-12-18 15:47:45 +00:00
|
|
|
return i.view.ProcessedIDPConfigSequence(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.PutIDPConfig(idp, event)
|
2020-09-18 11:26:28 +00:00
|
|
|
}
|
|
|
|
|
2022-03-31 09:36:26 +00:00
|
|
|
func (i *IDPConfig) OnError(event *models.Event, err error) error {
|
2020-09-18 11:26:28 +00:00
|
|
|
logging.LogWithFields("SPOOL-Ejf8s", "id", event.AggregateID).WithError(err).Warn("something went wrong in idp config handler")
|
2020-12-02 07:50:59 +00:00
|
|
|
return spooler.HandleError(event, err, i.view.GetLatestIDPConfigFailedEvent, i.view.ProcessedIDPConfigFailedEvent, i.view.ProcessedIDPConfigSequence, i.errorCountUntilSkip)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *IDPConfig) OnSuccess() error {
|
|
|
|
return spooler.HandleSuccess(i.view.UpdateIDPConfigSpoolerRunTimestamp)
|
2020-09-18 11:26:28 +00:00
|
|
|
}
|