当 ARM 模板托管在 Key Vault 中时,如何在我的 ARM 模板中定义证书资源?

How do I define a certificate resource in my ARM template when it is hosted in the Key Vault?

我正在尝试为我的资源组定义一个 ARM 模板。最终,我试图通过导航到门户中应用服务的 SSL 证书选项卡来复制我必须手动执行的操作。

我已将 PFX 文件上传到我的 KeyVault 的“机密”选项卡。我已授予 Get 访问全局 RM 服务主体的权限。

目前,这就是我的 Microsoft.Web/certificates 资源在我的模板中的样子。它只是被定义为资源组顶层的资源,而不是网站的子资源或类似的资源:

    {
        "type":"Microsoft.Web/certificates",
        "name": "signingCredentials",
        "location": "[parameters('region')]",
        "apiVersion": "2015-08-01",
        "properties": {
            "keyVaultId": "<My KeyVault ID>",
            "keyVaultSecretName": "<My Secret Name>"
        }
    }

当我尝试部署此模板时收到消息:

The parameter KeyVault Certificate has an invalid value

我找不到关于此参数的任何文档以及它的预期值。我假设它在资源的 properties 部分中丢失。到目前为止,我在该主题上发现的任何内容都只引用了 keyVaultIdkeyVaultSecretName

我做错了什么?我正在努力实现的目标是否可行?

The parameter KeyVault Certificate has an invalid value

看来这个问题不是你的模板引起的。我们可以参考this article来查看。从错误消息中,它告诉我证书名称不正确。我们可以使用 Get-AzureKeyVaultSecret 来获取它的名字。详情如下:

如上图所示,“kvcertificate”的值就是我们期望的值。

问题似乎不是由我的模板引起的,而是与证书通过 UI 上传到 KeyVault 的方式有关。 This article 为我提供了一个使用 powershell 将文件直接上传到 KeyVault 的脚本。

$pfxFilePath = "F:\KeyVault\PrivateCertificate.pfx"
$pwd = "[2+)t^BgfYZ2C0WAu__gw["
$flag = [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::Exportable 
$collection = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2Collection  
$collection.Import($pfxFilePath, $pwd, $flag) 
$pkcs12ContentType = [System.Security.Cryptography.X509Certificates.X509ContentType]::Pkcs12 
$clearBytes = $collection.Export($pkcs12ContentType) 
$fileContentEncoded = [System.Convert]::ToBase64String($clearBytes) 
$secret = ConvertTo-SecureString -String $fileContentEncoded -AsPlainText –Force 
$secretContentType = 'application/x-pkcs12' 
Set-AzureKeyVaultSecret -VaultName akurmitestvault -Name keyVaultCert -SecretValue $Secret -ContentType $secretContentType # Change the Key Vault name and secret name

使用 Jambor 回答中的 Get-AzureKeyVault 脚本,我看不出 UI 中上传的证书之间有任何区别。我什至将我上传的证书的内容类型从 Certificate 更改为 application/x-pcks2 但它仍然不起作用。看起来它可能是 UI 中的错误,或者只是 powershell 脚本处理它的方式不同。