towerops/lib/towerops_web/channels/agent_socket.ex
Graham McIntire 9cb4c59638 dialyzer: replace suppressions with real fixes where possible
Changes to eliminate @dialyzer suppressions by fixing underlying causes:

NIF stubs (towerops_native.ex, mib_translator.ex):
- Change stubs to :erlang.nif_error(:nif_not_loaded) (no_return type).
  Real NIF replaces stubs at load time; calls to unloaded stubs now fail
  loudly instead of returning fake data. Lets dialyzer trust @spec.
- Remove @dialyzer :nowarn_function on three NIFs and on translate/1.

Discovery sync_* functions (snmp/discovery.ex, channels/agent_channel.ex):
- agent_channel passes %{device_id: _, interfaces: _} and %{id: _} maps
  into Discovery.sync_ip_addresses/sync_processors/sync_storage, which
  @spec'd only %Device{}. Add narrow map-type unions (ip_sync_device,
  snmp_device_ref) reflecting what the functions actually access.
- Remove @dialyzer :nowarn_function on three agent_channel helpers.

remote_ip.ex — real bug caught and fixed:
- `:ranch.get_addr(socket.transport_pid)` was always raising since
  Bandit uses ThousandIsland, not Ranch; the rescue _ -> nil silently
  returned nil every time. Switched to Phoenix's documented
  :peer_data connect_info (already enabled in endpoint.ex) via
  socket.assigns; remote IP now actually works.
- Remove remote_ip.ex entry from .dialyzer_ignore.exs.

Accounts / Organizations (Ecto.Multi opacity):
- Add @specs to Multi-building helpers, refactor into pipe chains.
- 6 @dialyzer :nowarn_function → 0, but 7 :no_opaque remain. Root
  cause is upstream: Ecto.Multi.new/0 returns a struct with a literal
  %MapSet{} whose @opaque internal representation trips dialyzer on
  every subsequent Multi.* call. Unfixable without an Ecto patch or
  bypassing Multi entirely. Comments document the specific upstream
  issue rather than a vague "Ecto.Multi opacity" claim.

Devices.ex:
- Adding @specs made it worse (call_without_opaque → contract_with_
  opaque); inlining the Multi didn't help either — same MapSet root
  cause. Suppression kept with a sharper comment.
2026-04-21 11:01:30 -05:00

34 lines
1.1 KiB
Elixir

defmodule ToweropsWeb.AgentSocket do
@moduledoc """
WebSocket endpoint for remote agent communication.
Agents connect to: ws://server/socket/agent/websocket
Authentication token is sent in the channel join payload.
Uses binary Protocol Buffers encoding for all messages.
"""
use Phoenix.Socket
channel "agent:*", ToweropsWeb.AgentChannel
@impl true
@spec connect(map(), Phoenix.Socket.t(), map()) :: {:ok, Phoenix.Socket.t()}
def connect(_params, socket, connect_info) do
# Allow all connections - authentication happens at channel join.
# Stash peer_data so we can extract the client IP later without
# reaching into the transport process (which is adapter-specific).
peer_data = Map.get(connect_info, :peer_data)
{:ok, Phoenix.Socket.assign(socket, :peer_data, peer_data)}
end
@impl true
@spec id(Phoenix.Socket.t()) :: String.t() | nil
def id(socket) do
# Return nil before authentication (channel join will set agent_token_id)
case Map.get(socket.assigns, :agent_token_id) do
nil -> nil
agent_token_id -> "agent_socket:#{agent_token_id}"
end
end
end