Implement hashi corp vault
This commit is contained in:
		| @@ -16,17 +16,34 @@ namespace Core.Blueprint.KeyVault.Configuration | ||||
|     { | ||||
|         public static IServiceCollection AddKeyVault(this IServiceCollection services, IConfiguration configuration) | ||||
|         { | ||||
|             var keyVaultUriString = configuration["ConnectionStrings:KeyVaultDAL"]; | ||||
|  | ||||
|             if (string.IsNullOrEmpty(keyVaultUriString)) | ||||
|             var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? string.Empty; | ||||
|  | ||||
|             if(environment ==  "Local") | ||||
|             { | ||||
|                 throw new ArgumentNullException("ConnectionStrings:KeyVault", "KeyVault URI is missing in the configuration."); | ||||
|                 var vaultSettings = configuration.GetSection("Vault").Get<VaultOptions>(); | ||||
|  | ||||
|                 if (string.IsNullOrEmpty(vaultSettings?.Address) || string.IsNullOrEmpty(vaultSettings.Token) || | ||||
|                     string.IsNullOrEmpty(vaultSettings?.SecretPath) || string.IsNullOrEmpty(vaultSettings.SecretMount)) | ||||
|                 { | ||||
|                     throw new ArgumentNullException("Vault options are not configured correctly."); | ||||
|                 } | ||||
|  | ||||
|                 services.AddSingleton(vaultSettings); | ||||
|             } | ||||
|             else | ||||
|             { | ||||
|                 var keyVaultUriString = configuration["ConnectionStrings:KeyVaultDAL"]; | ||||
|  | ||||
|             var keyVaultUri = new Uri(keyVaultUriString); | ||||
|                 if (string.IsNullOrEmpty(keyVaultUriString)) | ||||
|                 { | ||||
|                     throw new ArgumentNullException("ConnectionStrings:KeyVault", "KeyVault URI is missing in the configuration."); | ||||
|                 } | ||||
|  | ||||
|             // Register SecretClient as a singleton | ||||
|             services.AddSingleton(_ => new SecretClient(keyVaultUri, new DefaultAzureCredential())); | ||||
|                 var keyVaultUri = new Uri(keyVaultUriString); | ||||
|  | ||||
|                 services.AddSingleton(_ => new SecretClient(keyVaultUri, new DefaultAzureCredential())); | ||||
|             } | ||||
|  | ||||
|             services.AddSingleton<IKeyVaultProvider, KeyVaultProvider>(); | ||||
|             return services; | ||||
|   | ||||
							
								
								
									
										16
									
								
								Core.Blueprint.KeyVault/Configuration/VaultOptions.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								Core.Blueprint.KeyVault/Configuration/VaultOptions.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,16 @@ | ||||
| using System; | ||||
| using System.Collections.Generic; | ||||
| using System.Linq; | ||||
| using System.Text; | ||||
| using System.Threading.Tasks; | ||||
|  | ||||
| namespace Core.Blueprint.KeyVault.Configuration | ||||
| { | ||||
|     public class VaultOptions | ||||
|     { | ||||
|         public string Address { get; set; } = string.Empty; | ||||
|         public string Token { get; set; } = string.Empty; | ||||
|         public string SecretMount { get; set; } = string.Empty; | ||||
|         public string SecretPath { get; set; } = string.Empty; | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user
	 Sergio Matias Urquin
					Sergio Matias Urquin