Skip to content

Commit

Permalink
Push more test configs into localstack secrets manager
Browse files Browse the repository at this point in the history
  • Loading branch information
mbklein committed Nov 5, 2024
1 parent a5bfd79 commit e7b4217
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 196 deletions.
87 changes: 2 additions & 85 deletions app/lib/meadow/config/runtime.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,7 @@ defmodule Meadow.Config.Runtime do

alias Meadow.Config.Pipeline

@config_map %{
ezid: "infrastructure/ezid",
honeybadger: "infrastructure/honeybadger",
iiif: "infrastructure/iiif",
index: "infrastructure/index",
inference: "infrastructure/inference",
ldap: "infrastructure/ldap",
meadow: "config/meadow",
nusso: "infrastructure/nusso",
wildcard_ssl: "config/wildcard_ssl"
}
import Meadow.Config.Secrets

# TODO: UPDATE ALL get_secret(:meadow, ["dc",...]) to use DC secrets

Expand Down Expand Up @@ -102,7 +92,7 @@ defmodule Meadow.Config.Runtime do
pubsub_server: Meadow.PubSub

config :meadow, Meadow.Search.Cluster,
url: get_secret(:meadow, ["index", "index_endpoint"]),
url: get_secret(:index, ["endpoint"]),
default_options: [
timeout: 20_000,
recv_timeout: 90_000
Expand Down Expand Up @@ -302,77 +292,4 @@ defmodule Meadow.Config.Runtime do

:ok
end

def get_secret(config, path, default \\ nil) do
secrets =
case :ets.lookup(:secret_cache, config) |> Keyword.get(config) do
nil ->
loaded = load_config(@config_map[config])
:ets.insert(:secret_cache, {config, loaded})
loaded

value ->
value
end

case get_in(secrets, path) do
nil -> default
secret -> secret
end
end

defp load_config(config_path) do
System.get_env("SECRETS_PATH", nil) |> load_config(config_path)
end

defp load_config(nil, config_path), do: retrieve_config(config_path)

defp load_config(prefix, config_path),
do: Path.join(Enum.reject([prefix, config_path], &is_nil/1)) |> retrieve_config()

defp retrieve_config(path) do
case ExAws.SecretsManager.get_secret_value(path) |> ExAws.request() do
{:ok, %{"SecretString" => secret_string}} -> Jason.decode!(secret_string)
{:error, _} -> nil
end
end

def environment do
if function_exported?(Mix, :env, 0), do: Mix.env(), else: :prod
end

def prefix do
env =
cond do
System.get_env("RELEASE_NAME") -> nil
function_exported?(Mix, :env, 0) -> Mix.env()
true -> nil
end

[System.get_env("DEV_PREFIX"), env] |> Enum.reject(&is_nil/1) |> Enum.join("-")
end

def prefix(val), do: [prefix(), to_string(val)] |> reject_empty() |> Enum.join("-")
# defp atom_prefix(val), do: prefix(val) |> String.to_atom()
defp reject_empty(list), do: Enum.reject(list, &(is_nil(&1) or &1 == ""))

defp environment_int(key, default) do
case System.get_env(key) do
nil -> default
val -> String.to_integer(val)
end
end

def project_root do
if Code.loaded?(Mix),
do: Path.dirname(Path.dirname(Mix.Project.build_path())),
else: Path.dirname(:code.priv_dir(:meadow))
end

defp priv_path(path) do
case :code.priv_dir(:meadow) do
{:error, :bad_name} -> Path.join([".", "priv", path])
priv_dir -> priv_dir |> to_string() |> Path.join(path)
end
end
end
2 changes: 1 addition & 1 deletion app/lib/meadow/config/runtime/dev.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule Meadow.Config.Runtime.Dev do
Load and apply Meadow's runtime configuration for the dev environment
"""

import Meadow.Config.Runtime
import Meadow.Config.Secrets

defp fetch_cert do
cert_path = project_root() |> Path.join("priv/cert")
Expand Down
41 changes: 1 addition & 40 deletions app/lib/meadow/config/runtime/test.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,7 @@ defmodule Meadow.Config.Runtime.Test do

config :meadow,
index_interval: 1234,
mediaconvert_client: MediaConvert.Mock,
streaming_url: "https://test-streaming-url/",
iiif_server_url: "http://localhost:8184/iiif/3/",
iiif_manifest_url_deprecated:
"http://test-pyramids.s3.localhost.localstack.cloud:4566/public/",
digital_collections_url: "https://fen.rdc-staging.library.northwestern.edu/"
mediaconvert_client: MediaConvert.Mock

# Configures lambda scripts
config :meadow, :lambda,
Expand All @@ -38,27 +33,13 @@ defmodule Meadow.Config.Runtime.Test do
required_checksum_tags: ["computed-md5"],
checksum_wait_timeout: 15_000

config :meadow,
ark: %{
default_shoulder: "ark:/12345/nu2",
user: "mockuser",
password: "mockpassword",
target_url: "https://devbox.library.northwestern.edu:3333/items/",
url: "http://localhost:3944/"
}

config :meadow, :elasticsearch_retry,
interval: 100,
max_retries: 3

config :authoritex, authorities: [Authoritex.Mock, NUL.Authority]

config :meadow, Meadow.Repo,
username: "docker",
password: "d0ck3r",
database: "meadow",
hostname: "localhost",
port: 5432,
show_sensitive_data_on_connection_error: true,
timeout: 60_000,
connect_timeout: 60_000,
Expand All @@ -77,26 +58,6 @@ defmodule Meadow.Config.Runtime.Test do
],
iiif_distribution_id: nil

config :ueberauth, Ueberauth,
providers: [
nusso:
{Ueberauth.Strategy.NuSSO,
[
base_url: "https://northwestern-dev.apigee.net/agentless-websso/",
callback_path: "/auth/nusso/callback",
consumer_key: "test-sso-key",
include_attributes: false
]}
]

config :exldap, :settings,
base: "OU=test,DC=library,DC=northwestern,DC=edu",
server: "localhost",
port: 389,
user_dn: "cn=Administrator,cn=Users,dc=library,dc=northwestern,dc=edu",
password: "d0ck3rAdm1n!",
ssl: false

config :ex_unit,
assert_receive_timeout: 500

Expand Down
91 changes: 91 additions & 0 deletions app/lib/meadow/config/secrets.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
defmodule Meadow.Config.Secrets do
@moduledoc """
Functions for retrieving and loading configuration from AWS Secrets Manager and
the local runtime environment
"""

@config_map %{
ezid: "infrastructure/ezid",
honeybadger: "infrastructure/honeybadger",
iiif: "infrastructure/iiif",
index: "infrastructure/index",
inference: "infrastructure/inference",
ldap: "infrastructure/ldap",
meadow: "config/meadow",
nusso: "infrastructure/nusso",
wildcard_ssl: "config/wildcard_ssl"
}

def get_secret(config, path, default \\ nil) do
secrets =
case :ets.lookup(:secret_cache, config) |> Keyword.get(config) do
nil ->
loaded = load_config(@config_map[config])
:ets.insert(:secret_cache, {config, loaded})
loaded

value ->
value
end

case get_in(secrets, path) do
nil -> default
secret -> secret
end
end

defp load_config(config_path) do
System.get_env("SECRETS_PATH", nil) |> load_config(config_path)
end

defp load_config(nil, config_path), do: retrieve_config(config_path)

defp load_config(prefix, config_path),
do: Path.join(Enum.reject([prefix, config_path], &is_nil/1)) |> retrieve_config()

defp retrieve_config(path) do
case ExAws.SecretsManager.get_secret_value(path) |> ExAws.request() do
{:ok, %{"SecretString" => secret_string}} -> Jason.decode!(secret_string)
{:error, _} -> nil
end
end

def environment do
if function_exported?(Mix, :env, 0), do: Mix.env(), else: :prod
end

def prefix do
env =
cond do
System.get_env("RELEASE_NAME") -> nil
function_exported?(Mix, :env, 0) -> Mix.env()
true -> nil
end

[System.get_env("DEV_PREFIX"), env] |> Enum.reject(&is_nil/1) |> Enum.join("-")
end

def prefix(val), do: [prefix(), to_string(val)] |> reject_empty() |> Enum.join("-")
# defp atom_prefix(val), do: prefix(val) |> String.to_atom()
defp reject_empty(list), do: Enum.reject(list, &(is_nil(&1) or &1 == ""))

def environment_int(key, default) do
case System.get_env(key) do
nil -> default
val -> String.to_integer(val)
end
end

def project_root do
if Code.loaded?(Mix),
do: Path.dirname(Path.dirname(Mix.Project.build_path())),
else: Path.dirname(:code.priv_dir(:meadow))
end

def priv_path(path) do
case :code.priv_dir(:meadow) do
{:error, :bad_name} -> Path.join([".", "priv", path])
priv_dir -> priv_dir |> to_string() |> Path.join(path)
end
end
end
4 changes: 2 additions & 2 deletions app/lib/meadow/utils/aws.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule Meadow.Utils.AWS do
Utility functions for AWS requests and object management
"""
alias Meadow.Config
alias Meadow.Config.Runtime
alias Meadow.Config.Secrets
alias Meadow.Error
alias Meadow.Utils.AWS.MultipartCopy
alias Meadow.Utils.Pairtree
Expand Down Expand Up @@ -282,5 +282,5 @@ defmodule Meadow.Utils.AWS do
end
end

defp prefix, do: Runtime.prefix()
defp prefix, do: Secrets.prefix()
end
Loading

0 comments on commit e7b4217

Please sign in to comment.