2022-02-16 15:49:17 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/caos/zitadel/internal/domain"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
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-24 16:21:34 +00:00
|
|
|
type InstanceSecretGeneratorConfigWriteModel struct {
|
2022-02-16 15:49:17 +00:00
|
|
|
eventstore.WriteModel
|
|
|
|
|
|
|
|
GeneratorType domain.SecretGeneratorType
|
|
|
|
Length uint
|
|
|
|
Expiry time.Duration
|
|
|
|
IncludeLowerLetters bool
|
|
|
|
IncludeUpperLetters bool
|
|
|
|
IncludeDigits bool
|
|
|
|
IncludeSymbols bool
|
|
|
|
State domain.SecretGeneratorState
|
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func NewInstanceSecretGeneratorConfigWriteModel(GeneratorType domain.SecretGeneratorType) *InstanceSecretGeneratorConfigWriteModel {
|
|
|
|
return &InstanceSecretGeneratorConfigWriteModel{
|
2022-02-16 15:49:17 +00:00
|
|
|
WriteModel: eventstore.WriteModel{
|
|
|
|
AggregateID: domain.IAMID,
|
|
|
|
ResourceOwner: domain.IAMID,
|
|
|
|
},
|
|
|
|
GeneratorType: GeneratorType,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func (wm *InstanceSecretGeneratorConfigWriteModel) Reduce() error {
|
2022-02-16 15:49:17 +00:00
|
|
|
for _, event := range wm.Events {
|
|
|
|
switch e := event.(type) {
|
2022-03-24 16:21:34 +00:00
|
|
|
case *instance.SecretGeneratorAddedEvent:
|
2022-02-16 15:49:17 +00:00
|
|
|
if wm.GeneratorType != e.GeneratorType {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.Length = e.Length
|
|
|
|
wm.Expiry = e.Expiry
|
|
|
|
wm.IncludeLowerLetters = e.IncludeLowerLetters
|
|
|
|
wm.IncludeUpperLetters = e.IncludeUpperLetters
|
|
|
|
wm.IncludeDigits = e.IncludeDigits
|
|
|
|
wm.IncludeSymbols = e.IncludeDigits
|
|
|
|
wm.State = domain.SecretGeneratorStateActive
|
2022-03-24 16:21:34 +00:00
|
|
|
case *instance.SecretGeneratorChangedEvent:
|
2022-02-16 15:49:17 +00:00
|
|
|
if wm.GeneratorType != e.GeneratorType {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if e.Length != nil {
|
|
|
|
wm.Length = *e.Length
|
|
|
|
}
|
|
|
|
if e.Expiry != nil {
|
|
|
|
wm.Expiry = *e.Expiry
|
|
|
|
}
|
|
|
|
if e.IncludeUpperLetters != nil {
|
|
|
|
wm.IncludeUpperLetters = *e.IncludeUpperLetters
|
|
|
|
}
|
|
|
|
if e.IncludeLowerLetters != nil {
|
|
|
|
wm.IncludeLowerLetters = *e.IncludeLowerLetters
|
|
|
|
}
|
|
|
|
if e.IncludeDigits != nil {
|
|
|
|
wm.IncludeDigits = *e.IncludeDigits
|
|
|
|
}
|
|
|
|
if e.IncludeSymbols != nil {
|
|
|
|
wm.IncludeSymbols = *e.IncludeSymbols
|
|
|
|
}
|
2022-03-24 16:21:34 +00:00
|
|
|
case *instance.SecretGeneratorRemovedEvent:
|
2022-02-16 15:49:17 +00:00
|
|
|
if wm.GeneratorType != e.GeneratorType {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.State = domain.SecretGeneratorStateRemoved
|
|
|
|
wm.Length = 0
|
|
|
|
wm.Expiry = 0
|
|
|
|
wm.IncludeLowerLetters = false
|
|
|
|
wm.IncludeUpperLetters = false
|
|
|
|
wm.IncludeDigits = false
|
|
|
|
wm.IncludeSymbols = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return wm.WriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func (wm *InstanceSecretGeneratorConfigWriteModel) Query() *eventstore.SearchQueryBuilder {
|
2022-02-16 15:49:17 +00:00
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
|
|
|
ResourceOwner(wm.ResourceOwner).
|
|
|
|
AddQuery().
|
2022-03-24 16:21:34 +00:00
|
|
|
AggregateTypes(instance.AggregateType).
|
2022-02-16 15:49:17 +00:00
|
|
|
AggregateIDs(wm.AggregateID).
|
|
|
|
EventTypes(
|
2022-03-24 16:21:34 +00:00
|
|
|
instance.SecretGeneratorAddedEventType,
|
|
|
|
instance.SecretGeneratorChangedEventType,
|
|
|
|
instance.SecretGeneratorRemovedEventType).
|
2022-02-16 15:49:17 +00:00
|
|
|
Builder()
|
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func (wm *InstanceSecretGeneratorConfigWriteModel) NewChangedEvent(
|
2022-02-16 15:49:17 +00:00
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
generatorType domain.SecretGeneratorType,
|
|
|
|
length uint,
|
|
|
|
expiry time.Duration,
|
|
|
|
includeLowerLetters,
|
|
|
|
includeUpperLetters,
|
|
|
|
includeDigits,
|
|
|
|
includeSymbols bool,
|
2022-03-24 16:21:34 +00:00
|
|
|
) (*instance.SecretGeneratorChangedEvent, bool, error) {
|
|
|
|
changes := make([]instance.SecretGeneratorChanges, 0)
|
2022-02-16 15:49:17 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
if wm.Length != length {
|
2022-03-24 16:21:34 +00:00
|
|
|
changes = append(changes, instance.ChangeSecretGeneratorLength(length))
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
|
|
|
if wm.Expiry != expiry {
|
2022-03-24 16:21:34 +00:00
|
|
|
changes = append(changes, instance.ChangeSecretGeneratorExpiry(expiry))
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
|
|
|
if wm.IncludeLowerLetters != includeLowerLetters {
|
2022-03-24 16:21:34 +00:00
|
|
|
changes = append(changes, instance.ChangeSecretGeneratorIncludeLowerLetters(includeLowerLetters))
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
|
|
|
if wm.IncludeUpperLetters != includeUpperLetters {
|
2022-03-24 16:21:34 +00:00
|
|
|
changes = append(changes, instance.ChangeSecretGeneratorIncludeUpperLetters(includeUpperLetters))
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
|
|
|
if wm.IncludeDigits != includeDigits {
|
2022-03-24 16:21:34 +00:00
|
|
|
changes = append(changes, instance.ChangeSecretGeneratorIncludeDigits(includeDigits))
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
|
|
|
if wm.IncludeSymbols != includeSymbols {
|
2022-03-24 16:21:34 +00:00
|
|
|
changes = append(changes, instance.ChangeSecretGeneratorIncludeSymbols(includeSymbols))
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
|
|
|
if len(changes) == 0 {
|
|
|
|
return nil, false, nil
|
|
|
|
}
|
2022-03-24 16:21:34 +00:00
|
|
|
changeEvent, err := instance.NewSecretGeneratorChangeEvent(ctx, aggregate, generatorType, changes)
|
2022-02-16 15:49:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
return changeEvent, true, nil
|
|
|
|
}
|