This repository has been archived by the owner on Aug 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
insights.go
238 lines (202 loc) · 7 KB
/
insights.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
package insightcloudsec
import (
"encoding/json"
"fmt"
"net/http"
)
const (
INSIGHT_SEVERITY_CRITICAL = 5
INSIGHT_SEVERITY_SEVERE = 4
INSIGHT_SEVERITY_MAJOR = 3
INSIGHT_SEVERITY_MODERATE = 2
INSIGHT_SEVERITY_MINOR = 1
)
var _ Insights = (*insights)(nil)
type Insights interface {
Create(i Insight) (*Insight, error)
Edit(i Insight) error
Delete(insight_id int) error
Get_Insight(insight_id int, insight_source string) (*Insight, error)
Get_Insight_7_Days(insight_id int, insight_source string) (map[string]int, error)
List() ([]Insight, error)
List_Packs() ([]InsightPack, error)
}
type insights struct {
client *Client
}
type Insight struct {
ID int `json:"insight_id,omitempty"`
Name string `json:"name"`
Description string `json:"description"`
TemplateID int `json:"template_id"`
OrgID int `json:"organization_id,omitempty"`
OwnerResourceID *string `json:"owner_resource_id"`
Severity int `json:"severity"`
Scopes []string `json:"scopes"`
Tags []string `json:"tags"`
ResourceTypes []string `json:"resource_types"`
Filters []InsightFilter `json:"filters"`
Timeseries bool `json:"timeseries,omitempty"`
TimeseriesCache int `json:"timeseries_cache,omitempty"`
Badges []Badge `json:"badges,omitempty"`
BadgeFilterOperator string `json:"badge_filter_operator,omitempty"`
}
type InsightFilter struct {
Name string `json:"name"`
Config map[string]interface{} `json:"config"`
Collections map[string]interface{} `json:"collections"`
}
type BackofficeMetadata struct {
PackID int `json:"pack_id"`
PackName string `json:"pack_name"`
TemplateID int `json:"template_id"`
TemplateName string `json:"template_name"`
Description string `json:"description"`
Order int `json:"order"`
}
type InsightPack struct {
ID int `json:"pack_id"`
OrgID int `json:"organization_id"`
Name string `json:"name"`
Description string `json:"description"`
Source string `json:"source"`
LogoURL string `json:"logo_url"`
InsertedAt string `json:"inserted_at"`
UpdatedAt string `json:"updated_at"`
Backoffice []int `json:"backoffice"`
Backoffice_Metadata []BackofficeMetadata `json:"backoffice_metadata"`
Custom []int `json:"custom"`
}
func (c *insights) Create(i Insight) (*Insight, error) {
// Creates an Insight in InsightCloudSec given the insight object with appropriate configs. Returns an error if insight creation fails.
// Make sure severity is set
if i.Severity == 0 {
return nil, fmt.Errorf("[-] ERROR: Insight Severity must be set")
}
// Make sure Filters are set
if i.Filters == nil {
return nil, fmt.Errorf("[-] ERROR: Insight filters must be set")
}
// Clean up any empty config and collection fields for filters so they return empty object in json
for idx, filter := range i.Filters {
if filter.Config == nil {
i.Filters[idx].Config = make(map[string]interface{})
}
if filter.Collections == nil {
i.Filters[idx].Collections = make(map[string]interface{})
}
}
// Clean up any empty scopes so they return an empty object in json
if i.Scopes == nil {
i.Scopes = make([]string, 0)
}
resp, err := c.client.makeRequest(http.MethodPost, "/v2/public/insights/create", i)
if err != nil {
return nil, err
}
var ret Insight
if err := json.NewDecoder(resp.Body).Decode(&ret); err != nil {
return nil, err
}
return &ret, nil
}
func (c *insights) Edit(i Insight) error {
current_data, err := c.client.Insights.Get_Insight(i.ID, "custom")
if err != nil {
return err
}
fmt.Println(current_data)
// Cleanup any missing required fields for update using existing fields
if i.Name == "" {
i.Name = current_data.Name
}
if i.Description == "" {
i.Description = current_data.Description
}
if i.ResourceTypes == nil {
i.ResourceTypes = current_data.ResourceTypes
}
if i.TemplateID == 0 {
i.TemplateID = current_data.TemplateID
}
if i.Severity == 0 {
i.Severity = current_data.Severity
}
if i.Filters == nil {
i.Filters = current_data.Filters
}
// Clean up any empty config and collection fields for filters so they return empty object in json
for idx, filter := range i.Filters {
if filter.Config == nil {
i.Filters[idx].Config = make(map[string]interface{})
}
if filter.Collections == nil {
i.Filters[idx].Collections = make(map[string]interface{})
}
}
// Clean up any empty scopes so they return an empty object in json
if i.Scopes == nil {
i.Scopes = make([]string, 0)
}
_, err = c.client.makeRequest(http.MethodPost, fmt.Sprintf("/v2/public/insights/%d/edit", i.ID), i)
if err != nil {
return err
}
return nil
}
func (c *insights) List() ([]Insight, error) {
// Returns a list of all Insights from the API
resp, err := c.client.makeRequest(http.MethodGet, "/v2/public/insights/list", nil)
if err != nil {
return nil, err
}
var ret []Insight
if err := json.NewDecoder(resp.Body).Decode(&ret); err != nil {
return nil, err
}
return ret, nil
}
func (c *insights) Get_Insight(insight_id int, insight_source string) (*Insight, error) {
// Returns the specific Insight associated with the Insight ID and the Source provided
resp, err := c.client.makeRequest(http.MethodGet, fmt.Sprintf("/v2/public/insights/%d/%s", insight_id, insight_source), nil)
if err != nil {
return nil, err
}
var ret Insight
if err := json.NewDecoder(resp.Body).Decode(&ret); err != nil {
return nil, err
}
return &ret, nil
}
func (c *insights) Delete(insight_id int) error {
// Deletes the Insight for the given id. Returns an error if fails.
resp, err := c.client.makeRequest(http.MethodDelete, fmt.Sprintf("/v2/public/insights/%d/delete", insight_id), nil)
if err != nil || resp.StatusCode != 200 {
return err
}
return nil
}
func (c *insights) Get_Insight_7_Days(insight_id int, insight_source string) (map[string]int, error) {
// Returns the 7 Day View of Insight associated with the Insight ID and the Source provided
resp, err := c.client.makeRequest(http.MethodGet, fmt.Sprintf("/v2/public/insights/%d/%s/insight-data-7-days", insight_id, insight_source), nil)
if err != nil {
return nil, err
}
var ret map[string]int
if err := json.NewDecoder(resp.Body).Decode(&ret); err != nil {
return nil, err
}
return ret, nil
}
func (c *insights) List_Packs() ([]InsightPack, error) {
// Returns a list of all Insight Packs from the API
resp, err := c.client.makeRequest(http.MethodGet, "/v2/public/insights/packs/list", nil)
if err != nil {
return nil, err
}
var ret []InsightPack
if err := json.NewDecoder(resp.Body).Decode(&ret); err != nil {
return nil, err
}
return ret, nil
}