Class: Nexpose::Ticket

Inherits:
TicketSummary show all
Defined in:
lib/nexpose/ticket.rb

Defined Under Namespace

Classes: Event

Instance Attribute Summary collapse

Attributes inherited from TicketSummary

#assigned_to, #author, #created_on, #device_id, #id, #name, #priority, #state

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from TicketSummary

parse

Constructor Details

#initialize(name, id = nil) ⇒ Ticket

Returns a new instance of Ticket.



131
132
133
134
135
136
137
# File 'lib/nexpose/ticket.rb', line 131

def initialize(name, id = nil)
  @id, @name = id, name
  @priority = Priority::NORMAL
  @vulnerabilities = []
  @comments = []
  @history = []
end

Instance Attribute Details

#commentsObject

Array of comments about the ticket.



126
127
128
# File 'lib/nexpose/ticket.rb', line 126

def comments
  @comments
end

#historyObject

History of events on this ticket.



129
130
131
# File 'lib/nexpose/ticket.rb', line 129

def history
  @history
end

#vulnerabilitiesObject

List of vulnerabilities (by ID) this ticket addresses.



123
124
125
# File 'lib/nexpose/ticket.rb', line 123

def vulnerabilities
  @vulnerabilities
end

Class Method Details

.load(connection, id) ⇒ Ticket

Load existing ticket data.

Parameters:

  • connection (Connection)

    Connection to console where ticket exists.

  • id (Fixnum)

    Ticket ID of an existing ticket.

Returns:

  • (Ticket)

    Ticket populated with current state.



167
168
169
170
171
172
173
174
175
# File 'lib/nexpose/ticket.rb', line 167

def self.load(connection, id)
  # TODO: Load multiple tickets in a single request, as supported by API.
  xml = connection.make_xml('TicketDetailsRequest')
  xml.add_element('Ticket', { 'id' => id })
  response = connection.execute(xml, '1.2')
  response.res.elements.each('//TicketInfo') do |info|
    return parse_details(info)
  end
end

.parse_details(xml) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/nexpose/ticket.rb', line 203

def self.parse_details(xml)
  ticket = parse(xml)

  xml.elements.each('Vulnerabilities/Vulnerability') do |vuln|
    ticket.vulnerabilities << vuln.attributes['id']
  end

  xml.elements.each('TicketHistory/Entry') do |entry|
    ticket.history << Event.parse(entry)
  end

  ticket.comments = ticket.history.select { |h| h.description == 'Added comment' }.map { |e| e.comment }

  ticket
end

Instance Method Details

#delete(connection) ⇒ Boolean

Delete this ticket from the system.

Parameters:

  • connection (Connection)

    Connection to console where ticket exists.

Returns:

  • (Boolean)

    Whether the ticket was successfully delete.



157
158
159
# File 'lib/nexpose/ticket.rb', line 157

def delete(connection)
  connection.delete_ticket(@id)
end

#save(connection) ⇒ Fixnum

Save this ticket to a Nexpose console.

Parameters:

  • connection (Connection)

    Connection to console where ticket exists.

Returns:

  • (Fixnum)

    Unique ticket ID assigned to this ticket.



144
145
146
147
148
149
150
# File 'lib/nexpose/ticket.rb', line 144

def save(connection)
  xml = connection.make_xml('TicketCreateRequest')
  xml.add_element(to_xml)

  response = connection.execute(xml, '1.2')
  @id = response.attributes['id'].to_i if response.success
end

#to_xmlObject



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/nexpose/ticket.rb', line 177

def to_xml
  xml = REXML::Element.new('TicketCreate')
  xml.add_attributes({ 'name' => @name,
                       'priority' => @priority,
                       'device-id' => @device_id,
                       'assigned-to' => @assigned_to })

  vuln_xml = REXML::Element.new('Vulnerabilities')
  @vulnerabilities.each do |vuln_id|
    vuln_xml.add_element('Vulnerability', { 'id' => vuln_id.downcase })
  end
  xml.add_element(vuln_xml)

  unless @comments.empty?
    comments_xml = REXML::Element.new('Comments')
    @comments.each do |comment|
      comment_xml = REXML::Element.new('Comment')
      comment_xml.add_text(comment)
      comments_xml.add_element(comment_xml)
    end
    xml.add_element(comments_xml)
  end

  xml
end