Class: Mu::Muapi

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/mu/api/muapi.rb

Constant Summary

Constants included from Helper

Helper::ESCAPES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper

#ask, #bin2hex, #error, #escape, #format_float, #get_file_as_string_array, #make_xml, #msg, #shift, #to_boolean

Constructor Details

#initialize(host = ENV['MU_IP'], username = ENV['MU_ADMIN_USER'], password = ENV['MU_ADMIN_PASS']) ⇒ Muapi

Returns a new instance of Muapi.



7
8
9
10
11
12
13
14
15
16
# File 'lib/mu/api/muapi.rb', line 7

def initialize(host=ENV['MU_IP'], username=ENV['MU_ADMIN_USER'], password=ENV['MU_ADMIN_PASS'])
  @host = host
  @username  = username
  @password  = password
  @docroot = "/api/v3/"
  @params = nil
  @expected_error = nil
  @http = HttpHelper.new(@host, @username, @password, @docroot)
  msg "Created Mu Api object to :#{@host}", Logger::DEBUG
end

Instance Attribute Details

#docrootObject

Returns the value of attribute docroot.



5
6
7
# File 'lib/mu/api/muapi.rb', line 5

def docroot
  @docroot
end

#expected_errorObject

Returns the value of attribute expected_error.



5
6
7
# File 'lib/mu/api/muapi.rb', line 5

def expected_error
  @expected_error
end

#filepathObject

Returns the value of attribute filepath.



5
6
7
# File 'lib/mu/api/muapi.rb', line 5

def filepath
  @filepath
end

#hostObject

Returns the value of attribute host.



5
6
7
# File 'lib/mu/api/muapi.rb', line 5

def host
  @host
end

#job_idObject

Returns the value of attribute job_id.



5
6
7
# File 'lib/mu/api/muapi.rb', line 5

def job_id
  @job_id
end

#paramsObject

Returns the value of attribute params.



5
6
7
# File 'lib/mu/api/muapi.rb', line 5

def params
  @params
end

#passwordObject

Returns the value of attribute password.



5
6
7
# File 'lib/mu/api/muapi.rb', line 5

def password
  @password
end

#posted_uuidObject

Returns the value of attribute posted_uuid.



5
6
7
# File 'lib/mu/api/muapi.rb', line 5

def posted_uuid
  @posted_uuid
end

#run_uuidObject

Returns the value of attribute run_uuid.



5
6
7
# File 'lib/mu/api/muapi.rb', line 5

def run_uuid
  @run_uuid
end

#usernameObject

Returns the value of attribute username.



5
6
7
# File 'lib/mu/api/muapi.rb', line 5

def username
  @username
end

Instance Method Details

#archive(command = "run", id = @run_uuid, filepath = '.', title = "", notes = "", evts = true, pcaps = true, samples = false, logo = false, audit = false, sign = false) ⇒ Object

  • command=run to create a test archive

    • command=status to view the status of a test archive job

    • command=get to download a test archive job

    • uuid = the uuid of the test

    • title = string to include as title if not empty

    • notes = string to add to notes field if not empty

    • evts = boolean to include evts, default is true

    • pcaps = boolean to include pcaps, default is true

    • samples = boolean to include samples, default is false

    • logo = boolean to include logo, default is false

    • audit = boolean to include audit, default is false

    • sign = boolean to perform sign, default is false



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/mu/api/muapi.rb', line 208

def archive(command="run", id=@run_uuid, filepath='.', title="", notes="", evts=true, pcaps=true, samples=false, =false, audit=false, sign=false)
  case command
  when 'run'
    request_string = "archive/run?uuid=#{id}"
    # not sure where the hell whis is set so I'm commenting out and running my way
    #request_string += @params unless @params.nil?
    # need to set title is not empty
    if title != "" and title != nil
    	# replace all spaces with %20 and add
    	request_string += "&title=" + URI.encode(title)
    end
    # need to set notes if not empty
    if notes != "" and notes != nil
    	# replace all spaces with %20 and add
    	request_string += "&notes=" + URI.encode(notes)
    end
    tmpS = ""
    # do we need to add evts
    if evts
    	tmpS += "evts,"
    end
    # do we need need to add pcaps
    if pcaps
    	tmpS += "pcaps,"
    end
    # do we need need to add sample pcaps
    if samples
    	tmpS += "samples,"
    end
    # do we need need to add logo
    if 
    	tmpS += "logo,"
    end
    # do we need need to add audit
    if audit
    	tmpS += "audit"
    end
    if tmpS != ""
    	request_string += "&include="+tmpS
    end
    # do we need need to sign
    if sign
    	request_string +=  "&sign"
    end
    msg request_string, Logger::DEBUG
    doc = @http.get_xml(request_string)
    unless doc.xpath("//job").empty?
      @job_id = doc.xpath("//job")[0].attribute('id')
      msg "job_id = #{@job_id}"
      return @job_id
    end
    return doc
  when 'status'
    doc = @http.get_xml("archive/status?jobId=#{id}")
    unless doc.xpath("//status").empty?
      return doc.xpath("//status").text
    end
    return doc
  when 'get'
    file_size = @http.download_file("archive/get?jobId=#{id}","#{filepath}/#{id}.zip")
    return "#{filepath}/#{id}.zip file size = #{file_size}"
  end
  return false
