如何在 Mac 上为 Ruby 添加根 CA 证书

How to add a Root CA Cert for Ruby on Mac

我正在尝试寻找一些简单问题的答案。

  1. Ruby 在执行 SSL 时使用什么证书? (我假设这也适用于 gem
  2. 如何将根证书添加到受信任的 CA 证书集中。
  3. 是否有这方面的最佳做法?它在哪里记录?

背景

我正在使用 mac(当前为 Sierra),并且 rbenv/ruby-build 用于安装红宝石。 我们已经使用 Sonatype Nexus 建立了一个内部 ruby​​gems 存储库。服务器的证书是由公司内部的根证书签署的,我有一份。

当我尝试将我们的存储库添加为源时

gem sources --add https://our.repository.com/bla/bla

它抱怨证书的根不受信任。这与我在尝试从存储库(作为代理)中提取时遇到的问题相同,并且对于使用我们的 gem 的任何其他团队来说都是一个问题。我需要一个好的解决方案来为我的团队和其他人提供建议。

显然,如果我设置了环境变量SSL_CERT_FILE=/path/to/root_cert.pem,我就可以使用它。但我更愿意将证书文件放在受信任的位置,并让它像 Java 信任存储一样透明地工作。

通过一些调查和实验,我发现了以下注意事项。 (我建议为此信息做一个 brew info openssl):

  • Ruby 使用系统 OpenSSL
  • 最新版本的 OS X 不更新 openssl,因为 OS X 现在推出了自己的 TLS 和加密库
  • 需要重新哈希 openssl 使用的 CA 文件

看看自制软件输出中关于 openssl 的有趣部分:

$ brew info openssl
...
A CA file has been bootstrapped using certificates from the SystemRoots
keychain. To add additional certificates (e.g. the certificates added in
the System keychain), place .pem files in
  /usr/local/etc/openssl/certs

and run
  /usr/local/opt/openssl/bin/c_rehash

This formula is keg-only, which means it was not symlinked into /usr/local,
because Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries.
...

所以我所要做的就是以下内容,我所有的 SSL 问题都消失了Ruby:

cp /path/to/my/root_certificate.pem /usr/local/etc/openssl/certs
/usr/local/opt/openssl/bin/c_rehash

希望能帮到别人