-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathgrpc.py
More file actions
49 lines (39 loc) · 1.63 KB
/
Copy pathgrpc.py
File metadata and controls
49 lines (39 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import asyncio
import logging
import grpc
from feast.permissions.auth.auth_manager import (
get_auth_manager,
)
from feast.permissions.security_manager import get_security_manager
logger = logging.getLogger(__name__)
class AuthInterceptor(grpc.ServerInterceptor):
def intercept_service(self, continuation, handler_call_details):
sm = get_security_manager()
if sm is not None:
auth_manager = get_auth_manager()
access_token = auth_manager.token_extractor.extract_access_token(
metadata=dict(handler_call_details.invocation_metadata)
)
logger.debug(
f"Fetching user details for token of length: {len(access_token)}"
)
current_user = asyncio.run(
auth_manager.token_parser.user_details_from_access_token(access_token)
)
logger.debug(f"User is: {current_user}")
sm.set_current_user(current_user)
handler = continuation(handler_call_details)
if sm is None or handler is None or handler.unary_unary is None:
return handler
behavior = handler.unary_unary
def project_aware_behavior(request, context):
token = sm.set_current_project(getattr(request, "project", None))
try:
return behavior(request, context)
finally:
sm.reset_current_project(token)
return grpc.unary_unary_rpc_method_handler(
project_aware_behavior,
request_deserializer=handler.request_deserializer,
response_serializer=handler.response_serializer,
)