Repository navigation
Expand file tree
/
Copy pathaudit_middleware.py
More file actions
144 lines (122 loc) · 5.02 KB
/
Copy pathaudit_middleware.py
File metadata and controls
144 lines (122 loc) · 5.02 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# Copyright 2025 The Feast Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
FastAPI middleware for structured audit logging of REST endpoints.
``AuditLoggingMiddleware`` logs every HTTP request/response on the REST
endpoints (``/get-online-features``, ``/push``, etc.) with principal,
resource, outcome, and duration.
MCP tool-call auditing is handled at the protocol layer by wrapping
the ``tools/call`` handler inside ``add_mcp_support_to_app()`` (see
``feast.infra.mcp_servers.mcp_server``).
The middleware is added only when ``audit_logging.enabled`` is ``true``
in the feature-server configuration.
"""
import logging
import time
from typing import Optional
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.requests import Request
from starlette.responses import Response
from feast.audit.audit_logger import (
AuditLogger,
AuditPrincipal,
AuditResource,
AuditSource,
mcp_audit_request_id,
)
logger = logging.getLogger(__name__)
# REST paths that correspond to read/write actions
_PATH_RESOURCE_MAP = {
"/get-online-features": ("feature_service", ["READ_ONLINE"]),
"/retrieve-online-documents": ("feature_service", ["READ_ONLINE"]),
"/push": ("push_source", ["WRITE_ONLINE", "WRITE_OFFLINE"]),
"/write-to-online-store": ("feature_view", ["WRITE_ONLINE"]),
"/materialize": ("feature_view", ["WRITE_ONLINE"]),
"/materialize-incremental": ("feature_view", ["WRITE_ONLINE"]),
}
def _extract_client_ip(request: Request) -> str:
forwarded = request.headers.get("x-forwarded-for", "")
if forwarded:
return forwarded.split(",")[0].strip()
if request.client:
return request.client.host
return ""
def _principal_from_request(request: Request) -> AuditPrincipal:
"""Build an ``AuditPrincipal`` from the security manager's current user."""
try:
from feast.permissions.security_manager import get_security_manager
sm = get_security_manager()
if sm and sm.current_user:
user = sm.current_user
return AuditPrincipal(
username=user.username,
roles=list(user.roles) if user.roles else [],
auth_type=request.headers.get("x-feast-auth-type", ""),
)
except Exception:
pass
return AuditPrincipal()
class AuditLoggingMiddleware(BaseHTTPMiddleware):
"""Emit ``http.request`` audit events for REST endpoints."""
async def dispatch(self, request: Request, call_next): # type: ignore[override]
audit: Optional[AuditLogger] = getattr(request.app.state, "audit_logger", None)
if audit is None:
return await call_next(request)
path = request.url.path
# Skip health and static endpoints
if path in ("/health", "/docs", "/openapi.json") or path.startswith("/static"):
return await call_next(request)
# Skip MCP transport endpoints — tool-call auditing is handled at the
# protocol layer in mcp_server._wrap_call_tool_handler().
if path.startswith("/mcp"):
return await call_next(request)
# If this request was triggered by an MCP tool call, reuse the same
# request_id so both events correlate in SIEM.
propagated_id = mcp_audit_request_id.get()
request_id = (
propagated_id
or request.headers.get("x-request-id")
or audit.new_request_id()
)
request.state.audit_request_id = request_id
start = time.monotonic()
response: Response
outcome = "success"
status_code = 200
try:
response = await call_next(request)
status_code = response.status_code
if status_code >= 400:
outcome = "failure"
except Exception:
outcome = "error"
status_code = 500
raise
finally:
duration_ms = (time.monotonic() - start) * 1000.0
resource_info = _PATH_RESOURCE_MAP.get(path, ("", []))
audit.log_http_request(
request_id=request_id,
method=request.method,
path=path,
principal=_principal_from_request(request),
source=AuditSource(ip=_extract_client_ip(request), transport="http"),
resource=AuditResource(
type=resource_info[0], actions=list(resource_info[1])
),
outcome=outcome,
duration_ms=round(duration_ms, 2),
status_code=status_code,
)
return response