Skip to content

Commit 74bceb4

Browse files
authored
Improve the logging for duplicate device identifier connection errors (#2285)
Improve the logging for duplicate device identifiers from: ``` 2025-08-14T00:45:50Z app[48e4199b764518] iad level=info msg="Device auth failed" reason="{:error, #Ecto.Changeset<action: :insert, changes: %{status: :registered, extensions: #Ecto.Changeset<action: :insert, changes: %{}, errors: [], data: #NervesHub.Extensions.DeviceExtensionsSetting<>, valid?: true, ...>, identifier: \"abc123\", org_id: 123, priority_updates: false, product_id: 123, update_attempts: [], updates_enabled: true}, errors: [identifier: {\"has already been taken\", [constraint: :unique, constraint_name: \"devices_identifier_index\"]}], data: #NervesHub.Devices.Device<>, valid?: false, ...>}" auth=shared_secrets event=nerves_hub.devices.invalid_auth product_key=nhp_abc123 ``` to ``` 2025-08-14T00:45:50Z app[48e4199b764518] iad [info]ts=2025-08-14T00:45:50.910 level=info msg="Device auth failed" auth=shared_secrets event=nerves_hub.devices.invalid_auth org_id=123 product_id=123 identifier=abc123 ``` and expired shared secret signatures from: `reason="{:error, :expired}"` to `reason="signature_expired"`
1 parent 50fe3d5 commit 74bceb4

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

lib/nerves_hub/devices.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ defmodule NervesHub.Devices do
325325
end
326326

327327
@spec get_or_create_device(Products.SharedSecretAuth.t(), String.t()) ::
328-
{:ok, Device.t()} | {:error, :not_found}
328+
{:ok, Device.t()} | {:error, Ecto.Changeset.t()}
329329
def get_or_create_device(%Products.SharedSecretAuth{} = auth, identifier) do
330330
with {:error, :not_found} <-
331331
get_active_device(product_id: auth.product_id, identifier: identifier),

lib/nerves_hub/logger.ex

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ defmodule NervesHub.Logger do
7272
event: "nerves_hub.devices.invalid_auth",
7373
auth: to_string(metadata[:auth]),
7474
reason: inspect(metadata[:reason]),
75-
product_key: metadata[:product_key]
75+
org_id: metadata[:org_id],
76+
product_id: metadata[:product_id],
77+
shared_key: metadata[:shared_key],
78+
identifier: metadata[:device_identifier]
7679
}
7780
|> Map.reject(fn {_key, val} -> is_nil(val) end)
7881

lib/nerves_hub_web/channels/device_socket.ex

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,37 @@ defmodule NervesHubWeb.DeviceSocket do
105105
{:ok, device} <- get_or_maybe_create_device(auth, identifier) do
106106
socket_and_assigns(socket, device)
107107
else
108+
{:error,
109+
%Ecto.Changeset{
110+
changes: %{identifier: identifier, org_id: org_id, product_id: product_id},
111+
errors: [
112+
identifier: {_msg, [constraint: :unique, constraint_name: "devices_identifier_index"]}
113+
]
114+
}} ->
115+
:telemetry.execute([:nerves_hub, :devices, :invalid_auth], %{count: 1}, %{
116+
auth: :shared_secrets,
117+
reason: :duplicate_device_identifier,
118+
org_id: org_id,
119+
product_id: product_id,
120+
identifier: identifier
121+
})
122+
123+
{:error, :invalid_auth}
124+
125+
{:error, :expired} ->
126+
:telemetry.execute([:nerves_hub, :devices, :invalid_auth], %{count: 1}, %{
127+
auth: :shared_secrets,
128+
reason: :signature_expired,
129+
shared_key: Map.get(headers, "x-nh-key", "*empty*")
130+
})
131+
132+
{:error, :invalid_auth}
133+
108134
error ->
109135
:telemetry.execute([:nerves_hub, :devices, :invalid_auth], %{count: 1}, %{
110136
auth: :shared_secrets,
111137
reason: error,
112-
product_key: Map.get(headers, "x-nh-key", "*empty*")
138+
shared_key: Map.get(headers, "x-nh-key", "*empty*")
113139
})
114140

115141
{:error, :invalid_auth}
@@ -121,7 +147,7 @@ defmodule NervesHubWeb.DeviceSocket do
121147
:telemetry.execute([:nerves_hub, :devices, :invalid_auth], %{count: 1}, %{
122148
auth: :shared_secrets,
123149
reason: e,
124-
product_key: Map.get(headers, "x-nh-key", "*empty*")
150+
shared_key: Map.get(headers, "x-nh-key", "*empty*")
125151
})
126152

127153
{:error, :invalid_auth}

0 commit comments

Comments
 (0)