2021-02-08 10:30:30 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-02-18 13:48:27 +00:00
|
|
|
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/domain"
|
2021-02-08 10:30:30 +00:00
|
|
|
"github.com/caos/zitadel/internal/errors"
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/repository/user"
|
2021-02-08 10:30:30 +00:00
|
|
|
"github.com/caos/zitadel/internal/telemetry/tracing"
|
|
|
|
)
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
func (c *Commands) AddUserMachineKey(ctx context.Context, machineKey *domain.MachineKey, resourceOwner string) (*domain.MachineKey, error) {
|
|
|
|
err := c.checkUserExists(ctx, machineKey.AggregateID, resourceOwner)
|
2021-02-08 10:30:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-24 10:17:39 +00:00
|
|
|
keyID, err := c.idGenerator.Next()
|
2021-02-08 10:30:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
keyWriteModel := NewMachineKeyWriteModel(machineKey.AggregateID, keyID, resourceOwner)
|
2021-02-24 10:17:39 +00:00
|
|
|
err = c.eventstore.FilterToQueryReducer(ctx, keyWriteModel)
|
2021-02-08 10:30:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-02-22 11:27:47 +00:00
|
|
|
if err = domain.EnsureValidExpirationDate(machineKey); err != nil {
|
|
|
|
return nil, err
|
2021-02-08 10:30:30 +00:00
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
if err = domain.SetNewAuthNKeyPair(machineKey, c.machineKeySize); err != nil {
|
2021-02-18 13:48:27 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-08 10:30:30 +00:00
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
events, err := c.eventstore.PushEvents(ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
user.NewMachineKeyAddedEvent(
|
|
|
|
ctx,
|
|
|
|
UserAggregateFromWriteModel(&keyWriteModel.WriteModel),
|
|
|
|
keyID,
|
|
|
|
machineKey.Type,
|
|
|
|
machineKey.ExpirationDate,
|
|
|
|
machineKey.PublicKey))
|
2021-02-08 10:30:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-18 13:48:27 +00:00
|
|
|
err = AppendAndReduce(keyWriteModel, events...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-02-08 10:30:30 +00:00
|
|
|
key := keyWriteModelToMachineKey(keyWriteModel)
|
|
|
|
key.PrivateKey = machineKey.PrivateKey
|
|
|
|
return key, nil
|
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
func (c *Commands) RemoveUserMachineKey(ctx context.Context, userID, keyID, resourceOwner string) error {
|
|
|
|
keyWriteModel, err := c.machineKeyWriteModelByID(ctx, userID, keyID, resourceOwner)
|
2021-02-08 10:30:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-18 13:48:27 +00:00
|
|
|
if !keyWriteModel.Exists() {
|
2021-02-08 10:30:30 +00:00
|
|
|
return errors.ThrowNotFound(nil, "COMMAND-4m77G", "Errors.User.Machine.Key.NotFound")
|
|
|
|
}
|
2021-02-18 13:48:27 +00:00
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
_, err = c.eventstore.PushEvents(ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
user.NewMachineKeyRemovedEvent(ctx, UserAggregateFromWriteModel(&keyWriteModel.WriteModel), keyID))
|
|
|
|
return err
|
2021-02-08 10:30:30 +00:00
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
func (c *Commands) machineKeyWriteModelByID(ctx context.Context, userID, keyID, resourceOwner string) (writeModel *MachineKeyWriteModel, err error) {
|
2021-02-08 10:30:30 +00:00
|
|
|
if userID == "" {
|
|
|
|
return nil, errors.ThrowInvalidArgument(nil, "COMMAND-4n8vs", "Errors.User.UserIDMissing")
|
|
|
|
}
|
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
|
|
|
writeModel = NewMachineKeyWriteModel(userID, keyID, resourceOwner)
|
2021-02-24 10:17:39 +00:00
|
|
|
err = c.eventstore.FilterToQueryReducer(ctx, writeModel)
|
2021-02-08 10:30:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModel, nil
|
|
|
|
}
|