daemon

package
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 24, 2024 License: Apache-2.0 Imports: 47 Imported by: 0

Documentation

Index

Constants

View Source
const (
	StatusRunning int32 = iota
	StatusSuspended
	StatusClosed
)
View Source
const (
	NoTokenErrMsg     = "token contains an invalid number of segments"
	UnauthorizeErrMsg = "unauthorized"
	AUTH_KEY          = "au"
)

Variables

View Source
var (
	AllAgentsReturnedErr        = errors.New("all agents returned error on creating environment")
	NoAgentsConnected           = errors.New("no agents connected")
	NoResourcesError            = errors.New("estimated memory usage of event is larger than what is available")
	MemoryThreshHold     uint64 = 5 // In GB
)
View Source
var (
	UnreachableDBErr = errors.New("Database seems to be unreachable")
	UnauthorizedErr  = errors.New("You seem to not be logged in")
)

Functions

func New

func New(conf *Config) (*daemon, error)

func NewAgentConnection

func NewAgentConnection(config ServiceConfig) (*grpc.ClientConn, uint64, error)

func NewExerciseClientConn

func NewExerciseClientConn(config ServiceConfig) (eproto.ExerciseStoreClient, error)

NewExerciseClientConn does not require CA file to communicate due to the fact that a script is running on Gitlab CI to push exercises to the service

func TranslateRPCErr

func TranslateRPCErr(err error) error

Types

type APICreds

type APICreds struct {
	Username string `yaml:"username,omitempty"`
	Password string `yaml:"password,omitempty"`
}

type APIResponse

type APIResponse struct {
	Status         string                                  `json:"status,omitempty"`
	Message        string                                  `json:"message,omitempty"`
	Token          string                                  `json:"token,omitempty"`
	UserInfo       *AdminUserReponse                       `json:"userinfo,omitempty"`
	Users          []AdminUserReponse                      `json:"users,omitempty"`
	Exercises      []*proto.Exercise                       `json:"exercises,omitempty"`
	Profiles       []ExerciseProfile                       `json:"profiles,omitempty"`
	EventExercises *EventExercisesResponse                 `json:"eventExercises,omitempty"`
	TeamLab        *LabResponse                            `json:"teamLab,omitempty"`
	Categories     []*proto.GetCategoriesResponse_Category `json:"categories,omitempty"`
	Orgs           []Organization                          `json:"orgs,omitempty"`
	Agents         []AgentResponse                         `json:"agents,omitempty"`
	Events         []EventResponse                         `json:"events,omitempty"`
	TeamInfo       *TeamResponse                           `json:"teaminfo,omitempty"`
	EventInfo      *EventInfoResponse                      `json:"eventinfo,omitempty"`
	LabHosts       []string                                `json:"labHosts,omitempty"`
}

type AdminClaims

type AdminClaims struct {
	Username     string        `json:"username"`
	Email        string        `json:"email"`
	Sid          string        `json:"sid"`
	Organization string        `json:"organization"`
	Role         string        `json:"role"`
	Jti          string        `json:"jti"`
	Exp          int64         `json:"exp"`
	LabQuota     sql.NullInt32 `json:"labQuota"`
}

type AdminUserNoPw added in v1.0.0

type AdminUserNoPw struct {
	Username     string
	FullName     string
	Email        string
	Role         string
	Organization string
	LabQuota     *int32
}

type AdminUserReponse

type AdminUserReponse struct {
	User  AdminUserNoPw     `json:"user,omitempty"`
	Perms map[string]string `json:"perms,omitempty"`
}

type Agent

type Agent struct {
	M            sync.RWMutex `json:"-"`
	Name         string
	Url          string
	Tls          bool
	Conn         *grpc.ClientConn   `json:"-"`
	Close        context.CancelFunc `json:"-"`
	Resources    AgentResources
	Weight       int32
	RequestsLeft int32 // Used for round robin algorithm
	QueuedTasks  uint32
	Heartbeat    string
	StateLock    bool
	Errors       []error
}