end

#backup(command = "run", name = "") ⇒ Object

performs backup operations

* command=run to create a system backup file.
* command=status to view the status of a backup job. If no backup job is running, gets the date of the most recent backup.
* command=get to retrieve the backup file
* name = backup file name


278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/mu/api/muapi.rb', line 278

def backup(command="run", name="")
  case command
  when 'run'
    doc = @http.get_xml("backup/run")
    unless doc.xpath("//job").empty?
      @job_id = doc.xpath("//job")[0].attribute('id')
      msg "job_id = #{@job_id}"
      return @job_id
    end
    return doc
  when 'status'
    doc = @http.get("backup/status")
    return doc
  when 'get'
    file_size = @http.download_file("backup/get","#{name}.dat")
    return "#{name}.dat file size = #{file_size}"
  end
  return false
end

#capture(command = 'run', port = 'a1', id = @job_id) ⇒ Object

packet capture operations

* command=run to start capturing packets on the specified Mu appliance port
* command=status tp view the status of packet capture activity
* command=get to download the pcap file generated during packet capture
* port = the Mu appliance port
* id = job_id for status and get commands


304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/mu/api/muapi.rb', line 304

def capture(command='run', port='a1', id=@job_id)
  case command
  when 'run'
    doc = @http.get_xml("capture/run?port=#{port}")
    unless doc.xpath("//job").empty?
      @job_id = doc.xpath("//job")[0].attribute('id')
      msg "job_id = #{@job_id}"
      return @job_id
    end
    return doc
  when 'status'
    doc = @http.get("capture/status?jobId=#{id}")
    return doc
  when 'get'
    file_size = @http.download_file("capture/get?jobId=#{id}","#{id}.pcap")
    return "#{id}.pcap file size = #{file_size}"
  end
  return false
end

#delete(uuid = @run_uuid) ⇒ Object

deletes the specified test

* uuid = the uuid of the test


138
139
140
141
142
143
144
145
# File 'lib/mu/api/muapi.rb', line 138

def delete(uuid=@run_uuid)
  doc = @http.get_xml("analysis/delete?uuid=#{uuid}")
  unless doc.xpath("//analysis").empty?
    status = doc.xpath("//analysis")[0].attribute('status')
    return status
  end
  return true
end

#export_by_type_and_name(type, name) ⇒ Object

exports a template from the Mu system using the template type and template name

* name = the template name


185
186
187
# File 'lib/mu/api/muapi.rb', line 185

def export_by_type_and_name(type, name)
  return @http.get_xml("templates/export?type=#{type}&name=#{name}")
end

#export_by_uuid(uuid) ⇒ Object

exports a template from the Mu system using the template uuid

* uuid = the template uuid


191
192
193
# File 'lib/mu/api/muapi.rb', line 191

def export_by_uuid(uuid)
  return @http.get_xml("templates/export?uuid=#{uuid}")
end

#get_faults(uuid = @run_uuid) ⇒ Object

returns a list of faults from the specified test

* uuid = the uuid of the test


149
150
151
152
153
154
155
156
# File 'lib/mu/api/muapi.rb', line 149

def get_faults(uuid=@run_uuid)
  doc = @http.get_xml("analysis/status?uuid=#{uuid}")
  unless doc.xpath("//analysis").empty?
    faults = @http.get_xml("analysis/getFaultList?uuid=#{uuid}")
    return faults
  end
  return "error: no analysis with uuid: #{uuid} found"
end

#get_name(uuid = @run_uuid) ⇒ Object

returns the name of an test

* uuid = the uuid of the test


160
161
162
163
164
165
166
# File 'lib/mu/api/muapi.rb', line 160

def get_name(uuid=@run_uuid)
  doc = @http.get_xml("templates/export?uuid=#{uuid}")
  unless doc.xpath("//analysis").empty?
    return doc.xpath("//analysis")[0].attribute('name')
  end
  return
end

#import_templates(template) ⇒ Object

import templates from a file

  • template = the path and name of the template file that contains a single Test template



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/mu/api/muapi.rb', line 87

