Skip to content

Commit

Permalink
Fixed cache handler for app auth
Browse files Browse the repository at this point in the history
  • Loading branch information
frikky committed May 28, 2021
1 parent 8567460 commit 12aafde
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
15 changes: 12 additions & 3 deletions db-connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -1691,7 +1691,7 @@ func GetAllWorkflowAppAuth(ctx context.Context, orgId string) ([]AppAuthenticati
return allworkflowappAuths, nil
}
} else {
log.Printf("[DEBUG] Failed getting cache for app auth: %s", err)
//log.Printf("[DEBUG] Failed getting cache for app auth: %s", err)
}
}

Expand All @@ -1706,6 +1706,7 @@ func GetAllWorkflowAppAuth(ctx context.Context, orgId string) ([]AppAuthenticati
},
},
}

if err := json.NewEncoder(&buf).Encode(query); err != nil {
log.Printf("[WARNING] Error encoding find user query: %s", err)
return allworkflowappAuths, err
Expand Down Expand Up @@ -1781,9 +1782,15 @@ func GetAllWorkflowAppAuth(ctx context.Context, orgId string) ([]AppAuthenticati
log.Printf("[WARNING] Failed updating get app auth cache: %s", err)
}

log.Printf("[DEBUG] Set cache for app auth %s", cacheKey)
log.Printf("[DEBUG] Set cache for app auth %s with length %d", cacheKey, len(allworkflowappAuths))
}

//for _, env := range allworkflowappAuths {
// for _, param := range env.Fields {
// log.Printf("ENV: %#v", param)
// }
//}

return allworkflowappAuths, nil
}

Expand Down Expand Up @@ -2755,7 +2762,9 @@ func SetWorkflowAppAuthDatastore(ctx context.Context, workflowappauth AppAuthent
}
}

cacheKey := fmt.Sprintf("%s_%s", nameKey, workflowappauth.OrgId)
cacheKey := fmt.Sprintf("%s_%s", nameKey, id)
DeleteCache(ctx, cacheKey)
cacheKey = fmt.Sprintf("%s_%s", nameKey, workflowappauth.OrgId)
DeleteCache(ctx, cacheKey)

return nil
Expand Down
34 changes: 28 additions & 6 deletions shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,12 @@ func GetAppAuthentication(resp http.ResponseWriter, request *http.Request) {
return
}

//for _, env := range allworkflowappAuths {
// for _, param := range env.Fields {
// log.Printf("ENV: %#v", param)
// }
//}

// Cleanup for frontend
newAuth := []AppAuthenticationStorage{}
for _, auth := range allAuths {
Expand All @@ -406,18 +412,28 @@ func GetAppAuthentication(resp http.ResponseWriter, request *http.Request) {
newAuth = append(newAuth, newAuthField)
}

newbody, err := json.Marshal(allAuths)
type returnStruct struct {
Success bool `json:"success"`
Data []AppAuthenticationStorage `json:"data"`
}

allAuth := returnStruct{
Success: true,
Data: allAuths,
}

newbody, err := json.Marshal(allAuth)
if err != nil {
log.Printf("Failed unmarshalling all app auths: %s", err)
resp.WriteHeader(401)
resp.Write([]byte(fmt.Sprintf(`{"success": false, "reason": "Failed unpacking workflow app auth"}`)))
return
}

data := fmt.Sprintf(`{"success": true, "data": %s}`, string(newbody))
//data := fmt.Sprintf(`{"success": true, "data": %s}`, string(newbody))

resp.WriteHeader(200)
resp.Write([]byte(data))
resp.Write([]byte(newbody))

}

Expand Down Expand Up @@ -616,8 +632,13 @@ func DeleteAppAuthentication(resp http.ResponseWriter, request *http.Request) {
}

ctx := getContext(request)
nameKey := "workflowappauth"
auth, err := GetWorkflowAppAuthDatastore(ctx, fileId)
if err != nil {
// Deleting cache here, as it seems to be a constant issue
cacheKey := fmt.Sprintf("%s_%s", nameKey, user.ActiveOrg.Id)
DeleteCache(ctx, cacheKey)

log.Printf("[WARNING] Authget error (DELETE): %s", err)
resp.WriteHeader(401)
resp.Write([]byte(`{"success": false, "reason": ":("}`))
Expand All @@ -634,7 +655,6 @@ func DeleteAppAuthentication(resp http.ResponseWriter, request *http.Request) {
// 1. Get the auth
// 2. Loop the workflows (.Usage) and set them to have errors
// 3. Loop the nodes in workflows and do the same
nameKey := "workflowappauth"
err = DeleteKey(ctx, nameKey, fileId)
if err != nil {
log.Printf("Failed deleting workflowapp")
Expand All @@ -645,6 +665,8 @@ func DeleteAppAuthentication(resp http.ResponseWriter, request *http.Request) {

cacheKey := fmt.Sprintf("%s_%s", nameKey, user.ActiveOrg.Id)
DeleteCache(ctx, cacheKey)
cacheKey = fmt.Sprintf("%s_%s", nameKey, fileId)
DeleteCache(ctx, cacheKey)

resp.WriteHeader(200)
resp.Write([]byte(`{"success": true}`))
Expand Down Expand Up @@ -2451,7 +2473,7 @@ func SaveWorkflow(resp http.ResponseWriter, request *http.Request) {
oldworkflow.IsValid = false
err = SetWorkflow(ctx, *oldworkflow, fileId)
if err != nil {
log.Printf("Failed saving workflow to database: %s", err)
log.Printf("[WARNING] Failed saving workflow to database: %s", err)
if workflow.PreviouslySaved {
resp.WriteHeader(401)
resp.Write([]byte(`{"success": false}`))
Expand Down Expand Up @@ -3273,7 +3295,7 @@ func HandlePasswordChange(resp http.ResponseWriter, request *http.Request) {
}

if !orgFound {
log.Printf("[INFO] User %s is admin, but can't change user's passowrd outside their own org.", userInfo.Id)
log.Printf("[INFO] User %s is admin, but can't change user's password outside their own org.", userInfo.Id)
resp.WriteHeader(401)
resp.Write([]byte(fmt.Sprintf(`{"success": false, "reason": "Can't change users outside your org."}`)))
return
Expand Down

0 comments on commit 12aafde

Please sign in to comment.