Skip to content

Commit

Permalink
Fix project_id handling for RequestContext objects
Browse files Browse the repository at this point in the history
  • Loading branch information
notque committed Nov 15, 2024
1 parent bf61780 commit 3621936
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions auditmiddleware/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,13 +520,25 @@ def _create_cadf_event(self, project, res_spec, res_id, res_parent_id,
if not action:
return None

# Try to get project_id from request headers
project_id = request.environ.get('HTTP_X_PROJECT_ID')
# If project_id is undefined, look for another variable. This is
# added specific to catching delete events from Neutron

# If no project_id found, try to get it from the request context
if project_id is None:
# Get the adhoc attributes from the request environment
adhoc_attrs = request.environ.get('webob.adhoc_attrs', {})
context = adhoc_attrs.get('context', {})
original_resources = context.get('original_resources', [])

# Get the context - could be dict or RequestContext object
context = adhoc_attrs.get('context')

# Handle both dict and RequestContext object types
original_resources = None
if isinstance(context, dict):
original_resources = context.get('original_resources', [])
elif hasattr(context, 'original_resources'):
original_resources = context.original_resources

# If we found original_resources and it's a list, get project_id
if original_resources and isinstance(original_resources, list):
first_resource = original_resources[0]
if isinstance(first_resource, dict):
Expand Down

0 comments on commit 3621936

Please sign in to comment.