首页 > 解决方案 > 无法使用 SSL 将 Corda 节点连接到 Postgres

问题描述

我在 GCP(谷歌云平台)中的 Postgres DB 只接受通过 SSL 的连接。
我在我的内部尝试了以下内容,node.conf但没有成功:

dataSourceProperties {
    dataSourceClassName = "org.postgresql.ds.PGSimpleDataSource"
    dataSource.url = "jdbc:postgresql://db-private-ip:5432/my_node"
    dataSource.ssl = true
    dataSource.sslMode = verify-ca
    dataSource.sslRootCert = "/opt/corda/db-certs/server-ca.pem"
    dataSource.sslCert = "/opt/corda/db-certs/client-cert.pem"
    dataSource.sslKey = "/opt/corda/db-certs/client-key.pem"
    dataSource.user = my_node_db_user
    dataSource.password = my_pass
}

我确定密钥(sslMode、sslRootCert、sslCert 和 sslKey)在node.conf(即使在 Corda 文档中的任何地方都没有提到)是可以接受的,因为在日志中我没有收到任何错误表明这些密钥无法识别.
当我尝试启动节点时出现此错误:

[ERROR] 21:58:48+0000 [main] pool.HikariPool. - HikariPool-1 - Exception during pool initialization. [errorCode=zmhrwq, moreInformationAt=https://errors.corda.net/OS/4.3/zmhrwq]
[ERROR] 21:58:48+0000 [main] internal.NodeStartupLogging. - Could not connect to the database. Please check your JDBC connection URL, or the connectivity to the database.: Could not connect to the database. Please check your JDBC connection URL, or the connectivity to the database. [errorCode=18t70u2, moreInformationAt=https://errors.corda.net/OS/4.3/18t70u2]

我尝试按照( Azure Postgres 数据库需要 Corda 的 SSL 连接?ssl=true)中的建议添加到数据源 URL 的末尾,但这并没有解决问题。

同样对于相同的值,我可以使用 psql 客户端将我的 VM 连接到数据库:

psql "sslmode=verify-ca sslrootcert=server-ca.pem sslcert=client-cert.pem sslkey=client-key.pem hostaddr=db-private-ip user=some-user dbname=some-pass"

标签: corda

解决方案


结果 JDBC 驱动程序无法从 PEM 文件中读取密钥,必须使用以下命令将其转换为 DER 文件:

openssl pkcs8 -topk8 -inform PEM -in client-key.pem -outform DER -nocrypt -out client-key.der

chmod 400 client-key.der
chown corda:corda client-key.der

更多细节在这里:https ://github.com/pgjdbc/pgjdbc/issues/1364

所以正确的配置应该是这样的:

dataSourceProperties {
    dataSourceClassName = "org.postgresql.ds.PGSimpleDataSource"
    dataSource.url = "jdbc:postgresql://db-private-ip:5432/db-name"
    dataSource.ssl = true
    dataSource.sslMode = verify-ca
    dataSource.sslRootCert = "/opt/corda/db-certs/server-ca.pem"
    dataSource.sslCert = "/opt/corda/db-certs/client-cert.pem"
    dataSource.sslKey = "/opt/corda/db-certs/client-key.der"
    dataSource.user = db-user-name
    dataSource.password = db-user-pass
}

推荐阅读