Class: Portunus::Rotators::Dek

Inherits:
Object
  • Object
show all
Defined in:
lib/portunus/rotators/dek.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data_encryption_key) ⇒ Dek

Returns a new instance of Dek.



8
9
10
# File 'lib/portunus/rotators/dek.rb', line 8

def initialize(data_encryption_key)
  @data_encryption_key = data_encryption_key
end

Class Method Details

.for(data_encryption_key) ⇒ Object



4
5
6
# File 'lib/portunus/rotators/dek.rb', line 4

def self.for(data_encryption_key)
  new(data_encryption_key).rotate
end

Instance Method Details

#rotateObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/portunus/rotators/dek.rb', line 12

def rotate
  encryptable = data_encryption_key.encryptable

  if encryptable.blank?
    Rails.logger.debug("Dek id: #{data_encryption_key.id} is missing it's encryptable... deleting")
    data_encryption_key.destroy
    return true
  end

  Rails.logger.debug(
    "Rotating Encryptable: #{encryptable.class}, id: #{encryptable.id}"
  )

  ActiveRecord::Base.transaction do
    encryptable.class.encrypted_fields_list.map do |field_name|
      field_value_map[field_name.to_sym] = encryptable.send(field_name.to_sym)
    end

    data_encryption_key.update(encrypted_key: new_encrypted_key)
    encryptable.data_encryption_key.reload

    field_value_map.map do |field_name, value|
      encryptable.send("#{field_name}=".to_sym, value)
    end

    encryptable.save
    data_encryption_key.update(last_dek_rotation: DateTime.now)
  end

  true
rescue StandardError => error
  raise ::Portunus::Error.new(
    "Rotating DEK failed: #{error.full_message}"
  )
end