blob: 14acc3c96bb6ae0789ec2e772c2287464254dbc1 [file] [log] [blame]
Ivan Udovichenko894fd8a2020-04-13 17:24:50 +03001#!groovy
2
Ivan Udovichenko314a0732020-04-13 22:47:26 +03003package com.mirantis.mk
4
Ivan Udovichenko894fd8a2020-04-13 17:24:50 +03005import groovy.json.JsonSlurper
6
7def callREST (String uri, String auth,
8 String method = 'GET', String message = null) {
9 String authEnc = auth.bytes.encodeBase64()
10 def req = new URL(uri).openConnection()
11 req.setRequestMethod(method)
12 req.setRequestProperty('Content-Type', 'application/json')
13 req.setRequestProperty('Authorization', "Basic ${authEnc}")
14 if (message) {
15 req.setDoOutput(true)
16 req.getOutputStream().write(message.getBytes('UTF-8'))
17 }
18 Integer responseCode = req.getResponseCode()
19 String responseText = ''
20 if (responseCode == 200 || responseCode == 201) {
21 responseText = req.getInputStream().getText()
22 }
23 req = null
24 return [ 'responseCode': responseCode, 'responseText': responseText ]
25}
26
27def getTeam (String image = '') {
28 def team_assignee = ''
29 switch(image) {
30 case ~/^(tungsten|tungsten-operator)\/.*$/:
31 team_assignee = 'OpenContrail'
32 break
33 case ~/^bm\/.*$/:
34 team_assignee = 'BM/OS (KaaS BM)'
35 break
36 case ~/^openstack\/.*$/:
37 team_assignee = 'OpenStack hardening'
38 break
39 case ~/^stacklight\/.*$/:
40 team_assignee = 'Stacklight LMA'
41 break
42 case ~/^ceph\/.*$/:
43 team_assignee = 'Storage'
44 break
45 case ~/^iam\/.*$/:
46 team_assignee = 'KaaS'
47 break
48 case ~/^lcm\/.*$/:
49 team_assignee = 'Kubernetes'
50 break
51 default:
52 team_assignee = 'Release Engineering'
53 break
54 }
55
56 return team_assignee
57}
58
59def updateDictionary (String jira_issue_key, Map dict, String uri, String auth, String jira_user_id) {
60 def response = callREST("${uri}/${jira_issue_key}", auth)
61 if ( response['responseCode'] == 200 ) {
62 def issueJSON = new JsonSlurper().parseText(response["responseText"])
63 if (issueJSON.containsKey('fields')) {
64 if (!dict.containsKey(jira_issue_key)) {
65 dict[jira_issue_key] = [
66 summary : '',
67 description: '',
68 comments: []
69 ]
70 }
71 if (issueJSON['fields'].containsKey('summary')){
72 dict[jira_issue_key].summary = issueJSON['fields']['summary']
73 }
74 if (issueJSON['fields'].containsKey('description')) {
75 dict[jira_issue_key].description = issueJSON['fields']['description']
76 }
77 if (issueJSON['fields'].containsKey('comment') && issueJSON['fields']['comment']['comments']) {
78 issueJSON['fields']['comment']['comments'].each {
79 if (it.containsKey('author') && it['author'].containsKey('accountId') && it['author']['accountId'] == jira_user_id) {
80 dict[jira_issue_key]['comments'].add(it['body'])
81 }
82 }
83 }
84 }
85 }
86 return dict
87}
88
89def cacheLookUp(Map dict, String image_short_name, String image_full_name = '', String cve_id = '' ) {
90 def found_key = ['','']
91 if (!found_key[0] && dict && image_short_name) {
92 dict.each { issue_key_name ->
93 if (!found_key[0]) {
94 def s = dict[issue_key_name.key]['summary'] =~ /\b${image_short_name}\b/
95 if (s) {
96 if (image_full_name) {
97 def d = dict[issue_key_name.key]['description'] =~ /(?m)\b${image_full_name}\b/
Ivan Udovichenko894fd8a2020-04-13 17:24:50 +030098 if (d) {
99 found_key = [issue_key_name.key,'']
100 } else {
101 if (dict[issue_key_name.key]['comments']) {
102 def comment_match = false
103 dict[issue_key_name.key]['comments'].each{ comment ->
104 if (!comment_match) {
105 def c = comment =~ /(?m)\b${image_full_name}\b/
106 if (c) {
107 comment_match = true
108 }
109 }
110 }
111 if (!comment_match) {
112 found_key = [issue_key_name.key,'na']
113 } else {
114 found_key = [issue_key_name.key,'']
115 }
116 } else {
117 found_key = [issue_key_name.key,'na']
118 }
119 }
120 }
121 }
122 }
123 }
124 }
125 return found_key
126}
127
Ivan Udovichenko6c870b72020-04-14 11:31:51 +0300128def reportJiraTickets(String reportFileContents, String jiraCredentialsID, String jiraUserID) {
Ivan Udovichenko894fd8a2020-04-13 17:24:50 +0300129
130 def dict = [:]
131
Ivan Udovichenko6c870b72020-04-14 11:31:51 +0300132 def common = new com.mirantis.mk.Common()
Ivan Udovichenko894fd8a2020-04-13 17:24:50 +0300133 def cred = common.getCredentialsById(jiraCredentialsID)
134 def auth = "${cred.username}:${cred.password}"
135 def uri = "${cred.description}/rest/api/2/issue"
136
137 def search_api_url = "${cred.description}/rest/api/2/search"
138
139 def search_json = """
140{
141 "jql": "reporter = ${jiraUserID} and (labels = cve and labels = security) and (status = 'To Do' or status = 'For Triage' or status = Open or status = 'In Progress')"
142}
143"""
144
145 def response = callREST("${search_api_url}", auth, 'POST', search_json)
146
147 def InputJSON = new JsonSlurper().parseText(response["responseText"])
148
149 InputJSON['issues'].each {
150 dict[it['key']] = [
151 summary : '',
152 description: '',
153 comments: []
154 ]
155 }
156
157 InputJSON['issues'].each { jira_issue ->
158 dict = updateDictionary(jira_issue['key'], dict, uri, auth, jiraUserID)
Ivan Udovichenko894fd8a2020-04-13 17:24:50 +0300159 }
160
Ivan Udovichenko6c870b72020-04-14 11:31:51 +0300161 def reportJSON = new JsonSlurper().parseText(reportFileContents)
Ivan Udovichenko894fd8a2020-04-13 17:24:50 +0300162 def imageDict = [:]
163 def cves = []
164 reportJSON.each{
165 image ->
166 if ("${image.value}".contains('issues')) { return }
167 image.value.each{
168 pkg ->
169 cves = []
170 pkg.value.each{
171 cve ->
172 if (cve[2] && (cve[1].contains('High') || cve[1].contains('Critical'))) {
173 if (!imageDict.containsKey("${image.key}")) {
174 imageDict.put(image.key, [:])
175 }
176 if (!imageDict[image.key].containsKey(pkg.key)) {
177 imageDict[image.key].put(pkg.key, [])
178 }
179 cves.add("${cve[0]} (${cve[2]})")
180 }
181 }
182 if (cves) {
183 imageDict[image.key] = [
184 "${pkg.key}": cves
185 ]
186 }
187 }
188 }
189
190 def jira_summary = ''
191 def jira_description = ''
192 imageDict.each{
193 image ->
194 def image_key = image.key.replaceAll(/(^[a-z0-9-.]+.mirantis.(net|com)\/|:.*$)/, '')
195 // Temporary exclude tungsten images
196 if (image_key.startsWith('tungsten/') || image_key.startsWith('tungsten-operator/')) { return }
197 jira_summary = "[${image_key}] Found CVEs in Docker image"
198 jira_description = "{noformat}${image.key}\\n"
199 image.value.each{
200 pkg ->
201 jira_description += " * ${pkg.key}\\n"
202 pkg.value.each{
203 cve ->
204 jira_description += " - ${cve}\\n"
205 }
206 }
207 jira_description += "{noformat}"
208
209 def team_assignee = getTeam(image_key)
210
211 def post_issue_json = """
212{
213 "fields": {
214 "project": {
215 "key": "PRODX"
216 },
217 "summary": "${jira_summary}",
218 "description": "${jira_description}",
219 "issuetype": {
220 "name": "Bug"
221 },
222 "labels": [
223 "security",
224 "cve"
225 ],
226 "customfield_19000": {
227 "value": "${team_assignee}"
228 },
229 "versions": [
230 {
231 "name": "Backlog"
232 }
233 ]
234 }
235}
236"""
237 def post_comment_json = """
238{
239 "body": "${jira_description}"
240}
241"""
242 def jira_key = cacheLookUp(dict, image_key, image.key)
243 if (jira_key[0] && jira_key[1] == 'na') {
244 def post_comment_response = callREST("${uri}/${jira_key[0]}/comment", auth, 'POST', post_comment_json)
245 if ( post_comment_response['responseCode'] == 201 ) {
246 def issueCommentJSON = new JsonSlurper().parseText(post_comment_response["responseText"])
247 } else {
248 print "\nComment to ${jira_key[0]} Jira issue was not posted"
249 }
250 } else if (!jira_key[0]) {
251 def post_issue_response = callREST("${uri}/", auth, 'POST', post_issue_json)
252 if (post_issue_response['responseCode'] == 201) {
253 def issueJSON = new JsonSlurper().parseText(post_issue_response["responseText"])
254 dict = updateDictionary(issueJSON['key'], dict, uri, auth, jiraUserID)
255 } else {
256 print "\n${image.key} CVE issues were not published\n"
257 }
258 } else {
259 print "\n\nNothing to process for for ${image_key} and ${image.key}"
260 }
Ivan Udovichenko894fd8a2020-04-13 17:24:50 +0300261 }
Ivan Udovichenko314a0732020-04-13 22:47:26 +0300262}