Dir*_*nry 5 postgresql ssl swift vapor vapor-fluent
我在 Ubuntu 服务器上使用 Vapor 连接到我的 DigitalOcean 管理的 PostgreSQL 数据库。
从命令行,运行以下命令可以正常工作:
psql postgresql://user:password@host:port/dbname?sslmode=require
Run Code Online (Sandbox Code Playgroud)
但是使用以下代码运行等效内容会给出:
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
Run Code Online (Sandbox Code Playgroud)
这是代码:
psql postgresql://user:password@host:port/dbname?sslmode=require
Run Code Online (Sandbox Code Playgroud)
将传输参数切换为.unverifiedTLS
有效。
我需要帮助才能让 Vapor 正常建立 SSL 连接,但我不知道从哪里开始。
小智 4
我最近在 Digital Ocean 上使用了 Vapor 4 和 MySQL,我怀疑同样的方法也适用于 PostgreSQL。主要部分是将 Vapor 配置为信任 Digital Ocean 的证书。
从 Digital Ocean 上的托管数据库仪表板(连接详细信息部分)下载 CA 证书。
配置数据库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
}
Run Code Online (Sandbox Code Playgroud)
在本例中,我使用DATABASE_SSL_CERT_PATH
环境变量来设置下载文件的路径ca-certificate.crt
。
归档时间: |
|
查看次数: |
1036 次 |
最近记录: |