ARM 模板中的新 AzureRmVpnClientRootCertificate

New-AzureRmVpnClientRootCertificate in ARM Template

如何使用 Azure ARM 模板而不是使用 Powershell 命令 New-AzureRmVpnClientRootCertificate 上传客户端根证书?我在模板中拥有整个环境,这是唯一缺少的东西。

在 VM 的 OSProfile 部分,您可以输入:

"secrets": [
              {
                "sourceVault": {
                  "id": "[parameters('sourceVaultValue')]"
                },
                "vaultCertificates": [
                  {
                    "certificateStore": "[parameters('certificateStoreValue')]",
                    "certificateUrl": "[parameters('certificateUrlValue')]"
                  }
                ]
              }

其中源库值为“/subscriptions/subId/resourceGroups/RGName/providers/Microsoft.KeyVault/vaults/KVName”

证书存储值是我的或您要使用的任何存储。

cert URL 值为“https://KVName.vault.azure.net:443/secrets/CertName/certVersionNumber

所以您显然需要创建一个 KeyVault 并将您的证书作为秘密添加到其中。您通过对证书进行 base 64 编码并上传该字符串来执行此操作,如下所示:

$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)

$jsonObject = @{
    data = $filecontentencoded
    dataType = 'pfx'
 } | ConvertTo-Json

 $jsonObjectBytes = [System.Text.Encoding]::UTF8.GetBytes($jsonObject)
 $jsonEncoded = [System.Convert]::ToBase64String($jsonObjectBytes)
 $secret = ConvertTo-SecureString -String $jsonEncoded -AsPlainText -Force

 return $secret

其中 $pfxFilePath 和 $pwd 是证书的路径,pwd 是密码。

然后使用 Set-AzureKeyVaultSecret -SecretValue $secret(和其他参数)上传