forked from gstotts/insightcloudsec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bots.go
217 lines (183 loc) · 6.75 KB
/
bots.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 insightcloudsec
import (
"encoding/json"
"fmt"
"net/http"
)
const (
// Bot Severities
BOT_SEVERITY_LOW = "low"
BOT_SEVERITY_MEDIUM = "medium"
BOT_SEVERITY_HIGH = "high"
// Bot Categories
BOT_CATEGORY_SECURITY = "Security"
BOT_CATEGORY_OPTIMIZATION = "Optimization"
BOT_CATEGORY_CURATION = "Curation"
BOT_CATEGORY_BEST_PRACTICES = "Best Practices"
BOT_CATEOGRY_MISC = "Miscellaneous"
// Bot States
BOT_STATE_RUNNING = "RUNNING"
BOT_STATE_ARCHIVED = "ARCHIVED"
BOT_STATE_SCANNING = "SCANNING"
BOT_STATE_PAUSED = "PAUSED"
)
var (
BOT_SEVERITY_RANGES = []string{BOT_SEVERITY_HIGH, BOT_SEVERITY_MEDIUM, BOT_SEVERITY_LOW}
BOT_CATEGORIES = []string{BOT_CATEGORY_SECURITY, BOT_CATEGORY_OPTIMIZATION, BOT_CATEGORY_CURATION, BOT_CATEGORY_BEST_PRACTICES, BOT_CATEOGRY_MISC}
BOT_STATES = []string{BOT_STATE_RUNNING, BOT_STATE_PAUSED, BOT_STATE_ARCHIVED, BOT_STATE_SCANNING}
)
var _ Bots = (*bots)(nil)
type Bots interface {
ArchiveBot(id string) error
Create(bot_data Bot) (BotResults, error)
GetBotByID(id string) (BotResults, error)
EnableBot(id string) error
List() (BotList, error)
PauseBot(id string) error
}
type bots struct {
client *Client
}
type Bot struct {
Name string `json:"name"`
Description string `json:"description"`
Severity string `json:"severity"`
Category string `json:"category"`
OnDemandEnabled bool `json:"ondemand_enabled"`
State string `json:"state"`
Instructions BotInstructions `json:"instructions"`
}
type BotList struct {
Bots []BotResults `json:"bots"`
Count int `json:"count"`
}
type BotResults struct {
ResourceID string `json:"resource_id"`
Name string `json:"name"`
Description string `json:"description"`
Notes string `json:"notes"`
InsightID string `json:"insight_id"`
Source string `json:"source"`
InsightName string `json:"insight_name"`
Owner string `json:"owner"`
OwnerName string `json:"owner_name"`
State string `json:"state"`
DateCreated string `json:"date_created"`
DateModified string `json:"date_modified"`
Category string `json:"category"`
BadgeScopeOperator string `json:"badge_scope_operator"`
Instructions BotInstructions `json:"instructions"`
Schedule BotSchedule `json:"schedule"`
HookpointCreated bool `json:"hookpoint_created"`
HookpointModified bool `json:"hookpoint_modified"`
HookpointTagsModified bool `json:"hookpoint_tags_modified"`
HookpointDestroyed bool `json:"hookpoint_destroyed"`
NextScheduled float32 `json:"next_scheduled_run"`
Valid bool `json:"valid"`
EventFailures BotErrors `json:"event_failures"`
Severity string `json:"severity"`
DetailedLogging bool `json:"detailed_logging"`
Version int `json:"version"`
ExemptionsCount int `json:"exemptions_count"`
}
type BotErrors struct {
Errors int `json:"errors"`
Timeouts int `json:"timeouts"`
InvalidPerms int `json:"invalid_perms"`
}
type BotSchedule struct {
Type string `json:"_type"`
TimeOfDay BotScheduleTimeOfDay `json:"time_of_day"`
DayOfMonth int `json:"day_of_month"`
DayOfWeek int `json:"day_of_week"`
ExcludeDays []int `json:"exclude_days"`
MinuteOfHour int `json:"minute_of_hour"`
SecondOfHour int `json:"second_of_hour"`
}
type BotScheduleTimeOfDay struct {
Type string `json:"_type"`
Second int `json:"second"`
Minute int `json:"minute"`
Hour int `json:"hour"`
}
type BotInstructions struct {
ResourceTypes []string `json:"resource_types"`
Filters []BotFilter `json:"filters"`
Actions []BotAction `json:"actions"`
Groups []string `json:"groups"`
Badges []map[string]string `json:"badges"`
}
type BotFilter struct {
Config map[string]interface{} `json:"config"`
Name string `json:"name"`
}
type BotAction struct {
RunWhenResultIs bool `json:"run_when_result_is"`
Config interface{} `json:"config"`
Name string `json:"name"`
}
// RESOURCE FUNCTIONS
///////////////////////////////////////////
func (s *bots) Create(bot_data Bot) (BotResults, error) {
err := validateBot(bot_data)
if err != nil {
return BotResults{}, nil
}
resp, err := s.client.makeRequest(http.MethodPost, "/v2/public/botfactory/bot/create", bot_data)
if err != nil {
return BotResults{}, err
}
var ret BotResults
if err := json.NewDecoder(resp.Body).Decode(&ret); err != nil {
return BotResults{}, err
}
return ret, nil
}
func (s *bots) List() (BotList, error) {
default_body := make(map[string]interface{})
default_body["filters"] = []string{}
default_body["offset"] = 0
resp, err := s.client.makeRequest(http.MethodPost, "/v2/public/botfactory/list", default_body)
if err != nil {
return BotList{}, err
}
var ret BotList
if err := json.NewDecoder(resp.Body).Decode(&ret); err != nil {
return BotList{}, err
}
return ret, nil
}
func (s *bots) ArchiveBot(id string) error {
_, err := s.client.makeRequest(http.MethodPost, fmt.Sprintf("/v2/public/botfactory/%s/archive", id), nil)
return err
}
func (s *bots) PauseBot(id string) error {
// Function pauses the bot of the given Resource ID and returns error if failed
_, err := s.client.makeRequest(http.MethodPost, fmt.Sprintf("/v2/public/botfactory/%s/pause", id), nil)
return err
}
func (s *bots) EnableBot(id string) error {
// Function enables the both of the given Resource ID and returns error if failed
_, err := s.client.makeRequest(http.MethodPost, fmt.Sprintf("/v2/public/botfactory/%s/resume", id), nil)
return err
}
func (s *bots) GetBotByID(id string) (BotResults, error) {
resp, err := s.client.makeRequest(http.MethodPost, fmt.Sprintf("/v2/public/botfactory/%s/get", id), nil)
if err != nil {
return BotResults{}, err
}
var ret BotResults
if err := json.NewDecoder(resp.Body).Decode(&ret); err != nil {
return BotResults{}, err
}
return ret, nil
}
func validateBot(b Bot) error {
if !isInSlice(b.Severity, BOT_SEVERITY_RANGES) {
return fmt.Errorf("[-] ERROR: Bot Severity must be one of %s", BOT_SEVERITY_RANGES)
}
if !isInSlice(b.Category, BOT_CATEGORIES) {
return fmt.Errorf("[-] ERROR: Bot Category must be one of %s", BOT_CATEGORIES)
}
return nil
}