161 lines
5.6 KiB
C#
161 lines
5.6 KiB
C#
using Core.Blueprint.Application.UsesCases.KeyVault.Input;
|
|
using Core.Blueprint.Application.UsesCases.KeyVault.Ports;
|
|
using Core.Blueprint.KeyVault;
|
|
using Core.Blueprint.Service.External.Clients;
|
|
using FluentValidation;
|
|
using Lib.Architecture.BuildingBlocks;
|
|
using Lib.Architecture.BuildingBlocks.Helpers;
|
|
|
|
namespace Core.Cerberos.Application.UseCases.KeyVault
|
|
{
|
|
public class KeyVaultHandler :
|
|
IComponentHandler<CreateSecretRequest>,
|
|
IComponentHandler<GetSecretRequest>,
|
|
IComponentHandler<UpdateSecretRequest>,
|
|
IComponentHandler<DeleteSecretRequest>
|
|
{
|
|
private readonly IKeyVaultPort _port;
|
|
private readonly IValidator<CreateSecretRequest> _createSecretValidator;
|
|
private readonly IValidator<GetSecretRequest> _getSecretValidator;
|
|
private readonly IValidator<UpdateSecretRequest> _updateSecretValidator;
|
|
private readonly IValidator<DeleteSecretRequest> _deleteSecretValidator;
|
|
private readonly IBlueprintServiceClient _keyVaultDALService;
|
|
|
|
public KeyVaultHandler(
|
|
IKeyVaultPort port,
|
|
IValidator<CreateSecretRequest> createSecretValidator,
|
|
IValidator<GetSecretRequest> getSecretValidator,
|
|
IValidator<UpdateSecretRequest> updateSecretValidator,
|
|
IValidator<DeleteSecretRequest> deleteSecretValidator,
|
|
IBlueprintServiceClient keyVaultDALService)
|
|
{
|
|
_port = port ?? throw new ArgumentNullException(nameof(port));
|
|
_createSecretValidator = createSecretValidator ?? throw new ArgumentNullException(nameof(createSecretValidator));
|
|
_getSecretValidator = getSecretValidator ?? throw new ArgumentNullException(nameof(getSecretValidator));
|
|
_updateSecretValidator = updateSecretValidator ?? throw new ArgumentNullException(nameof(updateSecretValidator));
|
|
_deleteSecretValidator = deleteSecretValidator ?? throw new ArgumentNullException(nameof(deleteSecretValidator));
|
|
_keyVaultDALService = keyVaultDALService ?? throw new ArgumentNullException(nameof(keyVaultDALService));
|
|
}
|
|
|
|
public async ValueTask ExecuteAsync(CreateSecretRequest command, CancellationToken cancellationToken = default)
|
|
{
|
|
try
|
|
{
|
|
ArgumentNullException.ThrowIfNull(command);
|
|
|
|
if (!command.IsValid(_createSecretValidator))
|
|
{
|
|
_port.ValidationErrors(command.Notifications);
|
|
return;
|
|
}
|
|
|
|
var request = new KeyVaultRequest
|
|
{
|
|
Name = command.Name,
|
|
Value = command.Value,
|
|
};
|
|
|
|
var result = await _keyVaultDALService.CreateSecretAsync(request, cancellationToken).ConfigureAwait(false);
|
|
|
|
if (result == null)
|
|
{
|
|
_port.NoContentSuccess();
|
|
return;
|
|
}
|
|
|
|
_port.Success(result);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ApiResponseHelper.EvaluatePort(ex, _port);
|
|
}
|
|
}
|
|
|
|
public async ValueTask ExecuteAsync(GetSecretRequest command, CancellationToken cancellationToken = default)
|
|
{
|
|
try
|
|
{
|
|
ArgumentNullException.ThrowIfNull(command);
|
|
|
|
var result = await _keyVaultDALService.GetSecretByNameAsync(command.Name, cancellationToken).ConfigureAwait(false);
|
|
|
|
if (result == null)
|
|
{
|
|
_port.NoContentSuccess();
|
|
return;
|
|
}
|
|
|
|
_port.Success(result);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ApiResponseHelper.EvaluatePort(ex, _port);
|
|
}
|
|
}
|
|
|
|
|
|
public async ValueTask ExecuteAsync(UpdateSecretRequest command, CancellationToken cancellationToken = default)
|
|
{
|
|
try
|
|
{
|
|
ArgumentNullException.ThrowIfNull(command);
|
|
|
|
if (!command.IsValid(_updateSecretValidator))
|
|
{
|
|
_port.ValidationErrors(command.Notifications);
|
|
return;
|
|
}
|
|
|
|
var request = new KeyVaultRequest
|
|
{
|
|
Name = command.Name,
|
|
Value = command.Value,
|
|
};
|
|
|
|
var result = await _keyVaultDALService.UpdateSecretAsync(request, cancellationToken).ConfigureAwait(false);
|
|
|
|
if (result == null)
|
|
{
|
|
_port.NoContentSuccess();
|
|
return;
|
|
}
|
|
|
|
_port.Success(result);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ApiResponseHelper.EvaluatePort(ex, _port);
|
|
}
|
|
}
|
|
|
|
|
|
public async ValueTask ExecuteAsync(DeleteSecretRequest command, CancellationToken cancellationToken = default)
|
|
{
|
|
try
|
|
{
|
|
ArgumentNullException.ThrowIfNull(command);
|
|
|
|
if (!command.IsValid(_deleteSecretValidator))
|
|
{
|
|
_port.ValidationErrors(command.Notifications);
|
|
return;
|
|
}
|
|
|
|
var result = await _keyVaultDALService.DeleteSecretAsync(command.Name, cancellationToken).ConfigureAwait(false);
|
|
|
|
if (result == null)
|
|
{
|
|
_port.NoContentSuccess();
|
|
return;
|
|
}
|
|
|
|
_port.Success(result);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ApiResponseHelper.EvaluatePort(ex, _port);
|
|
}
|
|
}
|
|
}
|
|
}
|