jmazzi / crypt_keeper

Transparent ActiveRecord encryption
http://jmazzi.github.com/crypt_keeper/
MIT License
288 stars 99 forks source link

Store encryption password into relation model #154

Closed 4e4c52 closed 6 years ago

4e4c52 commented 6 years ago

Hello,

I try so setup encrypted fields for a model with the encryption key stored in a related model named Account via belongs_to.

Here is what I did in the model:

crypt_keeper :login, :password,
    encryptor: :active_support,
    key: self.account_encryption_key,
    salt: Rails.application.credentials.encoder_salt

  private

  def self.account_encryption_key
    self.account.encryption_key
  end

When I create a record, I get the following error:

NoMethodError: undefined method 'account_encryption_key' for #<Class:XXX...>

Is it possible to do what I'm trying?

I'm using CryptKeeper 2.0.0rc1 and Rails 5.2.0rc1.

Thanks!

itspriddle commented 6 years ago

I'm afraid this isn't possible. CryptKeeper relies on ActiveRecord's serialize method, which doesn't have direct access to the model's instance methods in order to run a query.

I'd recommend adding the key in the same way you've added the salt.