Azure 在更新和颁发应用服务证书时下载旧证书

Azure downloads old certificate whilst the app service certificate has been renewed and issued

我一直潜伏在这里,但最后我有一个问题,我真的无法在任何地方回答或在网上找到。我发现人们在下载 .pfx 文件时遇到问题,但这里并非如此。

我正在尝试更新 Azure 应用程序网关中的应用服务证书,以便我的 SSL 继续工作。问题是这样的:

我试过通过 Powershell、Azure CLI、旧版 Azure CLI 下载它。但是唉...

** 编辑:** 我无法让它工作并为同一个通配符域创建了一个全新的证书。而且 - 令人惊讶,令人惊讶 - 现在 Azure 确实在密钥保管库中创建了新的秘密以用于此证书。问题仍然存在......当现有证书自动更新时,为什么它不这样做???

大概又是个简单的东西,就是看不出来。你们知道如何解决这个问题吗?

谢谢!

添加了一些证据:

Screenshot: certificate data

Screenshot: key vault secret date

记录脚本:

# Script for exporting pfx certificate from the Azure Cloud
#
# Type the following commands in PowerShell console to execute the script:
#   > Powershell –ExecutionPolicy Bypass
#   > .\copyasc.ps1
#

param (
    [string]$appServiceCertificateName = "Cert_name",
    [string]$azureLoginEmailId = "username@contoso.com"
)

$resourceGroupName = "RG_name"
$subscriptionId = "sub_id"
$exportFileName = "$appServiceCertificateName.pfx"

Login-AzureRmAccount
Set-AzureRmContext -SubscriptionId $subscriptionId

$ascResource = Get-AzureRmResource -ResourceName $appServiceCertificateName -ResourceGroupName $resourceGroupName -ResourceType "Microsoft.CertificateRegistration/certificateOrders" -ApiVersion "2015-08-01"
$keyVaultId = ""
$keyVaultSecretName = ""

$certificateProperties=Get-Member -InputObject $ascResource.Properties.certificates[0] -MemberType NoteProperty
$certificateName = $certificateProperties[0].Name
$keyVaultId = $ascResource.Properties.certificates[0].$certificateName.KeyVaultId
$keyVaultSecretName = $ascResource.Properties.certificates[0].$certificateName.KeyVaultSecretName

$keyVaultIdParts = $keyVaultId.Split("/")
$keyVaultName = $keyVaultIdParts[$keyVaultIdParts.Length - 1]
$keyVaultResourceGroupName = $keyVaultIdParts[$keyVaultIdParts.Length - 5]
Set-AzureRmKeyVaultAccessPolicy -ResourceGroupName $keyVaultResourceGroupName -VaultName $keyVaultName -UserPrincipalName $azureLoginEmailId -PermissionsToSecrets get
$secret = Get-AzureKeyVaultSecret -VaultName $keyVaultName -Name $keyVaultSecretName
$pfxCertObject=New-Object System.Security.Cryptography.X509Certificates.X509Certificate2 -ArgumentList @([Convert]::FromBase64String($secret.SecretValueText),"", [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::Exportable)
$pfxPassword = -join ((65..90) + (97..122) + (48..57) | Get-Random -Count 50 | % {[char]$_})
$currentDirectory = (Get-Location -PSProvider FileSystem).ProviderPath
[Environment]::CurrentDirectory = (Get-Location -PSProvider FileSystem).ProviderPath
New-Item $currentDirectory$exportFileName -ItemType file
[io.file]::WriteAllBytes(".$exportFileName", $pfxCertObject.Export([System.Security.Cryptography.X509Certificates.X509ContentType]::Pkcs12, $pfxPassword))
Write-Host "Created an App Service Certificate copy at: $currentDirectory$exportFileName"
Write-Warning "For security reasons, do not store the PFX password. Use it directly from the console as required."
Write-Host "PFX password: $pfxPassword"

根据我的测试,一个密文如果被续订,会有多个版本。

  1. 能否请您仔细检查一下当前版本的到期日期是否正确?

  2. 在PowerShell中,您可以检查版本是否正确:

$secret = Get-AzureKeyVaultSecret -VaultName $keyVaultName -Name $keyVaultSecretName
$secret.Version
$secret.Expires

如果以上都正确,那么导出的pfx应该也是正确的。如果没有,您可以删除现有的 pfx 文件,然后重试。

好吧...经过一番试验,我终于找到了解决办法。看来 Azure 本身没有足够的权限,这就是我解决它的方法:

  1. 创建一个新的应用服务证书并配置它。
  2. 配置后,Azure 似乎在 Azure Key Vault 的 'Access Policies' 中做了一些调整,添加了两个以前不存在的应用程序。这些应用程序有几个 'Secret permissions' See this screenshot for application names
  3. 当我去检查我的其他证书时,它们也得到了更新,我可以使用正确的密码再次下载 .pfx(使用我原来问题中的脚本)。 (请注意,您还需要在访问策略中为自己的帐户授予 'get' .pfx 文件的权限。)

这让我想到在密钥保管库访问策略中手动添加两个实体(应用程序)可能也足够了,但解决方法是创建一个新的应用服务证书并删除(或使用) 之后。