Class: Decidim::Voca::AppDecryptBackupFile

Inherits:
Object
  • Object
show all
Defined in:
lib/decidim/voca/backup/app_decrypt_backup_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_enc) ⇒ AppDecryptBackupFile

Returns a new instance of AppDecryptBackupFile.

Raises:

  • (ArgumentError)


10
11
12
13
14
# File 'lib/decidim/voca/backup/app_decrypt_backup_file.rb', line 10

def initialize(file_enc)
  logger.info "⚙️ starts decrypt file"
  raise ArgumentError, "encrypted file cannot be nil" if file_enc.nil?
  @file_enc = file_enc
end

Instance Attribute Details

#file_pathObject

Returns the value of attribute file_path.



8
9
10
# File 'lib/decidim/voca/backup/app_decrypt_backup_file.rb', line 8

def file_path
  @file_path
end

Instance Method Details

#decrypt!Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/decidim/voca/backup/app_decrypt_backup_file.rb', line 16

def decrypt!
  setup_decipher!
  file_name = File.basename(@file_enc, ".enc")
  @file_path = File.join(File.dirname(@file_enc), file_name)

  File.open(@file_path, "w:BINARY") do |decrypted_file|
    File.open(@file_enc, "r:BINARY") do |encrypted_file|
      data = encrypted_file.read
      decrypted_data = @decipher.update(data) + @decipher.final
      decrypted_file.write(decrypted_data)
    end
  end
  raise Error, "decrypted file not created" unless File.exists?(@file_path)
rescue Exception => e
  logger.error e.message
  raise e
end

#loggerObject

Define logger for the class.



45
46
47
# File 'lib/decidim/voca/backup/app_decrypt_backup_file.rb', line 45

def logger
  @logger ||= Rails.logger
end

#setup_decipher!Object



34
35
36
37
38
39
40
41
# File 'lib/decidim/voca/backup/app_decrypt_backup_file.rb', line 34

def setup_decipher!
  @decipher ||= OpenSSL::Cipher::AES.new(256, :CBC).decrypt
  @decipher.key = Base64.decode64(ENV.fetch("BACKUP_CIPHER_KEY"))
  @decipher.iv = Base64.decode64(ENV.fetch("BACKUP_CIPHER_IV"))
rescue Exception => e
  logger.error e.message
  raise e
end