-
Notifications
You must be signed in to change notification settings - Fork 14
/
incident.go
217 lines (200 loc) · 7.49 KB
/
incident.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
package client
import (
"bytes"
"encoding/json"
"fmt"
"io"
"mime/multipart"
"time"
)
// Target ...
type Label struct {
Value string `json:"value"`
Type string `json:"type"`
}
// Attachment ...
type Attachment struct {
Name string `json:"name"`
Type string `json:"type"`
Path string `json:"path"`
Description string `json:"description"`
}
// CustomFields ...
type CustomFields map[string]interface{}
// Incident details.
// An incident can be opened by us algorithmically or arrive from an external source like SIEM.
// If you add fields, make sure to add them to the mapping as well
type Incident struct {
// ID of the incident
ID string `json:"id"`
// Version ...
Version int64 `json:"version"`
// Modified timestamp
Modified time.Time `json:"modified"`
// Type of the incident
Type string `json:"type"`
// Name of the incident
Name string `json:"name"`
// Status ...
Status int `json:"status"`
// Reason for the resolve
Reason string `json:"reason"`
// When was this created
Created time.Time `json:"created"`
// When this incident has really occurred
Occurred time.Time `json:"occurred"`
// When was this closed
Closed time.Time `json:"closed"`
// The severity of the incident
Level int `json:"severity"`
// Investigation that was opened as a result of the incoming event
Investigation string `json:"investigationId"`
// The targets involved
Labels []Label `json:"labels"`
// Attachments
Attachments []Attachment `json:"attachment"`
// The details of the incident - reason, etc.
Details string `json:"details"`
//Duration incident was open
OpenDuration int64 `json:"openDuration"`
//The user ID that closed this investigation
ClosingUserID string `json:"closingUserId"`
// The user that activated this investigation
ActivatingUserID string `json:"activatingingUserId,omitempty"`
//The user who owns this incident
OwnerID string `json:"owner"`
// When was this activated
Activated time.Time `json:"activated,omitempty"`
// The reason for archiving the incident
ArchiveReason string `json:"archiveReason"`
// The associated playbook for this incident
PlaybookID string `json:"playbookId"`
// When was this activated
DueDate time.Time `json:"dueDate,omitempty"`
// Should we automagically create the investigation
CreateInvestigation bool `json:"createInvestigation"`
// This field must have empty json key
CustomFields `json:""`
}
type idVersion struct {
ID string `json:"id"`
Version int64 `json:"version"`
}
type updateSeverity struct {
idVersion
Severity int `json:"severity"`
}
// Order struct holds a sort field and the direction of sorting
type Order struct {
Field string `json:"field"`
Asc bool `json:"asc"`
}
// IncidentFilter allows for very simple filtering.
type IncidentFilter struct {
Page int `json:"page,omitempty"`
Size int `json:"size,omitempty"`
Sort []Order `json:"sort,omitempty"`
ID []string `json:"id,omitempty"` // list of IDs to filter by
Type []string `json:"type,omitempty"` // list of sources
Name []string `json:"name,omitempty"` // list of sources
Status []int `json:"status,omitempty"` // list of statuses we are interested in
NotStatus []int `json:"notStatus,omitempty"` // list of statuses we are not interested in
Reason []string `json:"reason,omitempty"` // The reason for resolve
FromDate time.Time `json:"fromDate,omitempty"` // filter from date
ToDate time.Time `json:"toDate,omitempty"` // filter to date
FromClosedDate time.Time `json:"fromClosedDate,omitempty"` // filter from date
ToClosedDate time.Time `json:"toClosedDate,omitempty"` // filter to date
FromActivatedDate time.Time `json:"fromActivatedDate,omitempty"` // filter from date
ToActivatedDate time.Time `json:"toActivatedDate,omitempty"` // filter to date
FromDueDate time.Time `json:"fromDueDate,omitempty"` // filter from date
ToDueDate time.Time `json:"toDueDate,omitempty"` // filter to date
Level []int `json:"level,omitempty"` // filter based on severity
Investigation []string `json:"investigation,omitempty"` // list of investigations we would like to filter by
Systems []string `json:"systems,omitempty"` // list of systems affected
Files []string `json:"files,omitempty"` // list of files affected
Urls []string `json:"urls,omitempty"` // list of urls affected
Users []string `json:"users,omitempty"` // list of users affected
Details string `json:"details,omitempty"` // details for the query
AndOp bool `json:"andOp,omitempty"` // should all fields match or at least one
Query string `json:"query,omitempty"` // free query string
TotalOnly bool `json:"totalOnly"` // should return only total with no body
}
type SearchIncidentsData struct {
Filter IncidentFilter `json:"filter"`
FilterByUser bool `json:"userFilter"`
FetchInsight bool `json:"fetchInsights"`
}
// IncidentSearchResponse is the response from the search
type IncidentSearchResponse struct {
Total int64 `json:"total"`
Data []Incident `json:"data"`
}
// CreateIncident in Demisto
func (c *Client) CreateIncident(inc *Incident, account string) (*Incident, error) {
data, err := json.Marshal(inc)
if err != nil {
return nil, err
}
res := &Incident{}
url := "incident"
if account != "" {
url = fmt.Sprintf("acc_%s/incident", account)
}
err = c.req("POST", url, "", bytes.NewBuffer(data), res)
return res, err
}
// Incidents search based on provided filter
func (c *Client) Incidents(filter *IncidentFilter) (*IncidentSearchResponse, error) {
data, err := json.Marshal(&SearchIncidentsData{Filter: *filter, FilterByUser: false, FetchInsight: false})
if err != nil {
return nil, err
}
res := &IncidentSearchResponse{}
err = c.req("POST", "incidents/search", "", bytes.NewBuffer(data), res)
return res, err
}
type investigation struct {
idVersion
Investigation *Investigation `json:"investigation"`
}
// IncidentAddAttachment adds an attachment to a given incident
func (c *Client) IncidentAddAttachment(inc *Incident, file io.Reader, name, comment string, account string) (*Incident, error) {
b := &bytes.Buffer{}
writer := multipart.NewWriter(b)
part, err := writer.CreateFormFile("file", name)
if err != nil {
return nil, err
}
_, err = io.Copy(part, file)
if err != nil {
return nil, err
}
if comment != "" {
part, err := writer.CreateFormField("comment")
if err != nil {
return nil, err
}
_, err = part.Write([]byte(comment))
if err != nil {
return nil, err
}
}
writer.Close()
res := &Incident{}
url := "incident/upload/" + inc.ID
if account != "" {
url = fmt.Sprintf("acc_%s/%s", account, url)
}
err = c.req("POST", url, writer.FormDataContentType(), b, res)
return res, err
}
// Investigate a given incident, returns ID and version of invetigation created.
func (c *Client) Investigate(incidentID string, incidentVersion int64) (*Investigation, error) {
data, err := json.Marshal(&idVersion{ID: incidentID, Version: incidentVersion})
if err != nil {
return nil, err
}
res := &investigation{}
err = c.req("POST", "incident/investigate", "", bytes.NewBuffer(data), res)
return res.Investigation, err
}