class Handsomefencer::Environment::Crypto

Public Class Methods

new() click to toggle source
# File lib/handsomefencer/environment/crypto.rb, line 6
def initialize
  @cipher = OpenSSL::Cipher.new 'AES-128-CBC'
  @salt = '8 octets'
  @pass_phrase = Base64.decode64(get_deploy_key)
end

Public Instance Methods

decrypt(file) click to toggle source
# File lib/handsomefencer/environment/crypto.rb, line 43
def decrypt(file)
  encrypted = Base64.decode64 File.read(file)
  @cipher.decrypt.pkcs5_keyivgen @pass_phrase, @salt
  decrypted = @cipher.update(encrypted) + @cipher.final
  decrypted_file = file.split('.enc').first
  write_to_file decrypted, decrypted_file
end
encrypt(file) click to toggle source
# File lib/handsomefencer/environment/crypto.rb, line 37
def encrypt(file)
  @cipher.encrypt.pkcs5_keyivgen @pass_phrase, @salt
  encrypted = @cipher.update(File.read file) + @cipher.final
  write_to_file(Base64.encode64(encrypted), file + '.enc')
end
expose(directory=nil, extension=nil) click to toggle source
# File lib/handsomefencer/environment/crypto.rb, line 62
def expose(directory=nil, extension=nil)
  extension = extension || '.env.enc'
  directory = directory || '.env'
  source_files(directory, extension).each { |file| decrypt(file) }
end
get_deploy_key() click to toggle source
# File lib/handsomefencer/environment/crypto.rb, line 12
def get_deploy_key
  ENV['DEPLOY_KEY'].nil? ? read_deploy_key : ENV['DEPLOY_KEY']
end
ignore_sensitive_files() click to toggle source
# File lib/handsomefencer/environment/crypto.rb, line 27
def ignore_sensitive_files
  if File.exist? '.gitignore'
    ["/#{dkfile}", "/.env/*"].each do |pattern|
      unless File.read('.gitignore').match pattern
        open('.gitignore', 'a') { |f| f << pattern }
      end
    end
  end
end
obfuscate(directory=nil, extension=nil) click to toggle source
# File lib/handsomefencer/environment/crypto.rb, line 56
def obfuscate(directory=nil, extension=nil)
  extension = extension || '.env'
  directory = directory || '.env'
  source_files(directory, extension).each { |file| encrypt file }
end
read_deploy_key() click to toggle source
# File lib/handsomefencer/environment/crypto.rb, line 16
def read_deploy_key
  File.exist?(dkfile) ? File.read(dkfile) : save_deploy_key
end
save_deploy_key() click to toggle source
# File lib/handsomefencer/environment/crypto.rb, line 20
def save_deploy_key
  @new_key = @cipher.random_key
  write_to_file Base64.encode64(@new_key), dkfile
  ignore_sensitive_files
  read_deploy_key
end
source_files(directory=nil, extension=nil) click to toggle source
# File lib/handsomefencer/environment/crypto.rb, line 51
def source_files(directory=nil, extension=nil)
  default = Dir.glob(".env/**/*#{extension}")
  directory.nil? ? default : Dir.glob(directory + "/**/*#{extension}")
end

Private Instance Methods

dkfile() click to toggle source
# File lib/handsomefencer/environment/crypto.rb, line 70
def dkfile
  "config/deploy.key"
end
write_to_file(data, filename) click to toggle source
# File lib/handsomefencer/environment/crypto.rb, line 74
def write_to_file(data, filename)
  open(filename, "w") { |io| io.write data }
end