Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/summary report #181

Open
wants to merge 40 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
8daf59a
#SOS-15
Jackafive753 May 28, 2021
35a3f71
#SOS-22
Jackafive753 May 29, 2021
5866b6e
#SOS-15
Jackafive753 May 31, 2021
839ae2f
#SOS-23 Add getAccounts + save account information in event_status
daniebrill Jun 4, 2021
6cd0a85
#SOS-23 Use querylimit in aggregation + add error handling for type a…
daniebrill Jun 5, 2021
f8e931b
#SOS-23 Fix existing tests + code format
daniebrill Jun 8, 2021
259ccf7
#SOS-23 Modify GetAccounts for specific execution ID
daniebrill Jun 8, 2021
45db9b4
#SOS-23 Fit MockStorage to modified interface StorageDescriber
daniebrill Jun 8, 2021
2f3c15c
#SOS-23 Add tests in interpolation_test.go and server_test.go
daniebrill Jun 8, 2021
1fd641a
#SOS-20
Jackafive753 Jun 9, 2021
9c157f2
#SOS-23 Add test for elasticsearch GetAccounts
daniebrill Jun 9, 2021
0f68ad2
#SOS-16
Jackafive753 Jun 10, 2021
2b48f30
Merge remote-tracking branch 'origin/feature_multi_account_selection'…
Jackafive753 Jun 10, 2021
bf4c57d
#SOS18 Add multiaccount selection functionality to tables
daniebrill Jun 11, 2021
ecbe7ef
#SOS-16
Jackafive753 Jun 11, 2021
5a7065e
Merge remote-tracking branch 'origin/feature_multi_account_selection'…
Jackafive753 Jun 11, 2021
5ea12e5
#SOS-18
Jackafive753 Jun 14, 2021
2f99548
#SOS-18
daniebrill Jun 14, 2021
2c3e562
Merge pull request #1 from evoila/feature_multi_account_selection
Jackafive753 Jun 16, 2021
5b7187c
Changed some things to test -> Delete if somehting breaks
Jun 22, 2021
b95df02
Fix ESLint Errors
Jackafive753 Jun 22, 2021
9429c6f
I dont care
Jun 25, 2021
49ec1ad
Merge remote-tracking branch 'origin/Testing_CSV' into Testing_CSV
Jun 25, 2021
bcc2b72
I cant care less
Jun 25, 2021
47e3a84
Fix csvDownload functionality
Jackafive753 Jun 25, 2021
580c0fc
CSVDownloadButton now only shows up in 'Summary' ResourceChart to avo…
Jun 28, 2021
6f24513
Merge remote-tracking branch 'origin/Testing_CSV' into feature_all_su…
Jun 28, 2021
e0a87bd
Removed exactly one whitespace
Jun 29, 2021
eeebb64
Added exactly one whitespace
Jun 29, 2021
f380430
Removed absolutely nothing
Jun 30, 2021
0912705
Testing
Jul 1, 2021
032c425
Reverted
Jul 1, 2021
98ec140
Add material ui button for CSV report
daniebrill Jul 1, 2021
4df494c
fix warnings: unused statements
Jackafive753 Jul 2, 2021
8e464cf
fix warnings: unused statements
Jackafive753 Jul 2, 2021
85acb62
# SOS-61
Jackafive753 Jul 23, 2021
ab6405e
# SOS-57
Jackafive753 Jul 23, 2021
2a09b02
# SOS-52
Jackafive753 Jul 26, 2021
f8adc70
set account to filters if an account specific Chart is clicked
Jackafive753 Jul 28, 2021
bf559e6
Merge pull request #4 from daniebrill/feature/multi_account_selection
daniebrill Aug 2, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 117 additions & 0 deletions api/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"finala/api/httpparameters"
"finala/api/storage"
"fmt"
"io/ioutil"
"net/http"
"net/url"
Expand Down Expand Up @@ -55,6 +56,20 @@ func (server *Server) GetExecutions(resp http.ResponseWriter, req *http.Request)
server.JSONWrite(resp, http.StatusOK, results)
}

