From e557f51ee474bf45631e077a340c87dc1f7ea046 Mon Sep 17 00:00:00 2001 From: Chris Laskey Date: Wed, 14 Apr 2021 14:31:00 -0400 Subject: [PATCH] Application Config Page Toggle --- .env.demo | 1 + .env.example | 1 + .env.travis | 1 + apps/artemis_web/config/config.exs | 3 +- apps/artemis_web/lib/artemis_web.ex | 23 +++++++++++ .../lib/artemis_web/config/navigation.ex | 5 ++- .../application_config_controller.ex | 40 +++++++++++++------ .../application_config/show.html.eex | 14 ++++++- 8 files changed, 72 insertions(+), 16 deletions(-) diff --git a/.env.demo b/.env.demo index c02ace1c..771b6b1e 100644 --- a/.env.demo +++ b/.env.demo @@ -171,6 +171,7 @@ ARTEMIS_WEB_ENABLED_AUTH_PROVIDERS=system_user ARTEMIS_WEB_HOSTNAME=localhost ARTEMIS_WEB_PORT=4077 ARTEMIS_WEB_LIVE_VIEW_SECRET_KEY=avY1wGQQwgSIDF1v7GXT5p8AmSbUvb32 +ARTEMIS_WEB_APPLICATION_CONFIG_PAGE_ENABLED=true ### Ueberauth diff --git a/.env.example b/.env.example index 48883fea..c6d34072 100644 --- a/.env.example +++ b/.env.example @@ -174,6 +174,7 @@ ARTEMIS_WEB_PORT=4077 # Copy the output below and uncomment env variable: # # ARTEMIS_WEB_LIVE_VIEW_SECRET_KEY= +ARTEMIS_WEB_APPLICATION_CONFIG_PAGE_ENABLED=false ### Ueberauth diff --git a/.env.travis b/.env.travis index 30535154..6095a851 100644 --- a/.env.travis +++ b/.env.travis @@ -161,6 +161,7 @@ ARTEMIS_WEB_ENABLED_AUTH_PROVIDERS=system_user ARTEMIS_WEB_HOSTNAME=localhost ARTEMIS_WEB_PORT=4077 ARTEMIS_WEB_LIVE_VIEW_SECRET_KEY=KUf9d9YMmWmNw9xnEdSXWDspN+F5+VaX +ARTEMIS_WEB_APPLICATION_CONFIG_PAGE_ENABLED=false ### Ueberauth diff --git a/apps/artemis_web/config/config.exs b/apps/artemis_web/config/config.exs index 0f6c7fcc..54571abe 100644 --- a/apps/artemis_web/config/config.exs +++ b/apps/artemis_web/config/config.exs @@ -10,7 +10,8 @@ config :artemis_web, ArtemisWeb.Endpoint, secret_key_base: System.get_env("ARTEMIS_SECRET_KEY"), render_errors: [view: ArtemisWeb.ErrorView, accepts: ~w(html json)], pubsub_server: ArtemisPubSub, - live_view: [signing_salt: System.get_env("ARTEMIS_WEB_LIVE_VIEW_SECRET_KEY")] + live_view: [signing_salt: System.get_env("ARTEMIS_WEB_LIVE_VIEW_SECRET_KEY")], + application_config_page: [enabled: System.get_env("ARTEMIS_WEB_APPLICATION_CONFIG_PAGE_ENABLED")] config :artemis_web, ArtemisWeb.Guardian, allowed_algos: ["HS512"], diff --git a/apps/artemis_web/lib/artemis_web.ex b/apps/artemis_web/lib/artemis_web.ex index cda8244c..d401b617 100644 --- a/apps/artemis_web/lib/artemis_web.ex +++ b/apps/artemis_web/lib/artemis_web.ex @@ -103,6 +103,29 @@ defmodule ArtemisWeb do end end + # Config + + defp app_config_enabled?(conn, app_config, render_controller) do + case apply(Artemis.Helpers.AppConfig, :enabled?, app_config) do + true -> render_controller.() + false -> render_not_found(conn) + end + end + + defp app_config_enabled_any?(conn, app_configs, render_controller) do + case Enum.any?(app_configs, &apply(Artemis.Helpers.AppConfig, :enabled?, &1)) do + true -> render_controller.() + false -> render_not_found(conn) + end + end + + defp app_config_enabled_all?(conn, app_configs, render_controller) do + case Enum.all?(app_configs, &apply(Artemis.Helpers.AppConfig, :enabled?, &1)) do + true -> render_controller.() + false -> render_not_found(conn) + end + end + # Features defp feature_active?(conn, feature, render_controller) do diff --git a/apps/artemis_web/lib/artemis_web/config/navigation.ex b/apps/artemis_web/lib/artemis_web/config/navigation.ex index 18f1048a..490cbe0d 100644 --- a/apps/artemis_web/lib/artemis_web/config/navigation.ex +++ b/apps/artemis_web/lib/artemis_web/config/navigation.ex @@ -16,7 +16,10 @@ defmodule ArtemisWeb.Config.Navigation do [ label: "Application Configs", path: &Routes.application_config_path(&1, :index), - verify: &has?(&1, "application-configs:list") + verify: fn current_user -> + has?(current_user, "application-configs:list") && + Artemis.Helpers.AppConfig.enabled?(:artemis_web, ArtemisWeb.Endpoint, :application_config_page) + end ], [ label: "System Tasks", diff --git a/apps/artemis_web/lib/artemis_web/controllers/application_config_controller.ex b/apps/artemis_web/lib/artemis_web/controllers/application_config_controller.ex index fb74f1a9..9f94c35d 100644 --- a/apps/artemis_web/lib/artemis_web/controllers/application_config_controller.ex +++ b/apps/artemis_web/lib/artemis_web/controllers/application_config_controller.ex @@ -1,7 +1,7 @@ defmodule ArtemisWeb.ApplicationConfigController do use ArtemisWeb, :controller - @allowed_application_configs [ + @allowed_applications [ :artemis, :artemis_api, :artemis_log, @@ -11,22 +11,28 @@ defmodule ArtemisWeb.ApplicationConfigController do def index(conn, _params) do authorize(conn, "application-configs:list", fn -> - assigns = [ - application_configs: @allowed_application_configs - ] + app_config_enabled?(conn, [:artemis_web, ArtemisWeb.Endpoint, :application_config_page], fn -> + assigns = [ + application_configs: @allowed_applications + ] - render(conn, "index.html", assigns) + render(conn, "index.html", assigns) + end) end) end def show(conn, %{"id" => id}) do authorize(conn, "application-configs:show", fn -> - assigns = [ - application_config: get_application_config(id), - application_id: id - ] - - render(conn, "show.html", assigns) + app_config_enabled?(conn, [:artemis_web, ArtemisWeb.Endpoint, :application_config_page], fn -> + assigns = [ + application_config: get_application_config(id), + application_id: id, + application_spec: get_application_spec(id), + applications_started: Application.started_applications() + ] + + render(conn, "show.html", assigns) + end) end) end @@ -34,11 +40,21 @@ defmodule ArtemisWeb.ApplicationConfigController do defp get_application_config(id) do application = String.to_existing_atom(id) - allowed? = Enum.member?(@allowed_application_configs, application) + allowed? = Enum.member?(@allowed_applications, application) case allowed? do true -> Application.get_all_env(application) false -> {:error, "Invalid Application"} end end + + defp get_application_spec(id) do + application = String.to_existing_atom(id) + allowed? = Enum.member?(@allowed_applications, application) + + case allowed? do + true -> Application.spec(application) + false -> {:error, "Invalid Application"} + end + end end diff --git a/apps/artemis_web/lib/artemis_web/templates/application_config/show.html.eex b/apps/artemis_web/lib/artemis_web/templates/application_config/show.html.eex index 52cb3d9b..1a85e910 100644 --- a/apps/artemis_web/lib/artemis_web/templates/application_config/show.html.eex +++ b/apps/artemis_web/lib/artemis_web/templates/application_config/show.html.eex @@ -15,7 +15,17 @@ <%= render_flash_notifications @conn %>
- <%= h3 "Raw Data" %> -
<%= inspect @application_config, pretty: true %>
+ <%= h3 "Application Config" %> +
<%= inspect @application_config, limit: :infinity, pretty: true, printable_limit: :infinity %>
+
+ +
+ <%= h3 "Application Spec" %> +
<%= inspect @application_spec, limit: :infinity, pretty: true, printable_limit: :infinity  %>
+
+ +
+ <%= h3 "Applications Started" %> +
<%= inspect @applications_started, limit: :infinity, pretty: true, printable_limit: :infinity  %>