func (*Agent) GetName added in v1.0.0

func (agent *Agent) GetName() string

type AgentLab

type AgentLab struct {
	ParentAgent          ParentAgent      `json:"parentAgent,omitempty"`
	EstimatedMemoryUsage uint64           `json:"estimatedMemoryUsage,omitempty"`
	Conn                 *grpc.ClientConn `json:"-"`
	LabInfo              *aproto.Lab      `json:"labInfo,omitempty"`
	IsAssigned           bool             `json:"isAssigned,omitempty"`
	ExpiresAtTime        time.Time        `json:"expiresAtTime,omitempty"`
}

type AgentPool

type AgentPool struct {
	M                 sync.RWMutex
	Agents            map[string]*Agent
	TotalMemInstalled uint64
}

func (*AgentPool) GetAllAgents added in v1.0.0

func (ap *AgentPool) GetAllAgents() map[string]*Agent

type AgentRequest

type AgentRequest struct {
	Name    string `json:"name"`
	Url     string `json:"url,omitempty"`
	Weight  int32  `json:"weight,omitempty"`
	SignKey string `json:"signKey,omitempty"`
	AuthKey string `json:"authKey,omitempty"`
	Tls     bool   `json:"tls,omitempty"`
}

type AgentResources

type AgentResources struct {
	Cpu                      float64
	Memory                   float64
	MemoryAvailable          uint64
	MemoryInstalled          uint64
	EstimatedMemoryAvailable uint64
	LabCount                 uint32
	VmCount                  uint32
	ContainerCount           uint32
}

type AgentResponse

type AgentResponse struct {
	Name      string `json:"name"`
	Connected bool   `json:"connected"`
	Url       string `json:"url"`
	Weight    int32  `json:"weight,omitempty"`
	SignKey   string `json:"signKey"`
	AuthKey   string `json:"authKey"`
	Tls       bool   `json:"tls"`
	StateLock bool   `json:"stateLock"`
}

type Category

type Category struct {
	Name      string     `json:"name"`
	Exercises []Exercise `json:"exercises"`
}

type Config

type Config struct {
	Host               string        `yaml:"host"`                           // Host is is not really important at this time
	Port               uint          `yaml:"port"`                           // Port to be listening on
	ListeningIp        string        `yaml:"listening-ip,omitempty"`         // ex. "127.0.0.1", "0.0.0.0". Default is "0.0.0.0"
	VmName             string        `yaml:"vm-name,omitempty"`              // Name of the VM
	AuditLog           Logging       `yaml:"auditLog"`                       // Audit log, used for admin endpoints to log admin events. See Logging struct below
	Database           db.DbConfig   `yaml:"db-config,omitempty"`            // Creds and host for the postgres database
	ExerciseService    ServiceConfig `yaml:"exercise-service"`               // Creds and host information for the exercise service
	Production         bool          `yaml:"prodmode,omitempty"`             // Currently unused
	JwtSecret          string        `yaml:"jwtSecret,omitempty"`            // Secret used to sign JWT's
	Rechaptcha         string        `yaml:"recaptcha-key,omitempty"`        // Recaptcha keys, currently not used
	APICreds           APICreds      `yaml:"api-creds,omitempty"`            // Currently unused
	StatePath          string        `yaml:"state-path,omitempty"`           // Path of the state file
	TestDelay          TestDelay     `yaml:"test-delay,omitempty"`           // Can be enabled or disabled, used to delay api responses to test long response times
	LabExpiryDuration  time.Duration `yaml:"lab-expiry-duration,omitempty"`  // Base duration before lab expires without extension in minutes
	LabExpiryExtension time.Duration `yaml:"lab-expiry-extension,omitempty"` // Duration to extend lab expiration time by in minutes
	EventRetention     uint16        `yaml:"eventRetention"`                 // time in days before a closed event is deleted
}

func NewConfigFromFile

func NewConfigFromFile(path string) (*Config, error)

type Creds

type Creds struct {
	Token    string
	Insecure bool
}

