【问题标题】:Binding SSL certificate to App service using Azure CLI and Keyvault使用 Azure CLI 和 Keyvault 将 SSL 证书绑定到应用服务
【发布时间】:2019-10-10 14:39:12
【问题描述】:

我正在尝试使用 Azure CLI 配置存储在 Azure KeyVault 中的 Azure 应用服务 SSL 证书。我是 Azure CLI 的新手,无法找到执行此操作的完整示例代码集。我找到了各个命令的文档/示例,但是无法将它们链接在一起。绝对会感谢一些帮助/指导,因为我觉得这是一种常见的情况。

起初,我认为这将是一个简单的“链接”类型的命令。证书已经上传到 keyvault,所以 Azure 应用服务,去获取它们,这里是 $pfxPassword。

这似乎是不可能的。我发现了一些文档,您似乎需要从 keyvault 下载证书然后上传。

我花了一点时间才意识到您没有为此使用 az keyvault 证书...您需要使用 az secret 下载。

然后我找到了一些关于如何上传证书、获取指纹以及将证书绑定到应用服务的其他命令。

我将这三个命令链接在一起,但无法使其工作。

#download the cert
az keyvault secret download --file $fileName --vault-name $vaultName --name $certName;

#upload the cert and get the thumbprint
$thumbprint=az webapp config ssl upload --certificate-file $fileName --certificate-password $pfxPassword --name $site_name --resource-group $ResourceGroupName --query thumbprint --output tsv

#bind the uploaded cert to the app service.
az webapp config ssl bind --certificate-thumbprint $thumbprint --ssl-type SNI --name $site_name --resource-group $ResourceGroupName

我可以确认第一个命令正在下载证书。 (过了一会儿,我能够弄清楚将事情导入我的 win10 开发机器——即使证书是用密码上传到 keyvault 的,下载它们会删除密码。)。

不幸的是,看起来第二个命令(上传并获取指纹)需要密码。

执行此操作的“正确”方法是什么?

感谢您的指导/建议。

【问题讨论】:

  • 据我所知,当我们使用az keyvault secret download 下载pfx 文件时。该文件的密码为空。所以在运行az webapp config ssl upload命令时,请将certificate-password设置为null。
  • 你是如何设置这个 null 的?当我使用 $null、“”、无值或完全忽略参数时,我会收到错误。 az webapp config ssl upload: error: argument --certificate-password: expected one argument
  • 你可以吗?您还有其他顾虑吗?

标签: azure-web-app-service azure-keyvault azure-cli


【解决方案1】:

根据我的测试,当我们使用 Azure CLI 从 Azure Key Vault 下载证书为 pfx 文件时,它的密码为空。所以当我们使用 CLI 将 pfx 文件上传到 Azure web app 时,我们可以使用以下命令

az webapp config ssl upload --certificate-file "<pfx file name>"  --name "<web name>" --resource-group "<group name>"  --certificate-password "" --query thumbprint --output tsv
az login

# upload certificate to Azure key vault
az keyvault certificate import --file "E:\Cert\P2SChildCert.pfx" --password "" --name "test1234" --vault-name "testkey08"

# download certificate as pfx file
az keyvault secret download --file "test2.pfx" --vault-name "testkey08" --name "test1234" --encoding base64

# upload the pfx file to Azue web app
az webapp config ssl upload --certificate-file "test2.pfx"  --name "andywebsite" --resource-group "andywebbot"  --certificate-password "" --query thumbprint --output tsv

此外,如果您的证书已存储在 Azure 密钥保管库中,我们可以通过 Azure 门户直接将其导入 Azure Web 应用程序。

【讨论】:

  • @ccarlton 你可以吗?您还有其他顾虑吗?
  • 我的任务是尝试通过脚本完成此操作,以供将来的 CI/CD 活动使用(因此使用门户已失效)。我实际上收到了一个错误。az webapp config ssl upload: error: argument --certificate-password: expected one argument 当我尝试将 "" 传递给 --certificate-password 参数
  • @ccarlton 你试过使用''吗?如果还是不行,建议你使用ARM模板或者powershell。关于ARM template,我们可以直接引用Azure Key Vault证书。关于powershell,我们可以dowlaod pfx file with a password.
  • 不,它不起作用!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-28
  • 1970-01-01
  • 1970-01-01
  • 2021-11-01
  • 2023-01-21
  • 2018-04-24
相关资源
最近更新 更多