Skip to content

Commit

Permalink
Merge pull request #475 from aau-network-security/develop
Browse files Browse the repository at this point in the history
version release 2.1.0
  • Loading branch information
mrtrkmn committed Sep 15, 2020
2 parents a49c32f + 8f93fc2 commit 11a4762
Show file tree
Hide file tree
Showing 19 changed files with 687 additions and 175 deletions.
62 changes: 62 additions & 0 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
name: "CodeQL"

on:
push:
branches: [master,develop]
pull_request:
# The branches below must be a subset of the branches above
branches: [master,develop]
schedule:
- cron: '0 19 * * 3'

jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
# Override automatic language detection by changing the below list
# Supported options are ['csharp', 'cpp', 'go', 'java', 'javascript', 'python']
language: ['go']
# Learn more...
# https://docs.github.com/en/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#overriding-automatic-language-detection

steps:
- name: Checkout repository
uses: actions/checkout@v2
with:
# We must fetch at least the immediate parents so that if this is
# a pull request then we can checkout the head.
fetch-depth: 2

# If this run was triggered by a pull request event, then checkout
# the head of the pull request instead of the merge commit.
- run: git checkout HEAD^2
if: ${{ github.event_name == 'pull_request' }}

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v1
with:
languages: ${{ matrix.language }}

# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v1

# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl

# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
# and modify them (or add more) to build your code if your project
# uses a compiled language

#- run: |
# make bootstrap
# make release

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
- name: Test
env:
GITHUB_CI: "GITHUB_CI"
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
# CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
run: |
go test -race -coverprofile=coverage.txt -covermode=atomic -v ./...
bash <(curl -s https://codecov.io/bash)
# bash <(curl -s https://codecov.io/bash)
2 changes: 1 addition & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ builds:
# Custom ldflags templates.
# Default is `-s -w -X main.version={{.Version}} -X main.commit={{.ShortCommit}} -X main.date={{.Date}} -X main.builtBy=goreleaser`.
ldflags:
- -s -w -X main.version={{ .Version }}
- -s -w -X main.version={{.Version}} -X main.commit={{.ShortCommit}} -X main.date={{.Date}} -X main.builtBy=goreleaser

# Custom environment variables to be set during the builds.
# Default is empty.
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.7.0
2.1.0
27 changes: 26 additions & 1 deletion client/cli/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (c *Client) CmdEvent() *cobra.Command {
c.CmdEventResume(),
c.CmdEventList(),
c.CmdEventTeams(),

c.CmdEventLoadTest(),
c.CmdEventTeamRestart())

return cmd
Expand Down Expand Up @@ -303,6 +303,31 @@ func (c *Client) CmdEventTeams() *cobra.Command {
}
}

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) CmdEventTeamRestart() *cobra.Command {
return &cobra.Command{
Use: "restart [event tag] [team id]",
Expand Down
97 changes: 94 additions & 3 deletions daemon/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ package daemon

import (
"context"
"errors"
"fmt"
"math"
"math/rand"
"net/http"
"net/url"
"strconv"
"strings"
"time"
Expand All @@ -15,9 +19,16 @@ import (
"github.com/rs/zerolog/log"
)

const (
charSet = "abcdefghijklmnopqrstuvwxyz" +
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
numSet = "0123456789"
)

var (
NPUserMaxLabs = 40
NotAvailableTag = "not available tag, there is already an event which is either running, booked or suspended"
NoPrivilegeToStressTest = errors.New("No privilege to have stress test on Haaukins !")
NPUserMaxLabs = 40
NotAvailableTag = "not available tag, there is already an event which is either running, booked or suspended"
)

// INITIAL POINT OF CREATE EVENT FUNCTION, IT INITIALIZE EVENT AND ADDS EVENTPOOL
Expand All @@ -36,7 +47,7 @@ func (d *daemon) startEvent(ev guacamole.Event) {
Strs("Frontends", frontendNames).
Msg("Creating event")

ev.Start(context.TODO())
go ev.Start(context.TODO())

d.eventPool.AddEvent(ev)
}
Expand Down Expand Up @@ -547,6 +558,86 @@ func (d *daemon) closeEvents() error {
return nil
}

// StressEvent is making requests to daemon to see how daemon can handle them
func (d *daemon) StressEvent(ctx context.Context, req *pb.TestEventLoadReq) (*pb.TestEventLoadResp, error) {
user, err := getUserFromIncomingContext(ctx)
if err != nil {
return &pb.TestEventLoadResp{}, err
}
if !user.SuperUser {
return &pb.TestEventLoadResp{}, NoPrivilegeToStressTest
}

ev, err := d.eventPool.GetEvent(store.Tag(req.EventName))
if ev == nil {
return &pb.TestEventLoadResp{}, fmt.Errorf("no such an event called %s, error: %v !", req.EventName, err)
}

if ev.GetConfig().Capacity < int(req.NumberOfTeams) {
return &pb.TestEventLoadResp{}, errors.New("event capacity is less than provided number of teams. skipping testing...")
}
var port, protocol string
if d.conf.Certs.Enabled {
port = strconv.FormatUint(uint64(d.conf.Port.Secure), 10)
protocol = "https://"
} else {
port = strconv.FormatUint(uint64(d.conf.Port.InSecure), 10)
protocol = "http://"
}
endPoint := fmt.Sprintf(protocol + req.EventName + "." + d.conf.Host.Http + ":" + port + "/signup")
resp := make(chan string)
for i := 0; i < int(req.NumberOfTeams); i++ {
go func() {
resp <- d.postRequest(endPoint, protocol)
}()
}
response := <-resp

return &pb.TestEventLoadResp{SignUpResult: response}, nil
}

// constructRandomValues will create random form for signing up
func constructRandomValues() url.Values {
name := stringWithCharset(10, charSet)
email := fmt.Sprintf("%s@%s.com", stringWithCharset(10, charSet), stringWithCharset(10, charSet))
password := fmt.Sprintf("%s", stringWithCharset(6, numSet))
form := url.Values{}
form.Add("email", email)
form.Add("team-name", name)
form.Add("password", password)
form.Add("password-repeat", password)
return form
}

func (d *daemon) postRequest(endPoint, protocol string) string {
hc := http.Client{}
v := constructRandomValues()
postReq, err := http.NewRequest("POST", endPoint, strings.NewReader(v.Encode()))
if err != nil {
return err.Error()
}
postReq.Form = v
postReq.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0")
postReq.Header.Add("Referer", protocol+endPoint)
postReq.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err := hc.Do(postReq)
if err != nil {
return err.Error()
}
return resp.Status
}

// StringWithCharset will return random characters
func stringWithCharset(length int, charset string) string {
var seededRand *rand.Rand = rand.New(
rand.NewSource(time.Now().UnixNano()))
b := make([]byte, length)
for i := range b {
b[i] = charset[seededRand.Intn(len(charset))]
}
return string(b)
}

func isDelayed(customTime string) bool {
now := time.Now()
givenTime, _ := time.Parse(displayTimeFormat, customTime)
Expand Down
Loading

0 comments on commit 11a4762

Please sign in to comment.