func (Creds) GetRequestMetadata

func (c Creds) GetRequestMetadata(context.Context, ...string) (map[string]string, error)

func (Creds) RequireTransportSecurity

func (c Creds) RequireTransportSecurity() bool

type Event

type Event struct {
	M                          sync.RWMutex         `json:"-"`
	DbId                       int32                `json:"dbId"`
	StartedAt                  time.Time            `json:"startedAt"`
	Config                     EventConfig          `json:"config,omitempty"`
	Teams                      map[string]*Team     `json:"teams,omitempty"`
	Labs                       map[string]*AgentLab `json:"labs,omitempty"`
	UnassignedBrowserLabs      chan *AgentLab       `json:"-"`
	UnassignedVpnLabs          chan *AgentLab       `json:"-"`
	TeamsWaitingForBrowserLabs *list.List           `json:"-"` // Using linked list in order to remove teams from the queue again
	TeamsWaitingForVpnLabs     *list.List           `json:"-"`
	EstimatedMemoryUsage       uint64               `json:"estimatedMemoryUsage,omitempty"`
	EstimatedMemoryUsagePerLab uint64               `json:"estimatedMemoryUsagePerLab,omitempty"`
}

func (*Event) AddTeam

func (event *Event) AddTeam(team *Team)

func (*Event) GetConfig added in v1.0.0

func (event *Event) GetConfig() EventConfig

func (*Event) GetTeam

func (event *Event) GetTeam(username string) (*Team, error)

Event

func (*Event) IsMaxLabsReached

func (event *Event) IsMaxLabsReached() bool

Calculates the current amount of labs for an event then checks if it has passed or equal to the configured amount of maximum labs for event

type EventConfig

type EventConfig struct {
	Type                  int32     `json:"type"`
	Name                  string    `json:"name" binding:"required"`
	Tag                   string    `json:"tag" binding:"required"`
	TeamSize              int32     `json:"teamSize" binding:"required"`
	MaxLabs               int32     `json:"maxLabs" binding:"required"`
	VmName                string    `json:"vmName,omitempty"`
	ExerciseTags          []string  `json:"exerciseTags" binding:"required"`
	ExpectedFinishDate    time.Time `json:"expectedFinishDate" binding:"required"`
	PublicScoreBoard      bool      `json:"publicScoreBoard,omitempty"`
	SecretKey             string    `json:"secretKey,omitempty"`
	DynamicScoring        bool      `json:"dynamicScoring,omitempty"`
	DynamicMax            int32     `json:"dynamicMax,omitempty"`
	DynamicMin            int32     `json:"dynamicMin,omitempty"`
	DynamicSolveThreshold int32     `json:"dynamicSolveThreshold,omitempty"`
	ExerciseConfigs       []*aproto.ExerciseConfig
}

type EventExercisesResponse

type EventExercisesResponse struct {
	Categories []Category `json:"categories"`
}

type EventInfoResponse

type EventInfoResponse struct {
	Tag              string `json:"tag"`
	Name             string `json:"name"`
	Type             string `json:"type"`
	Secret           bool   `json:"secret"`
	PublicScoreboard bool   `json:"publicScoreboard"`
	TeamSize         int32  `json:"teamSize"`
	IsMaxLabsReached bool   `json:"isMaxLabsReached"`
}

type EventPool

type EventPool struct {
	M      sync.RWMutex      `json:"-"`
	Events map[string]*Event `json:"events,omitempty"`
}

func (*EventPool) AddEvent

func (ep *EventPool) AddEvent(event *Event)

Adds an event to the event pool

func (*EventPool) GetAllAgentLabsForAgent

func (ep *EventPool) GetAllAgentLabsForAgent(agentName string) []*AgentLab

func (*EventPool) GetAllEvents added in v1.0.0

func (ep *EventPool) GetAllEvents() map[string]*Event

func (*EventPool) GetEvent

func (ep *EventPool) GetEvent(eventTag string) (*Event, error)

func (*EventPool) RemoveEvent

