diff --git a/auditmiddleware/_api.py b/auditmiddleware/_api.py index 2fa3a82..2cf97a4 100644 --- a/auditmiddleware/_api.py +++ b/auditmiddleware/_api.py @@ -284,6 +284,10 @@ def _build_events(self, target_project, res_spec, res_id, res_parent_id, path: URL path being parsed cursor: current position in the path as it is parsed """ + self._log.debug("_build_events: path=%(path)s, cursor=%(cursor)s, " + "res_spec=%(res_spec)s, res_id=%(res_id)s", + {'path': path, 'cursor': cursor, + 'res_spec': res_spec, 'res_id': res_id}) # Check if the end of path is reached and event can be created finally if cursor == -1: # end of path reached, create the event @@ -630,6 +634,10 @@ def _attach_payload(event, payload, res_spec): def _create_target_resource(self, target_project, res_spec, res_id, res_parent_id=None, payload=None, key=None): """Build the event's target element from the payload.""" + self._log.debug("_create_target_resource: res_spec=%(res_spec)s, " + "res_id=%(res_id)s, res_parent_id=%(res_parent_id)s", + {'res_spec': res_spec, 'res_id': res_id, + 'res_parent_id': res_parent_id}) project_id = target_project rid = res_id name = None @@ -684,6 +692,9 @@ def _get_action_and_key(self, res_spec, res_id, request, suffix): request: the request suffix: the last path component (already known) """ + self._log.debug("_get_action_and_key: res_spec=%(res_spec)s, " + "res_id=%(res_id)s, suffix=%(suffix)s", + {'res_spec': res_spec, 'res_id': res_id, 'suffix': suffix}) if suffix is None: return self._get_action_from_method(request.method, res_spec, res_id), None @@ -731,9 +742,12 @@ def _get_action_and_key_from_path_suffix(self, path_suffix, method, # action suppressed by intention return None, None - # no action mapped to suffix => custom key + # no action mapped to suffix => custom key operation action = self._get_action_from_method(method, res_spec, res_id) - action += _key_action_suffix_map[action] + + if action in _key_action_suffix_map: + action += _key_action_suffix_map[action] + return action, path_suffix def _get_action_from_payload(self, request, res_spec, res_id): diff --git a/auditmiddleware/tests/unit/base.py b/auditmiddleware/tests/unit/base.py index f4da706..cef5219 100644 --- a/auditmiddleware/tests/unit/base.py +++ b/auditmiddleware/tests/unit/base.py @@ -15,6 +15,7 @@ import auditmiddleware from auditmiddleware._api import _make_tags from auditmiddleware.tests.unit import utils +import logging from mock import mock from oslo_config import fixture as cfg_fixture from oslo_messaging import conffixture as msg_fixture @@ -24,6 +25,7 @@ import webob import webob.dec +logging.basicConfig(level=logging.DEBUG) iso8601 = r'^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d\.\d{6}[+-]\d\d:\d\d$' diff --git a/auditmiddleware/tests/unit/test_http_methods.py b/auditmiddleware/tests/unit/test_http_methods.py index a2ed5ed..64d343b 100644 --- a/auditmiddleware/tests/unit/test_http_methods.py +++ b/auditmiddleware/tests/unit/test_http_methods.py @@ -47,7 +47,7 @@ def test_get_request(self): event['requestPath'] = mock_request.path self.check_event(mock_request, mock_response, event, 'read', - 'compute/server', + 'compute/server', '00000000-0000-0000-0000-00000000007b', None, 'success') self.assertEqual(event['initiator']['id'], self.user_id) @@ -69,7 +69,7 @@ def test_post_request(self): mock_response = MagicMock() mock_response.status_int = 202 mock_response.status_code = 202 - mock_response.content_length = 100 + mock_response.content_length = 100 mock_response.content_type = 'application/json' mock_response.json = {'server': {'id': 'xyz'}} mock_response.text = '{"server": {"id": "xyz"}}' @@ -104,7 +104,7 @@ def test_delete_request_with_no_content_length(self): event['requestPath'] = mock_request.path self.check_event(mock_request, mock_response, event, 'delete', - 'compute/server', + 'compute/server', '00000000-0000-0000-0000-00000000007b', None, 'success') self.assertEqual(event['initiator']['id'], self.user_id) @@ -133,7 +133,7 @@ def test_update_request(self): event['requestPath'] = mock_request.path self.check_event(mock_request, mock_response, event, 'update', - 'compute/server', + 'compute/server', '00000000-0000-0000-0000-00000000007b', None, 'success') self.assertEqual(event['initiator']['id'], self.user_id) diff --git a/auditmiddleware/tests/unit/test_logging_notifier.py b/auditmiddleware/tests/unit/test_logging_notifier.py index c6d841b..3368d8a 100644 --- a/auditmiddleware/tests/unit/test_logging_notifier.py +++ b/auditmiddleware/tests/unit/test_logging_notifier.py @@ -30,6 +30,8 @@ def setUp(self): def test_api_request_no_messaging(self): """Test that logging works and event_type is correct.""" + self.cfg.config(use_oslo_messaging=False, + group='audit_middleware_notifications') app = self.create_simple_app() with mock.patch('auditmiddleware._LOG.info') as log: diff --git a/auditmiddleware/tests/unit/test_mappings.py b/auditmiddleware/tests/unit/test_mappings.py index 4c52295..00a200e 100644 --- a/auditmiddleware/tests/unit/test_mappings.py +++ b/auditmiddleware/tests/unit/test_mappings.py @@ -555,17 +555,14 @@ def audit_map(self): return self.audit_map_file_fixture def test_get_list_shares(self): - """Test listing resource instances. - - This is just a arbitary smoke-test for Manila. - """ - url = self.build_url('shares', prefix='/v2/' + self.project_id) + """Test a list action for Manila shares.""" + url = self.build_url('shares', prefix='/v2') request, response = self.build_api_call('GET', url) event = self.build_event(request, response) - self.check_event(request, response, event, taxonomy.ACTION_LIST, - "storage/share/shares", - None, self.service_name) + self.check_event(request, response, event, 'read/list', + "storage/share/shares", + None, self.service_name) class DesignateAuditMappingTest(base.BaseAuditMiddlewareTest): diff --git a/etc/manila_audit_map.yaml b/etc/manila_audit_map.yaml index 7885f5b..67918f5 100644 --- a/etc/manila_audit_map.yaml +++ b/etc/manila_audit_map.yaml @@ -1,7 +1,7 @@ service_type: 'storage/share' service_name: 'manila' -prefix: '/v[0-9\.]*' +prefix: '/v[1-2]' resources: availability-zones: @@ -19,6 +19,9 @@ resources: pools: security-services: # model details listing a action + payloads: + exclude: + - password custom_actions: detail: read/list/details services: diff --git a/tox.ini b/tox.ini index 58227ea..03e9c01 100644 --- a/tox.ini +++ b/tox.ini @@ -99,3 +99,12 @@ deps = -c{toxinidir}/lower-constraints.txt -r{toxinidir}/test-requirements.txt -r{toxinidir}/requirements.txt +setenv = + PYTHONWARNINGS = ignore + PYTHON_LOGGER_LEVEL = DEBUG + +[testenv:lint] +deps = + flake8 +commands = + flake8 {posargs} \ No newline at end of file