From 18b0079dbe776bb7cd5b95299f424eb25a8d5b35 Mon Sep 17 00:00:00 2001 From: Jamil Bou Kheir Date: Thu, 9 Sep 2021 20:13:06 +0000 Subject: [PATCH] Fix device delete --- .env.sample | 5 +- apps/fz_common/lib/cli.ex | 6 +- apps/fz_http/lib/fz_http/devices.ex | 3 +- apps/fz_http/lib/fz_http/devices/device.ex | 6 +- apps/fz_http/lib/fz_http_web/events.ex | 4 +- .../live/account_live/show.html.heex | 42 ++--- .../live/device_live/index.html.heex | 62 ++++---- .../live/device_live/index_live.ex | 6 +- .../live/device_live/show.html.heex | 150 +++++++++--------- .../live/rule_live/index.html.heex | 39 +++-- apps/fz_http/lib/fz_http_web/mock_events.ex | 4 +- .../templates/layout/live.html.heex | 24 --- .../templates/layout/root.html.heex | 20 +-- .../templates/shared/flash.html.heex | 24 +++ .../templates/shared/heading.html.heex | 16 ++ .../lib/fz_http_web/views/shared_view.ex | 3 + .../20200228145810_create_devices.exs | 2 - apps/fz_http/priv/repo/seeds.exs | 1 - apps/fz_http/test/fz_http/devices_test.exs | 3 +- apps/fz_http/test/support/fixtures.ex | 1 - apps/fz_vpn/lib/fz_vpn/cli/live.ex | 13 +- apps/fz_vpn/lib/fz_vpn/cli/sandbox.ex | 1 - apps/fz_vpn/lib/fz_vpn/config.ex | 27 +--- apps/fz_vpn/lib/fz_vpn/peer.ex | 3 +- apps/fz_vpn/lib/fz_vpn/server.ex | 9 +- apps/fz_vpn/test/fz_vpn/config_test.exs | 8 +- apps/fz_vpn/test/fz_vpn/server_test.exs | 2 +- config/config.exs | 2 +- config/dev.exs | 1 + config/releases.exs | 4 +- .../cookbooks/firezone/attributes/default.rb | 3 +- .../cookbooks/firezone/libraries/config.rb | 4 +- omnibus/cookbooks/firezone/recipes/config.rb | 8 + omnibus/cookbooks/firezone/recipes/network.rb | 9 -- 34 files changed, 248 insertions(+), 267 deletions(-) create mode 100644 apps/fz_http/lib/fz_http_web/templates/shared/flash.html.heex create mode 100644 apps/fz_http/lib/fz_http_web/templates/shared/heading.html.heex create mode 100644 apps/fz_http/lib/fz_http_web/views/shared_view.ex diff --git a/.env.sample b/.env.sample index ef21cdf7f..589d10e84 100644 --- a/.env.sample +++ b/.env.sample @@ -6,7 +6,7 @@ # source .env # set +a -DATABASE_URL="ecto://fireguard:postgres@localhost/fireguard_dev" +DATABASE_URL="ecto://firezone:postgres@localhost/firezone_dev" # Generate with mix phx.gen.secret SECRET_KEY_BASE= @@ -14,8 +14,7 @@ SECRET_KEY_BASE= # Generate with mix phx.gen.secret 32 LIVE_VIEW_SIGNING_SALT= -# Generate with wg genkey -WIREGUARD_PRIVATE_KEY= +WIREGUARD_PUBLIC_KEY= WIREGUARD_PORT=51820 WIREGUARD_INTERFACE_NAME=wg-firezone diff --git a/apps/fz_common/lib/cli.ex b/apps/fz_common/lib/cli.ex index ef426cd85..8310cc573 100644 --- a/apps/fz_common/lib/cli.ex +++ b/apps/fz_common/lib/cli.ex @@ -4,7 +4,7 @@ defmodule FzCommon.CLI do """ def bash(cmd) do - System.cmd("bash", ["-c", cmd]) + System.cmd("bash", ["-c", cmd], stderr_to_stdout: true) end def exec!(cmd) do @@ -12,9 +12,9 @@ defmodule FzCommon.CLI do {result, 0} -> result - {error, _} -> + {error, exit_code} -> raise """ - Error executing command #{cmd} with error #{error}. + Error executing command #{cmd}. Exited with code #{exit_code} and error #{error}. FireZone cannot recover from this error. """ end diff --git a/apps/fz_http/lib/fz_http/devices.ex b/apps/fz_http/lib/fz_http/devices.ex index 515b8da2c..1d86ef423 100644 --- a/apps/fz_http/lib/fz_http/devices.ex +++ b/apps/fz_http/lib/fz_http/devices.ex @@ -47,8 +47,7 @@ defmodule FzHttp.Devices do for device <- Repo.all(Device) do %{ public_key: device.public_key, - allowed_ips: device.allowed_ips, - preshared_key: device.preshared_key + allowed_ips: device.allowed_ips } end end diff --git a/apps/fz_http/lib/fz_http/devices/device.ex b/apps/fz_http/lib/fz_http/devices/device.ex index 1752165c3..052baf840 100644 --- a/apps/fz_http/lib/fz_http/devices/device.ex +++ b/apps/fz_http/lib/fz_http/devices/device.ex @@ -12,7 +12,6 @@ defmodule FzHttp.Devices.Device do field :name, :string field :public_key, :string field :allowed_ips, :string - field :preshared_key, FzHttp.Encrypted.Binary field :private_key, FzHttp.Encrypted.Binary field :server_public_key, :string field :remote_ip, EctoNetwork.INET @@ -36,7 +35,6 @@ defmodule FzHttp.Devices.Device do :interface_address6, :server_public_key, :private_key, - :preshared_key, :user_id, :name, :public_key @@ -46,12 +44,10 @@ defmodule FzHttp.Devices.Device do :name, :public_key, :server_public_key, - :private_key, - :preshared_key + :private_key ]) |> unique_constraint(:public_key) |> unique_constraint(:private_key) - |> unique_constraint(:preshared_key) |> unique_constraint([:user_id, :name]) end end diff --git a/apps/fz_http/lib/fz_http_web/events.ex b/apps/fz_http/lib/fz_http_web/events.ex index af468b556..f64a521e3 100644 --- a/apps/fz_http/lib/fz_http_web/events.ex +++ b/apps/fz_http/lib/fz_http_web/events.ex @@ -9,8 +9,8 @@ defmodule FzHttpWeb.Events do GenServer.call(vpn_pid(), :create_device) end - def device_created(pubkey, psk, ip) do - GenServer.cast(vpn_pid(), {:device_created, pubkey, psk, ip}) + def device_created(pubkey, ip) do + GenServer.cast(vpn_pid(), {:device_created, pubkey, ip}) end def delete_device(device_pubkey) do diff --git a/apps/fz_http/lib/fz_http_web/live/account_live/show.html.heex b/apps/fz_http/lib/fz_http_web/live/account_live/show.html.heex index 884147171..64b0f110f 100644 --- a/apps/fz_http/lib/fz_http_web/live/account_live/show.html.heex +++ b/apps/fz_http/lib/fz_http_web/live/account_live/show.html.heex @@ -7,27 +7,33 @@ user: @current_user, action: @live_action) %> <% end %> -
-
-
-
-
- Email -
-
<%= @current_user.email %>
-
Last signed in at
-
<%= @current_user.last_signed_in_at %>
-
+<%= render FzHttpWeb.SharedView, "heading.html", page_title: @page_title %> -
- <%= live_patch("Change email or password", to: Routes.account_show_path(@socket, :edit)) %> +
+ <%= render FzHttpWeb.SharedView, "flash.html", assigns %> +
+
+
+
+
+ Email +
+
<%= @current_user.email %>
- <%# This is purposefully a synchronous form in order to easily clear the session %> - <%= form_for @changeset, Routes.user_path(@socket, :delete), [id: "delete-account", method: :delete], fn _f -> %> - <%= submit "Delete your account", class: "button is-danger", data: [confirm: "Are you sure?"] %> - <% end %> +
Last signed in at
+
<%= @current_user.last_signed_in_at %>
+
+ +
+ <%= live_patch("Change email or password", to: Routes.account_show_path(@socket, :edit)) %> + + <%# This is purposefully a synchronous form in order to easily clear the session %> + <%= form_for @changeset, Routes.user_path(@socket, :delete), [id: "delete-account", method: :delete], fn _f -> %> + <%= submit "Delete your account", class: "button is-danger", data: [confirm: "Are you sure?"] %> + <% end %> +
-
+ diff --git a/apps/fz_http/lib/fz_http_web/live/device_live/index.html.heex b/apps/fz_http/lib/fz_http_web/live/device_live/index.html.heex index b22fa7ca0..09d73acd7 100644 --- a/apps/fz_http/lib/fz_http_web/live/device_live/index.html.heex +++ b/apps/fz_http/lib/fz_http_web/live/device_live/index.html.heex @@ -1,30 +1,36 @@ -
-
+<%= render FzHttpWeb.SharedView, "heading.html", page_title: @page_title %> - - - - - - - - - - - <%= for device <- @devices do %> - - - - - - - <% end %> - -
NamePublic keyRemote IPLast seen at
- <%= link(device.name, to: Routes.device_show_path(@socket, :show, device)) %> - <%= device.public_key %><%= device.remote_ip || "Never connected" %><%= device.last_seen_at %>
-
+
+ <%= render FzHttpWeb.SharedView, "flash.html", assigns %> - +
+
+ + + + + + + + + + + + <%= for device <- @devices do %> + + + + + + + <% end %> + +
NamePublic keyRemote IPLast seen at
+ <%= live_redirect(device.name, to: Routes.device_show_path(@socket, :show, device)) %> + <%= device.public_key %><%= device.remote_ip || "Never connected" %><%= device.last_seen_at %>
+
+ + +
diff --git a/apps/fz_http/lib/fz_http_web/live/device_live/index_live.ex b/apps/fz_http/lib/fz_http_web/live/device_live/index_live.ex index 912781952..c43a5683b 100644 --- a/apps/fz_http/lib/fz_http_web/live/device_live/index_live.ex +++ b/apps/fz_http/lib/fz_http_web/live/device_live/index_live.ex @@ -15,13 +15,12 @@ defmodule FzHttpWeb.DeviceLive.Index do def handle_event("create_device", _params, socket) do # XXX: Remove device from WireGuard if create isn't successful - {:ok, privkey, pubkey, server_pubkey, psk} = @events_module.create_device() + {:ok, privkey, pubkey, server_pubkey} = @events_module.create_device() device_attrs = %{ private_key: privkey, public_key: pubkey, - server_public_key: server_pubkey, - preshared_key: psk + server_public_key: server_pubkey } attributes = @@ -37,7 +36,6 @@ defmodule FzHttpWeb.DeviceLive.Index do {:ok, device} -> @events_module.device_created( device.public_key, - device.preshared_key, "10.3.2.#{device.octet_sequence}" ) diff --git a/apps/fz_http/lib/fz_http_web/live/device_live/show.html.heex b/apps/fz_http/lib/fz_http_web/live/device_live/show.html.heex index e3c05e9d7..c65ec422a 100644 --- a/apps/fz_http/lib/fz_http_web/live/device_live/show.html.heex +++ b/apps/fz_http/lib/fz_http_web/live/device_live/show.html.heex @@ -8,78 +8,77 @@ action: @live_action) %> <% end %> -
-
-
-
-

