【发布时间】:2021-11-28 00:42:03
【问题描述】:
我创建并发布(到 Azure)一个工作且相当简单的 Azure 函数应用程序(HTTP 触发器),它在通过用户身份(如果在本地测试)或托管身份验证后将从 API 调用检索到的数据插入到 Azure SQL 数据库身份(在 VM 上运行时)。
一切正常,但是,我现在需要加密我使用 SSMS 完成的 SQL 表列之一。据我了解,下一步是验证提供程序以通过 Azure Key Vault 访问 CMK(我关注 this Microsoft guide)。
我想知道如何在以下代码中初始化AzureKeyVaultProvider,而不使用 Azure 应用注册资源中的 applicationId 和 clientKey,而是使用用户或托管标识角色。或者,如果有任何其他方式来获取/使用 applicationId 和 clientKey 而无需创建/使用 Azure 应用注册资源。
是否有更新/更简单的方法来访问 Azure Key Vault for Always Encrypted Columns sql 查询?
static void InitializeAzureKeyVaultProvider() {
_clientCredential = new ClientCredential(applicationId, clientKey);
SqlColumnEncryptionAzureKeyVaultProvider azureKeyVaultProvider = new SqlColumnEncryptionAzureKeyVaultProvider(GetToken);
Dictionary<string, SqlColumnEncryptionKeyStoreProvider> providers = new Dictionary<string, SqlColumnEncryptionKeyStoreProvider>();
providers.Add(SqlColumnEncryptionAzureKeyVaultProvider.ProviderName, azureKeyVaultProvider);
SqlConnection.RegisterColumnEncryptionKeyStoreProviders(providers);
}
}
这是我一直在尝试的另一种方式,但是,在安装和使用 Microsoft.Data.SqlClient.AlwaysEncrypted.AzureKeyVaultProvider 时,我收到以下代码示例的错误:
private static bool EncryptionProviderInitialized = false;
private static void InitializeAzureKeyVaultProvider()
{
if (!EncryptionProviderInitialized)
{
SqlColumnEncryptionAzureKeyVaultProvider akvProvider = null;
#if DEBUG
if (Debugger.IsAttached)
{
Console.WriteLine("Debugger attached - configuring KeyVaultProvider via VisualStudioCredential");
akvProvider = new SqlColumnEncryptionAzureKeyVaultProvider(new VisualStudioCredential());
}
if (akvProvider == null)
#endif
akvProvider = new SqlColumnEncryptionAzureKeyVaultProvider(new ManagedIdentityCredential());
SqlConnection.RegisterColumnEncryptionKeyStoreProviders(customProviders: new Dictionary<string, SqlColumnEncryptionKeyStoreProvider>(capacity: 1, comparer: StringComparer.OrdinalIgnoreCase)
{
{ SqlColumnEncryptionAzureKeyVaultProvider.ProviderName, akvProvider}
});
EncryptionProviderInitialized = true;
}
}
ERROR:
[2021-10-08T01:36:24.209Z] Executed 'EncryptedSQLMigration' (Failed, Id=1323dcbb-e671-4ed4-8a7c-6259447326c5, Duration=537ms)
[2021-10-08T01:36:24.209Z] System.Private.CoreLib: Exception while executing function: EncryptedSQLMigration. FreshFirstApproach: Method not found: 'Microsoft.Extensions.Primitives.StringValues Microsoft.AspNetCore.Http.IQueryCollection.get_Item(System.String)'.
最初,我在使用 Microsoft.Extensions.Logging.Abstractions 时遇到了同样的错误 - 从我的主要功能中删除 ILogger 只是为了继续前进,因为我也无法解决该问题,我现在得到这个 Microsoft.Extensions 异常。
非常感谢我在 Azure Function App 和 Azure Key Vault 中使用 Always Encrypted Columns 的目标!
非常感谢。
【问题讨论】:
标签: azure azure-functions azure-sql-database azure-keyvault always-encrypted