Class: HandsomeFencer::CircleCI::Crypto

Inherits:
Object
  • Object
show all
Defined in:
lib/handsome_fencer/circle_c_i/crypto.rb

Constant Summary collapse

DeployKeyError =
Class.new(StandardError)

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Crypto

Returns a new instance of Crypto.



10
11
12
13
14
15
# File 'lib/handsome_fencer/circle_c_i/crypto.rb', line 10

def initialize(options={})
  @cipher = OpenSSL::Cipher.new 'AES-128-CBC'
  @salt = '8 octets'
  @dkfile = options[:dkfile] ? ('.circleci/' + options[:dkfile] + '.key') : dkfile
  @pass_phrase = get_deploy_key
end

Instance Method Details

#decrypt(file) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/handsome_fencer/circle_c_i/crypto.rb', line 58

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



51
52
53
54
55
56
# File 'lib/handsome_fencer/circle_c_i/crypto.rb', line 51

def encrypt(file)
  file = 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



76
77
78
79
80
# File 'lib/handsome_fencer/circle_c_i/crypto.rb', line 76

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

#get_deploy_keyObject



17
18
19
20
21
22
23
24
25
26
# File 'lib/handsome_fencer/circle_c_i/crypto.rb', line 17

def get_deploy_key
  case
  when ENV['DEPLOY_KEY'].nil? && !File.exist?(@dkfile)
    raise DeployKeyError, "No deploy key set. Please generate a deploy key using '$ bin/rails generate handsome_fencer:circle_c_i:deploy_key' or set it using '$ export ENV['DEPLOY_KEY'] = some-complicated-key'"
  when File.exist?(@dkfile)
    Base64.decode64(File.read(@dkfile))
  when !ENV['DEPLOY_KEY'].nil?
    Base64.decode64(ENV['DEPLOY_KEY'])
  end
end

#ignore_sensitive_filesObject



41
42
43
44
45
46
47
48
49
# File 'lib/handsome_fencer/circle_c_i/crypto.rb', line 41

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



70
71
72
73
74
# File 'lib/handsome_fencer/circle_c_i/crypto.rb', line 70

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

#read_deploy_keyObject



28
29
30
# File 'lib/handsome_fencer/circle_c_i/crypto.rb', line 28

def read_deploy_key
  File.exist?(dkfile) ? File.read(dkfile) : save_deploy_key
end

#save_deploy_keyObject



32
33
34
35
36
37
38
39
# File 'lib/handsome_fencer/circle_c_i/crypto.rb', line 32

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



66
67
68
# File 'lib/handsome_fencer/circle_c_i/crypto.rb', line 66

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