diff --git a/apps/arena_load_test/lib/arena_load_test/application.ex b/apps/arena_load_test/lib/arena_load_test/application.ex index 67532d1c4..bd6ba478c 100644 --- a/apps/arena_load_test/lib/arena_load_test/application.ex +++ b/apps/arena_load_test/lib/arena_load_test/application.ex @@ -9,7 +9,9 @@ defmodule ArenaLoadTest.Application do def start(_type, _args) do children = [ ArenaLoadTest.SocketSupervisor, - ArenaLoadTest.LoadtestManager + ArenaLoadTest.LoadtestManager, + {Finch, name: ArenaLoadTest.Finch}, + ArenaLoadTest.TokenFetcher # Starts a worker by calling: ArenaLoadTest.Worker.start_link(arg) # {ArenaLoadTest.Worker, arg} ] diff --git a/apps/arena_load_test/lib/arena_load_test/token_fetcher.ex b/apps/arena_load_test/lib/arena_load_test/token_fetcher.ex new file mode 100644 index 000000000..e68eefd9c --- /dev/null +++ b/apps/arena_load_test/lib/arena_load_test/token_fetcher.ex @@ -0,0 +1,47 @@ +defmodule ArenaLoadTest.TokenFetcher do + @moduledoc """ + GenServer that calls gateway to create and refresh a JWT token used by the bots + """ + use GenServer + + def get_auth() do + GenServer.call(__MODULE__, :get_auth) + end + + def start_link(_args) do + GenServer.start_link(__MODULE__, [], name: __MODULE__) + end + + @impl true + def init(_) do + Process.send_after(self(), :fetch_token, 500) + {:ok, %{}} + end + + @impl true + def handle_call(:get_auth, _, state) do + {:reply, state, state} + end + + @impl true + def handle_info(:fetch_token, state) do + gateway_url = Application.get_env(:arena_load_test, :gateway_url) + secret = :crypto.strong_rand_bytes(32) |> Base.url_encode64 + payload = Jason.encode!(%{"bot_secret" => secret}) + + result = + Finch.build(:post, "#{gateway_url}/auth/generate-bot-token", [{"content-type", "application/json"}], payload) + |> Finch.request(ArenaLoadTest.Finch) + + case result do + {:ok, %Finch.Response{status: 200, body: body}} -> + Process.send_after(self(), :fetch_token, 1_800_000) + %{"token" => token} = Jason.decode!(body) + {:noreply, %{token: token, secret: secret}} + + _else_error -> + Process.send_after(self(), :fetch_token, 5_000) + {:noreply, state} + end + end +end diff --git a/apps/arena_load_test/mix.exs b/apps/arena_load_test/mix.exs index c8ce90f12..11ff2184d 100644 --- a/apps/arena_load_test/mix.exs +++ b/apps/arena_load_test/mix.exs @@ -27,7 +27,8 @@ defmodule ArenaLoadTest.MixProject do defp deps do [ {:websockex, "~> 0.4.3"}, - {:protobuf, "~> 0.12.0"} + {:protobuf, "~> 0.12.0"}, + {:finch, "~> 0.13"} # {:dep_from_hexpm, "~> 0.3.0"}, # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}, # {:sibling_app_in_umbrella, in_umbrella: true} diff --git a/config/runtime.exs b/config/runtime.exs index f769a53a7..da416e592 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -401,4 +401,11 @@ config :bot_manager, :end_point_configuration, plug: BotManager.Endpoint, options: [port: bot_manager_port] + +################################### +# App configuration: Bot Manager # +################################### + +config :arena_load_test, :gateway_url, System.get_env("GATEWAY_URL") || "http://localhost:4001" + ###################################