Skip to content

Commit f7a4f5e

Browse files
authored
Merge pull request #259 from satti-hari-krishna-reddy/notification-auth-fix
Improve notification workflow and API key handling.
2 parents 488244f + cd84035 commit f7a4f5e

File tree

1 file changed

+40
-47
lines changed

1 file changed

+40
-47
lines changed

notifications.go

Lines changed: 40 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"encoding/json"
99
"errors"
1010
"fmt"
11-
"github.com/satori/go.uuid"
1211
"io/ioutil"
1312
"log"
1413
"net/http"
@@ -17,6 +16,8 @@ import (
1716
"strconv"
1817
"strings"
1918
"time"
19+
20+
uuid "github.com/satori/go.uuid"
2021
)
2122

2223
// Standalone to make it work many places
@@ -626,13 +627,13 @@ func forwardNotificationRequest(ctx context.Context, title, description, referen
626627
func CreateOrgNotification(ctx context.Context, title, description, referenceUrl, orgId string, adminsOnly bool) error {
627628
if len(orgId) == 0 {
628629
log.Printf("[ERROR] No org ID provided to create notification '%s'", title)
629-
return errors.New("No org ID provided")
630+
return errors.New("no org ID provided")
630631
}
631632

632633
// Since we use a static workflow name, this should be effective.
633634
if strings.Contains(title, "Ops Dashboard Workflow") {
634635
log.Printf("[INFO] Skipping create notification for health check workflow")
635-
return errors.New("Health check workflow detected")
636+
return errors.New("health check workflow detected")
636637
}
637638

638639
if project.Environment == "" {
@@ -733,29 +734,43 @@ func CreateOrgNotification(ctx context.Context, title, description, referenceUrl
733734

734735
authOrg := org
735736
if org.Defaults.NotificationWorkflow == "parent" && org.CreatorOrg != "" {
736-
//log.Printf("[DEBUG] Sending notification to parent org %s' notification workflow", org.CreatorOrg)
737-
738737
parentOrg, err := GetOrg(ctx, org.CreatorOrg)
739738
if err != nil {
740-
log.Printf("[WARNING] Error getting parent org %s in createOrgNotification: %s. This is usually a region problem, and may cause issues with notification workflows..", orgId, err)
741-
//return err
739+
log.Printf("[ERROR] Failed to get required parent org %s: %s", org.CreatorOrg, err)
740+
return err
741+
}
742+
if parentOrg == nil {
743+
log.Printf("[ERROR] Required parent org %s not found", org.CreatorOrg)
744+
return errors.New("parent org not found")
742745
}
743-
744-
// Overwriting to make sure access rights are correct
745746
authOrg = parentOrg
746747
org.Defaults.NotificationWorkflow = parentOrg.Defaults.NotificationWorkflow
747748
}
748749

749750
for _, user := range authOrg.Users {
750-
if user.Role == "admin" && len(user.Id) > 0 && len(selectedApikey) == 0 {
751-
// Checking if it's the right active org
752-
// FIXME: Should it need to be in the active org? Shouldn't matter? :thinking:
753-
foundUser, err := GetUser(ctx, user.Id)
754-
if err == nil && len(foundUser.ApiKey) > 0 {
755-
selectedApikey = foundUser.ApiKey
756-
break
751+
if user.Role == "org-reader" || user.Id == "" {
752+
continue
753+
}
754+
755+
foundUser, err := GetUser(ctx, user.Id)
756+
if err != nil {
757+
continue
758+
}
759+
760+
apiKey := foundUser.ApiKey
761+
762+
// if user has no API key, generate one
763+
if apiKey == "" {
764+
generatedUser, genErr := GenerateApikey(ctx, *foundUser)
765+
if genErr != nil {
766+
log.Printf("[ERROR] Failed to auto-generate API key for user %s: %s", foundUser.Username, genErr)
767+
continue
757768
}
769+
apiKey = generatedUser.ApiKey
758770
}
771+
772+
selectedApikey = apiKey
773+
break
759774
}
760775

761776
if len(matchingNotifications) > 0 {
@@ -803,8 +818,9 @@ func CreateOrgNotification(ctx context.Context, title, description, referenceUrl
803818
if mainNotification.Ignored {
804819
log.Printf("[INFO] Ignored notification %s for %s", mainNotification.Title, mainNotification.UserId)
805820
} else {
821+
authOrgValue := *authOrg
806822
go func() {
807-
err = sendToNotificationWorkflow(ctx, mainNotification, selectedApikey, org.Defaults.NotificationWorkflow, false, *authOrg)
823+
err = sendToNotificationWorkflow(ctx, mainNotification, selectedApikey, org.Defaults.NotificationWorkflow, false, authOrgValue)
808824
if err != nil {
809825
if !strings.Contains(err.Error(), "cache stored") && !strings.Contains(err.Error(), "Same workflow") {
810826
log.Printf("[ERROR] Failed sending notification to workflowId %s for reference %s (2): %s", org.Defaults.NotificationWorkflow, mainNotification.Id, err)
@@ -831,34 +847,10 @@ func CreateOrgNotification(ctx context.Context, title, description, referenceUrl
831847

832848
//NotificationWorkflow string `json:"notification_workflow" datastore:"notification_workflow"`
833849

834-
filteredUsers := []User{}
835-
if adminsOnly == false {
836-
filteredUsers = org.Users
837-
} else {
838-
for _, user := range org.Users {
839-
if user.Role == "admin" {
840-
filteredUsers = append(filteredUsers, user)
841-
}
842-
}
843-
}
844-
845-
selectedApikey := ""
846-
for _, user := range filteredUsers {
847-
if user.Role == "admin" && len(selectedApikey) == 0 {
848-
foundUser, err := GetUser(ctx, user.Id)
849-
if err == nil && len(foundUser.ApiKey) > 0 {
850-
selectedApikey = foundUser.ApiKey
851-
break
852-
}
853-
}
854-
}
855-
856850
if len(org.Defaults.NotificationWorkflow) > 0 {
857851
if len(selectedApikey) == 0 {
858-
log.Printf("[ERROR] Didn't find an apikey to use when sending notifications for org %s to workflow %s", org.Id, org.Defaults.NotificationWorkflow)
859-
if debug {
860-
log.Printf("\n\n\n")
861-
}
852+
log.Printf("[ERROR] No API key available to trigger notification workflow for org %s to workflow %s", org.Id, org.Defaults.NotificationWorkflow)
853+
return errors.New("no API key available for notification workflow")
862854
}
863855

864856
workflow, err := GetWorkflow(ctx, org.Defaults.NotificationWorkflow)
@@ -888,8 +880,9 @@ func CreateOrgNotification(ctx context.Context, title, description, referenceUrl
888880
}
889881
}
890882

883+
authOrgValue := *authOrg
891884
go func() {
892-
err = sendToNotificationWorkflow(ctx, mainNotification, selectedApikey, org.Defaults.NotificationWorkflow, false, *authOrg)
885+
err = sendToNotificationWorkflow(ctx, mainNotification, selectedApikey, org.Defaults.NotificationWorkflow, false, authOrgValue)
893886
if err != nil {
894887
log.Printf("[ERROR] Failed sending notification to workflowId %s for reference %s: %s", org.Defaults.NotificationWorkflow, mainNotification.Id, err)
895888
}
@@ -1092,8 +1085,8 @@ func HandleCreateNotification(resp http.ResponseWriter, request *http.Request) {
10921085
}
10931086

10941087
found := false
1095-
for _, user := range org.Users {
1096-
if user.Id == user.Id {
1088+
for _, orgUser := range org.Users {
1089+
if orgUser.Id == user.Id {
10971090
found = true
10981091
break
10991092
}
@@ -1107,7 +1100,7 @@ func HandleCreateNotification(resp http.ResponseWriter, request *http.Request) {
11071100
}
11081101
}
11091102
}
1110-
1103+
11111104
log.Printf("[DEBUG] User '%s' (%s) in org '%s' (%s) is creating notification '%s'", user.Username, user.Id, user.ActiveOrg.Name, user.ActiveOrg.Id, notification.Title)
11121105
err = CreateOrgNotification(
11131106
ctx,

0 commit comments

Comments
 (0)