-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
146 lines (120 loc) · 4.21 KB
/
Copy pathmain.py
File metadata and controls
146 lines (120 loc) · 4.21 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
import logging
import time
import httpx
from fastapi import FastAPI, HTTPException, Query, Request
from fastapi.middleware.cors import CORSMiddleware
from slowapi import Limiter, _rate_limit_exceeded_handler
from slowapi.errors import RateLimitExceeded
from slowapi.util import get_remote_address
from constants import (
MSG_INITIAL_FETCH_FAILED,
MSG_MITIGATION_NOT_FOUND,
MSG_RECORD_NOT_FOUND,
MSG_REFRESH_FAILED,
REPO_URL,
SERVICE_NAME,
STANDARD_URL,
)
logger = logging.getLogger("ave-api")
logging.basicConfig(level=logging.INFO)
SOURCE_URL = (
"https://raw.githubusercontent.com/aveproject/ave/main/"
"dist/ave-records-latest.json"
)
REFRESH_INTERVAL_SECONDS = 15 * 60
_cache: dict = {"records": [], "fetched_at": 0.0}
limiter = Limiter(key_func=get_remote_address)
app = FastAPI(
title=SERVICE_NAME,
description="A read-only reference implementation of the AVE standard's record lookup API.",
)
app.state.limiter = limiter
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # public, read-only data, no reason to restrict
allow_methods=["GET"],
allow_headers=["*"],
)
@app.middleware("http")
async def add_security_headers(request: Request, call_next):
response = await call_next(request)
response.headers["X-Content-Type-Options"] = "nosniff"
response.headers["X-Frame-Options"] = "DENY"
response.headers["Referrer-Policy"] = "no-referrer"
return response
async def _refresh_cache() -> None:
async with httpx.AsyncClient(timeout=10) as client:
response = await client.get(SOURCE_URL)
response.raise_for_status()
records = response.json()
_cache["records"] = records
_cache["fetched_at"] = time.time()
async def _ensure_fresh() -> None:
if time.time() - _cache["fetched_at"] > REFRESH_INTERVAL_SECONDS:
try:
await _refresh_cache()
except Exception as e:
if not _cache["records"]:
# Never had a successful fetch at all; nothing to fall
# back to, this has to surface as a real failure.
raise
logger.warning(MSG_REFRESH_FAILED.format(error=e))
@app.on_event("startup")
async def startup() -> None:
try:
await _refresh_cache()
except Exception as e:
logger.error(MSG_INITIAL_FETCH_FAILED.format(error=e))
@app.get("/")
async def index() -> dict:
return {
"name": SERVICE_NAME,
"standard": STANDARD_URL,
"repo": REPO_URL,
"record_count": len(_cache["records"]),
"cache_age_seconds": round(time.time() - _cache["fetched_at"]),
"docs": "/docs",
}
@app.get("/records")
@limiter.limit("60/minute")
async def list_records(request: Request) -> list[dict]:
await _ensure_fresh()
return _cache["records"]
@app.get("/records/{ave_id}")
@limiter.limit("60/minute")
async def get_record(request: Request, ave_id: str) -> dict:
await _ensure_fresh()
for record in _cache["records"]:
if record.get("ave_id") == ave_id:
return record
raise HTTPException(
status_code=404, detail=MSG_RECORD_NOT_FOUND.format(ave_id=ave_id)
)
@app.get("/records/{ave_id}/mitigation")
@limiter.limit("60/minute")
async def get_mitigation(request: Request, ave_id: str) -> dict:
await _ensure_fresh()
for record in _cache["records"]:
if record.get("ave_id") == ave_id:
mitigation = record.get("mitigation")
if mitigation is None:
raise HTTPException(
status_code=404,
detail=MSG_MITIGATION_NOT_FOUND.format(ave_id=ave_id),
)
return mitigation
raise HTTPException(
status_code=404, detail=MSG_RECORD_NOT_FOUND.format(ave_id=ave_id)
)
@app.get("/search")
@limiter.limit("60/minute")
async def search(request: Request, q: str = Query(..., min_length=1)) -> list[dict]:
await _ensure_fresh()
needle = q.lower()
fields = ("title", "description", "attack_class", "behavioral_fingerprint")
return [
record
for record in _cache["records"]
if any(needle in str(record.get(field, "")).lower() for field in fields)
]