c# - SSL 问题 - 无法使用 docker-compose 连接到多个容器

标签 c# docker ssl docker-compose kestrel

我从使用 .Net 核心的 Docker-Compose 开始,并且在容器之间进行通信时遇到了 SSL 握手问题。

堆栈
.Net 核心 2.2,
适用于 Windows 的 Docker CE(Linux 容器)2.0.0.3,
docker 撰写 3.4,
VS2019 更新 2,
window 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



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

我尝试了很多事情:
  • dotnet 开发证书 https
    这显然允许在 docker-compose 之外运行的 api 在 SSL 上运行,并且在通信中没有任何问题
  • 生成和使用自签名证书
    我用的资料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 进行测试)时,浏览器显示 Not Secure Error

    我得到这个异常(exception):

          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 但没有任何成功。
  • 几个类似于上面(2)的不同方法,但没有乐趣。

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

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

    我花了几天时间试图让这个工作。非常感谢任何帮助或指示。

    最佳答案

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

    dotnet dev-certs https
    

    关于c# - SSL 问题 - 无法使用 docker-compose 连接到多个容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55970362/

    相关文章:

    apache - 无法从我的本地主机访问 docker 上的 apache

    linux - 在 RHEL 上安装 Docker 时出现错误

    ruby-on-rails - 使用 ngninx 运行 errbit,使用 ssl 运行 passenger

    java - trustAnchors 参数必须是非空的

    C# 运算符 'is' 参数

    c# - Entity Framework 不声明 datetime.now 为空

    c# - 如何在 IntelliSense 中按类型过滤?

    docker进程未在后台运行

    c# - 使用初始化 block 不好吗

    haskell - 带有 Keter 和 Cloudflare 的免费 SSL 的 Yesod 网络应用程序