-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.go
190 lines (172 loc) · 6.16 KB
/
template.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
/*
thehive5 implements functionality to interact with the most recent version of thehive.
https://www.strangebee.com/thehive/
*/
package thehive5
import (
"encoding/json"
"net/url"
"time"
)
// A CaseTemplate contains the mapping for the thehive5 api
type CaseTemplate struct {
Name string `json:"name"`
DisplayName string `json:"displayName,omitempty"`
TitlePrefix string `json:"titlePrefix,omitempty"`
Description string `json:"description,omitempty"`
Severity *Severity `json:"severity,omitempty"`
Tags *[]string `json:"tags,omitempty"`
Flag *bool `json:"flag,omitempty"`
Tlp string `json:"tlp,omitempty"`
Pap string `json:"pap,omitempty"`
Summary string `json:"summary,omitempty"`
CustomFields *[]CustomField `json:"customFields"`
}
// Marshalling the alert requests
func (c *CaseTemplate) MarshalJSON() ([]byte, error) {
type Alias CaseTemplate
var (
tlpInt int
papInt int
)
if len(c.Tlp) != 0 {
var tlp Tlp
err := tlp.FromString(c.Tlp)
if err != nil {
return nil, err
}
tlpInt = int(tlp)
}
if len(c.Pap) != 0 {
var pap Pap
err := pap.FromString(c.Pap)
if err != nil {
return nil, err
}
papInt = int(pap)
}
return json.Marshal(&struct {
Tlp int `json:"tlp,omitempty"`
Pap int `json:"pap,omitempty"`
*Alias
}{
Tlp: tlpInt,
Pap: papInt,
Alias: (*Alias)(c),
})
}
// CaseTemplateResponse contain the response of thehive5 templates endpoint
type CaseTemplateResponse struct {
Id string `json:"_id"`
Type string `json:"_type"`
CreatedBy string `json:"_createdBy"`
UpdatedBy string `json:"_updatedBy,omitempty"`
CreatedAt time.Time `json:"_createdAt"`
UpdatedAt time.Time `json:"_updatedAt,omitempty"`
Name string `json:"name"`
DisplayName string `json:"displayName"`
TitlePrefix string `json:"titlePrefix,omitempty"`
Description string `json:"description,omitempty"`
Severity Severity `json:"severity,omitempty"`
SeverityLabel string `json:"severityLabel,omitempty"`
Tags []string `json:"tags,omitempty"`
Flag bool `json:"flag"`
Tlp Tlp `json:"tlp,omitempty"`
TlpLabel string `json:"tlpLabel,omitempty"`
Pap Pap `json:"pap,omitempty"`
PapLabel string `json:"papLabel,omitempty"`
Summary string `json:"summary,omitempty"`
CustomFields []CustomField `json:"customFields,omitempty"`
Tasks []CaseTask `json:"tasks,omitempty"`
ExtraData struct{} `json:"extraData,omitempty"`
}
// CaseTemplateResponse contain the response of thehive5 templates endpoint
type shadowCaseTemplateResponse struct {
Id string `json:"_id"`
Type string `json:"_type"`
CreatedBy string `json:"_createdBy"`
UpdatedBy string `json:"_updatedBy,omitempty"`
CreatedAt int64 `json:"_createdAt"`
UpdatedAt int64 `json:"_updatedAt,omitempty"`
Name string `json:"name"`
DisplayName string `json:"displayName"`
TitlePrefix string `json:"titlePrefix,omitempty"`
Description string `json:"description,omitempty"`
Severity Severity `json:"severity,omitempty"`
SeverityLabel string `json:"severityLabel,omitempty"`
Tags []string `json:"tags,omitempty"`
Flag bool `json:"flag"`
Tlp Tlp `json:"tlp,omitempty"`
TlpLabel string `json:"tlpLabel,omitempty"`
Pap Pap `json:"pap,omitempty"`
PapLabel string `json:"papLabel,omitempty"`
Summary string `json:"summary,omitempty"`
CustomFields []CustomField `json:"customFields,omitempty"`
Tasks []CaseTask `json:"tasks,omitempty"`
ExtraData struct{} `json:"extraData,omitempty"`
}
// shadow unmarshal function for CaseTemplate
func (ctr *CaseTemplateResponse) UnmarshalJSON(data []byte) error {
shadow := new(shadowCaseTemplateResponse)
err := json.Unmarshal(data, &shadow)
if err != nil {
return err
}
ctr.Id = shadow.Id
ctr.Type = shadow.Type
ctr.CreatedBy = shadow.CreatedBy
ctr.UpdatedBy = shadow.UpdatedBy
ctr.CreatedAt = convertInt64ToTime(shadow.CreatedAt)
ctr.UpdatedAt = convertInt64ToTime(shadow.UpdatedAt)
ctr.Name = shadow.Name
ctr.DisplayName = shadow.DisplayName
ctr.TitlePrefix = shadow.TitlePrefix
ctr.Description = shadow.Description
ctr.Severity = shadow.Severity
ctr.Tags = shadow.Tags
ctr.Flag = shadow.Flag
ctr.Tlp = shadow.Tlp
ctr.Pap = shadow.Pap
ctr.Summary = shadow.Summary
ctr.CustomFields = shadow.CustomFields
return nil
}
// GetCaseTemplate looks up a specific template on thehive5 instance.
// It returns the CaseTemplateResponse of an error on failure
func (hive *Hivedata) GetCaseTemplate(templateName string) (*CaseTemplateResponse, error) {
url, err := url.JoinPath(hive.Url, "/api/v1/caseTemplate/", templateName)
if err != nil {
return nil, err
}
ret, err := hive.webRequest(url, GET, nil)
if err != nil {
return nil, err
}
parsedRet := new(CaseTemplateResponse)
err = json.Unmarshal(ret, parsedRet)
return parsedRet, err
}
// DeleteCaseTemplate allows the deletion of templates on thehive5 instance.
// it returns an error only if the deletion failed.
func (hive *Hivedata) DeleteCaseTemplate(templateName string) error {
url, err := url.JoinPath(hive.Url, "/api/v1/caseTemplate/", templateName)
if err != nil {
return err
}
_, err = hive.webRequest(url, DELETE, nil)
return err
}
// UpdateCaseTemplate updates an already existing template on thehive5.
// Submit the template name as first argument and a CaseTemplate object with the attributes you want to overwrite as the second.
func (hive *Hivedata) UpdateCaseTemplate(templateName string, updatedTemplate CaseTemplate) error {
url, err := url.JoinPath(hive.Url, "/api/v1/caseTemplate/", templateName)
if err != nil {
return err
}
jsonrequest, err := json.Marshal(updatedTemplate)
if err != nil {
return err
}
_, err = hive.webRequest(url, PATCH, jsonrequest)
return err
}