Repository navigation
Expand file tree
/
Copy pathaudit_logger.py
More file actions
381 lines (308 loc) · 11.7 KB
/
Copy pathaudit_logger.py
File metadata and controls
381 lines (308 loc) · 11.7 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# 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.
"""
Structured audit logging for the Feast feature server.
Emits JSONL audit events for MCP tool calls, REST requests, and
authentication/authorization decisions. Sensitive payloads (tokens,
entity rows, feature values) are never included.
"""
import abc
import contextvars
import logging
import sys
import threading
import uuid
from datetime import datetime, timezone
from typing import TYPE_CHECKING, Any, Dict, List, Optional
if TYPE_CHECKING:
from feast.infra.feature_servers.base_config import AuditLoggingConfig
from pydantic import BaseModel, Field
logger = logging.getLogger(__name__)
# ContextVar used by the MCP audit wrapper to propagate a single request_id
# into the internal REST call so that ``mcp.tools.call`` and
# ``http.request`` events share the same identifier in SIEM.
mcp_audit_request_id: contextvars.ContextVar[Optional[str]] = contextvars.ContextVar(
"mcp_audit_request_id", default=None
)
# ---------------------------------------------------------------------------
# Audit event schema
# ---------------------------------------------------------------------------
class AuditPrincipal(BaseModel):
username: str = ""
roles: List[str] = Field(default_factory=list)
auth_type: str = ""
class AuditSource(BaseModel):
ip: str = ""
transport: str = ""
class AuditAction(BaseModel):
mcp_tool: str = ""
path: str = ""
class AuditResource(BaseModel):
type: str = ""
name: str = ""
actions: List[str] = Field(default_factory=list)
class AuditEvent(BaseModel):
"""A single structured audit log entry.
Fields follow the schema proposed in feast-dev/feast#6452.
No sensitive payloads (tokens, entity rows, feature values) are stored.
When OpenTelemetry is active, ``trace_id`` and ``span_id`` are populated
automatically for correlation with distributed traces.
"""
event_type: str
timestamp: str = ""
request_id: str = ""
trace_id: Optional[str] = None
span_id: Optional[str] = None
jsonrpc_id: Optional[str] = None
principal: AuditPrincipal = Field(default_factory=AuditPrincipal)
source: AuditSource = Field(default_factory=AuditSource)
action: AuditAction = Field(default_factory=AuditAction)
resource: AuditResource = Field(default_factory=AuditResource)
outcome: str = ""
duration_ms: Optional[float] = None
detail: str = ""
def to_jsonl(self) -> str:
return self.model_dump_json(exclude_none=True)
# ---------------------------------------------------------------------------
# Sink abstraction
# ---------------------------------------------------------------------------
class AuditSink(abc.ABC):
"""Base class for audit event sinks."""
@abc.abstractmethod
def emit(self, event: AuditEvent) -> None: ...
def close(self) -> None:
pass
class StdoutAuditSink(AuditSink):
"""Write JSONL events to stdout (the default)."""
def emit(self, event: AuditEvent) -> None:
sys.stdout.write(event.to_jsonl() + "\n")
sys.stdout.flush()
class FileAuditSink(AuditSink):
"""Append JSONL events to a local file."""
def __init__(self, file_path: str) -> None:
self._file_path = file_path
self._fh = open(file_path, "a", buffering=1)
def emit(self, event: AuditEvent) -> None:
self._fh.write(event.to_jsonl() + "\n")
def close(self) -> None:
self._fh.close()
class LoggerAuditSink(AuditSink):
"""Emit audit events through Python's ``logging`` module at INFO level."""
def __init__(self, logger_name: str = "feast.audit") -> None:
self._logger = logging.getLogger(logger_name)
def emit(self, event: AuditEvent) -> None:
self._logger.info(event.to_jsonl())
# ---------------------------------------------------------------------------
# AuditLogger — the main entry point
# ---------------------------------------------------------------------------
class AuditLogger:
"""Central audit logger that routes events to the configured sink.
Instantiate once during feature-server startup and share via
``app.state.audit_logger``.
"""
def __init__(
self,
sink: AuditSink,
*,
log_successful_reads: bool = True,
) -> None:
self._sink = sink
self._log_successful_reads = log_successful_reads
self._lock = threading.Lock()
# -- helpers -----------------------------------------------------------
@staticmethod
def new_request_id() -> str:
return str(uuid.uuid4())
@staticmethod
def _utcnow_iso() -> str:
return datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] + "Z"
@staticmethod
def _inject_otel_context(event: AuditEvent) -> None:
"""Populate trace_id/span_id from the active OpenTelemetry span, if any."""
if event.trace_id is not None:
return
try:
from opentelemetry import trace as otel_trace
span = otel_trace.get_current_span()
ctx = span.get_span_context()
if ctx and ctx.is_valid:
event.trace_id = format(ctx.trace_id, "032x")
event.span_id = format(ctx.span_id, "016x")
except ImportError:
pass
except Exception:
pass
# MCP tool names and REST paths that correspond to read operations.
_READ_TOOL_NAMES = frozenset(
{
"get_online_features",
"retrieve_online_documents",
"get_historical_features",
}
)
_READ_ACTIONS = frozenset({"READ_ONLINE", "READ_OFFLINE"})
@classmethod
def _is_read_event(cls, event: AuditEvent) -> bool:
"""Return ``True`` when *event* represents a successful read.
Checks both ``resource.actions`` (populated by REST middleware) and
``action.mcp_tool`` (populated by the MCP handler wrapper) so that
suppression via ``log_successful_reads=False`` works for both layers.
"""
if event.event_type not in {"mcp.tools.call", "http.request"}:
return False
resource_actions = set(event.resource.actions)
if resource_actions and resource_actions.issubset(cls._READ_ACTIONS):
return True
if event.action.mcp_tool in cls._READ_TOOL_NAMES:
return True
return False
# -- public API --------------------------------------------------------
def log(self, event: AuditEvent) -> None:
if not event.timestamp:
event.timestamp = self._utcnow_iso()
if not event.request_id:
event.request_id = self.new_request_id()
self._inject_otel_context(event)
if event.outcome == "success" and not self._log_successful_reads:
if self._is_read_event(event):
return
try:
with self._lock:
self._sink.emit(event)
except Exception:
logger.exception("Failed to emit audit event")
def log_mcp_call(
self,
*,
request_id: str,
tool_name: str,
path: str = "",
principal: Optional[AuditPrincipal] = None,
source: Optional[AuditSource] = None,
resource: Optional[AuditResource] = None,
outcome: str = "success",
duration_ms: Optional[float] = None,
detail: str = "",
) -> None:
self.log(
AuditEvent(
event_type="mcp.tools.call",
request_id=request_id,
principal=principal or AuditPrincipal(),
source=source or AuditSource(transport="mcp-http"),
action=AuditAction(mcp_tool=tool_name, path=path),
resource=resource or AuditResource(),
outcome=outcome,
duration_ms=duration_ms,
detail=detail,
)
)
def log_http_request(
self,
*,
request_id: str,
method: str,
path: str,
principal: Optional[AuditPrincipal] = None,
source: Optional[AuditSource] = None,
resource: Optional[AuditResource] = None,
outcome: str = "success",
duration_ms: Optional[float] = None,
status_code: int = 200,
) -> None:
self.log(
AuditEvent(
event_type="http.request",
request_id=request_id,
principal=principal or AuditPrincipal(),
source=source or AuditSource(transport="http"),
action=AuditAction(path=path),
resource=resource or AuditResource(),
outcome=outcome,
duration_ms=duration_ms,
detail=f"{method} {path} -> {status_code}",
)
)
def log_authn(
self,
*,
request_id: str,
outcome: str,
principal: Optional[AuditPrincipal] = None,
source: Optional[AuditSource] = None,
detail: str = "",
) -> None:
event_type = "authn.success" if outcome == "success" else "authn.failure"
self.log(
AuditEvent(
event_type=event_type,
request_id=request_id,
principal=principal or AuditPrincipal(),
source=source or AuditSource(),
outcome=outcome,
detail=detail,
)
)
def log_authz(
self,
*,
request_id: str,
outcome: str,
principal: Optional[AuditPrincipal] = None,
resource: Optional[AuditResource] = None,
detail: str = "",
) -> None:
self.log(
AuditEvent(
event_type="authz.decision",
request_id=request_id,
principal=principal or AuditPrincipal(),
resource=resource or AuditResource(),
outcome=outcome,
detail=detail,
)
)
def close(self) -> None:
self._sink.close()
# ---------------------------------------------------------------------------
# Factory
# ---------------------------------------------------------------------------
_SINK_FACTORIES = {
"stdout": lambda cfg: StdoutAuditSink(),
"file": lambda cfg: FileAuditSink(cfg.get("file_path", "feast_audit.log")),
"logger": lambda cfg: LoggerAuditSink(cfg.get("logger_name", "feast.audit")),
}
def create_audit_logger_from_config(
audit_cfg: Optional["AuditLoggingConfig"],
) -> Optional[AuditLogger]:
"""Build an ``AuditLogger`` from an ``AuditLoggingConfig`` pydantic model.
Returns ``None`` when audit logging is disabled.
"""
if audit_cfg is None or not getattr(audit_cfg, "enabled", False):
return None
sink_type = getattr(audit_cfg, "sink", "stdout")
raw: Dict[str, Any] = {}
if hasattr(audit_cfg, "file_path"):
raw["file_path"] = audit_cfg.file_path
if hasattr(audit_cfg, "logger_name"):
raw["logger_name"] = audit_cfg.logger_name
factory = _SINK_FACTORIES.get(sink_type)
if factory is None:
logger.warning("Unknown audit sink %r, falling back to stdout", sink_type)
factory = _SINK_FACTORIES["stdout"]
sink = factory(raw)
return AuditLogger(
sink,
log_successful_reads=getattr(audit_cfg, "log_successful_reads", True),
)