mirror of
https://github.com/msoedov/agentic_security.git
synced 2026-09-30 11:51:46 +02:00
Merge pull request #338 from rwinkelman/fix/scan-route-logging-199
feat: add structured logging to scan API routes (#199)
This commit is contained in:
1 file changed
+23
-1
@@ -29,17 +29,25 @@ router = APIRouter()
|
|||||||
async def verify(
|
async def verify(
|
||||||
info: LLMInfo, secrets: InMemorySecrets = Depends(get_in_memory_secrets)
|
info: LLMInfo, secrets: InMemorySecrets = Depends(get_in_memory_secrets)
|
||||||
) -> dict[str, int | str | float]:
|
) -> dict[str, int | str | float]:
|
||||||
|
logger.info("verify: checking LLM spec connectivity")
|
||||||
spec = LLMSpec.from_string(info.spec)
|
spec = LLMSpec.from_string(info.spec)
|
||||||
try:
|
try:
|
||||||
r = await spec.verify()
|
r = await spec.verify()
|
||||||
except InvalidHTTPSpecError as e:
|
except InvalidHTTPSpecError as e:
|
||||||
|
logger.warning("verify: invalid HTTP spec: %s", e)
|
||||||
raise HTTPException(status_code=400, detail=str(e))
|
raise HTTPException(status_code=400, detail=str(e))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.exception(e)
|
logger.exception("verify: unexpected failure")
|
||||||
raise HTTPException(status_code=400, detail=str(e))
|
raise HTTPException(status_code=400, detail=str(e))
|
||||||
|
|
||||||
if r.status_code >= 400:
|
if r.status_code >= 400:
|
||||||
|
logger.warning("verify: upstream returned HTTP %s", r.status_code)
|
||||||
raise HTTPException(status_code=r.status_code, detail=r.text)
|
raise HTTPException(status_code=r.status_code, detail=r.text)
|
||||||
|
logger.info(
|
||||||
|
"verify: success status=%s elapsed=%.2fs",
|
||||||
|
r.status_code,
|
||||||
|
r.elapsed.total_seconds(),
|
||||||
|
)
|
||||||
return dict(
|
return dict(
|
||||||
status_code=r.status_code,
|
status_code=r.status_code,
|
||||||
body=r.text,
|
body=r.text,
|
||||||
@@ -70,6 +78,12 @@ async def scan(
|
|||||||
background_tasks: BackgroundTasks,
|
background_tasks: BackgroundTasks,
|
||||||
secrets: InMemorySecrets = Depends(get_in_memory_secrets),
|
secrets: InMemorySecrets = Depends(get_in_memory_secrets),
|
||||||
) -> StreamingResponse:
|
) -> StreamingResponse:
|
||||||
|
logger.info(
|
||||||
|
"scan: starting stream maxBudget=%s optimize=%s multiStep=%s",
|
||||||
|
scan_parameters.maxBudget,
|
||||||
|
scan_parameters.optimize,
|
||||||
|
scan_parameters.enableMultiStepAttack,
|
||||||
|
)
|
||||||
scan_parameters.with_secrets(secrets)
|
scan_parameters.with_secrets(secrets)
|
||||||
return StreamingResponse(
|
return StreamingResponse(
|
||||||
streaming_response_generator(scan_parameters), media_type="application/json"
|
streaming_response_generator(scan_parameters), media_type="application/json"
|
||||||
@@ -78,6 +92,7 @@ async def scan(
|
|||||||
|
|
||||||
@router.post("/stop")
|
@router.post("/stop")
|
||||||
async def stop_scan() -> dict[str, str]:
|
async def stop_scan() -> dict[str, str]:
|
||||||
|
logger.info("stop: scan stop requested")
|
||||||
get_stop_event().set()
|
get_stop_event().set()
|
||||||
return {"status": "Scan stopped"}
|
return {"status": "Scan stopped"}
|
||||||
|
|
||||||
@@ -103,8 +118,15 @@ async def scan_csv(
|
|||||||
{"name": dataset.dataset_name, "prompts": dataset.prompts}
|
{"name": dataset.dataset_name, "prompts": dataset.prompts}
|
||||||
)
|
)
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
|
logger.warning("scan-csv: failed to parse CSV upload: %s", e)
|
||||||
raise HTTPException(status_code=400, detail=str(e)) from e
|
raise HTTPException(status_code=400, detail=str(e)) from e
|
||||||
|
|
||||||
|
logger.info(
|
||||||
|
"scan-csv: starting stream rows=%s maxBudget=%s optimize=%s",
|
||||||
|
len(inline_datasets[0]["prompts"]) if inline_datasets else 0,
|
||||||
|
maxBudget,
|
||||||
|
optimize,
|
||||||
|
)
|
||||||
scan_parameters = Scan(
|
scan_parameters = Scan(
|
||||||
llmSpec=llm_spec,
|
llmSpec=llm_spec,
|
||||||
optimize=optimize,
|
optimize=optimize,
|
||||||
|
|||||||
Reference in new issue
Block a user