Add project files.
This commit is contained in:
9
Core.Blueprint.KeyVault/Adapters/KeyVaultRequest.cs
Normal file
9
Core.Blueprint.KeyVault/Adapters/KeyVaultRequest.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
|
||||
namespace Core.Blueprint.KeyVault
|
||||
{
|
||||
public sealed class KeyVaultRequest
|
||||
{
|
||||
public required string Name { get; set; }
|
||||
public required string Value { get; set; }
|
||||
}
|
||||
}
|
||||
10
Core.Blueprint.KeyVault/Adapters/KeyVaultResponse.cs
Normal file
10
Core.Blueprint.KeyVault/Adapters/KeyVaultResponse.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
|
||||
|
||||
namespace Core.Blueprint.KeyVault
|
||||
{
|
||||
public sealed class KeyVaultResponse
|
||||
{
|
||||
public string Name { get; set; } = null!;
|
||||
public string Value { get; set; } = null!;
|
||||
}
|
||||
}
|
||||
35
Core.Blueprint.KeyVault/Configuration/RegisterBlueprint.cs
Normal file
35
Core.Blueprint.KeyVault/Configuration/RegisterBlueprint.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using Azure.Identity;
|
||||
using Azure.Security.KeyVault.Secrets;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Core.Blueprint.KeyVault.Configuration
|
||||
{
|
||||
/// <summary>
|
||||
/// Registers the SecretClient for Azure Key Vault as a singleton service.
|
||||
/// </summary>
|
||||
/// <param name="services">The IServiceCollection to add the services to.</param>
|
||||
/// <param name="configuration">The application's configuration.</param>
|
||||
/// <returns>The updated IServiceCollection.</returns>
|
||||
/// <exception cref="ArgumentNullException">Thrown when the KeyVault URI is missing in the configuration.</exception>
|
||||
public static class RegisterBlueprint
|
||||
{
|
||||
public static IServiceCollection AddKeyVault(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
var keyVaultUriString = configuration["ConnectionStrings:KeyVaultDAL"];
|
||||
|
||||
if (string.IsNullOrEmpty(keyVaultUriString))
|
||||
{
|
||||
throw new ArgumentNullException("ConnectionStrings:KeyVault", "KeyVault URI is missing in the configuration.");
|
||||
}
|
||||
|
||||
var keyVaultUri = new Uri(keyVaultUriString);
|
||||
|
||||
// Register SecretClient as a singleton
|
||||
services.AddSingleton(_ => new SecretClient(keyVaultUri, new DefaultAzureCredential()));
|
||||
|
||||
services.AddSingleton<IKeyVaultProvider, KeyVaultProvider>();
|
||||
return services;
|
||||
}
|
||||
}
|
||||
}
|
||||
48
Core.Blueprint.KeyVault/Contracts/IKeyVaultProvider.cs
Normal file
48
Core.Blueprint.KeyVault/Contracts/IKeyVaultProvider.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
|
||||
namespace Core.Blueprint.KeyVault
|
||||
{
|
||||
/// <summary>
|
||||
/// Interface for managing secrets in Azure Key Vault.
|
||||
/// </summary>
|
||||
public interface IKeyVaultProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new secret in Azure Key Vault.
|
||||
/// </summary>
|
||||
/// <param name="keyVaultRequest">The request containing the name and value of the secret.</param>
|
||||
/// <param name="cancellationToken">The cancellation token to cancel the operation.</param>
|
||||
/// <returns>A <see cref="KeyVaultResponse"/> containing the details of the created secret.</returns>
|
||||
ValueTask<KeyVaultResponse> CreateSecretAsync(KeyVaultRequest keyVaultRequest, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Deletes a secret from Azure Key Vault if it exists.
|
||||
/// </summary>
|
||||
/// <param name="secretName">The name of the secret to delete.</param>
|
||||
/// <param name="cancellationToken">The cancellation token to cancel the operation.</param>
|
||||
/// <returns>
|
||||
/// A <see cref="Tuple"/> containing a status message and a boolean indicating whether the secret was successfully deleted.
|
||||
/// </returns>
|
||||
ValueTask<Tuple<string, bool>> DeleteSecretAsync(string secretName, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves a secret from Azure Key Vault.
|
||||
/// </summary>
|
||||
/// <param name="secretName">The name of the secret to retrieve.</param>
|
||||
/// <param name="cancellationToken">The cancellation token to cancel the operation.</param>
|
||||
/// <returns>
|
||||
/// A <see cref="Tuple"/> containing the <see cref="KeyVaultResponse"/> with secret details
|
||||
/// and an optional error message if the secret was not found.
|
||||
/// </returns>
|
||||
ValueTask<Tuple<KeyVaultResponse, string?>> GetSecretAsync(string secretName, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Updates an existing secret in Azure Key Vault. If the secret does not exist, an error is returned.
|
||||
/// </summary>
|
||||
/// <param name="newSecret">The updated secret information.</param>
|
||||
/// <param name="cancellationToken">The cancellation token to cancel the operation.</param>
|
||||
/// <returns>
|
||||
/// A <see cref="Tuple"/> containing the updated <see cref="KeyVaultResponse"/> and an optional error message if the secret was not found.
|
||||
/// </returns>
|
||||
ValueTask<Tuple<KeyVaultResponse, string>> UpdateSecretAsync(KeyVaultRequest newSecret, CancellationToken cancellationToken);
|
||||
}
|
||||
}
|
||||
16
Core.Blueprint.KeyVault/Core.Blueprint.KeyVault.csproj
Normal file
16
Core.Blueprint.KeyVault/Core.Blueprint.KeyVault.csproj
Normal file
@@ -0,0 +1,16 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Azure.Identity" Version="1.13.1" />
|
||||
<PackageReference Include="Azure.Security.KeyVault.Secrets" Version="4.7.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="9.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
93
Core.Blueprint.KeyVault/Provider/KeyVaultProvider.cs
Normal file
93
Core.Blueprint.KeyVault/Provider/KeyVaultProvider.cs
Normal file
@@ -0,0 +1,93 @@
|
||||
using Azure;
|
||||
using Azure.Security.KeyVault.Secrets;
|
||||
|
||||
namespace Core.Blueprint.KeyVault
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides operations for managing secrets in Azure Key Vault.
|
||||
/// </summary>
|
||||
public sealed class KeyVaultProvider(SecretClient keyVaultProvider): IKeyVaultProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new secret in Azure Key Vault.
|
||||
/// </summary>
|
||||
/// <param name="keyVaultRequest">The request containing the name and value of the secret.</param>
|
||||
/// <param name="cancellationToken">The cancellation token to cancel the operation.</param>
|
||||
/// <returns>A <see cref="KeyVaultResponse"/> containing the details of the created secret.</returns>
|
||||
public async ValueTask<KeyVaultResponse> CreateSecretAsync(KeyVaultRequest keyVaultRequest, CancellationToken cancellationToken)
|
||||
{
|
||||
KeyVaultResponse _response = new();
|
||||
KeyVaultSecret azureResponse = await keyVaultProvider.SetSecretAsync(new KeyVaultSecret(keyVaultRequest.Name, keyVaultRequest.Value), cancellationToken);
|
||||
|
||||
_response.Value = azureResponse.Value;
|
||||
_response.Name = azureResponse.Name;
|
||||
|
||||
return _response;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deletes a secret from Azure Key Vault if it exists.
|
||||
/// </summary>
|
||||
/// <param name="secretName">The name of the secret to delete.</param>
|
||||
/// <param name="cancellationToken">The cancellation token to cancel the operation.</param>
|
||||
/// <returns>
|
||||
/// A <see cref="Tuple"/> containing a status message and a boolean indicating whether the secret was successfully deleted.
|
||||
/// </returns>
|
||||
public async ValueTask<Tuple<string, bool>> DeleteSecretAsync(string secretName, CancellationToken cancellationToken)
|
||||
{
|
||||
var existingSecret = await this.GetSecretAsync(secretName, cancellationToken);
|
||||
if (existingSecret != null)
|
||||
{
|
||||
await keyVaultProvider.StartDeleteSecretAsync(secretName, cancellationToken);
|
||||
return new("Key Deleted", true);
|
||||
}
|
||||
|
||||
return new("Key Not Found", false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves a secret from Azure Key Vault.
|
||||
/// </summary>
|
||||
/// <param name="secretName">The name of the secret to retrieve.</param>
|
||||
/// <param name="cancellationToken">The cancellation token to cancel the operation.</param>
|
||||
/// <returns>
|
||||
/// A <see cref="Tuple"/> containing the <see cref="KeyVaultResponse"/> with secret details
|
||||
/// and an optional error message if the secret was not found.
|
||||
/// </returns>
|
||||
public async ValueTask<Tuple<KeyVaultResponse, string?>> GetSecretAsync(string secretName, CancellationToken cancellationToken)
|
||||
{
|
||||
KeyVaultSecret azureResponse = await keyVaultProvider.GetSecretAsync(secretName, cancellationToken: cancellationToken);
|
||||
|
||||
if (azureResponse == null)
|
||||
{
|
||||
return new(new KeyVaultResponse(), "Key Not Found");
|
||||
}
|
||||
|
||||
return new(new KeyVaultResponse { Name = secretName, Value = azureResponse.Value }, string.Empty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates an existing secret in Azure Key Vault. If the secret does not exist, an error is returned.
|
||||
/// </summary>
|
||||
/// <param name="newSecret">The updated secret information.</param>
|
||||
/// <param name="cancellationToken">The cancellation token to cancel the operation.</param>
|
||||
/// <returns>
|
||||
/// A <see cref="Tuple"/> containing the updated <see cref="KeyVaultResponse"/> and an optional error message if the secret was not found.
|
||||
/// </returns>
|
||||
public async ValueTask<Tuple<KeyVaultResponse, string>> UpdateSecretAsync(KeyVaultRequest newSecret, CancellationToken cancellationToken)
|
||||
{
|
||||
KeyVaultResponse _response = new();
|
||||
var existingSecret = await this.GetSecretAsync(newSecret.Name, cancellationToken);
|
||||
if (existingSecret == null)
|
||||
{
|
||||
return new(new KeyVaultResponse(), "Key Not Found");
|
||||
}
|
||||
KeyVaultSecret azureResponse = await keyVaultProvider.SetSecretAsync(new KeyVaultSecret(newSecret.Name, newSecret.Value), cancellationToken);
|
||||
|
||||
_response.Value = azureResponse.Value;
|
||||
_response.Name = azureResponse.Name;
|
||||
|
||||
return new(new KeyVaultResponse { Name = newSecret.Name, Value = azureResponse.Value }, string.Empty);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user