【发布时间】:2019-10-29 22:19:09
【问题描述】:
我正在 Azure 函数的应用设置中设置到服务总线的连接字符串。目前,我将整个连接字符串存储在 Key Vault 中,并在应用设置中引用 Key Vault 机密。这工作正常。但是我没有成功的尝试是仅将服务总线密钥而不是整个连接字符串存储在密钥库中。
我已尝试将连接字符串连接到门户中的 KeyVault 参考应用程序设置,如下所示
Endpoint=sb://xxxxx.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=@Microsoft.KeyVault(SecretUri=xxxx.vault.azure.net/secrets/yyyy/zzzzz)
但这不起作用。
我需要进行这种分离的原因是我想轮换密钥保管库中的密钥,如果整个连接字符串都存储在密钥保管库中,我就无法这样做。
更新1:
将连接字符串拆分为多个应用程序设置键可以解决此问题,但它会限制我使用服务总线触发天蓝色函数的能力,这需要在运行中的应用程序设置中使用完整连接字符串键的名称方法签名如下
public static void Run(
[ServiceBusTrigger("myqueue", AccessRights.Manage, Connection = "ServiceBusConnection")]
string myQueueItem
ILogger log)
更新 2:
到目前为止我已经完成的工作,我希望我可以做一个更清洁的方法是在我的自动化 Powershell 中使用正则表达式来仅替换连接字符串的 SharedAccessKey 部分。这样,我只对连接字符串使用一个应用程序设置。它正在工作,但我对此感到不舒服。
这是我在 Automation Runbook 中使用的代码:
$azureAutomationConnectionName = "AzureRunAsConnection"
$servicePrincipalConnection = Get-AutomationConnection -Name $azureAutomationConnectionName
Add-AzureRmAccount -ServicePrincipal -TenantId $servicePrincipalConnection.TenantId -ApplicationId $servicePrincipalConnection.ApplicationId -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
$resourceGroupName = 'XXXX'
$serviceBusName = 'XXXX'
$serviceBusAccessPolicyName = 'RootManageSharedAccessKey'
$keyVaultName = 'XXXX'
$keyVaultSecretKey = 'XXXX'
$currentSecret = (Get-AzureKeyVaultSecret -VaultName $keyVaultName -Name $keyVaultSecretKey).SecretValueText
# Regenerate the Service Bus Primary Key
New-AzureRmServiceBusKey -ResourceGroupName $resourceGroupName -Namespace $serviceBusName -Name $serviceBusAccessPolicyName -RegenerateKey PrimaryKey
# Get the newly regenerated Primary Key
$newPrimaryKey = (Get-AzureRmServiceBusKey -ResourceGroupName $resourceGroupName -Namespace $serviceBusName -Name $serviceBusAccessPolicyName).PrimaryKey
# The secret is storing the entire connection string. We want to replace the SharedAccessKey Only
$newSecretStr = $currentSecret -replace 'SharedAccessKey=[^;]*', ([string]::Format('SharedAccessKey={0}',$newPrimaryKey))
# Convert the Primary Key to Secure String
$newSecureSecretStr = ConvertTo-SecureString $newSecretStr -AsPlainText -Force
# Update the Secret Value in the Key Vault
Set-AzureKeyVaultSecret -VaultName $keyVaultName -Name $keyVaultSecretKey -SecretValue $newSecureSecretStr
【问题讨论】:
-
我认为你需要 2 使用 2 个不同的环境变量
标签: azure azure-keyvault appsettings