4 Commits

4 changed files with 28 additions and 18 deletions

View File

@@ -22,7 +22,7 @@ namespace Core.Blueprint.KeyVault
/// <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);
ValueTask<(string Message, bool Deleted)> DeleteSecretAsync(string secretName, CancellationToken cancellationToken);
/// <summary>
/// Retrieves a secret from Azure Key Vault.
@@ -33,7 +33,7 @@ namespace Core.Blueprint.KeyVault
/// 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);
ValueTask<(KeyVaultResponse Secret, string? Message)> GetSecretAsync(string secretName, CancellationToken cancellationToken);
/// <summary>
/// Updates an existing secret in Azure Key Vault. If the secret does not exist, an error is returned.
@@ -43,6 +43,6 @@ namespace Core.Blueprint.KeyVault
/// <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);
ValueTask<(KeyVaultResponse Secret, string? Message)> UpdateSecretAsync(KeyVaultRequest newSecret, CancellationToken cancellationToken);
}
}

View File

@@ -1,10 +1,10 @@
using Azure.Security.KeyVault.Secrets;
using VaultSharp;
using VaultSharp.V1.AuthMethods.Token;
using Core.Blueprint.KeyVault.Configuration;
using Microsoft.Extensions.Configuration;
using System.Net.Http.Json;
using VaultSharp;
using VaultSharp.Core;
using VaultSharp.V1.AuthMethods.Token;
namespace Core.Blueprint.KeyVault;
@@ -67,7 +67,7 @@ public sealed class KeyVaultProvider : IKeyVaultProvider
/// <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)
public async ValueTask<(string Message, bool Deleted)> DeleteSecretAsync(string secretName, CancellationToken cancellationToken)
{
if (environment == "Local")
{
@@ -88,7 +88,7 @@ public sealed class KeyVaultProvider : IKeyVaultProvider
/// <summary>
/// Retrieves a secret from Azure Key Vault or HashiCorp Vault.
/// </summary>
public async ValueTask<Tuple<KeyVaultResponse, string?>> GetSecretAsync(string secretName, CancellationToken cancellationToken)
public async ValueTask<(KeyVaultResponse Secret, string? Message)> GetSecretAsync(string secretName, CancellationToken cancellationToken)
{
if (environment == "Local")
{
@@ -108,7 +108,7 @@ public sealed class KeyVaultProvider : IKeyVaultProvider
}
catch (VaultSharp.Core.VaultApiException ex) when (ex.HttpStatusCode == System.Net.HttpStatusCode.NotFound)
{
return new(new KeyVaultResponse(), "Key Not Found");
return new(new KeyVaultResponse { }, "Key Not Found");
}
}
@@ -126,7 +126,7 @@ public sealed class KeyVaultProvider : IKeyVaultProvider
/// <summary>
/// Updates an existing secret in Azure Key Vault or HashiCorp Vault. If the secret does not exist, an error is returned.
/// </summary>
public async ValueTask<Tuple<KeyVaultResponse, string>> UpdateSecretAsync(KeyVaultRequest newSecret, CancellationToken cancellationToken)
public async ValueTask<(KeyVaultResponse Secret, string? Message)> UpdateSecretAsync(KeyVaultRequest newSecret, CancellationToken cancellationToken)
{
var existingSecret = await this.GetSecretAsync(newSecret.Name, cancellationToken);
if (!string.IsNullOrEmpty(existingSecret.Item2))

View File

@@ -1,6 +1,7 @@
using Azure.Identity;
using Microsoft.Extensions.Logging;
using StackExchange.Redis;
using System;
using System.Text.Json;
namespace Core.Blueprint.Redis
@@ -29,20 +30,32 @@ namespace Core.Blueprint.Redis
}
/// <summary>
/// Initializes and establishes a connection to Redis using the provided connection string.
/// Initializes and establishes a connection to Redis based on the environment.
/// Uses a local connection in development, and Azure with token credentials in other environments.
/// </summary>
/// <param name="connectionString">The Redis connection string.</param>
/// <returns>An <see cref="IDatabase"/> instance representing the Redis cache database.</returns>
/// <exception cref="Exception">Thrown when the connection to Redis fails.</exce
/// <exception cref="Exception">Thrown when the connection to Redis fails.</exception>
async Task<IDatabase> InitializeRedisAsync(string connectionString)
{
try
{
var configurationOptions = await ConfigurationOptions.Parse($"{connectionString}")
.ConfigureForAzureWithTokenCredentialAsync(new DefaultAzureCredential());
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? string.Empty;
ConnectionMultiplexer connectionMultiplexer;
configurationOptions.AbortOnConnectFail = false;
var connectionMultiplexer = await ConnectionMultiplexer.ConnectAsync(configurationOptions);
if (environment.Equals("Local", StringComparison.OrdinalIgnoreCase))
{
connectionMultiplexer = await ConnectionMultiplexer.ConnectAsync(connectionString);
}
else
{
var configurationOptions = await ConfigurationOptions.Parse(connectionString)
.ConfigureForAzureWithTokenCredentialAsync(new DefaultAzureCredential());
configurationOptions.AbortOnConnectFail = false;
connectionMultiplexer = await ConnectionMultiplexer.ConnectAsync(configurationOptions);
}
_logger.LogInformation("Successfully connected to Redis.");

View File

@@ -20,15 +20,12 @@ namespace Core.Blueprint.SQLServer.Configuration
{
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? string.Empty;
if (environment != "Local")
{
var chainedCredentials = new ChainedTokenCredential(
new ManagedIdentityCredential(),
new SharedTokenCacheCredential(),
new VisualStudioCredential(),
new VisualStudioCodeCredential()
);
}
services.AddScoped(typeof(IEntityRepository<,>), typeof(EntityRepository<,>));