diff --git a/keystone/notifications.py b/keystone/notifications.py index 2beec8227c..fb2a5f543d 100644 --- a/keystone/notifications.py +++ b/keystone/notifications.py @@ -145,7 +145,8 @@ class Audit(object): operation, resource_type, resource_id, - actor_dict, + initiator=initiator, + actor_dict=actor_dict, public=public) if CONF.notification_format == 'cadf' and public: @@ -444,8 +445,8 @@ def _create_cadf_payload(operation, resource_type, resource_id, target, event_type, reason=reason, **audit_kwargs) -def _send_notification(operation, resource_type, resource_id, actor_dict=None, - public=True): +def _send_notification(operation, resource_type, resource_id, initiator=None, + actor_dict=None, public=True): """Send notification to inform observers about the affected resource. This method doesn't raise an exception when sending the notification fails. @@ -453,6 +454,7 @@ def _send_notification(operation, resource_type, resource_id, actor_dict=None, :param operation: operation being performed (created, updated, or deleted) :param resource_type: type of resource being operated on :param resource_id: ID of resource being operated on + :param initiator: representation of the user that created the request :param actor_dict: a dictionary containing the actor's ID and type :param public: if True (default), the event will be sent to the notifier API. @@ -466,6 +468,12 @@ def _send_notification(operation, resource_type, resource_id, actor_dict=None, payload['actor_type'] = actor_dict['type'] payload['actor_operation'] = actor_dict['actor_operation'] + if initiator: + payload['request_id'] = initiator.request_id + global_request_id = getattr(initiator, 'global_request_id', None) + if global_request_id: + payload['global_request_id'] = global_request_id + notify_event_callbacks(SERVICE, resource_type, operation, payload) # Only send this notification if the 'basic' format is used, otherwise diff --git a/keystone/tests/unit/common/test_notifications.py b/keystone/tests/unit/common/test_notifications.py index b7a6ead05f..ec3ba54cdb 100644 --- a/keystone/tests/unit/common/test_notifications.py +++ b/keystone/tests/unit/common/test_notifications.py @@ -244,12 +244,13 @@ class BaseNotificationTest(test_v3.RestfulTestCase): self._notifications = [] self._audits = [] - def fake_notify(operation, resource_type, resource_id, + def fake_notify(operation, resource_type, resource_id, initiator=None, actor_dict=None, public=True): note = { 'resource_id': resource_id, 'operation': operation, 'resource_type': resource_type, + 'initiator': initiator, 'send_notification_called': True, 'public': public} if actor_dict: @@ -364,7 +365,8 @@ class BaseNotificationTest(test_v3.RestfulTestCase): 'send_notification_called': True, 'public': public} for note in self._notifications: - if expected == note: + # compare only expected fields + if all(note.get(k) == v for k, v in expected.items()): break else: self.fail("Notification not sent.") @@ -717,6 +719,30 @@ class NotificationsForEntities(BaseNotificationTest): actor_id=user_ref['id'], actor_type='user', actor_operation='removed') + def test_initiator_request_id(self): + ref = unit.new_domain_ref() + self.post('/domains', body={'domain': ref}) + note = self._notifications[-1] + initiator = note['initiator'] + self.assertIsNotNone(initiator.request_id) + + def test_initiator_global_request_id(self): + global_request_id = 'req-%s' % uuid.uuid4() + ref = unit.new_domain_ref() + self.post('/domains', body={'domain': ref}, + headers={'X-OpenStack-Request-Id': global_request_id}) + note = self._notifications[-1] + initiator = note['initiator'] + self.assertEqual( + initiator.global_request_id, global_request_id) + + def test_initiator_global_request_id_not_set(self): + ref = unit.new_domain_ref() + self.post('/domains', body={'domain': ref}) + note = self._notifications[-1] + initiator = note['initiator'] + self.assertFalse(hasattr(initiator, 'global_request_id')) + class CADFNotificationsForPCIDSSEvents(BaseNotificationTest): diff --git a/releasenotes/notes/bug-1801095-6e28d7a86719da74.yaml b/releasenotes/notes/bug-1801095-6e28d7a86719da74.yaml index e9d260c54f..4ebc7bc9f3 100644 --- a/releasenotes/notes/bug-1801095-6e28d7a86719da74.yaml +++ b/releasenotes/notes/bug-1801095-6e28d7a86719da74.yaml @@ -2,4 +2,5 @@ features: - > [`bug 1801095 `_] - Request ID and global request ID have been added to CADF notifications. + Request ID and global request ID have been added to both basic and CADF + notifications.