-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathintegration.go
312 lines (272 loc) · 11.1 KB
/
integration.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package wallarm
import (
"encoding/json"
"fmt"
"net/url"
"strconv"
"github.com/pkg/errors"
)
type (
// Integration contains operations available on Integration resource
Integration interface {
IntegrationCreate(integrationBody *IntegrationCreate) (*IntegrationCreateResp, error)
IntegrationUpdate(integrationBody *IntegrationCreate, integrationID int) (*IntegrationCreateResp, error)
IntegrationRead(clientID int, id int) (*IntegrationObject, error)
IntegrationDelete(integrationID int) error
IntegrationWithAPICreate(integrationBody *IntegrationWithAPICreate) (*IntegrationCreateResp, error)
IntegrationWithAPIUpdate(integrationBody *IntegrationWithAPICreate, integrationID int) (*IntegrationCreateResp, error)
EmailIntegrationCreate(emailBody *EmailIntegrationCreate) (*IntegrationCreateResp, error)
EmailIntegrationUpdate(integrationBody *EmailIntegrationCreate, integrationID int) (*IntegrationCreateResp, error)
TelegramIntegrationUpdate(integrationBody *TelegramIntegrationCreate, integrationID int) (IntegrationCreateResp, error)
TelegramIntegrationCreate(tgBody *TelegramIntegrationCreate) (IntegrationCreateResp, error)
}
// IntegrationEvents represents `Events` object while creating a new integration.
// Event possible values: "hit", "vuln_high", "vuln_medium", "vuln_low", "system", "scope".
// If `IntegrationObject.Type` is "opsgenie" possible values: "hit", "vuln".
// `Active` identifies whether the current Event should be reported.
IntegrationEvents struct {
Event string `json:"event"`
Active bool `json:"active"`
}
// IntegrationObject is an inner object for the Read function containing.
// ID is a unique identifier of the Integration.
IntegrationObject struct {
ID int `json:"id"`
Active bool `json:"active"`
Name string `json:"name"`
Type string `json:"type"`
CreatedAt int `json:"created_at"`
CreatedBy string `json:"created_by"`
Target interface{} `json:"target"`
Events []struct {
IntegrationEvents
} `json:"events"`
}
// IntegrationRead is the response on the Read action.
// This is used for correct Unmarshalling of the response as a container.
IntegrationRead struct {
Body struct {
Result string `json:"result"`
Object *[]IntegrationObject `json:"object"`
} `json:"body"`
}
// IntegrationCreate defines how to configure Integration.
// `Type` possible values: "insight_connect", "opsgenie", "slack",
// "pager_duty", "splunk", "sumo_logic"
IntegrationCreate struct {
Name string `json:"name"`
Active bool `json:"active"`
Target interface{} `json:"target"`
Events *[]IntegrationEvents `json:"events"`
Type string `json:"type"`
Clientid int `json:"clientid,omitempty"`
}
// IntegrationCreateResp represents successful creating of
// an integration entity with the associative parameters.
IntegrationCreateResp struct {
Body struct {
Result string `json:"result"`
IntegrationObject `json:"object"`
} `json:"body"`
}
// IntegrationWithAPITarget is used to create an Integration with the following parameters.
// On purpose to fulfil a custom Webhooks integration.
IntegrationWithAPITarget struct {
Token string `json:"token,omitempty"`
API string `json:"api,omitempty"`
URL string `json:"url,omitempty"`
Format string `json:"format,omitempty"`
HTTPMethod string `json:"http_method,omitempty"`
Headers map[string]interface{} `json:"headers"`
CaFile string `json:"ca_file"`
CaVerify bool `json:"ca_verify"`
Timeout int `json:"timeout,omitempty"`
OpenTimeout int `json:"open_timeout,omitempty"`
}
// IntegrationWithAPICreate is a root object of Create action for Integrations.
// It aids to set `Events` to trigger this integration.
// `Type` possible values: "web_hooks"
// `Target` is a struct for a Webhooks endpoint containing params such as URL, Token, etc.
IntegrationWithAPICreate struct {
Name string `json:"name"`
Active bool `json:"active"`
Target *IntegrationWithAPITarget `json:"target"`
Events *[]IntegrationEvents `json:"events"`
Type string `json:"type"`
Clientid int `json:"clientid,omitempty"`
}
// EmailIntegrationCreate is a root object of `Create` action for the `email` integration.
// Temporary workaround (`Target` is a slice instead of string) to not check type many times.
// Then it will be changed to interface{} with type checking
EmailIntegrationCreate struct {
Name string `json:"name,omitempty"`
Active bool `json:"active"`
Target []string `json:"target,omitempty"`
Events *[]IntegrationEvents `json:"events,omitempty"`
Type string `json:"type,omitempty"`
Clientid int `json:"clientid,omitempty"`
}
TelegramIntegrationCreate struct {
Name string `json:"name,omitempty"`
Clientid int `json:"clientid,omitempty"`
Token string `json:"token,omitempty"`
ChatData string `json:"chat_data,omitempty"`
}
DatadogTarget struct {
Region string `json:"region"`
Token string `json:"token"`
}
)
// IntegrationCreate returns create object if Integration
// has been created successfully, otherwise - error.
// It accepts a body with defined settings namely Event types, Name, Target.
// API reference: https://apiconsole.eu1.wallarm.com
func (api *api) IntegrationCreate(integrationBody *IntegrationCreate) (*IntegrationCreateResp, error) {
uri := "/v2/integration"
respBody, err := api.makeRequest("POST", uri, "integration", integrationBody)
if err != nil {
return nil, err
}
var i IntegrationCreateResp
if err = json.Unmarshal(respBody, &i); err != nil {
return nil, err
}
return &i, nil
}
// IntegrationUpdate is used to Update existing resources.
// It utilises the same format of body as the Create function.
// API reference: https://apiconsole.eu1.wallarm.com
func (api *api) IntegrationUpdate(integrationBody *IntegrationCreate, integrationID int) (*IntegrationCreateResp, error) {
uri := fmt.Sprintf("/v2/integration/%d", integrationID)
respBody, err := api.makeRequest("PUT", uri, "integration", integrationBody)
if err != nil {
return nil, err
}
var i IntegrationCreateResp
if err = json.Unmarshal(respBody, &i); err != nil {
return nil, err
}
return &i, nil
}
// IntegrationRead is used to read existing integrations.
// It returns the list of Integrations
// API reference: https://apiconsole.eu1.wallarm.com
func (api *api) IntegrationRead(clientID int, id int) (*IntegrationObject, error) {
uri := "/v2/integration"
q := url.Values{}
q.Add("clientid", strconv.Itoa(clientID))
query := q.Encode()
respBody, err := api.makeRequest("GET", uri, "integration", query)
if err != nil {
return nil, err
}
var i IntegrationRead
if err = json.Unmarshal(respBody, &i); err != nil {
return nil, err
}
for _, obj := range *i.Body.Object {
if obj.ID == id {
return &obj, nil
}
}
return nil, errors.New(fmt.Sprintf("Not found. Body: %s", string(respBody)))
}
// IntegrationDelete is used to delete an existing integration.
// If successful, returns nothing.
// API reference: https://apiconsole.eu1.wallarm.com
func (api *api) IntegrationDelete(integrationID int) error {
uri := fmt.Sprintf("/v2/integration/%d", integrationID)
_, err := api.makeRequest("DELETE", uri, "integration", nil)
if err != nil {
return err
}
return nil
}
// IntegrationWithAPICreate returns created object if an integration
//
// has been created successfully, otherwise - error.
//
// It accepts defined settings namely Event types, Name, Target.
// API reference: https://apiconsole.eu1.wallarm.com
func (api *api) IntegrationWithAPICreate(integrationBody *IntegrationWithAPICreate) (*IntegrationCreateResp, error) {
uri := "/v2/integration"
respBody, err := api.makeRequest("POST", uri, "integration", integrationBody)
if err != nil {
return nil, err
}
var i IntegrationCreateResp
if err = json.Unmarshal(respBody, &i); err != nil {
return nil, err
}
return &i, nil
}
// IntegrationWithAPIUpdate is used to Update existing API integration resources.
// It utilises the same format of body as the Create function.
// API reference: https://apiconsole.eu1.wallarm.com
func (api *api) IntegrationWithAPIUpdate(integrationBody *IntegrationWithAPICreate, integrationID int) (*IntegrationCreateResp, error) {
uri := fmt.Sprintf("/v2/integration/%d", integrationID)
respBody, err := api.makeRequest("PUT", uri, "integration", integrationBody)
if err != nil {
return nil, err
}
var i IntegrationCreateResp
if err = json.Unmarshal(respBody, &i); err != nil {
return nil, err
}
return &i, nil
}
// EmailIntegrationCreate returns created object if the `email` Integration
// has been created successfully, otherwise - error.
// It accepts defined settings namely Event types, Name, Target.
// API reference: https://apiconsole.eu1.wallarm.com
func (api *api) EmailIntegrationCreate(emailBody *EmailIntegrationCreate) (*IntegrationCreateResp, error) {
uri := "/v2/integration"
respBody, err := api.makeRequest("POST", uri, "email", emailBody)
if err != nil {
return nil, err
}
var i IntegrationCreateResp
if err = json.Unmarshal(respBody, &i); err != nil {
return nil, err
}
return &i, nil
}
// EmailIntegrationUpdate is used to Update existing resources.
// It utilises the same format of body as the Create function.
// API reference: https://apiconsole.eu1.wallarm.com
func (api *api) EmailIntegrationUpdate(integrationBody *EmailIntegrationCreate, integrationID int) (*IntegrationCreateResp, error) {
uri := fmt.Sprintf("/v2/integration/%d", integrationID)
respBody, err := api.makeRequest("PUT", uri, "email", integrationBody)
if err != nil {
return nil, err
}
var i IntegrationCreateResp
if err = json.Unmarshal(respBody, &i); err != nil {
return nil, err
}
return &i, nil
}
func (api *api) TelegramIntegrationCreate(tgBody *TelegramIntegrationCreate) (IntegrationCreateResp, error) {
uri := "/v2/integration/telegram"
var i IntegrationCreateResp
respBody, err := api.makeRequest("POST", uri, "email", tgBody)
if err != nil {
return i, fmt.Errorf("TelegramIntegrationCreate: failed to make request - %w", err)
}
if err = json.Unmarshal(respBody, &i); err != nil {
return i, fmt.Errorf("TelegramIntegrationCreate: failed to read request body - %w", err)
}
return i, nil
}
func (api *api) TelegramIntegrationUpdate(integrationBody *TelegramIntegrationCreate, integrationID int) (IntegrationCreateResp, error) {
var i IntegrationCreateResp
uri := fmt.Sprintf("/v2/integration/%d", integrationID)
respBody, err := api.makeRequest("PUT", uri, "email", integrationBody)
if err != nil {
return i, fmt.Errorf("TelegramIntegrationUpdate: failed to make request - %w", err)
}
if err = json.Unmarshal(respBody, &i); err != nil {
return i, fmt.Errorf("TelegramIntegrationUpdate: failed to make request - %w", err)
}
return i, nil
}