Class: Watobo::FileSessionStore

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

Instance Method Summary collapse

Constructor Details

#initialize(project_name, session_name) ⇒ FileSessionStore

Returns a new instance of FileSessionStore.



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
190
191
192
193
194
195
196
197
198
199
# File 'lib/watobo/adapters/file/file_store.rb', line 147

def initialize(project_name, session_name)

  wsp = Watobo.workspace_path
  return false unless File.exist? wsp
  puts "* using workspace path: #{wsp}" if $DEBUG
  @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

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

Instance Method Details

#add_chat(chat) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/watobo/adapters/file/file_store.rb', line 112

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



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/watobo/adapters/file/file_store.rb', line 32

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.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/watobo/adapters/file/file_store.rb', line 83

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?
    # puts ">> scan_name"
    path = File.join(@scanlog_path, scan_name)

    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



54
55
56
57
58
59
60
# File 'lib/watobo/adapters/file/file_store.rb', line 54

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



131
132
133
134
135
136
137
# File 'lib/watobo/adapters/file/file_store.rb', line 131

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



139
140
141
142
143
144
145
# File 'lib/watobo/adapters/file/file_store.rb', line 139

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



239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/watobo/adapters/file/file_store.rb', line 239

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



213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/watobo/adapters/file/file_store.rb', line 213

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

#num_chatsObject



24
25
26
# File 'lib/watobo/adapters/file/file_store.rb', line 24

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

#num_findingsObject



28
29
30
# File 'lib/watobo/adapters/file/file_store.rb', line 28

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

#save_project_settings(group, project_settings) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/watobo/adapters/file/file_store.rb', line 226

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



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

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



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/watobo/adapters/file/file_store.rb', line 62

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