Class: Arver::Keystore

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Keystore

Returns a new instance of Keystore.



20
21
22
23
24
25
# File 'lib/arver/keystore.rb', line 20

def initialize( name )
  @keys = {}
  @key_versions = {}
  @username = name
  @loaded = false
end

Instance Attribute Details

#loadedObject (readonly)

Returns the value of attribute loaded.



18
19
20
# File 'lib/arver/keystore.rb', line 18

def loaded
  @loaded
end

#usernameObject (readonly)

Returns the value of attribute username.



18
19
20
# File 'lib/arver/keystore.rb', line 18

def username
  @username
end

Class Method Details

.for(username) ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/arver/keystore.rb', line 4

def for( username )
  if username.empty?
    Log.error("no user given, cannot create keystore")
    return
  end
  @@keystores           ||= {}
  @@keystores[username] ||= Keystore.new( username )
end

.resetObject



13
14
15
# File 'lib/arver/keystore.rb', line 13

def reset
  @@keystores = {}
end

Instance Method Details

#add_luks_key(partition, new_key) ⇒ Object



84
85
86
# File 'lib/arver/keystore.rb', line 84

def add_luks_key(partition, new_key)
  @keys[partition.path] = { :key => new_key, :time => Time.new.to_f }
end

#flush_keysObject



49
50
51
# File 'lib/arver/keystore.rb', line 49

def flush_keys
  @keys = {}
end

#key_versions(partition) ⇒ Object



80
81
82
# File 'lib/arver/keystore.rb', line 80

def key_versions(partition)
  @key_versions[partition.path] || []
end

#loadObject



27
28
29
30
31
32
33
34
35
# File 'lib/arver/keystore.rb', line 27

def load
  flush_keys
  KeySaver.read(username).each do | loaded |
    YAML.load( loaded ).each do | target, key |
      load_luks_key(target,key)
    end
  end
  @loaded = true
end

#load_luks_key(partition, new_key) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/arver/keystore.rb', line 61

def load_luks_key(partition, new_key)
  if( new_key.kind_of? Hash )
    if( ! @keys[partition] || @keys[partition][:time] <= new_key[:time] )
      @keys[partition] = new_key
    end
  else
    unless( @keys[partition] )
      Log.debug("loding key in old format")          
      @keys[partition] = { :key => new_key, :time => 0.0 }
    end
  end
  mark_key_version(partition,@keys[partition])
end

#luks_key(partition) ⇒ Object



53
54
55
# File 'lib/arver/keystore.rb', line 53

def luks_key(partition)
  luks_key_for_path(partition.path)
end

#luks_key?(partition) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/arver/keystore.rb', line 88

def luks_key?(partition)
  ! @keys[partition.path].nil?
end

#luks_key_for_path(path) ⇒ Object



57
58
59
# File 'lib/arver/keystore.rb', line 57

def luks_key_for_path(path)
  @keys[path][:key] unless ! @keys[path]
end

#mark_key_version(path, key) ⇒ Object



75
76
77
78
# File 'lib/arver/keystore.rb', line 75

def mark_key_version(path,key)
  @key_versions[path] ||= []
  @key_versions[path] << key[:time]
end

#purge_keysObject



45
46
47
# File 'lib/arver/keystore.rb', line 45

def purge_keys
  KeySaver.purge_keys( username )
end

#saveObject



37
38
39
40
41
42
43
# File 'lib/arver/keystore.rb', line 37

def save
  if loaded
    KeySaver.save(username, @keys.to_yaml)
  else
    KeySaver.add(username, @keys.to_yaml)
  end 
end