Ignore JSON decode error in request log.

For DELETE requests with no request body or "Content-Length" field, the
expected return value for "hasattr(rest_state.request, 'json')" would be
False and for "rest_state.request.json" would be an empty string.
Instead, what occurs is that both "hasattr(rest_state.request, 'json')"
and "rest_state.request.json" are throwing JSON decode exceptions in
this case. This change adds tolerance for this exception. This is done
because it is not known all the possible request conditions that may
cause this exception to be thrown.

Test Plan:

PASS:  Send a delete request through the CLI to subcloud-group and
       verify if any exception shows up dcmanager-api logs.

Closes-Bug: 1999901
Change-Id: I58af3f4711d6caf840e188fb4690f2bde219d79a
Signed-off-by: Karla Felix <karla.karolinenogueirafelix@windriver.com>
This commit is contained in:
Karla Felix 2022-12-15 07:30:47 -03:00
parent 59bf9dca2e
commit b056a514c8
1 changed files with 5 additions and 3 deletions

View File

@ -211,10 +211,12 @@ class AuditLoggingHook(hooks.PecanHook):
def json_post_data(rest_state):
if 'form-data' in rest_state.request.headers.get('Content-Type'):
return " POST: {}".format(rest_state.request.params)
if not hasattr(rest_state.request, 'json'):
try:
if not hasattr(rest_state.request, 'json'):
return ""
return " POST: {}".format(rest_state.request.json)
except Exception:
return ""
return " POST: {}".format(rest_state.request.json)
# Filter password from log
filtered_json = re.sub(r'{[^{}]*(passwd_hash|community|password)[^{}]*},*',
'',