Skip to content
Closed
Changes from all commits
Commits
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
59 changes: 56 additions & 3 deletions health.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
Expand All @@ -33,6 +34,11 @@
ID string `json:"id"`
}

type testRun struct {
CRUrl string `json:"cloudrun_url"`
Region string `json:"region"`
}

func updateOpsCache(workflowHealth WorkflowHealth) {
cacheKey := fmt.Sprintf("ops-health-check")
ctx := context.Background()
Expand Down Expand Up @@ -584,7 +590,7 @@
errorChannel := make(chan error)
go func() {
log.Printf("[DEBUG] Running workflowHealthChannel goroutine")
workflowHealth, err := RunOpsWorkflow(apiKey, orgId)
workflowHealth, err := RunOpsWorkflow(apiKey, orgId, "")
if err != nil {
log.Printf("[ERROR] Failed workflow health check: %s", err)
}
Expand Down Expand Up @@ -884,7 +890,7 @@
return nil
}

func RunOpsWorkflow(apiKey string, orgId string) (WorkflowHealth, error) {
func RunOpsWorkflow(apiKey string, orgId string, cloudRunUrl string) (WorkflowHealth, error) {
// run workflow with id 602c7cf5-500e-4bd1-8a97-aa5bc8a554e6
ctx := context.Background()

Expand All @@ -899,11 +905,15 @@
}

baseUrl := os.Getenv("SHUFFLE_CLOUDRUN_URL")
if len(baseUrl) == 0 {
if len(baseUrl) == 0 && (cloudRunUrl == "" || len(cloudRunUrl) == 0) {
log.Printf("[DEBUG] Base url not set. Setting to default")
baseUrl = "https://shuffler.io"
}

if len(baseUrl) == 0 {
baseUrl = cloudRunUrl
}

if project.Environment == "onprem" {
log.Printf("[DEBUG] Onprem environment. Setting base url to localhost")
baseUrl = "http://localhost:5001"
Expand Down Expand Up @@ -958,7 +968,7 @@

// send the request
client := &http.Client{}
resp, err := client.Do(req)

Check failure

Code scanning / CodeQL

Uncontrolled data used in network request Critical

The
URL
of this request depends on a
user-provided value
.
if err != nil {
log.Printf("[ERROR] Failed sending health check HTTP request: %s", err)
return workflowHealth, err
Expand Down Expand Up @@ -1044,7 +1054,7 @@

// send the request
client := &http.Client{}
resp, err := client.Do(req)

Check failure

Code scanning / CodeQL

Uncontrolled data used in network request Critical

The
URL
of this request depends on a
user-provided value
.
if err != nil {
log.Printf("[ERROR] Failed sending HTTP request: %s", err)
return workflowHealth, err
Expand Down Expand Up @@ -1078,6 +1088,17 @@
workflowHealth.RunStatus = executionResults.Status
}

if executionResults.Status == "FINISHED" {
log.Printf("[DEBUG] Workflow Health exeution is finished, checking it's results")
for _, r := range executionResults.Results {
if r.Status != "SUCCESS" {
workflowHealth.RunStatus = "FAILED"
break
}
}
}


updateOpsCache(workflowHealth)

//log.Printf("[DEBUG] Workflow Health execution Result Status: %#v for executionID: %s", executionResults.Status, workflowHealth.ExecutionId)
Expand Down Expand Up @@ -1105,6 +1126,38 @@
return workflowHealth, nil
}

func RunHealthTest(resp http.ResponseWriter, req *http.Request) {
response, err := io.ReadAll(req.Body)
if err != nil {
log.Printf("[ERROR] Failed to read body of the health test case: %s", err)
resp.WriteHeader(500)
resp.Write([]byte(`{"success": false, "reason": "failed to read the body"}`))
return
}

var execData testRun
err = json.Unmarshal(response, &execData)
if err != nil {
log.Printf("[ERROR] Error unmarshaling test data: %s", err)
resp.WriteHeader(500)
resp.Write([]byte(`{"success": false, "reason": "failed to unmarshal the data"}`))
return
}

apiKey := os.Getenv("SHUFFLE_OPS_DASHBOARD_APIKEY")
orgId := os.Getenv("SHUFFLE_OPS_DASHBOARD_ORG")


health, err := RunOpsWorkflow(apiKey, orgId, execData.CRUrl)
if err != nil {
log.Printf("[ERROR] Health test failed %v", err)
}

jsonHealth, err := json.Marshal(health)
resp.WriteHeader(200)
resp.Write(jsonHealth)
}

func InitOpsWorkflow(apiKey string, OrgId string) (string, error) {
opsDashboardApikey := apiKey
opsDashboardOrgId := OrgId
Expand Down
Loading