291 lines
9.5 KiB
Go
Raw Normal View History

2023-06-21 11:40:16 +02:00
package projection
import (
"context"
"github.com/zitadel/zitadel/internal/eventstore"
"github.com/zitadel/zitadel/internal/eventstore/handler"
"github.com/zitadel/zitadel/internal/eventstore/handler/crdb"
"github.com/zitadel/zitadel/internal/repository/instance"
2023-06-30 13:43:26 +02:00
"github.com/zitadel/zitadel/internal/repository/milestone"
2023-06-28 08:19:34 +02:00
"github.com/zitadel/zitadel/internal/repository/project"
"github.com/zitadel/zitadel/internal/repository/user"
2023-06-21 11:40:16 +02:00
)
const (
MilestonesProjectionTable = "projections.milestones"
2023-06-29 18:43:02 +02:00
MilestoneColumnInstanceID = "instance_id"
MilestoneColumnType = "type"
MilestoneColumnPrimaryDomain = "primary_domain"
MilestoneColumnReachedDate = "reached_date"
MilestoneColumnPushedDate = "last_pushed_date"
MilestoneColumnIgnoreClientIDs = "ignore_client_ids"
2023-06-21 11:40:16 +02:00
)
type milestoneProjection struct {
crdb.StatementHandler
}
2023-06-28 08:19:34 +02:00
func newMilestoneProjection(ctx context.Context, config crdb.StatementHandlerConfig) *milestoneProjection {
2023-06-21 11:40:16 +02:00
p := new(milestoneProjection)
config.ProjectionName = MilestonesProjectionTable
config.Reducers = p.reducers()
config.InitCheck = crdb.NewMultiTableCheck(
crdb.NewTable([]*crdb.Column{
crdb.NewColumn(MilestoneColumnInstanceID, crdb.ColumnTypeText),
2023-06-28 11:35:22 +02:00
crdb.NewColumn(MilestoneColumnType, crdb.ColumnTypeEnum),
2023-06-28 08:19:34 +02:00
crdb.NewColumn(MilestoneColumnReachedDate, crdb.ColumnTypeTimestamp, crdb.Nullable()),
crdb.NewColumn(MilestoneColumnPushedDate, crdb.ColumnTypeTimestamp, crdb.Nullable()),
2023-06-21 11:40:16 +02:00
crdb.NewColumn(MilestoneColumnPrimaryDomain, crdb.ColumnTypeText, crdb.Nullable()),
2023-06-29 18:43:02 +02:00
crdb.NewColumn(MilestoneColumnIgnoreClientIDs, crdb.ColumnTypeTextArray, crdb.Nullable()),
2023-06-21 11:40:16 +02:00
},
2023-06-28 11:35:22 +02:00
crdb.NewPrimaryKey(MilestoneColumnInstanceID, MilestoneColumnType),
2023-06-21 11:40:16 +02:00
),
)
p.StatementHandler = crdb.NewStatementHandler(ctx, config)
return p
}
func (p *milestoneProjection) reducers() []handler.AggregateReducer {
return []handler.AggregateReducer{
{
Aggregate: instance.AggregateType,
EventRedusers: []handler.EventReducer{
{
Event: instance.InstanceAddedEventType,
Reduce: p.reduceInstanceAdded,
},
{
Event: instance.InstanceDomainPrimarySetEventType,
Reduce: p.reduceInstanceDomainPrimarySet,
},
{
Event: instance.InstanceRemovedEventType,
2023-06-30 16:23:39 +02:00
Reduce: p.reduceInstanceRemoved,
2023-06-21 11:40:16 +02:00
},
},
},
{
Aggregate: project.AggregateType,
EventRedusers: []handler.EventReducer{
{
Event: project.ProjectAddedType,
2023-06-30 16:23:39 +02:00
Reduce: p.reduceProjectAdded,
2023-06-21 11:40:16 +02:00
},
{
Event: project.ApplicationAddedType,
2023-06-30 16:23:39 +02:00
Reduce: p.reduceApplicationAdded,
2023-06-29 18:43:02 +02:00
},
{
Event: project.OIDCConfigAddedType,
Reduce: p.reduceOIDCConfigAdded,
},
{
Event: project.APIConfigAddedType,
Reduce: p.reduceAPIConfigAdded,
2023-06-21 11:40:16 +02:00
},
},
},
{
Aggregate: user.AggregateType,
EventRedusers: []handler.EventReducer{
{
2023-06-29 18:43:02 +02:00
// user.UserTokenAddedType is not emitted on creation of personal access tokens
// PATs have no effect on milestone.AuthenticationSucceededOnApplication or milestone.AuthenticationSucceededOnInstance
2023-06-21 11:40:16 +02:00
Event: user.UserTokenAddedType,
Reduce: p.reduceUserTokenAdded,
},
},
},
2023-06-28 11:35:22 +02:00
{
Aggregate: milestone.AggregateType,
EventRedusers: []handler.EventReducer{
{
Event: milestone.PushedEventType,
2023-06-30 16:23:39 +02:00
Reduce: p.reduceMilestonePushed,
2023-06-28 11:35:22 +02:00
},
},
},
2023-06-21 11:40:16 +02:00
}
}
2023-06-28 08:19:34 +02:00
func (p *milestoneProjection) reduceInstanceAdded(event eventstore.Event) (*handler.Statement, error) {
2023-06-30 16:23:39 +02:00
e, err := assertEvent[*instance.InstanceAddedEvent](event)
if err != nil {
return nil, err
2023-06-28 08:19:34 +02:00
}
2023-06-28 11:35:22 +02:00
allTypes := milestone.AllTypes()
2023-06-28 08:19:34 +02:00
statements := make([]func(eventstore.Event) crdb.Exec, 0, len(allTypes))
2023-06-28 11:35:22 +02:00
for _, msType := range allTypes {
2023-06-28 08:19:34 +02:00
createColumns := []handler.Column{
handler.NewCol(MilestoneColumnInstanceID, e.Aggregate().InstanceID),
2023-06-28 11:35:22 +02:00
handler.NewCol(MilestoneColumnType, msType),
2023-06-28 08:19:34 +02:00
}
2023-06-28 11:35:22 +02:00
if msType == milestone.InstanceCreated {
2023-06-28 08:19:34 +02:00
createColumns = append(createColumns, handler.NewCol(MilestoneColumnReachedDate, event.CreationDate()))
}
statements = append(statements, crdb.AddCreateStatement(createColumns))
}
return crdb.NewMultiStatement(e, statements...), nil
}
2023-06-21 11:40:16 +02:00
func (p *milestoneProjection) reduceInstanceDomainPrimarySet(event eventstore.Event) (*handler.Statement, error) {
2023-06-30 16:23:39 +02:00
e, err := assertEvent[*instance.DomainPrimarySetEvent](event)
if err != nil {
return nil, err
2023-06-21 11:40:16 +02:00
}
2023-06-29 12:18:16 +02:00
return crdb.NewUpdateStatement(
e,
[]handler.Column{
handler.NewCol(MilestoneColumnPrimaryDomain, e.Domain),
},
[]handler.Condition{
handler.NewCond(MilestoneColumnInstanceID, e.Aggregate().InstanceID),
crdb.NewIsNullCond(MilestoneColumnPushedDate),
},
), nil
2023-06-21 11:40:16 +02:00
}
2023-06-30 16:23:39 +02:00
func (p *milestoneProjection) reduceProjectAdded(event eventstore.Event) (*handler.Statement, error) {
if _, err := assertEvent[*project.ProjectAddedEvent](event); err != nil {
return nil, err
2023-06-29 18:43:02 +02:00
}
2023-06-30 16:23:39 +02:00
return p.reduceReachedIfUserEventFunc(milestone.ProjectCreated)(event)
2023-06-29 18:43:02 +02:00
}
2023-06-30 16:23:39 +02:00
func (p *milestoneProjection) reduceApplicationAdded(event eventstore.Event) (*handler.Statement, error) {
if _, err := assertEvent[*project.ApplicationAddedEvent](event); err != nil {
return nil, err
}
return p.reduceReachedIfUserEventFunc(milestone.ApplicationCreated)(event)
}
func (p *milestoneProjection) reduceOIDCConfigAdded(event eventstore.Event) (*handler.Statement, error) {
e, err := assertEvent[*project.OIDCConfigAddedEvent](event)
if err != nil {
return nil, err
2023-06-29 18:43:02 +02:00
}
return p.reduceAppConfigAdded(e, e.ClientID)
}
2023-06-30 16:23:39 +02:00
func (p *milestoneProjection) reduceAPIConfigAdded(event eventstore.Event) (*handler.Statement, error) {
e, err := assertEvent[*project.APIConfigAddedEvent](event)
if err != nil {
return nil, err
2023-06-28 11:35:22 +02:00
}
2023-06-30 16:23:39 +02:00
return p.reduceAppConfigAdded(e, e.ClientID)
2023-06-21 11:40:16 +02:00
}
2023-06-28 11:35:22 +02:00
func (p *milestoneProjection) reduceUserTokenAdded(event eventstore.Event) (*handler.Statement, error) {
2023-06-30 16:23:39 +02:00
e, err := assertEvent[*user.UserTokenAddedEvent](event)
if err != nil {
return nil, err
}
if p.isSystemEvent(event) {
return crdb.NewNoOpStatement(event), nil
2023-06-29 12:18:16 +02:00
}
2023-06-29 18:43:02 +02:00
statements := []func(eventstore.Event) crdb.Exec{
2023-06-29 12:18:16 +02:00
crdb.AddUpdateStatement(
[]handler.Column{
handler.NewCol(MilestoneColumnReachedDate, event.CreationDate()),
},
[]handler.Condition{
handler.NewCond(MilestoneColumnInstanceID, event.Aggregate().InstanceID),
handler.NewCond(MilestoneColumnType, milestone.AuthenticationSucceededOnInstance),
crdb.NewIsNullCond(MilestoneColumnReachedDate),
},
),
2023-06-29 18:43:02 +02:00
}
// We ignore authentications without app, for example JWT profile or PAT
if e.ApplicationID != "" {
2023-06-30 02:11:17 +02:00
statements = append(statements, crdb.AddUpdateStatement(
2023-06-29 14:28:27 +02:00
[]handler.Column{
handler.NewCol(MilestoneColumnReachedDate, event.CreationDate()),
},
[]handler.Condition{
handler.NewCond(MilestoneColumnInstanceID, event.Aggregate().InstanceID),
handler.NewCond(MilestoneColumnType, milestone.AuthenticationSucceededOnApplication),
2023-06-30 02:11:17 +02:00
crdb.Not(crdb.NewTextArrayContainsCond(MilestoneColumnIgnoreClientIDs, e.ApplicationID)),
2023-06-29 14:28:27 +02:00
crdb.NewIsNullCond(MilestoneColumnReachedDate),
},
2023-06-30 02:11:17 +02:00
))
2023-06-29 18:43:02 +02:00
}
return crdb.NewMultiStatement(e, statements...), nil
2023-06-21 11:40:16 +02:00
}
2023-06-30 16:23:39 +02:00
func (p *milestoneProjection) reduceInstanceRemoved(event eventstore.Event) (*handler.Statement, error) {
if _, err := assertEvent[*instance.InstanceRemovedEvent](event); err != nil {
return nil, err
}
return p.reduceReachedFunc(milestone.InstanceDeleted)(event)
}
func (p *milestoneProjection) reduceMilestonePushed(event eventstore.Event) (*handler.Statement, error) {
e, err := assertEvent[*milestone.PushedEvent](event)
if err != nil {
return nil, err
}
if e.MilestoneType != milestone.InstanceDeleted {
return crdb.NewUpdateStatement(
event,
[]handler.Column{
handler.NewCol(MilestoneColumnPushedDate, event.CreationDate()),
},
[]handler.Condition{
handler.NewCond(MilestoneColumnInstanceID, event.Aggregate().InstanceID),
handler.NewCond(MilestoneColumnType, e.MilestoneType),
},
), nil
}
return crdb.NewDeleteStatement(
event,
[]handler.Condition{
handler.NewCond(MilestoneColumnInstanceID, event.Aggregate().InstanceID),
},
), nil
}
func (p *milestoneProjection) reduceReachedIfUserEventFunc(msType milestone.Type) func(event eventstore.Event) (*handler.Statement, error) {
return func(event eventstore.Event) (*handler.Statement, error) {
if p.isSystemEvent(event) {
return crdb.NewNoOpStatement(event), nil
}
return p.reduceReachedFunc(msType)(event)
}
}
func (p *milestoneProjection) reduceReachedFunc(msType milestone.Type) func(event eventstore.Event) (*handler.Statement, error) {
return func(event eventstore.Event) (*handler.Statement, error) {
return crdb.NewUpdateStatement(event, []handler.Column{
handler.NewCol(MilestoneColumnReachedDate, event.CreationDate()),
},
[]handler.Condition{
handler.NewCond(MilestoneColumnInstanceID, event.Aggregate().InstanceID),
handler.NewCond(MilestoneColumnType, msType),
crdb.NewIsNullCond(MilestoneColumnReachedDate),
}), nil
}
}
func (p *milestoneProjection) reduceAppConfigAdded(event eventstore.Event, clientID string) (*handler.Statement, error) {
if !p.isSystemEvent(event) {
return crdb.NewNoOpStatement(event), nil
}
return crdb.NewUpdateStatement(
event,
[]handler.Column{
crdb.NewArrayAppendCol(MilestoneColumnIgnoreClientIDs, clientID),
},
[]handler.Condition{
handler.NewCond(MilestoneColumnInstanceID, event.Aggregate().InstanceID),
handler.NewCond(MilestoneColumnType, milestone.AuthenticationSucceededOnApplication),
crdb.NewIsNullCond(MilestoneColumnReachedDate),
},
), nil
}
2023-06-29 18:43:02 +02:00
func (p *milestoneProjection) isSystemEvent(event eventstore.Event) bool {
return event.EditorUser() == "" || event.EditorService() == "" || event.EditorService() == "SYSTEM"
2023-06-21 11:40:16 +02:00
}