Add encryption, need to add _hash fields to search, and think about automatization of migration proccess
parent
d7eef32074
commit
4e9e7acd0d
@ -0,0 +1,81 @@ |
||||
defmodule Mix.Tasks.Encrypt do |
||||
@moduledoc "The encrypt mix task: `mix help encrypt`" |
||||
use Mix.Task |
||||
|
||||
@shortdoc "Encrypt" |
||||
def run(_) do |
||||
Mix.Task.run("app.start") |
||||
|
||||
Explorer.Account.Identity |
||||
|> Explorer.Repo.Account.all() |
||||
|> Enum.map(fn identity -> |
||||
identity |
||||
|> Ecto.Changeset.change(%{ |
||||
encrypted_uid: identity.uid, |
||||
encrypted_email: identity.email, |
||||
encrypted_name: identity.name, |
||||
encrypted_nickname: identity.nickname, |
||||
encrypted_avatar: identity.avatar |
||||
}) |
||||
|> Explorer.Repo.Account.update!() |
||||
end) |
||||
|
||||
Explorer.Account.TagAddress |
||||
|> Explorer.Repo.Account.all() |
||||
|> Enum.map(fn element -> |
||||
element |
||||
|> Ecto.Changeset.change(%{ |
||||
encrypted_name: element.name, |
||||
encrypted_address_hash: element.address_hash |
||||
}) |
||||
|> Explorer.Repo.Account.update!() |
||||
end) |
||||
|
||||
Explorer.Account.TagTransaction |
||||
|> Explorer.Repo.Account.all() |
||||
|> Enum.map(fn element -> |
||||
element |
||||
|> Ecto.Changeset.change(%{ |
||||
encrypted_name: element.name, |
||||
encrypted_tx_hash: element.tx_hash |
||||
}) |
||||
|> Explorer.Repo.Account.update!() |
||||
end) |
||||
|
||||
Explorer.Account.CustomABI |
||||
|> Explorer.Repo.Account.all() |
||||
|> Enum.map(fn element -> |
||||
element |
||||
|> Ecto.Changeset.change(%{ |
||||
encrypted_name: element.name, |
||||
encrypted_address_hash: element.address_hash |
||||
}) |
||||
|> Explorer.Repo.Account.update!() |
||||
end) |
||||
|
||||
Explorer.Account.WatchlistAddress |
||||
|> Explorer.Repo.Account.all() |
||||
|> Enum.map(fn element -> |
||||
element |
||||
|> Ecto.Changeset.change(%{ |
||||
encrypted_name: element.name, |
||||
encrypted_address_hash: element.address_hash |
||||
}) |
||||
|> Explorer.Repo.Account.update!() |
||||
end) |
||||
|
||||
Explorer.Account.WatchlistNotification |
||||
|> Explorer.Repo.Account.all() |
||||
|> Enum.map(fn element -> |
||||
element |
||||
|> Ecto.Changeset.change(%{ |
||||
encrypted_name: element.name, |
||||
encrypted_from_address_hash: element.from_address_hash, |
||||
encrypted_to_address_hash: element.to_address_hash, |
||||
encrypted_transaction_hash: element.transaction_hash, |
||||
encrypted_subject: element.subject |
||||
}) |
||||
|> Explorer.Repo.Account.update!() |
||||
end) |
||||
end |
||||
end |
@ -0,0 +1,3 @@ |
||||
defmodule Explorer.Encrypted.AddressHash do |
||||
use Explorer.Encrypted.Types.AddressHash, vault: Explorer.Vault |
||||
end |
@ -0,0 +1,3 @@ |
||||
defmodule Explorer.Encrypted.Binary do |
||||
use Cloak.Ecto.Binary, vault: Explorer.Vault |
||||
end |
@ -0,0 +1,3 @@ |
||||
defmodule Explorer.Encrypted.TransactionHash do |
||||
use Explorer.Encrypted.Types.TransactionHash, vault: Explorer.Vault |
||||
end |
@ -0,0 +1,26 @@ |
||||
defmodule Explorer.Encrypted.Types.AddressHash do |
||||
@moduledoc """ |
||||
An `Ecto.Type` to encrypt address_hash fields. |
||||
""" |
||||
|
||||
@doc false |
||||
defmacro __using__(opts) do |
||||
opts = Keyword.merge(opts, vault: Keyword.fetch!(opts, :vault)) |
||||
|
||||
quote do |
||||
use Cloak.Ecto.Type, unquote(opts) |
||||
|
||||
def cast(value) do |
||||
Explorer.Chain.Hash.Address.cast(value) |
||||
end |
||||
|
||||
def after_decrypt(nil), do: nil |
||||
def after_decrypt(""), do: nil |
||||
|
||||
def after_decrypt(value) do |
||||
{:ok, address_hash} = Explorer.Chain.Hash.Address.cast(value) |
||||
address_hash |
||||
end |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,26 @@ |
||||
defmodule Explorer.Encrypted.Types.TransactionHash do |
||||
@moduledoc """ |
||||
An `Ecto.Type` to encrypt address_hash fields. |
||||
""" |
||||
|
||||
@doc false |
||||
defmacro __using__(opts) do |
||||
opts = Keyword.merge(opts, vault: Keyword.fetch!(opts, :vault)) |
||||
|
||||
quote do |
||||
use Cloak.Ecto.Type, unquote(opts) |
||||
|
||||
def cast(value) do |
||||
Explorer.Chain.Hash.Full.cast(value) |
||||
end |
||||
|
||||
def after_decrypt(nil), do: nil |
||||
def after_decrypt(""), do: nil |
||||
|
||||
def after_decrypt(value) do |
||||
{:ok, address_hash} = Explorer.Chain.Hash.Full.cast(value) |
||||
address_hash |
||||
end |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,17 @@ |
||||
defmodule Explorer.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 |
@ -0,0 +1,45 @@ |
||||
defmodule Explorer.Repo.Account.Migrations.EncryptAccountData do |
||||
use Ecto.Migration |
||||
|
||||
def change do |
||||
alter table(:account_identities) do |
||||
add(:encrypted_uid, :binary) |
||||
add(:encrypted_email, :binary) |
||||
add(:encrypted_name, :binary) |
||||
add(:encrypted_nickname, :binary, null: true) |
||||
add(:encrypted_avatar, :binary, null: true) |
||||
end |
||||
|
||||
# alter table(:account_watchlists) do |
||||
# add(:encrypted_name, :binary) |
||||
# end |
||||
|
||||
alter table(:account_custom_abis) do |
||||
add(:encrypted_address_hash, :binary) |
||||
add(:encrypted_name, :binary) |
||||
end |
||||
|
||||
alter table(:account_tag_addresses) do |
||||
add(:encrypted_name, :binary) |
||||
add(:encrypted_address_hash, :binary) |
||||
end |
||||
|
||||
alter table(:account_tag_transactions) do |
||||
add(:encrypted_name, :binary) |
||||
add(:encrypted_tx_hash, :binary) |
||||
end |
||||
|
||||
alter table(:account_watchlist_addresses) do |
||||
add(:encrypted_name, :binary) |
||||
add(:encrypted_address_hash, :binary) |
||||
end |
||||
|
||||
alter table(:account_watchlist_notifications) do |
||||
add(:encrypted_name, :binary) |
||||
add(:encrypted_subject, :binary, null: true) |
||||
add(:encrypted_from_address_hash, :binary) |
||||
add(:encrypted_to_address_hash, :binary) |
||||
add(:encrypted_transaction_hash, :binary) |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,70 @@ |
||||
defmodule Explorer.Repo.Account.Migrations.RemoveUnencryptedFields do |
||||
use Ecto.Migration |
||||
|
||||
def change do |
||||
alter table(:account_identities) do |
||||
remove(:uid) |
||||
remove(:email) |
||||
remove(:name) |
||||
remove(:nickname) |
||||
remove(:avatar) |
||||
end |
||||
|
||||
rename(table(:account_identities), :encrypted_uid, to: :uid) |
||||
rename(table(:account_identities), :encrypted_email, to: :email) |
||||
rename(table(:account_identities), :encrypted_name, to: :name) |
||||
rename(table(:account_identities), :encrypted_nickname, to: :nickname) |
||||
rename(table(:account_identities), :encrypted_avatar, to: :avatar) |
||||
|
||||
# alter table(:account_watchlists) do |
||||
# remove(:name) |
||||
# end |
||||
# rename(table(:account_watchlists), :encrypted_name, to: :name) |
||||
|
||||
alter table(:account_custom_abis) do |
||||
remove(:address_hash) |
||||
remove(:name) |
||||
end |
||||
|
||||
rename(table(:account_custom_abis), :encrypted_address_hash, to: :address_hash) |
||||
rename(table(:account_custom_abis), :encrypted_name, to: :name) |
||||
|
||||
alter table(:account_tag_addresses) do |
||||
remove(:address_hash) |
||||
remove(:name) |
||||
end |
||||
|
||||
rename(table(:account_tag_addresses), :encrypted_address_hash, to: :address_hash) |
||||
rename(table(:account_tag_addresses), :encrypted_name, to: :name) |
||||
|
||||
alter table(:account_tag_transactions) do |
||||
remove(:tx_hash) |
||||
remove(:name) |
||||
end |
||||
|
||||
rename(table(:account_tag_transactions), :encrypted_tx_hash, to: :tx_hash) |
||||
rename(table(:account_tag_transactions), :encrypted_name, to: :name) |
||||
|
||||
alter table(:account_watchlist_addresses) do |
||||
remove(:address_hash) |
||||
remove(:name) |
||||
end |
||||
|
||||
rename(table(:account_watchlist_addresses), :encrypted_address_hash, to: :address_hash) |
||||
rename(table(:account_watchlist_addresses), :encrypted_name, to: :name) |
||||
|
||||
alter table(:account_watchlist_notifications) do |
||||
remove(:to_address_hash) |
||||
remove(:from_address_hash) |
||||
remove(:transaction_hash) |
||||
remove(:subject) |
||||
remove(:name) |
||||
end |
||||
|
||||
rename(table(:account_watchlist_notifications), :encrypted_name, to: :name) |
||||
rename(table(:account_watchlist_notifications), :encrypted_subject, to: :subject) |
||||
rename(table(:account_watchlist_notifications), :encrypted_from_address_hash, to: :from_address_hash) |
||||
rename(table(:account_watchlist_notifications), :encrypted_to_address_hash, to: :to_address_hash) |
||||
rename(table(:account_watchlist_notifications), :encrypted_transaction_hash, to: :transaction_hash) |
||||
end |
||||
end |
Loading…
Reference in new issue