Class: Handsomefencer::Environment::Crypto

Inherits:
Object
  • Object
show all
Defined in:
lib/handsomefencer/environment/crypto.rb

Instance Method Summary collapse

Constructor Details

#initializeCrypto

Returns a new instance of Crypto.



6
7
8
9
10
# File 'lib/handsomefencer/environment/crypto.rb', line 6

def initialize
  @cipher = OpenSSL::Cipher.new 'AES-128-CBC'
  @salt = '8 octets'
  get_deploy_key
end

Instance Method Details

#decrypt(file) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/handsomefencer/environment/crypto.rb', line 45

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) ⇒ Object



39
40
41
42
43
# File 'lib/handsomefencer/environment/crypto.rb', line 39

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) ⇒ Object



64
65
66
67
68
# File 'lib/handsomefencer/environment/crypto.rb', line 64

def expose(directory=nil, extension=nil)
  extension = extension || '.env.enc'
  directory = directory || '.env'
  source_files(directory, extension).each { |file| decrypt(file) }
end

#get_deploy_keyObject



12
13
14
15
16
17
18
# File 'lib/handsomefencer/environment/crypto.rb', line 12

def get_deploy_key
  if ENV['DEPLOY_KEY'].nil?
    @pass_phrase = read_deploy_key.nil? ? save_deploy_key : read_deploy_key
  else
    @pass_phrase = ENV['DEPLOY_KEY']
  end
end

#ignore_sensitive_filesObject



27
28
29
30
31
32
33
# File 'lib/handsomefencer/environment/crypto.rb', line 27

def ignore_sensitive_files
  ["/#{dkfile}", "/.env/*"].each do |pattern|
    unless File.read('.gitignore').match pattern
      open('.gitignore', 'a') { |f| f << pattern }
    end
  end
end

#obfuscate(directory = nil, extension = nil) ⇒ Object



58
59
60
61
62
# File 'lib/handsomefencer/environment/crypto.rb', line 58

def obfuscate(directory=nil, extension=nil)
  extension = extension || '.env'
  directory = directory || '.env'
  source_files(directory, extension).each { |file| encrypt file }
end

#read_deploy_keyObject



35
36
37
# File 'lib/handsomefencer/environment/crypto.rb', line 35

def read_deploy_key
  File.exist?(dkfile) ? Base64.decode64(File.read dkfile) : nil
end

#save_deploy_keyObject



20
21
22
23
24
25
# 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) ⇒ Object



53
54
55
56
# File 'lib/handsomefencer/environment/crypto.rb', line 53

def source_files(directory=nil, extension=nil)
  default = Dir.glob(".env/**/*#{extension}")
  directory.nil? ? default : Dir.glob(directory + "/**/*#{extension}")
end