-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
925 lines (847 loc) · 34.7 KB
/
Copy pathapp.py
File metadata and controls
925 lines (847 loc) · 34.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
from __future__ import annotations
import csv
import importlib.util
import io
import logging
import os
import secrets
import shutil
import uuid
from contextlib import suppress
from datetime import datetime, timedelta
from pathlib import Path
from tempfile import SpooledTemporaryFile
from types import SimpleNamespace
from typing import Any, Dict, Mapping, Optional, Tuple
import jwt
from fastapi import Body, Depends, FastAPI, File, HTTPException, UploadFile
from fastapi.middleware.cors import CORSMiddleware
from fastapi.security import APIKeyHeader
from backend.api.evidence import router as evidence_router
from backend.api.graph import router as graph_router
from backend.api.provenance import router as provenance_router
from backend.api.risk import router as risk_router
from core.analytics import AnalyticsStore
from core.configuration import OverlayConfig, load_overlay
from core.enhanced_decision import EnhancedDecisionEngine
from core.feedback import FeedbackRecorder
from core.paths import ensure_secure_directory, verify_allowlisted_path
from core.storage import ArtefactArchive
from telemetry import configure as configure_telemetry
if importlib.util.find_spec("opentelemetry.instrumentation.fastapi"):
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
else: # pragma: no cover - fallback when instrumentation is unavailable
from telemetry.fastapi_noop import FastAPIInstrumentor
from .normalizers import (
InputNormalizer,
NormalizedBusinessContext,
NormalizedCNAPP,
NormalizedCVEFeed,
NormalizedSARIF,
NormalizedSBOM,
NormalizedVEX,
)
from .pipeline import PipelineOrchestrator
from .rate_limiter import create_rate_limiter
from .routes.enhanced import router as enhanced_router
from .upload_manager import ChunkUploadManager
logger = logging.getLogger(__name__)
JWT_ALGORITHM = "HS256"
JWT_EXP_MINUTES = int(os.getenv("FIXOPS_JWT_EXP_MINUTES", "120"))
_JWT_SECRET_FILE = Path(os.getenv("FIXOPS_DATA_DIR", ".fixops_data")) / ".jwt_secret"
def _load_or_generate_jwt_secret() -> str:
"""
Load JWT secret from environment or file, or generate and persist a new one.
Priority:
1. FIXOPS_JWT_SECRET environment variable
2. Persisted secret file
3. Generate new secret and persist to file (demo mode only)
Returns:
str: The JWT secret key
Raises:
ValueError: If no secret is available in non-demo mode
"""
# Priority 1: Environment variable
env_secret = os.getenv("FIXOPS_JWT_SECRET")
if env_secret:
logger.info("Using JWT secret from FIXOPS_JWT_SECRET environment variable")
return env_secret
# Priority 2: Persisted file
try:
_JWT_SECRET_FILE.parent.mkdir(parents=True, exist_ok=True)
if _JWT_SECRET_FILE.exists():
secret = _JWT_SECRET_FILE.read_text().strip()
if secret:
logger.info(f"Loaded persisted JWT secret from {_JWT_SECRET_FILE}")
return secret
except Exception as e:
logger.warning(f"Failed to read JWT secret file: {e}")
# Priority 3: Generate and persist (demo mode only)
mode = os.getenv("FIXOPS_MODE", "").lower()
if mode == "demo":
secret = secrets.token_hex(32)
try:
_JWT_SECRET_FILE.write_text(secret)
_JWT_SECRET_FILE.chmod(0o600) # Secure permissions
logger.warning(
f"Generated and persisted new JWT secret to {_JWT_SECRET_FILE}. "
"For production, set FIXOPS_JWT_SECRET environment variable."
)
return secret
except Exception as e:
logger.error(f"Failed to persist JWT secret: {e}")
logger.warning(
"Using non-persisted secret. Tokens will be invalid after restart."
)
return secret
else:
raise ValueError(
"FIXOPS_JWT_SECRET environment variable must be set in non-demo mode. "
"Generate one with: python -c 'import secrets; print(secrets.token_hex(32))'"
)
JWT_SECRET = _load_or_generate_jwt_secret()
def generate_access_token(data: Dict[str, Any]) -> str:
"""Generate a signed JWT access token with an expiry."""
exp = datetime.utcnow() + timedelta(minutes=JWT_EXP_MINUTES)
payload = {**data, "exp": exp}
token = jwt.encode(payload, JWT_SECRET, algorithm=JWT_ALGORITHM)
return token
def decode_access_token(token: str) -> Dict[str, Any]:
"""Decode and validate a JWT access token."""
try:
payload = jwt.decode(token, JWT_SECRET, algorithms=[JWT_ALGORITHM])
except jwt.ExpiredSignatureError as exc: # pragma: no cover - depends on wall clock
raise HTTPException(status_code=401, detail="Token expired") from exc
except jwt.InvalidTokenError as exc:
raise HTTPException(status_code=401, detail="Invalid token") from exc
return payload
def create_app() -> FastAPI:
"""Create the FastAPI application with file-upload ingestion endpoints."""
configure_telemetry(service_name="fixops-api")
app = FastAPI(title="FixOps Ingestion Demo API", version="0.1.0")
FastAPIInstrumentor.instrument_app(app)
if not hasattr(app, "state"):
app.state = SimpleNamespace()
origins_env = os.getenv("FIXOPS_ALLOWED_ORIGINS", "")
origins = [origin.strip() for origin in origins_env.split(",") if origin.strip()]
if not origins:
origins = ["https://core.ai"]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Add rate limiting middleware
rate_limit_enabled = os.getenv("FIXOPS_RATE_LIMIT_ENABLED", "true").lower() == "true"
rate_limit_requests = int(os.getenv("FIXOPS_RATE_LIMIT_REQUESTS", "100"))
rate_limit_window = int(os.getenv("FIXOPS_RATE_LIMIT_WINDOW_SECONDS", "60"))
app.add_middleware(
create_rate_limiter(
requests_per_window=rate_limit_requests,
window_seconds=rate_limit_window,
enabled=rate_limit_enabled
)
)
normalizer = InputNormalizer()
orchestrator = PipelineOrchestrator()
try:
overlay = load_overlay(allow_demo_token_fallback=True)
except TypeError:
overlay = load_overlay()
# API authentication setup
auth_strategy = overlay.auth.get("strategy", "").lower()
header_name = overlay.auth.get(
"header", "X-API-Key" if auth_strategy != "jwt" else "Authorization"
)
api_key_header = APIKeyHeader(name=header_name, auto_error=False)
expected_tokens = overlay.auth_tokens if auth_strategy == "token" else tuple()
async def _verify_api_key(api_key: Optional[str] = Depends(api_key_header)) -> None:
if auth_strategy == "token":
if not api_key or api_key not in expected_tokens:
raise HTTPException(
status_code=401, detail="Invalid or missing API token"
)
return
if auth_strategy == "jwt":
if not api_key:
raise HTTPException(
status_code=401, detail="Missing Authorization header"
)
token = api_key
if token.lower().startswith("bearer "):
token = token[7:].strip()
decode_access_token(token)
allowlist = overlay.allowed_data_roots or (Path("data").resolve(),)
for directory in overlay.data_directories.values():
secure_path = verify_allowlisted_path(directory, allowlist)
ensure_secure_directory(secure_path)
archive_dir = overlay.data_directories.get("archive_dir")
if archive_dir is None:
root = allowlist[0]
root = verify_allowlisted_path(root, allowlist)
archive_dir = (root / "archive" / overlay.mode).resolve()
archive_dir = verify_allowlisted_path(archive_dir, allowlist)
archive = ArtefactArchive(archive_dir, allowlist=allowlist)
analytics_dir = overlay.data_directories.get("analytics_dir")
if analytics_dir is None:
root = allowlist[0]
root = verify_allowlisted_path(root, allowlist)
analytics_dir = (root / "analytics" / overlay.mode).resolve()
analytics_dir = verify_allowlisted_path(analytics_dir, allowlist)
analytics_store = AnalyticsStore(analytics_dir, allowlist=allowlist)
provenance_dir = overlay.data_directories.get("provenance_dir")
if provenance_dir is None:
root = allowlist[0]
root = verify_allowlisted_path(root, allowlist)
provenance_dir = (root / "artifacts" / "attestations" / overlay.mode).resolve()
provenance_dir = verify_allowlisted_path(provenance_dir, allowlist)
provenance_dir = ensure_secure_directory(provenance_dir)
risk_dir = overlay.data_directories.get("risk_dir")
if risk_dir is None:
root = allowlist[0]
root = verify_allowlisted_path(root, allowlist)
risk_dir = (root / "artifacts").resolve()
risk_dir = verify_allowlisted_path(risk_dir, allowlist)
risk_dir = ensure_secure_directory(risk_dir)
app.state.normalizer = normalizer
app.state.orchestrator = orchestrator
app.state.artifacts: Dict[str, Any] = {}
app.state.overlay = overlay
app.state.archive = archive
app.state.archive_records: Dict[str, Dict[str, Any]] = {}
app.state.analytics_store = analytics_store
app.state.feedback = (
FeedbackRecorder(overlay, analytics_store=analytics_store)
if overlay.toggles.get("capture_feedback")
else None
)
app.state.enhanced_engine = EnhancedDecisionEngine(
overlay.enhanced_decision_settings
)
sbom_dir = overlay.data_directories.get("sbom_dir")
if sbom_dir is None:
root = allowlist[0]
root = verify_allowlisted_path(root, allowlist)
sbom_dir = (root / "artifacts" / "sbom").resolve()
sbom_dir = verify_allowlisted_path(sbom_dir, allowlist)
sbom_dir = ensure_secure_directory(sbom_dir)
graph_dir = overlay.data_directories.get("graph_dir")
if graph_dir is None:
root = allowlist[0]
root = verify_allowlisted_path(root, allowlist)
graph_dir = (root / "analysis").resolve()
graph_dir = verify_allowlisted_path(graph_dir, allowlist)
graph_dir = ensure_secure_directory(graph_dir)
evidence_dir = overlay.data_directories.get("evidence_dir")
if evidence_dir is None:
root = allowlist[0]
root = verify_allowlisted_path(root, allowlist)
evidence_dir = (root / "evidence").resolve()
evidence_dir = verify_allowlisted_path(evidence_dir, allowlist)
evidence_dir = ensure_secure_directory(evidence_dir)
evidence_manifest_dir = ensure_secure_directory(evidence_dir / "manifests")
evidence_bundle_dir = ensure_secure_directory(evidence_dir / "bundles")
app.state.provenance_dir = provenance_dir
app.state.risk_dir = risk_dir
app.state.sbom_dir = sbom_dir
app.state.graph_config = {
"repo_path": Path(".").resolve(),
"attestation_dir": provenance_dir,
"sbom_dir": sbom_dir,
"risk_dir": risk_dir,
"releases_path": graph_dir / "releases.json",
}
app.state.evidence_manifest_dir = evidence_manifest_dir
app.state.evidence_bundle_dir = evidence_bundle_dir
uploads_dir = overlay.data_directories.get("uploads_dir")
if uploads_dir is None:
root = allowlist[0]
uploads_dir = (root / "uploads" / overlay.mode).resolve()
uploads_dir = verify_allowlisted_path(uploads_dir, allowlist)
upload_manager = ChunkUploadManager(uploads_dir)
app.state.upload_manager = upload_manager
app.include_router(enhanced_router, dependencies=[Depends(_verify_api_key)])
app.include_router(provenance_router, dependencies=[Depends(_verify_api_key)])
app.include_router(risk_router, dependencies=[Depends(_verify_api_key)])
app.include_router(graph_router, dependencies=[Depends(_verify_api_key)])
app.include_router(evidence_router, dependencies=[Depends(_verify_api_key)])
_CHUNK_SIZE = 1024 * 1024
_RAW_BYTES_THRESHOLD = 4 * 1024 * 1024
async def _read_limited(
file: UploadFile, stage: str
) -> Tuple[SpooledTemporaryFile, int]:
"""Stream an upload into a spooled file respecting the configured limit."""
limit = overlay.upload_limit(stage)
total = 0
try:
buffer = SpooledTemporaryFile(max_size=_CHUNK_SIZE, mode="w+b")
while total < limit:
remaining = limit - total
chunk = await file.read(min(_CHUNK_SIZE, remaining))
if not chunk:
break
buffer.write(chunk)
total += len(chunk)
if total > limit:
raise HTTPException(
status_code=413,
detail={
"message": f"Upload for stage '{stage}' exceeded limit",
"max_bytes": limit,
},
)
except Exception:
buffer.close()
raise
buffer.seek(0)
return buffer, total
def _maybe_materialise_raw(
buffer: SpooledTemporaryFile,
total: int,
*,
threshold: int = _RAW_BYTES_THRESHOLD,
) -> Optional[bytes]:
if total > threshold:
return None
buffer.seek(0)
data = buffer.read()
buffer.seek(0)
return data
def _validate_content_type(file: UploadFile, expected: tuple[str, ...]) -> None:
if file.content_type and file.content_type not in expected:
raise HTTPException(
status_code=415,
detail={
"message": "Unsupported content type",
"received": file.content_type,
"expected": list(expected),
},
)
def _store(
stage: str,
payload: Any,
*,
original_filename: Optional[str] = None,
raw_bytes: Optional[bytes] = None,
) -> None:
logger.debug("Storing stage %s", stage)
app.state.artifacts[stage] = payload
try:
record = app.state.archive.persist(
stage,
payload,
original_filename=original_filename,
raw_bytes=raw_bytes,
)
except (
Exception
) as exc: # pragma: no cover - persistence must not break ingestion
logger.exception("Failed to persist artefact stage %s", stage)
record = {"stage": stage, "error": str(exc)}
app.state.archive_records[stage] = record
supported_stages = {
"design",
"sbom",
"sarif",
"cve",
"vex",
"cnapp",
"context",
}
def _process_design(
buffer: SpooledTemporaryFile, total: int, filename: str
) -> Dict[str, Any]:
text_stream = io.TextIOWrapper(
buffer, encoding="utf-8", errors="ignore", newline=""
)
try:
reader = csv.DictReader(text_stream)
rows = [
row
for row in reader
if any((value or "").strip() for value in row.values())
]
columns = reader.fieldnames or []
finally:
buffer = text_stream.detach()
if not rows:
raise HTTPException(status_code=400, detail="Design CSV contained no rows")
dataset = {"columns": columns, "rows": rows}
raw_bytes = _maybe_materialise_raw(buffer, total)
_store("design", dataset, original_filename=filename, raw_bytes=raw_bytes)
return {
"stage": "design",
"input_filename": filename,
"row_count": len(rows),
"columns": columns,
"data": dataset,
}
def _process_sbom(
buffer: SpooledTemporaryFile, total: int, filename: str
) -> Dict[str, Any]:
try:
sbom: NormalizedSBOM = normalizer.load_sbom(buffer)
except Exception as exc: # pragma: no cover - pass to FastAPI
logger.exception("SBOM normalisation failed")
raise HTTPException(
status_code=400, detail=f"Failed to parse SBOM: {exc}"
) from exc
raw_bytes = _maybe_materialise_raw(buffer, total)
_store("sbom", sbom, original_filename=filename, raw_bytes=raw_bytes)
return {
"stage": "sbom",
"input_filename": filename,
"metadata": sbom.metadata,
"component_preview": [
component.to_dict() for component in sbom.components[:5]
],
"format": sbom.format,
}
def _process_cve(
buffer: SpooledTemporaryFile, total: int, filename: str
) -> Dict[str, Any]:
try:
cve_feed: NormalizedCVEFeed = normalizer.load_cve_feed(buffer)
except Exception as exc: # pragma: no cover - FastAPI serialises
logger.exception("CVE feed normalisation failed")
raise HTTPException(
status_code=400, detail=f"Failed to parse CVE feed: {exc}"
) from exc
raw_bytes = _maybe_materialise_raw(buffer, total)
_store("cve", cve_feed, original_filename=filename, raw_bytes=raw_bytes)
return {
"stage": "cve",
"input_filename": filename,
"record_count": cve_feed.metadata.get("record_count", 0),
"validation_errors": cve_feed.errors,
}
def _process_vex(
buffer: SpooledTemporaryFile, total: int, filename: str
) -> Dict[str, Any]:
try:
vex_doc: NormalizedVEX = normalizer.load_vex(buffer)
except Exception as exc:
logger.exception("VEX normalisation failed")
raise HTTPException(
status_code=400, detail=f"Failed to parse VEX document: {exc}"
) from exc
raw_bytes = _maybe_materialise_raw(buffer, total)
_store("vex", vex_doc, original_filename=filename, raw_bytes=raw_bytes)
return {
"stage": "vex",
"input_filename": filename,
"assertions": vex_doc.metadata.get("assertion_count", 0),
"not_affected": len(vex_doc.suppressed_refs),
}
def _process_cnapp(
buffer: SpooledTemporaryFile, total: int, filename: str
) -> Dict[str, Any]:
try:
cnapp_payload: NormalizedCNAPP = normalizer.load_cnapp(buffer)
except Exception as exc:
logger.exception("CNAPP normalisation failed")
raise HTTPException(
status_code=400, detail=f"Failed to parse CNAPP payload: {exc}"
) from exc
raw_bytes = _maybe_materialise_raw(buffer, total)
_store("cnapp", cnapp_payload, original_filename=filename, raw_bytes=raw_bytes)
return {
"stage": "cnapp",
"input_filename": filename,
"asset_count": cnapp_payload.metadata.get(
"asset_count", len(cnapp_payload.assets)
),
"finding_count": cnapp_payload.metadata.get(
"finding_count", len(cnapp_payload.findings)
),
}
def _process_sarif(
buffer: SpooledTemporaryFile, total: int, filename: str
) -> Dict[str, Any]:
try:
sarif: NormalizedSARIF = normalizer.load_sarif(buffer)
except Exception as exc:
logger.exception("SARIF normalisation failed")
raise HTTPException(
status_code=400, detail=f"Failed to parse SARIF: {exc}"
) from exc
raw_bytes = _maybe_materialise_raw(buffer, total)
_store("sarif", sarif, original_filename=filename, raw_bytes=raw_bytes)
return {
"stage": "sarif",
"input_filename": filename,
"metadata": sarif.metadata,
"tools": sarif.tool_names,
}
def _process_context(
buffer: SpooledTemporaryFile,
total: int,
filename: str,
content_type: Optional[str] = None,
) -> Dict[str, Any]:
try:
context: NormalizedBusinessContext = normalizer.load_business_context(
buffer, content_type=content_type
)
except Exception as exc:
logger.exception("Business context normalisation failed")
raise HTTPException(
status_code=400, detail=f"Failed to parse business context: {exc}"
) from exc
raw_bytes = _maybe_materialise_raw(buffer, total)
_store("context", context, original_filename=filename, raw_bytes=raw_bytes)
return {
"stage": "context",
"input_filename": filename,
"format": context.format,
"ssvc_factors": context.ssvc,
"components": context.components,
}
def _process_from_buffer(
stage: str,
buffer: SpooledTemporaryFile,
total: int,
filename: str,
content_type: Optional[str] = None,
) -> Dict[str, Any]:
if stage == "design":
return _process_design(buffer, total, filename)
if stage == "sbom":
return _process_sbom(buffer, total, filename)
if stage == "cve":
return _process_cve(buffer, total, filename)
if stage == "vex":
return _process_vex(buffer, total, filename)
if stage == "cnapp":
return _process_cnapp(buffer, total, filename)
if stage == "sarif":
return _process_sarif(buffer, total, filename)
if stage == "context":
return _process_context(buffer, total, filename, content_type)
raise HTTPException(status_code=400, detail=f"Unsupported stage '{stage}'")
def _process_from_path(
stage: str, path: Path, filename: str, content_type: Optional[str] = None
) -> Dict[str, Any]:
buffer = SpooledTemporaryFile(max_size=_CHUNK_SIZE, mode="w+b")
try:
with path.open("rb") as handle:
shutil.copyfileobj(handle, buffer)
total = buffer.tell()
buffer.seek(0)
return _process_from_buffer(stage, buffer, total, filename, content_type)
finally:
with suppress(Exception):
buffer.close()
@app.post("/inputs/design", dependencies=[Depends(_verify_api_key)])
async def ingest_design(file: UploadFile = File(...)) -> Dict[str, Any]:
_validate_content_type(
file, ("text/csv", "application/vnd.ms-excel", "application/csv")
)
buffer, total = await _read_limited(file, "design")
try:
return _process_design(buffer, total, file.filename or "design.csv")
finally:
with suppress(Exception):
buffer.close()
@app.post("/inputs/sbom", dependencies=[Depends(_verify_api_key)])
async def ingest_sbom(file: UploadFile = File(...)) -> Dict[str, Any]:
_validate_content_type(
file,
(
"application/json",
"text/json",
"application/zip",
"application/x-zip-compressed",
"application/gzip",
),
)
buffer, total = await _read_limited(file, "sbom")
try:
return _process_sbom(buffer, total, file.filename or "sbom.json")
finally:
with suppress(Exception):
buffer.close()
@app.post("/inputs/cve", dependencies=[Depends(_verify_api_key)])
async def ingest_cve(file: UploadFile = File(...)) -> Dict[str, Any]:
_validate_content_type(
file,
(
"application/json",
"text/json",
"application/zip",
"application/x-zip-compressed",
"application/gzip",
),
)
buffer, total = await _read_limited(file, "cve")
try:
return _process_cve(buffer, total, file.filename or "cve.json")
finally:
with suppress(Exception):
buffer.close()
@app.post("/inputs/vex", dependencies=[Depends(_verify_api_key)])
async def ingest_vex(file: UploadFile = File(...)) -> Dict[str, Any]:
_validate_content_type(file, ("application/json", "text/json"))
buffer, total = await _read_limited(file, "vex")
try:
return _process_vex(buffer, total, file.filename or "vex.json")
finally:
with suppress(Exception):
buffer.close()
@app.post("/inputs/cnapp", dependencies=[Depends(_verify_api_key)])
async def ingest_cnapp(file: UploadFile = File(...)) -> Dict[str, Any]:
_validate_content_type(file, ("application/json", "text/json"))
buffer, total = await _read_limited(file, "cnapp")
try:
return _process_cnapp(buffer, total, file.filename or "cnapp.json")
finally:
with suppress(Exception):
buffer.close()
@app.post("/inputs/sarif", dependencies=[Depends(_verify_api_key)])
async def ingest_sarif(file: UploadFile = File(...)) -> Dict[str, Any]:
_validate_content_type(
file,
(
"application/json",
"text/json",
"application/zip",
"application/x-zip-compressed",
"application/gzip",
),
)
buffer, total = await _read_limited(file, "sarif")
try:
return _process_sarif(buffer, total, file.filename or "scan.sarif")
finally:
with suppress(Exception):
buffer.close()
@app.post("/inputs/context", dependencies=[Depends(_verify_api_key)])
async def ingest_context(file: UploadFile = File(...)) -> Dict[str, Any]:
_validate_content_type(
file,
(
"application/json",
"text/json",
"application/x-yaml",
"text/yaml",
"application/yaml",
"text/plain",
),
)
buffer, total = await _read_limited(file, "context")
try:
return _process_context(
buffer, total, file.filename or "context.yaml", file.content_type
)
finally:
with suppress(Exception):
buffer.close()
@app.post("/inputs/{stage}/chunks/start", dependencies=[Depends(_verify_api_key)])
async def initialise_chunk_upload(
stage: str, payload: Dict[str, Any] = Body(...)
) -> Dict[str, Any]:
if stage not in supported_stages:
raise HTTPException(
status_code=404, detail=f"Stage '{stage}' not recognised"
)
filename = str(
payload.get("file_name") or payload.get("filename") or f"{stage}.bin"
)
try:
total_bytes = (
int(payload.get("total_size"))
if payload.get("total_size") is not None
else None
)
except (TypeError, ValueError):
raise HTTPException(status_code=400, detail="total_size must be an integer")
checksum = payload.get("checksum")
content_type = payload.get("content_type")
session = upload_manager.create_session(
stage,
filename=filename,
total_bytes=total_bytes,
checksum=checksum,
content_type=content_type,
)
return {"status": "initialised", "session": session.to_dict()}
@app.put(
"/inputs/{stage}/chunks/{session_id}", dependencies=[Depends(_verify_api_key)]
)
async def upload_chunk(
stage: str,
session_id: str,
chunk: UploadFile = File(...),
offset: Optional[int] = None,
) -> Dict[str, Any]:
if stage not in supported_stages:
raise HTTPException(
status_code=404, detail=f"Stage '{stage}' not recognised"
)
# Validate offset parameter
if offset is not None and offset < 0:
raise HTTPException(
status_code=400,
detail=f"Invalid offset: {offset}. Offset must be non-negative."
)
data = await chunk.read()
try:
session = upload_manager.append_chunk(session_id, data, offset=offset)
except KeyError:
raise HTTPException(status_code=404, detail="Upload session not found")
except ValueError as exc:
raise HTTPException(status_code=400, detail=str(exc))
return {"status": "chunk_received", "session": session.to_dict()}
@app.post(
"/inputs/{stage}/chunks/{session_id}/complete",
dependencies=[Depends(_verify_api_key)],
)
async def complete_upload(stage: str, session_id: str) -> Dict[str, Any]:
if stage not in supported_stages:
raise HTTPException(
status_code=404, detail=f"Stage '{stage}' not recognised"
)
try:
session = upload_manager.finalise(session_id)
except KeyError:
raise HTTPException(status_code=404, detail="Upload session not found")
except ValueError as exc:
raise HTTPException(status_code=400, detail=str(exc))
path = session.path
if path is None:
raise HTTPException(status_code=500, detail="Upload payload missing")
response = _process_from_path(
stage, path, session.filename, session.content_type
)
response["upload_session"] = session.to_dict()
return response
@app.get(
"/inputs/{stage}/chunks/{session_id}", dependencies=[Depends(_verify_api_key)]
)
async def upload_status(stage: str, session_id: str) -> Dict[str, Any]:
if stage not in supported_stages:
raise HTTPException(
status_code=404, detail=f"Stage '{stage}' not recognised"
)
try:
session = upload_manager.status(session_id)
except KeyError:
raise HTTPException(status_code=404, detail="Upload session not found")
return {"status": "ok", "session": session.to_dict()}
@app.api_route(
"/pipeline/run",
methods=["GET", "POST"],
dependencies=[Depends(_verify_api_key)],
)
async def run_pipeline() -> Dict[str, Any]:
overlay: OverlayConfig = app.state.overlay
required = overlay.required_inputs
missing = [stage for stage in required if stage not in app.state.artifacts]
if missing:
raise HTTPException(
status_code=400,
detail={"message": "Missing required artefacts", "missing": missing},
)
if overlay.toggles.get("enforce_ticket_sync") and not overlay.jira.get(
"project_key"
):
raise HTTPException(
status_code=500,
detail={
"message": "Ticket synchronisation enforced but Jira project_key missing",
"integration": overlay.jira,
},
)
run_id = uuid.uuid4().hex
result = orchestrator.run(
design_dataset=app.state.artifacts.get(
"design", {"columns": [], "rows": []}
),
sbom=app.state.artifacts["sbom"],
sarif=app.state.artifacts["sarif"],
cve=app.state.artifacts["cve"],
overlay=overlay,
vex=app.state.artifacts.get("vex"),
cnapp=app.state.artifacts.get("cnapp"),
context=app.state.artifacts.get("context"),
)
result["run_id"] = run_id
analytics_store = getattr(app.state, "analytics_store", None)
if analytics_store is not None:
try:
persistence = analytics_store.persist_run(run_id, result)
except (
Exception
): # pragma: no cover - analytics persistence must not block pipeline
logger.exception(
"Failed to persist analytics artefacts for run %s", run_id
)
persistence = {}
if persistence:
result["analytics_persistence"] = persistence
analytics_section = result.get("analytics")
if isinstance(analytics_section, dict):
analytics_section["persistence"] = persistence
if app.state.archive_records:
result["artifact_archive"] = ArtefactArchive.summarise(
app.state.archive_records
)
app.state.archive_records = {}
if overlay.toggles.get("auto_attach_overlay_metadata", True):
result["overlay"] = overlay.to_sanitised_dict()
result["overlay"]["required_inputs"] = list(required)
return result
@app.get("/analytics/dashboard", dependencies=[Depends(_verify_api_key)])
async def analytics_dashboard(limit: int = 10) -> Dict[str, Any]:
store: Optional[AnalyticsStore] = getattr(app.state, "analytics_store", None)
if store is None:
raise HTTPException(
status_code=404,
detail="Analytics persistence disabled for this profile",
)
try:
return store.load_dashboard(limit=limit)
except ValueError as exc: # pragma: no cover - defensive guard
raise HTTPException(status_code=400, detail=str(exc)) from exc
@app.get("/analytics/runs/{run_id}", dependencies=[Depends(_verify_api_key)])
async def analytics_run(run_id: str) -> Dict[str, Any]:
store: Optional[AnalyticsStore] = getattr(app.state, "analytics_store", None)
if store is None:
raise HTTPException(
status_code=404,
detail="Analytics persistence disabled for this profile",
)
try:
data = store.load_run(run_id)
except ValueError as exc:
raise HTTPException(status_code=400, detail=str(exc)) from exc
has_content = bool(
data.get("forecasts")
or data.get("exploit_snapshots")
or data.get("ticket_metrics")
)
feedback_section = data.get("feedback")
if isinstance(feedback_section, Mapping):
has_content = has_content or bool(
feedback_section.get("events") or feedback_section.get("outcomes")
)
if not has_content:
raise HTTPException(
status_code=404, detail="No analytics persisted for run"
)
return data
@app.post("/feedback", dependencies=[Depends(_verify_api_key)])
async def submit_feedback(payload: Dict[str, Any]) -> Dict[str, Any]:
recorder: Optional[FeedbackRecorder] = app.state.feedback
if recorder is None:
raise HTTPException(
status_code=400, detail="Feedback capture disabled in this profile"
)
try:
entry = recorder.record(payload)
except ValueError as exc:
raise HTTPException(status_code=400, detail=str(exc)) from exc
return entry
return app