Class: YAMP::Vault
- Inherits:
-
Object
- Object
- YAMP::Vault
- Defined in:
- lib/yamp.rb
Instance Method Summary collapse
- #add(id, password, username = nil) ⇒ Object
- #export(file) ⇒ Object
- #get(id, key) ⇒ Object
- #import(file) ⇒ Object
-
#initialize(master, redis = Redis.new) ⇒ Vault
constructor
A new instance of Vault.
- #list ⇒ Object
- #remove(id) ⇒ Object
- #update(id, key, value) ⇒ Object
Constructor Details
#initialize(master, redis = Redis.new) ⇒ Vault
Returns a new instance of Vault.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/yamp.rb', line 10 def initialize master, redis=Redis.new @redis = redis master_hash = redis.get "__mstr_h" master_salt = redis.get "__mstr_s" if master_hash && master_salt @master_key = OpenSSL::PKCS5.pbkdf2_hmac_sha1(master, hex_to_bytes(master_salt), 10000, 32) unless OpenSSL::Digest::SHA512.hexdigest(@master_key) == master_hash raise ArgumentError, 'ERR invalid password' end else salt = OpenSSL::Random.random_bytes(32) @master_key = OpenSSL::PKCS5.pbkdf2_hmac_sha1(master, salt, 10000, 32) redis.set "__mstr_h", OpenSSL::Digest::SHA512.hexdigest(@master_key) redis.set "__mstr_s", bytes_to_hex(salt) end end |
Instance Method Details
#add(id, password, username = nil) ⇒ Object
27 28 29 30 31 32 |
# File 'lib/yamp.rb', line 27 def add id, password, username=nil return false if %w{__mstr_h __mstr_s}.include? id return false unless @redis.hsetnx id, "pwd", encrypt(id, password) @redis.hsetnx id, "usr", encrypt(id, username) if username return true end |
#export(file) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/yamp.rb', line 56 def export file begin File.open file, 'w' do |f| f.write "__mstr_h: #{@redis.get '__mstr_h'}\n" f.write "__mstr_s: #{@redis.get '__mstr_s'}\n" @redis.keys.select {|k| @redis.type(k) == "hash"} .each do |k| f.write(({k => @redis.hgetall(k)}).to_yaml[3..-1]) end end rescue Errno::ENOENT raise ArgumentError, 'ERR invalid path' end end |
#get(id, key) ⇒ Object
45 46 47 48 49 50 |
# File 'lib/yamp.rb', line 45 def get id, key return nil if %w{__mstr_h __mstr_s}.include? id return nil unless [:pwd, :usr].include? key return nil unless @redis.hexists id, key decrypt id, @redis.hget(id, key) end |
#import(file) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/yamp.rb', line 70 def import file begin data = YAML.load_file file @redis.flushdb data.each do |key, val| if key.match "^__mstr_[h|s]" @redis.set key, val next end data[key].each {|k, v| @redis.hset key, k, v} end rescue Errno::ENOENT raise ArgumentError, 'ERR file not found' end end |
#list ⇒ Object
52 53 54 |
# File 'lib/yamp.rb', line 52 def list @redis.keys - %w{__mstr_h __mstr_s} end |
#remove(id) ⇒ Object
40 41 42 43 |
# File 'lib/yamp.rb', line 40 def remove id return false if %w{__mstr_h __mstr_s}.include? id @redis.del id end |
#update(id, key, value) ⇒ Object
34 35 36 37 38 |
# File 'lib/yamp.rb', line 34 def update id, key, value return false if %w{__mstr_h __mstr_s}.include? id return false unless [:pwd, :usr].include? key @redis.hset id, key, encrypt(id, value) end |