Skip to content

Commit 6c34968

Browse files
rkuo-danswerRichard Kuo (Onyx)
andauthored
improve impersonation logging slightly (#4758)
Co-authored-by: Richard Kuo (Onyx) <rkuo@onyx.app>
1 parent 3b64793 commit 6c34968

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

backend/ee/onyx/server/tenants/admin_api.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from fastapi import Depends
33
from fastapi import HTTPException
44
from fastapi import Response
5+
from fastapi_users import exceptions
56

67
from ee.onyx.auth.users import current_cloud_superuser
78
from ee.onyx.server.tenants.models import ImpersonateRequest
@@ -24,14 +25,24 @@ async def impersonate_user(
2425
_: User = Depends(current_cloud_superuser),
2526
) -> Response:
2627
"""Allows a cloud superuser to impersonate another user by generating an impersonation JWT token"""
27-
tenant_id = get_tenant_id_for_email(impersonate_request.email)
28+
try:
29+
tenant_id = get_tenant_id_for_email(impersonate_request.email)
30+
except exceptions.UserNotExists:
31+
detail = f"User has no tenant mapping: {impersonate_request.email=}"
32+
logger.warning(detail)
33+
raise HTTPException(status_code=422, detail=detail)
2834

2935
with get_session_with_tenant(tenant_id=tenant_id) as tenant_session:
3036
user_to_impersonate = get_user_by_email(
3137
impersonate_request.email, tenant_session
3238
)
3339
if user_to_impersonate is None:
34-
raise HTTPException(status_code=404, detail="User not found")
40+
detail = (
41+
f"User not found in tenant: {impersonate_request.email=} {tenant_id=}"
42+
)
43+
logger.warning(detail)
44+
raise HTTPException(status_code=422, detail=detail)
45+
3546
token = await get_redis_strategy().write_token(user_to_impersonate)
3647

3748
response = await auth_backend.transport.get_login_response(token)

backend/ee/onyx/server/tenants/user_mapping.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ def get_tenant_id_for_email(email: str) -> str:
4747
mapping.active = True
4848
db_session.commit()
4949
tenant_id = mapping.tenant_id
50-
5150
except Exception as e:
5251
logger.exception(f"Error getting tenant id for email {email}: {e}")
5352
raise exceptions.UserNotExists()
53+
5454
if tenant_id is None:
5555
raise exceptions.UserNotExists()
5656
return tenant_id

0 commit comments

Comments
 (0)