Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

Improve boolean environment variables reading (#4632)

ruslandogaff3ee1ca3b27
diff --git a/config/runtime.exs b/config/runtime.exs
index aa1906df0d..7d6f5d4ce0 100644
--- a/config/runtime.exs
+++ b/config/runtime.exs
@@ -246,14 +246,9 @@ data_dir = data_dir || persistent_cache_dir || System.get_env("DEFAULT_DATA_DIR"
persistent_cache_dir = persistent_cache_dir || data_dir
enable_email_verification =
- config_dir
- |> get_var_from_path_or_env("ENABLE_EMAIL_VERIFICATION", "false")
- |> String.to_existing_atom()
+ get_bool_from_path_or_env(config_dir, "ENABLE_EMAIL_VERIFICATION", false)
-is_selfhost =
- config_dir
- |> get_var_from_path_or_env("SELFHOST", "true")
- |> String.to_existing_atom()
+is_selfhost = get_bool_from_path_or_env(config_dir, "SELFHOST", true)
# by default, only registration from invites is enabled in CE
disable_registration_default =
@@ -279,15 +274,10 @@ custom_script_name =
config_dir
|> get_var_from_path_or_env("CUSTOM_SCRIPT_NAME", "script")
-disable_cron =
- config_dir
- |> get_var_from_path_or_env("DISABLE_CRON", "false")
- |> String.to_existing_atom()
+disable_cron = get_bool_from_path_or_env(config_dir, "DISABLE_CRON", false)
log_failed_login_attempts =
- config_dir
- |> get_var_from_path_or_env("LOG_FAILED_LOGIN_ATTEMPTS", "false")
- |> String.to_existing_atom()
+ get_bool_from_path_or_env(config_dir, "LOG_FAILED_LOGIN_ATTEMPTS", false)
websocket_url = get_var_from_path_or_env(config_dir, "WEBSOCKET_URL", "")
@@ -426,7 +416,7 @@ if config_env() in [:ce, :ce_dev, :ce_test] do
end
db_maybe_ipv6 =
- if get_var_from_path_or_env(config_dir, "ECTO_IPV6") do
+ if get_bool_from_path_or_env(config_dir, "ECTO_IPV6") do
if config_env() in [:ce, :ce_dev, :ce_test] do
Logger.warning(
"ECTO_IPV6 is no longer necessary as all TCP connections now try IPv6 automatically with IPv4 fallback"
@@ -527,9 +517,7 @@ config :plausible, Plausible.HelpScout,
config :plausible, :imported,
max_buffer_size: get_int_from_path_or_env(config_dir, "IMPORTED_MAX_BUFFER_SIZE", 10_000)
-maybe_ch_ipv6 =
- get_var_from_path_or_env(config_dir, "ECTO_CH_IPV6", "false")
- |> String.to_existing_atom()
+maybe_ch_ipv6 = get_bool_from_path_or_env(config_dir, "ECTO_CH_IPV6", false)
if maybe_ch_ipv6 && config_env() in [:ce, :ce_dev, :ce_test] do
Logger.warning(
@@ -637,27 +625,22 @@ case mailer_adapter do
password: get_var_from_path_or_env(config_dir, "SMTP_USER_PWD"),
tls: :if_available,
allowed_tls_versions: [:tlsv1, :"tlsv1.1", :"tlsv1.2"],
- ssl: get_var_from_path_or_env(config_dir, "SMTP_HOST_SSL_ENABLED") || false,
+ ssl: get_bool_from_path_or_env(config_dir, "SMTP_HOST_SSL_ENABLED", false),
retries: get_var_from_path_or_env(config_dir, "SMTP_RETRIES") || 2,
- no_mx_lookups: get_var_from_path_or_env(config_dir, "SMTP_MX_LOOKUPS_ENABLED") || true
+ no_mx_lookups: get_bool_from_path_or_env(config_dir, "SMTP_MX_LOOKUPS_ENABLED", true)
"Bamboo.Mua" ->
config :plausible, Plausible.Mailer, adapter: Bamboo.Mua
# prevents common problems with Erlang's TLS v1.3
middlebox_comp_mode =
- get_var_from_path_or_env(config_dir, "SMTP_MIDDLEBOX_COMP_MODE", "false")
+ get_bool_from_path_or_env(config_dir, "SMTP_MIDDLEBOX_COMP_MODE", false)
- middlebox_comp_mode = String.to_existing_atom(middlebox_comp_mode)
config :plausible, Plausible.Mailer, ssl: [middlebox_comp_mode: middlebox_comp_mode]
if relay = get_var_from_path_or_env(config_dir, "SMTP_HOST_ADDR") do
port = get_int_from_path_or_env(config_dir, "SMTP_HOST_PORT", 587)
-
- ssl_enabled =
- if ssl_enabled = get_var_from_path_or_env(config_dir, "SMTP_HOST_SSL_ENABLED") do
- String.to_existing_atom(ssl_enabled)
- end
+ ssl_enabled = get_bool_from_path_or_env(config_dir, "SMTP_HOST_SSL_ENABLED")
protocol =
cond do
@@ -897,10 +880,7 @@ end
config :tzdata, :data_dir, Path.join(persistent_cache_dir || System.tmp_dir!(), "tzdata_data")
-promex_disabled? =
- config_dir
- |> get_var_from_path_or_env("PROMEX_DISABLED", "true")
- |> String.to_existing_atom()
+promex_disabled? = get_bool_from_path_or_env(config_dir, "PROMEX_DISABLED", true)
config :plausible, Plausible.PromEx,
disabled: promex_disabled?,
@@ -927,10 +907,7 @@ if not is_selfhost do
config :plausible, Plausible.Site, default_ingest_threshold: site_default_ingest_threshold
end
-s3_disabled? =
- config_dir
- |> get_var_from_path_or_env("S3_DISABLED", "true")
- |> String.to_existing_atom()
+s3_disabled? = get_bool_from_path_or_env(config_dir, "S3_DISABLED", true)
unless s3_disabled? do
s3_env = [
diff --git a/lib/plausible/helpers/config.ex b/lib/plausible/helpers/config.ex
index aa28255d03..ed5833bd08 100644
--- a/lib/plausible/helpers/config.ex
+++ b/lib/plausible/helpers/config.ex
@@ -23,4 +23,31 @@ defmodule Plausible.ConfigHelpers do
end
end
end
+
+ def get_bool_from_path_or_env(config_dir, var_name, default \\ nil) do
+ case get_var_from_path_or_env(config_dir, var_name) do
+ nil -> default
+ var -> parse_bool(var)
+ end
+ end
+
+ @var_true ["1", "t", "true", "y", "yes", "on"]
+ @var_false ["0", "f", "false", "n", "no", "off"]
+ @var_bool_message Enum.zip_with(@var_true, @var_false, fn t, f -> [t, f] end)
+ |> List.flatten()
+ |> Enum.join(", ")
+
+ defp parse_bool(var) do
+ case String.downcase(var) do
+ t when t in @var_true ->
+ true
+
+ f when f in @var_false ->
+ false
+
+ _ ->
+ raise ArgumentError,
+ "Invalid boolean value: #{inspect(var)}. Expected one of: " <> @var_bool_message
+ end
+ end
end
diff --git a/test/plausible/config_test.exs b/test/plausible/config_test.exs
index 07ae08c1c1..46eaa2c8d1 100644
--- a/test/plausible/config_test.exs
+++ b/test/plausible/config_test.exs
@@ -1,5 +1,45 @@
defmodule Plausible.ConfigTest do
use ExUnit.Case
+ import Plausible.ConfigHelpers
+
+ describe "get_bool_from_path_or_env/3" do
+ test "parses truthy vars" do
+ truthy = ["1", "t", "true", "y", "yes", "on"]
+
+ for var <- truthy do
+ env = [{"ENABLE_EMAIL_VERIFICATION", var}]
+ config = runtime_config(env)
+ assert get_in(config, [:plausible, :selfhost, :enable_email_verification]) == true
+ end
+ end
+
+ test "parses false vars" do
+ falsy = ["0", "f", "false", "n", "no", "off"]
+
+ for var <- falsy do
+ env = [{"ENABLE_EMAIL_VERIFICATION", var}]
+ config = runtime_config(env)
+ assert get_in(config, [:plausible, :selfhost, :enable_email_verification]) == false
+ end
+ end
+
+ test "supports defaults" do
+ put_system_env_undo([{"ENABLE_EMAIL_VERIFICATION", nil}])
+ config_dir = "/run/secrets"
+
+ assert get_bool_from_path_or_env(config_dir, "ENABLE_EMAIL_VERIFICATION") == nil
+ assert get_bool_from_path_or_env(config_dir, "ENABLE_EMAIL_VERIFICATION", true) == true
+ assert get_bool_from_path_or_env(config_dir, "ENABLE_EMAIL_VERIFICATION", false) == false
+ end
+
+ test "raises on invalid var" do
+ env = [{"ENABLE_EMAIL_VERIFICATION", "YOLO"}]
+
+ assert_raise ArgumentError,
+ "Invalid boolean value: \"YOLO\". Expected one of: 1, 0, t, f, true, false, y, n, yes, no, on, off",
+ fn -> runtime_config(env) end
+ end
+ end
describe "mailer" do
test "mailer email default" do
@@ -132,9 +172,9 @@ defmodule Plausible.ConfigTest do
{:password, "one"},
{:tls, :if_available},
{:allowed_tls_versions, [:tlsv1, :"tlsv1.1", :"tlsv1.2"]},
- {:ssl, "true"},
+ {:ssl, true},
{:retries, "3"},
- {:no_mx_lookups, "true"}
+ {:no_mx_lookups, true}
]
end