Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Postgrex SSL config #4460

Merged
merged 6 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:[email protected]:25060/defaultdb",
socket_options: []
]
end

test "DATABASE_CACERTFILE enables SSL" do
env = [
{"DATABASE_URL",
"postgresql://your_username:[email protected]:25060/defaultdb"},
{"DATABASE_CACERTFILE", "/path/to/cacert.pem"}
]

config = runtime_config(env)

assert get_in(config, [:plausible, Plausible.Repo]) == [
url:
"postgresql://your_username:[email protected]: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