Skip to content

Commit

Permalink
implement enrichent
Browse files Browse the repository at this point in the history
  • Loading branch information
balanza committed Oct 22, 2024
1 parent 2555515 commit 738417d
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/trento/activity_logging/parser/metadata_enricher.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule Trento.ActivityLog.Logger.Parser.MetadataEnricher do

alias Trento.ActivityLog.ActivityCatalog

alias Trento.{Clusters, Databases, Hosts, SapSystems}
alias Trento.{Clusters, Databases, Hosts, SapSystems, Users}

@spec enrich(activity :: ActivityCatalog.activity_type(), metadata :: map()) ::
{:ok, maybe_enriched_metadata :: map()}
Expand All @@ -27,6 +27,7 @@ defmodule Trento.ActivityLog.Logger.Parser.MetadataEnricher do
|> maybe_enrich(:cluster, Clusters, :name)
|> maybe_enrich(:database, Databases, :sid)
|> maybe_enrich(:sap_system, SapSystems, :sid)
|> maybe_enrich(:user, Users, :username)
|> elem(1)

defp maybe_enrich({activity, metadata}, entity_reference, context_module, enriching_field) do
Expand All @@ -39,7 +40,7 @@ defmodule Trento.ActivityLog.Logger.Parser.MetadataEnricher do
_ -> metadata
end

{activity, enriched_metadata}
{activity, clean_metadata(enriched_metadata)}
end

defp detect_enrichment(
Expand Down Expand Up @@ -79,7 +80,15 @@ defmodule Trento.ActivityLog.Logger.Parser.MetadataEnricher do
defp detect_enrichment(:cluster, {_, %{cluster_id: id}}), do: {:ok, id}
defp detect_enrichment(:database, {_, %{database_id: id}}), do: {:ok, id}
defp detect_enrichment(:sap_system, {_, %{sap_system_id: id}}), do: {:ok, id}
defp detect_enrichment(:user, {_, %{user_id: id}}), do: {:ok, id}

defp detect_enrichment(_target_entity, {_activity, _metadata}),
do: {:error, :no_enrichment_needed}

defp clean_metadata(%{username: username} = metadata) do
clean_username = username |> String.split("__") |> List.first()
Map.put(metadata, :username, clean_username)
end

defp clean_metadata(metadata), do: metadata
end
5 changes: 5 additions & 0 deletions lib/trento/activity_logging/parser/phoenix_conn_parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ defmodule Trento.ActivityLog.Logger.Parser.PhoenixConnParser do
),
do: %{host_id: Map.get(params, :id)}

def get_activity_metadata(:user_deletion, %Plug.Conn{
params: params
}),
do: %{user_id: Map.get(params, :id)}

def get_activity_metadata(_, _), do: %{}

defp redact(request_body, key) do
Expand Down
8 changes: 8 additions & 0 deletions lib/trento/users.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ defmodule Trento.Users do

alias Trento.Users.User

@spec by_id(Number.t()) :: {:ok, User.t()} | {:error, :not_found}
def by_id(id) do
case Repo.get(User, id) do
%User{} = user -> {:ok, user}
nil -> {:error, :not_found}
end
end

@impl true
@doc """
get_by function overrides the one defined in Pow.Ecto.Context,
Expand Down
23 changes: 23 additions & 0 deletions test/trento/activity_logging/activity_logger_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,29 @@ defmodule Trento.ActivityLog.ActivityLoggerTest do
end
end

describe "user management activity detection" do
test "should trace user deletion", %{
conn: conn,
user: %{id: admin_id, username: admin_username}
} do
%{id: user_id, username: username} = insert(:user)

conn
|> with_token(admin_id)
|> delete("/api/v1/users/#{user_id}")

wait_for_tasks_completion()

assert [
%ActivityLog{
type: "user_deletion",
actor: ^admin_username,
metadata: %{"username" => ^username, "user_id" => ^user_id}
}
] = Trento.Repo.all(ActivityLog)
end
end

describe "tagging/untagging activity detection" do
defp tag_resource(conn, resource_id, resource_type, tag) do
conn
Expand Down
13 changes: 13 additions & 0 deletions test/trento/activity_logging/metadata_enricher_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,19 @@ defmodule Trento.ActivityLog.MetadataEnricherTest do
}} =
MetadataEnricher.enrich(:cluster_checks_execution_request, initial_metadata)
end

test "should enrich user deletion request" do
%{id: user_id, username: username} = insert(:user)

initial_metadata = %{user_id: user_id}

assert {:ok,
%{
user_id: ^user_id,
username: ^username
}} =
MetadataEnricher.enrich(:user_deletion, initial_metadata)
end
end

describe "domain event activity log metadata enrichment" do
Expand Down
20 changes: 20 additions & 0 deletions test/trento/users_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,26 @@ defmodule Trento.UsersTest do

assert {:error, :totp_invalid} = Users.validate_totp(user, totp_code)
end

test "by_id/1 get the user by its id" do
%{id: user_id} = user = insert(:user)

{:ok, found} = Users.by_id(user_id)

assert found.id == user.id

# password is autogenerated and cannot be compared
assert Map.drop(found, [:password]) == Map.drop(user, [:password])
end

test "by_id/1 returns not found for unknown users" do
insert(:user)
unknown_user_id = Faker.Random.Elixir.random_between(1, 1000)

result = Users.by_id(unknown_user_id)

assert {:error, :not_found} == result
end
end

defp admin_username, do: Application.fetch_env!(:trento, :admin_user)
Expand Down

0 comments on commit 738417d

Please sign in to comment.