diff --git a/pyproject.toml b/pyproject.toml index 401415a9492e04..996348f65409e5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -476,7 +476,6 @@ module = [ "sentry.tsdb.dummy", "sentry.tsdb.inmemory", "sentry.types.integrations", - "sentry.utils.audit", "sentry.utils.auth", "sentry.utils.committers", "sentry.utils.distutils.commands.base", @@ -594,6 +593,7 @@ module = [ "sentry.tasks.commit_context", "sentry.tasks.on_demand_metrics", "sentry.tasks.reprocessing2", + "sentry.utils.audit", "sentry.utils.email.*", "sentry.utils.iterators", "sentry.utils.locking.backends.redis", diff --git a/src/sentry/utils/audit.py b/src/sentry/utils/audit.py index 5fbd73ef5ce154..bef4c9a7a73b65 100644 --- a/src/sentry/utils/audit.py +++ b/src/sentry/utils/audit.py @@ -53,6 +53,17 @@ def actor_from_audit_entry(entry: AuditLogEntry) -> RpcAuditLogEntryActor: ) +def _org_id(org: Organization | RpcOrganization | None, org_id: int | None) -> int: + if org is not None and org_id is not None: + raise TypeError("expected organization=... or organization_id=... not both!") + elif org is not None: + return org.id + elif org_id is not None: + return org_id + else: + raise TypeError("expected organization=... or organization_id=...") + + def create_audit_entry_from_user( user: User | RpcUser | None, api_key: ApiKey | None = None, @@ -63,9 +74,7 @@ def create_audit_entry_from_user( organization_id: int | None = None, **kwargs: Any, ) -> AuditLogEntry: - if organization: - assert organization_id is None - organization_id = organization.id + organization_id = _org_id(organization, organization_id) entry = AuditLogEntry( actor_id=user.id if user else None, @@ -203,10 +212,7 @@ def create_system_audit_entry( Creates an audit log entry for events that are triggered by Sentry's systems and do not have an associated Sentry user as the "actor". """ - if organization: - assert organization_id is None - organization_id = organization.id - + organization_id = _org_id(organization, organization_id) entry = AuditLogEntry(actor_label="Sentry", organization_id=organization_id, **kwargs) if entry.event is not None: log_service.record_audit_log(event=entry.as_event()) diff --git a/tests/sentry/utils/test_audit.py b/tests/sentry/utils/test_audit.py index 8156ab258ca59e..bab4251c77cbb3 100644 --- a/tests/sentry/utils/test_audit.py +++ b/tests/sentry/utils/test_audit.py @@ -49,7 +49,7 @@ def test_audit_entry_api(self): req = fake_http_request(AnonymousUser()) req.auth = apikey - entry = create_audit_entry(req) + entry = create_audit_entry(req, organization_id=org.id) assert entry.actor_key == apikey assert entry.actor is None assert entry.ip_address == req.META["REMOTE_ADDR"] @@ -57,8 +57,10 @@ def test_audit_entry_api(self): self.assert_no_delete_log_created() def test_audit_entry_frontend(self): + org = self.create_organization() + req = fake_http_request(self.create_user()) - entry = create_audit_entry(req) + entry = create_audit_entry(req, organization_id=org.id) assert entry.actor == req.user assert entry.actor_key is None