base-api-utils 1.1.122__tar.gz → 1.1.123__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {base_api_utils-1.1.122/src/base_api_utils.egg-info → base_api_utils-1.1.123}/PKG-INFO +2 -2
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/pyproject.toml +2 -2
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/domain/__init__.py +2 -1
- base_api_utils-1.1.123/src/base_api_utils/domain/exceptions.py +7 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/handlers/sponsor_event_handler.py +7 -5
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/handlers/sponsorship_addon_event_handler.py +7 -5
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/handlers/sponsorship_event_handler.py +7 -5
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/handlers/summit_event_handler.py +7 -5
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/security/abstract_oauth2_api_client.py +9 -6
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/security/access_token_service.py +27 -16
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/security/group_required.py +2 -3
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/security/oauth2_authentication.py +5 -2
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/security/oauth2_scope_required.py +2 -3
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/security/permission_event_handler.py +5 -2
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/security/user_permissions_service.py +7 -4
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/serializers/timestamp_field.py +1 -2
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/services/abstract_event_dispatcher.py +1 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/services/amqp_service.py +84 -4
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/services/event_dispatcher.py +9 -2
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/utils/__init__.py +1 -0
- base_api_utils-1.1.123/src/base_api_utils/utils/db_connection_helpers.py +58 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/utils/exceptions.py +4 -2
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/utils/file_lock.py +7 -2
- base_api_utils-1.1.123/src/base_api_utils/utils/log_redaction.py +112 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123/src/base_api_utils.egg-info}/PKG-INFO +2 -2
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils.egg-info/SOURCES.txt +7 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils.egg-info/requires.txt +1 -1
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/tests/test_amqp_service.py +154 -0
- base_api_utils-1.1.123/tests/test_db_connection_helpers.py +107 -0
- base_api_utils-1.1.123/tests/test_file_lock.py +53 -0
- base_api_utils-1.1.123/tests/test_log_redaction.py +269 -0
- base_api_utils-1.1.123/tests/test_retryable_event_propagation.py +146 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/tests/test_timestamp_field.py +15 -4
- base_api_utils-1.1.123/tests/test_token_logging.py +714 -0
- base_api_utils-1.1.122/src/base_api_utils/utils/db_connection_helpers.py +0 -26
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/LICENSE +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/README.md +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/setup.cfg +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/__init__.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/commands/__init__.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/commands/failed_task_management_mixin.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/domain/event_message.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/domain/event_message_factory.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/domain/events.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/dtos/__init__.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/dtos/authz_user.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/fields/__init__.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/fields/email_list_field.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/filters/__init__.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/filters/base_filter.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/filters/custom_ordering_filter.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/filters/filter_utils_mixin.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/handlers/__init__.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/handlers/base_event_handler.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/ioc/__init__.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/ioc/container.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/models/__init__.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/models/item_image.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/security/__init__.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/security/abstract_access_token_service.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/security/check_sponsor_user_permissions.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/security/oauth2_client_factory.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/security/shared.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/serializers/__init__.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/serializers/base_model_serializer.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/serializers/email_list_char_field.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/serializers/nullable_slug_related_field.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/serializers/serializers_registry.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/serializers/task_result_serializer.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/serializers/v2/__init__.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/serializers/v2/abstract_serializer.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/serializers/v2/base_model_serializer.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/serializers/v2/expands.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/serializers/v2/query_params.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/serializers/v2/spectacular.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/services/__init__.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/services/abstract_domain_events_service.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/services/abstract_images_service.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/services/abstract_sponsor_metadata_service.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/services/abstract_storage_service.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/services/abstract_summit_metadata_service.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/services/celery_failed.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/services/images_service.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/services/s3_service.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/services/strategies/__init__.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/services/strategies/abstract_task_runner_strategy.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/services/strategies/publish_domain_event_strategy.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/tests/__init__.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/tests/test_failed_celery_task_management.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/tests/test_task_status_endpoints.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/urls.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/utils/admin_groups.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/utils/async_helpers.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/utils/circuit_breaker.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/utils/config.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/utils/custom_auto_schema.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/utils/debounce_helpers.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/utils/enums.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/utils/env_var_eval.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/utils/expand_queryset_optimization_mixin.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/utils/external_errors.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/utils/fingerprint_helpers.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/utils/http_helpers.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/utils/inject_job.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/utils/look_up_value_mixin.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/utils/model_helpers.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/utils/oauth2_custom_schema_base.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/utils/pagination.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/utils/rate_utils.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/utils/read_write_serializer_mixin.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/utils/serializers.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/utils/service_utils.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/utils/string.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/utils/tests.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/views/__init__.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/views/base_view.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/views/parent_children_crud.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/views/root_entity_crud.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/views/tasks_result_view.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils.egg-info/dependency_links.txt +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils.egg-info/top_level.txt +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/tests/test_check_sponsor_user_permissions.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/tests/test_circuit_breaker.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/tests/test_is_admin.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/tests/test_lazy_imports.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/tests/test_oauth2_scope_required.py +0 -0
- {base_api_utils-1.1.122 → base_api_utils-1.1.123}/tests/test_v2_abstract_serializer.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: base-api-utils
|
|
3
|
-
Version: 1.1.
|
|
3
|
+
Version: 1.1.123
|
|
4
4
|
Summary: Django Rest Framework micro services common utilities
|
|
5
5
|
Author-email: Román Gutierrez <roman@tipit.net>, Sebastian Marcet <sebastian@tipit.net>
|
|
6
6
|
Project-URL: Homepage, https://github.com/fntechgit/base-api-utils
|
|
@@ -11,7 +11,7 @@ Description-Content-Type: text/markdown
|
|
|
11
11
|
License-File: LICENSE
|
|
12
12
|
Requires-Dist: boto3
|
|
13
13
|
Requires-Dist: celery
|
|
14
|
-
Requires-Dist: Django
|
|
14
|
+
Requires-Dist: Django>=3.2
|
|
15
15
|
Requires-Dist: django-celery-results
|
|
16
16
|
Requires-Dist: django_filter
|
|
17
17
|
Requires-Dist: djangorestframework>=3.15.2
|
|
@@ -7,11 +7,11 @@ where = ["src"]
|
|
|
7
7
|
|
|
8
8
|
[project]
|
|
9
9
|
name = "base-api-utils"
|
|
10
|
-
version = "1.1.
|
|
10
|
+
version = "1.1.123"
|
|
11
11
|
dependencies = [
|
|
12
12
|
"boto3",
|
|
13
13
|
"celery",
|
|
14
|
-
"Django",
|
|
14
|
+
"Django>=3.2",
|
|
15
15
|
"django-celery-results",
|
|
16
16
|
"django_filter",
|
|
17
17
|
"djangorestframework>=3.15.2",
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
class RetryableEventError(Exception):
|
|
2
|
+
"""Raised by an event handler to signal a transient failure (e.g. the DB connection was lost).
|
|
3
|
+
|
|
4
|
+
``EventDispatcher`` lets it propagate and ``AMQPService`` redelivers the message
|
|
5
|
+
(up to ``max_redeliveries`` times) instead of discarding it. Any other exception keeps
|
|
6
|
+
the default behaviour: the message is nacked without requeue.
|
|
7
|
+
"""
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import logging
|
|
2
2
|
|
|
3
|
-
from ..domain import EventMessage
|
|
3
|
+
from ..domain import EventMessage, RetryableEventError
|
|
4
4
|
from .base_event_handler import BaseEventHandler
|
|
5
|
-
from ..utils import handle_db_connections
|
|
5
|
+
from ..utils import handle_db_connections, redact_secrets
|
|
6
6
|
|
|
7
7
|
from ..domain.events import InboundDomainEvent
|
|
8
8
|
from ..services import AbstractSponsorMetadataService
|
|
@@ -30,19 +30,21 @@ class SponsorEventHandler(BaseEventHandler):
|
|
|
30
30
|
return self._handle_sponsor_created_or_updated(event)
|
|
31
31
|
elif event.event_type == InboundDomainEvent.SPONSOR_DELETED.value:
|
|
32
32
|
return self._handle_sponsor_deleted(event)
|
|
33
|
+
except RetryableEventError:
|
|
34
|
+
raise
|
|
33
35
|
except Exception as e:
|
|
34
|
-
logging.getLogger('api').error(f"Error processing sponsor event: {e}")
|
|
36
|
+
logging.getLogger('api').error(f"Error processing sponsor event: {redact_secrets(e)}")
|
|
35
37
|
return False
|
|
36
38
|
|
|
37
39
|
return False
|
|
38
40
|
|
|
39
|
-
@handle_db_connections(max_retries=3)
|
|
41
|
+
@handle_db_connections(max_retries=3, raise_retryable=True)
|
|
40
42
|
def _handle_sponsor_created_or_updated(self, event: EventMessage) -> bool:
|
|
41
43
|
sponsor_data = event.payload
|
|
42
44
|
self.sponsor_service.upsert(sponsor_data)
|
|
43
45
|
return True
|
|
44
46
|
|
|
45
|
-
@handle_db_connections(max_retries=3)
|
|
47
|
+
@handle_db_connections(max_retries=3, raise_retryable=True)
|
|
46
48
|
def _handle_sponsor_deleted(self, event: EventMessage) -> bool:
|
|
47
49
|
sponsor_data = event.payload
|
|
48
50
|
self.sponsor_service.delete(sponsor_data.get('id'))
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import logging
|
|
2
2
|
|
|
3
|
-
from ..domain import EventMessage
|
|
3
|
+
from ..domain import EventMessage, RetryableEventError
|
|
4
4
|
from .base_event_handler import BaseEventHandler
|
|
5
|
-
from ..utils import handle_db_connections
|
|
5
|
+
from ..utils import handle_db_connections, redact_secrets
|
|
6
6
|
|
|
7
7
|
from ..domain.events import InboundDomainEvent
|
|
8
8
|
from ..services import AbstractSponsorMetadataService
|
|
@@ -31,19 +31,21 @@ class SponsorshipAddOnEventHandler(BaseEventHandler):
|
|
|
31
31
|
return self._handle_sponsorship_addon_created_or_updated(event)
|
|
32
32
|
elif event.event_type == InboundDomainEvent.SPONSORSHIP_ADDON_REMOVED.value:
|
|
33
33
|
return self._handle_sponsorship_addon_deleted(event)
|
|
34
|
+
except RetryableEventError:
|
|
35
|
+
raise
|
|
34
36
|
except Exception as e:
|
|
35
|
-
logging.getLogger('api').error(f"Error processing sponsorship addon event: {e}")
|
|
37
|
+
logging.getLogger('api').error(f"Error processing sponsorship addon event: {redact_secrets(e)}")
|
|
36
38
|
return False
|
|
37
39
|
|
|
38
40
|
return False
|
|
39
41
|
|
|
40
|
-
@handle_db_connections(max_retries=3)
|
|
42
|
+
@handle_db_connections(max_retries=3, raise_retryable=True)
|
|
41
43
|
def _handle_sponsorship_addon_created_or_updated(self, event: EventMessage) -> bool:
|
|
42
44
|
sponsorship_addon_data = event.payload
|
|
43
45
|
self.sponsor_service.upsert_sponsorship_addon(sponsorship_addon_data)
|
|
44
46
|
return True
|
|
45
47
|
|
|
46
|
-
@handle_db_connections(max_retries=3)
|
|
48
|
+
@handle_db_connections(max_retries=3, raise_retryable=True)
|
|
47
49
|
def _handle_sponsorship_addon_deleted(self, event: EventMessage) -> bool:
|
|
48
50
|
sponsorship_addon_data = event.payload
|
|
49
51
|
self.sponsor_service.remove_sponsorship_addon(
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import logging
|
|
2
2
|
|
|
3
|
-
from ..domain import EventMessage
|
|
3
|
+
from ..domain import EventMessage, RetryableEventError
|
|
4
4
|
from .base_event_handler import BaseEventHandler
|
|
5
|
-
from ..utils import handle_db_connections
|
|
5
|
+
from ..utils import handle_db_connections, redact_secrets
|
|
6
6
|
|
|
7
7
|
from ..domain.events import InboundDomainEvent
|
|
8
8
|
from ..services import AbstractSponsorMetadataService
|
|
@@ -31,19 +31,21 @@ class SponsorshipEventHandler(BaseEventHandler):
|
|
|
31
31
|
return self._handle_sponsorship_created_or_updated(event)
|
|
32
32
|
elif event.event_type == InboundDomainEvent.SPONSORSHIP_REMOVED.value:
|
|
33
33
|
return self._handle_sponsorship_deleted(event)
|
|
34
|
+
except RetryableEventError:
|
|
35
|
+
raise
|
|
34
36
|
except Exception as e:
|
|
35
|
-
logging.getLogger('api').error(f"Error processing sponsorship event: {e}")
|
|
37
|
+
logging.getLogger('api').error(f"Error processing sponsorship event: {redact_secrets(e)}")
|
|
36
38
|
return False
|
|
37
39
|
|
|
38
40
|
return False
|
|
39
41
|
|
|
40
|
-
@handle_db_connections(max_retries=3)
|
|
42
|
+
@handle_db_connections(max_retries=3, raise_retryable=True)
|
|
41
43
|
def _handle_sponsorship_created_or_updated(self, event: EventMessage) -> bool:
|
|
42
44
|
sponsorship_data = event.payload
|
|
43
45
|
self.sponsor_service.upsert_sponsorship(sponsorship_data)
|
|
44
46
|
return True
|
|
45
47
|
|
|
46
|
-
@handle_db_connections(max_retries=3)
|
|
48
|
+
@handle_db_connections(max_retries=3, raise_retryable=True)
|
|
47
49
|
def _handle_sponsorship_deleted(self, event: EventMessage) -> bool:
|
|
48
50
|
sponsorship_data = event.payload
|
|
49
51
|
self.sponsor_service.remove_sponsorship(sponsorship_data.get('id'))
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import logging
|
|
2
2
|
|
|
3
|
-
from ..domain import EventMessage
|
|
3
|
+
from ..domain import EventMessage, RetryableEventError
|
|
4
4
|
from .base_event_handler import BaseEventHandler
|
|
5
|
-
from ..utils import handle_db_connections
|
|
5
|
+
from ..utils import handle_db_connections, redact_secrets
|
|
6
6
|
|
|
7
7
|
from ..domain.events import InboundDomainEvent
|
|
8
8
|
from ..services import AbstractSummitMetadataService
|
|
@@ -29,19 +29,21 @@ class SummitEventHandler(BaseEventHandler):
|
|
|
29
29
|
return self._handle_summit_created_or_updated(event)
|
|
30
30
|
elif event.event_type == InboundDomainEvent.SUMMIT_DELETED.value:
|
|
31
31
|
return self._handle_summit_deleted(event)
|
|
32
|
+
except RetryableEventError:
|
|
33
|
+
raise
|
|
32
34
|
except Exception as e:
|
|
33
|
-
logging.getLogger('api').error(f"Error processing summit event: {e}")
|
|
35
|
+
logging.getLogger('api').error(f"Error processing summit event: {redact_secrets(e)}")
|
|
34
36
|
return False
|
|
35
37
|
|
|
36
38
|
return False
|
|
37
39
|
|
|
38
|
-
@handle_db_connections(max_retries=3)
|
|
40
|
+
@handle_db_connections(max_retries=3, raise_retryable=True)
|
|
39
41
|
def _handle_summit_created_or_updated(self, event: EventMessage) -> bool:
|
|
40
42
|
summit_data = event.payload
|
|
41
43
|
self.show_service.upsert(summit_data.get('id'), summit_data)
|
|
42
44
|
return True
|
|
43
45
|
|
|
44
|
-
@handle_db_connections(max_retries=3)
|
|
46
|
+
@handle_db_connections(max_retries=3, raise_retryable=True)
|
|
45
47
|
def _handle_summit_deleted(self, event: EventMessage) -> bool:
|
|
46
48
|
summit_data = event.payload
|
|
47
49
|
self.show_service.delete(summit_data.get('id'))
|
|
@@ -11,7 +11,7 @@ from rest_framework.status import HTTP_404_NOT_FOUND, HTTP_401_UNAUTHORIZED, HTT
|
|
|
11
11
|
|
|
12
12
|
from .oauth2_client_factory import OAuth2ClientFactory
|
|
13
13
|
from .shared import client_secret_basic_auth_method
|
|
14
|
-
from ..utils import config
|
|
14
|
+
from ..utils import config, redact_secrets, token_fingerprint
|
|
15
15
|
|
|
16
16
|
|
|
17
17
|
class AbstractOAuth2APIClient(ABC):
|
|
@@ -78,10 +78,11 @@ class AbstractOAuth2APIClient(ABC):
|
|
|
78
78
|
if retries >= self.MAX_RETRIES:
|
|
79
79
|
raise
|
|
80
80
|
self.clean_access_token()
|
|
81
|
-
|
|
81
|
+
# The message of a requests exception contains the URL, and with it ?access_token=.
|
|
82
|
+
logger.warning(f"Retry attempt {retries}: {redact_secrets(ex)}")
|
|
82
83
|
except Exception as ex:
|
|
83
84
|
self.clean_access_token()
|
|
84
|
-
logger.error(f"Unhandled exception: {ex}")
|
|
85
|
+
logger.error(f"Unhandled exception: {redact_secrets(ex)}")
|
|
85
86
|
raise
|
|
86
87
|
|
|
87
88
|
def __get_access_token(self, auth_method: AuthMethod) -> str:
|
|
@@ -119,15 +120,17 @@ class AbstractOAuth2APIClient(ABC):
|
|
|
119
120
|
if ttl > 0:
|
|
120
121
|
cache.set(cache_key, token, ttl)
|
|
121
122
|
|
|
123
|
+
# The token itself is never logged, only its fingerprint.
|
|
122
124
|
logger.debug("AbstractOAuth2APIClient::__get_access_token - "
|
|
123
|
-
"New access token obtained: %s, expires in %s seconds.", token, ttl)
|
|
125
|
+
"New access token obtained: %s, expires in %s seconds.", token_fingerprint(token), ttl)
|
|
124
126
|
except RequestException as ex:
|
|
125
|
-
logger.warning(f"RequestException: {ex}")
|
|
127
|
+
logger.warning(f"RequestException: {redact_secrets(ex)}")
|
|
126
128
|
if ex.response and ex.response.status_code == HTTP_401_UNAUTHORIZED:
|
|
127
129
|
cache.delete(cache_key)
|
|
128
130
|
raise
|
|
129
131
|
|
|
130
|
-
logger.debug("AbstractOAuth2APIClient::__get_access_token - Returning cached access token: %s",
|
|
132
|
+
logger.debug("AbstractOAuth2APIClient::__get_access_token - Returning cached access token: %s",
|
|
133
|
+
token_fingerprint(token))
|
|
131
134
|
return token
|
|
132
135
|
|
|
133
136
|
def get_access_token_using_client_secret_basic(self) -> str:
|
|
@@ -9,7 +9,7 @@ from django.core.exceptions import PermissionDenied
|
|
|
9
9
|
from django.utils.translation import gettext_lazy as _
|
|
10
10
|
|
|
11
11
|
from .abstract_access_token_service import AbstractAccessTokenService
|
|
12
|
-
from ..utils import config
|
|
12
|
+
from ..utils import config, describe_token_info, redact_secrets, token_fingerprint
|
|
13
13
|
|
|
14
14
|
|
|
15
15
|
class AccessTokenService(AbstractAccessTokenService):
|
|
@@ -18,14 +18,16 @@ class AccessTokenService(AbstractAccessTokenService):
|
|
|
18
18
|
"""
|
|
19
19
|
Authenticate the request, given the access token.
|
|
20
20
|
"""
|
|
21
|
-
|
|
21
|
+
# Log lines name the token by its fingerprint: the token and the introspection body are never logged.
|
|
22
|
+
fingerprint = token_fingerprint(access_token)
|
|
23
|
+
logging.getLogger('oauth2').debug('AccessTokenService::validate trying to get {fingerprint} from cache ...'.format(fingerprint=fingerprint))
|
|
22
24
|
# try get access_token from DB and check if not expired
|
|
23
25
|
cached_token_info = cache.get(access_token)
|
|
24
26
|
|
|
25
27
|
if cached_token_info is None:
|
|
26
28
|
try:
|
|
27
29
|
logging.getLogger('oauth2').debug(
|
|
28
|
-
'AccessTokenService::validate {
|
|
30
|
+
'AccessTokenService::validate {fingerprint} is not present on cache, trying to validate from instrospection endpoint'.format(fingerprint=fingerprint))
|
|
29
31
|
response = requests.post(
|
|
30
32
|
'{base_url}/{endpoint}'.format
|
|
31
33
|
(
|
|
@@ -42,17 +44,21 @@ class AccessTokenService(AbstractAccessTokenService):
|
|
|
42
44
|
cached_token_info = response.json()
|
|
43
45
|
lifetime = config('OAUTH2.CLIENT.ACCESS_TOKEN_CACHE_LIFETIME', cached_token_info['expires_in'])
|
|
44
46
|
logging.getLogger('oauth2').debug(
|
|
45
|
-
'AccessTokenService::validate {
|
|
46
|
-
|
|
47
|
+
'AccessTokenService::validate {fingerprint} storing on cache with lifetime {lifetime}'.format(
|
|
48
|
+
fingerprint=fingerprint, lifetime=lifetime))
|
|
47
49
|
cache.set(access_token, cached_token_info, timeout=int(lifetime))
|
|
48
|
-
logging.getLogger('oauth2').
|
|
49
|
-
'http code {code}
|
|
50
|
-
|
|
50
|
+
logging.getLogger('oauth2').debug(
|
|
51
|
+
'AccessTokenService::validate {fingerprint} http code {code}: {token_info}'.format(
|
|
52
|
+
fingerprint=fingerprint, code=response.status_code,
|
|
53
|
+
token_info=describe_token_info(cached_token_info)))
|
|
51
54
|
return AnonymousUser, cached_token_info
|
|
52
55
|
|
|
56
|
+
# The body is not logged: OpenStackID writes the refused token into error_description.
|
|
57
|
+
# The error and the description are returned below; validate_token_info logs the
|
|
58
|
+
# error code only.
|
|
53
59
|
logging.getLogger('oauth2').warning(
|
|
54
|
-
'AccessTokenService::validate http code {code} http content {
|
|
55
|
-
|
|
60
|
+
'AccessTokenService::validate {fingerprint} http code {code} http content length {length}'.format(
|
|
61
|
+
fingerprint=fingerprint, code=response.status_code, length=len(response.content or b'')))
|
|
56
62
|
content = {}
|
|
57
63
|
if response.content:
|
|
58
64
|
content = json.loads(response.content.decode('utf-8'))
|
|
@@ -64,20 +70,25 @@ class AccessTokenService(AbstractAccessTokenService):
|
|
|
64
70
|
}
|
|
65
71
|
return None, result
|
|
66
72
|
except requests.exceptions.RequestException as e:
|
|
67
|
-
|
|
73
|
+
# requests builds the message from the URL, which carries ?token=.
|
|
74
|
+
logging.getLogger('oauth2').error(redact_secrets(e))
|
|
68
75
|
return None
|
|
69
76
|
except:
|
|
70
|
-
|
|
77
|
+
# The error's type and message only: its repr can hold the body or the client secret.
|
|
78
|
+
error = sys.exc_info()[1]
|
|
79
|
+
logging.getLogger('oauth2').error(
|
|
80
|
+
f'AccessTokenService::validate {type(error).__name__}: {redact_secrets(error)}')
|
|
71
81
|
return None
|
|
72
82
|
|
|
73
83
|
logging.getLogger('oauth2').debug(
|
|
74
|
-
'AccessTokenService::validate {
|
|
75
|
-
|
|
84
|
+
'AccessTokenService::validate {fingerprint} cache hit'.format(
|
|
85
|
+
fingerprint=fingerprint))
|
|
76
86
|
return AnonymousUser, cached_token_info
|
|
77
87
|
|
|
78
88
|
@staticmethod
|
|
79
89
|
def validate_token_info(token_info):
|
|
80
|
-
logging.getLogger('oauth2').debug(
|
|
90
|
+
logging.getLogger('oauth2').debug(
|
|
91
|
+
f'AccessTokenService::validate_token_info {describe_token_info(token_info)}')
|
|
81
92
|
|
|
82
93
|
if token_info is None:
|
|
83
94
|
logging.getLogger('oauth2').warning('AccessTokenService::validate_token_info token info not present.')
|
|
@@ -86,5 +97,5 @@ class AccessTokenService(AbstractAccessTokenService):
|
|
|
86
97
|
if 'error_code' in token_info:
|
|
87
98
|
error_description = token_info.get('description', 'token info is invalid.')
|
|
88
99
|
logging.getLogger('oauth2').warning(
|
|
89
|
-
f'AccessTokenService::validate_token_info token info is invalid: {token_info}')
|
|
100
|
+
f'AccessTokenService::validate_token_info token info is invalid: {describe_token_info(token_info)}')
|
|
90
101
|
raise PermissionDenied(error_description)
|
{base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/security/group_required.py
RENAMED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import logging
|
|
2
2
|
import os
|
|
3
|
-
import json
|
|
4
3
|
|
|
5
4
|
from django.core.exceptions import PermissionDenied
|
|
6
5
|
from django.utils.functional import wraps
|
|
@@ -8,7 +7,7 @@ from django.utils.translation import gettext_lazy as _
|
|
|
8
7
|
|
|
9
8
|
from . import AccessTokenService
|
|
10
9
|
from .shared import get_path, application_type_service
|
|
11
|
-
from ..utils import config, is_empty
|
|
10
|
+
from ..utils import config, describe_token_info, is_empty
|
|
12
11
|
|
|
13
12
|
|
|
14
13
|
def group_required():
|
|
@@ -39,7 +38,7 @@ def group_required():
|
|
|
39
38
|
|
|
40
39
|
endpoints = config('OAUTH2.CLIENT.ENDPOINTS')
|
|
41
40
|
|
|
42
|
-
logger.debug(f'group_required::decorator token_info {
|
|
41
|
+
logger.debug(f'group_required::decorator token_info {describe_token_info(token_info)}')
|
|
43
42
|
|
|
44
43
|
if 'application_type' in token_info and token_info['application_type'] == application_type_service:
|
|
45
44
|
logger.debug('authz groups are not evaluated for service-type applications')
|
|
@@ -4,6 +4,7 @@ from rest_framework import exceptions
|
|
|
4
4
|
from rest_framework.authentication import get_authorization_header, BaseAuthentication
|
|
5
5
|
|
|
6
6
|
from .access_token_service import AccessTokenService
|
|
7
|
+
from ..utils import token_fingerprint
|
|
7
8
|
|
|
8
9
|
|
|
9
10
|
class OAuth2Authentication(BaseAuthentication):
|
|
@@ -30,6 +31,8 @@ class OAuth2Authentication(BaseAuthentication):
|
|
|
30
31
|
else:
|
|
31
32
|
return None
|
|
32
33
|
|
|
33
|
-
|
|
34
|
-
|
|
34
|
+
# The token itself is never logged, only a fingerprint that tells one token from another.
|
|
35
|
+
logging.getLogger('oauth2').debug(
|
|
36
|
+
'OAuth2Authentication::authenticate access_token {fingerprint}'.format(
|
|
37
|
+
fingerprint=token_fingerprint(access_token)))
|
|
35
38
|
return self.service.validate(access_token)
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import logging
|
|
2
2
|
import os
|
|
3
|
-
import json
|
|
4
3
|
|
|
5
4
|
from django.core.exceptions import PermissionDenied
|
|
6
5
|
from django.utils.functional import wraps
|
|
@@ -8,7 +7,7 @@ from django.utils.translation import gettext_lazy as _
|
|
|
8
7
|
|
|
9
8
|
from . import AccessTokenService
|
|
10
9
|
from .shared import get_path
|
|
11
|
-
from ..utils import config, is_empty
|
|
10
|
+
from ..utils import config, describe_token_info, is_empty
|
|
12
11
|
|
|
13
12
|
|
|
14
13
|
def oauth2_scope_required():
|
|
@@ -38,7 +37,7 @@ def oauth2_scope_required():
|
|
|
38
37
|
endpoints = config('OAUTH2.CLIENT.ENDPOINTS')
|
|
39
38
|
|
|
40
39
|
logger.debug('oauth2_scope_required::decorator token_info {}'.format(
|
|
41
|
-
|
|
40
|
+
describe_token_info(token_info)
|
|
42
41
|
))
|
|
43
42
|
|
|
44
43
|
endpoint = endpoints[path] if path in endpoints else None
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import logging
|
|
2
2
|
|
|
3
|
-
from ..domain import EventMessage, DomainEvent
|
|
3
|
+
from ..domain import EventMessage, DomainEvent, RetryableEventError
|
|
4
4
|
from ..handlers import BaseEventHandler
|
|
5
|
+
from ..utils import redact_secrets
|
|
5
6
|
|
|
6
7
|
from .user_permissions_service import UserPermissionsService
|
|
7
8
|
|
|
@@ -31,8 +32,10 @@ class PermissionEventHandler(BaseEventHandler):
|
|
|
31
32
|
return self._handle_permission_removed(event)
|
|
32
33
|
|
|
33
34
|
return False
|
|
35
|
+
except RetryableEventError:
|
|
36
|
+
raise
|
|
34
37
|
except Exception as e:
|
|
35
|
-
logging.getLogger('api').error(f"Error processing permission event: {e}")
|
|
38
|
+
logging.getLogger('api').error(f"Error processing permission event: {redact_secrets(e)}")
|
|
36
39
|
return False
|
|
37
40
|
|
|
38
41
|
def _handle_permission_created_or_updated(self, event: EventMessage) -> bool:
|
|
@@ -7,7 +7,7 @@ from django.core.cache import cache
|
|
|
7
7
|
|
|
8
8
|
from requests import HTTPError
|
|
9
9
|
|
|
10
|
-
from ..utils import NamedCircuitBreaker, config, call_with_breaker
|
|
10
|
+
from ..utils import NamedCircuitBreaker, config, call_with_breaker, redact_secrets, token_fingerprint
|
|
11
11
|
|
|
12
12
|
|
|
13
13
|
class UserPermissionsService:
|
|
@@ -117,7 +117,7 @@ class UserPermissionsService:
|
|
|
117
117
|
cache_key = cls._build_cache_key(sponsor_id, summit_id, user_id)
|
|
118
118
|
|
|
119
119
|
logging.getLogger('api').debug(
|
|
120
|
-
f'SponsorUsersService::has_access_rights trying to get {access_token} from cache...')
|
|
120
|
+
f'SponsorUsersService::has_access_rights trying to get {token_fingerprint(access_token)} from cache...')
|
|
121
121
|
|
|
122
122
|
cached_access_right = cache.get(cache_key)
|
|
123
123
|
|
|
@@ -130,10 +130,13 @@ class UserPermissionsService:
|
|
|
130
130
|
|
|
131
131
|
cls._set_cache(summit_id, sponsor_id, user_id, True, timeout=lifetime)
|
|
132
132
|
except HTTPError as e:
|
|
133
|
-
|
|
133
|
+
# The message contains the URL, and with it ?access_token=.
|
|
134
|
+
logging.getLogger('api').error(redact_secrets(e))
|
|
134
135
|
return False
|
|
135
136
|
except:
|
|
136
|
-
|
|
137
|
+
# The error's type and message only: its repr can hold more than its message does.
|
|
138
|
+
error = sys.exc_info()[1]
|
|
139
|
+
logging.getLogger('api').error(f'{type(error).__name__}: {redact_secrets(error)}')
|
|
137
140
|
return False
|
|
138
141
|
|
|
139
142
|
return True
|
{base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/serializers/timestamp_field.py
RENAMED
|
@@ -2,7 +2,6 @@ from datetime import timezone, datetime
|
|
|
2
2
|
|
|
3
3
|
from drf_spectacular.utils import extend_schema_field
|
|
4
4
|
from rest_framework import serializers
|
|
5
|
-
import time
|
|
6
5
|
|
|
7
6
|
@extend_schema_field(
|
|
8
7
|
{
|
|
@@ -21,4 +20,4 @@ class TimestampField(serializers.Field):
|
|
|
21
20
|
def to_representation(self, value):
|
|
22
21
|
if value is None:
|
|
23
22
|
return None
|
|
24
|
-
return int(
|
|
23
|
+
return int(value.timestamp())
|
{base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/services/amqp_service.py
RENAMED
|
@@ -2,13 +2,14 @@ import json
|
|
|
2
2
|
import logging
|
|
3
3
|
import random
|
|
4
4
|
import time
|
|
5
|
+
import traceback
|
|
5
6
|
from typing import Any
|
|
6
7
|
|
|
7
8
|
import pika
|
|
8
9
|
from pika.exceptions import AMQPConnectionError, AMQPChannelError
|
|
9
10
|
|
|
10
|
-
from ..domain import DomainEvent, EventMessageFactory
|
|
11
|
-
from ..utils import config
|
|
11
|
+
from ..domain import DomainEvent, EventMessageFactory, RetryableEventError
|
|
12
|
+
from ..utils import config, redact_secrets
|
|
12
13
|
|
|
13
14
|
from .abstract_event_dispatcher import AbstractEventDispatcher
|
|
14
15
|
from .abstract_domain_events_service import AbstractDomainEventsService
|
|
@@ -30,7 +31,9 @@ class AMQPService(AbstractDomainEventsService):
|
|
|
30
31
|
max_retries=3,
|
|
31
32
|
base_delay=1.0,
|
|
32
33
|
max_delay=30.0,
|
|
33
|
-
jitter=True
|
|
34
|
+
jitter=True,
|
|
35
|
+
max_redeliveries=5,
|
|
36
|
+
redelivery_delay=5.0):
|
|
34
37
|
|
|
35
38
|
self.rabbit_host = rabbit_host
|
|
36
39
|
self.rabbit_port = rabbit_port
|
|
@@ -48,6 +51,9 @@ class AMQPService(AbstractDomainEventsService):
|
|
|
48
51
|
self.base_delay = base_delay
|
|
49
52
|
self.max_delay = max_delay
|
|
50
53
|
self.jitter = jitter
|
|
54
|
+
# Redelivery of messages whose handler raised RetryableEventError
|
|
55
|
+
self.max_redeliveries = max_redeliveries
|
|
56
|
+
self.redelivery_delay = redelivery_delay
|
|
51
57
|
|
|
52
58
|
self.dispatcher = event_dispatcher
|
|
53
59
|
self.event_message_type = event_message_type
|
|
@@ -113,6 +119,7 @@ class AMQPService(AbstractDomainEventsService):
|
|
|
113
119
|
|
|
114
120
|
def _callback(self, ch, method, properties, body):
|
|
115
121
|
"""Process incoming messages depending on routing key."""
|
|
122
|
+
self._restore_original_routing_key(method, properties)
|
|
116
123
|
logging.getLogger('api').info(f"Message received: {body} with routing_key={method.routing_key}")
|
|
117
124
|
|
|
118
125
|
try:
|
|
@@ -134,9 +141,82 @@ class AMQPService(AbstractDomainEventsService):
|
|
|
134
141
|
# Reject
|
|
135
142
|
ch.basic_nack(delivery_tag=method.delivery_tag, requeue=False)
|
|
136
143
|
|
|
144
|
+
except RetryableEventError as e:
|
|
145
|
+
self._redeliver(ch, method, properties, body, e)
|
|
146
|
+
|
|
137
147
|
except Exception as e:
|
|
138
|
-
|
|
148
|
+
# The traceback is logged as text, redacted: exc_info would write the error as it is.
|
|
149
|
+
logging.getLogger('api').error(
|
|
150
|
+
f"AMQPService::_callback: Critical error processing message: {redact_secrets(e)}\n"
|
|
151
|
+
f"{redact_secrets(traceback.format_exc())}")
|
|
152
|
+
ch.basic_nack(delivery_tag=method.delivery_tag, requeue=False)
|
|
153
|
+
|
|
154
|
+
RETRY_COUNT_HEADER = 'x-retry-count'
|
|
155
|
+
ORIGINAL_ROUTING_KEY_HEADER = 'x-original-routing-key'
|
|
156
|
+
|
|
157
|
+
def _restore_original_routing_key(self, method, properties):
|
|
158
|
+
"""A message republished by ``_redeliver`` comes back through the default exchange with the
|
|
159
|
+
queue name as routing key. Put the original event routing key back before it is parsed, so
|
|
160
|
+
any ``EventMessage`` type that reads ``method.routing_key`` sees the real event type.
|
|
161
|
+
"""
|
|
162
|
+
if method.exchange != '':
|
|
163
|
+
return
|
|
164
|
+
original = (properties.headers or {}).get(self.ORIGINAL_ROUTING_KEY_HEADER)
|
|
165
|
+
if original is not None:
|
|
166
|
+
method.routing_key = original
|
|
167
|
+
|
|
168
|
+
def _redeliver(self, ch, method, properties, body, error: Exception):
|
|
169
|
+
"""Redeliver a message whose handler failed with a transient error.
|
|
170
|
+
|
|
171
|
+
The attempt count travels in the ``x-retry-count`` header (a plain nack/requeue cannot
|
|
172
|
+
carry it). Once ``max_redeliveries`` is exceeded the message is discarded (nack without
|
|
173
|
+
requeue, so a dead-letter exchange configured on the queue still receives it).
|
|
174
|
+
|
|
175
|
+
The copy is published to the queue through the default exchange, which delivers it with the
|
|
176
|
+
queue name as routing key; the event routing key travels in ``x-original-routing-key`` and
|
|
177
|
+
``_callback`` restores it.
|
|
178
|
+
"""
|
|
179
|
+
log = logging.getLogger('api')
|
|
180
|
+
headers = dict(properties.headers or {})
|
|
181
|
+
count = int(headers.get(self.RETRY_COUNT_HEADER, 0))
|
|
182
|
+
|
|
183
|
+
if count >= self.max_redeliveries:
|
|
184
|
+
log.error(f"AMQPService::_redeliver: giving up after {count} redeliveries "
|
|
185
|
+
f"(routing_key={method.routing_key}): {redact_secrets(error)}")
|
|
139
186
|
ch.basic_nack(delivery_tag=method.delivery_tag, requeue=False)
|
|
187
|
+
return
|
|
188
|
+
|
|
189
|
+
log.warning(f"AMQPService::_redeliver: transient error, redelivery {count + 1}/{self.max_redeliveries} "
|
|
190
|
+
f"in {self.redelivery_delay}s (routing_key={method.routing_key}): {redact_secrets(error)}")
|
|
191
|
+
# Delay so a sustained outage does not become a hot redelivery loop
|
|
192
|
+
time.sleep(self.redelivery_delay)
|
|
193
|
+
|
|
194
|
+
headers[self.RETRY_COUNT_HEADER] = count + 1
|
|
195
|
+
# method.routing_key is authoritative here: _callback already restored it for a redelivered
|
|
196
|
+
# copy, and a header that arrived through another exchange must not survive the retry
|
|
197
|
+
headers[self.ORIGINAL_ROUTING_KEY_HEADER] = method.routing_key
|
|
198
|
+
try:
|
|
199
|
+
# Default exchange routes straight to this queue, whatever the original binding
|
|
200
|
+
ch.basic_publish(
|
|
201
|
+
exchange='',
|
|
202
|
+
routing_key=self.queue_name,
|
|
203
|
+
body=body,
|
|
204
|
+
properties=pika.BasicProperties(
|
|
205
|
+
delivery_mode=2,
|
|
206
|
+
content_type=properties.content_type,
|
|
207
|
+
content_encoding=properties.content_encoding,
|
|
208
|
+
correlation_id=properties.correlation_id,
|
|
209
|
+
message_id=properties.message_id,
|
|
210
|
+
type=properties.type,
|
|
211
|
+
headers=headers,
|
|
212
|
+
),
|
|
213
|
+
)
|
|
214
|
+
except Exception as publish_error:
|
|
215
|
+
# Could not republish: requeue the original so the event is not lost
|
|
216
|
+
log.error(f"AMQPService::_redeliver: republish failed, requeueing original: {publish_error}")
|
|
217
|
+
ch.basic_nack(delivery_tag=method.delivery_tag, requeue=True)
|
|
218
|
+
return
|
|
219
|
+
ch.basic_ack(delivery_tag=method.delivery_tag)
|
|
140
220
|
|
|
141
221
|
def publish(self, message: Any, event_type: DomainEvent, exchange: str = None):
|
|
142
222
|
rabbit_exchange = exchange if not exchange is None else self.exchange_name
|
{base_api_utils-1.1.122 → base_api_utils-1.1.123}/src/base_api_utils/services/event_dispatcher.py
RENAMED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import logging
|
|
2
|
+
import traceback
|
|
2
3
|
from typing import List
|
|
3
4
|
|
|
4
|
-
from ..domain import EventMessage
|
|
5
|
+
from ..domain import EventMessage, RetryableEventError
|
|
5
6
|
from ..handlers import BaseEventHandler
|
|
6
7
|
from ..services import AbstractEventDispatcher
|
|
8
|
+
from ..utils import redact_secrets
|
|
7
9
|
|
|
8
10
|
|
|
9
11
|
class EventDispatcher(AbstractEventDispatcher):
|
|
@@ -26,8 +28,13 @@ class EventDispatcher(AbstractEventDispatcher):
|
|
|
26
28
|
else:
|
|
27
29
|
logging.getLogger('api').warning(f"Handler returned False for {event.event_type}")
|
|
28
30
|
return False
|
|
31
|
+
except RetryableEventError:
|
|
32
|
+
raise
|
|
29
33
|
except Exception as e:
|
|
30
|
-
|
|
34
|
+
# The traceback is logged as text, redacted: exc_info would write the error as it is.
|
|
35
|
+
logging.getLogger('api').error(
|
|
36
|
+
f"Handler failed for {event.event_type}: {redact_secrets(e)}\n"
|
|
37
|
+
f"{redact_secrets(traceback.format_exc())}")
|
|
31
38
|
return False
|
|
32
39
|
|
|
33
40
|
logging.getLogger('api').warning(f"No handler found for event type: {event.event_type}")
|
|
@@ -19,6 +19,7 @@ from .fingerprint_helpers import (
|
|
|
19
19
|
)
|
|
20
20
|
from .http_helpers import raise_for_auth_errors, raise_for_external_catch, raise_for_status_codes
|
|
21
21
|
from .inject_job import inject_job
|
|
22
|
+
from .log_redaction import describe_token_info, redact_secrets, token_fingerprint
|
|
22
23
|
from .look_up_value_mixin import LookupValueMixin
|
|
23
24
|
from .model_helpers import get_or_not_found
|
|
24
25
|
from .pagination import LargeResultsSetPagination
|