Trolleger

Trolleger

Signed SSL Certificate Errors with postgresql database (all self hosted)

Hello! I’ve been having a lot of troubles trying to set up ssl with postgresql and I tried looking also on the discord before asking but turns out there was another guy who was also having issues and made a post without response, I’ve already tried looking everywhere online but I can’t really find a solution, also given I’m not too experienced with elixir, ssl, postgresql and all these programming things :expressionless:

So the current system:

So I create the certs like this (I’ve tried 2 ways)

openssl req -new -x509 -days 365 -nodes -out ca.crt -keyout ca.key -subj "/CN=root" &&

openssl genrsa -des3 -out server.key 2048 &&
openssl rsa -in server.key -out server.key && 
openssl req -new -nodes -key server.key -out server.csr -subj "/CN=root" && 
openssl x509 -req -in server.csr -days 365 -CA ca.crt -CAkey ca.key -CAcreateserial -out server.crt && 

openssl genrsa -des3 -out client.key 2048 &&
openssl rsa -in client.key -out client.key && 
openssl req -new -nodes -key client.key -out client.csr -subj "/CN=root" &&
openssl x509 -req -in client.csr -days 365 -CA ca.crt -CAkey ca.key -CAcreateserial -out client.crt &&

sudo chown 70:70 server.key &&
sudo chmod 600 server.key

and later

# https://www.howtoforge.com/postgresql-ssl-certificates
openssl genrsa -des3 -out server.key 4096 &&
openssl rsa -in server.key -out server.key
sudo chown 70:70 server.key &&
sudo chmod 600 server.key &&
openssl req -new -key server.key -days 3650 -out server.crt -x509 -subj '/C=CA/ST=British Columbia/L=Comox/O=localhost/CN=root/emailAddress=example@xyz.com' &&
cp server.crt root.crt &&
openssl genrsa -des3 -out ./client_certs/client.key 4096 &&
openssl rsa -in ./client_certs/client.key -out ./client_certs/client.key &&
openssl req -new -key ./client_certs/client.key -out ./client_certs/client.csr -subj '/C=CA/ST=British Columbia/L=Comox/O=localhost/CN=root' &&
openssl x509 -req -in ./client_certs/client.csr -CA root.crt -CAkey server.key -out ./client_certs/client.crt -CAcreateserial 
cp root.crt client_certs

The configurations look something like this

listen_addresses = '*'
ssl = on
ssl_cert_file = '/etc/postgresql/certs/server.crt'
ssl_key_file = '/etc/postgresql/certs/server.key'
ssl_ca_file = '/etc/postgresql/certs/client_certs/root.crt'

port=5432
ssl = on
hostnossl   all   all   0.0.0.0/0   reject
hostnossl   all   all   ::/0        reject
hostssl     all   all   0.0.0.0/0   cert clientcert=verify-full
hostssl     all   all   ::/0        cert clientcert=verify-full

I am using docker compose and before SSL the containers were able to communicate just fine, I am copying over the certs properly

db has
        - ./postgres/postgres_config/pg_hba.conf:/etc/postgresql/pg_hba.conf
        - ./postgres/postgres_config/postgresql.conf:/etc/postgresql.conf
        - ./postgres/certs:/etc/postgresql/certs/:ro
backend has      
- ./postgres/certs/client_certs:/app/certs:ro

In regards to the configurations within my code

config :chat_app, ecto_repos: [ChatApp.Repo]
config :chat_app, ChatApp.Repo,

  # fetches the DATABASE_URL environment variable to set database connection
  url: System.fetch_env!("DATABASE_URL"),
  # Connection pool size defaults to ten if not set by POOL_SIZE variable
  pool_size: String.to_integer(System.get_env("POOL_SIZE") || "10"),
  migration_primary_key: [type: :uuid],
  migration_foreign_key: [type: :uuid],

  # enable ssl connection
  ssl: [
  verify: :verify_peer,
  cacertfile: "/app/certs/root.crt",
  keyfile: "/app/certs/client.key",
  certfile: "/app/certs/client.crt",
  server_name_indication: to_charlist("localhost"),
]

