From 455bffc503d389a0b708f5a17738334ef1f7d004 Mon Sep 17 00:00:00 2001 From: Daniel Berkompas Date: Fri, 17 Jun 2022 10:12:06 -0700 Subject: [PATCH 1/2] :bug: Add support for {:array, inner_type} fields See #8 --- lib/cloak_ecto/migrator.ex | 4 ++++ test/cloak_ecto/migrator_test.exs | 22 +++++++++++++++++-- .../20180915035851_create_posts.exs | 1 + test/support/post.ex | 1 + 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/lib/cloak_ecto/migrator.ex b/lib/cloak_ecto/migrator.ex index 0f64aca..5f990df 100644 --- a/lib/cloak_ecto/migrator.ex +++ b/lib/cloak_ecto/migrator.ex @@ -73,6 +73,10 @@ defmodule Cloak.Ecto.Migrator do false end + defp cloak_field?({field, {kind, inner_type}}) when kind in [:array] do + cloak_field?({field, inner_type}) + end + defp cloak_field?({_field, type}) do Code.ensure_loaded?(type) && function_exported?(type, :__cloak__, 0) end diff --git a/test/cloak_ecto/migrator_test.exs b/test/cloak_ecto/migrator_test.exs index 4b812d6..aa3a515 100644 --- a/test/cloak_ecto/migrator_test.exs +++ b/test/cloak_ecto/migrator_test.exs @@ -71,16 +71,19 @@ defmodule Cloak.Ecto.MigratorTest do end @post_title "Test Title" + @post_tags ["test", "encryption"] describe ".migrate/2 with binary ids" do setup do now = DateTime.utc_now() - encrypted_title = Cloak.Ecto.TestVault.encrypt!(@post_title, :secondary) + encrypted_title = Vault.encrypt!(@post_title, :secondary) + encrypted_tags = Enum.map(@post_tags, &Vault.encrypt!(&1, :secondary)) posts = for _ <- 1..500 do %{ title: encrypted_title, + tags: encrypted_tags, comments: [%{author: "Daniel", body: "Comment"}], inserted_at: now, updated_at: now @@ -98,6 +101,8 @@ defmodule Cloak.Ecto.MigratorTest do Migrator.migrate(Repo, Cloak.Ecto.TestPost) end) + assert io =~ "__cloak_cursor_fields__", "Did not call __cloak_cursor_fields__ on schema!" + titles = "posts" |> select([:title]) @@ -105,8 +110,21 @@ defmodule Cloak.Ecto.MigratorTest do |> Enum.map(&decrypt(&1.title, :default)) |> Enum.uniq() - assert io =~ "__cloak_cursor_fields__", "Did not call __cloak_cursor_fields__ on schema!" assert titles == [{:ok, @post_title}], "Not all titles were migrated!" + + tags = + "posts" + |> select([:tags]) + |> Repo.all() + |> Enum.map(fn %{tags: tags} -> + tags + |> Enum.map(&decrypt(&1, :default)) + |> Enum.map(&elem(&1, 1)) + end) + |> Enum.uniq() + |> List.flatten() + + assert tags == @post_tags, "Not all tags were migrated!" end end diff --git a/test/support/migrations/20180915035851_create_posts.exs b/test/support/migrations/20180915035851_create_posts.exs index ae5841c..1503e03 100644 --- a/test/support/migrations/20180915035851_create_posts.exs +++ b/test/support/migrations/20180915035851_create_posts.exs @@ -7,6 +7,7 @@ defmodule Cloak.Ecto.TestRepo.Migrations.CreatePosts do create table(:posts, primary_key: false) do add(:id, :uuid, primary_key: true, default: fragment("uuid_generate_v4()")) add(:title, :binary) + add(:tags, {:array, :binary}) add(:comments, {:array, :map}) timestamps(type: :utc_datetime) end diff --git a/test/support/post.ex b/test/support/post.ex index 0951844..af693a0 100644 --- a/test/support/post.ex +++ b/test/support/post.ex @@ -7,6 +7,7 @@ defmodule Cloak.Ecto.TestPost do schema "posts" do field(:title, Cloak.Ecto.Encrypted.Binary) + field(:tags, {:array, Cloak.Ecto.Encrypted.Binary}) embeds_many(:comments, Cloak.Ecto.TestComment) timestamps(type: :utc_datetime) end From 9d2131ca887413cfd4530f0f71cc592204655e65 Mon Sep 17 00:00:00 2001 From: Daniel Berkompas Date: Fri, 17 Jun 2022 10:23:37 -0700 Subject: [PATCH 2/2] :bug: Add support for {:map, inner_type} fields See #8 --- lib/cloak_ecto/migrator.ex | 2 +- mix.exs | 3 ++- test/support/migrations/20180915035851_create_posts.exs | 1 + test/support/post.ex | 1 + 4 files changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/cloak_ecto/migrator.ex b/lib/cloak_ecto/migrator.ex index 5f990df..0ab5af7 100644 --- a/lib/cloak_ecto/migrator.ex +++ b/lib/cloak_ecto/migrator.ex @@ -73,7 +73,7 @@ defmodule Cloak.Ecto.Migrator do false end - defp cloak_field?({field, {kind, inner_type}}) when kind in [:array] do + defp cloak_field?({field, {kind, inner_type}}) when kind in [:array, :map] do cloak_field?({field, inner_type}) end diff --git a/mix.exs b/mix.exs index 943d75f..d613139 100644 --- a/mix.exs +++ b/mix.exs @@ -105,7 +105,8 @@ defmodule Cloak.Ecto.MixProject do defp aliases do [ - test: ["ecto.create --quiet", "ecto.migrate --quiet", "test"] + test: ["ecto.create --quiet", "ecto.migrate --quiet", "test"], + "ecto.reset": ["ecto.drop", "ecto.create", "ecto.migrate"] ] end end diff --git a/test/support/migrations/20180915035851_create_posts.exs b/test/support/migrations/20180915035851_create_posts.exs index 1503e03..7acd023 100644 --- a/test/support/migrations/20180915035851_create_posts.exs +++ b/test/support/migrations/20180915035851_create_posts.exs @@ -8,6 +8,7 @@ defmodule Cloak.Ecto.TestRepo.Migrations.CreatePosts do add(:id, :uuid, primary_key: true, default: fragment("uuid_generate_v4()")) add(:title, :binary) add(:tags, {:array, :binary}) + add(:metadata, {:map, :string}) add(:comments, {:array, :map}) timestamps(type: :utc_datetime) end diff --git a/test/support/post.ex b/test/support/post.ex index af693a0..3694869 100644 --- a/test/support/post.ex +++ b/test/support/post.ex @@ -8,6 +8,7 @@ defmodule Cloak.Ecto.TestPost do schema "posts" do field(:title, Cloak.Ecto.Encrypted.Binary) field(:tags, {:array, Cloak.Ecto.Encrypted.Binary}) + field(:metadata, {:map, :string}) embeds_many(:comments, Cloak.Ecto.TestComment) timestamps(type: :utc_datetime) end