不能多次设置密钥存储提供程序(始终使用 Azure 函数进行加密)

Key store providers cannot be set more than once (Always Encryption using Azure Function )

我已经实现了一个 TimeTriggerC# Azure 函数来 运行 一个存储过程来插入一个 table 使用列加密加密的 table(始终加密,AzureKey Vault 作为提供者)

我的阻塞区域是我的函数 运行 成功了 1 次,然后又多次出错。所以我得到了成功的路径作为一个罕见的案例

我遇到的错误是

mscorlib: Exception has been thrown by the target of an invocation. System.Data: Key store providers cannot be set more than once.

我对此做了一些调查 它发生在

SqlConnection.RegisterColumnEncryptionKeyStoreProviders(providers);

函数中有

static void InitializeAzureKeyVaultProvider()
{
    string clientId = ConfigurationManager.AppSettings["AuthClientId"];
    string clientSecret = ConfigurationManager.AppSettings["AuthClientSecret"];
    _clientCredential = new ClientCredential(clientId, clientSecret);

    // Direct the provider to the authentication delegate
    SqlColumnEncryptionAzureKeyVaultProvider azureKeyVaultProvider = new SqlColumnEncryptionAzureKeyVaultProvider(GetToken);

    Dictionary<string, SqlColumnEncryptionKeyStoreProvider> providers = new Dictionary<string, SqlColumnEncryptionKeyStoreProvider>();
    providers.Add(SqlColumnEncryptionAzureKeyVaultProvider.ProviderName, azureKeyVaultProvider);

    // register the provider with ADO.net
    SqlConnection.RegisterColumnEncryptionKeyStoreProviders(providers);
}

当我试图更深入地了解错误的根本原因时,它会像

Exception has been thrown by the target of an invocation. ---> System.InvalidOperationException : Key store providers cannot be set more than once. at System.Data.SqlClient.SqlConnection.RegisterColumnEncryptionKeyStoreProviders(IDictionary`2 customProviders)

正如我上面提到的,我使用 Azure Key Vault 作为我的密钥库 Provider.I 使用 AD 正确注册了我的 Keyvault 并将注册的应用程序添加到我的密钥库(访问政策)。

为什么我会遇到这种异常行为,比如一次成功一次失败?

感谢您的回复,
贾恩德运行

您可能 运行 InitializeAzureKeyVaultProvider() 在每个函数调用中。如果是第一次工作,当没有对这个 App 实例执行初始化时。

在下一次函数调用期间,实例被重用,因此初始化已经执行。但是你再调用一次,就出现了重复注册的错误。

要解决它,在静态构造函数中进行初始化,或者存储一个标志(static bool)是否已经初始化,如果是则跳过初始化(并注意线程安全)。

我遇到了由进入事件中心的消息触发的 Azure 函数的相同问题。

我的解决方案可能不是 100% 正确,但我不确定在令牌(来自 GetToken)过期后是否会影响对提供商的注册。因此,直到我第一次调用需要 Azure Key Vault / Always Encrypted 的 SQL 查询时,我才进行注册。

try
{
   cmd.ExecuteNonQuery();
}
catch (System.ArgumentException ex)
{
   if (ex.TargetSite.Name == "DecryptSymmetricKey" && ex.InnerException == null)
   {
                    InitializeAzureKeyVaultProvider() //Register the Provider
                    cmd.ExecuteNonQuery(); // Try the query again.
   }
}