-
Notifications
You must be signed in to change notification settings - Fork 2
/
exercise.go
176 lines (153 loc) · 4.32 KB
/
exercise.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
package lab
import (
"context"
"errors"
"fmt"
"strings"
"sync"
"github.com/aau-network-security/haaukins-agent/internal/environment/lab/exercise"
"github.com/aau-network-security/haaukins-agent/pkg/proto"
"github.com/hashicorp/go-multierror"
"github.com/rs/zerolog/log"
"golang.org/x/exp/slices"
)
// AddExercises uses exercise configs from the exercise service to configure containers and flags to be started at a later time
func (l *Lab) AddExercises(ctx context.Context, confs ...exercise.ExerciseConfig) error {
var e *exercise.Exercise
var aRecord string
for _, conf := range confs {
if conf.Tag == "" {
return errors.New("No tags, need atleast one tag")
}
if _, ok := l.Exercises[conf.Tag]; ok {
return errors.New("Tag already exists")
}
if conf.Static {
// TODO remove static exercises on agent side, but need the overview first
e = exercise.NewExercise(conf, nil, nil, "")
} else {
e = exercise.NewExercise(conf, l.Vlib, l.Network, l.DnsAddress)
if err := e.Create(ctx); err != nil {
return err
}
ip := strings.Split(e.DnsAddr, ".")
for i, c := range e.ContainerOpts {
for _, r := range c.Records {
if strings.Contains(c.DockerConf.Image, "client") {
continue
}
if r.Type == "A" {
aRecord = r.Name
l.DnsRecords = append(l.DnsRecords, &DNSRecord{Record: map[string]string{
fmt.Sprintf("%s.%s.%s.%d", ip[0], ip[1], ip[2], e.Ips[i]): aRecord,
}})
}
}
}
}
l.Exercises[conf.Tag] = e
//.Exercises = append(l.Exercises, e)
}
return nil
}
// Used to add exercises to an already running lab.
// It configures the containers, refreshes the DNS to add the new records and then starts the new exercise containers
func (l *Lab) AddAndStartExercises(ctx context.Context, exerConfs ...exercise.ExerciseConfig) error {
l.M.Lock()
defer l.M.Unlock()
if err := l.AddExercises(ctx, exerConfs...); err != nil {
log.Error().Err(err).Msg("error adding exercise to lab")
return err
}
// Refresh the DNS
if err := l.RefreshDNS(ctx); err != nil {
log.Error().Err(err).Msg("error refreshing DNS")
return err
}
newExTags := []string{}
for _, exerConf := range exerConfs {
newExTags = append(newExTags, exerConf.Tag)
}
// Start the exercises
var res error
var wg sync.WaitGroup
for _, ex := range l.Exercises {
if slices.Contains(newExTags, ex.Tag) {
wg.Add(1)
go func(e *exercise.Exercise) {
if err := e.Start(ctx); err != nil {
res = multierror.Append(res, err)
}
wg.Done()
}(ex)
}
}
wg.Wait()
if res != nil {
return res
}
return nil
}
// TODO: Set status of exercise so user can see weither if it is started or stopped.
func (l *Lab) StartExercise(ctx context.Context, exTag string) error {
e, ok := l.Exercises[exTag]
if !ok {
return fmt.Errorf("could not find exercise with tag: %s", exTag)
}
if err := e.Start(ctx); err != nil {
return err
}
return nil
}
func (l *Lab) StopExercise(ctx context.Context, exTag string) error {
e, ok := l.Exercises[exTag]
if !ok {
return fmt.Errorf("could not find exercise with tag: %s", exTag)
}
if err := e.Stop(ctx); err != nil {
return err
}
return nil
}
func (l *Lab) ResetExercise(ctx context.Context, exTag string) error {
e, ok := l.Exercises[exTag]
if !ok {
return fmt.Errorf("could not find exercise with tag: %s", exTag)
}
if err := e.Reset(ctx); err != nil {
return err
}
return nil
}
// Returns all exercises currently in lab to be sent to the daemon
func (l *Lab) GetExercisesInfo() []*proto.Exercise {
var exercises []*proto.Exercise
for _, e := range l.Exercises {
var machines []*proto.Machine
for _, m := range e.Machines {
machine := &proto.Machine{
Id: m.Info().Id,
Status: m.Info().State.String(),
Type: m.Info().Type,
Image: m.Info().Image,
}
machines = append(machines, machine)
}
childExercises := e.GetChildExercises()
var protoChildExercises []*proto.ChildExercise
for _, childExercise := range childExercises {
protoChildExercise := &proto.ChildExercise{
Tag: childExercise.Tag,
Flag: childExercise.Value,
}
protoChildExercises = append(protoChildExercises, protoChildExercise)
}
exercise := &proto.Exercise{
Tag: e.Tag,
ChildExercises: protoChildExercises,
Machines: machines,
}
exercises = append(exercises, exercise)
}
return exercises
}