-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
163 lines (135 loc) · 5.15 KB
/
Copy pathmain.py
File metadata and controls
163 lines (135 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
from __future__ import annotations
import shutil
from pathlib import Path
from fastapi import FastAPI, File, HTTPException, UploadFile
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse
from .config import settings
from .db import init_db
from .repository import (
add_evidence,
add_note,
create_audit,
get_audit,
get_evidence_items,
get_findings,
get_notes,
list_audits,
)
from .schemas import (
AuditCreateRequest,
AuditCreateResponse,
AuditDetailResponse,
AuditSummaryResponse,
NoteCreateRequest,
TargetListRequest,
TargetListResponse,
)
from .services.gap_assistant import generate_follow_up_questions
from .services.target_generator import generate_target_list
from .services.worker import start_audit_thread
app = FastAPI(title=settings.app_name)
app.add_middleware(
CORSMiddleware,
allow_origins=settings.cors_origins if settings.cors_origins else ["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.on_event("startup")
def startup_event() -> None:
init_db()
@app.get("/health")
def health() -> dict[str, str]:
return {"status": "ok"}
@app.post("/api/audits", response_model=AuditCreateResponse)
def create_audit_endpoint(payload: AuditCreateRequest) -> AuditCreateResponse:
domain = payload.domain.strip().lower()
if "." not in domain:
raise HTTPException(status_code=400, detail="Enter a valid domain.")
audit_id = create_audit(domain=domain, company_name=payload.company_name)
start_audit_thread(audit_id)
return AuditCreateResponse(audit_id=audit_id, status="queued")
@app.get("/api/audits", response_model=list[AuditSummaryResponse])
def list_audits_endpoint() -> list[AuditSummaryResponse]:
return [AuditSummaryResponse(**row) for row in list_audits()]
@app.get("/api/audits/{audit_id}", response_model=AuditDetailResponse)
def get_audit_endpoint(audit_id: str) -> AuditDetailResponse:
audit = get_audit(audit_id)
if not audit:
raise HTTPException(status_code=404, detail="Audit not found.")
return AuditDetailResponse(
**audit,
findings=get_findings(audit_id),
evidence_items=get_evidence_items(audit_id),
notes=get_notes(audit_id),
)
@app.post("/api/audits/{audit_id}/evidence")
async def upload_evidence(audit_id: str, file: UploadFile = File(...)) -> dict[str, str]:
audit = get_audit(audit_id)
if not audit:
raise HTTPException(status_code=404, detail="Audit not found.")
target_dir = settings.upload_dir / audit_id
target_dir.mkdir(parents=True, exist_ok=True)
target_path = target_dir / file.filename
with target_path.open("wb") as handle:
shutil.copyfileobj(file.file, handle)
add_evidence(
audit_id=audit_id,
kind="upload",
filename=file.filename,
path=str(target_path),
content_type=file.content_type or "application/octet-stream",
)
return {"message": "Evidence uploaded."}
@app.post("/api/audits/{audit_id}/notes")
def create_note(audit_id: str, payload: NoteCreateRequest) -> dict[str, str]:
audit = get_audit(audit_id)
if not audit:
raise HTTPException(status_code=404, detail="Audit not found.")
add_note(audit_id=audit_id, source=payload.source, content=payload.content)
return {"message": "Note saved."}
@app.get("/api/audits/{audit_id}/gaps")
def get_gap_questions(audit_id: str) -> dict[str, list[str]]:
audit = get_audit(audit_id)
if not audit:
raise HTTPException(status_code=404, detail="Audit not found.")
findings = get_findings(audit_id)
evidence_items = get_evidence_items(audit_id)
notes = get_notes(audit_id)
questions = generate_follow_up_questions(audit, findings, evidence_items, notes)
return {"questions": questions}
@app.post("/api/targets/generate", response_model=TargetListResponse)
def generate_targets_endpoint(payload: TargetListRequest | None = None) -> TargetListResponse:
payload = payload or TargetListRequest()
target_list = generate_target_list(
primary_count=payload.primary_count,
secondary_count=payload.secondary_count,
use_ai=payload.use_ai,
)
return TargetListResponse(**target_list.to_dict())
@app.get("/api/targets", response_model=TargetListResponse)
def get_targets_endpoint(
primary_count: int = 20,
secondary_count: int = 15,
use_ai: bool = True,
) -> TargetListResponse:
target_list = generate_target_list(
primary_count=primary_count,
secondary_count=secondary_count,
use_ai=use_ai,
)
return TargetListResponse(**target_list.to_dict())
@app.get("/api/audits/{audit_id}/report")
def download_report(audit_id: str) -> FileResponse:
audit = get_audit(audit_id)
if not audit:
raise HTTPException(status_code=404, detail="Audit not found.")
report_path = audit.get("report_path")
if not report_path or not Path(report_path).exists():
raise HTTPException(status_code=404, detail="Report not generated yet.")
return FileResponse(
report_path,
media_type="text/markdown",
filename=f"{audit['domain']}-next-gen-it-report.md",
)