Skip to content

Commit

Permalink
Merge pull request #13 from aau-network-security/feature/agent-syncro…
Browse files Browse the repository at this point in the history
…nization

Feature/agent syncronization
  • Loading branch information
Mikkelhost authored Sep 24, 2023
2 parents 60bf06a + 465c818 commit 894b960
Show file tree
Hide file tree
Showing 9 changed files with 555 additions and 315 deletions.
12 changes: 8 additions & 4 deletions internal/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,18 @@ func New(conf *Config) (*Agent, error) {
if err != nil {
log.Error().Err(err).Msg("error resuming state")
envPool = &env.EnvPool{
M: &sync.RWMutex{},
Envs: make(map[string]*env.Environment),
M: &sync.RWMutex{},
Envs: make(map[string]*env.Environment),
StartingEnvs: make(map[string]bool),
ClosingEnvs: make(map[string]bool),
}
}
if envPool == nil {
envPool = &env.EnvPool{
M: &sync.RWMutex{},
Envs: make(map[string]*env.Environment),
M: &sync.RWMutex{},
Envs: make(map[string]*env.Environment),
StartingEnvs: make(map[string]bool),
ClosingEnvs: make(map[string]bool),
}
}
// Creating agent struct
Expand Down
13 changes: 13 additions & 0 deletions internal/agent/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ var (
// beginner events where the user would just need to press the connect button and a lab would be ready with all challenges running.
func (a *Agent) CreateEnvironment(ctx context.Context, req *proto.CreatEnvRequest) (*proto.StatusResponse, error) {
// Env for event already exists, Do not start a new guac container
a.EnvPool.AddStartingEnv(req.EventTag)
defer a.EnvPool.RemoveStartingEnv(req.EventTag)
log.Debug().Msgf("got createEnv request: %v", req)

if a.EnvPool.DoesEnvExist(req.EventTag) {
Expand Down Expand Up @@ -176,6 +178,8 @@ func (a *Agent) CreateEnvironment(ctx context.Context, req *proto.CreatEnvReques

// Closes environment and attached containers/vms, and removes the environment from the event pool
func (a *Agent) CloseEnvironment(ctx context.Context, req *proto.CloseEnvRequest) (*proto.StatusResponse, error) {
a.EnvPool.AddClosingEnv(req.EventTag)
defer a.EnvPool.RemoveClosingEnv(req.EventTag)
env, err := a.EnvPool.GetEnv(req.EventTag)
if err != nil {
log.Error().Str("envTag", req.EventTag).Msg("error finding finding environment with tag")
Expand Down Expand Up @@ -266,6 +270,15 @@ func (a *Agent) AddExercisesToEnv(ctx context.Context, req *proto.ExerciseReques
return &proto.StatusResponse{Message: "OK"}, nil
}

// Lists currently running, starting and closing environments.
func (a *Agent) ListEnvironments(ctx context.Context, req *proto.Empty) (*proto.ListEnvResponse, error) {
return &proto.ListEnvResponse{
EventTags: a.EnvPool.GetEnvList(),
StartingEventTags: a.EnvPool.GetStartingEnvs(),
ClosingEventTags: a.EnvPool.GetClosingEnvs(),
}, nil
}

func getVPNIP() (string, error) {
// Get VPN IP address from ip pool
ip, err := vpnIPPool.Get()
Expand Down
8 changes: 3 additions & 5 deletions internal/agent/lab.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ import (
"github.com/rs/zerolog/log"
)

// TODO: Rethink func name as this should be the function that configures a lab for a user

func (a *Agent) CreateLabForEnv(ctx context.Context, req *proto.CreateLabRequest) (*proto.StatusResponse, error) {
a.EnvPool.M.RLock()
env, ok := a.EnvPool.Envs[req.EventTag]
a.EnvPool.M.RUnlock()
if !ok {
env, err := a.EnvPool.GetEnv(req.EventTag)
if err != nil {
return nil, errors.New("environment for event does not exist")
}

Expand Down
59 changes: 59 additions & 0 deletions internal/environment/envpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,62 @@ func (ep *EnvPool) RemoveEnv(tag string) error {
delete(ep.Envs, tag)
return nil
}

func (ep *EnvPool) LockForFunc(function func()) {
ep.M.Lock()
defer ep.M.Unlock()

function()
}

func (ep *EnvPool) GetEnvList() (envList map[string]bool) {
ep.M.RLock()
defer ep.M.RUnlock()
envList = make(map[string]bool)
for eventTag := range ep.Envs {
envList[eventTag] = true
}
return
}

func (ep *EnvPool) GetStartingEnvs() map[string]bool {
ep.M.RLock()
defer ep.M.RUnlock()

return ep.StartingEnvs
}

func (ep *EnvPool) AddStartingEnv(eventTag string) {
ep.M.Lock()
defer ep.M.Unlock()

ep.StartingEnvs[eventTag] = true
}

func (ep *EnvPool) RemoveStartingEnv(eventTag string) {
ep.M.Lock()
defer ep.M.Unlock()

delete(ep.StartingEnvs, eventTag)
}

func (ep *EnvPool) GetClosingEnvs() map[string]bool {
ep.M.RLock()
defer ep.M.RUnlock()

return ep.ClosingEnvs
}

func (ep *EnvPool) AddClosingEnv(eventTag string) {
ep.M.Lock()
defer ep.M.Unlock()

ep.ClosingEnvs[eventTag] = true
}

func (ep *EnvPool) RemoveClosingEnv(eventTag string) {
ep.M.Lock()
defer ep.M.Unlock()

delete(ep.ClosingEnvs, eventTag)
}
7 changes: 5 additions & 2 deletions internal/environment/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ import (

// General environment types
type EnvPool struct {
M *sync.RWMutex
Envs map[string]*Environment
M *sync.RWMutex
// Map of environments with eventTag as key
Envs map[string]*Environment
StartingEnvs map[string]bool
ClosingEnvs map[string]bool
}

type Environment struct {
Expand Down
6 changes: 4 additions & 2 deletions internal/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,10 @@ func ResumeState(vlib *virtual.VboxLibrary, workerPool worker.WorkerPool, stateP
}

envPool := &environment.EnvPool{
M: &sync.RWMutex{},
Envs: make(map[string]*environment.Environment),
M: &sync.RWMutex{},
Envs: make(map[string]*environment.Environment),
StartingEnvs: make(map[string]bool),
ClosingEnvs: make(map[string]bool),
}
for k, envState := range state.Environments {
env, err := convertEnvState(envState, vlib, workerPool)
Expand Down
Loading

0 comments on commit 894b960

Please sign in to comment.