Skip to content

Commit 12efd7b

Browse files
Improve notification workflow and API key handling.
1 parent 8b6344f commit 12efd7b

File tree

1 file changed

+56
-20
lines changed

1 file changed

+56
-20
lines changed

notifications.go

Lines changed: 56 additions & 20 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
@@ -733,27 +734,38 @@ 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("[WARNING] Error getting parent org %s in createOrgNotification: %s. Keeping authOrg as current org.", org.CreatorOrg, err)
740+
} else if parentOrg == nil {
741+
log.Printf("[WARNING] Parent org %s returned nil. Keeping authOrg as current org.", org.CreatorOrg)
742+
} else {
743+
authOrg = parentOrg
744+
org.Defaults.NotificationWorkflow = parentOrg.Defaults.NotificationWorkflow
742745
}
743-
744-
// Overwriting to make sure access rights are correct
745-
authOrg = parentOrg
746-
org.Defaults.NotificationWorkflow = parentOrg.Defaults.NotificationWorkflow
747746
}
748747

749748
for _, user := range authOrg.Users {
750749
if user.Role == "admin" && len(user.Id) > 0 && len(selectedApikey) == 0 {
751750
// Checking if it's the right active org
752751
// FIXME: Should it need to be in the active org? Shouldn't matter? :thinking:
753752
foundUser, err := GetUser(ctx, user.Id)
754-
if err == nil && len(foundUser.ApiKey) > 0 {
755-
selectedApikey = foundUser.ApiKey
756-
break
753+
if err == nil {
754+
if len(foundUser.ApiKey) > 0 {
755+
selectedApikey = foundUser.ApiKey
756+
break
757+
} else {
758+
// Admin exists but has no API key - auto-generate one
759+
log.Printf("[INFO] Admin user %s (%s) has no API key. Auto-generating one.", foundUser.Username, foundUser.Id)
760+
generatedUser, genErr := GenerateApikey(ctx, *foundUser)
761+
if genErr != nil {
762+
log.Printf("[ERROR] Failed to auto-generate API key for user %s: %s", foundUser.Username, genErr)
763+
continue // Try next admin
764+
}
765+
selectedApikey = generatedUser.ApiKey
766+
log.Printf("[INFO] Successfully auto-generated API key for admin %s", foundUser.Username)
767+
break
768+
}
757769
}
758770
}
759771
}
@@ -803,8 +815,9 @@ func CreateOrgNotification(ctx context.Context, title, description, referenceUrl
803815
if mainNotification.Ignored {
804816
log.Printf("[INFO] Ignored notification %s for %s", mainNotification.Title, mainNotification.UserId)
805817
} else {
818+
authOrgValue := *authOrg
806819
go func() {
807-
err = sendToNotificationWorkflow(ctx, mainNotification, selectedApikey, org.Defaults.NotificationWorkflow, false, *authOrg)
820+
err = sendToNotificationWorkflow(ctx, mainNotification, selectedApikey, org.Defaults.NotificationWorkflow, false, authOrgValue)
808821
if err != nil {
809822
if !strings.Contains(err.Error(), "cache stored") && !strings.Contains(err.Error(), "Same workflow") {
810823
log.Printf("[ERROR] Failed sending notification to workflowId %s for reference %s (2): %s", org.Defaults.NotificationWorkflow, mainNotification.Id, err)
@@ -846,9 +859,22 @@ func CreateOrgNotification(ctx context.Context, title, description, referenceUrl
846859
for _, user := range filteredUsers {
847860
if user.Role == "admin" && len(selectedApikey) == 0 {
848861
foundUser, err := GetUser(ctx, user.Id)
849-
if err == nil && len(foundUser.ApiKey) > 0 {
850-
selectedApikey = foundUser.ApiKey
851-
break
862+
if err == nil {
863+
if len(foundUser.ApiKey) > 0 {
864+
selectedApikey = foundUser.ApiKey
865+
break
866+
} else {
867+
// Admin exists but has no API key - auto-generate one
868+
log.Printf("[INFO] Admin user %s (%s) has no API key. Auto-generating one.", foundUser.Username, foundUser.Id)
869+
generatedUser, genErr := GenerateApikey(ctx, *foundUser)
870+
if genErr != nil {
871+
log.Printf("[ERROR] Failed to auto-generate API key for user %s: %s", foundUser.Username, genErr)
872+
continue // Try next admin
873+
}
874+
selectedApikey = generatedUser.ApiKey
875+
log.Printf("[INFO] Successfully auto-generated API key for admin %s", foundUser.Username)
876+
break
877+
}
852878
}
853879
}
854880
}
@@ -888,8 +914,9 @@ func CreateOrgNotification(ctx context.Context, title, description, referenceUrl
888914
}
889915
}
890916

917+
authOrgValue := *authOrg
891918
go func() {
892-
err = sendToNotificationWorkflow(ctx, mainNotification, selectedApikey, org.Defaults.NotificationWorkflow, false, *authOrg)
919+
err = sendToNotificationWorkflow(ctx, mainNotification, selectedApikey, org.Defaults.NotificationWorkflow, false, authOrgValue)
893920
if err != nil {
894921
log.Printf("[ERROR] Failed sending notification to workflowId %s for reference %s: %s", org.Defaults.NotificationWorkflow, mainNotification.Id, err)
895922
}
@@ -906,6 +933,15 @@ func HandleCreateNotification(resp http.ResponseWriter, request *http.Request) {
906933
return
907934
}
908935

936+
if project.Environment == "cloud" {
937+
gceProject := os.Getenv("SHUFFLE_GCEPROJECT")
938+
if gceProject != "shuffler" && gceProject != sandboxProject && len(gceProject) > 0 {
939+
log.Printf("[DEBUG] Redirecting notification request to main site handler (shuffler.io)")
940+
RedirectUserRequest(resp, request)
941+
return
942+
}
943+
}
944+
909945
// Unmarshal body to the Notification struct
910946
// Done first so we can use the data for auth
911947
body, err := ioutil.ReadAll(request.Body)
@@ -1092,8 +1128,8 @@ func HandleCreateNotification(resp http.ResponseWriter, request *http.Request) {
10921128
}
10931129

10941130
found := false
1095-
for _, user := range org.Users {
1096-
if user.Id == user.Id {
1131+
for _, orgUser := range org.Users {
1132+
if orgUser.Id == user.Id {
10971133
found = true
10981134
break
10991135
}
@@ -1108,7 +1144,7 @@ func HandleCreateNotification(resp http.ResponseWriter, request *http.Request) {
11081144
}
11091145
}
11101146

1111-
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)
1147+
log.Printf("[DEBUG] User '%s' (%s) from org '%s' (%s) is creating notification '%s' for org %s", user.Username, user.Id, user.ActiveOrg.Name, user.ActiveOrg.Id, notification.Title, orgId)
11121148
err = CreateOrgNotification(
11131149
ctx,
11141150
notification.Title,

0 commit comments

Comments
 (0)