Device Details

+<%= render FzHttpWeb.SharedView, "heading.html", page_title: @page_title %> + +
+ <%= render FzHttpWeb.SharedView, "flash.html", assigns %> +
+
+
+
+

Device Details

+
+
+ <%= live_patch("Edit", to: Routes.device_show_path(@socket, :edit, @device)) %> +
-
- <%= live_patch("Edit", to: Routes.device_show_path(@socket, :edit, @device)) %> +
+
+
+
+ Name: +
+
<%= @device.name %>
+ +
+ Interface IP: +
+
10.3.2.<%= @device.octet_sequence %>
+ +
+ Public key: +
+
<%= @device.public_key %>
+ +
+ Private key: +
+
<%= @device.private_key %>
+ +
+ Server Public key: +
+
<%= @device.server_public_key %>
+ +
+ Remote IP: +
+
<%= @device.remote_ip %>
+ +
+ Last seen at: +
+
<%= @device.last_seen_at %>
+
+
+
+ <%= live_redirect("Back to Devices", to: Routes.device_index_path(@socket, :index), class: "button") %> +
-
-
-
-
- Name: -
-
<%= @device.name %>
- -
- Interface IP: -
-
10.3.2.<%= @device.octet_sequence %>
- -
- Public key: -
-
<%= @device.public_key %>
- -
- Private key: -
-
<%= @device.private_key %>
- -
- Preshared key: -
-
<%= @device.preshared_key %>
- -
- Server Public key: -
-
<%= @device.server_public_key %>
- -
- Remote IP: -
-
<%= @device.remote_ip %>
- -
- Last seen at: -
-
<%= @device.last_seen_at %>
-
-
-
- <%= link("Back to Devices", to: Routes.device_index_path(@socket, :index), class: "button") %> - -
-
-
-

