-
Notifications
You must be signed in to change notification settings - Fork 573
/
jira.go
175 lines (155 loc) · 4.4 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
package jira
import (
"context"
"crypto/tls"
"crypto/x509"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
jira "github.com/andygrunwald/go-jira"
"github.com/deepfence/ThreatMapper/deepfence_utils/log"
"github.com/deepfence/ThreatMapper/deepfence_utils/telemetry"
)
func New(ctx context.Context, b []byte) (*Jira, error) {
h := Jira{}
err := json.Unmarshal(b, &h)
if err != nil {
return &h, err
}
return &h, nil
}
func (j Jira) SendNotification(ctx context.Context, message string, extras map[string]interface{}) error {
_, span := telemetry.NewSpan(ctx, "integrations", "jira-send-notification")
defer span.End()
auth := jira.BasicAuthTransport{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: &tls.Config{RootCAs: x509.NewCertPool(), InsecureSkipVerify: true},
},
}
if j.Config.IsAuthToken {
auth.Username = strings.TrimSpace(j.Config.Username)
auth.Password = strings.TrimSpace(j.Config.APIToken)
} else {
auth.Username = strings.TrimSpace(j.Config.Username)
auth.Password = strings.TrimSpace(j.Config.Password)
}
client, err := jira.NewClient(auth.Client(), strings.TrimSpace(j.Config.JiraSiteURL))
if err != nil {
log.Error().Msgf(err.Error())
span.EndWithErr(err)
return err
}
extraStr := []string{}
for k, v := range extras {
if v != "" {
extraStr = append(extraStr, fmt.Sprintf("%s: %v", k, v))
}
}
i := jira.Issue{
Fields: &jira.IssueFields{
Assignee: &jira.User{
Name: j.Config.JiraAssignee,
},
Description: fmt.Sprintf("Scan Details:\n\n%s", strings.Join(extraStr, "\n")),
Type: jira.IssueType{
Name: j.Config.IssueType,
},
Project: jira.Project{
Key: j.Config.JiraProjectKey,
},
Summary: fmt.Sprintf("Deepfence %v Scan Issues for %s - %s",
extras["scan_type"], extras["node_type"], extras["node_name"]),
},
}
issue, resp, err := client.Issue.Create(&i)
if err != nil {
log.Error().Msgf(err.Error())
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Error().Msgf(err.Error())
}
log.Error().Msgf("jira error reponse: %s", string(body))
span.EndWithErr(err)
return err
}
log.Info().Msgf("jira issue created id %s link %s", issue.ID, issue.Self)
// parse message in case of custom fields
var msgWithCustomFields []map[string]interface{}
if len(j.Config.CustomFields) > 0 {
var msg []map[string]interface{}
err = json.Unmarshal([]byte(message), &msg)
if err != nil {
log.Error().Msgf(err.Error())
return err
}
for _, m := range msg {
customFields := make(map[string]interface{})
for _, f := range j.Config.CustomFields {
if value, ok := m[f]; ok {
customFields[f] = value
}
}
msgWithCustomFields = append(msgWithCustomFields, customFields)
}
finalByte, err := json.MarshalIndent(msgWithCustomFields, "", " ")
if err != nil {
log.Error().Msgf(err.Error())
span.EndWithErr(err)
return err
}
message = string(finalByte)
}
attachment, resp, err := client.Issue.PostAttachment(issue.ID, strings.NewReader(message), "scan-results.json")
if err != nil {
log.Error().Msgf(err.Error())
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Error().Msgf(err.Error())
}
log.Error().Msgf("jira attachment error reponse: %s", string(body))
span.EndWithErr(err)
return err
}
defer resp.Body.Close()
log.Info().Msgf(
"jira issue id %s attchment added %+v",
issue.ID,
func(as *[]jira.Attachment) string {
a := []string{}
for _, i := range *as {
a = append(a, i.Self)
}
return strings.Join(a, ",")
}(attachment),
)
return nil
}
func (j Jira) IsValidCredential(ctx context.Context) (bool, error) {
auth := jira.BasicAuthTransport{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: &tls.Config{RootCAs: x509.NewCertPool(), InsecureSkipVerify: true},
},
}
if j.Config.IsAuthToken {
auth.Username = strings.TrimSpace(j.Config.Username)
auth.Password = strings.TrimSpace(j.Config.APIToken)
} else {
auth.Username = strings.TrimSpace(j.Config.Username)
auth.Password = strings.TrimSpace(j.Config.Password)
}
jiraClient, err := jira.NewClient(auth.Client(), strings.TrimSpace(j.Config.JiraSiteURL))
if err != nil {
log.Error().Msgf(err.Error())
return false, err
}
_, _, err = jiraClient.User.GetSelf()
if err != nil {
log.Error().Msgf(err.Error())
return false, fmt.Errorf("failed to connect to Jira: %v", err)
}
return true, nil
}