Uchoa

Uchoa

Hello everyone

I’m newcomer for here and mainly using elixir language. Currently I’m trying to connect my system (Elixir) with an Azure Database for PostgreSQL using ssl certificate. I downloaded the certificate from the blue documents page (f.e.: BaltimoreCyberTrustRoot.crt.pem), but every time I try to run the system it throws the following error:

12:50:29.007 [error] Postgrex.Protocol (pid<0.165.0>) failed to connect: ** (DBConnection.ConnectionError) ssl connect: closed
12:50:29.019 [error] Postgrex.Protocol (pid<0.164.0>) failed to connect: ** (DBConnection.ConnectionError) ssl connect: closed
12:50:30.981 [error] Postgrex.Protocol (pid<0.165.0>) failed to connect: ** (DBConnection.ConnectionError) ssl connect: closed
12:50:31.254 [error] Postgrex.Protocol (pid<0.164.0>) failed to connect: ** (DBConnection.ConnectionError) ssl connect: closed
12:50:31.445 [error] Could not create schema migrations table. This error usually happens due to the following:

  • The database does not exist
  • The “schema_migrations” table, which Ecto uses for managing
    migrations, was defined by another library
  • There is a deadlock while migrating (such as using concurrent
    indexes with a migration_lock)

To fix the first issue, run “mix ecto.create”.

To address the second, you can run “mix ecto.drop” followed by
“mix ecto.create”. Alternatively you may configure Ecto to use
another table and/or repository for managing migrations:

config :database, Database.Repo,
  migration_source: "some_other_table_for_schema_migrations",
  migration_repo: AnotherRepoForSchemaMigrations

The full error report is shown below.

** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2983ms. This means requests are coming in and your connection pool cannot serve them fast enough. You can address this by:

  1. Ensuring your database is available and that you can connect to it
  2. Tracking down slow queries and making sure they are running fast enough
  3. Increasing the pool_size (albeit it increases resource consumption)
  4. Allowing requests to wait longer by increasing :queue_target and :queue_interval

Can someone help me?

First 10 of 14 Posts Switch mode

ruslandoga

ruslandoga

You can pass your certs in :ssl_opts when configuring the repo.

Uchoa

Uchoa OP

I already passed. Here are the settings:

config :database, Database.Repo,
database: cgc_db_name,
username: cgc_db_username,
password: cgc_db_password,
hostname: cgc_db_hostname,
port: String.to_integer(cgc_db_port),
pool_size: String.to_integer(cgc_db_pool_size),
ssl: true,
ssl_opts: [
verify: :verify_peer,
versions: [:“tlsv1.2”],
ciphers: :ssl.cipher_suites(:all, :“tlsv1.2”),
cacertfile: cgc_db_cacertfile
]

ruslandoga

ruslandoga

Here’s my working setup with google’s postgres:

# in config/runtime.exs

decode_cert = fn cert ->
  [{:Certificate, der, _}] = :public_key.pem_decode(cert)
  der
end

decode_key = fn cert ->
  [{:RSAPrivateKey, key, :not_encrypted}] = :public_key.pem_decode(cert)
  {:RSAPrivateKey, key}
end

ca_cert = System.get_env("DATABASE_CA_CERT")
client_key = System.get_env("DATABASE_CLIENT_KEY")
client_cert = System.get_env("DATABASE_CLIENT_CERT")

ssl_opts =
  if ca_cert do
    [
      cacerts: [decode_cert.(ca_cert)],
      key: decode_key.(client_key),
      cert: decode_cert.(client_cert)
    ]
  end

config :app, App.Repo,
  ssl_opts: ssl_opts,
  # ...

Maybe it would help.

Uchoa

Uchoa OP

Ok, but you have 3 certificates: ca_cert, client_key and client_cert. In my case, I’m using Azure and the documentation only provides one certificate (BaltimoreCyberTrustRoot.crt.pem). I would like to know if the variable “verify” with the value “verify_peer” obligatorily asks for all certificates.

My attempts so far?

  1. Connect my system with azure database for postgres without ssl and it worked correctly. → OK!
  2. Connect my system with azure database for postgres with ssl and it gave an error.
  3. I’ve already changed the version to [:“tlsv1.1”], [:“tlsv1.2”], [:“tlsv1.3”] and all attempts gave error.
  4. I already took the verify property, but it also gave an error.
ruslandoga

ruslandoga

You probably can’t do full verification (I guess that’s what verify_peer stands for, but I’m not sure) with just a cacert, have you been able to connect to the db with psql, it might have more info on what’s going wrong? Also try using sslmode=require and sslmode=verify-ca. I pass these in repo url:

# export DATABASE_URL=ecto://user:password@hostname:5432/db?sslmode=require
db_url = System.fetch_env!("DATABASE_URL")
config :app, App.Repo,
  url: db_url
Uchoa

Uchoa OP

I haven’t tried that way yet

Uchoa

Uchoa OP

I’m trying again, but now the following error is appearing:

14:32:06.948 [error] Postgrex.Protocol (pid<0.2281.0>) failed to connect: ** (DBConnection.ConnectionError) ssl connect: TLS client: In state certify at ssl_handshake.erl:2017 generated CLIENT ALERT: Fatal - Handshake Failure
{bad_cert,hostname_check_failed} - {:tls_alert, {:handshake_failure, ‘TLS client: In state certify at ssl_handshake.erl:2017 generated CLIENT ALERT: Fatal - Handshake Failure\n {bad_cert,hostname_check_failed}’}}

ruslandoga

ruslandoga

That’s much better as it at least shows the error now instead of plain closed. Since the error reason is hostname_check_failed can you please verify that the name in the certificate is the same as the one you are connecting to? Maybe the host name there is something like my-db.azure.com and you are connecting to 13.48.123.123 or something like this.

You can do it with openssl: https://unix.stackexchange.com/questions/103461/get-common-name-cn-from-ssl-certificate

KristerV

KristerV

i am at this exact state now. any chance you have a working config you can share?

ruslandoga

ruslandoga

:wave: @KristerV

I think Postgrex should be able to handle it automatically in the recent versions (v0.18.0 and up).


For an older Postgrex this seems to work:

Mix.install [{:postgrex, "0.17.5"}]

Postgrex.start_link(
  hostname: "my-rds-db.c16quuxxxx.eu-north-1.rds.amazonaws.com",
  port: 5432,
  username: "my-rds-user",
  password: "xxx",
  database: "my-rds-db",
  show_sensitive_data_on_connection_error: true,
  ssl: true,
  ssl_opts: [
    verify: :verify_peer,
    cacertfile: ~c"/Users/ruslandoga/Desktop/eu-north-1-bundle.pem",
    depth: 10,
    server_name_indication: ~c"my-rds-db.c16quuxxxx.eu-north-1.rds.amazonaws.com",
    customize_hostname_check: [match_fun: :public_key.pkix_verify_hostname_match_fun(:https)]
  ]
)

The important bit that was missing from my previous replies is server_name_indication.

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement