diff --git a/blobs.go b/blobs.go new file mode 100644 index 0000000..dbf0384 --- /dev/null +++ b/blobs.go @@ -0,0 +1,5 @@ +package shuffle + +func GetWorkflowTest() []byte { + return []byte(`{"actions":[{"app_name":"Shuffle Tools","app_version":"1.2.0","description":"Set a value to be saved to your organization in Shuffle.","app_id":"b53109ec-2873-4076-9826-4e7f586dc714","errors":[],"id":"c93c2ce0-e42a-4d30-8a2e-e9adb7ee7cc4","is_valid":true,"isStartNode":true,"sharing":true,"label":"Change Me","public":true,"generated":false,"large_image":"","environment":"Shuffle","name":"set_cache_value","parameters":[{"description":"The key to set the value for","id":"","name":"key","example":"timestamp","value":"$onprem_dashboard_testing","multiline":false,"options":null,"action_field":"","variant":"STATIC_VALUE","required":true,"configuration":false,"tags":null,"schema":{"type":"string"},"skip_multicheck":false,"value_replace":null,"unique_toggled":false},{"description":"The value to set","id":"","name":"value","example":"1621959545","value":"192.168.2.3 https://google.com","multiline":true,"options":null,"action_field":"","variant":"STATIC_VALUE","required":true,"configuration":false,"tags":null,"schema":{"type":"string"},"skip_multicheck":false,"value_replace":null,"unique_toggled":false}],"execution_variable":{"description":"","id":"","name":"","value":""},"position":{"x":-142.20343154942202,"y":130.5567750670353},"authentication_id":"","category":"","reference_url":"","sub_action":false,"source_workflow":"","run_magic_output":false,"run_magic_input":false,"execution_delay":0,"category_label":null,"suggestion":false},{"app_name":"Shuffle Tools","app_version":"1.2.0","description":"Get a value saved to your organization in Shuffle","app_id":"b53109ec-2873-4076-9826-4e7f586dc714","errors":[],"id":"f8a44502-e350-4180-933c-f7c3d7e8460b","is_valid":true,"sharing":true,"label":"Shuffle_Tools_3","public":true,"generated":false,"large_image":"","environment":"Shuffle","name":"get_cache_value","parameters":[{"description":"The key to get","id":"","name":"key","example":"timestamp","value":"$onprem_dashboard_testing","multiline":false,"options":null,"action_field":"","variant":"STATIC_VALUE","required":true,"configuration":false,"tags":null,"schema":{"type":"string"},"skip_multicheck":false,"value_replace":null,"unique_toggled":false}],"execution_variable":{"description":"","id":"","name":"","value":""},"position":{"x":-133.57704208335156,"y":308.69403928684073},"authentication_id":"","category":"Other","reference_url":"","sub_action":false,"source_workflow":"","run_magic_output":false,"run_magic_input":false,"execution_delay":0,"category_label":null,"suggestion":false},{"app_name":"Shuffle Tools","app_version":"1.2.0","description":"Delete a value saved to your organization in Shuffle","app_id":"b53109ec-2873-4076-9826-4e7f586dc714","errors":[],"id":"240b5c73-72eb-4ff0-b177-1dbf5a3cb854","is_valid":true,"sharing":true,"label":"Shuffle_Tools_3_copy","public":true,"generated":false,"large_image":"","environment":"Shuffle","name":"delete_cache_value","parameters":[{"description":"The key to delete","id":"","name":"key","example":"timestamp","value":"$onprem_dashboard_testing","multiline":false,"options":null,"action_field":"","variant":"STATIC_VALUE","required":true,"configuration":false,"tags":null,"schema":{"type":"string"},"skip_multicheck":false,"value_replace":null,"unique_toggled":false}],"execution_variable":{"description":"","id":"","name":"","value":""},"position":{"x":-130.2282403427722,"y":480.74311435295436},"authentication_id":"","category":"Other","reference_url":"","sub_action":false,"source_workflow":"","run_magic_output":false,"run_magic_input":false,"execution_delay":0,"category_label":null,"suggestion":false}],"branches":[{"destination_id":"f8a44502-e350-4180-933c-f7c3d7e8460b","id":"73ed3768-a385-4e8d-a8f5-d7fba4f6ea7e","source_id":"c93c2ce0-e42a-4d30-8a2e-e9adb7ee7cc4","label":"","has_errors":false,"conditions":null,"decorator":false},{"destination_id":"240b5c73-72eb-4ff0-b177-1dbf5a3cb854","id":"34a30674-1325-41fd-90ab-f517cbeb6aa0","source_id":"f8a44502-e350-4180-933c-f7c3d7e8460b","label":"","has_errors":false,"conditions":null,"decorator":false}],"visual_branches":[],"triggers":[],"schedules":[],"comments":[],"configuration":{"exit_on_error":false,"start_from_top":false,"skip_notifications":false},"created":1692126563,"edited":1697725624,"last_runtime":0,"due_date":1697587200,"tags":["test"],"id":"1cd69f13-5f82-462c-b8e1-91a5fbac4746","is_valid":true,"name":"Workflow Testing","description":"Used for workflow testing in Shuffle Onprem","start":"c93c2ce0-e42a-4d30-8a2e-e9adb7ee7cc4","owner":"7cff070a-e888-4e27-a575-39769b6102c2","sharing":"private","image":"","execution_org":{"name":"default","id":"ba4d38b7-db3f-4908-9ccb-47ec03f2963e","users":[],"role":"","creator_org":"","image":"","child_orgs":null,"region_url":""},"org_id":"ba4d38b7-db3f-4908-9ccb-47ec03f2963e","workflow_variables":[{"description":"","id":"7e50be7e-e774-4f4b-ac44-34c5f786eb32","name":"onprem_dashboard_testing","value":"onprem_dashboard_testing"}],"execution_environment":"","previously_saved":true,"categories":{"siem":{"name":"","count":0,"id":"","description":"","large_image":""},"communication":{"name":"","count":0,"id":"","description":"","large_image":""},"assets":{"name":"","count":0,"id":"","description":"","large_image":""},"cases":{"name":"","count":0,"id":"","description":"","large_image":""},"network":{"name":"","count":0,"id":"","description":"","large_image":""},"intel":{"name":"","count":0,"id":"","description":"","large_image":""},"edr":{"name":"","count":0,"id":"","description":"","large_image":""},"iam":{"name":"","count":0,"id":"","description":"","large_image":""},"email":{"name":"","count":0,"id":"","description":"","large_image":""},"other":{"name":"","count":2,"id":"","description":"","large_image":""}},"example_argument":"","public":false,"default_return_value":"","contact_info":{"name":"","url":""},"published_id":"","revision_id":"","usecase_ids":["Email management"],"blogpost":"","video":"","status":"test","workflow_type":"standalone","generated":false,"hidden":false,"updated_by":"admin"}`) +} diff --git a/health.go b/health.go index 473c923..142c655 100644 --- a/health.go +++ b/health.go @@ -490,13 +490,24 @@ func RunOpsHealthCheck(resp http.ResponseWriter, request *http.Request) { return } - //log.Printf("[DEBUG] Running health check") - userInfo, err := HandleApiAuthentication(resp, request) - if err != nil { - log.Printf("[WARNING] Api authentication failed in handleInfo: %s", err) - resp.WriteHeader(401) - resp.Write([]byte(`{"success": false, "reason": "Api authentication failed!"}`)) - return + // Making a fake user to pass to the api authentication + // This is mainly because nothing in here allows you to control it + var err error + userInfo := User{ + ApiKey: apiKey, + Role: "admin", + } + + if project.Environment != "onprem" { + userInfo, err = HandleApiAuthentication(resp, request) + if err != nil { + log.Printf("[WARNING] Api authentication failed in handleInfo: %s", err) + resp.WriteHeader(401) + resp.Write([]byte(`{"success": false, "reason": "Api authentication failed!"}`)) + return + } + } else { + // FIXME: Add a check for if it's been length at least between runs. This is 15 minutes by default. } if project.Environment == "onprem" && userInfo.Role != "admin" { @@ -582,7 +593,7 @@ func RunOpsHealthCheck(resp http.ResponseWriter, request *http.Request) { func GetOpsDashboardStats(resp http.ResponseWriter, request *http.Request) { ctx := GetContext(request) - log.Printf("[DEBUG] request URL query:", request.URL.Query()) + //log.Printf("[DEBUG] request URL query:", request.URL.Query()) limit := request.URL.Query().Get("limit") before := request.URL.Query().Get("before") @@ -591,19 +602,19 @@ func GetOpsDashboardStats(resp http.ResponseWriter, request *http.Request) { // convert all to int64 limitInt, err := strconv.Atoi(limit) if err != nil { - log.Printf("[ERROR] Failed converting limit to int: %s", err) + //log.Printf("[ERROR] Failed converting limit to int: %s", err) limitInt = 0 } beforeInt, err := strconv.Atoi(before) if err != nil { - log.Printf("[ERROR] Failed converting before to int: %s", err) + //log.Printf("[ERROR] Failed converting before to int: %s", err) beforeInt = 0 } afterInt, err := strconv.Atoi(after) if err != nil { - log.Printf("[ERROR] Failed converting after to int: %s", err) + //log.Printf("[ERROR] Failed converting after to int: %s", err) afterInt = 0 } @@ -656,7 +667,6 @@ func deleteOpsWorkflow(workflowHealth WorkflowHealth, apiKey string) error { id := workflowHealth.WorkflowId // 4. Delete workflow - // make a DELETE request to https://shuffler.io/api/v1/workflows/ url := baseUrl + "/api/v1/workflows/" + id log.Printf("[DEBUG] Deleting workflow with id: %s", id) @@ -964,7 +974,7 @@ func RunOpsWorkflow(apiKey string, orgId string) (WorkflowHealth, error) { updateOpsCache(workflowHealth) - log.Printf("[DEBUG] Workflow Health execution Result Status: %#v for executionID: %s", executionResults.Status, workflowHealth.ExecutionId) + //log.Printf("[DEBUG] Workflow Health execution Result Status: %#v for executionID: %s", executionResults.Status, workflowHealth.ExecutionId) // check if timeout select { @@ -976,7 +986,7 @@ func RunOpsWorkflow(apiKey string, orgId string) (WorkflowHealth, error) { // do nothing } - log.Printf("[DEBUG] Waiting 2 seconds before retrying") + //log.Printf("[DEBUG] Waiting 2 seconds before retrying") time.Sleep(2 * time.Second) } @@ -1024,31 +1034,34 @@ func InitOpsWorkflow(apiKey string, OrgId string) (string, error) { // Should be local // Create a new HTTP GET request - url := "https://shuffler.io/api/v1/workflows/602c7cf5-500e-4bd1-8a97-aa5bc8a554e6" - req, err := http.NewRequest("GET", url, nil) - if err != nil { - log.Println("[ERROR] creating HTTP request:", err) - return "", errors.New("Error creating HTTP request: " + err.Error()) - } - - // send the request client := &http.Client{} - resp, err := client.Do(req) - if err != nil { - log.Println("[ERROR] sending Ops fetch app HTTP request:", err) - return "", errors.New("Error sending HTTP request: " + err.Error()) - } + body := GetWorkflowTest() + if project.Environment == "cloud" { + url := "https://shuffler.io/api/v1/workflows/602c7cf5-500e-4bd1-8a97-aa5bc8a554e6" + req, err := http.NewRequest("GET", url, nil) + if err != nil { + log.Println("[ERROR] creating HTTP request:", err) + return "", errors.New("Error creating HTTP request: " + err.Error()) + } - defer resp.Body.Close() + // send the request + resp, err := client.Do(req) + if err != nil { + log.Println("[ERROR] sending Ops fetch app HTTP request:", err) + return "", errors.New("Error sending HTTP request: " + err.Error()) + } - // Read the response body - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - log.Println("[ERROR] reading HTTP response body:", err) - return "", errors.New("Error reading HTTP App response response body: " + err.Error()) - } + defer resp.Body.Close() + + // Read the response body + body, err = ioutil.ReadAll(resp.Body) + if err != nil { + log.Println("[ERROR] reading HTTP response body:", err) + return "", errors.New("Error reading HTTP App response response body: " + err.Error()) + } - log.Printf("[DEBUG] Successfully fetched workflow! Now creating a copy workflow for ops dashboard") + log.Printf("[DEBUG] Successfully fetched workflow! Now creating a copy workflow for ops dashboard") + } // Unmarshal the JSON data into a Workflow instance var workflowData Workflow @@ -1130,7 +1143,7 @@ func InitOpsWorkflow(apiKey string, OrgId string) (string, error) { jsonData := `{"name":"demo","description":"demo","blogpost":"","status":"test","default_return_value":"","usecase_ids":[]}` // res, err := http.Post(url, "application/json", bytes.NewBuffer([]byte(jsonData))) - req, err = http.NewRequest("POST", baseUrl+"/api/v1/workflows", bytes.NewBuffer([]byte(jsonData))) + req, err := http.NewRequest("POST", baseUrl+"/api/v1/workflows", bytes.NewBuffer([]byte(jsonData))) log.Printf("[SANITY CHECK] req method is: %s", req.Method) if err != nil { @@ -1144,8 +1157,7 @@ func InitOpsWorkflow(apiKey string, OrgId string) (string, error) { req.Header.Set("Org-Id", opsDashboardOrgId) // send the request - client = &http.Client{} - resp, err = client.Do(req) + resp, err := client.Do(req) if err != nil { log.Println("[ERROR] sending Ops create workflow HTTP request:", err) return "", errors.New("Error sending HTTP request: " + err.Error()) @@ -1196,7 +1208,6 @@ func InitOpsWorkflow(apiKey string, OrgId string) (string, error) { workflowData.Public = false // Save the workflow: PUT http://localhost:5002/api/v1/workflows/{id}?skip_save=true - req, err = http.NewRequest("PUT", baseUrl+"/api/v1/workflows/"+workflowData.ID+"?skip_save=true", nil) if err != nil { log.Println("[ERROR] creating HTTP request:", err)