Skip to content

Commit 8c8d945

Browse files
committed
Security logs: delete old user security logs
1 parent 02a42b9 commit 8c8d945

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

readthedocs/audit/taks.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import logging
2+
3+
from django.conf import settings
4+
from django.utils import timezone
5+
6+
from readthedocs.audit.models import AuditLog
7+
from readthedocs.worker import app
8+
9+
log = logging.getLogger(__name__)
10+
11+
12+
@app.task(queue='web')
13+
def delete_old_personal_audit_logs(days=None):
14+
"""
15+
Delete audit logs from related to users older the `days`.
16+
17+
We only delete logs related to users,
18+
logs related to an organization are ignored here.
19+
There are tasks com .com to delete those according to their plan.
20+
"""
21+
days = days or settings.RTD_AUDITLOGS_DEFAULT_RETENTION_DAYS
22+
days_ago = timezone.now() - timezone.timedelta(days=days)
23+
audit_logs = AuditLog.objects.filter(
24+
log_user_id__isnull=False,
25+
log_organization_id__isnull=True,
26+
created__lt=days_ago,
27+
)
28+
log.info("Deleting old audit logs from users. days=%s logs=%s", days, audit_logs.count())
29+
audit_logs.delete()

readthedocs/settings/base.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,11 @@ def TEMPLATES(self):
422422
'schedule': crontab(minute=0, hour=1),
423423
'options': {'queue': 'web'},
424424
},
425+
'every-week-delete-old-personal-audit-logs': {
426+
'task': 'readthedocs.audi.tasks.delete_old_personal_audit_logs',
427+
'schedule': crontab(day_of_week='wednesday', hour=7, minute=0),
428+
'options': {'queue': 'web'},
429+
},
425430
'every-day-resync-sso-organization-users': {
426431
'task': 'readthedocs.oauth.tasks.sync_remote_repositories_organizations',
427432
'schedule': crontab(minute=0, hour=4),

0 commit comments

Comments
 (0)