def import_templates(template)
    # open the file and pull the contents for posting
    begin
        data = File.open(template,'r') {|f| f.read }
    rescue Exception => e
        msg "File read error for #{template}: #{e}"
        return
    end
    
    # post the contents of the file
    response = @http.post("templates/import",data)
    msg response, Logger::DEBUG

    return response
end

#list(type) ⇒ Object

lists the templates on the Mu system using the template type and template name:

* type = the template type, such as 'scenario' or 'monitor'


170
171
172
173
174
175
# File 'lib/mu/api/muapi.rb', line 170

def list(type)
  names = Array.new
  doc = @http.get_xml("templates/list?type=#{type}")
  doc.xpath("//templates/*").each {|a| names << a.attribute('name') }
  return names
end

#list_by_status(status = "") ⇒ Object

lists the statuses of tests

* status = the status type, such as 'running' or 'failed'


20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mu/api/muapi.rb', line 20

def list_by_status(status="")
  uuid_list = Array.new()
  if !status.empty?
    doc = @http.get_xml("analysis/list?status=#{status}")
  else
    doc = @http.get_xml("analysis/list")
  end
  unless doc.xpath("//analysis").empty?
    doc.xpath('//analysis').each { |e| uuid_list << e.attribute('uuid') }
  end
  return uuid_list
end

#pause(uuid = @run_uuid) ⇒ Object

pauses a running test

* uuid = the uuid of the test


116
117
118
119
120
121
122
123
# File 'lib/mu/api/muapi.rb', line 116

def pause(uuid=@run_uuid)
  doc = @http.get_xml("analysis/pause?uuid=#{uuid}")
  unless doc.xpath("//analysis").empty?
    status = doc.xpath("//analysis")[0].attribute('status')
    return status
  end
  return false
end

#resume(uuid = @run_uuid) ⇒ Object

resumes a suspended test

* uuid = the uuid of the test


127
128
129
130
131
132
133
134
# File 'lib/mu/api/muapi.rb', line 127

def resume(uuid=@run_uuid)
  doc = @http.get_xml("analysis/resume?uuid=#{uuid}")
  unless doc.xpath("//analysis").empty?
    status = doc.xpath("//analysis")[0].attribute('status')
    return status
  end
  return false
end

#run(uuid, rename = "") ⇒ Object

starts a test

* uuid = the uuid of the test


47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/mu/api/muapi.rb', line 47

def run(uuid, rename="")
  req = "analysis/run?uuid=#{uuid}"
  unless rename.empty?
    req = "#{req}&rename=#{rename}"
  end
  doc = @http.get_xml(req)
  unless doc.xpath("//analysis").empty?
    @run_uuid = doc.xpath("//analysis")[0].attribute('uuid')
    return @run_uuid
  end
  return @run_uuid
end

#run_from_template(template, rename = "") ⇒ Object

starts a test from a template file

* template = the path and name of the template file that contains a single Test template


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/mu/api/muapi.rb', line 62

def run_from_template(template, rename="")
	response = import_templates(template)
	msg response, Logger::DEBUG
	doc = make_xml(response)
	uuid = ""
	unless doc.xpath("//uuid").empty?
    node = doc.at_xpath("//uuid")
    uuid = node.content()
  end
  msg uuid, Logger::DEBUG
  req = "analysis/run?uuid=#{uuid}"
  unless rename.empty?
    req = "#{req}&rename=#{rename}"
  end
  msg req, Logger::DEBUG
  doc = @http.get_xml(req)
  unless doc.xpath("//analysis").empty?
    @run_uuid = doc.xpath("//analysis")[0].attribute('uuid')
    return @run_uuid
  end
  return @run_uuid
end

#status(uuid = @run_uuid) ⇒ Object

returns the status of a test

* uuid = the uuid of the test


35
36
37
38
39
40
41
42
43
# File 'lib/mu/api/muapi.rb', line 35

def status(uuid=@run_uuid)
  doc = @http.get_xml("analysis/status?uuid=#{uuid}")
  return nil if doc.nil?
  unless doc.xpath("//analysis").empty?
    status = doc.xpath("//status")[0].text
    return status
  end
  return doc
end

#stop(uuid = @run_uuid) ⇒ Object

stops a running test

* uuid = the uuid of the test


105
106
107
108
109
110
111
112
# File 'lib/mu/api/muapi.rb', line 105

def stop(uuid=@run_uuid)
  doc = @http.get_xml("analysis/stop?uuid=#{uuid}")
  unless doc.xpath("//analysis").empty?
    status = doc.xpath("//analysis")[0].attribute('status')
    return status
  end
  return false
end

#typesObject

lists the types of templates on the Mu system



178
179
180
181
# File 'lib/mu/api/muapi.rb', line 178

def types
  doc = @http.get("templates/types?")
  return doc
end