From dffd8a7693dc26f8f4da10815cde4d7096805ad7 Mon Sep 17 00:00:00 2001
From: Jan Semmelink <jan@uafrica.com>
Date: Mon, 15 Nov 2021 11:08:24 +0200
Subject: [PATCH] Update missing claim info in api-logs

---
 logs/api-logs.go | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/logs/api-logs.go b/logs/api-logs.go
index abbad45..da4c1b5 100644
--- a/logs/api-logs.go
+++ b/logs/api-logs.go
@@ -3,11 +3,13 @@ package logs
 import (
 	"net/http"
 	"sort"
+	"strconv"
 	"strings"
 	"time"
 
 	"github.com/aws/aws-lambda-go/events"
 	"gitlab.com/uafrica/go-utils/errors"
+	"gitlab.com/uafrica/go-utils/logger"
 	"gitlab.com/uafrica/go-utils/queues"
 )
 
@@ -25,6 +27,7 @@ func LogIncomingAPIRequest(startTime time.Time, requestID string, claim map[stri
 	}
 
 	//todo: filter out some noisy (method+path)
+	logger.Debugf("claim: %+v", claim)
 
 	endTime := time.Now()
 
@@ -44,8 +47,15 @@ func LogIncomingAPIRequest(startTime time.Time, requestID string, claim map[stri
 		}
 	}
 
-	username, _ := claim["user_id"].(string)
-	accountID, _ := claim["account_id"].(int64)
+	username, _ := claim["Username"].(string)
+	accountID, _ := claim["AccountID"].(int64)
+	if accountID == 0 {
+		if accountIDParam, ok := req.QueryStringParameters["account_id"]; ok {
+			if i64, err := strconv.ParseInt(accountIDParam, 10, 64); err == nil && i64 > 0 {
+				accountID = i64
+			}
+		}
+	}
 	apiLog := ApiLog{
 		StartTime:           startTime,
 		EndTime:             endTime,
@@ -93,6 +103,8 @@ func LogIncomingAPIRequest(startTime time.Time, requestID string, claim map[stri
 		apiLog.Response.Body = "<not logged>"
 	}
 
+	logger.Debugf("Send api-log to SQS: %+v", apiLog)
+
 	//todo: filter out sensitive values (e.g. OTP)
 	if _, err := producer.NewEvent("API_LOGS").
 		Type("api-log").
-- 
GitLab