From aad93e149e27c9d5b295ea60d8d36d51cc349678 Mon Sep 17 00:00:00 2001 From: Lauryn Menard Date: Fri, 6 Sep 2024 17:29:04 +0200 Subject: [PATCH] audit-log: Move remote server event types to AuditLogEventType enum. Event types moved: REMOTE_SERVER_DEACTIVATED, REMOTE_SERVER_REACTIVATED REMOTE_SERVER_PLAN_TYPE_CHANGED, REMOTE_SERVER_DISCOUNT_CHANGED REMOTE_SERVER_SPONSORSHIP_APPROVED, REMOTE_SERVER_BILLING_MODALITY_CHANGED REMOTE_SERVER_SPONSORSHIP_PENDING_STATUS_CHANGED, REMOTE_SERVER_CREATED --- corporate/lib/stripe.py | 24 ++++++++--------- corporate/lib/support.py | 3 ++- corporate/tests/test_activity_views.py | 4 +-- corporate/tests/test_stripe.py | 6 +++-- corporate/tests/test_support_views.py | 6 ++--- zerver/models/realm_audit_logs.py | 26 +++++++++---------- zerver/tests/test_push_notifications.py | 2 +- .../commands/populate_billing_realms.py | 3 ++- zilencer/management/commands/populate_db.py | 2 +- zilencer/views.py | 3 ++- 10 files changed, 42 insertions(+), 37 deletions(-) diff --git a/corporate/lib/stripe.py b/corporate/lib/stripe.py index 018c997086..628a3b98e1 100644 --- a/corporate/lib/stripe.py +++ b/corporate/lib/stripe.py @@ -4210,19 +4210,19 @@ class RemoteRealmBillingSession(BillingSession): elif event_type is BillingSessionEventType.CUSTOMER_PLAN_CREATED: return AuditLogEventType.CUSTOMER_PLAN_CREATED elif event_type is BillingSessionEventType.DISCOUNT_CHANGED: - return RemoteRealmAuditLog.REMOTE_SERVER_DISCOUNT_CHANGED + return AuditLogEventType.REMOTE_SERVER_DISCOUNT_CHANGED elif event_type is BillingSessionEventType.CUSTOMER_PROPERTY_CHANGED: return AuditLogEventType.CUSTOMER_PROPERTY_CHANGED # nocoverage elif event_type is BillingSessionEventType.SPONSORSHIP_APPROVED: - return RemoteRealmAuditLog.REMOTE_SERVER_SPONSORSHIP_APPROVED + return AuditLogEventType.REMOTE_SERVER_SPONSORSHIP_APPROVED elif event_type is BillingSessionEventType.SPONSORSHIP_PENDING_STATUS_CHANGED: - return RemoteRealmAuditLog.REMOTE_SERVER_SPONSORSHIP_PENDING_STATUS_CHANGED + return AuditLogEventType.REMOTE_SERVER_SPONSORSHIP_PENDING_STATUS_CHANGED elif event_type is BillingSessionEventType.BILLING_MODALITY_CHANGED: - return RemoteRealmAuditLog.REMOTE_SERVER_BILLING_MODALITY_CHANGED # nocoverage + return AuditLogEventType.REMOTE_SERVER_BILLING_MODALITY_CHANGED # nocoverage elif event_type is BillingSessionEventType.CUSTOMER_PLAN_PROPERTY_CHANGED: return AuditLogEventType.CUSTOMER_PLAN_PROPERTY_CHANGED elif event_type is BillingSessionEventType.BILLING_ENTITY_PLAN_TYPE_CHANGED: - return RemoteRealmAuditLog.REMOTE_SERVER_PLAN_TYPE_CHANGED + return AuditLogEventType.REMOTE_SERVER_PLAN_TYPE_CHANGED elif ( event_type is BillingSessionEventType.CUSTOMER_SWITCHED_FROM_MONTHLY_TO_ANNUAL_PLAN ): # nocoverage @@ -4653,19 +4653,19 @@ class RemoteServerBillingSession(BillingSession): elif event_type is BillingSessionEventType.CUSTOMER_PLAN_CREATED: return AuditLogEventType.CUSTOMER_PLAN_CREATED elif event_type is BillingSessionEventType.DISCOUNT_CHANGED: - return RemoteZulipServerAuditLog.REMOTE_SERVER_DISCOUNT_CHANGED # nocoverage + return AuditLogEventType.REMOTE_SERVER_DISCOUNT_CHANGED # nocoverage elif event_type is BillingSessionEventType.CUSTOMER_PROPERTY_CHANGED: return AuditLogEventType.CUSTOMER_PROPERTY_CHANGED # nocoverage elif event_type is BillingSessionEventType.SPONSORSHIP_APPROVED: - return RemoteZulipServerAuditLog.REMOTE_SERVER_SPONSORSHIP_APPROVED + return AuditLogEventType.REMOTE_SERVER_SPONSORSHIP_APPROVED elif event_type is BillingSessionEventType.SPONSORSHIP_PENDING_STATUS_CHANGED: - return RemoteZulipServerAuditLog.REMOTE_SERVER_SPONSORSHIP_PENDING_STATUS_CHANGED + return AuditLogEventType.REMOTE_SERVER_SPONSORSHIP_PENDING_STATUS_CHANGED elif event_type is BillingSessionEventType.BILLING_MODALITY_CHANGED: - return RemoteZulipServerAuditLog.REMOTE_SERVER_BILLING_MODALITY_CHANGED # nocoverage + return AuditLogEventType.REMOTE_SERVER_BILLING_MODALITY_CHANGED # nocoverage elif event_type is BillingSessionEventType.CUSTOMER_PLAN_PROPERTY_CHANGED: return AuditLogEventType.CUSTOMER_PLAN_PROPERTY_CHANGED # nocoverage elif event_type is BillingSessionEventType.BILLING_ENTITY_PLAN_TYPE_CHANGED: - return RemoteZulipServerAuditLog.REMOTE_SERVER_PLAN_TYPE_CHANGED + return AuditLogEventType.REMOTE_SERVER_PLAN_TYPE_CHANGED elif ( event_type is BillingSessionEventType.CUSTOMER_SWITCHED_FROM_MONTHLY_TO_ANNUAL_PLAN ): # nocoverage @@ -5168,7 +5168,7 @@ def do_reactivate_remote_server(remote_server: RemoteZulipServer) -> None: remote_server.deactivated = False remote_server.save(update_fields=["deactivated"]) RemoteZulipServerAuditLog.objects.create( - event_type=RealmAuditLog.REMOTE_SERVER_REACTIVATED, + event_type=AuditLogEventType.REMOTE_SERVER_REACTIVATED, server=remote_server, event_time=timezone_now(), ) @@ -5218,7 +5218,7 @@ def do_deactivate_remote_server( remote_server.deactivated = True remote_server.save(update_fields=["deactivated"]) RemoteZulipServerAuditLog.objects.create( - event_type=RealmAuditLog.REMOTE_SERVER_DEACTIVATED, + event_type=AuditLogEventType.REMOTE_SERVER_DEACTIVATED, server=remote_server, event_time=timezone_now(), ) diff --git a/corporate/lib/support.py b/corporate/lib/support.py index 192734c1c1..992c5ba760 100644 --- a/corporate/lib/support.py +++ b/corporate/lib/support.py @@ -29,6 +29,7 @@ from corporate.models import ( get_current_plan_by_customer, ) from zerver.models import Realm +from zerver.models.realm_audit_logs import AuditLogEventType from zerver.models.realms import get_org_type_display_name, get_realm from zilencer.lib.remote_counts import MissingDataError from zilencer.models import ( @@ -410,7 +411,7 @@ def get_data_for_remote_support_view(billing_session: BillingSession) -> RemoteS user_data = get_remote_server_guest_and_non_guest_count(billing_session.remote_server.id) stale_audit_log_data = has_stale_audit_log(billing_session.remote_server) date_created = RemoteZulipServerAuditLog.objects.get( - event_type=RemoteZulipServerAuditLog.REMOTE_SERVER_CREATED, + event_type=AuditLogEventType.REMOTE_SERVER_CREATED, server__id=billing_session.remote_server.id, ).event_time mobile_data = get_mobile_push_data(billing_session.remote_server) diff --git a/corporate/tests/test_activity_views.py b/corporate/tests/test_activity_views.py index 615e90d26d..19e7adea04 100644 --- a/corporate/tests/test_activity_views.py +++ b/corporate/tests/test_activity_views.py @@ -170,7 +170,7 @@ class ActivityTest(ZulipTestCase): contact_email="email@example.com", ) RemoteZulipServerAuditLog.objects.create( - event_type=RemoteZulipServerAuditLog.REMOTE_SERVER_CREATED, + event_type=AuditLogEventType.REMOTE_SERVER_CREATED, server=server, event_time=server.last_updated, ) @@ -303,7 +303,7 @@ class ActivityTest(ZulipTestCase): hostname=hostname, contact_email=f"admin@{hostname}", uuid=uuid.uuid4() ) RemoteZulipServerAuditLog.objects.create( - event_type=RemoteZulipServerAuditLog.REMOTE_SERVER_CREATED, + event_type=AuditLogEventType.REMOTE_SERVER_CREATED, server=remote_server, event_time=remote_server.last_updated, ) diff --git a/corporate/tests/test_stripe.py b/corporate/tests/test_stripe.py index efac918276..6e1edf59ca 100644 --- a/corporate/tests/test_stripe.py +++ b/corporate/tests/test_stripe.py @@ -5486,7 +5486,7 @@ class BillingHelpersTest(ZulipTestCase): remote_server = RemoteZulipServer.objects.get(uuid=server_uuid) remote_realm_audit_log = RemoteZulipServerAuditLog.objects.filter( - event_type=RealmAuditLog.REMOTE_SERVER_DEACTIVATED + event_type=AuditLogEventType.REMOTE_SERVER_DEACTIVATED ).last() assert remote_realm_audit_log is not None self.assertTrue(remote_server.deactivated) @@ -5506,7 +5506,9 @@ class BillingHelpersTest(ZulipTestCase): remote_server.refresh_from_db() self.assertFalse(remote_server.deactivated) remote_realm_audit_log = RemoteZulipServerAuditLog.objects.latest("id") - self.assertEqual(remote_realm_audit_log.event_type, RealmAuditLog.REMOTE_SERVER_REACTIVATED) + self.assertEqual( + remote_realm_audit_log.event_type, AuditLogEventType.REMOTE_SERVER_REACTIVATED + ) self.assertEqual(remote_realm_audit_log.server, remote_server) with self.assertLogs("corporate.stripe", "WARN") as warning_log: diff --git a/corporate/tests/test_support_views.py b/corporate/tests/test_support_views.py index df901df3b5..5067e21b5e 100644 --- a/corporate/tests/test_support_views.py +++ b/corporate/tests/test_support_views.py @@ -116,7 +116,7 @@ class TestRemoteServerSupportEndpoint(ZulipTestCase): hostname=hostname, contact_email=f"admin@{hostname}", uuid=uuid.uuid4() ) RemoteZulipServerAuditLog.objects.create( - event_type=RemoteZulipServerAuditLog.REMOTE_SERVER_CREATED, + event_type=AuditLogEventType.REMOTE_SERVER_CREATED, server=remote_server, event_time=remote_server.last_updated, ) @@ -688,7 +688,7 @@ class TestRemoteServerSupportEndpoint(ZulipTestCase): remote_server_no_upgrade.refresh_from_db() self.assertTrue(remote_server_no_upgrade.deactivated) audit_log = RemoteZulipServerAuditLog.objects.filter( - event_type=RemoteZulipServerAuditLog.REMOTE_SERVER_DEACTIVATED + event_type=AuditLogEventType.REMOTE_SERVER_DEACTIVATED ).last() assert audit_log is not None self.assertEqual(audit_log.server, remote_server_no_upgrade) @@ -709,7 +709,7 @@ class TestRemoteServerSupportEndpoint(ZulipTestCase): remote_server.refresh_from_db() self.assertFalse(remote_server.deactivated) audit_log = RemoteZulipServerAuditLog.objects.filter( - event_type=RemoteZulipServerAuditLog.REMOTE_SERVER_REACTIVATED + event_type=AuditLogEventType.REMOTE_SERVER_REACTIVATED ).last() assert audit_log is not None self.assertEqual(audit_log.server, remote_server) diff --git a/zerver/models/realm_audit_logs.py b/zerver/models/realm_audit_logs.py index 8d716f21a4..4d4926911d 100644 --- a/zerver/models/realm_audit_logs.py +++ b/zerver/models/realm_audit_logs.py @@ -107,6 +107,19 @@ class AuditLogEventType(IntEnum): USER_GROUP_DESCRIPTION_CHANGED = 721 USER_GROUP_GROUP_BASED_SETTING_CHANGED = 722 + # The following values are only for remote server/realm logs. + # Values should be exactly 10000 greater than the corresponding + # value used for the same purpose in realm audit logs (e.g., + # REALM_DEACTIVATED = 201, and REMOTE_SERVER_DEACTIVATED = 10201). + REMOTE_SERVER_DEACTIVATED = 10201 + REMOTE_SERVER_REACTIVATED = 10202 + REMOTE_SERVER_PLAN_TYPE_CHANGED = 10204 + REMOTE_SERVER_DISCOUNT_CHANGED = 10209 + REMOTE_SERVER_SPONSORSHIP_APPROVED = 10210 + REMOTE_SERVER_BILLING_MODALITY_CHANGED = 10211 + REMOTE_SERVER_SPONSORSHIP_PENDING_STATUS_CHANGED = 10213 + REMOTE_SERVER_CREATED = 10215 + class AbstractRealmAuditLog(models.Model): """Defines fields common to RealmAuditLog and RemoteRealmAuditLog.""" @@ -128,19 +141,6 @@ class AbstractRealmAuditLog(models.Model): # Event types - # The following values are only for RemoteZulipServerAuditLog - # Values should be exactly 10000 greater than the corresponding - # value used for the same purpose in RealmAuditLog (e.g. - # REALM_DEACTIVATED = 201, and REMOTE_SERVER_DEACTIVATED = 10201). - REMOTE_SERVER_DEACTIVATED = 10201 - REMOTE_SERVER_REACTIVATED = 10202 - REMOTE_SERVER_PLAN_TYPE_CHANGED = 10204 - REMOTE_SERVER_DISCOUNT_CHANGED = 10209 - REMOTE_SERVER_SPONSORSHIP_APPROVED = 10210 - REMOTE_SERVER_BILLING_MODALITY_CHANGED = 10211 - REMOTE_SERVER_SPONSORSHIP_PENDING_STATUS_CHANGED = 10213 - REMOTE_SERVER_CREATED = 10215 - # This value is for RemoteRealmAuditLog entries tracking changes to the # RemoteRealm model resulting from modified realm information sent to us # via send_server_data_to_push_bouncer. diff --git a/zerver/tests/test_push_notifications.py b/zerver/tests/test_push_notifications.py index 5630b111b3..1cd7678165 100644 --- a/zerver/tests/test_push_notifications.py +++ b/zerver/tests/test_push_notifications.py @@ -5164,7 +5164,7 @@ class PushBouncerSignupTest(ZulipTestCase): server = RemoteZulipServer.objects.get(uuid=zulip_org_id) remote_realm_audit_log = RemoteZulipServerAuditLog.objects.filter( - event_type=RealmAuditLog.REMOTE_SERVER_DEACTIVATED + event_type=AuditLogEventType.REMOTE_SERVER_DEACTIVATED ).last() assert remote_realm_audit_log is not None self.assertTrue(server.deactivated) diff --git a/zilencer/management/commands/populate_billing_realms.py b/zilencer/management/commands/populate_billing_realms.py index 8c66f558fa..9ccf44836d 100644 --- a/zilencer/management/commands/populate_billing_realms.py +++ b/zilencer/management/commands/populate_billing_realms.py @@ -28,6 +28,7 @@ from zerver.lib.management import ZulipBaseCommand from zerver.lib.remote_server import get_realms_info_for_push_bouncer from zerver.lib.streams import create_stream_if_needed from zerver.models import Realm, UserProfile +from zerver.models.realm_audit_logs import AuditLogEventType from zerver.models.realms import get_realm from zilencer.models import ( RemoteRealm, @@ -449,7 +450,7 @@ def populate_remote_server(customer_profile: CustomerProfile) -> dict[str, str]: ) RemoteZulipServerAuditLog.objects.create( - event_type=RemoteZulipServerAuditLog.REMOTE_SERVER_CREATED, + event_type=AuditLogEventType.REMOTE_SERVER_CREATED, server=remote_server, event_time=remote_server.last_updated, ) diff --git a/zilencer/management/commands/populate_db.py b/zilencer/management/commands/populate_db.py index 0981383787..cc2af9fea5 100644 --- a/zilencer/management/commands/populate_db.py +++ b/zilencer/management/commands/populate_db.py @@ -406,7 +406,7 @@ class Command(ZulipBaseCommand): contact_email="remotezulipserver@zulip.com", ) RemoteZulipServerAuditLog.objects.create( - event_type=RemoteZulipServerAuditLog.REMOTE_SERVER_CREATED, + event_type=AuditLogEventType.REMOTE_SERVER_CREATED, server=server, event_time=server.last_updated, ) diff --git a/zilencer/views.py b/zilencer/views.py index 09fbe693ab..5839d813b9 100644 --- a/zilencer/views.py +++ b/zilencer/views.py @@ -77,6 +77,7 @@ from zerver.lib.typed_endpoint import ( ) from zerver.lib.typed_endpoint_validators import check_string_fixed_length from zerver.lib.types import RemoteRealmDictValue +from zerver.models.realm_audit_logs import AuditLogEventType from zerver.models.realms import DisposableEmailError from zerver.views.push_notifications import validate_token from zilencer.auth import InvalidZulipServerKeyError @@ -226,7 +227,7 @@ def register_remote_server( last_request_datetime=timezone_now(), ) RemoteZulipServerAuditLog.objects.create( - event_type=RemoteZulipServerAuditLog.REMOTE_SERVER_CREATED, + event_type=AuditLogEventType.REMOTE_SERVER_CREATED, server=remote_server, event_time=remote_server.last_updated, )