From c1c4681a8d55c79c9f3f3bb243ae7df1213b206e Mon Sep 17 00:00:00 2001 From: Zack Siri Date: Thu, 9 Nov 2023 18:42:21 +0700 Subject: [PATCH] Remove zero ssl issuer for identifier and use ip for identifiers Signed-off-by: Zack Siri --- lib/uplink/clients/caddy/admin.ex | 23 ---------------- .../clients/caddy/admin/issuer/zero_ssl.ex | 27 ------------------- lib/uplink/clients/caddy/config/builder.ex | 23 +++++++++------- test/scenarios/deployment.ex | 13 ++++++++- test/uplink/boot_test.exs | 13 ++++++++- .../clients/caddy/config/builder_test.exs | 3 +-- .../clients/caddy/config/reload_test.exs | 13 ++++++++- 7 files changed, 51 insertions(+), 64 deletions(-) delete mode 100644 lib/uplink/clients/caddy/admin/issuer/zero_ssl.ex diff --git a/lib/uplink/clients/caddy/admin.ex b/lib/uplink/clients/caddy/admin.ex index f34f056d..70c0de7a 100644 --- a/lib/uplink/clients/caddy/admin.ex +++ b/lib/uplink/clients/caddy/admin.ex @@ -4,10 +4,6 @@ defmodule Uplink.Clients.Caddy.Admin do @derive Jason.Encoder - @mappings %{ - "zerossl" => __MODULE__.Issuer.ZeroSSL - } - @primary_key false embedded_schema do embeds_one :identity, Identity, primary_key: false do @@ -34,24 +30,5 @@ defmodule Uplink.Clients.Caddy.Admin do defp identity_changeset(identity, params) do identity |> cast(params, [:identifiers, :issuers]) - |> maybe_cast_issuers() - end - - defp maybe_cast_issuers(changeset) do - if issuers = get_change(changeset, :issuers) do - issuers = - issuers - |> Enum.map(fn issuer -> - module = - Map.get(@mappings, issuer["module"]) || - Map.get(@mappings, issuer[:module]) - - module.parse(issuer) - end) - - put_change(changeset, :issuers, issuers) - else - changeset - end end end diff --git a/lib/uplink/clients/caddy/admin/issuer/zero_ssl.ex b/lib/uplink/clients/caddy/admin/issuer/zero_ssl.ex deleted file mode 100644 index 9a89ca1d..00000000 --- a/lib/uplink/clients/caddy/admin/issuer/zero_ssl.ex +++ /dev/null @@ -1,27 +0,0 @@ -defmodule Uplink.Clients.Caddy.Admin.Issuer.ZeroSSL do - use Ecto.Schema - import Ecto.Changeset - - @derive Jason.Encoder - - @valid_attrs ~w( - api_key - )a - - @primary_key false - embedded_schema do - field :module, :string, default: "zerossl" - field :api_key, :string - end - - def changeset(issuer, params) do - issuer - |> cast(params, @valid_attrs) - end - - def parse(params) do - %__MODULE__{} - |> changeset(params) - |> apply_action!(:insert) - end -end diff --git a/lib/uplink/clients/caddy/config/builder.ex b/lib/uplink/clients/caddy/config/builder.ex index 4b846f28..956c9f40 100644 --- a/lib/uplink/clients/caddy/config/builder.ex +++ b/lib/uplink/clients/caddy/config/builder.ex @@ -20,25 +20,30 @@ defmodule Uplink.Clients.Caddy.Config.Builder do metadata.hosts == [] || is_nil(metadata.main_port) end) - %{"organization" => %{"storage" => storage_params} = organization} = - Uplink.Clients.Instellar.get_self() + %{"organization" => %{"storage" => storage_params}} = + uplink = Uplink.Clients.Instellar.get_self() %{ - admin: admin(organization), + admin: admin(uplink), apps: apps(install_states), storage: Storage.parse(storage_params) } end - def admin(%{"slug" => slug}) do - zero_ssl_api_key = Caddy.config(:zero_ssl_api_key) + def admin(uplink) do + identifiers = + if instances = Map.get(uplink, "instances") do + instances + |> Enum.map(fn i -> + i["node"]["public_ip"] + end) + else + [] + end %{ identity: %{ - identifiers: ["uplink", slug], - issuers: [ - %{module: "zerossl", api_key: zero_ssl_api_key} - ] + identifiers: identifiers } } |> Admin.parse() diff --git a/test/scenarios/deployment.ex b/test/scenarios/deployment.ex index 726c0f9e..9b2fe8a3 100644 --- a/test/scenarios/deployment.ex +++ b/test/scenarios/deployment.ex @@ -83,7 +83,18 @@ defmodule Uplink.Scenarios.Deployment do "secret_access_key" => "secret" } } - } + }, + "instances" => [ + %{ + "id" => 1, + "slug" => "uplink-01", + "node" => %{ + "id" => 1, + "slug" => "some-node-01", + "public_ip" => "127.0.0.1" + } + } + ] }) Application.put_env( diff --git a/test/uplink/boot_test.exs b/test/uplink/boot_test.exs index 5d10d2db..8bd32c6c 100644 --- a/test/uplink/boot_test.exs +++ b/test/uplink/boot_test.exs @@ -114,7 +114,18 @@ defmodule Uplink.BootTest do "secret_access_key" => "secret" } } - } + }, + "instances" => [ + %{ + "id" => 1, + "slug" => "uplink-01", + "node" => %{ + "id" => 1, + "slug" => "some-node-01", + "public_ip" => "127.0.0.1" + } + } + ] }) Application.put_env( diff --git a/test/uplink/clients/caddy/config/builder_test.exs b/test/uplink/clients/caddy/config/builder_test.exs index 9fc46a6a..8f6654ff 100644 --- a/test/uplink/clients/caddy/config/builder_test.exs +++ b/test/uplink/clients/caddy/config/builder_test.exs @@ -39,8 +39,7 @@ defmodule Uplink.Clients.Caddy.Config.BuilderTest do assert %{host: _hosts} = match assert %{identity: identity} = admin - assert %{issuers: [zerossl]} = identity - assert %{module: "zerossl"} = zerossl + assert %{issuers: [], identifiers: ["127.0.0.1"]} = identity assert %{module: "s3"} = storage end diff --git a/test/uplink/clients/caddy/config/reload_test.exs b/test/uplink/clients/caddy/config/reload_test.exs index 5afe9acc..44e07233 100644 --- a/test/uplink/clients/caddy/config/reload_test.exs +++ b/test/uplink/clients/caddy/config/reload_test.exs @@ -114,7 +114,18 @@ defmodule Uplink.Clients.Caddy.Config.ReloadTest do "secret_access_key" => "secret" } } - } + }, + "instances" => [ + %{ + "id" => 1, + "slug" => "uplink-01", + "node" => %{ + "id" => 1, + "slug" => "some-node-01", + "public_ip" => "127.0.0.1" + } + } + ] }) Application.put_env(