Class: SafeCredentials::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/safe_credentials/config.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = 'config/config.yml', password = nil) ⇒ Config

Returns a new instance of Config.



10
11
12
13
14
15
16
17
18
# File 'lib/safe_credentials/config.rb', line 10

def initialize(path = 'config/config.yml', password = nil)
  @path = File.expand_path(path)
  yaml_source = ERB.new(File.read(path)).result

  @cipher = Gibberish::AES.new(password)

  @attributes = YAML.load(yaml_source)
  @encryptor = HashEncryptor.new(@attributes, @cipher)
end

Instance Attribute Details

#config_fileObject (readonly)

Returns the value of attribute config_file.



8
9
10
# File 'lib/safe_credentials/config.rb', line 8

def config_file
  @config_file
end

#envsObject (readonly)

Returns the value of attribute envs.



8
9
10
# File 'lib/safe_credentials/config.rb', line 8

def envs
  @envs
end

#pathObject (readonly)

Returns the value of attribute path.



8
9
10
# File 'lib/safe_credentials/config.rb', line 8

def path
  @path
end

Class Method Details

.load_encrypted(password, path = 'config/encrypted_config.yml') ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/safe_credentials/config.rb', line 24

def self.load_encrypted(password, path = 'config/encrypted_config.yml')
  config = new(path, password)
  config.decrypt!
  config

rescue OpenSSL::Cipher::CipherError
  $stderr.puts "Wrong password!"
  exit -1
end

Instance Method Details

#[](env_name) ⇒ Object



20
21
22
# File 'lib/safe_credentials/config.rb', line 20

def [](env_name)
  @attributes[env_name.to_s]
end

#decrypt!(paths = '**encrypted_*') ⇒ Object



42
43
44
45
46
47
48
# File 'lib/safe_credentials/config.rb', line 42

def decrypt!(paths = '**encrypted_*')
  @encryptor.decrypt_paths!(paths)

rescue OpenSSL::Cipher::CipherError
  $stderr.puts "Wrong password!"
  exit -1
end

#encrypt!(paths = '*') ⇒ Object



34
35
36
37
38
39
40
# File 'lib/safe_credentials/config.rb', line 34

def encrypt!(paths = '*')
  @encryptor.encrypt_paths!(paths)

rescue OpenSSL::Cipher::CipherError
  $stderr.puts "Wrong password!"
  exit -1
end

#save(file_name = nil) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/safe_credentials/config.rb', line 50

def save(file_name = nil)
  unless file_name
    basename = File.basename(@path)
    dirname  = File.dirname(@path)

    file_name = File.join(dirname, "encrypted_#{basename}")
  end

  File.write(file_name, @attributes.to_yaml)
end