Class: Watobo::FileSessionStore

Inherits:
SessionStore show all
Defined in:
lib/watobo/adapters/file/file_store.rb,
lib/watobo/adapters/file/marshal_store.rb

Overview

:nodoc: all

Instance Method Summary collapse

Constructor Details

#initialize(project_name, session_name) ⇒ FileSessionStore

Returns a new instance of FileSessionStore.



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
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
# File 'lib/watobo/adapters/file/file_store.rb', line 131

def initialize(project_name, session_name)

  wsp = Watobo.workspace_path
  return false unless File.exist? wsp
  puts "* using workspace path: #{wsp}" if $DEBUG

  @log_file = nil
  @log_lock = Mutex.new

  @project_path = File.join(wsp, project_name)
  unless File.exist? @project_path
    puts "* create project path: #{@project_path}" if $DEBUG
    Dir.mkdir(@project_path)
  end

  @project_config_path = File.join(@project_path, ".config")
  Dir.mkdir @project_config_path unless File.exist? @project_config_path

  @session_path = File.join(@project_path, session_name)

  unless File.exist? @session_path
    puts "* create session path: #{@session_path}" if $DEBUG
    Dir.mkdir(@session_path)
  end

  @session_config_path = File.join(@session_path, ".config")
  Dir.mkdir @session_config_path unless File.exist? @session_config_path

  sext = Watobo::Conf::General.session_settings_file_ext

  @session_file = File.join(@session_path, session_name + sext)
  @project_file = File.join(@project_path, project_name + Watobo::Conf::General.project_settings_file_ext)

  @conversation_path = File.expand_path(File.join(@session_path, Watobo::Conf::Datastore.conversations))

  @findings_path = File.expand_path(File.join(@session_path, Watobo::Conf::Datastore.findings))
  @log_path = File.expand_path(File.join(@session_path, Watobo::Conf::Datastore.event_logs_dir))
  @scanlog_path = File.expand_path(File.join(@session_path, Watobo::Conf::Datastore.scan_logs_dir))

  [ @conversation_path, @findings_path, @log_path, @scanlog_path ].each do |folder|
    if not File.exists?(folder) then
      puts "create path #{folder}"
      begin
        Dir.mkdir(folder)
      rescue SystemCallError => bang
        puts "!!!ERROR:"
        puts bang
      rescue => bang
        puts "!!!ERROR:"
        puts bang
      end
    end
  end

  @log_file = File.join(@log_path, session_name + ".log")

#     @chat_files = get_file_list(@conversation_path, "*-chat")
#     @finding_files = get_file_list(@findings_path, "*-finding")
end

Instance Method Details

#add_chat(chat) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/watobo/adapters/file/file_store.rb', line 96

def add_chat(chat)
  return false unless chat_valid? chat
  chat_file = File.join("#{@conversation_path}", "#{chat.id}-chat.yml")
  chat_data = {
    :request => chat.request.map{|x| x.inspect},
    :response => chat.response.map{|x| x.inspect},
  }

  chat_data.update(chat.settings)
  if not File.exists?(chat_file) then
    File.open(chat_file, "w") { |fh|
      YAML.dump(chat_data, fh)
    }
  chat.file = chat_file
  return true
  end
  return false
end

#add_finding(finding) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/watobo/adapters/file/file_store.rb', line 12

def add_finding(finding)
  return false unless finding.respond_to? :request
  return false unless finding.respond_to? :response

  finding_file = File.join("#{@findings_path}", "#{finding.id}-finding.yml")
  if not File.exists?(finding_file) then

    finding_data = {
      :request => finding.request.map{|x| x.inspect},
      :response => finding.response.map{|x| x.inspect},
      :details => Hash.new
    }
    finding_data[:details].update(finding.details)

    fh = File.new(finding_file, "w+b")
    fh.print YAML.dump(finding_data)
  fh.close
  return true
  end
  return false
end

#add_scan_log(chat, scan_name = nil) ⇒ Object

add_scan_log adds a chat to a specific log store, e.g. if you want to log scan results. needs a scan_name (STRING) as its destination which will be created if the scan name does not exist.



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
# File 'lib/watobo/adapters/file/file_store.rb', line 63

def add_scan_log(chat, scan_name = nil)
  return false unless chat.respond_to? :request
  return false unless chat.respond_to? :response
  begin
  
    return false if scan_name.nil?
    return false if scan_name.empty?
    
    scan_name_clean = scan_name.gsub(/[:\\\/\.]*/,"_")
    # puts ">> scan_name"
    path = File.join(@scanlog_path, scan_name_clean)

    Dir.mkdir path unless File.exist? path

    log_file = File.join( path, "log_" + Time.now.to_f.to_s + ".yml")

    chat_data = {
      :request => chat.request.map{|x| x.inspect},
      :response => chat.response.map{|x| x.inspect},
    }
    # puts log_file
    chat_data.update(chat.settings)
    File.open(log_file, "w") { |fh|
      YAML.dump(chat_data, fh)
    }
    return true
  rescue => bang
    puts bang
    puts bang.backtrace if $DEBUG
  end
  return false
end

#delete_finding(finding) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/watobo/adapters/file/file_store.rb', line 34

