-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathevent.go
448 lines (394 loc) · 11 KB
/
event.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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
// Copyright (c) 2018-2019 Aalborg University
// Use of this source code is governed by a GPLv3
// license that can be found in the LICENSE file.
package cli
import (
"context"
"errors"
"fmt"
"io"
"time"
pb "github.com/aau-network-security/haaukins/daemon/proto"
"github.com/spf13/cobra"
)
var (
UnableCreateEListErr = errors.New("Failed to create event list")
)
func (c *Client) CmdEvent() *cobra.Command {
cmd := &cobra.Command{
Use: "event",
Short: "Actions to perform on events",
Args: cobra.MinimumNArgs(1),
}
cmd.AddCommand(
c.CmdEventCreate(),
c.CmdEventStop(),
c.CmdEventSuspend(),
c.CmdEventResume(),
c.CmdEventList(),
c.CmdEventTeams(),
c.CmdEventLoadTest(),
c.CmdEventTeamRestart(),
c.CmdEventModify(),
c.CmdAddNotification())
return cmd
}
func (c *Client) CmdEventCreate() *cobra.Command {
var (
name string
available int
capacity int
frontends []string
exercises []string
disabledExercises []string
startTime uint64
finishTime uint64
onlyVPN bool
secretKey string
)
cmd := &cobra.Command{
Use: "create [event tag]",
Short: "Create event",
Example: `hkn event create esboot -name "ES Bootcamp" -a 5 -c 30 -e scan,sql,hb -f kali -d 2020-02-15 -k secretKey`,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
ctx := context.Background()
tag := args[0]
stream, err := c.rpcClient.CreateEvent(ctx, &pb.CreateEventRequest{
Name: name,
Tag: tag,
Frontends: frontends,
Exercises: exercises,
DisableExercises: disabledExercises,
Available: int32(available),
Capacity: int32(capacity),
OnlyVPN: 0,
StartTime: time.Now().AddDate(0, 0, int(startTime)).Format("2006-01-02 15:04:05"),
FinishTime: time.Now().AddDate(0, 0, int(finishTime)).Format("2006-01-02 15:04:05"),
SecretEvent: secretKey,
})
if err != nil {
PrintError(err)
return
}
for {
_, err := stream.Recv()
if err == io.EOF {
break
}
if err != nil {
PrintError(err)
return
}
}
},
}
cmd.Flags().StringVarP(&name, "name", "n", "", "the event name")
cmd.Flags().IntVarP(&available, "available", "a", 5, "amount of labs to make available initially for the event")
cmd.Flags().IntVarP(&capacity, "capacity", "c", 10, "maximum amount of labs")
cmd.Flags().BoolVarP(&onlyVPN, "vpnconn", "v", false, "enable only vpn connection")
cmd.Flags().StringSliceVarP(&frontends, "frontends", "f", []string{}, "list of frontends to have for each lab")
cmd.Flags().StringSliceVarP(&exercises, "exercises", "e", []string{}, "list of exercises to have for each lab")
cmd.Flags().StringSliceVarP(&disabledExercises, "disabled-exercises", "x", []string{}, "list of disabled exercises, will be spin off by user in the event manually")
cmd.Flags().Uint64VarP(&finishTime, "finishtime", "d", 15, "expected finish time of the event")
cmd.Flags().Uint64VarP(&startTime, "starttime", "s", 0, "expected start time of the event")
cmd.Flags().StringVarP(&secretKey, "secretkey", "k", "", "secret key for protecting events")
cmd.MarkFlagRequired("name")
return cmd
}
func (c *Client) CmdEventStop() *cobra.Command {
return &cobra.Command{
Use: "stop [event tag]",
Short: "Stop event",
Example: `hkn event stop esboot`,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
ctx := context.Background()
tag := args[0]
stream, err := c.rpcClient.StopEvent(ctx, &pb.StopEventRequest{
Tag: tag,
})
if err != nil {
PrintError(err)
return
}
for {
_, err := stream.Recv()
if err == io.EOF {
break
}
if err != nil {
PrintError(err)
return
}
}
},
}
}
func (c *Client) CmdEventSuspend() *cobra.Command {
return &cobra.Command{
Use: "suspend",
Short: "Suspends event",
Example: "hkn event suspend <event-tag>",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
ctx := context.Background()
tag := args[0]
stream, err := c.rpcClient.SuspendEvent(ctx, &pb.SuspendEventRequest{
EventTag: tag,
Suspend: true,
})
if err != nil {
PrintError(err)
return
}
for {
_, err := stream.Recv()
if err == io.EOF {
break
}
if err != nil {
PrintError(err)
return
}
}
},
}
}
func (c *Client) CmdEventResume() *cobra.Command {
return &cobra.Command{
Use: "resume",
Short: "Resumes event",
Example: "hkn event resume <event-tag>",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
ctx := context.Background()
tag := args[0]
stream, err := c.rpcClient.SuspendEvent(ctx, &pb.SuspendEventRequest{
EventTag: tag,
Suspend: false,
})
if err != nil {
PrintError(err)
return
}
for {
_, err := stream.Recv()
if err == io.EOF {
break
}
if err != nil {
PrintError(err)
return
}
}
},
}
}
func (c *Client) CmdEvents() *cobra.Command {
var status string
var statusID int32
cmd := &cobra.Command{
Use: "events",
Short: "List events",
Example: `hkn event list / hkn events --status closed `,
Run: func(cmd *cobra.Command, args []string) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
statusID = checkStatus(status)
r, err := c.rpcClient.ListEvents(ctx, &pb.ListEventsRequest{Status: statusID})
if err != nil {
PrintError(err)
return
}
f := formatter{
header: []string{"EVENT TAG", "NAME", "# TEAM", "EXERCISES", "CAPACITY", "STATUS", "CREATION TIME", "EXPECTED FINISH TIME", "CREATEDBY"},
fields: []string{"Tag", "Name", "TeamCount", "Exercises", "Capacity", "Status", "CreationTime", "FinishTime", "CreatedBy"},
}
var elements []formatElement
for _, e := range r.Events {
elements = append(elements, e)
}
table, err := f.AsTable(elements)
if err != nil {
PrintError(UnableCreateEListErr)
return
}
fmt.Printf(table)
},
}
cmd.Flags().StringVarP(&status, "status", "s", "running", "return events in given condition")
return cmd
}
func (c *Client) CmdEventList() *cobra.Command {
cmd := *c.CmdEvents()
cmd.Use = "ls"
cmd.Aliases = []string{"ls", "list"}
return &cmd
}
func (c *Client) CmdEventTeams() *cobra.Command {
return &cobra.Command{
Use: "teams [event tag]",
Short: "Get teams for a event",
Example: `hkn event teams esboot`,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
tag := args[0]
r, err := c.rpcClient.ListEventTeams(ctx, &pb.ListEventTeamsRequest{
Tag: tag,
})
if err != nil {
PrintError(err)
return
}
f := formatter{
header: []string{"TEAM ID", "NAME", "EMAIL", "LAST ACCESSED"},
fields: []string{"Id", "Name", "Email", "AccessedAt"},
}
var elements []formatElement
for _, e := range r.Teams {
elements = append(elements, e)
}
table, err := f.AsTable(elements)
if err != nil {
PrintError(UnableCreateEListErr)
return
}
fmt.Printf(table)
},
}
}
func (c *Client) CmdEventLoadTest() *cobra.Command {
var eventTag string
var numberOfTeams int32
cmd := &cobra.Command{
Use: "load",
Short: "Apply load test on an event",
Example: `hkn event load -t test -r 3 `,
Run: func(cmd *cobra.Command, args []string) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
r, err := c.rpcClient.StressEvent(ctx, &pb.TestEventLoadReq{EventName: eventTag, NumberOfTeams: numberOfTeams})
if err != nil {
PrintError(err)
return
}
fmt.Println(r.SignUpResult)
return
},
}
cmd.Flags().StringVarP(&eventTag, "tag", "t", "", "event tag")
cmd.Flags().Int32VarP(&numberOfTeams, "requests", "r", 1, "number of users")
return cmd
}
func (c *Client) CmdAddNotification() *cobra.Command {
var message string
var loggedInUsers bool
cmd := &cobra.Command{
Use: "announce",
Short: "Announce something before hand to all event pages...",
Example: `hkn event announce -m "This is just an example announcement for all users" -l true `,
Run: func(cmd *cobra.Command, args []string) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
r, err := c.rpcClient.AddNotification(ctx, &pb.AddNotificationRequest{
Message: message,
LoggedUsers: loggedInUsers,
})
if err != nil {
PrintError(err)
return
}
fmt.Println(r.Response)
return
},
}
cmd.Flags().StringVarP(&message, "message", "m", "", "announcement message")
cmd.Flags().BoolVarP(&loggedInUsers, "onlyloggedin", "l", false, "only logged users ")
return cmd
}
func (c *Client) CmdEventModify() *cobra.Command {
var (
eventTag string
finishTime string
capacity int
)
cmd := &cobra.Command{
Use: "modify",
Short: "Modify an event",
Example: `hkn event modify -t test -f "2021-10-29 00:12:00" -c 15 `,
Run: func(cmd *cobra.Command, args []string) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
r, err := c.rpcClient.ModifyEvent(ctx, &pb.ModifyEventRequest{
EventTag: eventTag,
FinishTime: finishTime,
Capacity: int32(capacity),
})
if err != nil {
PrintError(err)
return
}
fmt.Println(r.Message)
},
}
cmd.Flags().StringVarP(&eventTag, "eventTag", "t", "", "apply on given event tag")
cmd.Flags().StringVarP(&finishTime, "finishTime", "f", "", "finish time to be extended")
cmd.Flags().IntVarP(&capacity, "capacity", "c", 0, "capacity to be updated")
return cmd
}
func (c *Client) CmdEventTeamRestart() *cobra.Command {
return &cobra.Command{
Use: "restart [event tag] [team id]",
Short: "Restart lab for a team",
Example: `hkn event restart esboot d11eb89b`,
Args: cobra.MinimumNArgs(2),
Run: func(cmd *cobra.Command, args []string) {
// timeout value is removed because when command is used for an event 1 min
// was not enough to restart all resources that team has. So, it stops all resources
// then the communicaton between client and daemon exited after 1 min, then the stopped
// resources could not start again properly.
ctx := context.Background()
eventTag := args[0]
teamId := args[1]
stream, err := c.rpcClient.RestartTeamLab(ctx, &pb.RestartTeamLabRequest{
EventTag: eventTag,
TeamId: teamId,
})
if err != nil {
PrintError(err)
return
}
for {
_, err := stream.Recv()
if err == io.EOF {
break
}
if err != nil {
PrintError(err)
return
}
}
},
}
}
func checkStatus(status string) int32 {
var statusID int32
switch status {
case "running":
statusID = 0
case "suspended":
statusID = 1
case "booked":
statusID = 2
case "closed":
statusID = 3
case "all":
statusID = 99
default:
statusID = 0
}
return statusID
}