124 lines
5.8 KiB
C#
124 lines
5.8 KiB
C#
using Asp.Versioning;
|
|
using Core.Blueprint.Application.UsesCases.KeyVault.Input;
|
|
using Core.Blueprint.Application.UsesCases.KeyVault.Ports;
|
|
using Lib.Architecture.BuildingBlocks;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace Core.Cerberos.Service.API.Controllers
|
|
{
|
|
/// <summary>
|
|
/// Handles all services and business rules related to <see cref="MKeyVaultController"/>.
|
|
/// </summary>
|
|
[ApiVersion("1.0")]
|
|
[Route("api/v{api-version:apiVersion}/[controller]")]
|
|
[Produces("application/json")]
|
|
[ApiController]
|
|
public class KeyVaultController : ControllerBase
|
|
{
|
|
private readonly IComponentHandler<CreateSecretRequest> createSecretHandler;
|
|
private readonly IComponentHandler<GetSecretRequest> getSecretHandler;
|
|
private readonly IComponentHandler<UpdateSecretRequest> updateSecretHandler;
|
|
private readonly IComponentHandler<DeleteSecretRequest> deleteSecretHandler;
|
|
private readonly IKeyVaultPort port;
|
|
|
|
/// <summary>
|
|
/// Handles all services and business rules related to <see cref="MKeyVaultController"/>.
|
|
/// </summary>
|
|
public KeyVaultController(
|
|
IComponentHandler<GetSecretRequest> getSecretHandler,
|
|
IComponentHandler<CreateSecretRequest> createSecretHandler,
|
|
IComponentHandler<UpdateSecretRequest> updateSecretHandler,
|
|
IComponentHandler<DeleteSecretRequest> deleteSecretHandler,
|
|
IKeyVaultPort port
|
|
)
|
|
{
|
|
this.createSecretHandler = createSecretHandler;
|
|
this.updateSecretHandler = updateSecretHandler;
|
|
this.deleteSecretHandler = deleteSecretHandler;
|
|
this.getSecretHandler = getSecretHandler;
|
|
this.port = port;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a new secret.
|
|
/// </summary>
|
|
[HttpPost("CreateSecret")]
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
|
[ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)]
|
|
[ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)]
|
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
public async Task<IActionResult> CreateSecretAsync([FromBody] CreateSecretRequest newSecret, CancellationToken cancellationToken = default)
|
|
{
|
|
await createSecretHandler.ExecuteAsync(newSecret, cancellationToken).ConfigureAwait(false);
|
|
return port.ViewModel;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the secret by name.
|
|
/// </summary>
|
|
[HttpPost]
|
|
[Route("GetSecretByName")]
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
|
[ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)]
|
|
[ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)]
|
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
|
public async Task<IActionResult> GetSecretByName([FromBody] GetSecretRequest request, CancellationToken cancellationToken)
|
|
{
|
|
if (string.IsNullOrEmpty(request.Name)) { return BadRequest("Invalid secret name"); }
|
|
|
|
await getSecretHandler.ExecuteAsync(request, cancellationToken).ConfigureAwait(false);
|
|
|
|
return port.ViewModel;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Updates a full secret by identifier.
|
|
/// </summary>
|
|
[HttpPut("UpdateSecret")]
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
|
[ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)]
|
|
[ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)]
|
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
public async Task<IActionResult> UpdateSecretAsync([FromBody] UpdateSecretRequest request, CancellationToken cancellationToken = default)
|
|
{
|
|
if (string.IsNullOrEmpty(request.Name)) { return BadRequest("Invalid secret name"); }
|
|
|
|
if (string.IsNullOrEmpty(request.Value)) { return BadRequest("Invalid secret value"); }
|
|
|
|
await updateSecretHandler.ExecuteAsync(request, cancellationToken).ConfigureAwait(false);
|
|
|
|
return port.ViewModel;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Deletes a secret by name.
|
|
/// </summary>
|
|
[HttpDelete]
|
|
[Route("DeleteSecret")]
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
|
[ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)]
|
|
[ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)]
|
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
|
public async Task<IActionResult> DeleteSecretAsync([FromBody] DeleteSecretRequest request,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
if (string.IsNullOrEmpty(request.Name)) { return BadRequest("Invalid secret name"); }
|
|
|
|
await deleteSecretHandler.ExecuteAsync(request, cancellationToken).ConfigureAwait(false);
|
|
|
|
return port.ViewModel;
|
|
}
|
|
}
|
|
} |