rodloboz

Strongbox encrypts the sensitive fields of your Ecto schemas in your app, before they reach the database: a national ID, a phone number, a token for another service, notes about someone’s health. A leaked backup, a read replica or a query run by hand shows only ciphertext, and your app reads them as it always did. It runs in production in our apps. It’s at 0.1, so before 1.0 a minor version may still change the API.
It started from the Cloak / Cloak Ecto alternatives thread. Ecto 3.15 now brings Ecto.Encrypted and Ecto.HMAC, and if they are all you need, they’re a fine choice. Strongbox is for what comes around them, and it reads and writes their format, so you can start with one and move to the other.
What it does
- A secret stays in its field. Each value is bound to its field’s name, so a value copied into another column (one that is shown, say) fails to decrypt.
- The right key, or nothing. Each value names its key and carries a commitment to it, checked before decrypting. AES-GCM, AES-CCM or ChaCha20-Poly1305 from
:crypto, with a key per value from a random salt. - Finding values again. Blind indexes with a key of their own each, normalizers, unique constraints, short hashes that tell less about which rows are equal, slow ones for values with few possibilities, and lookups that still match while index keys rotate.
- Rotation you can run. Put a new key first and deploy.
Strongbox.Rotation.run/3rewrites only what is under an old key, by compare-and-swap, so never over a write made meanwhile, andreport/3says what is left. PostgreSQL and SQLite. - Nothing to write yourself. Strings, numbers, dates, maps and lists are options of the field (
as: :date), and so are padding (pad: 64) and Base64 for text columns and embeds. Fields declared withStrongbox.Schemaare always redacted. - A way in. It reads what Cloak (AES.GCM and AES.CTR) and
Ecto.Encryptedwrote, as it is, and keepsCloak.Ecto.HMAC’s andEcto.HMAC’s hashes. A rotation writes them again in its own format, bound to their field. - No process. A vault is a module: there is nothing to start. The format is documented byte by byte.
A taste
A vault holds the keys:
defmodule MyApp.Vault do
use Strongbox
@impl true
def keys do
[%{tag: "k1", key: Application.fetch_env!(:my_app, :strongbox_key_k1)}]
end
@impl true
def index_keys do
[%{tag: "i1", key: Application.fetch_env!(:my_app, :strongbox_index_key_i1)}]
end
end
Encrypted fields, and a blind index to find one, in :binary columns:
use Strongbox.Schema, vault: MyApp.Vault
schema "patients" do
field :name, :string
encrypted_field :national_id
encrypted_field :born_on, as: :date
encrypted_field :email, as: :string
blind_index :email_index, of: :email, normalize: [:trim, :downcase]
end
def changeset(patient, attrs) do
patient
|> cast(attrs, [:name, :national_id, :born_on, :email])
|> Strongbox.Changeset.put_indexes()
|> unique_constraint(:email_index)
end
Patient
|> Strongbox.Query.where_index(:email_index, "Ana@Example.com")
|> Repo.one()
A new key, and the old values written again under it:
# keys/0: [%{tag: "k2", key: k2}, %{tag: "k1", key: k1}]
Strongbox.Rotation.report(Repo, Patient)
#=> %{national_id: %{"k2" => 1_204, "k1" => 88_311, nil => 12}, ...}
Strongbox.Rotation.run(Repo, Patient)
#=> {:ok, %{rewritten: 88_311, reindexed: 0, conflicts: 0, unreadable: 0}}
Coming from Cloak, the Cloak key goes after a Strongbox one, and the fields change type, not their data:
[
%{tag: "k1", key: k1},
%{tag: "AES.GCM.V1", key: cloak_key, format: :cloak, iv_length: 12}
]
Feedback very welcome.
- Hex: strongbox | Hex
- Docs: Strongbox v0.1.0 — Documentation
Trending in Announcing
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #code-sync
- #exunit
- #discussion
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #ai
- #authentication
- #elixirconf-us
- #umbrella
- #full-time-contract
- #blog-post
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-eu
- #distillery
- #security
- #processes
- #api
- #forms
- #hex










