danielberkompas / cloak_ecto

Encrypted fields for Ecto
MIT License
197 stars 31 forks source link

Add equals?/2 function to types for Ecto 3.5.0 support #11

Closed ellamosi closed 3 years ago

ellamosi commented 3 years ago

Attempting to upgrade our application to Ecto 3.5.0 (from 3.4.5) we hit a snag in which some Cloak based types would stop working properly when used in changesets with the following exception:

function App.Encrypted.Binary.equal?/2 is undefined or private

Where that is defined as:

defmodule App.Encrypted.Binary do
  @moduledoc """
  Encrypted Ecto Binary field type for use with Cloak.
  """

  use Cloak.Ecto.Binary, vault: App.Vault
end
defmodule App.Model do
  import Ecto.Query
  use Ecto.Schema
  import Ecto.Changeset

  schema "models" do
    # ...
    field(:api_key, Encrypted.Binary)
    # ...
  end

  def create_changeset(schema, params \\ %{}) do
    schema
    |> cast(params, [ # Cast would crash app
      # ...
      :api_key,
      # ...
    ])
    # ...
  end

  # ...
end

I'm no expert on the internals of Ecto and how Cloak hooks into them, so if this is not right or needs to be different for specific types I'm open to feedback.

For reference, the issue was encountered using Cloak 1.0.2, Cloak_Ecto 1.0.2.

SheriefAlaa commented 3 years ago

Can confirm the issue. Thanks for the PR. I wish it would get merged to avoid downgrading ecto.

ellamosi commented 3 years ago

In the meanwhile, if anyone has an urgent need for this, it should be possible to use the fix by replacing your cloak_ecto dependency in your mix file with: {:cloak_ecto, github: "ellamosi/cloak_ecto", branch: "ecto-3.5.0-support"} (might need deps unlocking/cleaning). Use this at your own risk, though.

mjquinlan2000 commented 3 years ago

Hey @ellamosi I'm also getting this:

** (UndefinedFunctionError) function Cloak.Ecto.SHA256.equal?/2 is undefined or private

I figured you could probably just update it in your PR before this gets merged

sb8244 commented 3 years ago

If anyone needs this support without pointing at a different library version, you can include it directly in your types, like so:

defmodule Clove.Ecto.Encrypted do
  use Cloak.Ecto.Binary, vault: Clove.Ecto.Vault

  def equal?(a, b) do
    a == b
  end
end

This would not work for the SHA256 type, since that is not macro-based like Binary is.

ellamosi commented 3 years ago

And the core issue has been resolved in https://github.com/danielberkompas/cloak_ecto/commit/2238af0359a79d145b9e60013afd8aba3478c3ac so I'll be closing this pull request.

tnaftali commented 3 years ago

Hello, I'm still having the issue. What can I do?