Skip to content

Commit b735670

Browse files
committed
chore: add documentation how to configure x.509 authentication
1 parent da93b19 commit b735670

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

README.md

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -704,10 +704,45 @@ You need roughly three additional configuration steps:
704704
* Add x.509 Certificate subject as a User
705705
* Authenticate with an x.509 Certificate
706706

707+
To get the x.509 authentication working you need to prepare the ssl configuration accordingly:
708+
* you need set the ssl option: `verify_peer`
709+
* you need to specify the `cacertfile` because Erlang BEAM don't provide any CA certificate store by default
710+
* you maybe need to customize the hostname check to allow wildcard certificates
711+
* you need to specify the `username` from the subject entry of the user certificate
712+
713+
If you use a user certificate from Atlas a working configuration looks like this. First we
714+
use the [castore](https://hex.pm/packages/castore) package as the CA certificate store. After downloading
715+
the user certificate we extract the username subject entry from the PEM file:
716+
717+
```shell
718+
openssl x509 -in <pathToClientPEM> -inform PEM -subject -nameopt RFC2253
719+
720+
> CN=cert-user
721+
```
722+
723+
The configuration looks now:
707724
```elixir
708-
{:ok, pid} = Mongo.start_link(database: "test", auth_mechanism: :x509)
725+
opts = [
726+
url: "mongodb+srv://cluster0.xxx.mongodb.net/myFirstDatabase?authSource=%24external&retryWrites=true&w=majority",
727+
ssl: true,
728+
username: "CN=cert-user",
729+
password: "",
730+
auth_mechanism: :x509,
731+
ssl_opts: [
732+
verify: :verify_peer,
733+
cacertfile: to_charlist(CAStore.file_path()),
734+
certfile: '/path-to-cert/X509-cert-2227052404946303101.pem',
735+
customize_hostname_check: [
736+
match_fun:
737+
:public_key.pkix_verify_hostname_match_fun(:https)
738+
]
739+
]]
740+
741+
Mongo.start_link(opts)
709742
```
710743

744+
Currently, we need to specify *an empty password* to get the x.509 auth module working. This will be changed soon.
745+
711746
## AWS, TLS and Erlang SSL Ciphers
712747

713748
Some MongoDB cloud providers (notably AWS) require a particular TLS cipher that isn't enabled

lib/mongo/auth/x509.ex

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@ defmodule Mongo.Auth.X509 do
33
alias Mongo.MongoDBConnection.Utils
44

55
def auth({username, _password}, _db, s) do
6+
IO.inspect(username)
67
cmd = [authenticate: 1, user: username, mechanism: "MONGODB-X509"]
78

89
case Utils.command(-2, cmd, s) do
9-
{:ok, _flags, _message} -> :ok
10+
{:ok, _flags, message} ->
11+
IO.inspect(message)
12+
:ok
1013
_error -> {:error, "X509 auth failed"}
1114
end
1215
end

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ defmodule Mongodb.Mixfile do
99
app: :mongodb_driver,
1010
version: @version,
1111
elixirc_paths: elixirc_paths(Mix.env()),
12-
elixir: "~> 1.8",
12+
elixir: "~> 1.15",
1313
name: "mongodb-driver",
1414
deps: deps(),
1515
docs: docs(),

0 commit comments

Comments
 (0)