Fl4m3Ph03n1x

Fl4m3Ph03n1x

How to connect to postgrex with SSL certificate?

Background

We are making a proof of concept to use a postgres DB with Elixir. To achieve this we are using Postgrex.

Issue

The problem here is we need to connect to the DB using a certificate instead of a username and password. Unfortunately the documentation only has 1 example where one connects via username and password.

{:ok, pid} = Postgrex.start_link(hostname: "localhost", username: "postgres", password: "postgres", database: "postgres")

Research

I have read the documentation and I found that I can use the ssl and ssl_opts parameters when connecting to the DB: Postgrex — Postgrex v0.22.2

However, the documentation tells me to loo for the ssl docs, but no link is given.

It is the first time I am dealing with this kind of information in Elixir, I have no idea on how to proceed from here on.

Questions

  1. How do I connect to my postgres DB using a certificate and Postgrex?
  2. What are these ssl_opts documents the documentation refers to? How do they translate to Elixir code?

Marked As Solved

hauleth

hauleth

As documentation states that :certfile need to be string() which is Erlang’s string, in Elixir known as charlist you need to do this like that:

{:ok, pid} = Postgrex.start_link(
  hostname: "localhost", 
  database: "postgres",
  ssl: true, 
  ssl_opts: [certfile: '/home/certs/my_cert.pem'] 
)

Also Liked

voltone

voltone

It depends what you’re trying to achieve by using TLS. If you just want to authenticate with a client certificate while obscuring data from passive observers, then just a :certfile option may work, assuming this one file contains the client certificate, any intermediate certificates needed, and the private key. If the private key is stored in a separate file you’d have to pass a :keyfile option as well.

If you want to strongly authenticate the server, to prevent active (MitM) attacks, you’re going to have to pass in a few more options, starting with verify: :verify_peer, the server’s hostname (using the :server_name_indication option is easiest) and the trusted CA certificates (using the :cacertfile option, which may interfere with selection of the client certificate’s intermediate CA certs).

Fl4m3Ph03n1x

Fl4m3Ph03n1x

This is really valuable information. I felt quite lost reading through all of those examples, it’s always good to have some extra directions.

We’re not completely sure of our implementation, but if we have additional questions we’ll be sure to post them in the forum.

As a followup from this discussion I made a PR to improve the docs:

https://github.com/elixir-ecto/postgrex/pull/486

As a newcomer, I truly believe I needed more directions and the docs were at fault. This is my approach to improving them (the simplest approach I could think of, though I am still not convinced it’s enough).

Feel free to drop in with any suggestions on how to improve.

hauleth

hauleth

It tells you to look into ssl module of Erlang standard library. To be exact you should look at tls_option/0. In the end it is keyword list with values specified in these type specs.

Last Post!

Fl4m3Ph03n1x

Fl4m3Ph03n1x

This is really valuable information. I felt quite lost reading through all of those examples, it’s always good to have some extra directions.

We’re not completely sure of our implementation, but if we have additional questions we’ll be sure to post them in the forum.

As a followup from this discussion I made a PR to improve the docs:

https://github.com/elixir-ecto/postgrex/pull/486

As a newcomer, I truly believe I needed more directions and the docs were at fault. This is my approach to improving them (the simplest approach I could think of, though I am still not convinced it’s enough).

Feel free to drop in with any suggestions on how to improve.

Where Next?

Popular in Questions Top

vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
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
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
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

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
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
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
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

We're in Beta

About us Mission Statement