diff --git a/src/sbom_dt_dd_api.py b/src/sbom_dt_dd_api.py index 0242a1a..d8f8f0a 100644 --- a/src/sbom_dt_dd_api.py +++ b/src/sbom_dt_dd_api.py @@ -2,8 +2,9 @@ import os import json import yaml from loguru import logger -from fastapi import FastAPI, UploadFile, File, Form, HTTPException -from fastapi.responses import JSONResponse +from fastapi import FastAPI, UploadFile, File, Form, HTTPException, Request +from fastapi.responses import JSONResponse, HTMLResponse +from fastapi.templating import Jinja2Templates from converter import minimalSbomFormatConverter from sbom_dt_dd import generateSBOM, loadToDTrackAndDefectDojo, ApiException @@ -27,6 +28,100 @@ except KeyError as e: app.state.config = config +@app.get("/upload-form", response_class=HTMLResponse) +async def upload_form(request: Request): + """ + Route serving an HTML page with the upload form + """ + html_content = """ + + + + + Upload Minimal SBOM + + +

Upload Minimal SBOM

+
+
+

+ + +

+ + +
+ +
+ + + + + + """ + return HTMLResponse(content=html_content) + @app.post("/upload-minimal-sbom/") async def uploadMinimalSBOM( @@ -47,13 +142,38 @@ async def uploadMinimalSBOM( logger.info("Done.") except yaml.scanner.ScannerError as e: logger.warning(f"uploadMinimalSBOM, yaml ScannerError: {e.context=}, {e.context_mark=}, {e.problem=}, {e.problem_mark=}, {e.note=}") - raise HTTPException(status_code=400, detail=f"yaml ScannerError: {e.context=}, {e.context_mark=}, {e.problem=}, {e.problem_mark=}, {e.note=}") + raise HTTPException( + status_code=400, + detail={ + "error": "yaml ScannerError", + "context": e.context, + "context_mark": str(e.context_mark), + "problem": e.problem, + "problem_mark": str(e.problem_mark), + "note": e.note + } + ) except ApiException as e: logger.warning(f"uploadMinimalSBOM, ApiException: {type(e.cause)=}, {e.status=}, {e.reason=}, {e.body=}") - raise HTTPException(status_code=e.status, detail=f"{type(e.cause)=}, {e.reason=}, {e.body=}, {e.data=}") + raise HTTPException( + status_code=e.status, + detail={ + "type": str(type(e.cause)), + "reason": e.reason, + "body": e.body, + "data": e.data + } + ) except Exception as e: logger.warning(f"uploadMinimalSBOM, Exception: {type(e)=}, {str(e)=}") - raise HTTPException(status_code=500, detail=f"Exception: {type(e)=}, {str(e)=}") + raise HTTPException( + status_code=500, + detail={ + "error": "Exception occurred", + "type": str(type(e)), + "message": str(e) + } + ) return JSONResponse(content={ "message": "Upload successful!" @@ -81,13 +201,38 @@ async def uploadSBOM( logger.info("Done.") except json.decoder.JSONDecodeError as e: logger.warning(f"uploadSBOM, JSONDecodeError: {e.msg=}") - raise HTTPException(status_code=400, detail=f"JSON decoding error: {e.msg=}, {e.doc=}, {e.pos=}, {e.lineno=}, {e.colno=}") + raise HTTPException( + status_code=400, + detail={ + "error": "JSON decoding error", + "msg": e.msg, + "doc": e.doc, + "pos": e.pos, + "lineno": e.lineno, + "colno": e.colno + } + ) except ApiException as e: logger.warning(f"uploadSBOM, ApiException: {type(e.cause)=}, {e.status=}, {e.reason=}, {e.body=}") - raise HTTPException(status_code=e.status, detail=f"{type(e.cause)=}, {e.reason=}, {e.body=}, {e.data=}") + raise HTTPException( + status_code=e.status, + detail={ + "type": str(type(e.cause)), + "reason": e.reason, + "body": e.body, + "data": e.data + } + ) except Exception as e: logger.warning(f"uploadSBOM, Exception: {type(e)=}, {str(e)=}") - raise HTTPException(status_code=500, detail=f"Exception: {type(e)=}, {str(e)=}") + raise HTTPException( + status_code=500, + detail={ + "error": "Exception occurred", + "type": str(type(e)), + "message": str(e) + } + ) return JSONResponse(content={ "message": "Upload successful!"