-
Notifications
You must be signed in to change notification settings - Fork 7
/
jira.go
238 lines (200 loc) · 5.43 KB
/
jira.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
88
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
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
202
203
204
205
206
207
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
package tracker
import (
"bytes"
"crypto/tls"
"crypto/x509"
"encoding/json"
"errors"
"fmt"
"github.com/blackjack/syslog"
"github.com/mikkolehtisalo/cvesync/nvd"
"io/ioutil"
"net/http"
"strconv"
"strings"
"text/template"
)
type Jira struct {
BaseURL string
CAFile string
Username string
Password string
Project string
Issuetype string
TemplateFile string
HighPriority string
MediumPriority string
LowPriority string
Template *template.Template
}
func (j *Jira) Init() {
// Loading Jira related settings
b, err := ioutil.ReadFile("/opt/cvesync/etc/jira.json")
if err != nil {
syslog.Errf("Unable to read Jira settings file: %v", err)
panic(err)
}
err = json.Unmarshal(b, &j)
if err != nil {
syslog.Errf("Unable to unmarshal Jira settings json: %v", err)
panic(err)
}
funcMap := template.FuncMap{
"escape_text": escape_text,
}
j.Template, err = template.New("jira.templ").Funcs(funcMap).ParseFiles(j.TemplateFile)
if err != nil {
syslog.Errf("Unable to parse Jira template file: %v", err)
panic(err)
}
}
// A few CVEs contain characters that break Jira's text formatting
func escape_text(s string) string {
result := strings.Replace(s, "[", "\\[", -1)
result = strings.Replace(result, "]", "\\]", -1)
result = strings.Replace(result, "~", "\\~", -1)
return result
}
func (j Jira) build_description(e nvd.Entry) string {
var result bytes.Buffer
err := j.Template.Execute(&result, e)
if err != nil {
syslog.Errf("Unable to execute Jira template file: %v", err)
panic(err)
}
return result.String()
}
// Populates struct for JSON request
func (j Jira) build_ticket(e nvd.Entry) (JiraTicket, error) {
ticket := JiraTicket{}
summary := fmt.Sprintf("%v: %v", e.Id, e.Summary)
// Effectively cut the summary at 200 characters (Jira supports <255 by default)
if len(summary) > 200 {
summary = summary[:200] + "..."
}
ticket.Fields.Summary = summary
ticket.Fields.Issuetype.Id = j.Issuetype
ticket.Fields.Project.Id = j.Project
ticket.Fields.Description = j.build_description(e)
// Priority
score_float64, err := strconv.ParseFloat(e.CVSS.Score, 64)
if err != nil {
// Some CVEs have no CVSS score set yet, this is ok!
// If err, then score_float64 to 4.0 => medium
score_float64 = float64(4.0)
}
ticket.Fields.Priority.Id = j.LowPriority
if score_float64 >= 4.0 {
ticket.Fields.Priority.Id = j.MediumPriority
}
if score_float64 >= 7.0 {
ticket.Fields.Priority.Id = j.HighPriority
}
return ticket, nil
}
// Add new ticket, return the Jira's ticket id
func (j Jira) Add(e nvd.Entry) (string, error) {
ticket, err := j.build_ticket(e)
if err != nil {
return "", err
}
json, err := json.Marshal(ticket)
if err != nil {
return "", err
}
id, err := jira_request("POST", j.BaseURL+"/rest/api/2/issue", j.CAFile, j.Username, j.Password, string(json))
return id, err
}
// Modify existing ticket, ticketid is Jira's ticket id
func (j Jira) Update(e nvd.Entry, ticketid string) error {
ticket, err := j.build_ticket(e)
if err != nil {
return err
}
json, err := json.Marshal(ticket)
if err != nil {
return err
}
_, err = jira_request("PUT", j.BaseURL+"/rest/api/2/issue/"+ticketid, j.CAFile, j.Username, j.Password, string(json))
return err
}
func jira_request(reqtype string, path string, cafile string, username string, password string, jsonstr string) (string, error) {
var client *http.Client
// If https, add CA certificate checking
if strings.HasPrefix(path, "https://") {
capool := x509.NewCertPool()
cacert, err := ioutil.ReadFile(cafile)
if err != nil {
syslog.Errf("Unable to read CA file: %v", err)
return "", err
}
capool.AppendCertsFromPEM(cacert)
// Check server certificate
tr := &http.Transport{
TLSClientConfig: &tls.Config{RootCAs: capool},
}
client = &http.Client{Transport: tr}
} else {
client = &http.Client{}
}
// Build request..
jsonreader := strings.NewReader(jsonstr)
req, err := http.NewRequest(reqtype, path, jsonreader)
if err != nil {
return "", err
}
// Without application/json Jira returns 415
req.Header.Set("Content-Type", "application/json")
// Basic Authentication
req.SetBasicAuth(username, password)
// Request!
resp, err := client.Do(req)
if err != nil {
return "", err
}
// If not successful, return with statuscode
if (resp.StatusCode < 200) || (resp.StatusCode > 299) {
return "", errors.New(fmt.Sprintf("Response contained %v", resp.StatusCode))
}
ticketid := ""
// Only POST returns something meaningful
if reqtype == "POST" {
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
jira_response := Jira_Response{}
err = json.Unmarshal(body, &jira_response)
if err != nil {
return "", err
}
ticketid = jira_response.Id
}
return ticketid, nil
}
type JiraTicket struct {
Fields Field `json:"fields"`
}
type Field struct {
Project Project_field `json:"project"`
Summary string `json:"summary"`
Issuetype Issuetype_field `json:"issuetype"`
Priority Priority_field `json:"priority"`
Description string `json:"description"`
}
type Project_field struct {
Id string `json:"id"`
}
type Issuetype_field struct {
Id string `json:"id"`
}
type Priority_field struct {
Id string `json:"id"`
}
// Jira responds with basic information about the created/modified ticket
type Jira_Response struct {
Id string `json:"id"`
Key string `json:"key"`
Self string `json:"self"`
}