Skip to content

Commit

Permalink
Merge branch 'Shuffle:main' into sso
Browse files Browse the repository at this point in the history
  • Loading branch information
LalitDeore authored Sep 24, 2024
2 parents 9c8c3d7 + dd26acd commit e7c5eb1
Showing 1 changed file with 74 additions and 1 deletion.
75 changes: 74 additions & 1 deletion shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -12275,7 +12275,6 @@ func HandleNewHook(resp http.ResponseWriter, request *http.Request) {
resp.WriteHeader(401)
resp.Write([]byte(`{"success": false, "reason": "Required fields id and name can't be empty"}`))
return

}

validTypes := []string{
Expand All @@ -12297,6 +12296,35 @@ func HandleNewHook(resp http.ResponseWriter, request *http.Request) {
return
}

originalWorkflow, err := GetWorkflow(ctx, requestdata.Workflow)
if err != nil {
log.Printf("[WARNING] Failed getting workflow %s: %s", requestdata.Workflow, err)
resp.WriteHeader(401)
resp.Write([]byte(`{"success": false, "reason": "Workflow doesn't exist"}`))
return
}

originalHook, err := GetHook(ctx, newId)
if err == nil {
log.Printf("[WARNING] Hook with ID %s doesn't exist", newId)
}

if originalWorkflow.OrgId != user.ActiveOrg.Id {
log.Printf("[WARNING] User %s doesn't have access to workflow %s", user.Username, requestdata.Workflow)
resp.WriteHeader(401)
resp.Write([]byte(`{"success": false}`))
return
}

if (!(user.SupportAccess || user.Id == originalHook.Owner) || len(user.Id) == 0) && originalHook.Id != "" {
if originalHook.OrgId != user.ActiveOrg.Id && originalHook.OrgId != "" {
log.Printf("[WARNING] User %s doesn't have access to hook %s", user.Username, originalHook.Id)
resp.WriteHeader(401)
resp.Write([]byte(`{"success": false, "reason": "User doesn't have access to hook"}`))
return
}
}

// Let remote endpoint handle access checks (shuffler.io)
baseUrl := "https://shuffler.io"
if len(os.Getenv("SHUFFLE_GCEPROJECT")) > 0 && len(os.Getenv("SHUFFLE_GCEPROJECT_LOCATION")) > 0 {
Expand Down Expand Up @@ -12378,6 +12406,33 @@ func HandleNewHook(resp http.ResponseWriter, request *http.Request) {
return
}

// set the same for the workflow
workflow, err := GetWorkflow(ctx, requestdata.Workflow)
if err != nil {
log.Printf("[WARNING] Failed getting workflow %s: %s", requestdata.Workflow, err)
resp.WriteHeader(401)
resp.Write([]byte(`{"success": false}`))
return
}

// get the webhook trigger with the same id
for triggerIndex, trigger := range workflow.Triggers {
if trigger.ID == newId {
workflow.Triggers[triggerIndex].Status = "running"
log.Printf("[INFO] Changed status of trigger %s to running", newId)
break
}
}

// update the workflow
err = SetWorkflow(ctx, *workflow, workflow.ID)
if err != nil {
log.Printf("[WARNING] Failed setting workflow %s: %s", workflow.ID, err)
resp.WriteHeader(401)
resp.Write([]byte(`{"success": false}`))
return
}

log.Printf("[INFO] Set up a new hook with ID %s and environment %s", newId, hook.Environment)
resp.WriteHeader(200)
resp.Write([]byte(`{"success": true}`))
Expand Down Expand Up @@ -12471,6 +12526,24 @@ func HandleDeleteHook(resp http.ResponseWriter, request *http.Request) {
return
}

// find workflow and set status to stopped
workflow, err := GetWorkflow(ctx, hook.Workflows[0])
if err != nil {
log.Printf("[WARNING] Failed getting workflow %s: %s", hook.Workflows[0], err)
resp.WriteHeader(401)
resp.Write([]byte(`{"success": false}`))
return
}

if len(workflow.Triggers) > 0 {
for triggerIndex, trigger := range workflow.Triggers {
if trigger.ID == fileId {
workflow.Triggers[triggerIndex].Status = "stopped"
break
}
}
}

if hook.Environment == "cloud" && project.Environment != "cloud" {
log.Printf("[INFO] Should STOP cloud webhook https://shuffler.io/api/v1/hooks/webhook_%s", hook.Id)
org, err := GetOrg(ctx, user.ActiveOrg.Id)
Expand Down

0 comments on commit e7c5eb1

Please sign in to comment.