Skip to content

Commit 19ecd3d

Browse files
ruslandogacnkk
andauthored
Update Postgrex SSL config (#4460)
* update postgrex config * enable ssl only if DATABASE_CACERTFILE is set * update tests * changelog --------- Co-authored-by: Cenk Kücük <[email protected]>
1 parent e9dd895 commit 19ecd3d

File tree

3 files changed

+25
-24
lines changed

3 files changed

+25
-24
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ All notable changes to this project will be documented in this file.
3434
- `bounce_rate` metric now returns 0 instead of null for event:page breakdown when page has never been entry page.
3535
- Make `TOTP_VAULT_KEY` optional plausible/analytics#4317
3636
- Sources like 'google' and 'facebook' are now stored in capitalized forms ('Google', 'Facebook') plausible/analytics#4417
37+
- `DATABASE_CACERTFILE` now forces TLS for PostgreSQL connections, so you don't need to add `?ssl=true` in `DATABASE_URL`
3738

3839
### Fixed
3940

config/runtime.exs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ if db_socket_dir = get_var_from_path_or_env(config_dir, "DATABASE_SOCKET_DIR") d
353353
""")
354354
end
355355

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

@@ -382,14 +382,11 @@ if db_socket_dir? do
382382
else
383383
config :plausible, Plausible.Repo,
384384
url: db_url,
385-
socket_options: db_maybe_ipv6,
386-
ssl_opts: [
387-
cacertfile: db_cacertfile,
388-
verify: :verify_peer,
389-
customize_hostname_check: [
390-
match_fun: :public_key.pkix_verify_hostname_match_fun(:https)
391-
]
392-
]
385+
socket_options: db_maybe_ipv6
386+
387+
if db_cacertfile do
388+
config :plausible, Plausible.Repo, ssl: [cacertfile: db_cacertfile]
389+
end
393390
end
394391

395392
sentry_app_version = runtime_metadata[:version] || app_version

test/plausible/config_test.exs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -359,14 +359,7 @@ defmodule Plausible.ConfigTest do
359359

360360
assert get_in(config, [:plausible, Plausible.Repo]) == [
361361
url: "postgres://postgres:postgres@plausible_db:5432/plausible_db",
362-
socket_options: [],
363-
ssl_opts: [
364-
cacertfile: CAStore.file_path(),
365-
verify: :verify_peer,
366-
customize_hostname_check: [
367-
match_fun: :public_key.pkix_verify_hostname_match_fun(:https)
368-
]
369-
]
362+
socket_options: []
370363
]
371364
end
372365

@@ -405,17 +398,27 @@ defmodule Plausible.ConfigTest do
405398

406399
config = runtime_config(env)
407400

401+
assert get_in(config, [:plausible, Plausible.Repo]) == [
402+
url:
403+
"postgresql://your_username:[email protected]:25060/defaultdb",
404+
socket_options: []
405+
]
406+
end
407+
408+
test "DATABASE_CACERTFILE enables SSL" do
409+
env = [
410+
{"DATABASE_URL",
411+
"postgresql://your_username:[email protected]:25060/defaultdb"},
412+
{"DATABASE_CACERTFILE", "/path/to/cacert.pem"}
413+
]
414+
415+
config = runtime_config(env)
416+
408417
assert get_in(config, [:plausible, Plausible.Repo]) == [
409418
url:
410419
"postgresql://your_username:[email protected]:25060/defaultdb",
411420
socket_options: [],
412-
ssl_opts: [
413-
cacertfile: CAStore.file_path(),
414-
verify: :verify_peer,
415-
customize_hostname_check: [
416-
match_fun: :public_key.pkix_verify_hostname_match_fun(:https)
417-
]
418-
]
421+
ssl: [cacertfile: "/path/to/cacert.pem"]
419422
]
420423
end
421424
end

0 commit comments

Comments
 (0)