Class: Xray

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/plugin/xray.rb

Instance Method Summary collapse

Constructor Details

#initialize(jpd_url, username, api_key, token, wait_interval, batch_size, pos_file_path, router, tag) ⇒ Xray

Returns a new instance of Xray.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/fluent/plugin/xray.rb', line 7

def initialize(jpd_url, username, api_key, token, wait_interval, batch_size, pos_file_path, router, tag)
  @jpd_url = jpd_url
  @username = username
  @api_key = api_key
  @token = token
  @wait_interval = wait_interval
  @batch_size = batch_size
  @pos_file_path = pos_file_path
  @router = router
  @tag = tag
end

Instance Method Details

#data_normalization(detailResp_json) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/fluent/plugin/xray.rb', line 89

def data_normalization(detailResp_json)
  cve = []
  cvss_v2_list = []
  cvss_v3_list = []
  policy_list = []
  rule_list = []
  impacted_artifact_url_list = []
  if detailResp_json.key?('properties')
    properties = detailResp_json['properties']
    for index in 0..properties.length-1 do
      if properties[index].key?('cve')
        cve.push(properties[index]['cve'])
      end
      if properties[index].key?('cvss_v2')
        cvss_v2_list.push(properties[index]['cvss_v2'])
      end
      if properties[index].key?('cvss_v3')
        cvss_v3_list.push(properties[index]['cvss_v3'])
      end
    end

    detailResp_json["cve"] = cve.sort.reverse[0]
    cvss_v2 = cvss_v2_list.sort.reverse[0]
    cvss_v3 = cvss_v3_list.sort.reverse[0]
    if !cvss_v3.nil?
      cvss = cvss_v3
    elsif !cvss_v2.nil?
      cvss = cvss_v2
    end
    cvss_score = cvss[0..2]
    cvss_version = cvss.split(':')[1][0..2]
    detailResp_json["cvss_score"] = cvss_score
    detailResp_json["cvss_version"] = cvss_version
  end

  if detailResp_json.key?('matched_policies')
    matched_policies = detailResp_json['matched_policies']
    for index in 0..matched_policies.length-1 do
      if matched_policies[index].key?('policy')
        policy_list.push(matched_policies[index]['policy'])
      end
      if matched_policies[index].key?('rule')
        rule_list.push(matched_policies[index]['rule'])
      end
    end
    detailResp_json['policies'] = policy_list
    detailResp_json['rules'] = rule_list
  end

  detailResp_json['impacted_artifacts'].each do |impacted_artifact|
    matchdata = impacted_artifact.match /default\/(?<repo_name>[^\/]*)\/(?<path>.*)/
    if matchdata
      impacted_artifact_url = matchdata['repo_name'] + ":" + matchdata['path'] + " "
      impacted_artifact_url_list.append(impacted_artifact_url)
    else
      impacted_artifact_url_list.append(impacted_artifact)
    end
  end
  detailResp_json['impacted_artifacts_url'] = impacted_artifact_url_list
  return detailResp_json
end

#get_violations_detail(xray_violation_detail_url) ⇒ Object



62
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
# File 'lib/fluent/plugin/xray.rb', line 62

def get_violations_detail(xray_violation_detail_url)
  if !@token.nil? && @token != ''
    response = RestClient::Request.new(
        :method => :get,
        :url => @jpd_url + xray_violation_detail_url[xray_violation_detail_url.index('/xray/'),xray_violation_detail_url.length],
        :headers => { :accept => :json, :content_type => :json, Authorization:'Bearer ' + @token }
    )
  elsif !@api_key.nil? && @api_key != ''
    response = RestClient::Request.new(
        :method => :get,
        :url => @jpd_url + xray_violation_detail_url[xray_violation_detail_url.index('/xray/'),xray_violation_detail_url.length],
        :user => @username,
        :password => @api_key
    )
  end

  response.execute do |response, request, result|
    case response.code
    when 200
      return JSON.parse(response.to_s)
    else
      puts "Validation failed error (cannot reach Artifactory to pull Xray Violation details): #{response.to_json}"
      raise Fluent::ConfigError, "Validation failed error (cannot reach Artifactory to pull Xray Violation details): #{response.to_json}"
    end
  end
end

#process(violation, violations_channel) ⇒ Object



151
152
153
154
155
156
157
158
159
160
# File 'lib/fluent/plugin/xray.rb', line 151

def process(violation, violations_channel)
  pos_file = PositionFile.new(@pos_file_path)
  unless pos_file.processed?(violation)
    violations_channel << violation
  else
    puts "Violation #{violation['issue_id']} is already processed"
  end
  #violations_channel << violation unless pos_file.processed?(violation)
  violations_channel
end

#process_violation_details(xray_violation_detail_url) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/fluent/plugin/xray.rb', line 50

def process_violation_details(xray_violation_detail_url)
  begin
    detailResp_json = data_normalization(get_violations_detail(xray_violation_detail_url))
    time = Fluent::Engine.now
    puts "Emitting normalized Xray Violation #{detailResp_json['issue_id']}"
    @router.emit(@tag, time, detailResp_json)
  rescue => e
    puts "Process Violation details error: #{e}"
    raise Fluent::ConfigError, "Process Violation details error: #{e}"
  end
end

#violation_details(violations_channel) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/fluent/plugin/xray.rb', line 39

def violation_details(violations_channel)
  violations_channel.each do |v|
    Concurrent::Promises.future(v) do |v|
      process_violation_details(v['violation_details_url'])
      pos_file = PositionFile.new(@pos_file_path)
      puts "Adding issue #{v['issue_id']} to position file at #{@pos_file_path}"
      pos_file.write(v)
    end
  end
end

#violations(date_since) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/fluent/plugin/xray.rb', line 19

def violations(date_since)
  violations_channel = Concurrent::Channel.new(capacity: @batch_size)
  page_number = 1
  timer_task = Concurrent::TimerTask.new(execution_interval: @wait_interval, timeout_interval: 30) do
    xray_json = {"filters": { "created_from": date_since }, "pagination": {"order_by": "created","limit": @batch_size ,"offset": page_number } }
    puts "Fetching Xray Violations with #{xray_json} parameters"
    resp = get_violations(xray_json)
    page_violation_count = resp['violations'].length
    puts "Total violations count is #{resp['total_violations']}"
    if resp['total_violations'] > 0
      puts "Number of Violations in page #{page_number} are #{page_violation_count}"
      resp['violations'].each {|v| violations_channel = process(v, violations_channel) }
      page_number += 1 if next_page?(page_violation_count)
    end
  end
  timer_task.execute

  violations_channel
end