Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a trusted certificate to HTTPPoison/hackney?

When I try:

HTTPoison.get! "https://facebook.com"

I get:

** (HTTPoison.Error) {:tls_alert, 'unknown ca'}
[error] SSL: :certify: ssl_handshake.erl:1606:Fatal error: unknown ca

    (httpoison) lib/httpoison.ex:66: HTTPoison.request!/5

which is kind of expected since in my company I need to trust the firewall's certificate to get out.

I have trusted the certificate (.cer file) system wide, which is why wget doesn't give me ssl errors when accessing https URLs. But it seems that hackney/HTTPoison ignores this configuration.

How to I make HTTPoison/hackney recognize the certificate as a trusted certificate?

like image 925
diogovk Avatar asked Oct 07 '16 13:10

diogovk


2 Answers

I recently ran into this issue as well. What worked for me was passing the location of the cert file directly to hackney as suggested by the dev in this ticket:

opts = [{:ssl_options, [{:cacertfile, "/<path to my cert>/MyCertificates.pem"}]}]
HTTPoison.post(login, headers, hackney: opts)

Alternatively you could perform the SSL connection without checking the certificate (more about request options here):

HTTPoison.post(login, headers, hackney: [:insecure])
like image 162
Keyan P Avatar answered Sep 18 '22 17:09

Keyan P


Cert file paths are being passed toHTTPoison options like this:

   defp add_certs do
          [                                                                                                                                        
            hackney: [ # :hackney options                                                                                                          
             ssl_options: [ # :ssl options                                                                                                         
               cacertfile: # CA certificate used to validate server cert; path(), "string" is ok                  
               certfile:  # client certificate, signed by CA; path(), "string" is ok                                 
               keyfile:  # private key for client.crt; path(). "string" is ok                                         
               password:  # password for keyfile; string(), "string" not ok, use 'char list'                                  
             ]                                                                                                                                     
           ]                                                                                                     
          ]
    end
    HTTPoison.post(url, request_xml, headers, add_certs)
like image 36
Zaal Kavelashvili Avatar answered Sep 17 '22 17:09

Zaal Kavelashvili