func (ep *EventPool) RemoveEvent(eventTag string) error

Removes an event from the event pool TODO make sure to close channels

type EventResponse added in v1.0.0

type EventResponse struct {
	Id           uint   `json:"id"`
	Tag          string `json:"tag"`
	Name         string `json:"name"`
	Type         string `json:"type"`
	Organization string `json:"organization"`
	Status       string `json:"status"`
	LabsRunning  uint   `json:"labsRunning"`
	Exercises    uint   `json:"exercises"`
	Teams        uint   `json:"teams"`
	MaxLabs      uint   `json:"maxLabs"`
	SecretKey    string `json:"secretKey"`
	CreatedBy    string `json:"createdBy"`
	CreatedAt    string `json:"createdAt"`
	FinishesAt   string `json:"finishesAt"`
	FinishedAt   string `json:"finishedAt"`
}

type EventType

type EventType uint32
const (
	// LabType
	TypeBeginner EventType = iota
	TypeAdvanced
)

func (EventType) String

func (eventType EventType) String() string

type Exercise

type Exercise struct {
	ParentExerciseTag string  `json:"parentExerciseTag"`
	Static            bool    `json:"static"` // False if no docker containers for challenge
	Name              string  `json:"name"`
	Tag               string  `json:"tag"`
	Points            int     `json:"points"`
	Category          string  `json:"category"`
	Description       string  `json:"description"`
	Solved            bool    `json:"solved"`
	Solves            []Solve `json:"solves"`
}

type ExerciseProfile

type ExerciseProfile struct {
	Id           int32                         `json:"id"`
	Name         string                        `json:"name"`
	Secret       bool                          `json:"secret"`
	Organization string                        `json:"organization"`
	Public       bool                          `json:"public"`
	Description  string                        `json:"description"`
	Exercises    []db.GetExercisesInProfileRow `json:"exercises,omitempty"`
}

type ExerciseProfileRequest

type ExerciseProfileRequest struct {
	Name         string   `json:"name" binding:"required"`
	ExerciseTags []string `json:"exerciseTags" binding:"required"`
	Description  string   `json:"description"`
	Public       bool     `json:"public"`
	Organization string   `json:"organization"`
}

type ExerciseStatus

type ExerciseStatus struct {
	Tag            string    `json:"tag"`
	ChildExercises []string  `json:"childExercises"`
	Machines       []Machine `json:"machines"`
}

type Lab

type Lab struct {
	Tag             string                    `json:"tag"`
	EventTag        string                    `json:"eventTag"`
	ExercisesStatus map[string]ExerciseStatus `json:"exercisesStatus"`
	IsVpn           bool                      `json:"isVpn"`
	GuacCreds       *aproto.GuacCreds         `json:"guacCreds"`
	ExpiresAtTime   time.Time                 `json:"expiresAtTime,omitempty"`
}

type LabRequest

type LabRequest struct {
	IsVpn bool `json:"isVpn"`
}

type LabResponse

type LabResponse struct {
	ParentAgent ParentAgent `json:"parentAgent,omitempty"`
	Lab         Lab         `json:"labInfo,omitempty"`
}

type Logging

type Logging struct {
	Directory  string `yaml:"directory"`
	FileName   string `yaml:"fileName"`
	MaxBackups int    `yaml:"max-backups"`
	MaxSize    int    `yaml:"max-size"`
	MaxAge     int    `yaml:"max-age"`
}

type Machine

type Machine struct {
	Id     string `json:"id"`
	Status string `json:"status"`
}

type Organization added in v1.0.0

type Organization struct {
	ID         int32  `json:"Id"`
	Name       string `json:"Name"`
	OwnerUser  string `json:"OwnerUser"`
	OwnerEmail string `json:"OwnerEmail"`
	LabQuota   *int32 `json:"LabQuota"`
}

type ParentAgent

type ParentAgent struct {
	Name string `json:"name"`
	Url  string `json:"url"`
	Tls  bool   `json:"tls"`
}

type ResourceEstimates