(I do have extra db configs elsewhere (it’s when I used ai (which I solemnly regret) and I’m trying to figure stuff out and fix it)) but in general that’s the important ssl config things and also the other guy having the issues on the elixir server Discord gives his own different way and config!

Now I think I have included enough details, I can give more if needed but I think it’s good for now, I’ve had several errors while trying to fix the issue in different ways such as

{utf8String,<<"localhost">>}}]]}}}} - {:tls_alert, {:handshake_failure, ~c"TLS client: In state wait_cert at ssl_handshake.erl:2186 generated CLIENT ALERT: Fatal - Handshake Failure\n {bad_cert,\n     {hostname_check_failed,\n         {requested,\"postgres\"},\n         {received,\n             {rdnSequence,\n                 [[{'AttributeTypeAndValue',\n                       {2,5,4,3},\n                       {utf8String,<<\"localhost\">>}}]]}}}}"}}

or

22:41:14.559 [error] Postgrex.Protocol (#PID<0.322.0>) failed to connect: ** (DBConnection.ConnectionError) ssl connect: TLS client: In state wait_cert at ssl_handshake.erl:2169 generated CLIENT ALERT: Fatal - Bad Certificate
 invalid_signature - {:tls_alert, {:bad_certificate, ~c"TLS client: In state wait_cert at ssl_handshake.erl:2169 generated CLIENT ALERT: Fatal - Bad Certificate\n invalid_signature"}}

or

23:58:39.739 [error] Postgrex.Protocol (#PID<0.623.0>) failed to connect: ** (DBConnection.ConnectionError) ssl connect: TLS client: In state wait_cert at ssl_handshake.erl:2183 generated CLIENT ALERT: Fatal - Unknown CA
 - {:tls_alert, {:unknown_ca, ~c"TLS client: In state wait_cert at ssl_handshake.erl:2183 generated CLIENT ALERT: Fatal - Unknown CA\n"}}

But at this point most recently I have

 selfsigned_peer - {:tls_alert, {:bad_certificate, ~c"TLS client: In state wait_cert at ssl_handshake.erl:2181 generated CLIENT ALERT: Fatal - Bad Certificate\n selfsigned_peer"}}

And the other guy in his discord post got

[my-app] [notice] TLS :client: In state :wait_cert_cr at ssl_handshake.erl generated CLIENT ALERT: Fatal - Internal Error
[my-app] - {:unexpected_error, :undef}
[my-app] [error] Postgrex.Protocol failed to connect: ** (DBConnection.ConnectionError) ssl connect: TLS client: In state wait_cert_cr at ssl_handshake.erl

Also to clarify those previous errors were with other configurations in general. Generally though it appears to me that the fundamental issue is that Elixir does not like the certs themselves, how can I properly set up and deal with Ssl and certs to actually get this working because it really is a pain ;-; All the errors as far as my limited knowledge goes lead back to rome (bad certs)

If anyone knows what to do, how to solve this that would be greatly appreciated! take care :slight_smile:

Most Liked

jstimps

jstimps

You may have an issue with the SNI config. SNI will check the CN and SAN attributes. Your cert generation commands use O=localhost and CN=root.

I suggest removing as many variables as possible from the problem space, confirm a working connection, and then start adding those variables back in slowly.

In this case, I recommend using openssl s_client on the command line to connect to postgres directly (just a TLS socket connection), removing Elixir from the equation for now. Once you have that working you can try to match the Elixir config to the s_client arguments.

Schultzer

Schultzer

Ouch, I was hoping I could help you, but when I looked in my code for setting up postgres then I realized that I’m using certbot to generate certificates, I find that AI can help with researching this, but I would not blindly trust it.

Schultzer

Schultzer

It’s not localhost, but certbot do have a guide Certificates for localhost - Let's Encrypt

I use two hooks one to finalize and one to take that certificate and create one that Postgres can use.

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: &lt;h1&gt;Create Post&lt;/h1&gt; &lt;%= ...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New

Other popular topics Top

hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31265 143
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement