ankane / lockbox

Modern encryption for Ruby and Rails
MIT License
1.44k stars 68 forks source link

How to access encrypted values from rails console? #192

Closed sickdyd closed 5 months ago

sickdyd commented 5 months ago

I have lockbox v1.3.3 / blind index installed (have to use this version, can't upgrade at the moment).

I followed these steps:

When I drop the plain text column, I am no longer able to access the email field by using User.last.email, it says

undefined method `email' for #<User:0x0000000125f46340>

I would expect to be able to access this field.

This is how my user model looks like:

  has_encrypted :email, migrating: true
  blind_index :email, migrating: true

  # removing this does not change anything, email is still not acessible
  self.ignored_columns = ["email"]

The initial migration:

class AddEncryptionToPiiFields < ActiveRecord::Migration[6.0]
  def change
    # users encrypted fields
    add_column :users, :email_ciphertext, :text

    # users blind columns and indexes
    add_column :users, :email_bidx, :string
    add_index :users, :email_bidx, unique: true
  end
end

After the first migration I ran Lockbox.migrate(User). Once that has completed, I drop the columns with this following migration:

class DropNonEncryptedFields < ActiveRecord::Migration[6.0]
  def change
    remove_column :users, :email
  end
end

What am I missing? Why I am not able to access the email field anymore?

The DB seems to have all the fields in place.

ankane commented 5 months ago

Hi @sickdyd, you need to remove migrating: true after migrating the data.