production.log

株式会社リブセンスでエンジニアをやっている星直史のブログです。

homebrewでinstallしたcurlがbrew cask install時のTLS1.2ではOSX標準のcurlが邪魔をしてうまく反映されなかった時の対処法

概要

homebrewでinstallしたTLS 1.2 接続できるcurlbrew installでTLS1.2接続しなければならないパッケージのinstall時に、OSX標準のcurlを見に行ってしまい、うまくinstallできなかったので、その対処方法を書きます。

具体的には、brew cask install sourcetreeをした際に、curl https://downloads.atlassian.com/software/sourcetree/SourceTree_2.3.2.zipを叩くのですが、 そこでssl handshake failedとなり失敗していました。

手順

homebrewで最新のcurlをinstallしOpenSSLを使用するように変更

確認に使用したコマンドなども含めつつ説明していきます。

まずは、curlでTLS1.2で接続していないことを確認します。

$ curl --dump-header - https://www.example.com --tlsv1.2 --verbose 
*   Trying nnn.nnn.nnn.nnn...
* Connected to www.example.com (nnn.nnn.nnn.nnn) port 443 (#0)
* SSL peer handshake failed, the server most likely requires a client certificate to connect
* Closing connection 0
curl: (35) SSL peer handshake failed, the server most likely requires a client certificate to connect

次に、最新のOpenSSLでTLS1.2が使用できることを確認します。

$ openssl s_client -connect www.example.com:443 -tls1_2
CONNECTED(00000003)
...

既存のcurlにOpenSSLが使われていないことを確認します。

$ which curl
/usr/bin/curl
$ curl --version
curl 7.43.0 (x86_64-apple-darwin14.0) libcurl/7.43.0 SecureTransport zlib/1.2.5
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp smb smbs smtp smtps telnet tftp 
Features: AsynchDNS IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB SSL libz UnixSockets

上記の通りOpenSSLが含まれていません。

homebrewで改めてcurlをinstallします。

$ brew install --with-openssl curl
$ brew link curl --force

OpenSSLが使用できているか確認します。

$ which curl
/usr/local/bin/curl

$ curl --version
curl 7.51.0 (x86_64-apple-darwin14.5.0) libcurl/7.51.0 **OpenSSL/1.0.2j** zlib/1.2.5
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp smb smbs smtp smtps telnet tftp 
Features: IPv6 Largefile NTLM NTLM_WB SSL libz TLS-SRP UnixSockets 

OpenSSL/1.0.2jが適用されていることが確認できました。

念のために、TLS1.2で接続できるかも確認してみます。

$ curl --dump-header - https://www.google.com --tlsv1.2 --verbose
* Rebuilt URL to: https://www.google.com/
*   Trying 216.58.200.196...
* TCP_NODELAY set
* Connected to www.google.com (216.58.200.196) port 443 (#0)
...
...
* Curl_http_done: called premature == 0
* Connection #0 to host www.google.com left intact

問題なくTLS1.2接続できていますね。

シンボリックリンクを張る

この時点で、brew cask install sourcetreeを実行してもssl handshake failedになります。 前回の記事同様パスの影響を受けているのではないかと考えましたので、 brew cask installで見ているcurlシンボリックリンクで無理やりbrew installしたcurlを参照させます。

$ sudo mv /usr/bin/curl /usr/bin/curl_back
$ sudo ln -s /usr/local/bin/curl /usr/bin/curl
$ brew cask install sourcetree
==> Caveats
Cask sourcetree installs files under "/usr/local". The presence of such
files can cause warnings when running "brew doctor", which is considered
to be a bug in Homebrew-Cask.

==> Downloading https://downloads.atlassian.com/software/sourcetree/SourceTree_2.3.2.zip
Already downloaded: /Users/user_name/Library/Caches/Homebrew/Cask/sourcetree--2.3.2.zip
==> Verifying checksum for Cask sourcetree
==> Moving App 'SourceTree.app' to '/Applications/SourceTree.app'
==> Symlinking Binary 'stree' to '/usr/local/bin/stree'
🍺  sourcetree was successfully installed!

うまくいきました。 シンボリックリンクで無理やり変更しているので、ワークアラウンド感がハンパないですね。 うまいやり方があれば是非教えていただきたいです。