Class: ShadowPass

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

Overview

Author:

  • Zach Leslie

Constant Summary collapse

VERSION =
'0.0.1'

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ ShadowPass

Returns a new instance of ShadowPass.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/shadowpass.rb', line 12

def initialize (config)
  @config = config
  @pwfile = File.expand_path(@config[:pwfile])
  @keyid = config[:keyid]
  @verbose = config[:opts][:verbose]
  # determine of this is a new database
  if File.exists?(@pwfile)
    @shadows = loadShadow
  else
    @shadows = initShadow
  end
end

Instance Method Details

#flush(data) ⇒ Object

Encrypt the data object and flush to disk



79
80
81
82
83
84
85
86
# File 'lib/shadowpass.rb', line 79

def flush( data )
  # Build ciphertext
  crypto = GPGME::Crypto.new(:armor => true)
  cipher = crypto.encrypt "#{data.to_json}", :recipients => @keyid
  # Write te cypertext to the file
  speak("write the shadow data to file at #{@pwfile}")
  File.open(@pwfile, 'w') {|f| f.write( cipher.read ) }
end

#get(path) ⇒ Object



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

def get (path)
  speak("looking for value at #{path.join('/')}")
  value = path.inject(@shadows) {|result, element|
    if result[element]
      result[element]
    else
      result
    end
  }
end

#merge_gently(a, b) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/shadowpass.rb', line 55

def merge_gently(a,b)
  speak('received original data object')
  speak('received new data object')
  data = Hash.new
  a.each do |k,v|
    if k == b.keys.first
      # Ig data[k] is a string, then we are likely replacing something here.
      data[k] = merge_gently(a[k],b[k]) unless data[k].is_a? String
      data
    else
      data[k] = a[k]
      data[b.keys.first] = b[b.keys.first]
      data
    end
  end
  speak('here is the merged data object to be returned')
  data
end

#set(path) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/shadowpass.rb', line 36

def set (path)
  value = path.pop

  # Build the new data object with the supplied path
  speak("building new data object")
  data = path.reverse.inject(value) {|result, element|
    { element => result }
  }

  # Merge the new data object into the existing shadow
  speak("merging new data object into shadows")
  if @shadows.empty?
    flush data
  else
    newshadows = merge_gently(@shadows,data)
    flush newshadows
  end
end

#speak(what) ⇒ Object



74
75
76
# File 'lib/shadowpass.rb', line 74

def speak(what)
  puts what if @verbose
end