Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

decrypt_table! fails with unencrypted data #205

Open
darkporpoise opened this issue Jun 4, 2024 · 0 comments
Open

decrypt_table! fails with unencrypted data #205

darkporpoise opened this issue Jun 4, 2024 · 0 comments

Comments

@darkporpoise
Copy link

This might just be one to document as it might save someone in the future a couple of hours:

If you have an encrypted field, but not every row in the database has been encrypted, then Model.decrypt_table! will fail with ActiveSupport::MessageEncryptor::InvalidMessage.

I think this must have come about in my data because a new field with existing data has been added to the list.

I got round it by just adding a decrypt_table! class method to the model in question which suppresses the error:

# models/application_record
def self.decrypt_table!
    tmp_table = Class.new(ActiveRecord::Base).tap {|c| c.table_name = self.table_name}

    transaction do
      tmp_table.find_each do |r|
        crypt_keeper_fields.each do |field|
          begin
            r.send("#{field}=", encryptor.decrypt(r[field])) if r[field].present?
          rescue ActiveSupport::MessageEncryptor::InvalidMessage

          end
        end

        r.save!
      end
    end
  end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant