Repository navigation
Expand file tree
/
Copy pathgrpc.py
More file actions
54 lines (41 loc) · 1.56 KB
/
Copy pathgrpc.py
File metadata and controls
54 lines (41 loc) · 1.56 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
50
51
52
53
54
import asyncio
import logging
from typing import Optional
import grpc
from feast.permissions.auth.auth_manager import (
get_auth_manager,
)
from feast.permissions.security_manager import get_security_manager
from feast.permissions.server.utils import (
AuthManagerType,
)
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
def grpc_interceptors(
auth_type: AuthManagerType,
) -> Optional[list[grpc.ServerInterceptor]]:
"""
A list of the authorization interceptors.
Args:
auth_type: The type of authorization manager, from the feature store configuration.
Returns:
list[grpc.ServerInterceptor]: Optional list of interceptors. If the authorization type is set to `NONE`, it returns `None`.
"""
if auth_type == AuthManagerType.NONE:
return None
return [AuthInterceptor()]
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)
)
print(f"Fetching user for token: {len(access_token)}")
current_user = asyncio.run(
auth_manager.token_parser.user_details_from_access_token(access_token)
)
print(f"User is: {current_user}")
sm.set_current_user(current_user)
return continuation(handler_call_details)