Skip to content

Commit

Permalink
Update Postgrex SSL config (#4460)
Browse files Browse the repository at this point in the history
* update postgrex config

* enable ssl only if DATABASE_CACERTFILE is set

* update tests

* changelog

---------

Co-authored-by: Cenk Kücük <cenk@plausible.io>
  • Loading branch information
ruslandoga and cnkk authored Aug 30, 2024
1 parent e9dd895 commit 19ecd3d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 24 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ All notable changes to this project will be documented in this file.
- `bounce_rate` metric now returns 0 instead of null for event:page breakdown when page has never been entry page.
- Make `TOTP_VAULT_KEY` optional plausible/analytics#4317
- Sources like 'google' and 'facebook' are now stored in capitalized forms ('Google', 'Facebook') plausible/analytics#4417
- `DATABASE_CACERTFILE` now forces TLS for PostgreSQL connections, so you don't need to add `?ssl=true` in `DATABASE_URL`

### Fixed

Expand Down
15 changes: 6 additions & 9 deletions config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ if db_socket_dir = get_var_from_path_or_env(config_dir, "DATABASE_SOCKET_DIR") d
""")
end

db_cacertfile = get_var_from_path_or_env(config_dir, "DATABASE_CACERTFILE", CAStore.file_path())
db_cacertfile = get_var_from_path_or_env(config_dir, "DATABASE_CACERTFILE")
%URI{host: db_host} = db_uri = URI.parse(db_url)
db_socket_dir? = String.starts_with?(db_host, "%2F") or db_host == ""

Expand Down Expand Up @@ -382,14 +382,11 @@ if db_socket_dir? do
else
config :plausible, Plausible.Repo,
url: db_url,
socket_options: db_maybe_ipv6,
ssl_opts: [
cacertfile: db_cacertfile,
verify: :verify_peer,
customize_hostname_check: [
match_fun: :public_key.pkix_verify_hostname_match_fun(:https)
]
]
socket_options: db_maybe_ipv6

if db_cacertfile do
config :plausible, Plausible.Repo, ssl: [cacertfile: db_cacertfile]
end
end

sentry_app_version = runtime_metadata[:version] || app_version
Expand Down
33 changes: 18 additions & 15 deletions test/plausible/config_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -359,14 +359,7 @@ defmodule Plausible.ConfigTest do

assert get_in(config, [:plausible, Plausible.Repo]) == [
url: "postgres://postgres:postgres@plausible_db:5432/plausible_db",
socket_options: [],
ssl_opts: [
cacertfile: CAStore.file_path(),
verify: :verify_peer,
customize_hostname_check: [
match_fun: :public_key.pkix_verify_hostname_match_fun(:https)
]
]
socket_options: []
]
end

Expand Down Expand Up @@ -405,17 +398,27 @@ defmodule Plausible.ConfigTest do

config = runtime_config(env)

assert get_in(config, [:plausible, Plausible.Repo]) == [
url:
"postgresql://your_username:your_password@cluster-do-user-1234567-0.db.ondigitalocean.com:25060/defaultdb",
socket_options: []
]
end

test "DATABASE_CACERTFILE enables SSL" do
env = [
{"DATABASE_URL",
"postgresql://your_username:your_password@cluster-do-user-1234567-0.db.ondigitalocean.com:25060/defaultdb"},
{"DATABASE_CACERTFILE", "/path/to/cacert.pem"}
]

config = runtime_config(env)

assert get_in(config, [:plausible, Plausible.Repo]) == [
url:
"postgresql://your_username:your_password@cluster-do-user-1234567-0.db.ondigitalocean.com:25060/defaultdb",
socket_options: [],
ssl_opts: [
cacertfile: CAStore.file_path(),
verify: :verify_peer,
customize_hostname_check: [
match_fun: :public_key.pkix_verify_hostname_match_fun(:https)
]
]
ssl: [cacertfile: "/path/to/cacert.pem"]
]
end
end
Expand Down

0 comments on commit 19ecd3d

Please sign in to comment.