pg gem sslmode=verify-full,在哪里放置证书?

pg gem sslmode=verify-full, where to place certificates?

有没有办法让 pg-gem 对其 postgres 连接使用 sslmode = verify-full?这会像传递那个字符串一样简单吗?我有一个 Rails 应用程序,我想在外部数据库上建立完整的 ca 保护 ssl 连接,但我真的不知道如何设置它的 ca 部分。在 sslmode 上使用 require 默认它使用 ssl 流量,但我不知道我应该在哪里(或在什么用户下等)放置我的证书来进行验证。我是否应该将它们放在 ~/.postgresql/ 下,就像在正常的 psql 客户端工作流程中一样,用于验证名称为 root.crt 和 postgresql.cert 和 postgresql.key 的 ssl 证书?

如果有帮助,我正在使用 Postgres 9.1。

为后来的人编辑:

以下 database.yml 文件似乎可以在我的开发机器上进行测试。我肯定会就这个问题写一篇博客 post,因为找出问题所在真是太麻烦了。

  host: 127.0.0.1
  sslcert: <%= Rails.root.join('config', 'client.crt') %>
  sslkey: <%= Rails.root.join('config', 'client.key') %>
  sslrootcert: <%= Rails.root.join('config', 'root.crt') %>
  sslmode: verify-full
  database: pg-test_development
  username: postgres
  password:

Pg gem 在内部使用 libpq,与 psql.

等 PostgreSQL 工具相同的客户端库

默认情况下 libpq~/.postgresql/ 中查找 CA 证书。

来自 the manual:

To allow server certificate verification, the certificate(s) of one or more trusted CAs must be placed in the file ~/.postgresql/root.crt in the user's home directory. (On Microsoft Windows the file is named %APPDATA%\postgresql\root.crt.)

...和...

The location of the root certificate file and the CRL can be changed by setting the connection parameters sslrootcert and sslcrl [...]

AFAIK Rails 将您放入 database.yml 的任何内容传递给 Pg gem,Pg gem 将其作为连接参数传递给 libpq。所以你应该能够将 key/value 条目添加到你的 database.yml 节中,例如:

sslmode: verify-full
# and if you don't want to use ~/.postgresq/root.crt for the cert location, set:
sslrootcert: /path/to/my/app/root/cert.crt

IMO 将单个根证书传递给 libpq 的要求是一个设计缺陷。它应该加载受信任的证书数据库。使用 SSL 客户端证书也存在类似问题,您无法在其中提供密钥库和证书库,您必须为给定主机传递特定文件。听起来这对你来说可能没问题,因为你知道上游证书签名机构。