Skip to content

Commit

Permalink
Clean up/deduplicate logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Tit Petric committed Aug 16, 2023
1 parent 140adeb commit 385b92e
Showing 1 changed file with 15 additions and 20 deletions.
35 changes: 15 additions & 20 deletions gateway/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
})

Check failure

Code scanning / CodeQL

Log entries created from user input High

This log entry depends on a
user-provided value
.
This log entry depends on a
user-provided value
.
This log entry depends on a
user-provided value
.

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")
}

Expand All @@ -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.
Expand Down Expand Up @@ -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()

Expand All @@ -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
}

Expand Down

0 comments on commit 385b92e

Please sign in to comment.