Class: JiraHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/nexpose_ticketing/helpers/jira_helper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(jira_data, options) ⇒ JiraHelper

Returns a new instance of JiraHelper.



12
13
14
15
# File 'lib/nexpose_ticketing/helpers/jira_helper.rb', line 12

def initialize(jira_data, options)
  @jira_data = jira_data
  @options = options
end

Instance Attribute Details

#jira_dataObject

Returns the value of attribute jira_data.



11
12
13
# File 'lib/nexpose_ticketing/helpers/jira_helper.rb', line 11

def jira_data
  @jira_data
end

#optionsObject

Returns the value of attribute options.



11
12
13
# File 'lib/nexpose_ticketing/helpers/jira_helper.rb', line 11

def options
  @options
end

Instance Method Details

#create_ticket(tickets) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/nexpose_ticketing/helpers/jira_helper.rb', line 17

def create_ticket(tickets)
  fail 'Ticket(s) cannot be empty.' if tickets.empty? || tickets.nil?
  tickets.each do |ticket|
    headers = { 'Content-Type' => 'application/json',
                'Accept' => 'application/json' }
    url = URI.parse("#{@jira_data[:jira_url]}")
    req = Net::HTTP::Post.new(@jira_data[:jira_url], headers)
    req.basic_auth @jira_data[:username], @jira_data[:password]
    req.body = ticket
    resp = Net::HTTP.new(url.host, url.port)
    # Enable this line for debugging the https call.
    # resp.set_debug_output $stderr
    resp.use_ssl = true if @jira_data[:jira_url].to_s.start_with?('https')
    resp.verify_mode = OpenSSL::SSL::VERIFY_NONE
    resp.start { |http| http.request(req) }
  end
end

#prepare_tickets(vulnerability_list) ⇒ Object

Prepares tickets from the CSV.



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/nexpose_ticketing/helpers/jira_helper.rb', line 36

def prepare_tickets(vulnerability_list)
  @ticket = Hash.new(-1)
  case @options[:ticket_mode]
  # 'D' Default IP *-* Vulnerability
  when 'D'
    prepare_tickets_default(vulnerability_list)
  # 'I' IP address -* Vulnerability
  when 'I'
    prepare_tickets_by_ip(vulnerability_list)
  else
      fail 'No ticketing mode selected.'
  end
end

#prepare_tickets_by_ip(vulnerability_list) ⇒ Object

Prepares and creates tickets in IP mode.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/nexpose_ticketing/helpers/jira_helper.rb', line 72

def prepare_tickets_by_ip(vulnerability_list)
  tickets = []
  current_ip = -1
  CSV.parse(vulnerability_list.chomp, headers: :first_row)  do |row|
    if current_ip == -1
      current_ip = row['ip_address']
      @ticket = {
          'fields' => {
              'project' => {
                  'key' => "#{@jira_data[:project]}" },
              'summary' => "#{row['ip_address']} => Vulnerabilities",
              'description' => '',
              'issuetype' => {
                  'name' => 'Task' }
          }
      }
    end
    if current_ip == row['ip_address']
      @ticket['fields']['description'] += "\n ==============================\n
        #{row['summary']} \n ==============================\n
        \n #{row['fix']}\n\n #{row['url']}"
    end
    unless current_ip == row['ip_address']
      @ticket = @ticket.to_json
      tickets.push(@ticket)
      current_ip = -1
      redo
    end
  end
  tickets.push(@ticket.to_json) unless @ticket.nil?
  tickets
end

#prepare_tickets_default(vulnerability_list) ⇒ Object

Prepares and creates tickets in default mode.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/nexpose_ticketing/helpers/jira_helper.rb', line 51

def prepare_tickets_default(vulnerability_list)
  tickets = []
  CSV.parse(vulnerability_list.chomp, headers: :first_row)  do |row|
    # JiraHelper doesn't like new line characters in their summaries.
    summary = row['summary'].gsub(/\n/, ' ')
    ticket = {
        'fields' => {
            'project' => {
                'key' => "#{@jira_data[:project]}" },
            'summary' => "#{row['ip_address']} => #{summary}",
            'description' => "#{row['fix']} \n\n #{row['url']}",
            'issuetype' => {
                'name' => 'Task' }
        }
    }.to_json
    tickets.push(ticket)
  end
  tickets
end