type ResourceEstimates struct {
	EstimatedMemUsage       uint64
	EstimatedMemUsagePerLab uint64
	EstimatedMemorySpent    uint64
}

type ScoreResponse

type ScoreResponse struct {
	ChallengesList []struct {
		Name string `json:"name"`
		Tag  string `json:"tag"`
	} `json:"challengesList"`
	TeamsScores []TeamScore `json:"teamsScore"`
}

type ServiceConfig

type ServiceConfig struct {
	Grpc       string `yaml:"grpc"`
	AuthKey    string `yaml:"auth-key"`
	SignKey    string `yaml:"sign-key"`
	TLSEnabled bool   `yaml:"tls-enabled"`
}

type Solve

type Solve struct {
	Date string `json:"date"`
	Team string `json:"team"`
}

type SolveExerciseRequest

type SolveExerciseRequest struct {
	ParentTag string `json:"parentTag"`
	Tag       string `json:"tag"`
	Flag      string `json:"flag"`
}

type State

type State struct {
	EventPool *EventPool `json:"eventPool"`
}

type Team

type Team struct {
	M                          sync.RWMutex               `json:"-"`
	Username                   string                     `json:"username,omitempty"`
	Email                      string                     `json:"email,omitempty"`
	Status                     TeamStatus                 `json:"status"`
	Lab                        *AgentLab                  `json:"lab,omitempty"`
	QueueElement               *list.Element              `json:"-"`
	ActiveWebsocketConnections map[string]*websocket.Conn `json:"-"`
}

func (*Team) AddLab

func (team *Team) AddLab(lab *AgentLab)

func (*Team) ExtendLabExpiry

func (team *Team) ExtendLabExpiry(extendDuration time.Duration)

func (*Team) GetLab added in v1.0.0

func (team *Team) GetLab() *AgentLab

func (*Team) LockForFunc added in v1.0.0

func (team *Team) LockForFunc(function func())

type TeamClaims

type TeamClaims struct {
	Username string `json:"username"`
	Email    string `json:"email"`
	Jti      string `json:"jti"`
	Exp      int64  `json:"exp"`
	EventTag string `json:"eventTag"`
}

type TeamLoginRequest

type TeamLoginRequest struct {
	Username string `json:"username" binding:"required"`
	Password string `json:"password" binding:"required"`
	EventTag string `json:"eventTag"`
}

type TeamResponse

type TeamResponse struct {
	Username string       `json:"username,omitempty"`
	Email    string       `json:"email,omitempty"`
	Status   string       `json:"status,omitempty"`
	Lab      *LabResponse `json:"lab,omitempty"`
}

type TeamScore

type TeamScore struct {
	Rank              int                  `json:"rank"`
	TeamName          string               `json:"teamName"`
	Score             int                  `json:"score"`
	TeamSolves        map[string]TeamSolve `json:"solves"`
	LatestSolve       time.Time            `json:"latestSolve"`
	TeamScoreTimeline [][]interface{}      `json:"teamScoreTimeline"`
}

type TeamSignupRequest

type TeamSignupRequest struct {
	Username        string `json:"username" binding:"required"`
	Password        string `json:"password" binding:"required"`
	ConfirmPassword string `json:"confirmPassword" binding:"required"`
	Email           string `json:"email" binding:"required"`
	EventTag        string `json:"eventTag"`
	SecretKey       string `json:"secretKey"`
}

type TeamSolve

type TeamSolve struct {
	Tag    string `json:"tag"`
	Solved bool   `json:"solved"`
	Rank   int    `json:"rank"`
}

type TeamStatus

type TeamStatus uint8
const (
	WaitingForLab TeamStatus = iota
	InQueue
	RunningExerciseCommand
	RunningVmCommand
	Idle
)

func (TeamStatus) String

func (status TeamStatus) String() string

type TestDelay

type TestDelay struct {
	Enabled        bool          `yaml:"enabled"`
	DelayInSeconds time.Duration `yaml:"delay-seconds"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL