Class: Lockr

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

Instance Method Summary collapse

Instance Method Details

#acquire_additional_input(options) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/lockr.rb', line 135

def acquire_additional_input( options)
  # id is required for all actions except list
  while options[:id].nil? and %w{ l list}.index( options[:action]).nil?
    options[:id] = ask("Id?  ") { |q| }.to_s
    options[:id] = nil if options[:id].strip() == '' 
  end
  
  # username is required for actions add, remove
  actions_requiring_username = %w{ a add r remove}
  while options[:username].nil? and not actions_requiring_username.index( options[:action]).nil?
    options[:username] = ask("Username?  ") { |q| }.to_s
    options[:username] = nil if options[:username].strip == ''
  end
  
  # url is optional for add
  actions_requiring_url = %w{ a add}
  if options[:url].nil? and not actions_requiring_url.index( options[:action]).nil?
    options[:url] = ask("Url?  ") { |q| }.to_s
    options[:url] = nil if options[:url].strip() == ''
  end
end

#merge_config(configfile, options) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/lockr.rb', line 97

def merge_config( configfile, options)
  if configfile.config.nil? 
    return
  end
  
  unless configfile.config.has_key?( :lockr)
    return
  end
  
  cfg = configfile.config[:lockr]
  options[:vault] = File.expand_path(cfg[:vault]) if options[:vault] == 'vault.yaml'
end

#parse_optionsObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/lockr.rb', line 27

def parse_options()
  options = {}
  
  optparse = OptionParser.new do|opts|
  # Set a banner, displayed at the top
  # of the help screen.
    opts.banner = "Usage: lockr.rb [options]"
  
    # Define the options, and what they do
    options[:action] = nil
    opts.on( '-a', '--action ACTION', 'Execute the requested ACTION (add, remove, list, show)' ) do |id|
      options[:action] = id
    end
  
    options[:id] = nil
    opts.on( '-i', '--id ID', 'the ID of the password set' ) do |id|
      options[:id] = id
    end
    
    options[:keyfile] = nil
    opts.on( '-k', '--keyfile FILE', 'the FILE to use as key for the password encryption') do |file|
      options[:keyfile] = File.expand_path(file)
    end
    
    options[:vault] = 'vault.yaml'
    opts.on( '-v', '--vault FILE', 'FILE is the name of the vault to store the password sets') do |file|
      options[:vault] = File.expand_path(file)
    end
    
    options[:generatepwd] = nil
    opts.on( '-g', '--genpwd PARAMS', 'generate a random password (based on the optional PARAMS)') do |params|
      options[:generatepwd] = params
    end
    
    options[:download] = nil
    opts.on( '-d', '--download', 'download latest vault from configured sftp location before executing action') do |d|
      options[:download] = true
    end
  
    options[:upload] = nil
    opts.on( '-u', '--upload', 'upload vault to configured sftp location after executing action') do |d|
      options[:upload] = true
    end

    # This displays the help screen, all programs are
    # assumed to have this option.
    opts.on( '-h', '--help', 'Display this screen' ) do
      puts opts
      exit
    end
    
    opts.on('--version', 'Show version') do
      puts "Lockr #{LockrVer::VERSION} (#{LockrVer::DATE})"
      exit
    end
    
    opts.separator ""
    opts.separator "For detailed instructions on how to use Lockr, please visit http://lockr.byteblues.com"
  end
  
  # Parse the command-line. Remember there are two forms
  # of the parse method. The 'parse' method simply parses
  # ARGV, while the 'parse!' method parses ARGV and removes
  # any options found there, as well as any parameters for
  # the options. What's left is the list of files to resize.
  optparse.parse!

  options
end

#process_actions(configfile, options) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/lockr.rb', line 157

def process_actions( configfile, options)
  unless options[:download].nil?
    sftp = SFTP.new
    sftp.download( configfile, options[:vault])
  end
  
  begin
    case options[:action]
    when 'a', 'add'
      if options[:generatepwd].nil?
        password = ask("Password?  ") { |q| q.echo = "x" }.to_s
      else 
        password = PasswordGenerator.new.generate( options[:generatepwd])
      end 
      
      action = AddAction.new( options[:id], options[:url], options[:username], password, options[:keyfile], options[:vault])
    when 'r', 'remove'
      action = RemoveAction.new( options[:id], options[:username], options[:keyfile], options[:vault])
    when 's', 'show'
      action = ShowAction.new( options[:id], options[:username], options[:keyfile], options[:vault])
    when 'l', 'list'
      action = ListAction.new( options[:keyfile], options[:vault])
    else
      puts "Unknown action #{options[:action]}"
    end
  rescue OpenSSL::Cipher::CipherError
    say( "<%= color('Invalid keyfile', :red) %>")
    exit 42
  end
  
  unless options[:upload].nil?
    sftp = SFTP.new
    sftp.upload( configfile, options[:vault])
  end
end

#runObject



18
19
20
21
22
23
24
25
# File 'lib/lockr.rb', line 18

def run()
  options = parse_options()
  configfile = Configuration.new()
  merge_config( configfile, options)
  validate_options( options)
  acquire_additional_input( options)
  process_actions( configfile, options)
end

#validate_options(options) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/lockr.rb', line 110

def validate_options( options)
  if options[:action].nil?
    puts 'Please provide an action (--action)'
    exit 1
  end
  
  allowed_actions = %w{ a add r remove s show l list}
  if allowed_actions.index( options[:action]).nil? 
    puts "Allowed actions are add, remove, list and show"
    exit 2
  end
  
  # keyfile is required for all actions other than list
  if options[:keyfile].nil? and %w{ l list}.index( options[:action]).nil?
    puts 'Please provide an encryption key file (--keyfile)'
    exit 3
  end
  
  # keyfile is required for all actions other than list
  if ! options[:keyfile].nil? and ! File.exists?( options[:keyfile] )
    puts 'The provided keyfile does not exist'
    exit 4
  end
end