Class: Envault::Core
- Inherits:
-
Object
- Object
- Envault::Core
- Defined in:
- lib/envault/core.rb
Instance Attribute Summary collapse
-
#cryptor ⇒ Object
Returns the value of attribute cryptor.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#prefix ⇒ Object
Returns the value of attribute prefix.
Instance Method Summary collapse
- #decrypt_process(hash) ⇒ Object
- #decrypt_value(key, value) ⇒ Object
- #decrypt_yaml(path) ⇒ Object
- #encrypt_process(hash, keys = nil) ⇒ Object
- #encrypt_value(key, value) ⇒ Object
- #encrypt_yaml(path, keys = nil) ⇒ Object
- #get_cipher_keys(hash, keys = ["^#{@prefix}.*"]) ⇒ Object
-
#initialize(config: nil, profile: nil, prefix: nil, debug: false) ⇒ Core
constructor
A new instance of Core.
- #load(path = DEFAULT_SOURCE_FILE) ⇒ Object
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 18 19 20 21 |
# 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 = if profile[:provider] == 'kms' Cryptor::Kms.new(profile) else Cryptor::Simple.new(profile) end @prefix = prefix || profile[:prefix] || DEFAULT_ENV_PREFIX end |
Instance Attribute Details
#cryptor ⇒ Object
Returns the value of attribute cryptor.
9 10 11 |
# File 'lib/envault/core.rb', line 9 def cryptor @cryptor end |
#logger ⇒ Object
Returns the value of attribute logger.
9 10 11 |
# File 'lib/envault/core.rb', line 9 def logger @logger end |
#prefix ⇒ Object
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
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/envault/core.rb', line 49 def decrypt_process(hash) cipher_keys = get_cipher_keys(hash) decrypted = hash.map do |k, v| if cipher_keys.include?(k) decrypt_value(k.gsub(/^#{@prefix}/, ''), v) else [k, v] end end Hash[decrypted] end |
#decrypt_value(key, value) ⇒ Object
61 62 63 |
# File 'lib/envault/core.rb', line 61 def decrypt_value(key, value) [key, @cryptor.decrypt(value)] end |
#decrypt_yaml(path) ⇒ Object
44 45 46 47 |
# File 'lib/envault/core.rb', line 44 def decrypt_yaml(path) hash = YAML.load_file(path) decrypt_process(hash) end |
#encrypt_process(hash, keys = nil) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/envault/core.rb', line 28 def encrypt_process(hash, keys = nil) cipher_keys = get_cipher_keys(hash, keys) encrypted = hash.map do |k, v| if cipher_keys.include?(k) encrypt_value(@prefix + k, v) else [k, v] end end Hash[encrypted] end |
#encrypt_value(key, value) ⇒ Object
40 41 42 |
# File 'lib/envault/core.rb', line 40 def encrypt_value(key, value) [key, @cryptor.encrypt(value)] end |
#encrypt_yaml(path, keys = nil) ⇒ Object
23 24 25 26 |
# File 'lib/envault/core.rb', line 23 def encrypt_yaml(path, keys = nil) hash = YAML.load_file(path) encrypt_process(hash, keys) end |
#get_cipher_keys(hash, keys = ["^#{@prefix}.*"]) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/envault/core.rb', line 65 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
83 84 85 86 87 88 89 90 |
# File 'lib/envault/core.rb', line 83 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 |