Skip to content

Commit

Permalink
Adds Audit Log On Disconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
Melvin authored and jjcarstens committed Jan 2, 2023
1 parent 8297c32 commit 89b3c21
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,14 @@ defmodule NervesHubDeviceWeb.DeviceChannel do

def terminate(_reason, %{assigns: %{device: device}}) do
if device = Devices.get_device(device.id) do
Devices.update_device(device, %{last_communication: DateTime.utc_now()})
{:ok, device} = Devices.update_device(device, %{last_communication: DateTime.utc_now()})

AuditLogs.audit!(device, device, :update, %{
description:
"device #{device.identifier} disconnected from the server at #{device.last_communication}",
last_communication: device.last_communication,
status: device.status
})
end

:ok
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule NervesHubDeviceWeb.DeviceChannelTest do
use NervesHubDeviceWeb.ChannelCase
use DefaultMocks
alias NervesHubDeviceWeb.{DeviceSocket, DeviceChannel}
alias NervesHubWebCore.Fixtures
alias NervesHubWebCore.{AuditLogs, Fixtures}
alias NervesHubDevice.Presence

test "basic connection to the channel" do
Expand All @@ -29,6 +29,23 @@ defmodule NervesHubDeviceWeb.DeviceChannelTest do
assert presence["#{device.id}"].firmware_metadata
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, %{}, %{peer_data: %{ssl_cert: certificate.der}})
{:ok, _, socket} = subscribe_and_join(socket, DeviceChannel, "firmware:#{firmware.uuid}")

Process.unlink(socket.channel_pid)

close(socket)

assert [_, disconnect_log] = AuditLogs.logs_for(device)

assert disconnect_log.changes["description"] =~
"device #{device.identifier} disconnected from the server at #{device.last_communication}"
end

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

0 comments on commit 89b3c21

Please sign in to comment.