2022-02-16 15:49:17 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
|
|
|
"github.com/zitadel/zitadel/internal/command/preparation"
|
|
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2022-02-16 15:49:17 +00:00
|
|
|
)
|
|
|
|
|
2022-04-20 14:59:37 +00:00
|
|
|
func (c *Commands) AddSecretGeneratorConfig(ctx context.Context, typ domain.SecretGeneratorType, config *crypto.GeneratorConfig) (*domain.ObjectDetails, error) {
|
|
|
|
agg := instance.NewAggregate(authz.GetInstance(ctx).InstanceID())
|
2022-05-24 09:28:17 +00:00
|
|
|
cmds, err := preparation.PrepareCommands(ctx, c.eventstore.Filter, prepareAddSecretGeneratorConfig(agg, typ, config))
|
2022-02-16 15:49:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-04-20 14:59:37 +00:00
|
|
|
|
|
|
|
events, err := c.eventstore.Push(ctx, cmds...)
|
2022-02-16 15:49:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-04-20 14:59:37 +00:00
|
|
|
return &domain.ObjectDetails{
|
|
|
|
Sequence: events[len(events)-1].Sequence(),
|
2023-10-19 10:19:10 +00:00
|
|
|
EventDate: events[len(events)-1].CreatedAt(),
|
2022-04-20 14:59:37 +00:00
|
|
|
ResourceOwner: agg.ResourceOwner,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-05-24 09:28:17 +00:00
|
|
|
func prepareAddSecretGeneratorConfig(a *instance.Aggregate, typ domain.SecretGeneratorType, config *crypto.GeneratorConfig) preparation.Validation {
|
2022-04-20 14:59:37 +00:00
|
|
|
return func() (preparation.CreateCommands, error) {
|
|
|
|
if !typ.Valid() {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "V2-FGqVj", "Errors.InvalidArgument")
|
2022-04-20 14:59:37 +00:00
|
|
|
}
|
|
|
|
if config.Length < 1 {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "V2-jEqCt", "Errors.InvalidArgument")
|
2022-04-20 14:59:37 +00:00
|
|
|
}
|
|
|
|
return func(ctx context.Context, filter preparation.FilterToQueryReducer) ([]eventstore.Command, error) {
|
|
|
|
writeModel := NewInstanceSecretGeneratorConfigWriteModel(ctx, typ)
|
|
|
|
events, err := filter(ctx, writeModel.Query())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
writeModel.AppendEvents(events...)
|
|
|
|
if err = writeModel.Reduce(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if writeModel.State == domain.SecretGeneratorStateActive {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowAlreadyExists(nil, "V2-6CqKo", "Errors.SecretGenerator.AlreadyExists")
|
2022-04-20 14:59:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return []eventstore.Command{
|
|
|
|
instance.NewSecretGeneratorAddedEvent(
|
|
|
|
ctx,
|
|
|
|
&a.Aggregate,
|
|
|
|
typ,
|
|
|
|
config.Length,
|
|
|
|
config.Expiry,
|
|
|
|
config.IncludeLowerLetters,
|
|
|
|
config.IncludeUpperLetters,
|
|
|
|
config.IncludeDigits,
|
|
|
|
config.IncludeSymbols,
|
|
|
|
),
|
|
|
|
}, nil
|
|
|
|
}, nil
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Commands) ChangeSecretGeneratorConfig(ctx context.Context, generatorType domain.SecretGeneratorType, config *crypto.GeneratorConfig) (*domain.ObjectDetails, error) {
|
|
|
|
if generatorType == domain.SecretGeneratorTypeUnspecified {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "COMMAND-33k9f", "Errors.SecretGenerator.TypeMissing")
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
generatorWriteModel, err := c.getSecretConfig(ctx, generatorType)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-07-26 11:00:41 +00:00
|
|
|
instanceAgg := InstanceAggregateFromWriteModel(&generatorWriteModel.WriteModel)
|
2022-02-16 15:49:17 +00:00
|
|
|
if generatorWriteModel.State == domain.SecretGeneratorStateUnspecified || generatorWriteModel.State == domain.SecretGeneratorStateRemoved {
|
2023-07-26 11:00:41 +00:00
|
|
|
err = c.pushAppendAndReduce(ctx, generatorWriteModel,
|
|
|
|
instance.NewSecretGeneratorAddedEvent(
|
|
|
|
ctx,
|
|
|
|
instanceAgg,
|
|
|
|
generatorType,
|
|
|
|
config.Length,
|
|
|
|
config.Expiry,
|
|
|
|
config.IncludeLowerLetters,
|
|
|
|
config.IncludeUpperLetters,
|
|
|
|
config.IncludeDigits,
|
|
|
|
config.IncludeSymbols,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModelToObjectDetails(&generatorWriteModel.WriteModel), nil
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
changedEvent, hasChanged, err := generatorWriteModel.NewChangedEvent(
|
|
|
|
ctx,
|
2022-03-24 16:21:34 +00:00
|
|
|
instanceAgg,
|
2022-02-16 15:49:17 +00:00
|
|
|
generatorType,
|
|
|
|
config.Length,
|
|
|
|
config.Expiry,
|
|
|
|
config.IncludeLowerLetters,
|
|
|
|
config.IncludeUpperLetters,
|
|
|
|
config.IncludeDigits,
|
|
|
|
config.IncludeSymbols)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !hasChanged {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowPreconditionFailed(nil, "COMMAND-m0o3f", "Errors.NoChangesFound")
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
2023-07-26 11:00:41 +00:00
|
|
|
if err = c.pushAppendAndReduce(ctx, generatorWriteModel, changedEvent); err != nil {
|
2022-02-16 15:49:17 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModelToObjectDetails(&generatorWriteModel.WriteModel), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Commands) RemoveSecretGeneratorConfig(ctx context.Context, generatorType domain.SecretGeneratorType) (*domain.ObjectDetails, error) {
|
|
|
|
if generatorType == domain.SecretGeneratorTypeUnspecified {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "COMMAND-2j9lw", "Errors.SecretGenerator.TypeMissing")
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
generatorWriteModel, err := c.getSecretConfig(ctx, generatorType)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if generatorWriteModel.State == domain.SecretGeneratorStateUnspecified || generatorWriteModel.State == domain.SecretGeneratorStateRemoved {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowNotFound(nil, "COMMAND-b8les", "Errors.SecretGenerator.NotFound")
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
2022-03-24 16:21:34 +00:00
|
|
|
instanceAgg := InstanceAggregateFromWriteModel(&generatorWriteModel.WriteModel)
|
|
|
|
pushedEvents, err := c.eventstore.Push(ctx, instance.NewSecretGeneratorRemovedEvent(ctx, instanceAgg, generatorType))
|
2022-02-16 15:49:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = AppendAndReduce(generatorWriteModel, pushedEvents...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModelToObjectDetails(&generatorWriteModel.WriteModel), nil
|
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func (c *Commands) getSecretConfig(ctx context.Context, generatorType domain.SecretGeneratorType) (_ *InstanceSecretGeneratorConfigWriteModel, err error) {
|
2022-04-05 05:58:09 +00:00
|
|
|
writeModel := NewInstanceSecretGeneratorConfigWriteModel(ctx, generatorType)
|
2022-02-16 15:49:17 +00:00
|
|
|
err = c.eventstore.FilterToQueryReducer(ctx, writeModel)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return writeModel, nil
|
|
|
|
}
|