-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtask.go
237 lines (195 loc) · 6.11 KB
/
task.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
package thehive
import (
"bytes"
"encoding/json"
"fmt"
"github.com/levigross/grequests"
)
// Stores a hive task
type CaseTask struct {
Title string `json:"title"`
Status string `json:"status"`
Owner string `json:"owner"`
Description string `json:"description"`
Flag bool `json:"flag"`
Raw []byte `json:"-"`
}
// Stores multiple tasks from searches
type CaseTaskMulti struct {
Raw []byte
Detail []CaseTask
}
// Stores task responses
type CaseTaskResponse struct {
Title string `json:"title"`
Status string `json:"status"`
Owner string `json:"owner"`
Description string `json:"description"`
Flag bool `json:"flag"`
CreatedBy string `json:"createdBy"`
Order int `json:"order"`
Id string `json:"id"`
Type string `json:"_type"`
Raw []byte `json:"-"`
}
// Stores multiple task responses from searches
type CaseTaskRespMulti struct {
Raw []byte
Detail []CaseTaskResponse
}
// Should maybe have title etc as well.
type CaseTaskLog struct {
Message string `json:"message"`
File string `json:"-"`
Raw []byte `json:"-"`
}
// Stores multiple casetasklogs
type CaseTaskLogMulti struct {
Raw []byte
Detail []CaseTaskLog
}
// Stores case tasklog responses
type CaseTaskLogResponse struct {
Message string `json:"message"`
Title string `json:"title"`
CreatedBy string `json:"createdBy"`
Order string `json:"order"`
Owner string `json:"owner"`
Flag bool `json:"flag"`
Status string `json:"status"`
Id string `json:"id"`
Type string `json:"_type"`
Attachment FileAttachment `json:"attachment"`
Raw []byte `json:"-"`
}
// Stores multiple tasklog responses
type CaseTaskLogRespMulti struct {
Raw []byte
Detail []CaseTaskLogResponse
}
// Defines case task log creation
// Takes two parameters:
// 1. taskId string
// 2. taskLog CaseTaskLog
// Returns CaseTaskLogresponse struct and response error
func (hive *Hivedata) CreateTaskLog(taskId string, taskLog CaseTaskLog) (*CaseTaskLogResponse, error) {
var url string
var err error
var ret *grequests.Response
var jsondata []byte
jsondata, err = json.Marshal(taskLog)
if err != nil {
return nil, err
}
// If files exist
if taskLog.File != "" {
fd, err := grequests.FileUploadFromDisk(taskLog.File)
if err != nil {
return nil, err
}
// Custom solution because files suck. Couldn't get it to work with hive.Ro for some reason
marshalData, err := json.Marshal(taskLog)
if err != nil {
return nil, err
}
data := map[string]string{"_json": fmt.Sprintf("%s", string(marshalData))}
fd[0].FieldName = "attachment"
requestOptions := &grequests.RequestOptions{
Files: fd,
Data: data,
Headers: map[string]string{
"Authorization": fmt.Sprintf("Bearer %s", hive.Apikey),
},
InsecureSkipVerify: !false,
}
url := fmt.Sprintf("%s/api/case/task/%s/log", hive.Url, taskId)
ret, err = grequests.Post(url, requestOptions)
} else {
hive.Ro.RequestBody = bytes.NewReader(jsondata)
url = fmt.Sprintf("%s/api/case/task/%s/log", hive.Url, taskId)
ret, err = grequests.Post(url, &hive.Ro)
}
if err != nil {
return new(CaseTaskLogResponse), err
}
parsedRet := new(CaseTaskLogResponse)
err = json.Unmarshal(ret.Bytes(), parsedRet)
parsedRet.Raw = ret.Bytes()
if err != nil {
return parsedRet, err
}
return parsedRet, nil
}
// Gets all tasks for a specific case
// Takes one argument
// 1. taskId string
// Returns the casetasks and the response error
func (hive *Hivedata) GetTaskLogs(taskId string) (*CaseTaskLogRespMulti, error) {
// Remove the header as the endpoint doesn't accept application/json..
hive.Ro.Headers = map[string]string{
"Authorization": fmt.Sprintf("Bearer %s", hive.Apikey),
}
urlpath := fmt.Sprintf("/api/case/task/%s/log?range=all", taskId)
url := fmt.Sprintf("%s%s", hive.Url, urlpath)
ret, err := grequests.Get(url, &hive.Ro)
parsedRet := new(CaseTaskLogRespMulti)
parsedRet.Raw = ret.Bytes()
json.Unmarshal(parsedRet.Raw, &parsedRet.Detail)
// Rebuild it
hive.Ro.Headers = map[string]string{
"Content-Type": "application/json",
"Authorization": fmt.Sprintf("Bearer %s", hive.Apikey),
}
return parsedRet, err
}
// Gets all tasks for a specific case
// Takes one argument
// 1. caseId string
// Returns the casetasks and the response error
func (hive *Hivedata) GetCaseTasks(caseId string) (*CaseTaskRespMulti, error) {
urlpath := fmt.Sprintf("/api/case/%s/task/_search?range=all", caseId)
jsonQuery := fmt.Sprintf(`{"_parent": {"_type": "case", "_query": {"id": "%s"}}}`, caseId)
jsondata := []byte(jsonQuery)
hive.Ro.RequestBody = bytes.NewReader(jsondata)
url := fmt.Sprintf("%s%s", hive.Url, urlpath)
ret, err := grequests.Post(url, &hive.Ro)
parsedRet := new(CaseTaskRespMulti)
json.Unmarshal(ret.Bytes(), &parsedRet.Detail)
parsedRet.Raw = ret.Bytes()
return parsedRet, err
}
// Defines creation of a case task within a case
// Takes two parameters:
// 1. caseId string
// 2. casetask CaseTask
// Returns CaseTask struct and response error
func (hive *Hivedata) CreateCaseTask(caseId string, casetask CaseTask) (*CaseTaskResponse, error) {
var url string
var err error
var jsondata []byte
jsondata, err = json.Marshal(casetask)
if err != nil {
return nil, err
}
hive.Ro.RequestBody = bytes.NewReader(jsondata)
url = fmt.Sprintf("%s/api/case/%s/task", hive.Url, caseId)
ret, err := grequests.Post(url, &hive.Ro)
parsedRet := new(CaseTaskResponse)
_ = json.Unmarshal(ret.Bytes(), parsedRet)
parsedRet.Raw = ret.Bytes()
return parsedRet, err
}
// Defines how to get a taskwithin a case
// Takes one parameters:
// 1. taskId string
// Returns CaseTaskResponse struct and response error
func (hive *Hivedata) GetTask(taskId string) (*CaseTaskResponse, error) {
var url, urlpath string
urlpath = fmt.Sprintf("/api/case/task/%s/log", taskId)
url = fmt.Sprintf("%s%s", hive.Url, urlpath)
ret, err := grequests.Get(url, &hive.Ro)
parsedRet := new(CaseTaskResponse)
_ = json.Unmarshal(ret.Bytes(), &parsedRet)
parsedRet.Raw = ret.Bytes()
return parsedRet, err
}