-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathkernelguard_api.py
More file actions
187 lines (152 loc) · 5.15 KB
/
kernelguard_api.py
File metadata and controls
187 lines (152 loc) · 5.15 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
from __future__ import annotations
import argparse
import importlib.metadata
from contextlib import contextmanager
from typing import Any
import kernelguard
def _get_version() -> str:
try:
return importlib.metadata.version("kernelguard")
except importlib.metadata.PackageNotFoundError:
return "dev"
@contextmanager
def _temporary_runtime_profile(profile: str):
normalized = profile or kernelguard.DEFAULT_PROFILE_NAME
current = str(
kernelguard.ACTIVE_RUNTIME_CONFIG.get("profile") or kernelguard.DEFAULT_PROFILE_NAME
)
if normalized == current:
yield
return
previous = kernelguard.ACTIVE_RUNTIME_CONFIG
kernelguard.configure_runtime(profile=normalized)
try:
yield
finally:
kernelguard.apply_runtime_config(previous)
_API_IMPORT_ERROR: Exception | None = None
try:
from starlette.applications import Starlette
from starlette.requests import Request
from starlette.responses import JSONResponse
from starlette.routing import Route
import uvicorn
except Exception as exc: # pragma: no cover
Starlette = None # type: ignore[assignment]
uvicorn = None # type: ignore[assignment]
_API_IMPORT_ERROR = exc
_INSTALL_HINT = (
'HTTP API support requires optional dependencies. '
'Install with `pip install "kernelguard[api]"`.'
)
_server_profile: str = kernelguard.DEFAULT_PROFILE_NAME
# ---------------------------------------------------------------------------
# Endpoints
# ---------------------------------------------------------------------------
async def health(request: Request) -> JSONResponse:
return JSONResponse({
"status": "ok",
"version": _get_version(),
"profile": _server_profile,
})
async def analyze(request: Request) -> JSONResponse:
try:
body = await request.json()
except Exception:
return JSONResponse({"error": "invalid JSON body"}, status_code=422)
if not isinstance(body, dict) or "code" not in body:
return JSONResponse({"error": "missing required field: code"}, status_code=400)
code = body["code"]
if not isinstance(code, str):
return JSONResponse({"error": "code must be a string"}, status_code=400)
metadata = body.get("metadata")
compute_structural_hash = bool(body.get("compute_structural_hash", False))
profile = body.get("profile", _server_profile)
try:
with _temporary_runtime_profile(profile):
result = kernelguard.analyze_code(
code,
metadata=metadata,
compute_structural_hash=compute_structural_hash,
)
except Exception as exc:
return JSONResponse({"error": f"analysis failed: {exc}"}, status_code=500)
return JSONResponse(result)
# ---------------------------------------------------------------------------
# App builder
# ---------------------------------------------------------------------------
def build_app() -> Starlette:
if Starlette is None:
if _API_IMPORT_ERROR is None:
raise RuntimeError(_INSTALL_HINT)
raise RuntimeError(
f"{_INSTALL_HINT} Original import error: {_API_IMPORT_ERROR}"
)
return Starlette(routes=[
Route("/health", health, methods=["GET"]),
Route("/analyze", analyze, methods=["POST"]),
])
# ---------------------------------------------------------------------------
# CLI
# ---------------------------------------------------------------------------
def _build_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
prog="kernelguard-api",
description="KernelGuard HTTP API server",
)
parser.add_argument(
"--version", "-V",
action="version",
version=f"kernelguard-api {_get_version()}",
)
parser.add_argument(
"--host",
default="127.0.0.1",
help="Bind address (default: %(default)s)",
)
parser.add_argument(
"--port",
type=int,
default=8088,
help="Bind port (default: %(default)s)",
)
parser.add_argument(
"--profile",
default=kernelguard.DEFAULT_PROFILE_NAME,
help="Default runtime profile (default: %(default)s)",
)
parser.add_argument(
"--workers",
type=int,
default=1,
help="Number of worker processes (default: %(default)s)",
)
return parser
def main(argv: list[str] | None = None) -> None:
global _server_profile
parser = _build_parser()
args = parser.parse_args(argv)
_server_profile = args.profile
if args.profile != kernelguard.DEFAULT_PROFILE_NAME:
kernelguard.configure_runtime(profile=args.profile)
try:
build_app()
except RuntimeError as exc:
raise SystemExit(str(exc)) from exc
uvicorn.run(
"kernelguard_api:_app_factory",
factory=True,
host=args.host,
port=args.port,
workers=args.workers,
log_level="info",
)
def _app_factory() -> Starlette:
"""Factory for uvicorn multiprocess mode — each worker builds its own app."""
return build_app()
__all__ = [
"analyze",
"build_app",
"health",
"main",
]