From d2ca4626bda2a4b554d41c579cda56503ae0a975 Mon Sep 17 00:00:00 2001 From: sikutisa Date: Tue, 3 Sep 2024 23:27:27 +0900 Subject: [PATCH 01/20] add table storage cofig to appsettings.json Related to: #213, #208 --- src/AzureOpenAIProxy.ApiApp/appsettings.json | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/AzureOpenAIProxy.ApiApp/appsettings.json b/src/AzureOpenAIProxy.ApiApp/appsettings.json index 6c89d496..0eb017c8 100644 --- a/src/AzureOpenAIProxy.ApiApp/appsettings.json +++ b/src/AzureOpenAIProxy.ApiApp/appsettings.json @@ -26,6 +26,10 @@ "KeyVault": { "VaultUri": "https://{{key-vault-name}}.vault.azure.net/", "SecretName": "azure-openai-instances" + }, + "StorageAccount": { + "ConnectionString": "", + "ContainerName": "" } }, From 1e932d558003b96486a6eb19319a1c8aebfe8eb8 Mon Sep 17 00:00:00 2001 From: sikutisa Date: Tue, 3 Sep 2024 23:43:03 +0900 Subject: [PATCH 02/20] add StorageAccountSettings Related to: #208, #213 --- .../Configurations/AzureSettings.cs | 5 +++++ .../Configurations/StorageAccountSettings.cs | 22 +++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 src/AzureOpenAIProxy.ApiApp/Configurations/StorageAccountSettings.cs diff --git a/src/AzureOpenAIProxy.ApiApp/Configurations/AzureSettings.cs b/src/AzureOpenAIProxy.ApiApp/Configurations/AzureSettings.cs index 59cb53cd..cd1eba02 100644 --- a/src/AzureOpenAIProxy.ApiApp/Configurations/AzureSettings.cs +++ b/src/AzureOpenAIProxy.ApiApp/Configurations/AzureSettings.cs @@ -19,4 +19,9 @@ public class AzureSettings /// Gets or sets the instance. /// public KeyVaultSettings KeyVault { get; set; } = new(); + + /// + /// Gets or sets the instance. + /// + public StorageAccountSettings StorageAccount { get; set; } = new(); } diff --git a/src/AzureOpenAIProxy.ApiApp/Configurations/StorageAccountSettings.cs b/src/AzureOpenAIProxy.ApiApp/Configurations/StorageAccountSettings.cs new file mode 100644 index 00000000..08eaf2cd --- /dev/null +++ b/src/AzureOpenAIProxy.ApiApp/Configurations/StorageAccountSettings.cs @@ -0,0 +1,22 @@ +namespace AzureOpenAIProxy.ApiApp.Configurations; + +/// +/// This represents the settings entity for Table Storage. +/// +public class StorageAccountSettings +{ + /// + /// Gets the name of the configuration settings. + /// + public const string Name = "StorageAccount"; + + /// + /// Gets or sets the connection string. + /// + public string? ConnectionString { get; set; } + + /// + /// Gets or sets the container name. + /// + public string? ContainerName { get; set; } +} \ No newline at end of file From 2e267c15c73d037c2d6cc8d42857582689035468 Mon Sep 17 00:00:00 2001 From: sikutisa Date: Wed, 4 Sep 2024 00:25:33 +0900 Subject: [PATCH 03/20] add AddTableStorageService Related to: #208, #213 --- .../AzureOpenAIProxy.ApiApp.csproj | 1 + .../Extensions/ServiceCollectionExtensions.cs | 35 ++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/AzureOpenAIProxy.ApiApp/AzureOpenAIProxy.ApiApp.csproj b/src/AzureOpenAIProxy.ApiApp/AzureOpenAIProxy.ApiApp.csproj index 92c1aae3..554f15c3 100644 --- a/src/AzureOpenAIProxy.ApiApp/AzureOpenAIProxy.ApiApp.csproj +++ b/src/AzureOpenAIProxy.ApiApp/AzureOpenAIProxy.ApiApp.csproj @@ -7,6 +7,7 @@ + diff --git a/src/AzureOpenAIProxy.ApiApp/Extensions/ServiceCollectionExtensions.cs b/src/AzureOpenAIProxy.ApiApp/Extensions/ServiceCollectionExtensions.cs index ae2971bb..880a030e 100644 --- a/src/AzureOpenAIProxy.ApiApp/Extensions/ServiceCollectionExtensions.cs +++ b/src/AzureOpenAIProxy.ApiApp/Extensions/ServiceCollectionExtensions.cs @@ -1,4 +1,5 @@ -using Azure.Identity; +using Azure.Data.Tables; +using Azure.Identity; using Azure.Security.KeyVault.Secrets; using AzureOpenAIProxy.ApiApp.Builders; @@ -134,4 +135,36 @@ public static IServiceCollection AddOpenApiService(this IServiceCollection servi return services; } + + /// + /// Adds the TableServiceClient to the services collection. + /// + /// instance. + /// Returns instance. + public static IServiceCollection AddTableStorageService(this IServiceCollection services) + { + services.AddSingleton(sp => { + var configuration = sp.GetService() + ?? throw new InvalidOperationException($"{nameof(IConfiguration)} service is not registered."); + + var settings = configuration.GetSection(AzureSettings.Name).GetSection(StorageAccountSettings.Name).Get() + ?? throw new InvalidOperationException($"{nameof(StorageAccountSettings)} could not be retrieved from the configuration."); + + if (string.IsNullOrWhiteSpace(settings.ConnectionString) == true) + { + throw new InvalidOperationException($"{nameof(StorageAccountSettings.ConnectionString)} is not defined."); + } + + if (string.IsNullOrWhiteSpace(settings.ContainerName) == true) + { + throw new InvalidOperationException($"{nameof(StorageAccountSettings.ContainerName)} is not defined."); + } + + var client = new TableServiceClient(settings.ConnectionString); + + return client; + }); + + return services; + } } From c491f0a2aaf73667492aa777fd5b237254c30a21 Mon Sep 17 00:00:00 2001 From: sikutisa Date: Wed, 4 Sep 2024 00:37:11 +0900 Subject: [PATCH 04/20] inject TableStorageClient Dependency Related to: #208, #213 --- src/AzureOpenAIProxy.ApiApp/Program.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/AzureOpenAIProxy.ApiApp/Program.cs b/src/AzureOpenAIProxy.ApiApp/Program.cs index 4c5a0e55..5bcaf935 100644 --- a/src/AzureOpenAIProxy.ApiApp/Program.cs +++ b/src/AzureOpenAIProxy.ApiApp/Program.cs @@ -16,6 +16,9 @@ // Add OpenAPI service builder.Services.AddOpenApiService(); +// Add TableStorageClient +builder.Services.AddTableStorageService(); + // Add admin services builder.Services.AddAdminEventService(); From a49ff986c241522512b5b8da98212675f321ce21 Mon Sep 17 00:00:00 2001 From: sikutisa Date: Wed, 4 Sep 2024 00:53:22 +0900 Subject: [PATCH 05/20] remove container name property it doesn't use when create TableStorageClient Related to: #208, #213 --- .../Configurations/StorageAccountSettings.cs | 5 ----- .../Extensions/ServiceCollectionExtensions.cs | 5 ----- src/AzureOpenAIProxy.ApiApp/appsettings.json | 3 +-- 3 files changed, 1 insertion(+), 12 deletions(-) diff --git a/src/AzureOpenAIProxy.ApiApp/Configurations/StorageAccountSettings.cs b/src/AzureOpenAIProxy.ApiApp/Configurations/StorageAccountSettings.cs index 08eaf2cd..4e7bd811 100644 --- a/src/AzureOpenAIProxy.ApiApp/Configurations/StorageAccountSettings.cs +++ b/src/AzureOpenAIProxy.ApiApp/Configurations/StorageAccountSettings.cs @@ -14,9 +14,4 @@ public class StorageAccountSettings /// Gets or sets the connection string. /// public string? ConnectionString { get; set; } - - /// - /// Gets or sets the container name. - /// - public string? ContainerName { get; set; } } \ No newline at end of file diff --git a/src/AzureOpenAIProxy.ApiApp/Extensions/ServiceCollectionExtensions.cs b/src/AzureOpenAIProxy.ApiApp/Extensions/ServiceCollectionExtensions.cs index 880a030e..a79a9383 100644 --- a/src/AzureOpenAIProxy.ApiApp/Extensions/ServiceCollectionExtensions.cs +++ b/src/AzureOpenAIProxy.ApiApp/Extensions/ServiceCollectionExtensions.cs @@ -155,11 +155,6 @@ public static IServiceCollection AddTableStorageService(this IServiceCollection throw new InvalidOperationException($"{nameof(StorageAccountSettings.ConnectionString)} is not defined."); } - if (string.IsNullOrWhiteSpace(settings.ContainerName) == true) - { - throw new InvalidOperationException($"{nameof(StorageAccountSettings.ContainerName)} is not defined."); - } - var client = new TableServiceClient(settings.ConnectionString); return client; diff --git a/src/AzureOpenAIProxy.ApiApp/appsettings.json b/src/AzureOpenAIProxy.ApiApp/appsettings.json index 0eb017c8..2b96d9ef 100644 --- a/src/AzureOpenAIProxy.ApiApp/appsettings.json +++ b/src/AzureOpenAIProxy.ApiApp/appsettings.json @@ -28,8 +28,7 @@ "SecretName": "azure-openai-instances" }, "StorageAccount": { - "ConnectionString": "", - "ContainerName": "" + "ConnectionString": "" } }, From 4db5c7c144474fefdd324dabfbab9365d3f767c1 Mon Sep 17 00:00:00 2001 From: sikutisa Date: Sat, 7 Sep 2024 10:34:31 +0900 Subject: [PATCH 06/20] fix StorageAccountSettings get azure storage connection string from key vault Related to: #300 --- .../Configurations/StorageAccountSettings.cs | 2 +- src/AzureOpenAIProxy.ApiApp/appsettings.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/AzureOpenAIProxy.ApiApp/Configurations/StorageAccountSettings.cs b/src/AzureOpenAIProxy.ApiApp/Configurations/StorageAccountSettings.cs index 4e7bd811..ee29305d 100644 --- a/src/AzureOpenAIProxy.ApiApp/Configurations/StorageAccountSettings.cs +++ b/src/AzureOpenAIProxy.ApiApp/Configurations/StorageAccountSettings.cs @@ -13,5 +13,5 @@ public class StorageAccountSettings /// /// Gets or sets the connection string. /// - public string? ConnectionString { get; set; } + public string? KeyVaultSecretName { get; set; } } \ No newline at end of file diff --git a/src/AzureOpenAIProxy.ApiApp/appsettings.json b/src/AzureOpenAIProxy.ApiApp/appsettings.json index 2b96d9ef..c22865ad 100644 --- a/src/AzureOpenAIProxy.ApiApp/appsettings.json +++ b/src/AzureOpenAIProxy.ApiApp/appsettings.json @@ -28,7 +28,7 @@ "SecretName": "azure-openai-instances" }, "StorageAccount": { - "ConnectionString": "" + "KeyVaultSecretName": "storage-connection-string" } }, From 8f3325751899857284bf6f05a571c5c655a7959b Mon Sep 17 00:00:00 2001 From: sikutisa Date: Sat, 7 Sep 2024 11:16:47 +0900 Subject: [PATCH 07/20] fix AddTableStorageService refactor to use KeyVault Related to: #300 --- .../Extensions/ServiceCollectionExtensions.cs | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/AzureOpenAIProxy.ApiApp/Extensions/ServiceCollectionExtensions.cs b/src/AzureOpenAIProxy.ApiApp/Extensions/ServiceCollectionExtensions.cs index a79a9383..725e98a6 100644 --- a/src/AzureOpenAIProxy.ApiApp/Extensions/ServiceCollectionExtensions.cs +++ b/src/AzureOpenAIProxy.ApiApp/Extensions/ServiceCollectionExtensions.cs @@ -1,4 +1,5 @@ -using Azure.Data.Tables; +using Azure; +using Azure.Data.Tables; using Azure.Identity; using Azure.Security.KeyVault.Secrets; @@ -145,17 +146,22 @@ public static IServiceCollection AddTableStorageService(this IServiceCollection { services.AddSingleton(sp => { var configuration = sp.GetService() - ?? throw new InvalidOperationException($"{nameof(IConfiguration)} service is not registered."); - - var settings = configuration.GetSection(AzureSettings.Name).GetSection(StorageAccountSettings.Name).Get() + ?? throw new InvalidOperationException($"{nameof(IConfiguration)} service is not registerd."); + + var settings = configuration.GetSection(StorageAccountSettings.Name).Get() ?? throw new InvalidOperationException($"{nameof(StorageAccountSettings)} could not be retrieved from the configuration."); - if (string.IsNullOrWhiteSpace(settings.ConnectionString) == true) + if (string.IsNullOrWhiteSpace(settings.KeyVaultSecretName) == true) { - throw new InvalidOperationException($"{nameof(StorageAccountSettings.ConnectionString)} is not defined."); + throw new InvalidOperationException($"{nameof(StorageAccountSettings.KeyVaultSecretName)} is not defined."); } - var client = new TableServiceClient(settings.ConnectionString); + var clientSecret = sp.GetService() + ?? throw new InvalidOperationException($"{nameof(SecretClient)} service is not registered."); + + var storageKeyVault = clientSecret.GetSecret(settings.KeyVaultSecretName); + + var client = new TableServiceClient(storageKeyVault.Value.Value); return client; }); From dd1104585bc25eb22d6b32b92ce16f4559152870 Mon Sep 17 00:00:00 2001 From: sikutisa Date: Sat, 7 Sep 2024 11:23:12 +0900 Subject: [PATCH 08/20] fix AddTableStorageService Related to: #300 --- .../Extensions/ServiceCollectionExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AzureOpenAIProxy.ApiApp/Extensions/ServiceCollectionExtensions.cs b/src/AzureOpenAIProxy.ApiApp/Extensions/ServiceCollectionExtensions.cs index 725e98a6..3804b149 100644 --- a/src/AzureOpenAIProxy.ApiApp/Extensions/ServiceCollectionExtensions.cs +++ b/src/AzureOpenAIProxy.ApiApp/Extensions/ServiceCollectionExtensions.cs @@ -148,7 +148,7 @@ public static IServiceCollection AddTableStorageService(this IServiceCollection var configuration = sp.GetService() ?? throw new InvalidOperationException($"{nameof(IConfiguration)} service is not registerd."); - var settings = configuration.GetSection(StorageAccountSettings.Name).Get() + var settings = configuration.GetSection(AzureSettings.Name).GetSection(StorageAccountSettings.Name).Get() ?? throw new InvalidOperationException($"{nameof(StorageAccountSettings)} could not be retrieved from the configuration."); if (string.IsNullOrWhiteSpace(settings.KeyVaultSecretName) == true) From 40ade5eec317cb45f99ea5aab49e725e6ed73fa6 Mon Sep 17 00:00:00 2001 From: sikutisa Date: Sat, 7 Sep 2024 11:58:26 +0900 Subject: [PATCH 09/20] refactor appsettings.json Related to: #300 --- src/AzureOpenAIProxy.ApiApp/appsettings.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/AzureOpenAIProxy.ApiApp/appsettings.json b/src/AzureOpenAIProxy.ApiApp/appsettings.json index c22865ad..a6725e09 100644 --- a/src/AzureOpenAIProxy.ApiApp/appsettings.json +++ b/src/AzureOpenAIProxy.ApiApp/appsettings.json @@ -25,10 +25,10 @@ }, "KeyVault": { "VaultUri": "https://{{key-vault-name}}.vault.azure.net/", - "SecretName": "azure-openai-instances" - }, - "StorageAccount": { - "KeyVaultSecretName": "storage-connection-string" + "SecretNames": { + "OpenAI": "azure-openai-instances", + "Storage": "storage-connection-string" + } } }, From 3e416a169a0a6341d93b9ae58f2bab52d0276256 Mon Sep 17 00:00:00 2001 From: sikutisa Date: Sat, 7 Sep 2024 11:59:50 +0900 Subject: [PATCH 10/20] refactor Configurations Related to: #300 --- .../Configurations/AzureSettings.cs | 5 ----- .../Configurations/KeyVaultSettings.cs | 4 ++-- .../Configurations/StorageAccountSettings.cs | 17 ----------------- 3 files changed, 2 insertions(+), 24 deletions(-) delete mode 100644 src/AzureOpenAIProxy.ApiApp/Configurations/StorageAccountSettings.cs diff --git a/src/AzureOpenAIProxy.ApiApp/Configurations/AzureSettings.cs b/src/AzureOpenAIProxy.ApiApp/Configurations/AzureSettings.cs index cd1eba02..59cb53cd 100644 --- a/src/AzureOpenAIProxy.ApiApp/Configurations/AzureSettings.cs +++ b/src/AzureOpenAIProxy.ApiApp/Configurations/AzureSettings.cs @@ -19,9 +19,4 @@ public class AzureSettings /// Gets or sets the instance. /// public KeyVaultSettings KeyVault { get; set; } = new(); - - /// - /// Gets or sets the instance. - /// - public StorageAccountSettings StorageAccount { get; set; } = new(); } diff --git a/src/AzureOpenAIProxy.ApiApp/Configurations/KeyVaultSettings.cs b/src/AzureOpenAIProxy.ApiApp/Configurations/KeyVaultSettings.cs index 80c3a51b..b3678861 100644 --- a/src/AzureOpenAIProxy.ApiApp/Configurations/KeyVaultSettings.cs +++ b/src/AzureOpenAIProxy.ApiApp/Configurations/KeyVaultSettings.cs @@ -16,7 +16,7 @@ public class KeyVaultSettings public string? VaultUri { get; set; } /// - /// Gets or sets the secret name. + /// Gets or sets the secret names. /// - public string? SecretName { get; set; } + public IDictionary SecretNames { get; set; } = new Dictionary(); } \ No newline at end of file diff --git a/src/AzureOpenAIProxy.ApiApp/Configurations/StorageAccountSettings.cs b/src/AzureOpenAIProxy.ApiApp/Configurations/StorageAccountSettings.cs deleted file mode 100644 index ee29305d..00000000 --- a/src/AzureOpenAIProxy.ApiApp/Configurations/StorageAccountSettings.cs +++ /dev/null @@ -1,17 +0,0 @@ -namespace AzureOpenAIProxy.ApiApp.Configurations; - -/// -/// This represents the settings entity for Table Storage. -/// -public class StorageAccountSettings -{ - /// - /// Gets the name of the configuration settings. - /// - public const string Name = "StorageAccount"; - - /// - /// Gets or sets the connection string. - /// - public string? KeyVaultSecretName { get; set; } -} \ No newline at end of file From 1418525ed8bb20e3c6c17c149d5f5bfe52da85c9 Mon Sep 17 00:00:00 2001 From: sikutisa Date: Sat, 7 Sep 2024 12:04:02 +0900 Subject: [PATCH 11/20] refactor WithKeyVault Related to: #300 --- .../Extensions/OpenAISettingsBuilderExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AzureOpenAIProxy.ApiApp/Extensions/OpenAISettingsBuilderExtensions.cs b/src/AzureOpenAIProxy.ApiApp/Extensions/OpenAISettingsBuilderExtensions.cs index dc05682b..2f44460c 100644 --- a/src/AzureOpenAIProxy.ApiApp/Extensions/OpenAISettingsBuilderExtensions.cs +++ b/src/AzureOpenAIProxy.ApiApp/Extensions/OpenAISettingsBuilderExtensions.cs @@ -48,7 +48,7 @@ public static IOpenAISettingsBuilder WithKeyVault(this IOpenAISettingsBuilder bu var client = sp.GetService() ?? throw new InvalidOperationException($"{nameof(SecretClient)} service is not registered."); - var value = client.GetSecret(settings.SecretName!).Value.Value; + var value = client.GetSecret(settings.SecretNames["OpenAI"]!).Value.Value; var instances = JsonSerializer.Deserialize>(value); From cdf174c43796e7a182a4fa82134905fb7c00e05b Mon Sep 17 00:00:00 2001 From: sikutisa Date: Sat, 7 Sep 2024 12:16:05 +0900 Subject: [PATCH 12/20] refactor AddTableStorageService Related to: #300 --- .../Extensions/ServiceCollectionExtensions.cs | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/AzureOpenAIProxy.ApiApp/Extensions/ServiceCollectionExtensions.cs b/src/AzureOpenAIProxy.ApiApp/Extensions/ServiceCollectionExtensions.cs index 3804b149..dd81018b 100644 --- a/src/AzureOpenAIProxy.ApiApp/Extensions/ServiceCollectionExtensions.cs +++ b/src/AzureOpenAIProxy.ApiApp/Extensions/ServiceCollectionExtensions.cs @@ -37,9 +37,9 @@ public static IServiceCollection AddKeyVaultService(this IServiceCollection serv throw new InvalidOperationException($"{nameof(KeyVaultSettings.VaultUri)} is not defined."); } - if (string.IsNullOrWhiteSpace(settings.SecretName) == true) + if (string.IsNullOrWhiteSpace(settings.SecretNames["OpenAI"]) == true) { - throw new InvalidOperationException($"{nameof(KeyVaultSettings.SecretName)} is not defined."); + throw new InvalidOperationException($"{nameof(KeyVaultSettings.SecretNames)}.OpenAI is not defined."); } var client = new SecretClient(new Uri(settings.VaultUri), new DefaultAzureCredential()); @@ -148,18 +148,13 @@ public static IServiceCollection AddTableStorageService(this IServiceCollection var configuration = sp.GetService() ?? throw new InvalidOperationException($"{nameof(IConfiguration)} service is not registerd."); - var settings = configuration.GetSection(AzureSettings.Name).GetSection(StorageAccountSettings.Name).Get() - ?? throw new InvalidOperationException($"{nameof(StorageAccountSettings)} could not be retrieved from the configuration."); - - if (string.IsNullOrWhiteSpace(settings.KeyVaultSecretName) == true) - { - throw new InvalidOperationException($"{nameof(StorageAccountSettings.KeyVaultSecretName)} is not defined."); - } + var settings = configuration.GetSection(AzureSettings.Name).GetSection(KeyVaultSettings.Name).Get() + ?? throw new InvalidOperationException($"{nameof(KeyVaultSettings)} could not be retrieved from the configuration."); var clientSecret = sp.GetService() ?? throw new InvalidOperationException($"{nameof(SecretClient)} service is not registered."); - var storageKeyVault = clientSecret.GetSecret(settings.KeyVaultSecretName); + var storageKeyVault = clientSecret.GetSecret(settings.SecretNames["Storage"]!); var client = new TableServiceClient(storageKeyVault.Value.Value); From f57decc1d4e6fe2a2eebcf990c7d3df8c1ffccf5 Mon Sep 17 00:00:00 2001 From: sikutisa Date: Sat, 7 Sep 2024 15:48:01 +0900 Subject: [PATCH 13/20] update KeyvaultSettings and tests Related to: #300 --- .../Configurations/KeyVaultSettings.cs | 2 +- .../Extensions/ServiceCollectionExtensionsTests.cs | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/AzureOpenAIProxy.ApiApp/Configurations/KeyVaultSettings.cs b/src/AzureOpenAIProxy.ApiApp/Configurations/KeyVaultSettings.cs index b3678861..daf39330 100644 --- a/src/AzureOpenAIProxy.ApiApp/Configurations/KeyVaultSettings.cs +++ b/src/AzureOpenAIProxy.ApiApp/Configurations/KeyVaultSettings.cs @@ -18,5 +18,5 @@ public class KeyVaultSettings /// /// Gets or sets the secret names. /// - public IDictionary SecretNames { get; set; } = new Dictionary(); + public Dictionary SecretNames { get; set; } = new(); } \ No newline at end of file diff --git a/test/AzureOpenAIProxy.ApiApp.Tests/Extensions/ServiceCollectionExtensionsTests.cs b/test/AzureOpenAIProxy.ApiApp.Tests/Extensions/ServiceCollectionExtensionsTests.cs index 142c1eac..47cf8847 100644 --- a/test/AzureOpenAIProxy.ApiApp.Tests/Extensions/ServiceCollectionExtensionsTests.cs +++ b/test/AzureOpenAIProxy.ApiApp.Tests/Extensions/ServiceCollectionExtensionsTests.cs @@ -92,7 +92,7 @@ public void Given_NullOrEmpty_VaultUri_When_Invoked_AddKeyVaultService_Then_It_S var dict = new Dictionary() { { "Azure:KeyVault:VaultUri", vaultUri! }, - { "Azure:KeyVault:SecretName", secretName }, + { "Azure:KeyVault:SecretNames:OpenAI", secretName }, }; #pragma warning disable CS8620 // Argument cannot be used for parameter due to differences in the nullability of reference types. var config = new ConfigurationBuilder().AddInMemoryCollection(dict).Build(); @@ -117,7 +117,7 @@ public void Given_NullOrEmpty_SecretName_When_Invoked_AddKeyVaultService_Then_It var dict = new Dictionary() { { "Azure:KeyVault:VaultUri", vaultUri }, - { "Azure:KeyVault:SecretName", secretName! }, + { "Azure:KeyVault:SecretNames:OpenAI", secretName! }, }; #pragma warning disable CS8620 // Argument cannot be used for parameter due to differences in the nullability of reference types. var config = new ConfigurationBuilder().AddInMemoryCollection(dict).Build(); @@ -141,7 +141,7 @@ public void Given_Invalid_VaultUri_When_Invoked_AddKeyVaultService_Then_It_Shoul var dict = new Dictionary() { { "Azure:KeyVault:VaultUri", vaultUri }, - { "Azure:KeyVault:SecretName", secretName }, + { "Azure:KeyVault:SecretNames:OpenAI", secretName }, }; #pragma warning disable CS8620 // Argument cannot be used for parameter due to differences in the nullability of reference types. var config = new ConfigurationBuilder().AddInMemoryCollection(dict).Build(); @@ -165,7 +165,7 @@ public void Given_AppSettings_When_Invoked_AddKeyVaultService_Then_It_Should_Ret var dict = new Dictionary() { { "Azure:KeyVault:VaultUri", vaultUri }, - { "Azure:KeyVault:SecretName", secretName }, + { "Azure:KeyVault:SecretNames:OpenAI", secretName }, }; #pragma warning disable CS8620 // Argument cannot be used for parameter due to differences in the nullability of reference types. var config = new ConfigurationBuilder().AddInMemoryCollection(dict).Build(); @@ -190,7 +190,7 @@ public void Given_AppSettings_When_Invoked_AddKeyVaultService_Then_It_Should_Ret var dict = new Dictionary() { { "Azure:KeyVault:VaultUri", vaultUri }, - { "Azure:KeyVault:SecretName", secretName }, + { "Azure:KeyVault:SecretNames:OpenAI", secretName }, }; #pragma warning disable CS8620 // Argument cannot be used for parameter due to differences in the nullability of reference types. var config = new ConfigurationBuilder().AddInMemoryCollection(dict).Build(); From ceaa16bd2f6724601c4af2348649f46b87f6e204 Mon Sep 17 00:00:00 2001 From: sikutisa Date: Sat, 7 Sep 2024 15:52:07 +0900 Subject: [PATCH 14/20] fix test Related to: #300 --- .../Extensions/ServiceCollectionExtensionsTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/AzureOpenAIProxy.ApiApp.Tests/Extensions/ServiceCollectionExtensionsTests.cs b/test/AzureOpenAIProxy.ApiApp.Tests/Extensions/ServiceCollectionExtensionsTests.cs index 47cf8847..363e3f79 100644 --- a/test/AzureOpenAIProxy.ApiApp.Tests/Extensions/ServiceCollectionExtensionsTests.cs +++ b/test/AzureOpenAIProxy.ApiApp.Tests/Extensions/ServiceCollectionExtensionsTests.cs @@ -117,7 +117,7 @@ public void Given_NullOrEmpty_SecretName_When_Invoked_AddKeyVaultService_Then_It var dict = new Dictionary() { { "Azure:KeyVault:VaultUri", vaultUri }, - { "Azure:KeyVault:SecretNames:OpenAI", secretName! }, + { "Azure:KeyVault:SecretNames:OpenAI", secretName }, }; #pragma warning disable CS8620 // Argument cannot be used for parameter due to differences in the nullability of reference types. var config = new ConfigurationBuilder().AddInMemoryCollection(dict).Build(); From 71d979a10725298803408005329fa86490275392 Mon Sep 17 00:00:00 2001 From: sikutisa Date: Sat, 7 Sep 2024 15:56:39 +0900 Subject: [PATCH 15/20] Revert "fix test" This reverts commit ceaa16bd2f6724601c4af2348649f46b87f6e204. --- .../Extensions/ServiceCollectionExtensionsTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/AzureOpenAIProxy.ApiApp.Tests/Extensions/ServiceCollectionExtensionsTests.cs b/test/AzureOpenAIProxy.ApiApp.Tests/Extensions/ServiceCollectionExtensionsTests.cs index 363e3f79..47cf8847 100644 --- a/test/AzureOpenAIProxy.ApiApp.Tests/Extensions/ServiceCollectionExtensionsTests.cs +++ b/test/AzureOpenAIProxy.ApiApp.Tests/Extensions/ServiceCollectionExtensionsTests.cs @@ -117,7 +117,7 @@ public void Given_NullOrEmpty_SecretName_When_Invoked_AddKeyVaultService_Then_It var dict = new Dictionary() { { "Azure:KeyVault:VaultUri", vaultUri }, - { "Azure:KeyVault:SecretNames:OpenAI", secretName }, + { "Azure:KeyVault:SecretNames:OpenAI", secretName! }, }; #pragma warning disable CS8620 // Argument cannot be used for parameter due to differences in the nullability of reference types. var config = new ConfigurationBuilder().AddInMemoryCollection(dict).Build(); From c7f34e6f11a99994e7dc9e579361cc8bbe130869 Mon Sep 17 00:00:00 2001 From: sikutisa Date: Sat, 7 Sep 2024 17:59:35 +0900 Subject: [PATCH 16/20] refactor KeyVaultSettings Related to: #300 --- src/AzureOpenAIProxy.ApiApp/Configurations/KeyVaultSettings.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AzureOpenAIProxy.ApiApp/Configurations/KeyVaultSettings.cs b/src/AzureOpenAIProxy.ApiApp/Configurations/KeyVaultSettings.cs index daf39330..763d861e 100644 --- a/src/AzureOpenAIProxy.ApiApp/Configurations/KeyVaultSettings.cs +++ b/src/AzureOpenAIProxy.ApiApp/Configurations/KeyVaultSettings.cs @@ -18,5 +18,5 @@ public class KeyVaultSettings /// /// Gets or sets the secret names. /// - public Dictionary SecretNames { get; set; } = new(); + public Dictionary SecretNames { get; set; } = []; } \ No newline at end of file From 4ba866670275d7d29325d9f7d3bf6b95990a6963 Mon Sep 17 00:00:00 2001 From: sikutisa Date: Wed, 11 Sep 2024 23:14:53 +0900 Subject: [PATCH 17/20] fix tests If a null secretName is given, a KeyNotFoundException is thrown instead of an InvalidOperationException. Related to: #300 --- .../Extensions/ServiceCollectionExtensionsTests.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/AzureOpenAIProxy.ApiApp.Tests/Extensions/ServiceCollectionExtensionsTests.cs b/test/AzureOpenAIProxy.ApiApp.Tests/Extensions/ServiceCollectionExtensionsTests.cs index 47cf8847..5a77eda8 100644 --- a/test/AzureOpenAIProxy.ApiApp.Tests/Extensions/ServiceCollectionExtensionsTests.cs +++ b/test/AzureOpenAIProxy.ApiApp.Tests/Extensions/ServiceCollectionExtensionsTests.cs @@ -108,9 +108,9 @@ public void Given_NullOrEmpty_VaultUri_When_Invoked_AddKeyVaultService_Then_It_S } [Theory] - [InlineData("http://localhost", default(string))] - [InlineData("http://localhost", "")] - public void Given_NullOrEmpty_SecretName_When_Invoked_AddKeyVaultService_Then_It_Should_Throw_Exception(string vaultUri, string? secretName) + [InlineData("http://localhost", default(string), typeof(KeyNotFoundException))] + [InlineData("http://localhost", "", typeof(InvalidOperationException))] + public void Given_NullOrEmpty_SecretName_When_Invoked_AddKeyVaultService_Then_It_Should_Throw_Exception(string vaultUri, string? secretName, Type exceptionType) { // Arrange var services = new ServiceCollection(); @@ -129,7 +129,7 @@ public void Given_NullOrEmpty_SecretName_When_Invoked_AddKeyVaultService_Then_It Action action = () => services.BuildServiceProvider().GetService(); // Assert - action.Should().Throw(); + action.Should().Throw().Which.Should().BeOfType(exceptionType); } [Theory] From 02946bc4546c5242bfcb5b951ebb284bff630a36 Mon Sep 17 00:00:00 2001 From: sikutisa Date: Wed, 11 Sep 2024 23:49:08 +0900 Subject: [PATCH 18/20] add KeyVaultSecretNames.cs Related to: #300 --- .../OpenAISettingsBuilderExtensions.cs | 2 +- .../Extensions/ServiceCollectionExtensions.cs | 6 +++--- .../KeyVaultSecretNames.cs | 18 ++++++++++++++++++ 3 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 src/AzureOpenAIProxy.ApiApp/KeyVaultSecretNames.cs diff --git a/src/AzureOpenAIProxy.ApiApp/Extensions/OpenAISettingsBuilderExtensions.cs b/src/AzureOpenAIProxy.ApiApp/Extensions/OpenAISettingsBuilderExtensions.cs index 2f44460c..d4944acf 100644 --- a/src/AzureOpenAIProxy.ApiApp/Extensions/OpenAISettingsBuilderExtensions.cs +++ b/src/AzureOpenAIProxy.ApiApp/Extensions/OpenAISettingsBuilderExtensions.cs @@ -48,7 +48,7 @@ public static IOpenAISettingsBuilder WithKeyVault(this IOpenAISettingsBuilder bu var client = sp.GetService() ?? throw new InvalidOperationException($"{nameof(SecretClient)} service is not registered."); - var value = client.GetSecret(settings.SecretNames["OpenAI"]!).Value.Value; + var value = client.GetSecret(settings.SecretNames[KeyVaultSecretNames.OpenAI]!).Value.Value; var instances = JsonSerializer.Deserialize>(value); diff --git a/src/AzureOpenAIProxy.ApiApp/Extensions/ServiceCollectionExtensions.cs b/src/AzureOpenAIProxy.ApiApp/Extensions/ServiceCollectionExtensions.cs index dd81018b..98dfc494 100644 --- a/src/AzureOpenAIProxy.ApiApp/Extensions/ServiceCollectionExtensions.cs +++ b/src/AzureOpenAIProxy.ApiApp/Extensions/ServiceCollectionExtensions.cs @@ -37,9 +37,9 @@ public static IServiceCollection AddKeyVaultService(this IServiceCollection serv throw new InvalidOperationException($"{nameof(KeyVaultSettings.VaultUri)} is not defined."); } - if (string.IsNullOrWhiteSpace(settings.SecretNames["OpenAI"]) == true) + if (string.IsNullOrWhiteSpace(settings.SecretNames[KeyVaultSecretNames.OpenAI]) == true) { - throw new InvalidOperationException($"{nameof(KeyVaultSettings.SecretNames)}.OpenAI is not defined."); + throw new InvalidOperationException($"{nameof(KeyVaultSettings.SecretNames)}.{KeyVaultSecretNames.OpenAI} is not defined."); } var client = new SecretClient(new Uri(settings.VaultUri), new DefaultAzureCredential()); @@ -154,7 +154,7 @@ public static IServiceCollection AddTableStorageService(this IServiceCollection var clientSecret = sp.GetService() ?? throw new InvalidOperationException($"{nameof(SecretClient)} service is not registered."); - var storageKeyVault = clientSecret.GetSecret(settings.SecretNames["Storage"]!); + var storageKeyVault = clientSecret.GetSecret(settings.SecretNames[KeyVaultSecretNames.Storage]!); var client = new TableServiceClient(storageKeyVault.Value.Value); diff --git a/src/AzureOpenAIProxy.ApiApp/KeyVaultSecretNames.cs b/src/AzureOpenAIProxy.ApiApp/KeyVaultSecretNames.cs new file mode 100644 index 00000000..a8778adb --- /dev/null +++ b/src/AzureOpenAIProxy.ApiApp/KeyVaultSecretNames.cs @@ -0,0 +1,18 @@ +namespace AzureOpenAIProxy.ApiApp; + +/// +/// This represents the keyvault secret names in appsettings.json +/// +public static class KeyVaultSecretNames +{ + /// + /// Keyvault secret name for OpenAI instance settings + /// + public const string OpenAI = "OpenAI"; + + /// + /// Keyvault secret name for table storage connection string + /// + public const string Storage = "Storage"; + +} \ No newline at end of file From f4e375c626e76c70bed885b9205fac8fd857e4a5 Mon Sep 17 00:00:00 2001 From: sikutisa Date: Thu, 12 Sep 2024 22:52:05 +0900 Subject: [PATCH 19/20] remove unnecessary using --- .../Extensions/ServiceCollectionExtensions.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/AzureOpenAIProxy.ApiApp/Extensions/ServiceCollectionExtensions.cs b/src/AzureOpenAIProxy.ApiApp/Extensions/ServiceCollectionExtensions.cs index 98dfc494..7800ad7c 100644 --- a/src/AzureOpenAIProxy.ApiApp/Extensions/ServiceCollectionExtensions.cs +++ b/src/AzureOpenAIProxy.ApiApp/Extensions/ServiceCollectionExtensions.cs @@ -1,5 +1,4 @@ -using Azure; -using Azure.Data.Tables; +using Azure.Data.Tables; using Azure.Identity; using Azure.Security.KeyVault.Secrets; From 6628d53a54ddc72b0b884359596c67db5df73c9d Mon Sep 17 00:00:00 2001 From: sikutisa Date: Thu, 12 Sep 2024 23:50:53 +0900 Subject: [PATCH 20/20] add validation for null or empty table storage secret name Related to: #300 --- .../Extensions/ServiceCollectionExtensions.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/AzureOpenAIProxy.ApiApp/Extensions/ServiceCollectionExtensions.cs b/src/AzureOpenAIProxy.ApiApp/Extensions/ServiceCollectionExtensions.cs index 7800ad7c..0f170517 100644 --- a/src/AzureOpenAIProxy.ApiApp/Extensions/ServiceCollectionExtensions.cs +++ b/src/AzureOpenAIProxy.ApiApp/Extensions/ServiceCollectionExtensions.cs @@ -153,6 +153,11 @@ public static IServiceCollection AddTableStorageService(this IServiceCollection var clientSecret = sp.GetService() ?? throw new InvalidOperationException($"{nameof(SecretClient)} service is not registered."); + if (string.IsNullOrWhiteSpace(settings.SecretNames[KeyVaultSecretNames.Storage]) == true) + { + throw new InvalidOperationException($"{nameof(KeyVaultSettings.SecretNames)}.{KeyVaultSecretNames.Storage} is not defined."); + } + var storageKeyVault = clientSecret.GetSecret(settings.SecretNames[KeyVaultSecretNames.Storage]!); var client = new TableServiceClient(storageKeyVault.Value.Value);