Class: Envault::Core

Inherits:
Object
  • Object
show all
Defined in:
lib/envault/core.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config: nil, profile: nil, prefix: nil, debug: false) ⇒ Core

Returns a new instance of Core.



11
12
13
14
15
16
17
# File 'lib/envault/core.rb', line 11

def initialize(config: nil, profile: nil, prefix: nil, debug: false)
  @logger = Logger.new(STDOUT)
  @logger.level = debug ? Logger::DEBUG : Logger::INFO
  profile = get_profile(config, profile)
  @cryptor = get_cryptor(profile[:passphrase] || '', profile[:sign_passphrase], profile[:salt] || '')
  @prefix = prefix || profile[:prefix] || DEFAULT_ENV_PREFIX
end

Instance Attribute Details

#cryptorObject

Returns the value of attribute cryptor.



9
10
11
# File 'lib/envault/core.rb', line 9

def cryptor
  @cryptor
end

#loggerObject

Returns the value of attribute logger.



9
10
11
# File 'lib/envault/core.rb', line 9

def logger
  @logger
end

#prefixObject

Returns the value of attribute prefix.



9
10
11
# File 'lib/envault/core.rb', line 9

def prefix
  @prefix
end

Instance Method Details

#decrypt_process(hash) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/envault/core.rb', line 41

def decrypt_process(hash)
  cipher_keys = get_cipher_keys(hash)
  decrypted = hash.map do |k, v|
    if cipher_keys.include?(k)
      [k.gsub(/^#{@prefix}/, ''), @cryptor.decrypt_and_verify(v)]
    else
      [k, v]
    end
  end
  Hash[decrypted]
end

#decrypt_yaml(path) ⇒ Object



36
37
38
39
# File 'lib/envault/core.rb', line 36

def decrypt_yaml(path)
  hash = YAML.load_file(path)
  decrypt_process(hash)
end

#encrypt_process(hash, keys = nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/envault/core.rb', line 24

def encrypt_process(hash, keys = nil)
  cipher_keys = get_cipher_keys(hash, keys)
  encrypted = hash.map do |k, v|
    if cipher_keys.include?(k)
      [@prefix + k, @cryptor.encrypt_and_sign(v)]
    else
      [k, v]
    end
  end
  Hash[encrypted]
end

#encrypt_yaml(path, keys = nil) ⇒ Object



19
20
21
22
# File 'lib/envault/core.rb', line 19

def encrypt_yaml(path, keys = nil)
  hash = YAML.load_file(path)
  encrypt_process(hash, keys)
end

#get_cipher_keys(hash, keys = ["^#{@prefix}.*"]) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/envault/core.rb', line 53

def get_cipher_keys(hash, keys = ["^#{@prefix}.*"])
  all_keys = hash.keys
  if keys
    regexps = []
    keys.each do |key|
      regexps << Regexp.new(key)
    end
    results = regexps.map do |regexp|
      all_keys.select do |key|
        regexp =~ key
      end
    end
    results.flatten
  else
    all_keys
  end
end

#load(path = DEFAULT_SOURCE_FILE) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/envault/core.rb', line 71

def load(path = DEFAULT_SOURCE_FILE)
  hash = decrypt_yaml(path)

  Tempfile.create("dotenv-vault") do |f|
    Formatter.write_escape_yaml(f.path, hash)
    Dotenv.load(f.path)
  end
end