Skip to content

Commit

Permalink
[#2762] update settings: log outgoing requests
Browse files Browse the repository at this point in the history
  • Loading branch information
pi-sigma committed May 31, 2023
1 parent 618e663 commit 4920b84
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 11 deletions.
1 change: 1 addition & 0 deletions requirements/base.in
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ django-cors-headers
django-decorator-include
django-digid-eherkenning
django-hijack
django-log-outgoing-requests
django-modeltranslation
django-ordered-model
django-privates
Expand Down
8 changes: 6 additions & 2 deletions requirements/base.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#
# This file is autogenerated by pip-compile with Python 3.10
# by the following command:
# This file is autogenerated by pip-compile with python 3.10
# To update, run:
#
# ./bin/compile_dependencies.sh
#
Expand Down Expand Up @@ -103,6 +103,7 @@ django==3.2.19
# django-filter
# django-formtools
# django-hijack
# django-log-outgoing-requests
# django-modeltranslation
# django-otp
# django-phonenumber-field
Expand Down Expand Up @@ -166,6 +167,8 @@ django-hijack==3.1.6
# via -r requirements/base.in
django-ipware==3.0.1
# via django-axes
django-log-outgoing-requests==0.1.0
# via -r requirements/base.in
django-modeltranslation==0.18.5
# via -r requirements/base.in
django-ordered-model==3.6
Expand Down Expand Up @@ -376,6 +379,7 @@ redis==4.5.4
requests==2.31.0
# via
# django-camunda
# django-log-outgoing-requests
# gemma-zds-client
# maykin-python3-saml
# mozilla-django-oidc
Expand Down
10 changes: 8 additions & 2 deletions requirements/ci.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#
# This file is autogenerated by pip-compile with Python 3.10
# by the following command:
# This file is autogenerated by pip-compile with python 3.10
# To update, run:
#
# ./bin/compile_dependencies.sh
#
Expand Down Expand Up @@ -178,6 +178,7 @@ django==3.2.19
# django-formtools
# django-hijack
# django-jenkins
# django-log-outgoing-requests
# django-modeltranslation
# django-otp
# django-phonenumber-field
Expand Down Expand Up @@ -280,6 +281,10 @@ django-ipware==3.0.1
# django-axes
django-jenkins==0.110.0
# via -r requirements/test-tools.in
django-log-outgoing-requests==0.1.0
# via
# -c requirements/base.txt
# -r requirements/base.txt
django-modeltranslation==0.18.5
# via
# -c requirements/base.txt
Expand Down Expand Up @@ -725,6 +730,7 @@ requests==2.31.0
# -c requirements/base.txt
# -r requirements/base.txt
# django-camunda
# django-log-outgoing-requests
# gemma-zds-client
# maykin-python3-saml
# mozilla-django-oidc
Expand Down
10 changes: 8 additions & 2 deletions requirements/dev.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#
# This file is autogenerated by pip-compile with Python 3.10
# by the following command:
# This file is autogenerated by pip-compile with python 3.10
# To update, run:
#
# ./bin/compile_dependencies.sh
#
Expand Down Expand Up @@ -205,6 +205,7 @@ django==3.2.19
# django-formtools
# django-hijack
# django-jenkins
# django-log-outgoing-requests
# django-modeltranslation
# django-otp
# django-phonenumber-field
Expand Down Expand Up @@ -315,6 +316,10 @@ django-jenkins==0.110.0
# via
# -c requirements/ci.txt
# -r requirements/ci.txt
django-log-outgoing-requests==0.1.0
# via
# -c requirements/ci.txt
# -r requirements/ci.txt
django-modeltranslation==0.18.5
# via
# -c requirements/ci.txt
Expand Down Expand Up @@ -858,6 +863,7 @@ requests==2.31.0
# -r requirements/ci.txt
# ddt-api-calls
# django-camunda
# django-log-outgoing-requests
# django-rosetta
# django-silk
# gemma-zds-client
Expand Down
10 changes: 8 additions & 2 deletions requirements/extensions.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#
# This file is autogenerated by pip-compile with Python 3.10
# by the following command:
# This file is autogenerated by pip-compile with python 3.10
# To update, run:
#
# ./bin/compile_dependencies.sh
#
Expand Down Expand Up @@ -142,6 +142,7 @@ django==3.2.19
# django-filter
# django-formtools
# django-hijack
# django-log-outgoing-requests
# django-modeltranslation
# django-otp
# django-phonenumber-field
Expand Down Expand Up @@ -240,6 +241,10 @@ django-ipware==3.0.1
# via
# -r requirements/base.txt
# django-axes
django-log-outgoing-requests==0.1.0
# via
# -c requirements/base.in
# -r requirements/base.txt
django-modeltranslation==0.18.5
# via
# -c requirements/base.in
Expand Down Expand Up @@ -579,6 +584,7 @@ requests==2.31.0
# via
# -r requirements/base.txt
# django-camunda
# django-log-outgoing-requests
# gemma-zds-client
# maykin-python3-saml
# mozilla-django-oidc
Expand Down
35 changes: 35 additions & 0 deletions src/openforms/conf/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import sentry_sdk
from celery.schedules import crontab
from corsheaders.defaults import default_headers as default_cors_headers
from log_outgoing_requests.formatters import HttpFormatter

from csp_post_processor.constants import NONCE_HTTP_HEADER

Expand Down Expand Up @@ -185,6 +186,7 @@
"cspreports",
"csp_post_processor",
"django_camunda",
"log_outgoing_requests",
# Project applications.
"openforms.accounts",
"openforms.analytics_tools",
Expand Down Expand Up @@ -376,6 +378,7 @@
"performance": {
"format": "%(asctime)s %(process)d | %(thread)d | %(message)s",
},
"outgoing_requests": {"()": HttpFormatter},
},
"filters": {
"require_debug_false": {"()": "django.utils.log.RequireDebugFalse"},
Expand Down Expand Up @@ -419,6 +422,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": {
"openforms": {
Expand All @@ -445,9 +457,27 @@
"handlers": ["project"] if not LOG_STDOUT else ["console"],
"level": "DEBUG",
},
"requests": {
"handlers": ["log_outgoing_requests", "save_outgoing_requests"],
"level": "DEBUG",
"propagate": True,
},
},
}

