Skip to content

Commit

Permalink
Changes to conform with codacy
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisCooney committed Aug 24, 2017
1 parent b9da4dd commit ffac2cc
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 37 deletions.
32 changes: 17 additions & 15 deletions attacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Response struct {
AttackConfig AttackConfig
}

func checkHttpResponse(httpResponse *http.Response, config AttackConfig) (bool, string, string, string) {
func checkHTTPResponse(httpResponse *http.Response, config AttackConfig) (bool, string, string, string) {
if strings.Trim(httpResponse.Status, " ") != strings.Trim(config.ExpectedStatus, " ") {
reason := fmt.Sprintf("Invalid status code of %s detected. Expected %s", httpResponse.Status, config.ExpectedStatus)
return false, reason, config.ExpectedStatus, httpResponse.Status
Expand All @@ -23,14 +23,14 @@ func checkHttpResponse(httpResponse *http.Response, config AttackConfig) (bool,
return true, "", "", ""
}

func checkHttpResponses(httpResponses []*http.Response, config AttackConfig) (bool, string, string, string) {
func checkHTTPResponses(httpResponses []*http.Response, config AttackConfig) (bool, string, string, string) {
for _,httpResp := range httpResponses {

if httpResp == nil {
return false, "Error occurred during HTTP request", "A valid HTTP Response", "No HTTP Response"
}

passed, reason, expected, actual := checkHttpResponse(httpResp, config)
passed, reason, expected, actual := checkHTTPResponse(httpResp, config)

if !passed {
return passed, reason, expected, actual
Expand All @@ -40,23 +40,23 @@ func checkHttpResponses(httpResponses []*http.Response, config AttackConfig) (bo
return true, "", "", ""
}

func dispatchMultipleHttpRequests(endpoint string, c chan *http.Response, count int, method string) {
func dispatchMultipleHTTPRequests(endpoint string, c chan *http.Response, count int, method string) {
for i := 0; i < count; i++ {
if method == "" {
SendRandomHttpRequest(endpoint, c)
SendRandomHTTPRequest(endpoint, c)
} else {
SendHttpRequest(endpoint, c, method)
SendHTTPRequest(endpoint, c, method)
}
}
}

func dispatchConcurrentHttpRequests(concurrentCount int, endpoint string, c chan *http.Response, count int, method string) {
func dispatchConcurrentHTTPRequests(concurrentCount int, endpoint string, c chan *http.Response, count int, method string) {
for i:=0; i < concurrentCount; i++ {
go dispatchMultipleHttpRequests(endpoint, c, count, method)
go dispatchMultipleHTTPRequests(endpoint, c, count, method)
}
}

func collectConcurrentHttpResponses(c chan *http.Response, expectedCount int) []*http.Response {
func collectConcurrentHTTPResponses(c chan *http.Response, expectedCount int) []*http.Response {
responses := []*http.Response{}

for len(responses) < (expectedCount) {
Expand All @@ -81,7 +81,8 @@ func readResponseFromChannel(responses []*http.Response, c chan *http.Response)
return append(responses, response)
}

func RunHttpSpam(endpointConfig EndpointConfig, attackConfig AttackConfig, responseChannel chan Response) error {
// Fires off the requested number of concurrent messages at an endpoint and tests response.
func RunHTTPSpam(endpointConfig EndpointConfig, attackConfig AttackConfig, responseChannel chan Response) error {
fmt.Printf("🔥 Running HTTP Spam against %s 🔥\n", endpointConfig.Name)

c := make(chan *http.Response)
Expand All @@ -90,15 +91,15 @@ func RunHttpSpam(endpointConfig EndpointConfig, attackConfig AttackConfig, respo

messageCount := attackConfig.Concurrents * attackConfig.MessagesPerConcurrent

dispatchConcurrentHttpRequests(attackConfig.Concurrents, endpoint, c, attackConfig.MessagesPerConcurrent, attackConfig.Method)
responses := collectConcurrentHttpResponses(c, messageCount)
dispatchConcurrentHTTPRequests(attackConfig.Concurrents, endpoint, c, attackConfig.MessagesPerConcurrent, attackConfig.Method)
responses := collectConcurrentHTTPResponses(c, messageCount)

if len(responses) == 0 {
responseChannel <- Response{AttackConfig: attackConfig, Passed: false, Report: "Error occurred during HTTP Spam."}
return nil
}

passed, reason, expected, actual := checkHttpResponses(responses, attackConfig)
passed, reason, expected, actual := checkHTTPResponses(responses, attackConfig)

if !passed {
responseChannel <- Response{Expected: expected, Actual: actual, AttackConfig: attackConfig, Passed: false, Report: fmt.Sprintf("Failure during HTTP Spam. %s", reason)}
Expand All @@ -109,12 +110,13 @@ func RunHttpSpam(endpointConfig EndpointConfig, attackConfig AttackConfig, respo
return nil
}

func RunCorruptHttp(endpointConfig EndpointConfig, attackConfig AttackConfig, responseChannel chan Response) error {
// Fires off a Corrupted HTTP request at the specific endpoint.
func RunCorruptHTTP(endpointConfig EndpointConfig, attackConfig AttackConfig, responseChannel chan Response) error {
fmt.Printf("🔥 Running Corrupt HTTP against %s 🔥\n", endpointConfig.Name)
c := make(chan string)
endpoint := BuildNetworkPath("", endpointConfig.Host, endpointConfig.Port, "")

go SendCorruptHttpData(endpoint, c)
go SendCorruptHTTPData(endpoint, c)
rawResponse := <- c

if rawResponse == "" {
Expand Down
22 changes: 11 additions & 11 deletions attacks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import (
"fmt"
)

func TestRunHttpSpam(t *testing.T) {
func TestRunHTTPSpam(t *testing.T) {
t.Run("Test run HTTP spam correctly reports on endpoints", func(t *testing.T) {
createMockHttpServer()
createMockHTTPServer()
defer httpmock.DeactivateAndReset()
c := createResponseChannel()

endpoint, attack := CreateTestEndpointAndAttackConfiguration("200")

go RunHttpSpam(endpoint, attack, c)
go RunHTTPSpam(endpoint, attack, c)

response := <- c

Expand All @@ -25,7 +25,7 @@ func TestRunHttpSpam(t *testing.T) {

endpoint, attack = CreateTestEndpointAndAttackConfiguration("404")

go RunHttpSpam(endpoint, attack, c)
go RunHTTPSpam(endpoint, attack, c)

response = <- c

Expand All @@ -35,13 +35,13 @@ func TestRunHttpSpam(t *testing.T) {
})
}

func TestRunCorruptHttp(t *testing.T) {
func TestRunCorruptHTTP(t *testing.T) {
t.Run("Test run Corrupt HTTP correctly reports on endpoints", func(t *testing.T) {
go createMockTcpServer()
c := createResponseChannel()
endpoint, attack := CreateTestEndpointAndAttackConfiguration("200")

go RunCorruptHttp(endpoint, attack, c)
go RunCorruptHTTP(endpoint, attack, c)

response := <- c

Expand All @@ -51,7 +51,7 @@ func TestRunCorruptHttp(t *testing.T) {

endpoint, attack = CreateTestEndpointAndAttackConfiguration("404")

go RunCorruptHttp(endpoint, attack, c)
go RunCorruptHTTP(endpoint, attack, c)

response = <- c

Expand All @@ -61,7 +61,7 @@ func TestRunCorruptHttp(t *testing.T) {
})
}

func createMockHttpServer() {
func createMockHTTPServer() {
httpmock.Activate()

httpmock.RegisterResponder("GET", "http://localhost:8080/my-endpoint",
Expand All @@ -81,11 +81,11 @@ func createMockTcpServer() {
fmt.Println("Mock TCP Server returning 200")
conn.Write([]byte("MOCK RESPONSE: 200\n"))
defer conn.Close()

count ++

if(count == 1) {
if(count == 2) {
return
} else {
count ++
}
}
}
Expand Down
14 changes: 8 additions & 6 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package main
import (
"encoding/json"
"io/ioutil"
"errors"
"fmt"
)

// Object representation of config file provided by the user.
type Config struct {
Endpoints []EndpointConfig `json:"endpoints"`
}

// Configuration for a specific endpoint.
type EndpointConfig struct {
Name string `json:"name"`
Host string `json:"host"`
Expand All @@ -20,6 +21,7 @@ type EndpointConfig struct {
Attacks []AttackConfig `json:"attacks"`
}

// Configuration for a specific attack on an endpoint.
type AttackConfig struct {
Type string `json:"type"`
ExpectedStatus string `json:"expectedStatus"`
Expand Down Expand Up @@ -49,25 +51,25 @@ func loadConfigFile(configPath string) ([]byte) {
func IsValidConfig(config *Config) (bool, error) {

if len(config.Endpoints) == 0 {
return false, errors.New(fmt.Sprintf("⚠️ Endpoints can not be empty. The monkey needs victims. ⚠️"))
return false, fmt.Errorf("⚠️ Endpoints can not be empty. The monkey needs victims. ⚠️")
}

for i,endpoint := range config.Endpoints {
if endpoint.Name == "" {
return false, errors.New(fmt.Sprintf("⚠️ Endpoint name can not be empty for endpoint #%d. The monkey is like Arya Stark. It needs a name. ⚠️", i + 1))
return false, fmt.Errorf("⚠️ Endpoint name can not be empty for endpoint #%d. The monkey is like Arya Stark. It needs a name. ⚠️", i + 1)
}

if endpoint.Host == "" {
return false, errors.New(fmt.Sprintf("⚠️ Host can not be null for endpoint with name %s. The monkey needs an address to go after. ⚠️", endpoint.Name))
return false, fmt.Errorf("⚠️ Host can not be null for endpoint with name %s. The monkey needs an address to go after. ⚠️", endpoint.Name)
}

if len(endpoint.Attacks) == 0 {
return false, errors.New(fmt.Sprintf("⚠️ Endpoint must have attacks associated with it. The monkey kills all it sees. ⚠️"))
return false, fmt.Errorf("⚠️ Endpoint must have attacks associated with it. The monkey kills all it sees. ⚠️")
}

for j,attack := range endpoint.Attacks {
if attack.Type == "" {
return false, errors.New(fmt.Sprintf("⚠️ Attack config #%d for endpoint %s needs a type. Future versions will interpret this as an all access pass for the monkey. ⚠️", endpoint.Name, j + 1))
return false, fmt.Errorf("⚠️ Attack config #%d for endpoint %s needs a type. Future versions will interpret this as an all access pass for the monkey. ⚠️", endpoint.Name, j + 1)
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion monkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

var MAX_TIME_BETWEEN_ATTACKS = 60

var ATTACKS_STRATEGY = map[string](func(endpointConfig EndpointConfig, attackConfig AttackConfig, responseChannel chan Response) error){"HTTP_SPAM": RunHttpSpam,"CORRUPT_HTTP": RunCorruptHttp}
var ATTACKS_STRATEGY = map[string](func(endpointConfig EndpointConfig, attackConfig AttackConfig, responseChannel chan Response) error){"HTTP_SPAM": RunHTTPSpam,"CORRUPT_HTTP": RunCorruptHTTP}

func main() {
config := GetConfigFromCli()
Expand All @@ -36,6 +36,7 @@ func listenForResponses(responseChannel chan Response) {
}
}

// Sets up the targets in the config file for attack.
func SetupTargets(config *Config, responseChannel chan Response) {
for _,endpoint := range config.Endpoints {
fmt.Printf("🎯 Setting up %s 🎯\n", endpoint.Name)
Expand Down
2 changes: 2 additions & 0 deletions testingUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

package main

// Creates configuration objects for an endpoint and an attack. Useful for testing specific attacks.
func CreateTestEndpointAndAttackConfiguration(expectedStatus string) (EndpointConfig, AttackConfig) {
endpoint := EndpointConfig{Name:"Test Endpoint", Protocol:"http",Host:"localhost",Port:"8080",Path:"/my-endpoint"}
attack := AttackConfig{Type:"HTTP_SPAM",Concurrents:1,MessagesPerConcurrent:1,ExpectedStatus:expectedStatus,Method:"GET"}

return endpoint, attack
}

// Creates a full config object with an endpoint and an attack. Useful for testing whole app.
func CreateFullTestConfiguration() Config {
config := Config{}

Expand Down
8 changes: 4 additions & 4 deletions webUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func BuildNetworkPath(protocol string, host string, port string, path string) st
return fmt.Sprintf("%s://%s:%s%s", protocol, host, port, path)
}

func SendHttpRequest(endpoint string, c chan *http.Response, method string) (*http.Response, error) {
func SendHTTPRequest(endpoint string, c chan *http.Response, method string) (*http.Response, error) {
client := &http.Client{}
request, _ := http.NewRequest(method, endpoint, bytes.NewBufferString("hello"))

Expand All @@ -32,14 +32,14 @@ func SendHttpRequest(endpoint string, c chan *http.Response, method string) (*ht
return response, nil
}

func SendRandomHttpRequest(endpoint string, c chan *http.Response) (*http.Response, error) {
func SendRandomHTTPRequest(endpoint string, c chan *http.Response) (*http.Response, error) {
method := getRandomRequestMethod()
return SendHttpRequest(endpoint, c, method)
return SendHTTPRequest(endpoint, c, method)
}

var MAX_JUNK_LENGTH = 100

func SendCorruptHttpData(endpoint string, c chan string) error {
func SendCorruptHTTPData(endpoint string, c chan string) error {
conn, err := net.Dial("tcp", endpoint)

if err != nil {
Expand Down

0 comments on commit ffac2cc

Please sign in to comment.