-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
399 lines (391 loc) · 9.84 KB
/
main.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
// ts - golang ts api client
// main.go: CLI framework definition
//
// Copyright 2019-2022 F5 Inc.
// Licensed under the BSD 3-clause license; see LICENSE for more information.
package main
import (
"log"
"net/http"
"os"
tsapi "github.com/threatstack/ts/api"
"github.com/urfave/cli"
)
func main() {
app := &cli.App{
Name: "ts",
Version: "0.0.1",
Usage: "Query the TS API via your command line interface.",
Authors: []cli.Author{
{Name: "Patrick Cable", Email: "[email protected]"},
},
Action: func(c *cli.Context) error {
noArgs(c)
return nil
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "user, u",
Usage: "User ID",
EnvVar: "TS_USER_ID",
},
&cli.StringFlag{
Name: "org, o",
Usage: "Organization ID",
EnvVar: "TS_ORGANIZATION_ID",
},
&cli.StringFlag{
Name: "endpoint, e",
Usage: "API Endpoint",
Value: "https://api.threatstack.com",
EnvVar: "TS_API_ENDPOINT",
},
&cli.StringFlag{
Name: "key, k",
Usage: "API Key",
EnvVar: "TS_API_KEY",
},
},
Commands: []cli.Command{
{
Name: "agent",
Usage: "Display information related to TS agents",
Subcommands: []cli.Command{
{
Name: "list",
Usage: "request all agents",
Subcommands: []cli.Command{
{
Name: "online",
Usage: "request all online agents",
Action: func(c *cli.Context) error {
getAgents(c, true)
return nil
},
},
{
Name: "offline",
Usage: "request all offline agents",
Action: func(c *cli.Context) error {
getAgents(c, false)
return nil
},
},
},
},
{
Name: "show",
Usage: "return information on a single agent",
Action: func(c *cli.Context) error {
getAgent(c)
return nil
},
},
},
},
{
Name: "alerts",
Usage: "Display information related to TS alerts",
Subcommands: []cli.Command{
{
Name: "list",
Usage: "request all alerts",
Subcommands: []cli.Command{
{
Name: "active",
Usage: "request all active alerts",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "severity, s",
Usage: "query for alerts of the chosen severity (choose 1, 2, or 3)",
},
&cli.StringFlag{
Name: "ruleid, r",
Usage: "query for alerts based on rule id",
},
&cli.StringFlag{
Name: "from, f",
Usage: "query for alerts starting from ISO-8610 datetime",
},
&cli.StringFlag{
Name: "until, t",
Usage: "query for alerts up to ISO-8610 datetime",
},
},
Action: func(c *cli.Context) error {
getAlerts(c, true)
return nil
},
},
{
Name: "dismissed",
Usage: "request all dismissed alerts",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "severity, s",
Usage: "Query for alerts of the chosen severity (choose 1, 2, or 3)",
},
&cli.StringFlag{
Name: "from, f",
Usage: "Query for alerts starting from ISO-8610 datetime",
},
&cli.StringFlag{
Name: "until, t",
Usage: "Query for alerts up to ISO-8610 datetime",
},
},
Action: func(c *cli.Context) error {
getAlerts(c, false)
return nil
},
},
},
},
{
Name: "show",
Usage: "return information on a single alert",
Action: func(c *cli.Context) error {
getAlert(c)
return nil
},
},
{
Name: "count",
Usage: "count the alerts in your organization",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "from, f",
Usage: "Count alerts starting from ISO-8610 datetime",
},
&cli.StringFlag{
Name: "until, t",
Usage: "Count alerts up to ISO-8610 datetime",
},
},
Action: func(c *cli.Context) error {
countAlerts(c)
return nil
},
},
{
Name: "events",
Usage: "request contributing events for an alert",
Action: func(c *cli.Context) error {
getEvents(c)
return nil
},
},
{
Name: "dismiss",
Usage: "dismiss alerts by alert ID (see --help)",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "alertIDs, i",
Usage: "Load alerts to dismiss from `FILE`",
},
&cli.StringFlag{
Name: "dismissReason, d",
Usage: "Dismiss reason (choose from BUSINESS_OP, COMPANY_POLICY, MAINTENANCE, or OTHER)",
},
&cli.StringFlag{
Name: "dismissReasonText, x",
Usage: "If dismissReason is OTHER, a string describing the dismiss reason",
},
},
Action: func(c *cli.Context) error {
dismissAlertsByID(c)
return nil
},
},
{
Name: "dismiss-by-parameters",
Usage: "dismiss alerts by query parameters (see --help)",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "from, f",
Usage: "Dismiss alerts starting from ISO-8610 datetime",
},
&cli.StringFlag{
Name: "until, t",
Usage: "Dismiss alerts up to ISO-8610 datetime",
},
&cli.StringFlag{
Name: "severity, s",
Usage: "Dismiss alerts of the chosen severity (choose 1, 2, or 3)",
},
&cli.StringFlag{
Name: "ruleID, r",
Usage: "Dismiss alerts generated by rule ID",
},
&cli.StringFlag{
Name: "agentID, g",
Usage: "Dismiss alerts generated by agent ID",
},
&cli.StringFlag{
Name: "dismissReason, d",
Usage: "Dismiss reason (choose from BUSINESS_OP, COMPANY_POLICY, MAINTENANCE, or OTHER)",
},
&cli.StringFlag{
Name: "dismissReasonText, x",
Usage: "If dismissReason is OTHER, a string describing the dismiss reason",
},
},
Action: func(c *cli.Context) error {
dismissAlertsByID(c)
return nil
},
},
},
},
{
Name: "portability",
Usage: "Manage data portability settings",
Subcommands: []cli.Command{
{
Name: "s3",
Usage: "display current S3 portability configuration",
Subcommands: []cli.Command{
{
Name: "create",
Usage: "create an S3 portability configuration (see --help)",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "s3bucket, s",
Usage: "S3 Bucket Name",
},
&cli.StringFlag{
Name: "arn, a",
Usage: "IAM Role Arn",
},
&cli.StringFlag{
Name: "externalID, i",
Usage: "IAM Role External ID",
},
&cli.StringFlag{
Name: "region, r",
Usage: "AWS Region (us-east-1, etc.)",
},
&cli.StringFlag{
Name: "prefix, p",
Usage: "Bucket Prefix (folder)",
},
},
Action: func(c *cli.Context) error {
createS3Portability(c)
return nil
},
},
{
Name: "list",
Usage: "show all S3 portability configurations",
Action: func(c *cli.Context) error {
getS3Portability(c)
return nil
},
},
{
Name: "delete",
Usage: "delete an S3 portability configuration",
Action: func(c *cli.Context) error {
deleteS3Portability(c)
return nil
},
},
},
},
},
},
{
Name: "members",
Usage: "Manage Threat Stack users",
Subcommands: []cli.Command{
{
Name: "invite",
Usage: "Create an new Threat Stack user for organization (see --help)",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "userrole, ur",
Usage: "add user with either 'user' or 'reader' role",
},
&cli.StringFlag{
Name: "email, em",
Usage: "the email to be used to invite the new user",
},
},
Action: func(c *cli.Context) error {
inviteUser(c)
return nil
},
},
{
Name: "list",
Usage: "show all user from a single organiztion",
Action: func(c *cli.Context) error {
getUsers(c)
return nil
},
},
{
Name: "delete",
Usage: "delete user from a single organiztion",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "userid, uid",
Usage: "remove user from organization",
},
},
Action: func(c *cli.Context) error {
deleteUser(c)
return nil
},
},
},
},
{
Name: "raw",
Usage: "send hawk-signed API requests",
Description: "The 'Secret Menu' of the TS CLI. Perform any action on any endpoint! Get raw JSON back!",
Hidden: true,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "request, X",
Usage: "Method to use. Try: PUT, POST, DELETE.",
Value: "GET",
},
&cli.StringFlag{
Name: "data, d",
Usage: "Raw JSON data to send",
},
&cli.BoolFlag{
Name: "debug, z",
Usage: "Print request information along with output",
},
},
Action: func(c *cli.Context) error {
raw(c)
return nil
},
},
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
func noArgs(c *cli.Context) {
cli.ShowAppHelp(c)
}
// tsBuildHTTPReq - function for using CLI context to build a HAWK request
func tsBuildHTTPReq(c *cli.Context, method string, endpoint string, payload []byte) (*http.Request, error) {
config := tsapi.Config{
User: c.GlobalString("user"),
Key: c.GlobalString("key"),
Org: c.GlobalString("org"),
}
req, err := tsapi.Request(config, method, c.GlobalString("endpoint")+endpoint, payload)
if err != nil {
return req, err
}
return req, nil
}