Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.
TypeScript analyticsanalytics-dashboardclickhouseelixirgoogle-analyticsmarketingopen-source-analyticsphoenixplausible-analyticspostgresqlprivacyprivacy-friendlyself-hostedsimple-analyticsstatisticstailwindcssweb-analyticswebsite-analyticswebsite-statswebsite-tracking
Improve boolean environment variables reading (#4632)
ruslandogaff3ee1ca3b27
diff --git a/config/runtime.exs b/config/runtime.exsindex 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_direnable_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 CEdisable_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] doenddb_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") doif config_env() in [:ce, :ce_dev, :ce_test] doLogger.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] doLogger.warning(@@ -637,27 +625,22 @@ case mailer_adapter dopassword: 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.3middlebox_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") doport = 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 @@ endconfig :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 doconfig :plausible, Plausible.Site, default_ingest_threshold: site_default_ingest_thresholdend-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? dos3_env = [diff --git a/lib/plausible/helpers/config.ex b/lib/plausible/helpers/config.exindex aa28255d03..ed5833bd08 100644--- a/lib/plausible/helpers/config.ex+++ b/lib/plausible/helpers/config.ex@@ -23,4 +23,31 @@ defmodule Plausible.ConfigHelpers doendendend++ 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+ endenddiff --git a/test/plausible/config_test.exs b/test/plausible/config_test.exsindex 07ae08c1c1..46eaa2c8d1 100644--- a/test/plausible/config_test.exs+++ b/test/plausible/config_test.exs@@ -1,5 +1,45 @@defmodule Plausible.ConfigTest douse 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+ enddescribe "mailer" dotest "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