Skip to content

Commit

Permalink
Update how we configure :basic_auth, ensuring we look for environme…
Browse files Browse the repository at this point in the history
…nt variables during compile time, allowing them to match the runtime configuration and thus making `Application.compile_env/3` happy.
  • Loading branch information
zorn committed Aug 25, 2024
1 parent a29115f commit 7b75f44
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 11 deletions.
8 changes: 8 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ config :flick, FlickWeb.Endpoint,
pubsub_server: Flick.PubSub,
live_view: [signing_salt: "D3iFdkff"]

# We do basic auth for the admin index in production.
#
# During a deployment build, we expect these environment variables to be set,
# otherwise we'll put in some defaults.
config :flick, :basic_auth,
username: System.get_env("BASIC_AUTH_ADMIN_USERNAME", "flick-admin"),
password: System.get_env("BASIC_AUTH_ADMIN_PASSWORD", "unsafe-password")

# Configures the mailer
#
# By default it uses the "Local" adapter which stores the emails
Expand Down
3 changes: 0 additions & 3 deletions config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ config :flick, FlickWeb.Endpoint,
tailwind: {Tailwind, :install_and_run, [:flick, ~w(--watch)]}
]

# Basic HTTP authentication for the `/admin` pages.
config :flick, :basic_auth, username: "flick-admin", password: "unsafe-password"

# ## SSL Support
#
# In order to use HTTPS in development, a self-signed
Expand Down
16 changes: 14 additions & 2 deletions config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,21 @@ if config_env() == :prod do
secret_key_base: secret_key_base

# We do basic auth for the admin index in production.
basic_auth_username =
System.get_env("BASIC_AUTH_ADMIN_USERNAME") ||
raise """
Environment variable SECRET_KEY_BASE is missing.
"""

basic_auth_password =
System.get_env("BASIC_AUTH_ADMIN_PASSWORD") ||
raise """
Environment variable BASIC_AUTH_ADMIN_PASSWORD is missing.
"""

config :flick, :basic_auth,
username: System.get_env("BASIC_AUTH_ADMIN_USERNAME"),
password: System.get_env("BASIC_AUTH_ADMIN_PASSWORD")
username: basic_auth_username,
password: basic_auth_password

# ## SSL Support
#
Expand Down
3 changes: 0 additions & 3 deletions config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,3 @@ config :logger, level: :warning

# Initialize plugs at runtime for faster test compilation
config :phoenix, :plug_init_mode, :runtime

# Basic HTTP authentication for the `/admin` pages.
config :flick, :basic_auth, username: "flick-admin", password: "unsafe-password"
4 changes: 1 addition & 3 deletions lib/flick_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ defmodule FlickWeb.Router do
end

pipeline :admin do
# The use of `get_env/2` is temp.
# https://github.com/zorn/flick/issues/56
plug :basic_auth, Application.get_env(:flick, :basic_auth)
plug :basic_auth, Application.compile_env(:flick, :basic_auth)
end

pipeline :api do
Expand Down

0 comments on commit 7b75f44

Please sign in to comment.