Config

-
-
-
- Add the following to your WireGuard™ configuration file: -
-

+    
+

Config

+
+
+
+ Add the following to your WireGuard™ configuration file: +
+

 [Interface]
 PrivateKey = <%= @device.private_key %>
 Address = 10.3.2.<%= @device.octet_sequence %>
@@ -89,12 +88,13 @@ DNS = 1.1.1.1, 1.0.0.1
 PublicKey = <%= @device.server_public_key %>
 AllowedIPs = 0.0.0.0/0, ::/0
 Endpoint = <%= @wireguard_endpoint_ip %>:<%= @wireguard_port %>
-
- Or scan the QR code with your mobile phone: -
-
- +
+ Or scan the QR code with your mobile phone: +
+
+ +
-
+
diff --git a/apps/fz_http/lib/fz_http_web/live/rule_live/index.html.heex b/apps/fz_http/lib/fz_http_web/live/rule_live/index.html.heex index b258a211e..86fd314da 100644 --- a/apps/fz_http/lib/fz_http_web/live/rule_live/index.html.heex +++ b/apps/fz_http/lib/fz_http_web/live/rule_live/index.html.heex @@ -1,18 +1,23 @@ -
-
- <%= live_component( - @socket, - FzHttpWeb.RuleLive.RuleListComponent, - title: "Allowlist", - id: :allowlist, - current_user: @current_user) %> +<%= render FzHttpWeb.SharedView, "heading.html", page_title: @page_title %> + +
+ <%= render FzHttpWeb.SharedView, "flash.html", assigns %> +
+
+ <%= live_component( + @socket, + FzHttpWeb.RuleLive.RuleListComponent, + title: "Allowlist", + id: :allowlist, + current_user: @current_user) %> +
+
+ <%= live_component( + @socket, + FzHttpWeb.RuleLive.RuleListComponent, + title: "Denylist", + id: :denylist, + current_user: @current_user) %> +
-
- <%= live_component( - @socket, - FzHttpWeb.RuleLive.RuleListComponent, - title: "Denylist", - id: :denylist, - current_user: @current_user) %> -
-
+ diff --git a/apps/fz_http/lib/fz_http_web/mock_events.ex b/apps/fz_http/lib/fz_http_web/mock_events.ex index 550b54848..17d35b04e 100644 --- a/apps/fz_http/lib/fz_http_web/mock_events.ex +++ b/apps/fz_http/lib/fz_http_web/mock_events.ex @@ -8,14 +8,14 @@ defmodule FzHttpWeb.MockEvents do """ def create_device do - {:ok, "privkey", "pubkey", "server_pubkey", "preshared_key"} + {:ok, "privkey", "pubkey", "server_pubkey"} end def delete_device(pubkey) do {:ok, pubkey} end - def device_created(_pubkey, _psk, _ip) do + def device_created(_pubkey, _ip) do :ok end diff --git a/apps/fz_http/lib/fz_http_web/templates/layout/live.html.heex b/apps/fz_http/lib/fz_http_web/templates/layout/live.html.heex index dcc9b71be..c25717ed4 100644 --- a/apps/fz_http/lib/fz_http_web/templates/layout/live.html.heex +++ b/apps/fz_http/lib/fz_http_web/templates/layout/live.html.heex @@ -1,27 +1,3 @@ -<%= if !is_nil(live_flash(@flash, :info)) or !is_nil(live_flash(@flash, :error)) do %> -
- <%= if live_flash(@flash, :info) do %> -
- -
<%= live_flash(@flash, :info) %>
-
- <% end %> - <%= if live_flash(@flash, :error) do %> -
- -
<%= live_flash(@flash, :error) %>
-
- <% end %> -
-<% end %>
<%= @inner_content %>
diff --git a/apps/fz_http/lib/fz_http_web/templates/layout/root.html.heex b/apps/fz_http/lib/fz_http_web/templates/layout/root.html.heex index b07bec4bb..a503fb162 100644 --- a/apps/fz_http/lib/fz_http_web/templates/layout/root.html.heex +++ b/apps/fz_http/lib/fz_http_web/templates/layout/root.html.heex @@ -94,26 +94,8 @@
-
-
-
-
-
-

- <%= @page_title %> -

-
-
- -
-
-
-
- <%= @inner_content %> -
+ <%= @inner_content %>