【问题标题】:Always Encrypted Column with Azure Function App & Azure Key Vault使用 Azure Function App 和 Azure Key Vault 始终加密列
【发布时间】: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


    【解决方案1】:

    如果不创建或使用 Azure 应用注册资源,则无法使用 applicationIdclientKey。还有一种替代方法,您可以传递clientIdclientSecret,如下所示,但在这里您也需要申请注册

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

    对于用户或托管标识,如果您选中此 document,您将不会在支持托管资源的服务列表中找到 Azure Key Vault。

    因此,应为 Azure Function 而不是 Azure Key Vault 创建托管服务标识。查看此Retrieve Azure Key Vault Secrets using Azure Functions and Managed Service Identity 文档了解更多信息。

    【讨论】:

      猜你喜欢
      • 2021-11-28
      • 1970-01-01
      • 2020-09-09
      • 2020-02-11
      • 2021-01-11
      • 1970-01-01
      • 2022-01-06
      • 2018-12-08
      • 1970-01-01
      相关资源
      最近更新 更多