func (server *Server) GetAccounts(resp http.ResponseWriter, req *http.Request) {
queryLimit, _ := strconv.Atoi(httpparameters.QueryParamWithDefault(req, "querylimit", storage.GetExecutionsQueryLimit))
params := mux.Vars(req)
executionID := params["executionID"]
accounts, err := server.storage.GetAccounts(executionID, queryLimit)

if err != nil {
server.JSONWrite(resp, http.StatusInternalServerError, HttpErrorResponse{Error: err.Error()})
return

}
server.JSONWrite(resp, http.StatusOK, accounts)
}

// GetResourceData return resuts details by resource type
func (server *Server) GetResourceData(resp http.ResponseWriter, req *http.Request) {
queryParams := req.URL.Query()
Expand Down Expand Up @@ -186,3 +201,105 @@ func (server *Server) VersionHandler(resp http.ResponseWriter, req *http.Request
}
server.JSONWrite(resp, http.StatusOK, version)
}

//Returns json thingy wingy dingy i dont know how
func (server *Server) GetReport(resp http.ResponseWriter, req *http.Request) {
queryParams := req.URL.Query()
params := mux.Vars(req)
executionID := params["executionID"]
filters := httpparameters.GetFilterQueryParamWithOutPrefix(queryParamFilterPrefix, queryParams)

log.WithFields(log.Fields{
"filter": filters,
}).Info("filter")

filterForSummary := make(map[string]string)
for filterKey, filterValue := range filters {
filterForSummary[filterKey] = filterValue
}

response, err := server.storage.GetSummary(executionID, filterForSummary)
if err != nil {
server.JSONWrite(resp, http.StatusInternalServerError, HttpErrorResponse{Error: err.Error()})
return

}

var result []map[string]interface{}
var attributeList []string

for resourceName, resourceSummary := range response {
fmt.Println(resourceName, resourceSummary) //sanity test
if resourceSummary.ResourceCount > 0 {
resourcesList, err := server.storage.GetResources(resourceName, executionID, filters)
if err != nil {
server.JSONWrite(resp, http.StatusInternalServerError, HttpErrorResponse{Error: err.Error()})
continue
}

log.WithFields(log.Fields{
"name": resourceName,
"executionID": executionID,
"filter": filters,
}).Info(resourcesList)

//can still fail for some odd reason so we check if the resource is actually there
if resourcesList[0] == nil {
server.JSONWrite(resp, http.StatusInternalServerError, HttpErrorResponse{Error: err.Error()})
return
}
data, ok := resourcesList[0]["Data"].(map[string]interface{})
if !ok {
//screw your log
continue
}
for key := range data {
if key == "Tag" {
continue
}
exists := false
for index := range attributeList {
if attributeList[index] == key {
exists = true
}
}
if !exists {
attributeList = append(attributeList, key)
}
}

}
}

for resourceName, resourceSummary := range response {
fmt.Println(resourceName, resourceSummary) //sanity test
if resourceSummary.ResourceCount > 0 {
resourcesList, err := server.storage.GetResources(resourceName, executionID, filters)
if err != nil {
server.JSONWrite(resp, http.StatusInternalServerError, HttpErrorResponse{Error: err.Error()})
continue
}
for _, element := range resourcesList {
data, ok := element["Data"].(map[string]interface{})
if !ok {
//screw your log
continue
}

delete(data, "Tag")

data["ResourceName"] = resourceName
for _, attrName := range attributeList {
_, ok := data[attrName]
if !ok {
data[attrName] = nil
}
}
result = append(result, data)
}

}
}

server.JSONWrite(resp, http.StatusOK, result)
}
2 changes: 2 additions & 0 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,14 @@ func (server *Server) BindEndpoints() {

server.router.HandleFunc("/api/v1/summary/{executionID}", server.GetSummary).Methods("GET")
server.router.HandleFunc("/api/v1/executions", server.GetExecutions).Methods("GET")
server.router.HandleFunc("/api/v1/accounts/{executionID}", server.GetAccounts).Methods(("GET"))
server.router.HandleFunc("/api/v1/resources/{type}", server.GetResourceData).Methods("GET")
server.router.HandleFunc("/api/v1/trends/{type}", server.GetResourceTrends).Methods("GET")
server.router.HandleFunc("/api/v1/tags/{executionID}", server.GetExecutionTags).Methods("GET")
server.router.HandleFunc("/api/v1/detect-events/{executionID}", server.DetectEvents).Methods("POST")
server.router.HandleFunc("/api/v1/version", server.VersionHandler).Methods("GET")
server.router.HandleFunc("/api/v1/health", server.HealthCheckHandler).Methods("GET")
server.router.HandleFunc("/api/v1/report/{executionID}", server.GetReport).Methods("GET")
server.router.NotFoundHandler = http.HandlerFunc(server.NotFoundRoute)

}
Expand Down
100 changes: 100 additions & 0 deletions api/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,57 @@ func TestGetExecutions(t *testing.T) {

})
}
}

