在 Windows 上使用 Azure CLI 发出证书链中的自签名证书

Self signed certificate in certificate chain issue using Azure CLI on Windows

我在配置 Windows 以使用 az 命令行工具时遇到了一些问题。我测试了多种配置。一个在本地安装的系统上,另一个带有基于 windows 的 docker 容器。我在两个系统上都遇到了同样的错误。

如果我发出以下命令:

  az login --tenant my-domain.org

我收到以下错误:

HTTPSConnectionPool(host='login.microsoftonline.com', port=443): Max retries exceeded with url: /my-domain.org/.well-known/openid-configuration (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1125)')))

容器具有以下 azopenssl 版本:

PS C:\azp> az version
{
  "azure-cli": "2.28.0",
  "azure-cli-core": "2.28.0",
  "azure-cli-telemetry": "1.0.6",
  "extensions": {}
}
PS C:\azp> openssl version
OpenSSL 1.1.1k  25 Mar 2021

本地系统有以下azopenssl版本:

(base) PS C:_Dev\dockerdevimage> az version

{
  "azure-cli": "2.26.1",
  "azure-cli-core": "2.26.1",
  "azure-cli-telemetry": "1.0.6",
  "extensions": {}
}
(base) PS C:_Dev\dockerdevimage> openssl version

OpenSSL 1.1.1c  28 May 2019

我试图理解为什么会出现错误,所以我测试了与 openssl 的连接,如下所示:

PS C:\azp> openssl s_client -proxy 10.76.209.147:3128 -connect login.microsoftonline.com:443 -showcerts
CONNECTED(00000180)
Can't use SSL_get_servername
depth=2 DC = org, DC = my-domain, CN = PKI, CN = BB-CA-DD   <-- edited manually
verify error:num=19:self signed certificate in certificate chain
verify return:1

我还使用相同的代理服务器和 Linux 容器进行了测试,并且 az 命令按预期工作:


$ az version
{
  "azure-cli": "2.25.0",
  "azure-cli-core": "2.25.0",
  "azure-cli-telemetry": "1.0.6",
  "extensions": {}
}

$ openssl version                                                                             
OpenSSL 1.1.1f  31 Mar 2020

$ az login --tenant my-domain.org 
The default web browser has been opened at https://login.microsoftonline.com/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/oauth2/authorize. Please continue the login in the web browser. If no web browser is available or if the web browser fails to open, use device code flow with `az login --use-device-code`.
You have logged in. Now let us find all the subscriptions to which you have access...
[
  {
    "cloudName": "AzureCloud",
...

在Linux容器上openssl命令returns输出如下:

$ openssl s_client -proxy 10.76.209.147:3128 -connect login.microsoftonline.com:443 -showcerts
Can't use SSL_get_servername
depth=2 DC = org, DC = my-domain, CN = PKI, CN = BB-CA-DD
verify return:1

我还根据this link使用以下命令导入了证书:

PS C:\azp> Import-Certificate -FilePath .\BB-CA-DD.crt -CertStoreLocation Cert:\LocalMachine\Root\

没有变化。我不确定如何进行。

这个问题可能与以下帖子和文章有关:

编辑:

我已将解决方案从此处移至“答案”部分,以强调我的问题已解决。根据反应,我得出结论,它确实对其他人也有用。

最后我解决了如下问题:

我找到了以下文档:

Setting up certificates for Azure CLI on Azure Stack Development Kit

基本思路是找到用于Azure CLI的python安装并更新相关证书文件。

在我的例子中,Azure CLI 与 python 一起安装在以下位置:

C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\python.exe

并使用建议的命令返回如下:

PS > & "C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\python.exe" -c "import certifi; print(certifi.where())"
C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\lib\site-packages\certifi\cacert.pem

更新上述文件解决了我的 az login 问题。 my-domain.org 提供的 python 安装之一包含正确配置的 cacert.pem 文件。

您可以使用以下方法

您的 azure CLI 正在此位置查找证书(如果使用 Windows)

默认证书授权包 Windows C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\Lib\site-packages\certifi\cacert.pem

下载您的 Azure 门户证书 (portal.azure.com)

将证书附加到上面的 cacert.pem 文件 并在重新启动 powershell 后再次尝试 Az 登录。

或者

如果您通过代理服务器使用 Azure CLI,可能会导致以下错误:SSLError("bad handshake: Error([('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')],)),).要解决此错误,请将环境变量 REQUESTS_CA_BUNDLE 设置为 PEM 格式的证书颁发机构捆绑证书文件的路径。

将代理服务器的证书附加到此文件或将内容复制到另一个证书文件,然后将其设置为REQUESTS_CA_BUNDLE。您可能还需要设置 HTTP_PROXY 或 HTTPS_PROXY 环境变量。

Link to Ms Docs Solution