From 385b92efdc083fd134da4470ad1e74783fe4e721 Mon Sep 17 00:00:00 2001 From: Tit Petric Date: Wed, 16 Aug 2023 14:01:27 +0200 Subject: [PATCH] Clean up/deduplicate logging --- gateway/api.go | 35 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/gateway/api.go b/gateway/api.go index cde238ab3159..3a8cca834231 100644 --- a/gateway/api.go +++ b/gateway/api.go @@ -320,20 +320,22 @@ func (gw *Gateway) doAddOrUpdate(keyName string, newSession *user.SessionState, newSession.LastUpdated = strconv.Itoa(int(time.Now().Unix())) } - if len(newSession.AccessRights) > 0 { - _, found := gw.GlobalSessionManager.SessionDetail(newSession.OrgID, keyName, isHashed) - if !found { - log.WithFields(logrus.Fields{ + logger := log.WithFields(logrus.Fields{ "prefix": "api", - "key": keyName, + "key": gw.obfuscateKey(keyName), "org_id": newSession.OrgID, - "api_id": apiId, + "expires": newSession.Expires, + "api_id": "--", "user_id": "system", "user_ip": "--", "path": "--", "server_name": "system", - }).Warn("API inactive or doesn't exist.") + }) + if len(newSession.AccessRights) > 0 { + _, found := gw.GlobalSessionManager.SessionDetail(newSession.OrgID, keyName, isHashed) + if !found { + logger.Warn("API inactive or doesn't exist.") return errors.New("API must be active to add keys") } @@ -343,6 +345,9 @@ func (gw *Gateway) doAddOrUpdate(keyName string, newSession *user.SessionState, for apiId := range newSession.AccessRights { apiSpec := gw.getApiSpec(apiId) if apiSpec == nil { + logger.WithField("api_id", apiId).Warn("Can't find active API, falling back to defaults") + + // Fill APISpec with some defaults, asuming the API ID is // referencing an inactive API. As long as the session // detail exists, we can apply the policies. @@ -371,10 +376,10 @@ func (gw *Gateway) doAddOrUpdate(keyName string, newSession *user.SessionState, } else { // nothing defined, add key to ALL if !gw.GetConfig().AllowMasterKeys { - log.Error("Master keys disallowed in configuration, key not added.") + logger.Error("Master keys disallowed in configuration, key not added.") return errors.New("Master keys not allowed") } - log.Warning("No API Access Rights set, adding key to ALL.") + logger.Warning("No API Access Rights set, adding key to ALL.") gw.apisMu.RLock() defer gw.apisMu.RUnlock() @@ -391,17 +396,7 @@ func (gw *Gateway) doAddOrUpdate(keyName string, newSession *user.SessionState, } } - log.WithFields(logrus.Fields{ - "prefix": "api", - "key": gw.obfuscateKey(keyName), - "expires": newSession.Expires, - "org_id": newSession.OrgID, - "api_id": "--", - "user_id": "system", - "user_ip": "--", - "path": "--", - "server_name": "system", - }).Info("Key added or updated.") + logger.Info("Key added or updated.") return nil }