mirror of
https://github.com/outbackdingo/firezone.git
synced 2026-03-22 05:41:54 +00:00
On the domain side this PR extends `Domain.Repo` with filtering, pagination, and ordering, along with some convention changes are removing the code that is not needed since we have the filtering now. This required to touch pretty much all contexts and code, but I went through all public functions and added missing tests to make sure nothing will be broken. On the web side I've introduced a `<.live_table />` which is as close as possible to being a drop-in replacement for the regular `<.table />` (but requires to structure the LiveView module differently due to assigns anyways). I've updated all the listing tables to use it.
59 lines
1.8 KiB
Elixir
59 lines
1.8 KiB
Elixir
defmodule API.Gateway.Socket do
|
|
use Phoenix.Socket
|
|
alias Domain.{Tokens, Gateways}
|
|
require Logger
|
|
require OpenTelemetry.Tracer
|
|
|
|
## Channels
|
|
|
|
channel "gateway", API.Gateway.Channel
|
|
|
|
## Authentication
|
|
|
|
@impl true
|
|
def connect(%{"token" => encoded_token} = attrs, socket, connect_info) do
|
|
:otel_propagator_text_map.extract(connect_info.trace_context_headers)
|
|
|
|
OpenTelemetry.Tracer.with_span "gateway.connect" do
|
|
context = API.Sockets.auth_context(connect_info, :gateway_group)
|
|
attrs = Map.take(attrs, ~w[external_id name public_key])
|
|
|
|
with {:ok, group, token} <- Gateways.authenticate(encoded_token, context),
|
|
{:ok, gateway} <- Gateways.upsert_gateway(group, token, attrs, context) do
|
|
OpenTelemetry.Tracer.set_attributes(%{
|
|
token_id: token.id,
|
|
gateway_id: gateway.id,
|
|
account_id: gateway.account_id,
|
|
version: gateway.last_seen_version
|
|
})
|
|
|
|
socket =
|
|
socket
|
|
|> assign(:token_id, token.id)
|
|
|> assign(:gateway_group, group)
|
|
|> assign(:gateway, gateway)
|
|
|> assign(:opentelemetry_span_ctx, OpenTelemetry.Tracer.current_span_ctx())
|
|
|> assign(:opentelemetry_ctx, OpenTelemetry.Ctx.get_current())
|
|
|
|
{:ok, socket}
|
|
else
|
|
{:error, :unauthorized} ->
|
|
OpenTelemetry.Tracer.set_status(:error, "invalid_token")
|
|
{:error, :invalid_token}
|
|
|
|
{:error, reason} ->
|
|
OpenTelemetry.Tracer.set_status(:error, inspect(reason))
|
|
Logger.debug("Error connecting gateway websocket: #{inspect(reason)}")
|
|
{:error, reason}
|
|
end
|
|
end
|
|
end
|
|
|
|
def connect(_params, _socket, _connect_info) do
|
|
{:error, :missing_token}
|
|
end
|
|
|
|
@impl true
|
|
def id(socket), do: Tokens.socket_id(socket.assigns.token_id)
|
|
end
|