cloak_ecto
cloak_ecto copied to clipboard
:error when decrypting field
I have a schema that was created with a migration for ecto_ch:
def change do
create table(:settings, primary_key: false, engine: "MergeTree") do
# The primary key for this is the organization id that the settings belong to.
add(:organization_id, :uuid, primary_key: true)
add(:token, :binary)
timestamps(type: :utc_datetime)
end
end
with a model like so:
defmodule MyApp.Orgs.Settings do
use Ecto.Schema
import Ecto.Changeset
@primary_key false
schema "settings" do
field(:token, MyApp.Orgs.SecretKey)
timestamps(type: :utc_datetime)
belongs_to(:organization, MyApp.Orgs.Organization,
references: :id,
type: Ecto.UUID
)
end
@doc false
def changeset(settings, attrs) do
settings
|> cast(attrs, [:token, :organization_id])
|> validate_required([:token, :organization_id])
end
end
where SecretKey is:
defmodule MyApp.Orgs.SecretKey do
use Cloak.Ecto.Binary, vault: MyApp.Orgs.Vault
end
defmodule MyApp.Orgs.Vault do
use Cloak.Vault, otp_app: :myapp
end
I have a simple test that makes a settings object and then lists the object. but I'm getting this error:
Assertion with == failed
code: assert Orgs.list_settings() == [settings]
left: [
%myapp.Orgs.Settings{
__meta__: #Ecto.Schema.Metadata<:loaded, "settings">,
inserted_at: ~U[2024-06-12 20:10:48Z],
token: :error,
organization: %myapp.Orgs.Organization{
__meta__: #Ecto.Schema.Metadata<:loaded, "organizations">,
id: "9a31980f-0342-49af-8377-8e8ed9fc2852",
inserted_at: ~U[2024-06-12 20:10:48Z],
name: "some name",
settings: #Ecto.Association.NotLoaded<association :settings is not loaded>,
updated_at: ~U[2024-06-12 20:10:48Z],
users: #Ecto.Association.NotLoaded<association :users is not loaded>
},
organization_id: "9a31980f-0342-49af-8377-8e8ed9fc2852",
updated_at: ~U[2024-06-12 20:10:48Z]
}
]
right: [
%myapp.Orgs.Settings{
__meta__: #Ecto.Schema.Metadata<:loaded, "settings">,
inserted_at: ~U[2024-06-12 20:10:48Z],
token: "some token",
organization: %myapp.Orgs.Organization{
__meta__: #Ecto.Schema.Metadata<:loaded, "organizations">,
id: "9a31980f-0342-49af-8377-8e8ed9fc2852",
inserted_at: ~U[2024-06-12 20:10:48Z],
name: "some name",
settings: #Ecto.Association.NotLoaded<association :settings is not loaded>,
updated_at: ~U[2024-06-12 20:10:48Z],
users: #Ecto.Association.NotLoaded<association :users is not loaded>
},
organization_id: "9a31980f-0342-49af-8377-8e8ed9fc2852",
updated_at: ~U[2024-06-12 20:10:48Z]
}
]
stacktrace:
test/myapp/orgs_test.exs:15: (test)
Which implies that when I go to decrypt from my database, it's unable to use the vault to decrypt my encrypted key. I updated my test_helper.exs to ensure the vault was started, but no dice. MyApp.Orgs.Vault.start_link()