Connecting to PostgreSQL hosted on Azure using SSL with Elixir backend

Viewed 819

I am trying to connect locally to a PostgreSQL (v. 9.6) hosted on Azure using SSL. Unfortunately it is not working as expected. The backend is running in a docker container with Alpine Linux as the base image, Elixir 1.7.4, the current ecto version with ecto-sql (v. 3.0.0).

On Azure Enforce SSL connection is enabled and corresponding Firewall rules are set.

I checked with a database tool (TablePlus) locally which can connect to the online database easily.

Here my config:

config :my_app, MyApp.Repo,
  username: "admin@postgres-dev",
  password: "secret",
  database: "dev",
  port: 5432,
  hostname: "postgres-dev.postgres.database.azure.com",
  ssl: true,
  ssl_opts: [
    versions: [:"tlsv1.2"],
    cacertfile: "priv/root.pem"
  ],
  pool_size: 15

Also I added :ssl to the extra_applications in the mix.exs.

I set the versions explicitly due to an answer here on stackoverflow. I got the certificate from Azure and decoded it using OpenSSL as described there.

I tried it without ssl_opts, only with one attribute set at ssl_opts and also with a database_url (with and without query-parameter : ?ssl=true) instead of describing all attributes seperately.

I always run into the same error:

(Mix) The database for MyApp.Repo couldn't be created: connection not available and request was dropped from queue after 2844ms. You can configure how long requests wait in the queue using :queue_target and :queue_interval. See DBConnection.start_link/2 for more information

Using a different versions leads also to an error:

14:39:26.222 [error] GenServer #PID<0.500.0> terminating
** (DBConnection.ConnectionError) ssl connect: Invalid TLS option: {'tlsv1.3',{versions,['tlsv1.3']}} - {:options, {:"tlsv1.3", {:versions, [:"tlsv1.3"]}}}
(db_connection) lib/db_connection/connection.ex:84: DBConnection.Connection.connect/2
(connection) lib/connection.ex:622: Connection.enter_connect/5
(stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
Last message: nil
State: Postgrex.Protocol
** (Mix) The database for MyApp.Repo couldn't be created: killed

So this is definitively not a solution

When not setting ssl: true, I`ll get an error as expected:

10:19:02.036 [error] GenServer #PID<0.251.0> terminating
** (Postgrex.Error) FATAL 28000 (invalid_authorization_specification) SSL connection is required. Please specify SSL options and retry.
(db_connection) lib/db_connection/connection.ex:84: DBConnection.Connection.connect/2
(connection) lib/connection.ex:622: Connection.enter_connect/5
(stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
Last message: nil
State: Postgrex.Protocol
** (Mix) The database for MyApp.Repo couldn't be created: killed

Is there anyone who already tried this and might can help?

1 Answers

Working here with TSL 1.2.

In the TLS handshake the server you are connecting will send its certificate. You don´t need to provide it.

ssl: true,
ssl_opts: [
  versions: [:"tlsv1.2"]
]

And to avoid timeout problems ....

timeout: 120_000,
queue_target: 10_000,
queue_interval: 100_000
Related