-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eefeb17
commit 5f2113b
Showing
7 changed files
with
482 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
defmodule Trento.ActivityLogging.Logger.ActivityLogWriter do | ||
@moduledoc """ | ||
Activity Log Writer behaviour | ||
""" | ||
|
||
@type log_entry :: %{ | ||
type: String.t(), | ||
actor: String.t(), | ||
metadata: map() | ||
} | ||
|
||
@callback write_log(log_entry()) :: {:ok, any()} | {:error, any()} | ||
|
||
def write_log(log_entry), do: adapter().write_log(log_entry) | ||
|
||
defp adapter, do: Application.fetch_env!(:trento, Trento.ActivityLog.ActivityLogger)[:writer] | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
defmodule Trento.ActivityLogging.Logger.PersistentLogger do | ||
@moduledoc """ | ||
Activity Logger that persists interesting activities | ||
""" | ||
|
||
alias Trento.ActivityLogging.ActivityCatalog | ||
alias Trento.ActivityLogging.Logger.ActivityLogWriter | ||
|
||
require Logger | ||
|
||
@behaviour Trento.ActivityLog.ActivityLogger | ||
|
||
@impl true | ||
def log_activity(activity_context) do | ||
with {:ok, activity_parser} <- get_activity_parser(activity_context), | ||
detected_activity <- detect_activity(activity_parser, activity_context), | ||
true <- ActivityCatalog.interested?(detected_activity, activity_context) do | ||
write_log(%{ | ||
type: get_activity_type(detected_activity), | ||
actor: get_actor(activity_parser, detected_activity, activity_context), | ||
metadata: get_metadata(activity_parser, detected_activity, activity_context) | ||
}) | ||
end | ||
|
||
:ok | ||
end | ||
|
||
defp get_activity_parser(%Plug.Conn{}), | ||
do: {:ok, Trento.ActivityLogging.Logger.Parser.PhoenixConnParser} | ||
|
||
defp get_activity_parser(_), do: {:error, :unsupported_activity} | ||
|
||
# defp get_activity_parser(%Commanded.Middleware.Pipeline{}), | ||
# do: {:ok, Trento.ActivityLogging.Logger.Parser.CommandedParser} | ||
|
||
defp detect_activity(activity_parser, activity_context), | ||
do: activity_parser.detect_activity(activity_context) | ||
|
||
defp get_activity_type(detected_activity), | ||
do: | ||
detected_activity | ||
|> ActivityCatalog.get_activity_type() | ||
|> Atom.to_string() | ||
|
||
defp get_actor(activity_parser, detected_activity, activity_context), | ||
do: activity_parser.get_activity_actor(detected_activity, activity_context) | ||
|
||
defp get_metadata(activity_parser, detected_activity, activity_context), | ||
do: activity_parser.get_activity_metadata(detected_activity, activity_context) | ||
|
||
defp write_log(%{type: activity_type} = entry) do | ||
case ActivityLogWriter.write_log(entry) do | ||
{:ok, _} -> | ||
Logger.info("Logged activity: #{activity_type}") | ||
|
||
{:error, reason} -> | ||
Logger.error( | ||
"An error occurred while logging activity: #{activity_type}. Reason: #{inspect(reason)}" | ||
) | ||
end | ||
end | ||
end |
140 changes: 140 additions & 0 deletions
140
lib/trento/activity_logging/parser/phoenix_conn_parser.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
defmodule Trento.ActivityLogging.Logger.Parser.PhoenixConnParser do | ||
@moduledoc """ | ||
Phoenix connection activity parser | ||
""" | ||
|
||
alias Phoenix.Controller | ||
|
||
alias Trento.Users.User | ||
|
||
require Trento.ActivityLogging.ActivityCatalog, as: ActivityCatalog | ||
|
||
@behaviour Trento.ActivityLogging.Parser.ActivityParser | ||
|
||
@impl true | ||
def detect_activity(%Plug.Conn{} = conn) do | ||
{Controller.controller_module(conn), Controller.action_name(conn)} | ||
rescue | ||
_ -> nil | ||
end | ||
|
||
@impl true | ||
def get_activity_actor(ActivityCatalog.login_attempt(), %Plug.Conn{body_params: request_payload}), | ||
do: Map.get(request_payload, "username", "no_username") | ||
|
||
@impl true | ||
def get_activity_actor(_, %Plug.Conn{} = conn) do | ||
case Pow.Plug.current_user(conn) do | ||
%User{username: username} -> username | ||
_ -> "system" | ||
end | ||
end | ||
|
||
@impl true | ||
def get_activity_metadata( | ||
ActivityCatalog.login_attempt() = action, | ||
%Plug.Conn{ | ||
assigns: %{ | ||
reason: reason | ||
}, | ||
status: 401 | ||
} = conn | ||
) do | ||
%{ | ||
username: get_activity_actor(action, conn), | ||
reason: reason | ||
} | ||
end | ||
|
||
@impl true | ||
def get_activity_metadata( | ||
ActivityCatalog.resource_tagging(), | ||
%Plug.Conn{ | ||
params: %{id: resource_id}, | ||
assigns: %{ | ||
resource_type: resource_type | ||
}, | ||
body_params: %{value: added_tag} | ||
} | ||
) do | ||
%{ | ||
resource_id: resource_id, | ||
resource_type: resource_type, | ||
added_tag: added_tag | ||
} | ||
end | ||
|
||
@impl true | ||
def get_activity_metadata( | ||
ActivityCatalog.resource_untagging(), | ||
%Plug.Conn{ | ||
params: %{ | ||
id: resource_id, | ||
value: removed_tag | ||
}, | ||
assigns: %{ | ||
resource_type: resource_type | ||
} | ||
} | ||
) do | ||
%{ | ||
resource_id: resource_id, | ||
resource_type: resource_type, | ||
removed_tag: removed_tag | ||
} | ||
end | ||
|
||
@impl true | ||
def get_activity_metadata( | ||
ActivityCatalog.api_key_generation(), | ||
%Plug.Conn{ | ||
body_params: request_body | ||
} | ||
) do | ||
request_body | ||
end | ||
|
||
@impl true | ||
def get_activity_metadata( | ||
action, | ||
%Plug.Conn{ | ||
body_params: request_body | ||
} | ||
) | ||
when action in [ | ||
ActivityCatalog.saving_suma_settings(), | ||
ActivityCatalog.changing_suma_settings() | ||
] do | ||
request_body | ||
|> redact(:password) | ||
|> redact(:ca_cert) | ||
end | ||
|
||
@impl true | ||
def get_activity_metadata( | ||
action, | ||
%Plug.Conn{ | ||
body_params: request_body | ||
} | ||
) | ||
when action in [ | ||
ActivityCatalog.user_creation(), | ||
ActivityCatalog.user_modification(), | ||
ActivityCatalog.profile_update() | ||
] do | ||
request_body | ||
|> redact(:password) | ||
|> redact(:current_password) | ||
|> redact(:password_confirmation) | ||
end | ||
|
||
@impl true | ||
def get_activity_metadata(_, _), do: %{} | ||
|
||
defp redact(request_body, key) do | ||
case Map.has_key?(request_body, key) && Map.fetch!(request_body, key) != nil do | ||
true -> Map.update!(request_body, key, fn _ -> "REDACTED" end) | ||
false -> request_body | ||
end | ||
end | ||
end |
16 changes: 16 additions & 0 deletions
16
lib/trento/infrastructure/activity_log/logger/adapter/database_writer.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
defmodule Trento.Infrastructure.ActivityLog.Logger.DatabasetWriter do | ||
@moduledoc """ | ||
Persistent Activity Log Writer Adapter | ||
""" | ||
|
||
alias Trento.ActivityLog.ActivityLog | ||
|
||
@behaviour Trento.ActivityLogging.Logger.ActivityLogWriter | ||
|
||
@impl true | ||
def write_log(log_entry), | ||
do: | ||
%ActivityLog{} | ||
|> ActivityLog.changeset(log_entry) | ||
|> Trento.Repo.insert() | ||
end |
Oops, something went wrong.