mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-11 19:07:30 +00:00
fix(import): add import for app and machine keys (#4536)
* fix(import): add import for app and machine keys * fix(export): add review changes * fix(import): Apply suggestions from code review Co-authored-by: Livio Spring <livio.a@gmail.com> * fix(import): add review changes Co-authored-by: Livio Spring <livio.a@gmail.com>
This commit is contained in:
@@ -9,34 +9,51 @@ import (
|
||||
"github.com/zitadel/zitadel/internal/telemetry/tracing"
|
||||
)
|
||||
|
||||
func (c *Commands) AddUserMachineKey(ctx context.Context, machineKey *domain.MachineKey, resourceOwner string) (*domain.MachineKey, error) {
|
||||
err := c.checkUserExists(ctx, machineKey.AggregateID, resourceOwner)
|
||||
func (c *Commands) AddUserMachineKeyWithID(ctx context.Context, machineKey *domain.MachineKey, resourceOwner string) (*domain.MachineKey, error) {
|
||||
writeModel, err := c.machineKeyWriteModelByID(ctx, machineKey.AggregateID, machineKey.KeyID, resourceOwner)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if writeModel.State != domain.MachineKeyStateUnspecified {
|
||||
return nil, errors.ThrowNotFound(nil, "COMMAND-p22101", "Errors.User.Machine.Key.AlreadyExisting")
|
||||
}
|
||||
return c.addUserMachineKey(ctx, machineKey, resourceOwner)
|
||||
}
|
||||
|
||||
func (c *Commands) AddUserMachineKey(ctx context.Context, machineKey *domain.MachineKey, resourceOwner string) (*domain.MachineKey, error) {
|
||||
keyID, err := c.idGenerator.Next()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
keyWriteModel := NewMachineKeyWriteModel(machineKey.AggregateID, keyID, resourceOwner)
|
||||
err = c.eventstore.FilterToQueryReducer(ctx, keyWriteModel)
|
||||
machineKey.KeyID = keyID
|
||||
return c.addUserMachineKey(ctx, machineKey, resourceOwner)
|
||||
}
|
||||
|
||||
func (c *Commands) addUserMachineKey(ctx context.Context, machineKey *domain.MachineKey, resourceOwner string) (*domain.MachineKey, error) {
|
||||
err := c.checkUserExists(ctx, machineKey.AggregateID, resourceOwner)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err = domain.EnsureValidExpirationDate(machineKey); err != nil {
|
||||
keyWriteModel := NewMachineKeyWriteModel(machineKey.AggregateID, machineKey.KeyID, resourceOwner)
|
||||
if err := c.eventstore.FilterToQueryReducer(ctx, keyWriteModel); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err = domain.SetNewAuthNKeyPair(machineKey, c.machineKeySize); err != nil {
|
||||
if err := domain.EnsureValidExpirationDate(machineKey); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(machineKey.PublicKey) == 0 {
|
||||
if err := domain.SetNewAuthNKeyPair(machineKey, c.machineKeySize); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
events, err := c.eventstore.Push(ctx,
|
||||
user.NewMachineKeyAddedEvent(
|
||||
ctx,
|
||||
UserAggregateFromWriteModel(&keyWriteModel.WriteModel),
|
||||
keyID,
|
||||
machineKey.KeyID,
|
||||
machineKey.Type,
|
||||
machineKey.ExpirationDate,
|
||||
machineKey.PublicKey))
|
||||
@@ -49,7 +66,9 @@ func (c *Commands) AddUserMachineKey(ctx context.Context, machineKey *domain.Mac
|
||||
}
|
||||
|
||||
key := keyWriteModelToMachineKey(keyWriteModel)
|
||||
key.PrivateKey = machineKey.PrivateKey
|
||||
if len(machineKey.PrivateKey) > 0 {
|
||||
key.PrivateKey = machineKey.PrivateKey
|
||||
}
|
||||
return key, nil
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user