Class: VPNMaker::KeyTracker

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, dir) ⇒ KeyTracker

Returns a new instance of KeyTracker.


149
150
151
152
153
154
# File 'lib/vpnmaker/key_tracker.rb', line 149

def initialize(name, dir)
  @path = dir
  @db = KeyDB.new(File.join(dir, name + '.db.yaml'))
  @config = KeyConfig.new(File.join(dir, name + '.config.yaml'))
  @builder = KeyBuilder.new(self, @config)
end

Instance Attribute Details

#builderObject (readonly)

Returns the value of attribute builder


3
4
5
# File 'lib/vpnmaker/key_tracker.rb', line 3

def builder
  @builder
end

#configObject (readonly)

Returns the value of attribute config


5
6
7
# File 'lib/vpnmaker/key_tracker.rb', line 5

def config
  @config
end

#dbObject (readonly)

Returns the value of attribute db


4
5
6
# File 'lib/vpnmaker/key_tracker.rb', line 4

def db
  @db
end

#pathObject (readonly)

Returns the value of attribute path


6
7
8
# File 'lib/vpnmaker/key_tracker.rb', line 6

def path
  @path
end

Class Method Details

.generate(name, path = nil) ⇒ Object


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/vpnmaker/key_tracker.rb', line 8

def self.generate(name, path=nil)
  path ||= '/tmp'
  dir = File.join(File.expand_path(path), name + '.vpn')

  FileUtils.mkdir_p(dir)
  datadir = "#{name}_data"
  dbpath = File.join(dir, "#{name}.db.yaml")

  d = KeyDB.new(dbpath)
  d[:version] = 0
  d[:modified] = Time.now
  d[:users] = {}
  d[:datadir] = datadir
  d.sync
end

Instance Method Details

#active_key_version(user) ⇒ Object


136
137
138
139
140
# File 'lib/vpnmaker/key_tracker.rb', line 136

def active_key_version(user)
  assert_user(user)

  @db[:users][user][:active_key]
end

#add_key(user, key, crt, p12, ver) ⇒ Object


73
74
75
76
77
# File 'lib/vpnmaker/key_tracker.rb', line 73

def add_key(user, key, crt, p12, ver)
  @db.dump("#{user}-#{ver}.key", key)
  @db.dump("#{user}-#{ver}.crt", crt)
  @db.dump("#{user}-#{ver}.p12", p12)
end

#add_user(user, name, email, key, crt, p12, index, serial) ⇒ Object


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/vpnmaker/key_tracker.rb', line 83

def add_user(user, name, email, key, crt, p12, index, serial)
  raise "User must be a non-empty string" unless user.is_a?(String) && user.size > 0
  raise "User already exists: #{user}" if @db[:users][user]

  @db[:users][user] = {
    :user => user,
    :name => name,
    :email => email,
    :active_key => 0,
    :revoked => [],
    :modified => Time.now
  }
  @db.dump('serial', serial, true)
  @db.dump('index.txt', index, true)
  add_key(user, key, crt, p12, 0)
  @db.touched!
  @db.sync
end

#add_user_key(user, name, email, key, crt, p12, index, serial) ⇒ Object


102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/vpnmaker/key_tracker.rb', line 102

def add_user_key(user, name, email, key, crt, p12, index, serial)
  assert_user(user)

  u = @db[:users][user]
  u[:modified] = Time.now
  u[:active_key] += 1
  add_key(user, key, crt, p12, u[:active_key])

  @db.dump('serial', serial, true)
  @db.dump('index.txt', index, true)

  @db.touched!
  @db.sync
end

#assert_user(user) ⇒ Object


24
25
26
# File 'lib/vpnmaker/key_tracker.rb', line 24

def assert_user(user)
  raise "User doesn't exist: #{user}" unless @db[:users][user]
end

#caObject


28
# File 'lib/vpnmaker/key_tracker.rb', line 28

def ca; @db[:ca]; end

#key(user, ver, type) ⇒ Object


79
80
81
# File 'lib/vpnmaker/key_tracker.rb', line 79

def key(user, ver, type)
  @db.data("#{user}-#{ver}.#{type}")
end

#revoked?(user, version) ⇒ Boolean

Returns:

  • (Boolean)

130
131
132
133
134
# File 'lib/vpnmaker/key_tracker.rb', line 130

def revoked?(user, version)
  assert_user(user)

  @db[:users][user][:revoked].include?(version)
end

#set_ca(key, crt, crl, index, serial) ⇒ Object


30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/vpnmaker/key_tracker.rb', line 30

def set_ca(key, crt, crl, index, serial)
  raise "CA already set" if @db[:ca]

  @db[:ca] = {:modified => Time.now}
  @db.dump('ca.key', key)
  @db.dump('ca.crt', crt)
  @db.dump('crl.pem', crl)
  @db.dump('index.txt', index)
  @db.dump('serial', serial)
  @db.touched!
  @db.sync
end

#set_dh(dh) ⇒ Object


64
65
66
67
68
69
70
71
# File 'lib/vpnmaker/key_tracker.rb', line 64

def set_dh(dh)
  raise "DH key already set" if @db[:dh]

  @db[:dh] = {:modified => Time.now}
  @db.dump('dh.pem', dh)
  @db.touched!
  @db.sync
end

#set_server_key(key, crt, index, serial) ⇒ Object


43
44
45
46
47
48
49
50
51
52
53
# File 'lib/vpnmaker/key_tracker.rb', line 43

def set_server_key(key, crt, index, serial)
  raise "Server key already set" if @db[:server]

  @db[:server] = {:modified => Time.now}
  @db.dump('server.key', key)
  @db.dump('server.crt', crt)
  @db.dump('index.txt', index, true)
  @db.dump('serial', serial, true)
  @db.touched!
  @db.sync
end

#set_ta_key(ta) ⇒ Object


55
56
57
58
59
60
61
62
# File 'lib/vpnmaker/key_tracker.rb', line 55

def set_ta_key(ta)
  raise "TA key already set" if @db[:ta]

  @db[:ta] = {:modified => Time.now}
  @db.dump('ta.key', ta)
  @db.touched!
  @db.sync
end

#user(user) ⇒ Object


142
143
144
145
# File 'lib/vpnmaker/key_tracker.rb', line 142

def user(user)
  assert_user(user)
  @db[:users][user]
end

#user_key_revoked(user, version, crl, index) ⇒ Object


117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/vpnmaker/key_tracker.rb', line 117

def user_key_revoked(user, version, crl, index)
  assert_user(user)

  raise "Verison must be an int" unless version.kind_of?(Integer)
  u = @db[:users][user]
  u[:revoked] << version
  u[:modified] = Time.now
  @db.dump('index.txt', index, true)
  @db.dump('crl.pem', crl, true)
  @db.touched!
  @db.sync
end

#usersObject


147
# File 'lib/vpnmaker/key_tracker.rb', line 147

def users; @db[:users]; end