def delete_finding(finding)
  finding_file = File.join("#{@findings_path}", "#{finding.id}-finding")
  File.delete finding_file if File.exist? finding_file
  finding_file << ".yml"
  File.delete finding_file if File.exist? finding_file

end

#each_chat(&block) ⇒ Object



115
116
117
118
119
120
121
# File 'lib/watobo/adapters/file/file_store.rb', line 115

def each_chat(&block)
  get_file_list(@conversation_path, "*-chat*").each do |fname|
    chat = Watobo::Utils.loadChatYAML(fname)
    next unless chat
    yield chat if block_given?
  end
end

#each_finding(&block) ⇒ Object



123
124
125
126
127
128
129
# File 'lib/watobo/adapters/file/file_store.rb', line 123

def each_finding(&block)
  get_file_list(@findings_path, "*-finding*").each do |fname|
    f = Watobo::Utils.loadFindingYAML(fname)
    next unless f
    yield f if block_given?
  end
end

#load_project_settings(group) ⇒ Object



229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/watobo/adapters/file/file_store.rb', line 229

def load_project_settings(group)
  # puts ">> load_project_settings : #{group}"
  file = Watobo::Utils.snakecase group.gsub(/\.yml/,'')
  file << ".yml"

  project_file = File.join(@project_config_path, file)
  # puts "File: #{project_file}"
  # puts "---"

  s = Watobo::Utils.load_settings(project_file)
  s

end

#load_session_settings(group) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/watobo/adapters/file/file_store.rb', line 203

def load_session_settings(group)
  # puts ">> load_session_settings : #{group}"
  file = Watobo::Utils.snakecase group.gsub(/\.yml/,'')
  file << ".yml"

  session_file = File.join(@session_config_path, file)
  # puts "File: #{session_file}"
  #  puts "---"

  s = Watobo::Utils.load_settings(session_file)
  s
end

#logger(message, prefs = {}) ⇒ Object



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/watobo/adapters/file/file_store.rb', line 251

def logger( message, prefs = {} )
  opts = { :sender => "unknown", :level => Watobo::Constants::LOG_INFO }
  opts.update prefs
  return false if @log_file.nil?
  begin
    t = Time.now
    now = t.strftime("%m/%d/%Y @ %H:%M:%S")
    log_message = [ now ]
    log_message << "#{opts[:sender]}"
    if message.is_a? Array
      log_message << message.join("\n| ")
      log_message << "\n-"
    else
    log_message << message
    end
    @log_lock.synchronize do
      File.open(@log_file,"a") do |lfh|
        lfh.puts log_message.join("|")
      end
    end
  rescue => bang
    puts bang
  end

end

#logsObject



243
244
245
246
247
248
249
# File 'lib/watobo/adapters/file/file_store.rb', line 243

def logs
  l = ''
  @log_lock.synchronize do
    l = File.open(@log_file).read
  end
  l
end

#num_chatsObject



4
5
6
# File 'lib/watobo/adapters/file/file_store.rb', line 4

def num_chats
  get_file_list(@conversation_path, "*-chat*").length
end

#num_findingsObject



8
9
10
# File 'lib/watobo/adapters/file/file_store.rb', line 8

def num_findings
  get_file_list(@findings_path, "*-finding*").length
end

#save_chat(file, chat) ⇒ Object



40
41
42
43
44
# File 'lib/watobo/adapters/file/marshal_store.rb', line 40

def save_chat(file, chat)
  File.open(file, 'wb'){|f|
    f.print Marshal::dump(chat.to_h)
    }
end

#save_finding(fname, finding) ⇒ Object



34
35
36
37
38
# File 'lib/watobo/adapters/file/marshal_store.rb', line 34

def save_finding(fname, finding)
  File.open(fname, 'wb'){|f|
    f.print Marshal::dump(finding.to_h)
    }
end

#save_project_settings(group, project_settings) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/watobo/adapters/file/file_store.rb', line 216

def save_project_settings(group, project_settings)
  # puts ">> save_project_settings : #{group}"
  file = Watobo::Utils.snakecase group.gsub(/\.yml/,'')
  file << ".yml"

  project_file = File.join(@project_config_path, file)
  # puts "Dest.File: #{project_file}"
  # puts project_settings.to_yaml
  # puts "---"
  Watobo::Utils.save_settings(project_file, project_settings)

end

#save_session_settings(group, session_settings) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
# File 'lib/watobo/adapters/file/file_store.rb', line 191

def save_session_settings(group, session_settings)
  # puts ">> save_session_settings <<"
  file = Watobo::Utils.snakecase group.gsub(/\.yml/,'')
  file << ".yml"

  session_file = File.join(@session_config_path, file)
  # puts "Dest.File: #{session_file}"
  #  puts session_settings.to_yaml
  # puts "---"
  Watobo::Utils.save_settings(session_file, session_settings)
end

#update_finding(finding) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/watobo/adapters/file/file_store.rb', line 42

def update_finding(finding)
  finding_file = File.join("#{@findings_path}", "#{finding.id}-finding.yml")
  finding_data = {
    :request => finding.request.map{|x| x.inspect},
    :response => finding.response.map{|x| x.inspect},
    :details => Hash.new
  }
  finding_data[:details].update(finding.details)

  if File.exists?(finding_file) then
    fh = File.new(finding_file, "w+b")
    fh.print YAML.dump(finding_data)
  fh.close
  end

end