def test_command_with_response_should_be_logged_with_parent_data(self, user): responses.add(responses.POST, 'http://localhost/test', body='test') with log_with_data(related_objects=[user], slug='TEST', extra_data={'test': 'test'}): with capture_security_logs() as logged_data: test_call_command('test_command_with_response') command_logger = logged_data.command[0] output_request_logger = logged_data.output_request[0] assert_equal(output_request_logger._get_parent_with_id(), command_logger) assert_equal(command_logger.slug, 'TEST') assert_equal(command_logger.related_objects, {get_object_triple(user)}) assert_equal(command_logger.extra_data, {'test': 'test'}) assert_equal(output_request_logger.slug, 'TEST') assert_equal(output_request_logger.related_objects, {get_object_triple(user)}) assert_equal(output_request_logger.extra_data, {'test': 'test'})
def test_input_logged_request_should_have_set_data(self, user): with capture_security_logs() as logged_data: assert_http_redirect(self.post('/admin/login/', data={'username': '******', 'password': '******'})) assert_http_ok(self.get('/home/')) assert_equal(len(logged_data.input_request[1].related_objects), 1) assert_equal(list(logged_data.input_request[1].related_objects)[0], get_object_triple(user)) assert_equal(logged_data.input_request_finished[1].slug, 'user-home')
def test_output_logged_request_should_be_related_with_object(self, user): with capture_security_logs() as logged_data: assert_http_redirect(self.post('/admin/login/', data={'username': '******', 'password': '******'})) responses.add(responses.GET, 'http://localhost', body='test') assert_equal(self.get('/proxy/?url=http://localhost').content, b'test') assert_equal(len(logged_data.output_request[0].related_objects), 1) assert_equal(list(logged_data.output_request[0].related_objects)[0], get_object_triple(user))
def get_logs_related_with_object(self, logger_name, related_object): if not capture_security_logs.logged_data: return [] return [ logger for logger in capture_security_logs.logged_data.get( logger_name.replace('-', '_')) if get_object_triple(related_object) in logger.related_objects ]
def test_input_request_to_homepage_should_be_logged_in_elasticsearch_backend_through_logstash(self, user): with log_with_data(related_objects=[user]): with capture_security_logs() as logged_data: with self.assertLogs('security.logstash', level='INFO') as cm: assert_http_ok(self.get('/home/?name=value')) input_request_log = logged_data.input_request[0] assert_equal(len(cm.output), 2) request_log, response_log = cm.output request_log_expected_data = { 'slug': None, 'release': None, 'related_objects': ['|'.join(str(v) for v in get_object_triple(user))], 'extra_data': {}, 'parent_log': None, 'request_headers': '{"Cookie": "[Filtered]"}', 'request_body': '', 'user_id': None, 'method': 'GET', 'host': 'testserver', 'path': '/home/', 'queries': '{"name": "value"}', 'is_secure': False, 'ip': '127.0.0.1', 'start': not_none_eq_obj, 'view_slug': 'home', 'state': 'INCOMPLETE' } response_log_expected_data = { **request_log_expected_data, 'state': 'INFO', 'stop': not_none_eq_obj, 'time': not_none_eq_obj, 'response_body': 'home page response', 'response_code': 200, 'response_headers': '{"Content-Type": "text/html; charset=utf-8", ''"X-Frame-Options": "DENY"}', } assert_equal_logstash( request_log, 'security-input-request-log', 0, input_request_log.id, request_log_expected_data ) assert_equal_logstash( response_log, 'security-input-request-log', 9999, input_request_log.id, response_log_expected_data )
def test_error_command_should_be_logged_in_elasticsearch_backend_through_logstash(self, user): with capture_security_logs() as logged_data: with log_with_data(related_objects=[user]): with self.assertLogs('security.logstash', level='INFO') as cm: with assert_raises(RuntimeError): test_call_command('test_error_command') command_log = logged_data.command[0] assert_equal(len(cm.output), 2) start_log, error_log = cm.output start_log_expected_data = { 'slug': None, 'release': None, 'related_objects': ['|'.join(str(v) for v in get_object_triple(user))], 'extra_data': {}, 'parent_log': None, 'name': 'test_error_command', 'input': '', 'is_executed_from_command_line': False, 'start': not_none_eq_obj, 'state': 'ACTIVE' } error_log_expected_data = { **start_log_expected_data, 'stop': not_none_eq_obj, 'error_message': not_none_eq_obj, 'state': 'FAILED', 'time': not_none_eq_obj } assert_equal_logstash( start_log, 'security-command-log', 0, command_log.id, start_log_expected_data ) assert_equal_logstash( error_log, 'security-command-log', 9999, command_log.id, error_log_expected_data )
def add_related_objects(self, *related_objects): self.related_objects |= set( get_object_triple(obj) for obj in related_objects)
def test_slug_and_related_data_should_be_send_to_input_request_logger(self, user): with log_with_data(related_objects=[user], slug='TEST'): with capture_security_logs() as logged_data: assert_http_ok(self.get('/home/')) assert_equal(logged_data.input_request[0].related_objects, {get_object_triple(user)}) assert_equal(logged_data.input_request[0].slug, 'TEST')