Azure WebJob中动态更新SQL Always Encrypted列配置失败排查
问题:Azure WebJob中PowerShell脚本更新SQL Always Encrypted列配置失败
我正尝试创建可在Azure WebJob中运行的PowerShell脚本(或模板),用于动态更新SQL Always Encrypted的列配置。例如,当表[Table1]新增需要加密的[Col1] NVARCHAR(1) NULL列时执行更新。
尝试1:使用托管身份认证
WebJob的App Service主机已配置托管身份,该身份具备数据库访问和修改权限。脚本如下:
$env:PSModulePath += ";<webjob_dir>" Import-Module Az.Accounts #2.12.1 Import-Module SqlServer #22.0.59 # Set up SQL connection and database object $smoDatabase = Get-SqlDatabase -ConnectionString "<connection_string>" # Authentication Connect-AzAccount -Identity # Change encryption schema $encryptionChanges = @() $encryptionChanges += New-SqlColumnEncryptionSettings -ColumnName dbo.Table1.Col1 -EncryptionType Deterministic -EncryptionKey "<encryption_key>" # Apply changes Set-SqlColumnEncryption -ColumnEncryptionSettings $encryptionChanges -InputObject $smoDatabase
尝试2:使用Azure AD应用认证
配置了具备数据库访问和修改权限的Azure AD应用,本地运行时先执行Connect-AzAccount -Tenant <tenant_id>即可成功,脚本如下:
$env:PSModulePath += ";<webjob_dir>" Import-Module Az.Accounts #2.12.1 Import-Module SqlServer #22.0.59 # Set up SQL connection and database object $smoDatabase = Get-SqlDatabase -ConnectionString "<connection_string>" # Authentication Add-SqlAzureAuthenticationContext -ClientID <client_id> -Secret '<secret>' -Tenant <tenant_id> # Change encryption schema $encryptionChanges = @() $encryptionChanges += New-SqlColumnEncryptionSettings -ColumnName dbo.Table1.Col1 -EncryptionType Deterministic -EncryptionKey "<encryption_key>" # Apply changes Set-SqlColumnEncryption -ColumnEncryptionSettings $encryptionChanges -InputObject $smoDatabase
错误提示
Method not found: 'Void Microsoft.Data.SqlClient.AlwaysEncrypted.AzureKeyVaultProvider.SqlColumnEncryptionAzureKeyVaultProvider..ctor(Azure.Core.TokenCredential)'.
问题分析与解决方案
你的两种方案整体思路可行,问题出在模块版本兼容性上:
这个错误是因为SqlServer模块(22.0.59)依赖的Microsoft.Data.SqlClient.AlwaysEncrypted.AzureKeyVaultProvider组件版本,与WebJob环境中已存在的Azure SDK相关组件(如Azure.Core)版本不匹配,导致找不到对应的构造函数。
解决步骤:
- 统一模块依赖版本:升级SqlServer模块到22.1.1及以上版本,同时确保Az.Accounts模块版本与SqlServer模块兼容(例如Az.Accounts 2.13.0搭配SqlServer 22.1.1)。
- 调整认证逻辑:对于托管身份方案,无需单独调用
Connect-AzAccount -Identity,直接在连接字符串中指定Azure AD托管身份认证,修改后的脚本示例:$env:PSModulePath += ";<webjob_dir>\Modules" # 指定兼容的模块版本 Import-Module Az.Accounts -RequiredVersion 2.13.0 Import-Module SqlServer -RequiredVersion 22.1.1 # 使用托管身份的连接字符串 $connectionString = "Server=tcp:<server_name>.database.windows.net,1433;Database=<db_name>;Authentication=Active Directory Managed Identity;" $smoDatabase = Get-SqlDatabase -ConnectionString $connectionString $encryptionChanges = @() $encryptionChanges += New-SqlColumnEncryptionSettings -ColumnName dbo.Table1.Col1 -EncryptionType Deterministic -EncryptionKey "<encryption_key>" Set-SqlColumnEncryption -ColumnEncryptionSettings $encryptionChanges -InputObject $smoDatabase - 打包依赖模块到WebJob:使用
Save-Module命令将所需模块保存到WebJob目录的Modules文件夹,确保部署时包含所有依赖文件,避免环境版本冲突:Save-Module -Name Az.Accounts, SqlServer -Path "<webjob_dir>\Modules" -RequiredVersion 2.13.0,22.1.1 - 验证密钥权限:确保托管身份或Azure AD应用拥有Azure Key Vault的Get、WrapKey、UnwrapKey权限,这是Always Encrypted操作加密密钥的必要权限。
内容的提问来源于stack exchange,提问作者psand2286
相关产品推荐
相关产品推荐

