gpt4 book ai didi

ruby - pg gem 中 SSL 支持的运行时检测

转载 作者:行者123 更新时间:2023-11-29 13:11:51 25 4
gpt4 key购买 nike

我们有一些 Ruby 代码可以做类似的事情:

require 'pg'

# Create a remote Postgres database instance, and wait
# until it's online, then try to connect:
conn = PG::Connection.new(
... params ...
sslmode: 'require')

重要的部分是远程 Postgres 实例配置为强制使用 SSL(例如 like this)——客户端代码中的 sslmode: 'require' 是多余的。

这在许多开发人员机器上运行良好,但一些开发人员注意到他们遇到了一些错误,例如:

 FATAL:  no pg_hba.conf entry for host "...", user "...", database "...", SSL off (PG::ConnectionBad)

根本原因似乎是他们的 pg gem 实际上没有 SSL 支持。

我的问题:有什么方法可以确定 pg gem 是否确实支持 SSL,而无需实际尝试连接到需要 SSL 的服务器? (如果我们能够立即检测到问题,而不是等到创建了远程 Postgres 实例,然后才发现问题,那就更好了。)

我的第一个想法是检查 pg gem 上的一个方法,例如 init_openssl ,但文档声称:

When SSL support is not compiled in, this function is present but does nothing.

并且无论是否存在 SSL 支持,此行为似乎都是相同的:

2.3.1 :002 > PG.init_openssl(true, true)
=> nil

环境信息:最新 OS X 上的 ruby​​ 2.3,pg gem 0.19.0,各种版本的 libpq

最佳答案

首先,确保 Ruby 编译时支持 OpenSSL:

ruby -r rbconfig -e 'puts RbConfig::CONFIG["configure_args"]'
'--prefix=/Users/foo/.rvm/rubies/ruby-2.6.0-preview2' '' '--with-opt-dir=/usr/local/opt/libyaml:/usr/local/opt/readline:/usr/local/opt/libksba:/usr/local/opt/openssl@1.1' '--disable-install-doc' '--enable-shared' 'build_alias=' 'host_alias=' 'target_alias=' 'CC=gcc'

确保 openssl 位于 --with-opt-dir 输出中的某处。或者,您可以检查一下:

ruby -r openssl -e 'puts OpenSSL::OPENSSL_LIBRARY_VERSION'
OpenSSL 1.1.1 11 Sep 2018

接下来,找到您的 pg_ext.bundle。它的位置将根据您安装 Ruby 和 gems 的方式而有所不同,但如果您使用的是 RVM,则可以很简单地在 ~/.rvm/gems 中找到它。例如,它位于我这里:

~/.rvm/gems/ruby-2.6.0-preview2/gems/pg-1.1.3/ext/pg_ext.bundle

现在运行 otool -L 找到它链接的库:

otool -L /Users/foo/.rvm/gems/ruby-2.6.0-preview2/gems/pg-1.1.3/ext/pg_ext.bundle
/Users/foo/.rvm/gems/ruby-2.6.0-preview2/gems/pg-1.1.3/ext/pg_ext.bundle:
/Users/foo/.rvm/rubies/ruby-2.6.0-preview2/lib/libruby.2.6.dylib (compatibility version 2.6.0, current version 2.6.0)
/usr/local/opt/postgresql/lib/libpq.5.dylib (compatibility version 5.0.0, current version 5.11.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1252.200.5)

现在针对上面输出中列出的 libpq dylib 运行 otool -L 以查看它链接的内容:

otool -L /usr/local/opt/postgresql/lib/libpq.5.dylib
/usr/local/opt/postgresql/lib/libpq.5.dylib:
/usr/local/opt/postgresql/lib/libpq.5.dylib (compatibility version 5.0.0, current version 5.11.0)
/usr/local/opt/openssl/lib/libssl.1.0.0.dylib (compatibility version 1.0.0, current version 1.0.0)
/usr/local/opt/openssl/lib/libcrypto.1.0.0.dylib (compatibility version 1.0.0, current version 1.0.0)
/System/Library/Frameworks/Kerberos.framework/Versions/A/Kerberos (compatibility version 5.0.0, current version 6.0.0)
/System/Library/Frameworks/LDAP.framework/Versions/A/LDAP (compatibility version 1.0.0, current version 2.4.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1252.200.5)

只要它链接到 libssllibcrypto,安装的 pg gem 也应该支持 OpenSSL。

关于ruby - pg gem 中 SSL 支持的运行时检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53746821/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com