Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion : Connecting to MongoDB Atlas Example #247

Open
Vidioleola opened this issue Jul 12, 2024 · 1 comment
Open

Suggestion : Connecting to MongoDB Atlas Example #247

Vidioleola opened this issue Jul 12, 2024 · 1 comment

Comments

@Vidioleola
Copy link

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
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants