2022-02-16 15:49:17 +00:00
|
|
|
package projection
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/caos/zitadel/internal/errors"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore/handler"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore/handler/crdb"
|
2022-03-24 16:21:34 +00:00
|
|
|
"github.com/caos/zitadel/internal/repository/instance"
|
2022-02-16 15:49:17 +00:00
|
|
|
)
|
|
|
|
|
2022-03-23 08:02:39 +00:00
|
|
|
const (
|
|
|
|
SecretGeneratorProjectionTable = "projections.secret_generators"
|
|
|
|
|
|
|
|
SecretGeneratorColumnGeneratorType = "generator_type"
|
|
|
|
SecretGeneratorColumnAggregateID = "aggregate_id"
|
|
|
|
SecretGeneratorColumnCreationDate = "creation_date"
|
|
|
|
SecretGeneratorColumnChangeDate = "change_date"
|
|
|
|
SecretGeneratorColumnSequence = "sequence"
|
|
|
|
SecretGeneratorColumnResourceOwner = "resource_owner"
|
|
|
|
SecretGeneratorColumnInstanceID = "instance_id"
|
|
|
|
SecretGeneratorColumnLength = "length"
|
|
|
|
SecretGeneratorColumnExpiry = "expiry"
|
|
|
|
SecretGeneratorColumnIncludeLowerLetters = "include_lower_letters"
|
|
|
|
SecretGeneratorColumnIncludeUpperLetters = "include_upper_letters"
|
|
|
|
SecretGeneratorColumnIncludeDigits = "include_digits"
|
|
|
|
SecretGeneratorColumnIncludeSymbols = "include_symbols"
|
|
|
|
)
|
|
|
|
|
2022-02-16 15:49:17 +00:00
|
|
|
type SecretGeneratorProjection struct {
|
|
|
|
crdb.StatementHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSecretGeneratorProjection(ctx context.Context, config crdb.StatementHandlerConfig) *SecretGeneratorProjection {
|
2022-02-25 15:05:06 +00:00
|
|
|
p := new(SecretGeneratorProjection)
|
2022-02-16 15:49:17 +00:00
|
|
|
config.ProjectionName = SecretGeneratorProjectionTable
|
|
|
|
config.Reducers = p.reducers()
|
2022-03-23 08:02:39 +00:00
|
|
|
config.InitCheck = crdb.NewTableCheck(
|
|
|
|
crdb.NewTable([]*crdb.Column{
|
|
|
|
crdb.NewColumn(SecretGeneratorColumnGeneratorType, crdb.ColumnTypeText),
|
|
|
|
crdb.NewColumn(SecretGeneratorColumnAggregateID, crdb.ColumnTypeText),
|
|
|
|
crdb.NewColumn(SecretGeneratorColumnCreationDate, crdb.ColumnTypeTimestamp),
|
|
|
|
crdb.NewColumn(SecretGeneratorColumnChangeDate, crdb.ColumnTypeTimestamp),
|
|
|
|
crdb.NewColumn(SecretGeneratorColumnSequence, crdb.ColumnTypeInt64),
|
|
|
|
crdb.NewColumn(SecretGeneratorColumnResourceOwner, crdb.ColumnTypeText),
|
|
|
|
crdb.NewColumn(SecretGeneratorColumnInstanceID, crdb.ColumnTypeText),
|
|
|
|
crdb.NewColumn(SecretGeneratorColumnLength, crdb.ColumnTypeInt64),
|
|
|
|
crdb.NewColumn(SecretGeneratorColumnExpiry, crdb.ColumnTypeInt64),
|
|
|
|
crdb.NewColumn(SecretGeneratorColumnIncludeLowerLetters, crdb.ColumnTypeBool),
|
|
|
|
crdb.NewColumn(SecretGeneratorColumnIncludeUpperLetters, crdb.ColumnTypeBool),
|
|
|
|
crdb.NewColumn(SecretGeneratorColumnIncludeDigits, crdb.ColumnTypeBool),
|
|
|
|
crdb.NewColumn(SecretGeneratorColumnIncludeSymbols, crdb.ColumnTypeBool),
|
|
|
|
},
|
|
|
|
crdb.NewPrimaryKey(SecretGeneratorColumnInstanceID, SecretGeneratorColumnGeneratorType, SecretGeneratorColumnAggregateID),
|
|
|
|
),
|
|
|
|
)
|
2022-02-16 15:49:17 +00:00
|
|
|
p.StatementHandler = crdb.NewStatementHandler(ctx, config)
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *SecretGeneratorProjection) reducers() []handler.AggregateReducer {
|
|
|
|
return []handler.AggregateReducer{
|
|
|
|
{
|
2022-04-12 14:20:17 +00:00
|
|
|
Aggregate: instance.AggregateType,
|
2022-02-16 15:49:17 +00:00
|
|
|
EventRedusers: []handler.EventReducer{
|
|
|
|
{
|
2022-03-24 16:21:34 +00:00
|
|
|
Event: instance.SecretGeneratorAddedEventType,
|
2022-02-16 15:49:17 +00:00
|
|
|
Reduce: p.reduceSecretGeneratorAdded,
|
|
|
|
},
|
|
|
|
{
|
2022-03-24 16:21:34 +00:00
|
|
|
Event: instance.SecretGeneratorChangedEventType,
|
2022-02-16 15:49:17 +00:00
|
|
|
Reduce: p.reduceSecretGeneratorChanged,
|
|
|
|
},
|
|
|
|
{
|
2022-03-24 16:21:34 +00:00
|
|
|
Event: instance.SecretGeneratorRemovedEventType,
|
2022-02-16 15:49:17 +00:00
|
|
|
Reduce: p.reduceSecretGeneratorRemoved,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *SecretGeneratorProjection) reduceSecretGeneratorAdded(event eventstore.Event) (*handler.Statement, error) {
|
2022-03-24 16:21:34 +00:00
|
|
|
e, ok := event.(*instance.SecretGeneratorAddedEvent)
|
2022-02-16 15:49:17 +00:00
|
|
|
if !ok {
|
2022-03-24 16:21:34 +00:00
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-sk99F", "reduce.wrong.event.type %s", instance.SecretGeneratorAddedEventType)
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
|
|
|
return crdb.NewCreateStatement(
|
|
|
|
e,
|
|
|
|
[]handler.Column{
|
|
|
|
handler.NewCol(SecretGeneratorColumnAggregateID, e.Aggregate().ID),
|
|
|
|
handler.NewCol(SecretGeneratorColumnGeneratorType, e.GeneratorType),
|
|
|
|
handler.NewCol(SecretGeneratorColumnCreationDate, e.CreationDate()),
|
|
|
|
handler.NewCol(SecretGeneratorColumnChangeDate, e.CreationDate()),
|
|
|
|
handler.NewCol(SecretGeneratorColumnResourceOwner, e.Aggregate().ResourceOwner),
|
2022-03-23 08:02:39 +00:00
|
|
|
handler.NewCol(SecretGeneratorColumnInstanceID, e.Aggregate().InstanceID),
|
2022-02-16 15:49:17 +00:00
|
|
|
handler.NewCol(SecretGeneratorColumnSequence, e.Sequence()),
|
|
|
|
handler.NewCol(SecretGeneratorColumnLength, e.Length),
|
|
|
|
handler.NewCol(SecretGeneratorColumnExpiry, e.Expiry),
|
|
|
|
handler.NewCol(SecretGeneratorColumnIncludeLowerLetters, e.IncludeLowerLetters),
|
|
|
|
handler.NewCol(SecretGeneratorColumnIncludeUpperLetters, e.IncludeUpperLetters),
|
|
|
|
handler.NewCol(SecretGeneratorColumnIncludeDigits, e.IncludeDigits),
|
|
|
|
handler.NewCol(SecretGeneratorColumnIncludeSymbols, e.IncludeSymbols),
|
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *SecretGeneratorProjection) reduceSecretGeneratorChanged(event eventstore.Event) (*handler.Statement, error) {
|
2022-03-24 16:21:34 +00:00
|
|
|
e, ok := event.(*instance.SecretGeneratorChangedEvent)
|
2022-02-16 15:49:17 +00:00
|
|
|
if !ok {
|
2022-03-24 16:21:34 +00:00
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-s00Fs", "reduce.wrong.event.type %s", instance.SecretGeneratorChangedEventType)
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
columns := make([]handler.Column, 0, 7)
|
|
|
|
columns = append(columns, handler.NewCol(SecretGeneratorColumnChangeDate, e.CreationDate()),
|
|
|
|
handler.NewCol(SecretGeneratorColumnSequence, e.Sequence()))
|
|
|
|
if e.Length != nil {
|
|
|
|
columns = append(columns, handler.NewCol(SecretGeneratorColumnLength, *e.Length))
|
|
|
|
}
|
|
|
|
if e.Expiry != nil {
|
|
|
|
columns = append(columns, handler.NewCol(SecretGeneratorColumnExpiry, *e.Expiry))
|
|
|
|
}
|
|
|
|
if e.IncludeLowerLetters != nil {
|
|
|
|
columns = append(columns, handler.NewCol(SecretGeneratorColumnIncludeLowerLetters, *e.IncludeLowerLetters))
|
|
|
|
}
|
|
|
|
if e.IncludeUpperLetters != nil {
|
|
|
|
columns = append(columns, handler.NewCol(SecretGeneratorColumnIncludeUpperLetters, *e.IncludeUpperLetters))
|
|
|
|
}
|
|
|
|
if e.IncludeDigits != nil {
|
|
|
|
columns = append(columns, handler.NewCol(SecretGeneratorColumnIncludeDigits, *e.IncludeDigits))
|
|
|
|
}
|
|
|
|
if e.IncludeSymbols != nil {
|
|
|
|
columns = append(columns, handler.NewCol(SecretGeneratorColumnIncludeSymbols, *e.IncludeSymbols))
|
|
|
|
}
|
|
|
|
return crdb.NewUpdateStatement(
|
|
|
|
e,
|
|
|
|
columns,
|
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(SecretGeneratorColumnAggregateID, e.Aggregate().ID),
|
|
|
|
handler.NewCond(SecretGeneratorColumnGeneratorType, e.GeneratorType),
|
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *SecretGeneratorProjection) reduceSecretGeneratorRemoved(event eventstore.Event) (*handler.Statement, error) {
|
2022-03-24 16:21:34 +00:00
|
|
|
e, ok := event.(*instance.SecretGeneratorRemovedEvent)
|
2022-02-16 15:49:17 +00:00
|
|
|
if !ok {
|
2022-03-24 16:21:34 +00:00
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-fmiIf", "reduce.wrong.event.type %s", instance.SecretGeneratorRemovedEventType)
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
|
|
|
return crdb.NewDeleteStatement(
|
|
|
|
e,
|
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(SecretGeneratorColumnAggregateID, e.Aggregate().ID),
|
|
|
|
handler.NewCond(SecretGeneratorColumnGeneratorType, e.GeneratorType),
|
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|