From 8a3e8bbdf0ded6a5d2e2d33de67d44f3d4db9737 Mon Sep 17 00:00:00 2001 From: Andrew Dryga Date: Wed, 1 Mar 2023 11:44:30 -0600 Subject: [PATCH] Add protocol implementation for IPPort struct and tests (#1478) Closes #1477 --- apps/fz_http/lib/fz_http/config/resolver.ex | 5 ++-- apps/fz_http/lib/fz_http/types/protocols.ex | 8 ++++++ .../fz_http_web/acceptance/admin_test.exs | 22 +++++++++++++++ .../setting_live/client_defaults_test.exs | 28 ++++++++++++++++++- apps/fz_http/test/support/conn_case.ex | 1 + 5 files changed, 60 insertions(+), 4 deletions(-) diff --git a/apps/fz_http/lib/fz_http/config/resolver.ex b/apps/fz_http/lib/fz_http/config/resolver.ex index 90eaf65ed..690ad87ba 100644 --- a/apps/fz_http/lib/fz_http/config/resolver.ex +++ b/apps/fz_http/lib/fz_http/config/resolver.ex @@ -61,10 +61,9 @@ defmodule FzHttp.Config.Resolver do defp fetch_process_value(pid, key) do with {:dictionary, pdict} <- :erlang.process_info(pid, :dictionary), {^key, value} <- List.keyfind(pdict, key, 0) do - value + {:ok, value} else - _other -> - :error + _other -> :error end end diff --git a/apps/fz_http/lib/fz_http/types/protocols.ex b/apps/fz_http/lib/fz_http/types/protocols.ex index f4eca4adb..37b3ce981 100644 --- a/apps/fz_http/lib/fz_http/types/protocols.ex +++ b/apps/fz_http/lib/fz_http/types/protocols.ex @@ -5,3 +5,11 @@ end defimpl Phoenix.HTML.Safe, for: Postgrex.INET do def to_iodata(%Postgrex.INET{} = inet), do: FzHttp.Types.INET.to_string(inet) end + +defimpl String.Chars, for: FzHttp.Types.IPPort do + def to_string(%FzHttp.Types.IPPort{} = ip_port), do: FzHttp.Types.IPPort.to_string(ip_port) +end + +defimpl Phoenix.HTML.Safe, for: FzHttp.Types.IPPort do + def to_iodata(%FzHttp.Types.IPPort{} = ip_port), do: FzHttp.Types.IPPort.to_string(ip_port) +end diff --git a/apps/fz_http/test/fz_http_web/acceptance/admin_test.exs b/apps/fz_http/test/fz_http_web/acceptance/admin_test.exs index b1954c764..95b568568 100644 --- a/apps/fz_http/test/fz_http_web/acceptance/admin_test.exs +++ b/apps/fz_http/test/fz_http_web/acceptance/admin_test.exs @@ -325,6 +325,28 @@ defmodule FzHttpWeb.Acceptance.AdminTest do %Postgrex.INET{address: {0, 0, 0, 0, 0, 0, 0, 0}, netmask: 0} ] end + + feature "can use IP with a port in default client endpoint and host in DNS", %{ + session: session + } do + session + |> visit(~p"/settings/client_defaults") + |> assert_el(Query.text("Client Defaults", count: 2)) + |> fill_in(Query.fillable_field("configuration[default_client_dns]"), + with: "dns.example.com" + ) + |> fill_in(Query.fillable_field("configuration[default_client_endpoint]"), + with: "1.2.3.4:8123" + ) + |> click(Query.button("Save")) + # XXX: We need to show a flash that settings are saved + |> visit(~p"/settings/client_defaults") + |> assert_el(Query.text("Client Defaults", count: 2)) + + assert configuration = FzHttp.Config.fetch_db_config!() + assert configuration.default_client_endpoint == "1.2.3.4:8123" + assert configuration.default_client_dns == ["dns.example.com"] + end end describe "customization" do diff --git a/apps/fz_http/test/fz_http_web/live/setting_live/client_defaults_test.exs b/apps/fz_http/test/fz_http_web/live/setting_live/client_defaults_test.exs index 8b5178c18..c3b179b57 100644 --- a/apps/fz_http/test/fz_http_web/live/setting_live/client_defaults_test.exs +++ b/apps/fz_http/test/fz_http_web/live/setting_live/client_defaults_test.exs @@ -31,7 +31,7 @@ defmodule FzHttpWeb.SettingLive.ClientDefaultsTest do path = ~p"/settings/client_defaults" {:ok, view, html} = live(conn, path) - %{html: html, view: view} + %{conn: conn, html: html, view: view} end test "renders current configuration", %{html: html} do @@ -92,6 +92,32 @@ defmodule FzHttpWeb.SettingLive.ClientDefaultsTest do """ end + test "blocks overridden default client endpoint" do + FzHttp.Config.put_system_env_override(:default_client_endpoint, "1.2.3.4:1234") + + {_admin_user, conn} = admin_conn(%{}) + {:ok, view, _html} = live(conn, ~p"/settings/client_defaults") + + test_view = + view + |> element("#client_defaults_form_component") + |> render() + + assert Floki.find(test_view, "#client_defaults_form_component_default_client_endpoint") == + [ + {"input", + [ + {"class", "input "}, + {"disabled", "disabled"}, + {"id", "client_defaults_form_component_default_client_endpoint"}, + {"name", "configuration[default_client_endpoint]"}, + {"placeholder", "firezone.example.com"}, + {"type", "text"}, + {"value", "Set in environment variable DEFAULT_CLIENT_ENDPOINT: 1.2.3.4:1234"} + ], []} + ] + end + test "updates default client persistent_keepalive", %{view: view} do test_view = view diff --git a/apps/fz_http/test/support/conn_case.ex b/apps/fz_http/test/support/conn_case.ex index 1a9390e19..eecdd21bc 100644 --- a/apps/fz_http/test/support/conn_case.ex +++ b/apps/fz_http/test/support/conn_case.ex @@ -27,6 +27,7 @@ defmodule FzHttpWeb.ConnCase do import Phoenix.ConnTest import Phoenix.LiveViewTest import FzHttp.TestHelpers + import FzHttpWeb.ConnCase # The default endpoint for testing @endpoint FzHttpWeb.Endpoint