Skip to content

Commit 8f33257

Browse files
committed
fix AddTableStorageService
refactor to use KeyVault Related to: #300
1 parent 4db5c7c commit 8f33257

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

src/AzureOpenAIProxy.ApiApp/Extensions/ServiceCollectionExtensions.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Azure.Data.Tables;
1+
using Azure;
2+
using Azure.Data.Tables;
23
using Azure.Identity;
34
using Azure.Security.KeyVault.Secrets;
45

@@ -145,17 +146,22 @@ public static IServiceCollection AddTableStorageService(this IServiceCollection
145146
{
146147
services.AddSingleton<TableServiceClient>(sp => {
147148
var configuration = sp.GetService<IConfiguration>()
148-
?? throw new InvalidOperationException($"{nameof(IConfiguration)} service is not registered.");
149-
150-
var settings = configuration.GetSection(AzureSettings.Name).GetSection(StorageAccountSettings.Name).Get<StorageAccountSettings>()
149+
?? throw new InvalidOperationException($"{nameof(IConfiguration)} service is not registerd.");
150+
151+
var settings = configuration.GetSection(StorageAccountSettings.Name).Get<StorageAccountSettings>()
151152
?? throw new InvalidOperationException($"{nameof(StorageAccountSettings)} could not be retrieved from the configuration.");
152153

153-
if (string.IsNullOrWhiteSpace(settings.ConnectionString) == true)
154+
if (string.IsNullOrWhiteSpace(settings.KeyVaultSecretName) == true)
154155
{
155-
throw new InvalidOperationException($"{nameof(StorageAccountSettings.ConnectionString)} is not defined.");
156+
throw new InvalidOperationException($"{nameof(StorageAccountSettings.KeyVaultSecretName)} is not defined.");
156157
}
157158

158-
var client = new TableServiceClient(settings.ConnectionString);
159+
var clientSecret = sp.GetService<SecretClient>()
160+
?? throw new InvalidOperationException($"{nameof(SecretClient)} service is not registered.");
161+
162+
var storageKeyVault = clientSecret.GetSecret(settings.KeyVaultSecretName);
163+
164+
var client = new TableServiceClient(storageKeyVault.Value.Value);
159165

160166
return client;
161167
});

0 commit comments

Comments
 (0)