【问题标题】:SSL Issue - Unable to connect to multiple containers using docker-composeSSL 问题 - 无法使用 docker-compose 连接到多个容器
【发布时间】:2019-09-22 00:47:34
【问题描述】:

我正在使用 .Net 核心开始使用 Docker-Compose,但在容器之间通信时遇到了 SSL 握手问题。

堆栈 .Net 核心 2.2, 适用于 Windows 的 Docker CE(Linux 容器)2.0.0.3, 码头工人撰写 3.4, VS2019 更新 2, 视窗 10, Chrome 版本 74.0.3729.131(官方版本)(64 位)

我有一个 API 被另一个使用 HttpClient 的 API 调用。我已将它们添加到一个 docker-compose 项目中,并使用服务名称引用它们:

version: '3.4'

services:
  caller.api:
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - ASPNETCORE_URLS=https://+:80
      - ASPNETCORE_HTTPS_PORT=44391
      - FirstServiceUrl=https://callee.api
    depends_on:
      - callee.api
    ports:
      - "44391:80"
    volumes:
      - ${APPDATA}/ASP.NET/Https:/root/.aspnet/https:ro
      - ${APPDATA}/Microsoft/UserSecrets:/root/.microsoft/usersecrets:ro

  callee.api:
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - ASPNETCORE_URLS=https://+:80
      - ASPNETCORE_HTTPS_PORT=44392
    ports:
      - "44392:80"
    volumes:
      - ${APPDATA}/ASP.NET/Https:/root/.aspnet/https:ro
      - ${APPDATA}/Microsoft/UserSecrets:/root/.microsoft/usersecrets:ro


但是,这些调用者无法调用被调用者,并且根据以下情况,我会收到不同类型的错误。

我已经尝试了很多东西:

  1. dotnet dev-certs https 这显然允许在 docker-compose 之外运行的 api 在 SSL 上运行,并且在通信中没有任何问题

  2. 生成和使用自签名证书 我使用了here 的信息并创建了证书。

它们被添加到受信任的根目录中。我可以在证书管理器中看到它们。
在 API Main 中添加以下代码:

var builder = WebHost.CreateDefaultBuilder(args);
builder.ConfigureAppConfiguration((ctx, cfg) => cfg.AddEnvironmentVariables());

builder.UseKestrel(options =>
{
     options.AddServerHeader = false;
     options.ListenAnyIP(80, o =>
     {
          o.UseHttps( new X509Certificate2( "callee.api.pfx", "password"));
     });
})
.UseStartup<Startup>();

但是当我启动 Chrome(使用 swagger 进行测试)时,浏览器显示

我得到了这个例外:

      Failed to authenticate HTTPS connection.
System.Security.Authentication.AuthenticationException: Authentication failed, see inner exception. ---> Interop+OpenSsl+SslException: SSL Handshake failed with OpenSSL error - SSL_ERROR_SSL. ---> Interop+Crypto+OpenSslCryptographicException: error:14094416:SSL routines:ssl3_read_bytes:sslv3 alert certificate unknown
   --- End of inner exception stack trace ---
   at Interop.OpenSsl.DoSslHandshake(SafeSslHandle context, Byte[] recvBuf, Int32 recvOffset, Int32 recvCount, Byte[]& sendBuf, Int32& sendCount)
   at System.Net.Security.SslStreamPal.HandshakeInternal(SafeFreeCredentials credential, SafeDeleteContext& context, SecurityBuffer inputBuffer, SecurityBuffer outputBuffer, SslAuthenticationOptions sslAuthenticationOptions)
   --- End of inner exception stack trace ---

我首先尝试将证书创建为 localhost.pfx,但没有成功。 我也尝试使用证书 callee.pfx 但没有任何成功。

  1. 几种与上述 (2) 类似但没有乐趣的不同方法。

我能够在浏览器中访问各个 API,并且可以手动运行它们。

当我禁用 SSL 时,他们可以毫无问题地进行交互。

我花了几天的时间试图让它发挥作用。非常感谢任何帮助或指示。

【问题讨论】:

    标签: c# docker ssl docker-compose kestrel


    【解决方案1】:

    我实际上遇到了同样的问题,看起来调用者容器不信任被调用容器的证书。您是否尝试在调用者容器上运行以下命令?:

    dotnet dev-certs https
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-18
      • 2016-06-15
      相关资源
      最近更新 更多