0

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

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 证书的值无效。”

你知道我错过了什么吗?

4

1 回答 1

0

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

请检查以下步骤:

单击下面的链接以了解从 keyVault 创建与应用服务链接的证书的步骤。 将应用程序的访问证书加载到 KeyVault


检查证书的文件格式,这是导入证书时的主要组成部分

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

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

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

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

此外,查看 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

“参数 KeyVault 证书的值无效”

  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

于 2021-09-16T11:06:53.203 回答