Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix project_id handling for RequestContext objects #102

Merged
merged 3 commits into from
Nov 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 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', {})

# Get the context - could be dict or RequestContext object
context = adhoc_attrs.get('context', {})
original_resources = context.get('original_resources', [])

# Handle both dict and RequestContext object types
original_resources = []
if hasattr(context, 'original_resources'):
original_resources = context.original_resources
else:
original_resources = context.get('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