diff --git a/docs/installation/config.rst b/docs/installation/config.rst index e0f82432..33290aa8 100644 --- a/docs/installation/config.rst +++ b/docs/installation/config.rst @@ -98,6 +98,10 @@ Other settings * ``DISABLE_2FA``: whether to disable two-factor authentication. Defaults to ``False``. If set to ``False``, 2FA will be required if not using OIDC. +* ``LOG_REQUESTS``: whether to enable logging of outgoing HTTP requests. Defaults to ``False``. + +* ``LOG_OUTGOING_REQUESTS_DB_SAVE``: whether to save logged requests to the database. Defaults to ``False``. + Initial superuser creation -------------------------- diff --git a/requirements/base.in b/requirements/base.in index d9e964e3..454d51ba 100644 --- a/requirements/base.in +++ b/requirements/base.in @@ -3,6 +3,7 @@ open-api-framework # Core python libraries glom # data represenation based on spec jsonschema +django-log-outgoing-requests # Common ground libraries notifications-api-common diff --git a/requirements/base.txt b/requirements/base.txt index 54246a53..aa655fe0 100644 --- a/requirements/base.txt +++ b/requirements/base.txt @@ -70,11 +70,13 @@ django==4.2.11 # via # commonground-api-common # django-admin-index + # django-appconf # django-axes # django-cors-headers # django-filter # django-formtools # django-jsonform + # django-log-outgoing-requests # django-markup # django-otp # django-phonenumber-field @@ -99,6 +101,8 @@ django==4.2.11 # zgw-consumers django-admin-index==3.1.0 # via open-api-framework +django-appconf==1.0.6 + # via django-log-outgoing-requests django-axes==6.3.0 # via open-api-framework django-cors-headers==4.3.1 @@ -113,6 +117,8 @@ django-jsonform==2.21.4 # via # mozilla-django-oidc-db # open-api-framework +django-log-outgoing-requests==0.6.1 + # via -r requirements/base.in django-markup==1.8.1 # via open-api-framework django-ordered-model==3.7.4 @@ -136,6 +142,7 @@ django-simple-certmanager==1.4.1 django-solo==2.2.0 # via # commonground-api-common + # django-log-outgoing-requests # mozilla-django-oidc-db # notifications-api-common # zgw-consumers @@ -276,6 +283,7 @@ requests==2.31.0 # via # commonground-api-common # coreapi + # django-log-outgoing-requests # gemma-zds-client # mozilla-django-oidc # open-api-framework diff --git a/requirements/ci.txt b/requirements/ci.txt index 13e4aadb..976fa188 100644 --- a/requirements/ci.txt +++ b/requirements/ci.txt @@ -110,11 +110,13 @@ django==4.2.11 # -r requirements/base.txt # commonground-api-common # django-admin-index + # django-appconf # django-axes # django-cors-headers # django-filter # django-formtools # django-jsonform + # django-log-outgoing-requests # django-markup # django-otp # django-phonenumber-field @@ -141,6 +143,10 @@ django-admin-index==3.1.0 # via # -r requirements/base.txt # open-api-framework +django-appconf==1.0.6 + # via + # -r requirements/base.txt + # django-log-outgoing-requests django-axes==6.3.0 # via # -r requirements/base.txt @@ -163,6 +169,8 @@ django-jsonform==2.21.4 # -r requirements/base.txt # mozilla-django-oidc-db # open-api-framework +django-log-outgoing-requests==0.6.1 + # via -r requirements/base.txt django-markup==1.8.1 # via # -r requirements/base.txt @@ -207,6 +215,7 @@ django-solo==2.2.0 # via # -r requirements/base.txt # commonground-api-common + # django-log-outgoing-requests # mozilla-django-oidc-db # notifications-api-common # zgw-consumers @@ -440,6 +449,7 @@ requests==2.31.0 # -r requirements/base.txt # commonground-api-common # coreapi + # django-log-outgoing-requests # gemma-zds-client # mozilla-django-oidc # open-api-framework diff --git a/requirements/dev.txt b/requirements/dev.txt index cc971009..38add344 100644 --- a/requirements/dev.txt +++ b/requirements/dev.txt @@ -130,6 +130,7 @@ django==4.2.11 # -r requirements/ci.txt # commonground-api-common # django-admin-index + # django-appconf # django-axes # django-cors-headers # django-debug-toolbar @@ -137,6 +138,7 @@ django==4.2.11 # django-filter # django-formtools # django-jsonform + # django-log-outgoing-requests # django-markup # django-otp # django-phonenumber-field @@ -163,6 +165,10 @@ django-admin-index==3.1.0 # via # -r requirements/ci.txt # open-api-framework +django-appconf==1.0.6 + # via + # -r requirements/ci.txt + # django-log-outgoing-requests django-axes==6.3.0 # via # -r requirements/ci.txt @@ -189,6 +195,8 @@ django-jsonform==2.21.4 # -r requirements/ci.txt # mozilla-django-oidc-db # open-api-framework +django-log-outgoing-requests==0.6.1 + # via -r requirements/ci.txt django-markup==1.8.1 # via # -r requirements/ci.txt @@ -233,6 +241,7 @@ django-solo==2.2.0 # via # -r requirements/ci.txt # commonground-api-common + # django-log-outgoing-requests # mozilla-django-oidc-db # notifications-api-common # zgw-consumers @@ -507,6 +516,7 @@ requests==2.31.0 # -r requirements/ci.txt # commonground-api-common # coreapi + # django-log-outgoing-requests # gemma-zds-client # mozilla-django-oidc # open-api-framework diff --git a/src/objects/conf/base.py b/src/objects/conf/base.py index 71f60cef..6a1cde85 100644 --- a/src/objects/conf/base.py +++ b/src/objects/conf/base.py @@ -2,6 +2,7 @@ from django.urls import reverse_lazy +from log_outgoing_requests.formatters import HttpFormatter from sentry_sdk.integrations import django, redis from .api import * # noqa @@ -98,6 +99,7 @@ # External applications. "axes", "drf_spectacular", + "log_outgoing_requests", "mozilla_django_oidc", "mozilla_django_oidc_db", "django_jsonform", @@ -231,6 +233,8 @@ FIXTURE_DIRS = (os.path.join(DJANGO_PROJECT_DIR, "fixtures"),) LOGGING_DIR = os.path.join(BASE_DIR, "log") +LOG_REQUESTS = config("LOG_REQUESTS", default=False) +LOG_OUTGOING_REQUESTS_DB_SAVE = config("LOG_OUTGOING_REQUESTS_DB_SAVE", default=False) LOGGING = { "version": 1, @@ -244,6 +248,7 @@ "performance": { "format": "%(asctime)s %(process)d | %(thread)d | %(message)s", }, + "outgoing_requests": {"()": HttpFormatter}, }, "filters": { "require_debug_false": {"()": "django.utils.log.RequireDebugFalse"}, @@ -287,6 +292,15 @@ "maxBytes": 1024 * 1024 * 10, # 10 MB "backupCount": 10, }, + "log_outgoing_requests": { + "level": "DEBUG", + "formatter": "outgoing_requests", + "class": "logging.StreamHandler", + }, + "save_outgoing_requests": { + "level": "DEBUG", + "class": "log_outgoing_requests.handlers.DatabaseOutgoingRequestsHandler", + }, }, "loggers": { "objects": { @@ -308,6 +322,13 @@ "handlers": ["project"], "level": "DEBUG", }, + "log_outgoing_requests": { + "handlers": ["log_outgoing_requests", "save_outgoing_requests"] + if LOG_REQUESTS + else [], + "level": "DEBUG", + "propagate": True, + }, }, } diff --git a/src/objects/fixtures/default_admin_index.json b/src/objects/fixtures/default_admin_index.json index 48aa5b94..d648834e 100644 --- a/src/objects/fixtures/default_admin_index.json +++ b/src/objects/fixtures/default_admin_index.json @@ -43,6 +43,14 @@ [ "mozilla_django_oidc_db", "openidconnectconfig" + ], + [ + "log_outgoing_requests", + "outgoingrequestslog" + ], + [ + "log_outgoing_requests", + "outgoingrequestslogconfig" ] ] }