Skip to content

Commit

Permalink
Move secret_key_base to an environment variable (#358)
Browse files Browse the repository at this point in the history
`secret_key_base` is a secret and should not be stored in code.

Use an init callback to load secret_key_base from the env var.
  • Loading branch information
arkadyan authored Dec 12, 2019
1 parent 5cbe865 commit 53fcc82
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ There are a number of configuration details defined in environment variables. Th

- **BUSLOC_URL**: Source of GTFS-realtime enhanced data file
- **SWIFTLY_REALTIME_VEHICLES_URL** and **SWIFTLY_AUTHORIZATION_KEY**: Source of Swiftly vehicle data
- **SECRET_KEY_BASE** Used for writing encrypted cookies. Generate a value using `mix phx.gen.secret` (only required in production)
- **COGNITO_DOMAIN**, **COGNITO_CLIENT_ID**, **COGNITO_CLIENT_SECRET**, **COGNITO_USER_POOL_ID**, **COGNITO_AWS_REGION**, and **GUARDIAN_SECRET_KEY**: Authentication/authorization details (only required in production)
- **STATIC_SCHEME**, **STATIC_HOST**, **STATIC_PATH**, and **STATIC_PORT**: CDN details (only required in production)
1 change: 0 additions & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ config :skate, :redirect_http?, false
# Configures the endpoint
config :skate, SkateWeb.Endpoint,
url: [host: "localhost"],
secret_key_base: "HjFPO4gzlDmAuvgXBMSd4MIFGLhvKHYfXpNkIoXRM5LMGxQhjYW0NQVdP2QFgZND",
render_errors: [view: SkateWeb.ErrorView, accepts: ~w(html json)],
pubsub: [name: Skate.PubSub, adapter: Phoenix.PubSub.PG2]

Expand Down
1 change: 1 addition & 0 deletions config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ config :skate, SkateWeb.Endpoint,
debug_errors: true,
code_reloader: true,
check_origin: false,
secret_key_base: "local_secret_key_base_at_least_64_bytes_________________________________",
watchers: [
node: [
"node_modules/webpack/bin/webpack.js",
Expand Down
3 changes: 2 additions & 1 deletion config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ config :skate, Gtfs.CacheFile, cache_filename: "test_cache.terms"
# you can enable the server option below.
config :skate, SkateWeb.Endpoint,
http: [port: 4002],
server: false
server: false,
secret_key_base: "local_secret_key_base_at_least_64_bytes_________________________________"

config :skate, SkateWeb.AuthManager, secret_key: "dev key"

Expand Down
15 changes: 15 additions & 0 deletions lib/skate_web/endpoint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,19 @@ defmodule SkateWeb.Endpoint do
signing_salt: "jkUgGkwy"

plug SkateWeb.Router

# callback for runtime configuration
def init(:supervisor, config) do
secret_key_base = System.get_env("SECRET_KEY_BASE")

config =
if secret_key_base do
Keyword.put(config, :secret_key_base, secret_key_base)
else
config[:secret_key_base] || raise "No SECRET_KEY_BASE ENV var!"
config
end

{:ok, config}
end
end

0 comments on commit 53fcc82

Please sign in to comment.