Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove connect/disconnect messages from the audit logs #1218

Merged
merged 2 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 8 additions & 16 deletions lib/nerves_hub_web/channels/device_channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,6 @@ defmodule NervesHubWeb.DeviceChannel do
def handle_info({:after_join, params}, %{assigns: %{device: device}} = socket) do
socket = assign(socket, :device_api_version, Map.get(params, "device_api_version", "1.0.0"))

description = "device #{device.identifier} connected to the server"

AuditLogs.audit_with_ref!(
device,
device,
description,
socket.assigns.reference_id
)

device =
device
|> Devices.verify_deployment()
Expand Down Expand Up @@ -85,7 +76,9 @@ defmodule NervesHubWeb.DeviceChannel do
end

## After join
:telemetry.execute([:nerves_hub, :devices, :connect], %{count: 1})
:telemetry.execute([:nerves_hub, :devices, :connect], %{count: 1}, %{
identifier: device.identifier
})

# local node tracking
Registry.update_value(NervesHub.Devices, device.id, fn value ->
Expand Down Expand Up @@ -392,17 +385,16 @@ defmodule NervesHubWeb.DeviceChannel do
{:noreply, socket}
end

def terminate(_reason, socket) do
:telemetry.execute([:nerves_hub, :devices, :disconnect], %{count: 1})
def terminate(_reason, %{assigns: %{device: device}} = socket) do
:telemetry.execute([:nerves_hub, :devices, :disconnect], %{count: 1}, %{
identifier: device.identifier
})

{:ok, device} =
Devices.update_device(socket.assigns.device, %{last_communication: DateTime.utc_now()})

description = "device #{device.identifier} disconnected from the server"

AuditLogs.audit_with_ref!(device, device, description, socket.assigns.reference_id)

Registry.unregister(NervesHub.Devices, device.id)

Tracker.offline(device)

:ok
Expand Down
23 changes: 0 additions & 23 deletions test/nerves_hub_web/channels/device_channel_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ defmodule NervesHubWeb.DeviceChannelTest do

import TrackerHelper

alias NervesHub.AuditLogs
alias NervesHub.Devices
alias NervesHub.Fixtures
alias NervesHubWeb.DeviceChannel
Expand Down Expand Up @@ -35,28 +34,6 @@ defmodule NervesHubWeb.DeviceChannelTest do
assert_online(device)
end

test "device disconnected adds audit log" do
user = Fixtures.user_fixture()
{device, _firmware, _deployment} = device_fixture(user, %{identifier: "123"})
%{db_cert: certificate, cert: _cert} = Fixtures.device_certificate_fixture(device)

{:ok, socket} =
connect(DeviceSocket, %{}, connect_info: %{peer_data: %{ssl_cert: certificate.der}})

{:ok, _, socket} = subscribe_and_join(socket, DeviceChannel, "device")

Process.unlink(socket.channel_pid)

close(socket)

disconnect_log =
Enum.find(AuditLogs.logs_for(device), fn audit_log ->
audit_log.description == "device #{device.identifier} disconnected from the server"
end)

assert disconnect_log
end

test "update_available on connect" do
user = Fixtures.user_fixture()
{device, _firmware, deployment} = device_fixture(user, %{identifier: "123"})
Expand Down
Loading