danielberkompas / cloak

Elixir encryption library designed for Ecto
MIT License
582 stars 55 forks source link

ETS problem with Clock as application in umbrella project #111

Closed pcarvsilva closed 2 years ago

pcarvsilva commented 2 years ago

:ets.lookup(MyApp.Vault.Config, :config)

acrolink commented 2 years ago

I am facing the same issue, what is the solution?

MeerKatDev commented 2 years ago

still no solution?

gmile commented 2 years ago

Same issue.

@pcarvsilva would you happen to be able to share the solution you've arrived at at the end? 🤞

pcarvsilva commented 2 years ago

@gmile I've migrrated to Oban

plicjo commented 2 years ago

I was able to get this working by manually adding it under my sub-application's supervision tree.

Manually adding it to the supervision tree is documented here: https://hexdocs.pm/cloak_ecto/install.html#create-a-vault

Use the init() callback in your Vault file:

defmodule MyApp.Vault do
  use Cloak.Vault, otp_app: :my_app

  @impl GenServer
  def init(config) do
    config =
      Keyword.put(config, :ciphers, [
        default: {Cloak.Ciphers.AES.GCM, tag: "AES.GCM.V1", key: decode_env!("CLOAK_KEY")}
      ])

    {:ok, config}
  end

  defp decode_env!(var) do
    var
    |> System.get_env()
    |> Base.decode64!()
  end
end

And then add your vault under your sub-app's supervision tree in the sub app's application.ex file:

children = [
  MyApp.Vault
]
gmile commented 2 years ago

We found what was causing this issue for us. In our case, this was happening in tests, and was caused by a combination of factors, which may not be relevant here. However, the important clue that helped us untangle this: missing MyApp.Vault.Config meant vault process was dead or haven't started.

After looking closer, we identified that many other processes that typically must be alive - are not alive. Which lead us to a discovery related to mocking in tests: mocking some code was causing app tree to completely die.

I wrote about it a bit here: https://elixirforum.com/t/an-issue-with-mock-library/51514/5?u=gmile