Skip to content

Maintenance & custom REDACTED fields #8

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

Merged
merged 4 commits into from
Oct 2, 2021
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
72 changes: 36 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# EctoTrail

[![Hex.pm Downloads](https://img.shields.io/hexpm/dw/ecto_trail.svg?maxAge=3600)](https://hex.pm/packages/ecto_trail) [![Latest Version](https://img.shields.io/hexpm/v/ecto_trail.svg?maxAge=3600)](https://hex.pm/packages/ecto_trail) [![License](https://img.shields.io/hexpm/l/ecto_trail.svg?maxAge=3600)](https://hex.pm/packages/ecto_trail) [![Build Status](https://travis-ci.org/Nebo15/ecto_trail.svg?branch=master)](https://travis-ci.org/Nebo15/ecto_trail) [![Coverage Status](https://coveralls.io/repos/github/Nebo15/ecto_trail/badge.svg?branch=master)](https://coveralls.io/github/Nebo15/ecto_trail?branch=master) [![Ebert](https://ebertapp.io/github/Nebo15/ecto_trail.svg)](https://ebertapp.io/github/Nebo15/ecto_trail)

EctoTrail allows to store changeset changes into a separate `audit_log` table.
Expand All @@ -7,62 +8,61 @@ EctoTrail allows to store changeset changes into a separate `audit_log` table.

1. Add `ecto_trail` to your list of dependencies in `mix.exs`:

```elixir
def deps do
[{:ecto_trail, "~> 0.2.0"}]
end
```
```elixir
def deps do
[{:ecto_trail, "~> 0.2.0"}]
end
```

2. Ensure `ecto_trail` is started before your application:

```elixir
def application do
[extra_applications: [:ecto_trail]]
end
```
```elixir
def application do
[extra_applications: [:ecto_trail]]
end
```

3. Add a migration that creates `audit_log` table to `priv/repo/migrations` folder:

```elixir
defmodule EctoTrail.TestRepo.Migrations.CreateAuditLogTable do
@moduledoc false
use Ecto.Migration
```elixir
defmodule EctoTrail.TestRepo.Migrations.CreateAuditLogTable do
@moduledoc false
use Ecto.Migration

@table_name String.to_atom(Application.fetch_env!(:ecto_trail, :table_name))
@table_name String.to_atom(Application.fetch_env!(:ecto_trail, :table_name))

def change(table_name \\ @table_name) do
EctoTrailChangeEnum.create_type
create table(table_name) do
add :actor_id, :string, null: false
add :resource, :string, null: false
add :resource_id, :string, null: false
add :changeset, :map, null: false
add(:change_type, :change)
def change(table_name \\ @table_name) do
EctoTrailChangeEnum.create_type
create table(table_name) do
add :actor_id, :string, null: false
add :resource, :string, null: false
add :resource_id, :string, null: false
add :changeset, :map, null: false
add(:change_type, :change)

timestamps([type: :utc_datetime, updated_at: false])
end
timestamps([type: :utc_datetime, updated_at: false])
end
end
```
end
```

4. Use `EctoTrail` in your repo:

```elixir
defmodule MyApp.Repo do
use Ecto.Repo, otp_app: :my_app
use EctoTrail
end
```
```elixir
defmodule MyApp.Repo do
use Ecto.Repo, otp_app: :my_app
use EctoTrail
end
```

5. Configure table name which is used to store audit log (in `config.ex`):

```elixir
config :ecto_trail, table_name: "audit_log"
```
```elixir
config :ecto_trail, table_name: "audit_log", redacted_fields: [:password, :token]
```

6. Use logging functions instead of defaults. See `EctoTrail` module docs.

## Docs

The docs can be found at [https://hexdocs.pm/ecto_trail](https://hexdocs.pm/ecto_trail).

3 changes: 1 addition & 2 deletions config/config.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use Mix.Config

config :ecto_trail,
table_name: "audit_log"
config :ecto_trail, table_name: "audit_log", redacted_fields: [:password]
21 changes: 21 additions & 0 deletions lib/ecto_trail/ecto_trail.ex
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ defmodule EctoTrail do
|> get_changes()
|> get_embed_changes(embeds)
|> get_assoc_changes(associations)
|> redact_custom_fields()
|> validate_changes(struct_or_changeset, operation_type)

result =
Expand Down Expand Up @@ -254,6 +255,26 @@ defmodule EctoTrail do
end
end

defp redact_custom_fields(changeset) do
redacted_fields = Application.fetch_env(:ecto_trail, :redacted_fields)

if redacted_fields == :error do
changeset
else
{:ok, redacted_fields} = redacted_fields

Enum.map(changeset, fn {key, value} ->
{key,
if Enum.member?(redacted_fields, key) do
"[REDACTED]"
else
value
end}
end)
|> Map.new()
end
end

defp remove_empty_assosiations(struct) do
Enum.map(struct, fn {key, value} ->
{key,
Expand Down
5 changes: 2 additions & 3 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defmodule EctoTrail.Mixfile do
description: description(),
package: package(),
version: @version,
elixir: "~> 1.4",
elixir: "~> 1.6",
elixirc_paths: elixirc_paths(Mix.env()),
compilers: [] ++ Mix.compilers(),
build_embedded: Mix.env() == :prod,
Expand All @@ -34,8 +34,7 @@ defmodule EctoTrail.Mixfile do
[
{:ecto_sql, "~> 3.0"},
{:postgrex, ">= 0.14.0"},
{:dialyxir, "~> 0.5", only: [:dev, :test], runtime: false},
{:geo, "~> 3.0.0", only: [:dev, :test]},
{:dialyxir, "~> 1.1", only: [:dev, :test], runtime: false},
{:ex_doc, ">= 0.15.0", only: [:dev, :test]},
{:excoveralls, ">= 0.5.0", only: [:dev, :test]},
{:credo, ">= 0.5.1", only: [:dev, :test]},
Expand Down
Loading