由于对 Nuget 的 SSL 身份验证,docker linux 容器中的 .NET Core 构建失败

far*_*332 7 linux containers nuget docker .net-core

我得到了一个在 Linux Docker 容器中运行的 .NET Core 项目来进行构建,在 docker 配置方面似乎一切正常,但是当我运行以下命令时:dotnet publish -c Release -o out,我收到下面的 SSL 身份验证错误。

The SSL connection could not be established, see inner exception. Authentication failed because the remote party has closed the transport stream.

我做了我的研究,显然我失踪了:

  1. ASPNET 的环境变量 Kestrel(根据https://github.com/aspnet/AspNetCore.Docs/issues/6199),我将其添加到我docker-compose的 .

  2. 开发人员 .pfx 证书,因此我docker-compose使用 Kestrel 路径更新了证书文件,如下所示。

version: '3'
services:
  netcore:
    container_name: test_alerting_comp
    tty: true
    stdin_open: true
    image: alerting_netcore
    environment:
      - http_proxy=http://someproxy:8080
      - https_proxy=http://someproxy:8080
      - ASPNETCORE_ENVIRONMENT=Development
      - ASPNETCORE_URLS=https://+;http://+
      - ASPNETCORE_HTTPS_PORT=443
      - ASPNETCORE_Kestrel__Certificates__Default__Password="ABC" 
      - ASPNETCORE_Kestrel__Certificates__Default__Path=/root/.dotnet/corefx/cryptography/x509stores/my
    ports:
      - "8080:80"
      - "443:443"  
    build: .
      #context: .
    security_opt:
      - seccomp:unconfined
    volumes:
      - "c:/FakePath/git/my_project/src:/app"
      - "c:/TEMP/nuget:/root"
    networks:
      - net
networks:
  net:
Run Code Online (Sandbox Code Playgroud)

我重新运行 docker 容器并dotnet publish -c Release -o out以相同的结果执行。

从我的主机,我可以对本地 NuGet 执行此操作:

A)wget https://nuget.local.com/api/v2没有问题,

B)但从容器中我不能。

C)但是,从容器中,我可以对官方 NuGet 执行此操作wget https://api.nuget.org/v3/index.json,因此我的代理可以正常工作。

调试 SSL 问题:

给定的 .pfx 证书是一个自签名证书,它在 Windows 操作系统上运行正常(至少我被告知)。

  1. strace 向我展示了从哪里提取证书,如下所示

root@9b98d5447904:/app# strace wget https://nuget.local.com/api/v2 |& grep certs open("/etc/ssl/certs/ca-certificates.crt", O_RDONLY) = 3

  1. 我按如下方式导出了 .pfx: openssl pkcs12 -in ADPRootCertificate.pfx -out my_adp_dev.crt然后将其移至/usr/local/share/ca-certificates/,删除了私有部分,仅保留在执行的文件 public 部分 ( -----BEGIN CERTIFICATE----- -----END CERTIFICATE----- ) 中update-ca-certificates,我可以看到1 added,仔细检查文件/etc/ssl/certs/ca-certificates.crt,新证书就在那里。

  2. 再次执行此操作wget https://nuget.local.com/api/v2并失败。

  3. 我使用 OpenSSL 来获取更多信息,如您所见,它不起作用,证书有一个奇怪的 CN,因为他们使用了通配符作为主题,对我来说这是错误的,但他们声明 .pfx 在 Windows 操作系统中工作.

root@ce21098e9643:/usr/local/share/ca-certificates# openssl s_client -connect nuget.local.com:443 -CApath /etc/ssl/certs
CONNECTED(00000003)
depth=0 CN = *.local.com
verify error:num=20:unable to get local issuer certificate
verify return:1
depth=0 CN = *.local.com
verify error:num=21:unable to verify the first certificate
verify return:1
---
Certificate chain
 0 s:/CN=\x00*\x00l\x00o\x00c\x00a\x00l\x00.\x00c\x00o\x00m
   i:/C=ES/ST=SomeCity/L=SomeCity/OU=DEV/O=ASD/CN=Development CA
---
Server certificate
-----BEGIN CERTIFICATE-----
XXXXXXXXXXX
XXXXXXXXXXX
-----END CERTIFICATE-----
subject=s:/CN=\x00*\x00l\x00o\x00c\x00a\x00l\x00.\x00c\x00o\x00m
issuer=i:/C=ES/ST=SomeCity/L=SomeCity/OU=DEV/O=ASD/CN=Development CA
---
No client certificate CA names sent
Peer signing digest: SHA1
Server Temp Key: ECDH, P-256, 256 bits
---
SSL handshake has read 1284 bytes and written 358 bytes
Verification error: unable to verify the first certificate
---
New, TLSv1.2, Cipher is ECDHE-RSA-AES256-SHA384
Server public key is 1024 bit
Secure Renegotiation IS supported
Compression: NONE
Expansion: NONE
No ALPN negotiated
SSL-Session:
    Protocol  : TLSv1.2
    Cipher    : ECDHE-RSA-AES256-SHA384
    Session-ID: 95410000753146AAE1D313E8538972244C7B79A60DAF3AA14206417490E703F3
    Session-ID-ctx:
    Master-Key: B09214XXXXXXX0007D126D24D306BB763673EC52XXXXXXB153D310B22C341200EF013BC991XXXXXXX888C08A954265623
    PSK identity: None
    PSK identity hint: None
    SRP username: None
    Start Time: 1558993408
    Timeout   : 7200 (sec)
    Verify return code: 21 (unable to verify the first certificate)
    Extended master secret: yes
---
Run Code Online (Sandbox Code Playgroud)

我不知道我面临什么问题,但似乎是:

A) 自签名 .pfx 配置错误,现在它在 Linux 中使用,它不能正常工作。

B)我需要在容器中进行更多配置,这是我不知道的。

我还应该做什么?

我正在考虑可能创建其他证书以从 Linux 主机使用。

使用 OpenSSL 为 IIS ver 8 创建另一个自签名证书并将其导入 IIS 是否可行?。

欢迎任何想法,干杯。

far*_*332 5

回答我自己

这不是 Linux 容器问题,而是Web 服务器 (IIS) 中的证书问题,因为我们使用自签名证书,这样证书将始终是invalid certificate. 自签名证书在 Windows 操作系统端工作正常,与无效错误无关。当然,自签名证书仅适用于测试环境等。

在 Linux 操作系统中,当您尝试从 NuGet 提取软件包时,您将收到以下错误,因为:

1) 证书确实无效,并且

2) 因为显然没有选项可以忽略 Linux 端的无效证书。

The SSL connection could not be established, see inner exception.
The remote certificate is invalid according to the validation procedure.
Run Code Online (Sandbox Code Playgroud)

解决方案是您在企业环境中工作,向系统管理员请求正确的签名证书,为此您从 Web 服务器(在我的例子中是 IIS)生成 CSR,然后将其传递给他们,以便他们将您发送回来要安装在该 Web 服务器中的 .cer 文件。

我试图做但由于我的公司环境的限制而无法做的另一个选择是创建一个假 CA(使用 OpenSSL),然后您自己签署 CSR 以获得一些用于您的开发或测试的有效证书环境。

为自己回答这个问题而道歉,但我相信值得分享我的发现。

希望能帮助到你。