export: Create REALM_EXPORTED audit log for exports via shell.

Earlier, we were creating RealmAuditLog with REALM_EXPORTED
event_type when export of public data took place via organization
settings panel.

We were not creating the audit log when the export was executed
via shell i.e './manage.py export'.

This commit creates the audit log in that case too. It will
help during import to distinguish readily between imports
from another Zulip server vs imports from another product.
This commit is contained in:
Prakhar Pratyush 2024-04-17 15:27:49 +05:30 committed by Tim Abbott
parent cf24d2c25e
commit cc793612f0
2 changed files with 16 additions and 2 deletions

View File

@ -2418,9 +2418,15 @@ def export_realm_wrapper(
def get_realm_exports_serialized(user: UserProfile) -> List[Dict[str, Any]]: def get_realm_exports_serialized(user: UserProfile) -> List[Dict[str, Any]]:
# Exclude exports made via shell. 'acting_user=None', since they
# aren't supported in the current API format.
#
# TODO: We should return those via the API as well, with an
# appropriate way to express for who issued them; this requires an
# API change.
all_exports = RealmAuditLog.objects.filter( all_exports = RealmAuditLog.objects.filter(
realm=user.realm, event_type=RealmAuditLog.REALM_EXPORTED realm=user.realm, event_type=RealmAuditLog.REALM_EXPORTED
) ).exclude(acting_user=None)
exports_dict = {} exports_dict = {}
for export in all_exports: for export in all_exports:
export_url = None export_url = None

View File

@ -5,12 +5,13 @@ from typing import Any
from django.conf import settings from django.conf import settings
from django.core.management.base import CommandError from django.core.management.base import CommandError
from django.utils.timezone import now as timezone_now
from typing_extensions import override from typing_extensions import override
from zerver.actions.realm_settings import do_deactivate_realm from zerver.actions.realm_settings import do_deactivate_realm
from zerver.lib.export import export_realm_wrapper from zerver.lib.export import export_realm_wrapper
from zerver.lib.management import ZulipBaseCommand from zerver.lib.management import ZulipBaseCommand
from zerver.models import Message, Reaction, UserProfile from zerver.models import Message, Reaction, RealmAuditLog, UserProfile
class Command(ZulipBaseCommand): class Command(ZulipBaseCommand):
@ -206,6 +207,13 @@ class Command(ZulipBaseCommand):
def percent_callback(bytes_transferred: Any) -> None: def percent_callback(bytes_transferred: Any) -> None:
print(end=".", flush=True) print(end=".", flush=True)
RealmAuditLog.objects.create(
acting_user=None,
realm=realm,
event_type=RealmAuditLog.REALM_EXPORTED,
event_time=timezone_now(),
)
# Allows us to trigger exports separately from command line argument parsing # Allows us to trigger exports separately from command line argument parsing
export_realm_wrapper( export_realm_wrapper(
realm=realm, realm=realm,