func TestGetAccounts(t *testing.T) {
ms, _ := MockServer()
ms.BindEndpoints()
ms.Serve()

testCases := []struct {
endpoint string
expectedStatusCode int
Count int
}{
{"/api/v1/accounts", http.StatusNotFound, 0},
{"/api/v1/accounts/1", http.StatusOK, 2},
{"/api/v1/accounts/err", http.StatusInternalServerError, 0},
}

for _, test := range testCases {
t.Run(test.endpoint, func(t *testing.T) {

rr := httptest.NewRecorder()
req, err := http.NewRequest("GET", test.endpoint, nil)
if err != nil {
t.Fatal(err)
}
ms.Router().ServeHTTP(rr, req)
if rr.Code != test.expectedStatusCode {
t.Fatalf("handler returned wrong status code: got %v want %v", rr.Code, test.expectedStatusCode)
}

if test.expectedStatusCode == http.StatusOK {
body, err := ioutil.ReadAll(rr.Body)
if err != nil {
t.Fatal(err)
}

var accountsData []storage.Accounts

err = json.Unmarshal(body, &accountsData)
if err != nil {
t.Fatalf("Could not parse http response")
}

if len(accountsData) != test.Count {
t.Fatalf("unexpected accounts data response, got %d expected %d", len(accountsData), test.Count)
}
}
})
}
}

func TestSave(t *testing.T) {
ms, mockStorage := MockServer()
ms.BindEndpoints()
Expand Down Expand Up @@ -460,3 +509,54 @@ func TestVersion(t *testing.T) {
}

}

func TestReport(t *testing.T) {
ms, _ := MockServer()
ms.BindEndpoints()
ms.Serve()

testCases := []struct {
endpoint string
expectedStatusCode int
Count int
}{
{"/api/v1/report", http.StatusNotFound, 0},
{"/api/v1/report/1", http.StatusOK, 0},
{"/api/v1/report/err", http.StatusInternalServerError, 0},
}

for _, test := range testCases {
t.Run(test.endpoint, func(t *testing.T) {
rr := httptest.NewRecorder()
req, err := http.NewRequest("GET", test.endpoint, nil)
if err != nil {
t.Fatal(err)
}
ms.Router().ServeHTTP(rr, req)
if rr.Code != test.expectedStatusCode {
t.Fatalf("handler returned wrong status code: got %v want %v", rr.Code, test.expectedStatusCode)
}

if test.expectedStatusCode == http.StatusOK {
body, err := ioutil.ReadAll(rr.Body)
if err != nil {
t.Fatal(err)
}

reportData := &[]map[string]interface{}{}
err = json.Unmarshal(body, reportData)
if err != nil {
t.Fatalf("Could not parse http response")
}

if len(*reportData) != test.Count {
t.Fatalf("unexpected resources data response, got %d expected %d", len(*reportData), test.Count)
}
} else {
if test.expectedStatusCode != rr.Code {
t.Fatalf("unexpected status code, got %d expected %d", rr.Code, test.expectedStatusCode)
}
}
})
}
}
Loading