Configure encryption keys (Master, Per-Field, and Per-Record)
masterLockbox supports three levels of key configuration:
1. Master Key
By default, Lockbox uses a master key to derive unique keys for each field/uploader based on the table name and attribute name.
If you rename a table or column, you must tell Lockbox the original name to maintain access to the derived key:
# If table was renamed
class User < ApplicationRecord
has_encrypted :email, key_table: "original_table"
end
# If column was renamed
class User < ApplicationRecord
has_encrypted :email, key_attribute: "original_column"
end2. Per Field/Uploader
Set a specific key for a single attribute using a string or a proc:
class User < ApplicationRecord
has_encrypted :email, key: ENV["USER_EMAIL_ENCRYPTION_KEY"]
# or
has_encrypted :email, key: -> { some_method }
end3. Per Record
Use a symbol or a proc to call a method on the record instance to retrieve a unique key:
class User < ApplicationRecord
has_encrypted :email, key: :some_method
# or
has_encrypted :email, key: -> { some_method }
end# Table rename
class User < ApplicationRecord
has_encrypted :email, key_table: "original_table"
end
# Column rename
class User < ApplicationRecord
has_encrypted :email, key_attribute: "original_column"
end
# Per field (String/Env)
class User < ApplicationRecord
has_encrypted :email, key: ENV["USER_EMAIL_ENCRYPTION_KEY"]
end
# Per field (Proc)
class User < ApplicationRecord
has_encrypted :email, key: -> { code }
end
# Per record (Symbol)
class User < ApplicationRecord
has_encrypted :email, key: :some_method
end
# Per record (Proc)
class User < ApplicationRecord
has_encrypted :email, key: -> { some_method }
end