【问题标题】:How can I help Vapor successfully SSL-handshake my PostgreSQL server?如何帮助 Vapor 成功地 SSL 握手我的 PostgreSQL 服务器?
【发布时间】:2020-03-20 05:32:56
【问题描述】:

我在 Ubuntu 服务器上使用 Vapor 连接到我的 DigitalOcean 管理的 PostgreSQL 数据库。

从命令行,运行以下工作正常:

psql postgresql://user:password@host:port/dbname?sslmode=require

但是用下面的代码运行等价的代码给了我:

Fatal error: Error raised at top level: NIOOpenSSL.NIOOpenSSLError.handshakeFailed(NIOOpenSSL.OpenSSLError.sslError([Error: 337047686 error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed])): file /home/buildnode/jenkins/workspace/oss-swift-5.1-package-linux-ubuntu-18_04/swift/stdlib/public/core/ErrorType.swift, line 200

代码如下:

    let postgres = PostgreSQLDatabase(config: PostgreSQLDatabaseConfig(
        hostname: Environment.get("POSTGRESQL_HOSTNAME")!,
        port: Int(Environment.get("POSTGRESQL_PORT")!)!,
        username: Environment.get("POSTGRESQL_USERNAME")!,
        database: Environment.get("POSTGRESQL_DATABASE")!,
        password: Environment.get("POSTGRESQL_PASSWORD")!,
        transport: .standardTLS
    ))

将传输参数切换为.unverifiedTLS 有效。

我需要帮助才能让 Vapor 顺利解决 SSL 连接问题,但我不知道从哪里开始。

【问题讨论】:

  • 您的客户端似乎无法验证您的 ubuntu 服务器的证书,因为它没有验证信任链所需的信任锚,或者因为您在你的 ubuntu 服务器。你能确认是哪种情况吗?
  • 我也有同样的问题。我正在使用来自 Digital Ocean 的托管数据库

标签: swift postgresql ssl vapor vapor-fluent


【解决方案1】:

我最近在 Digital Ocean 上使用 Vapor 4 和 MySQL,我怀疑同样适用于 PostgreSQL。主要的一点是配置 Vapor 以信任 Digital Ocean 的证书。

  1. 从 Digital Ocean 上的托管数据库仪表板下载 CA 证书(连接详细信息部分)

  2. 配置数据库tlsConfigurataion 以信任该证书。下面是一个示例:

import NIOSSL

public func configure(_ app: Application) throws {
    app.databases.use(.postgres(
        hostname: Environment.get("DATABASE_HOST") ?? "localhost",
        port: Environment.get("DATABASE_PORT").flatMap(Int.init(_:)) ?? PostgresConfiguration.ianaPortNumber,
        username: Environment.get("DATABASE_USERNAME") ?? "vapor_username",
        password: Environment.get("DATABASE_PASSWORD") ?? "vapor_password",
        database: Environment.get("DATABASE_NAME") ?? "vapor_database",
        tlsConfiguration: try makeTlsConfiguration()
    ), as: .psql)
  // ...
}

private func makeTlsConfiguration() throws -> TLSConfiguration {
    var tlsConfiguration = TLSConfiguration.makeClientConfiguration()
    if let certPath = Environment.get("DATABASE_SSL_CERT_PATH") {
        tlsConfiguration.trustRoots = NIOSSLTrustRoots.certificates(
            try NIOSSLCertificate.fromPEMFile(certPath)
        )
    }
    return tlsConfiguration
}

在这个例子中,我使用DATABASE_SSL_CERT_PATH环境变量来设置下载的ca-certificate.crt文件的路径。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-02
    • 1970-01-01
    • 1970-01-01
    • 2015-08-17
    • 2012-09-03
    • 2012-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多