Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
20 changes: 13 additions & 7 deletions src/sentry/utils/audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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())
Expand Down
6 changes: 4 additions & 2 deletions tests/sentry/utils/test_audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,18 @@ 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"]

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
Expand Down