zookzook / elixir-mongodb-driver

MongoDB driver for Elixir
Apache License 2.0
247 stars 63 forks source link

Suggestion : Connecting to MongoDB Atlas Example #247

Open Vidioleola opened 4 months ago

Vidioleola commented 4 months ago

Hi,

I just spent a few hours trying to connect, using version 1.4.1 and OTP 26, to my DB hosted on Mongodb Atlas. I finally found the problem and, even if the solution does exist in another issue it would have been much easier if there just was a code example of how to connect to a db hosted on Atlas (which I believe is something I won't be the only person to try to accomplish).

More precisely, the following snippet shows how to load the OS certificates and to make the hostname check work using the customize_hostname_check.

Something like this :

defmodule Atlas.Example do
  def connect do
    {:ok, conn} = Mongo.start_link(
      url: "mongodb+srv://<user>:<password>@the-rest-of-the-url",
      ssl: true,
      ssl_opts: [
        verify: :verify_peer,
        cacerts: :public_key.cacerts_get(),
        customize_hostname_check: [
          match_fun:
            :public_key.pkix_verify_hostname_match_fun(:https)
        ]
      ]
    )
    conn
  end
end

Thank you!

zookzook commented 4 months ago

Hi Vidioleola,

thanks for reporting. This is a fundamental issue for a TLS network connection regarding the Erlang's ssl module. The documentation can be found here: https://www.erlang.org/doc/apps/ssl/ssl.html

There are already some examples in the README https://www.erlang.org/doc/apps/ssl/ssl.html.