mirror of
https://github.com/andwati/zitadel.git
synced 2026-11-30 16:08:56 +00:00
refactor: merge relational table projections into one handler (#11740)
## Problem description During tests we found concurrency issues in the relational table projections. Tests got flaky if a table (e.g. organization domains) had too many foreign keys to tables which were projected by another handler. ## Resolution The projections of the relational tables got merged into one handler to enforce sequential reduction of events. ## Additional info - closes https://github.com/zitadel/zitadel/issues/11703 - closes https://github.com/zitadel/zitadel/issues/10810 --------- Co-authored-by: Marco Ardizzone <[email protected]>
This commit is contained in:
co-authored by
Marco Ardizzone
parent
689b90d9d9
commit
de1cab9976
@@ -0,0 +1,254 @@
|
||||
package projection
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"slices"
|
||||
|
||||
"github.com/muhlemmer/gu"
|
||||
|
||||
"github.com/zitadel/zitadel/backend/v3/domain"
|
||||
"github.com/zitadel/zitadel/backend/v3/storage/database"
|
||||
v3_sql "github.com/zitadel/zitadel/backend/v3/storage/database/dialect/sql"
|
||||
"github.com/zitadel/zitadel/backend/v3/storage/database/repository"
|
||||
"github.com/zitadel/zitadel/internal/eventstore"
|
||||
"github.com/zitadel/zitadel/internal/eventstore/handler/v2"
|
||||
"github.com/zitadel/zitadel/internal/repository/idp"
|
||||
"github.com/zitadel/zitadel/internal/repository/instance"
|
||||
"github.com/zitadel/zitadel/internal/repository/org"
|
||||
"github.com/zitadel/zitadel/internal/zerrors"
|
||||
)
|
||||
|
||||
func (p *relationalTablesProjection) reduceGitLabIDPAdded(event eventstore.Event) (*handler.Statement, error) {
|
||||
var orgId *string
|
||||
var idpEvent idp.GitLabIDPAddedEvent
|
||||
switch e := event.(type) {
|
||||
case *org.GitLabIDPAddedEvent:
|
||||
idpEvent = e.GitLabIDPAddedEvent
|
||||
orgId = &idpEvent.Aggregate().ResourceOwner
|
||||
case *instance.GitLabIDPAddedEvent:
|
||||
idpEvent = e.GitLabIDPAddedEvent
|
||||
default:
|
||||
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-Y9a022b", "reduce.wrong.event.type %v", []eventstore.EventType{org.GitLabIDPAddedEventType, instance.GitLabIDPAddedEventType})
|
||||
}
|
||||
|
||||
gitlab := domain.Gitlab{
|
||||
ClientID: idpEvent.ClientID,
|
||||
ClientSecret: idpEvent.ClientSecret,
|
||||
Scopes: idpEvent.Scopes,
|
||||
}
|
||||
|
||||
payloadJSON, err := json.Marshal(gitlab)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return handler.NewStatement(event, func(ctx context.Context, ex handler.Executer, _ string) error {
|
||||
tx, ok := ex.(*sql.Tx)
|
||||
if !ok {
|
||||
return zerrors.ThrowInvalidArgumentf(nil, "HANDL-kN8Qx", "reduce.wrong.db.pool %T", ex)
|
||||
}
|
||||
|
||||
repo := repository.IDProviderRepository()
|
||||
return repo.Create(ctx, v3_sql.SQLTx(tx), &domain.IdentityProvider{
|
||||
InstanceID: idpEvent.Aggregate().InstanceID,
|
||||
OrgID: orgId,
|
||||
ID: idpEvent.ID,
|
||||
State: domain.IDPStateActive,
|
||||
Name: idpEvent.Name,
|
||||
Type: gu.Ptr(domain.IDPTypeGitLab),
|
||||
AllowCreation: idpEvent.IsCreationAllowed,
|
||||
AllowAutoCreation: idpEvent.IsAutoCreation,
|
||||
AllowAutoUpdate: idpEvent.IsAutoUpdate,
|
||||
AllowLinking: idpEvent.IsLinkingAllowed,
|
||||
AutoLinkingField: mapAutoLinkingField(idpEvent.AutoLinkingOption),
|
||||
Payload: payloadJSON,
|
||||
CreatedAt: event.CreatedAt(),
|
||||
UpdatedAt: event.CreatedAt(),
|
||||
})
|
||||
}), nil
|
||||
}
|
||||
|
||||
func (p *relationalTablesProjection) reduceGitLabIDPChanged(event eventstore.Event) (*handler.Statement, error) {
|
||||
var orgId *string
|
||||
var idpEvent idp.GitLabIDPChangedEvent
|
||||
switch e := event.(type) {
|
||||
case *org.GitLabIDPChangedEvent:
|
||||
idpEvent = e.GitLabIDPChangedEvent
|
||||
orgId = &idpEvent.Aggregate().ResourceOwner
|
||||
case *instance.GitLabIDPChangedEvent:
|
||||
idpEvent = e.GitLabIDPChangedEvent
|
||||
default:
|
||||
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-mT5827b", "reduce.wrong.event.type %v", []eventstore.EventType{org.GitLabIDPChangedEventType, instance.GitLabIDPChangedEventType})
|
||||
}
|
||||
|
||||
return handler.NewStatement(event, func(ctx context.Context, ex handler.Executer, _ string) error {
|
||||
tx, ok := ex.(*sql.Tx)
|
||||
if !ok {
|
||||
return zerrors.ThrowInternal(nil, "HANDL-HX6ed", "unable to cast to tx executer")
|
||||
}
|
||||
idpRepo := repository.IDProviderRepository()
|
||||
gitlab, err := idpRepo.GetGitlab(ctx, v3_sql.SQLTx(tx), database.WithCondition(idpScopedCondition(idpRepo, idpEvent.Agg.InstanceID, idpEvent.ID, orgId)))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
changes := p.reduceIDPChangedTemplateColumns(idpRepo, idpEvent.Name, idpEvent.OptionChanges)
|
||||
|
||||
payload := &gitlab.Gitlab
|
||||
payloadChanged := p.reduceGitLabIDPChangedColumns(payload, &idpEvent)
|
||||
if payloadChanged {
|
||||
payloadJSON, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
changes = append(changes, idpRepo.SetPayload(string(payloadJSON)))
|
||||
}
|
||||
|
||||
changes = append(changes, idpRepo.SetUpdatedAt(gu.Ptr(event.CreatedAt())))
|
||||
_, err = idpRepo.Update(ctx, v3_sql.SQLTx(tx), idpScopedCondition(idpRepo, idpEvent.Aggregate().InstanceID, idpEvent.ID, orgId), changes...)
|
||||
return err
|
||||
|
||||
}), nil
|
||||
}
|
||||
|
||||
func (p *relationalTablesProjection) reduceGitLabSelfHostedIDPAdded(event eventstore.Event) (*handler.Statement, error) {
|
||||
var orgId *string
|
||||
var idpEvent idp.GitLabSelfHostedIDPAddedEvent
|
||||
switch e := event.(type) {
|
||||
case *org.GitLabSelfHostedIDPAddedEvent:
|
||||
idpEvent = e.GitLabSelfHostedIDPAddedEvent
|
||||
orgId = &idpEvent.Aggregate().ResourceOwner
|
||||
case *instance.GitLabSelfHostedIDPAddedEvent:
|
||||
idpEvent = e.GitLabSelfHostedIDPAddedEvent
|
||||
default:
|
||||
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-YAF3gw", "reduce.wrong.event.type %v", []eventstore.EventType{org.GitLabSelfHostedIDPAddedEventType, instance.GitLabSelfHostedIDPAddedEventType})
|
||||
}
|
||||
|
||||
gitlabSelfHosted := domain.GitlabSelfHosted{
|
||||
Issuer: idpEvent.Issuer,
|
||||
ClientID: idpEvent.ClientID,
|
||||
ClientSecret: idpEvent.ClientSecret,
|
||||
Scopes: idpEvent.Scopes,
|
||||
}
|
||||
|
||||
payloadJSON, err := json.Marshal(gitlabSelfHosted)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return handler.NewStatement(event, func(ctx context.Context, ex handler.Executer, _ string) error {
|
||||
tx, ok := ex.(*sql.Tx)
|
||||
if !ok {
|
||||
return zerrors.ThrowInvalidArgumentf(nil, "HANDL-FQrtw", "reduce.wrong.db.pool %T", ex)
|
||||
}
|
||||
|
||||
repo := repository.IDProviderRepository()
|
||||
return repo.Create(ctx, v3_sql.SQLTx(tx), &domain.IdentityProvider{
|
||||
InstanceID: idpEvent.Aggregate().InstanceID,
|
||||
OrgID: orgId,
|
||||
ID: idpEvent.ID,
|
||||
State: domain.IDPStateActive,
|
||||
Name: idpEvent.Name,
|
||||
Type: gu.Ptr(domain.IDPTypeGitLabSelfHosted),
|
||||
AllowCreation: idpEvent.IsCreationAllowed,
|
||||
AllowAutoCreation: idpEvent.IsAutoCreation,
|
||||
AllowAutoUpdate: idpEvent.IsAutoUpdate,
|
||||
AllowLinking: idpEvent.IsLinkingAllowed,
|
||||
AutoLinkingField: mapAutoLinkingField(idpEvent.AutoLinkingOption),
|
||||
Payload: payloadJSON,
|
||||
CreatedAt: event.CreatedAt(),
|
||||
UpdatedAt: event.CreatedAt(),
|
||||
})
|
||||
}), nil
|
||||
}
|
||||
|
||||
func (p *relationalTablesProjection) reduceGitLabSelfHostedIDPChanged(event eventstore.Event) (*handler.Statement, error) {
|
||||
var orgId *string
|
||||
var idpEvent idp.GitLabSelfHostedIDPChangedEvent
|
||||
switch e := event.(type) {
|
||||
case *org.GitLabSelfHostedIDPChangedEvent:
|
||||
idpEvent = e.GitLabSelfHostedIDPChangedEvent
|
||||
orgId = &idpEvent.Aggregate().ResourceOwner
|
||||
case *instance.GitLabSelfHostedIDPChangedEvent:
|
||||
idpEvent = e.GitLabSelfHostedIDPChangedEvent
|
||||
default:
|
||||
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-YAf3g2", "reduce.wrong.event.type %v", []eventstore.EventType{org.GitLabSelfHostedIDPChangedEventType, instance.GitLabSelfHostedIDPChangedEventType})
|
||||
}
|
||||
|
||||
return handler.NewStatement(event, func(ctx context.Context, ex handler.Executer, _ string) error {
|
||||
tx, ok := ex.(*sql.Tx)
|
||||
if !ok {
|
||||
return zerrors.ThrowInternal(nil, "HANDL-HX6ed", "unable to cast to tx executer")
|
||||
}
|
||||
idpRepo := repository.IDProviderRepository()
|
||||
gitlabSelfHosted, err := idpRepo.GetGitlabSelfHosted(ctx, v3_sql.SQLTx(tx), database.WithCondition(idpScopedCondition(idpRepo, idpEvent.Agg.InstanceID, idpEvent.ID, orgId)))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
changes := p.reduceIDPChangedTemplateColumns(idpRepo, idpEvent.Name, idpEvent.OptionChanges)
|
||||
|
||||
payload := &gitlabSelfHosted.GitlabSelfHosted
|
||||
payloadChanged := p.reduceGitLabSelfHostedIDPChangedColumns(payload, &idpEvent)
|
||||
if payloadChanged {
|
||||
payloadJSON, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
changes = append(changes, idpRepo.SetPayload(string(payloadJSON)))
|
||||
}
|
||||
|
||||
changes = append(changes, idpRepo.SetUpdatedAt(gu.Ptr(event.CreatedAt())))
|
||||
_, err = idpRepo.Update(ctx, v3_sql.SQLTx(tx), idpScopedCondition(idpRepo, idpEvent.Aggregate().InstanceID, idpEvent.ID, orgId), changes...)
|
||||
return err
|
||||
|
||||
}), nil
|
||||
}
|
||||
|
||||
func (p *relationalTablesProjection) reduceGitLabIDPChangedColumns(payload *domain.Gitlab, idpEvent *idp.GitLabIDPChangedEvent) bool {
|
||||
payloadChanged := false
|
||||
if payload == nil || idpEvent == nil {
|
||||
return payloadChanged
|
||||
}
|
||||
|
||||
if idpEvent.ClientID != nil && *idpEvent.ClientID != payload.ClientID {
|
||||
payloadChanged = true
|
||||
payload.ClientID = *idpEvent.ClientID
|
||||
}
|
||||
if idpEvent.ClientSecret != nil {
|
||||
payloadChanged = true
|
||||
payload.ClientSecret = idpEvent.ClientSecret
|
||||
}
|
||||
if idpEvent.Scopes != nil && !slices.Equal(idpEvent.Scopes, payload.Scopes) {
|
||||
payloadChanged = true
|
||||
payload.Scopes = idpEvent.Scopes
|
||||
}
|
||||
return payloadChanged
|
||||
}
|
||||
|
||||
func (p *relationalTablesProjection) reduceGitLabSelfHostedIDPChangedColumns(payload *domain.GitlabSelfHosted, idpEvent *idp.GitLabSelfHostedIDPChangedEvent) bool {
|
||||
payloadChanged := false
|
||||
if payload == nil || idpEvent == nil {
|
||||
return payloadChanged
|
||||
}
|
||||
|
||||
if idpEvent.ClientID != nil && *idpEvent.ClientID != payload.ClientID {
|
||||
payloadChanged = true
|
||||
payload.ClientID = *idpEvent.ClientID
|
||||
}
|
||||
if idpEvent.ClientSecret != nil {
|
||||
payloadChanged = true
|
||||
payload.ClientSecret = idpEvent.ClientSecret
|
||||
}
|
||||
if idpEvent.Issuer != nil && *idpEvent.Issuer != payload.Issuer {
|
||||
payloadChanged = true
|
||||
payload.Issuer = *idpEvent.Issuer
|
||||
}
|
||||
if idpEvent.Scopes != nil && !slices.Equal(idpEvent.Scopes, payload.Scopes) {
|
||||
payloadChanged = true
|
||||
payload.Scopes = idpEvent.Scopes
|
||||
}
|
||||
return payloadChanged
|
||||
}
|
||||
Reference in New Issue
Block a user