LOG_OUTGOING_REQUESTS_DB_SAVE = True
LOG_OUTGOING_REQUESTS_DB_SAVE_BODY = True
LOG_OUTGOING_REQUESTS_CONTENT_TYPES = [
"text/*",
"application/json",
"application/xml",
"application/soap+xml",
]
LOG_OUTGOING_REQUESTS_EMIT_BODY = True
LOG_OUTGOING_REQUESTS_MAX_AGE = config("LOG_OUTGOING_REQUESTS_MAX_AGE", default=7 * 24)
LOG_OUTGOING_REQUESTS_MAX_CONTENT_LENGTH = 524_288 # 0.5MB
LOG_OUTGOING_REQUESTS_LOG_BODY_TO_STDOUT = True

#
# AUTH settings - user accounts, passwords, backends...
#
Expand Down Expand Up @@ -668,6 +698,11 @@
"task": "openforms.forms.admin.tasks.clear_forms_export",
"schedule": crontab(hour=0, minute=0, day_of_week="sunday"),
},
"cleanup-outgoing-request-logs": {
"task": "openforms.logging.tasks.cleanup_request_logs",
"schedule": crontab(hour=0, minute=0, day_of_week="*"),
"args": (LOG_OUTGOING_REQUESTS_MAX_AGE,),
},
}

RETRY_SUBMISSIONS_TIME_LIMIT = config(
Expand Down
2 changes: 1 addition & 1 deletion src/openforms/fixtures/admin_index_unlisted.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@
"forms.FormLogic",
"of_authentication.AuthInfo",
"of_authentication.RegistratorInfo",
"upgrades.VersionInfo"
"upgrades.VersionInfo",
]
14 changes: 13 additions & 1 deletion src/openforms/logging/tasks.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from datetime import datetime
from datetime import datetime, timedelta
from typing import List, TypedDict

from django.utils import timezone

from log_outgoing_requests.models import OutgoingRequestsLog

from openforms.celery import app
from openforms.forms.models import FormLogic
from openforms.typing import JSONObject, JSONValue
Expand All @@ -12,6 +16,14 @@ class EvaluatedRuleDict(TypedDict):
action_log_data: dict[int, JSONValue]


@app.task()
def cleanup_request_logs(log_requests_max_age: int):
local_time = timezone.localtime()
delta = timedelta(hours=log_requests_max_age)

OutgoingRequestsLog.objects.filter(timestamp__lte=local_time - delta).delete()


@app.task(ignore_result=True)
def log_logic_evaluation(
*,
Expand Down
21 changes: 20 additions & 1 deletion src/openforms/logging/tests/test_tasks.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from datetime import timedelta

from django.test import TestCase
from django.utils import timezone

from log_outgoing_requests.models import OutgoingRequestsLog

from openforms.logging.models import TimelineLogProxy
from openforms.logging.tasks import log_logic_evaluation
from openforms.logging.tasks import cleanup_request_logs, log_logic_evaluation
from openforms.submissions.tests.factories import SubmissionFactory


Expand All @@ -20,3 +24,18 @@ def test_no_logged_rules(self):
)

self.assertEqual(0, TimelineLogProxy.objects.count())

def test_cleanup_request_logs(self):
"""Assert that logs are cleaned if and only if created before specified time"""

log_requests_max_age = 1 # 1 hour
delta = timedelta(hours=log_requests_max_age + 1)

OutgoingRequestsLog.objects.create(timestamp=timezone.localtime() - delta)
OutgoingRequestsLog.objects.create(timestamp=timezone.localtime())

self.assertEqual(OutgoingRequestsLog.objects.count(), 2)

cleanup_request_logs(log_requests_max_age)

self.assertEqual(OutgoingRequestsLog.objects.count(), 1)

0 comments on commit 4920b84

Please sign in to comment.