[英]How can I help Vapor successfully SSL-handshake my PostgreSQL server?
我在 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 連接,但我不知道從哪里開始。
我最近在 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
}
在這個例子中,我使用DATABASE_SSL_CERT_PATH
環境變量來設置下載的ca-certificate.crt
文件的路徑。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.