应用服务证书错误 "The parameter KeyVault Certificate has an invalid value"

Error "The parameter KeyVault Certificate has an invalid value" with App Service Certificate

我在我的 Azure Key Vault 中创建了一个秘密,其中包含从 .pfx 转换为 base64 字符串的 ssl 证书。现在我尝试使用它来创建链接到使用 bicep 文件的应用程序服务的证书。

resource kv 'Microsoft.KeyVault/vaults@2021-06-01-preview' = {
  name: 'mykeyvault'
  location: resourceGroup().location
  properties: {
    tenantId: tenantId
    sku: {
      name: 'standard'
      family: 'A'
    }   
    enabledForTemplateDeployment: true
    accessPolicies: [...]
  }
}

resource sslCertificateSecret 'Microsoft.KeyVault/vaults/secrets@2021-06-01-preview' = {
  name: '${kv.name}/sslcert'
  properties: {
    attributes: {
      enabled: true
    }
    value: <base64_string_ssl>
    contentType: 'application/x-pkcs12'
  }
}

resource appServicePlan 'Microsoft.Web/serverfarms@2021-01-15' = {
  name: 'myServiceplan'
  location: resourceGroup().location
  kind: 'linux'
  properties: {
    reserved: true
  }
  sku: {  
    name: 'B1'
  }
}

resource sslCertificate 'Microsoft.Web/certificates@2021-01-15' = {
  name: 'myCertificate'
  location: resourceGroup().location
  properties: {
    keyVaultId: <my_keyvaultId>
    keyVaultSecretName: <my_keyvaultCertificateSecretName>
    serverFarmId: appServicePlan.id
    
  }
}

我还尝试在密钥库中手动导入证书并重新导出它以确保 base64 字符串正确并且看起来没问题。

但是我收到错误消息“参数 KeyVault 证书具有无效值。”

你知道我错过了什么吗?

Azure KeyVault 作为机密信息安全存储的解决方案。 在 KeyVault 中对 Web 应用程序进行身份验证的两种方法。更好的方法是 使用证书 验证 Web 应用程序。此证书也直接从 KeyVault 部署。这意味着机密信息和金库钥匙都不会被泄露。

请检查以下步骤:

单击下面的 link 了解 使用来自 keyVault 的应用程序服务创建证书link的步骤. Loading the access certificate for your application into KeyVault


检查证书的文件格式,这是导入证书时的主要构建块

在 Azure Key Vault 中,支持的证书格式为 PFX 和 PEM。

• .pem 文件格式包含一个或多个 X509 证书文件。

• .pfx 文件格式是一种存档文件格式,用于在单个文件中存储多个加密对象,即服务器证书(为您的域颁发)、匹配的私钥,并且可以选择包括中间 CA。

应用服务使用的证书首先需要转换为(并标记为)application/x-pkcs12。使用 --password 参数从 pfx 文件重新导入证书 (az keyvault certificate import),然后从密钥中导入保险库到 webapp 可能会有所帮助。你可以参考这个博客可能会有帮助。

此外,查看 Cert 和 Key Vault 是否在其原始资源组中。

其他详细信息:https://docs.microsoft.com/en-us/azure/key-vault/certificates/tutorial-import-certificate

https://azure.github.io/AppService/2016/05/24/Deploying-Azure-Web-App-Certificate-through-Key-Vault.html


如果您在上传时错过了证书政策并且生成了新证书,请尝试在密钥保管库中生成。

$credential = Get-Credential

login-azurermaccount -Credential $credential
$vaultName = 'my-vault-full-of-keys'
$certificateName = 'my-new-cert'
$policy = New-AzureKeyVaultCertificatePolicy -SubjectName "CN=mememe.me" -IssuerName Self -ValidityInMonths 120
Add-AzureKeyVaultCertificate -VaultName $vaultName -Name $certificateName -CertificatePolicy $policy

"The parameter KeyVault Certificate has an invalid value"

  1. 请检查您是否已授予资源提供程序访问密钥保管库的权限

使用 powershell 启用“Microsoft.Web”资源提供程序直接访问 azure key Vault。

Login-AzureRmAccount 
Set-AzureRmContext -SubscriptionId AZURE_SUBSCRIPTION_ID 
Set-AzureRmKeyVaultAccessPolicy -VaultName KEY_VAULT_NAME -ServicePrincipalName abfa0a7c-a6b6-4736-8310-5855508787cd -PermissionsToSecrets get 
  1. 有时在将证书上传到KeyVault的步骤中存在这个问题:如果使用powershell,上传时给出证书的完整路径而不是相对路径。 $pfxFilePath = "PFX_CERTIFICATE_FILE_PATH" # 改变这个路径

示例:

$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